blob: 802f6d1ba99ad6c826d875b3fc483da074caff57 [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 Samsbc5c64b2015-04-16 15:13:52 -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
Derek Sollenbergereece0dd2014-02-27 14:31:29 -050027#include <SkBitmap.h>
Jason Samsffe9f482009-06-01 17:45:53 -070028
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080029#include <androidfw/Asset.h>
30#include <androidfw/AssetManager.h>
31#include <androidfw/ResourceTypes.h>
Jason Samsf29ca502009-06-23 12:22:47 -070032
Jason Samsd19f10d2009-05-22 14:03:28 -070033#include "jni.h"
34#include "JNIHelp.h"
35#include "android_runtime/AndroidRuntime.h"
Jim Milleree956052010-08-19 18:56:00 -070036#include "android_runtime/android_view_Surface.h"
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -080037#include "android_runtime/android_util_AssetManager.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070038
Jason Sams1d6983a2012-02-16 16:07:49 -080039#include <rs.h>
40#include <rsEnv.h>
Jason Samsfb9aa9f2012-03-28 15:30:07 -070041#include <gui/Surface.h>
Andy McFaddend47f7d82012-12-18 09:48:38 -080042#include <gui/GLConsumer.h>
Jason Samsfaa32b32011-06-20 16:58:04 -070043#include <android_runtime/android_graphics_SurfaceTexture.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070044
Steve Block3762c312012-01-06 19:20:56 +000045//#define LOG_API ALOGE
Andreas Gampe67333922014-11-10 20:35:59 -080046static constexpr bool kLogApi = false;
Yang Ni4e90b9b2015-04-30 16:13:54 -070047static constexpr size_t kMaxNumberArgsAndBindings = 1000;
48static constexpr size_t kMaxNumberClosuresInScriptGroup = 1000000;
49static constexpr size_t kMaxNumberKernelArguments = 256;
Jason Samsd19f10d2009-05-22 14:03:28 -070050
51using namespace android;
52
Andreas Gampe67333922014-11-10 20:35:59 -080053template <typename... T>
54void UNUSED(T... t) {}
55
Stephen Hines414fa2c2014-04-17 01:02:42 -070056#define PER_ARRAY_TYPE(flag, fnc, readonly, ...) { \
Jason Samse729a942013-11-06 11:22:02 -080057 jint len = 0; \
Chris Wailes488230c32014-08-14 11:22:40 -070058 void *ptr = nullptr; \
Miao Wang87e908d2015-03-02 15:15:15 -080059 void *srcPtr = nullptr; \
Jason Sams21659ac2013-11-06 15:08:07 -080060 size_t typeBytes = 0; \
Stephen Hines414fa2c2014-04-17 01:02:42 -070061 jint relFlag = 0; \
62 if (readonly) { \
63 /* The on-release mode should only be JNI_ABORT for read-only accesses. */ \
Miao Wang87e908d2015-03-02 15:15:15 -080064 /* readonly = true, also indicates we are copying to the allocation . */ \
Stephen Hines414fa2c2014-04-17 01:02:42 -070065 relFlag = JNI_ABORT; \
66 } \
Jason Samse729a942013-11-06 11:22:02 -080067 switch(dataType) { \
68 case RS_TYPE_FLOAT_32: \
69 len = _env->GetArrayLength((jfloatArray)data); \
70 ptr = _env->GetFloatArrayElements((jfloatArray)data, flag); \
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); \
Jason Sams21659ac2013-11-06 15:08:07 -080096 typeBytes = 8; \
Miao Wang87e908d2015-03-02 15:15:15 -080097 if (usePadding) { \
98 srcPtr = ptr; \
99 len = len / 3 * 4; \
100 if (count == 0) { \
101 count = len / 4; \
102 } \
103 ptr = malloc (len * typeBytes); \
104 if (readonly) { \
105 copyWithPadding(ptr, srcPtr, mSize, count); \
106 fnc(__VA_ARGS__); \
107 } else { \
108 fnc(__VA_ARGS__); \
109 copyWithUnPadding(srcPtr, ptr, mSize, count); \
110 } \
111 free(ptr); \
112 ptr = srcPtr; \
113 } else { \
114 fnc(__VA_ARGS__); \
115 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700116 _env->ReleaseDoubleArrayElements((jdoubleArray)data, (jdouble *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800117 return; \
118 case RS_TYPE_SIGNED_8: \
119 case RS_TYPE_UNSIGNED_8: \
120 len = _env->GetArrayLength((jbyteArray)data); \
121 ptr = _env->GetByteArrayElements((jbyteArray)data, flag); \
Jason Sams21659ac2013-11-06 15:08:07 -0800122 typeBytes = 1; \
Miao Wang87e908d2015-03-02 15:15:15 -0800123 if (usePadding) { \
124 srcPtr = ptr; \
125 len = len / 3 * 4; \
126 if (count == 0) { \
127 count = len / 4; \
128 } \
129 ptr = malloc (len * typeBytes); \
130 if (readonly) { \
131 copyWithPadding(ptr, srcPtr, mSize, count); \
132 fnc(__VA_ARGS__); \
133 } else { \
134 fnc(__VA_ARGS__); \
135 copyWithUnPadding(srcPtr, ptr, mSize, count); \
136 } \
137 free(ptr); \
138 ptr = srcPtr; \
139 } else { \
140 fnc(__VA_ARGS__); \
141 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700142 _env->ReleaseByteArrayElements((jbyteArray)data, (jbyte*)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800143 return; \
144 case RS_TYPE_SIGNED_16: \
145 case RS_TYPE_UNSIGNED_16: \
146 len = _env->GetArrayLength((jshortArray)data); \
147 ptr = _env->GetShortArrayElements((jshortArray)data, flag); \
Jason Sams21659ac2013-11-06 15:08:07 -0800148 typeBytes = 2; \
Miao Wang87e908d2015-03-02 15:15:15 -0800149 if (usePadding) { \
150 srcPtr = ptr; \
151 len = len / 3 * 4; \
152 if (count == 0) { \
153 count = len / 4; \
154 } \
155 ptr = malloc (len * typeBytes); \
156 if (readonly) { \
157 copyWithPadding(ptr, srcPtr, mSize, count); \
158 fnc(__VA_ARGS__); \
159 } else { \
160 fnc(__VA_ARGS__); \
161 copyWithUnPadding(srcPtr, ptr, mSize, count); \
162 } \
163 free(ptr); \
164 ptr = srcPtr; \
165 } else { \
166 fnc(__VA_ARGS__); \
167 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700168 _env->ReleaseShortArrayElements((jshortArray)data, (jshort *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800169 return; \
170 case RS_TYPE_SIGNED_32: \
171 case RS_TYPE_UNSIGNED_32: \
172 len = _env->GetArrayLength((jintArray)data); \
173 ptr = _env->GetIntArrayElements((jintArray)data, flag); \
Jason Sams21659ac2013-11-06 15:08:07 -0800174 typeBytes = 4; \
Miao Wang87e908d2015-03-02 15:15:15 -0800175 if (usePadding) { \
176 srcPtr = ptr; \
177 len = len / 3 * 4; \
178 if (count == 0) { \
179 count = len / 4; \
180 } \
181 ptr = malloc (len * typeBytes); \
182 if (readonly) { \
183 copyWithPadding(ptr, srcPtr, mSize, count); \
184 fnc(__VA_ARGS__); \
185 } else { \
186 fnc(__VA_ARGS__); \
187 copyWithUnPadding(srcPtr, ptr, mSize, count); \
188 } \
189 free(ptr); \
190 ptr = srcPtr; \
191 } else { \
192 fnc(__VA_ARGS__); \
193 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700194 _env->ReleaseIntArrayElements((jintArray)data, (jint *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800195 return; \
196 case RS_TYPE_SIGNED_64: \
197 case RS_TYPE_UNSIGNED_64: \
198 len = _env->GetArrayLength((jlongArray)data); \
199 ptr = _env->GetLongArrayElements((jlongArray)data, flag); \
Jason Sams21659ac2013-11-06 15:08:07 -0800200 typeBytes = 8; \
Miao Wang87e908d2015-03-02 15:15:15 -0800201 if (usePadding) { \
202 srcPtr = ptr; \
203 len = len / 3 * 4; \
204 if (count == 0) { \
205 count = len / 4; \
206 } \
207 ptr = malloc (len * typeBytes); \
208 if (readonly) { \
209 copyWithPadding(ptr, srcPtr, mSize, count); \
210 fnc(__VA_ARGS__); \
211 } else { \
212 fnc(__VA_ARGS__); \
213 copyWithUnPadding(srcPtr, ptr, mSize, count); \
214 } \
215 free(ptr); \
216 ptr = srcPtr; \
217 } else { \
218 fnc(__VA_ARGS__); \
219 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700220 _env->ReleaseLongArrayElements((jlongArray)data, (jlong *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800221 return; \
222 default: \
223 break; \
224 } \
Miao Wang87e908d2015-03-02 15:15:15 -0800225 UNUSED(len, ptr, srcPtr, typeBytes, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800226}
227
228
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800229class AutoJavaStringToUTF8 {
230public:
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800231 AutoJavaStringToUTF8(JNIEnv* env, jstring str) : fEnv(env), fJStr(str) {
Chris Wailes488230c32014-08-14 11:22:40 -0700232 fCStr = env->GetStringUTFChars(str, nullptr);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800233 fLength = env->GetStringUTFLength(str);
234 }
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800235 ~AutoJavaStringToUTF8() {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800236 fEnv->ReleaseStringUTFChars(fJStr, fCStr);
237 }
238 const char* c_str() const { return fCStr; }
239 jsize length() const { return fLength; }
240
241private:
242 JNIEnv* fEnv;
243 jstring fJStr;
244 const char* fCStr;
245 jsize fLength;
246};
247
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800248class AutoJavaStringArrayToUTF8 {
249public:
250 AutoJavaStringArrayToUTF8(JNIEnv* env, jobjectArray strings, jsize stringsLength)
251 : mEnv(env), mStrings(strings), mStringsLength(stringsLength) {
Chris Wailes488230c32014-08-14 11:22:40 -0700252 mCStrings = nullptr;
253 mSizeArray = nullptr;
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800254 if (stringsLength > 0) {
255 mCStrings = (const char **)calloc(stringsLength, sizeof(char *));
256 mSizeArray = (size_t*)calloc(stringsLength, sizeof(size_t));
257 for (jsize ct = 0; ct < stringsLength; ct ++) {
258 jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
Chris Wailes488230c32014-08-14 11:22:40 -0700259 mCStrings[ct] = mEnv->GetStringUTFChars(s, nullptr);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800260 mSizeArray[ct] = mEnv->GetStringUTFLength(s);
261 }
262 }
263 }
264 ~AutoJavaStringArrayToUTF8() {
265 for (jsize ct=0; ct < mStringsLength; ct++) {
266 jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
267 mEnv->ReleaseStringUTFChars(s, mCStrings[ct]);
268 }
269 free(mCStrings);
270 free(mSizeArray);
271 }
272 const char **c_str() const { return mCStrings; }
273 size_t *c_str_len() const { return mSizeArray; }
274 jsize length() const { return mStringsLength; }
275
276private:
277 JNIEnv *mEnv;
278 jobjectArray mStrings;
279 const char **mCStrings;
280 size_t *mSizeArray;
281 jsize mStringsLength;
282};
283
Jason Samsd19f10d2009-05-22 14:03:28 -0700284// ---------------------------------------------------------------------------
285
Jason Samsffe9f482009-06-01 17:45:53 -0700286static jfieldID gContextId = 0;
287static jfieldID gNativeBitmapID = 0;
Jason Samsd19f10d2009-05-22 14:03:28 -0700288
289static void _nInit(JNIEnv *_env, jclass _this)
290{
Tim Murrayeff663f2013-11-15 13:08:30 -0800291 gContextId = _env->GetFieldID(_this, "mContext", "J");
Jason Samsffe9f482009-06-01 17:45:53 -0700292
293 jclass bitmapClass = _env->FindClass("android/graphics/Bitmap");
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000294 gNativeBitmapID = _env->GetFieldID(bitmapClass, "mNativeBitmap", "J");
Jason Samsd19f10d2009-05-22 14:03:28 -0700295}
296
Jason Samsd19f10d2009-05-22 14:03:28 -0700297// ---------------------------------------------------------------------------
298
Miao Wang87e908d2015-03-02 15:15:15 -0800299static void copyWithPadding(void* ptr, void* srcPtr, int mSize, int count) {
300 int sizeBytesPad = mSize * 4;
301 int sizeBytes = mSize * 3;
302 uint8_t *dst = static_cast<uint8_t *>(ptr);
303 uint8_t *src = static_cast<uint8_t *>(srcPtr);
304 for (int i = 0; i < count; i++) {
305 memcpy(dst, src, sizeBytes);
306 dst += sizeBytesPad;
307 src += sizeBytes;
308 }
309}
310
311static void copyWithUnPadding(void* ptr, void* srcPtr, int mSize, int count) {
312 int sizeBytesPad = mSize * 4;
313 int sizeBytes = mSize * 3;
314 uint8_t *dst = static_cast<uint8_t *>(ptr);
315 uint8_t *src = static_cast<uint8_t *>(srcPtr);
316 for (int i = 0; i < count; i++) {
317 memcpy(dst, src, sizeBytes);
318 dst += sizeBytes;
319 src += sizeBytesPad;
320 }
321}
322
323
324// ---------------------------------------------------------------------------
Jason Sams3eaa338e2009-06-10 15:04:38 -0700325static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800326nContextFinish(JNIEnv *_env, jobject _this, jlong con)
Jason Sams96ed4cf2010-06-15 12:15:57 -0700327{
Andreas Gampe67333922014-11-10 20:35:59 -0800328 if (kLogApi) {
329 ALOGD("nContextFinish, con(%p)", (RsContext)con);
330 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800331 rsContextFinish((RsContext)con);
Jason Sams96ed4cf2010-06-15 12:15:57 -0700332}
333
Yang Ni281c3252014-10-24 08:52:24 -0700334static jlong
335nClosureCreate(JNIEnv *_env, jobject _this, jlong con, jlong kernelID,
336 jlong returnValue, jlongArray fieldIDArray,
337 jlongArray valueArray, jintArray sizeArray,
338 jlongArray depClosureArray, jlongArray depFieldIDArray) {
Yang Ni4e90b9b2015-04-30 16:13:54 -0700339 jlong ret = 0;
340
Yang Ni281c3252014-10-24 08:52:24 -0700341 jlong* jFieldIDs = _env->GetLongArrayElements(fieldIDArray, nullptr);
342 jsize fieldIDs_length = _env->GetArrayLength(fieldIDArray);
Yang Ni281c3252014-10-24 08:52:24 -0700343 jlong* jValues = _env->GetLongArrayElements(valueArray, nullptr);
344 jsize values_length = _env->GetArrayLength(valueArray);
Yang Ni4e90b9b2015-04-30 16:13:54 -0700345 jint* jSizes = _env->GetIntArrayElements(sizeArray, nullptr);
Yang Ni281c3252014-10-24 08:52:24 -0700346 jsize sizes_length = _env->GetArrayLength(sizeArray);
Yang Ni281c3252014-10-24 08:52:24 -0700347 jlong* jDepClosures =
348 _env->GetLongArrayElements(depClosureArray, nullptr);
349 jsize depClosures_length = _env->GetArrayLength(depClosureArray);
Yang Ni281c3252014-10-24 08:52:24 -0700350 jlong* jDepFieldIDs =
351 _env->GetLongArrayElements(depFieldIDArray, nullptr);
352 jsize depFieldIDs_length = _env->GetArrayLength(depFieldIDArray);
Yang Ni4e90b9b2015-04-30 16:13:54 -0700353
354 size_t numValues, numDependencies;
355 RsScriptFieldID* fieldIDs;
356 uintptr_t* values;
357 RsClosure* depClosures;
358 RsScriptFieldID* depFieldIDs;
359
360 if (fieldIDs_length != values_length || values_length != sizes_length) {
361 ALOGE("Unmatched field IDs, values, and sizes in closure creation.");
362 goto exit;
363 }
364
365 numValues = (size_t)fieldIDs_length;
366
367 if (depClosures_length != depFieldIDs_length) {
368 ALOGE("Unmatched closures and field IDs for dependencies in closure creation.");
369 goto exit;
370 }
371
372 numDependencies = (size_t)depClosures_length;
373
374 if (numDependencies > numValues) {
375 ALOGE("Unexpected number of dependencies in closure creation");
376 goto exit;
377 }
378
379 if (numValues > kMaxNumberArgsAndBindings) {
380 ALOGE("Too many arguments or globals in closure creation");
381 goto exit;
382 }
383
384 fieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numValues);
385 if (fieldIDs == nullptr) {
386 goto exit;
387 }
388
389 for (size_t i = 0; i < numValues; i++) {
390 fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i];
391 }
392
393 values = (uintptr_t*)alloca(sizeof(uintptr_t) * numValues);
394 if (values == nullptr) {
395 goto exit;
396 }
397
398 for (size_t i = 0; i < numValues; i++) {
399 values[i] = (uintptr_t)jValues[i];
400 }
401
402 depClosures = (RsClosure*)alloca(sizeof(RsClosure) * numDependencies);
403 if (depClosures == nullptr) {
404 goto exit;
405 }
406
407 for (size_t i = 0; i < numDependencies; i++) {
408 depClosures[i] = (RsClosure)jDepClosures[i];
409 }
410
411 depFieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numDependencies);
412 if (depFieldIDs == nullptr) {
413 goto exit;
414 }
415
416 for (size_t i = 0; i < numDependencies; i++) {
Yang Ni281c3252014-10-24 08:52:24 -0700417 depFieldIDs[i] = (RsClosure)jDepFieldIDs[i];
418 }
419
Yang Ni4e90b9b2015-04-30 16:13:54 -0700420 ret = (jlong)(uintptr_t)rsClosureCreate(
Yang Ni281c3252014-10-24 08:52:24 -0700421 (RsContext)con, (RsScriptKernelID)kernelID, (RsAllocation)returnValue,
Yang Ni4e90b9b2015-04-30 16:13:54 -0700422 fieldIDs, numValues, values, numValues,
423 (int*)jSizes, numValues,
424 depClosures, numDependencies,
425 depFieldIDs, numDependencies);
426
427exit:
428
429 _env->ReleaseLongArrayElements(depFieldIDArray, jDepFieldIDs, JNI_ABORT);
430 _env->ReleaseLongArrayElements(depClosureArray, jDepClosures, JNI_ABORT);
431 _env->ReleaseIntArrayElements (sizeArray, jSizes, JNI_ABORT);
432 _env->ReleaseLongArrayElements(valueArray, jValues, JNI_ABORT);
433 _env->ReleaseLongArrayElements(fieldIDArray, jFieldIDs, JNI_ABORT);
434
435 return ret;
Yang Ni281c3252014-10-24 08:52:24 -0700436}
437
Yang Nibe392ad2015-01-23 17:16:02 -0800438static jlong
439nInvokeClosureCreate(JNIEnv *_env, jobject _this, jlong con, jlong invokeID,
440 jbyteArray paramArray, jlongArray fieldIDArray, jlongArray valueArray,
441 jintArray sizeArray) {
Yang Ni4e90b9b2015-04-30 16:13:54 -0700442 jlong ret = 0;
443
Yang Nibe392ad2015-01-23 17:16:02 -0800444 jbyte* jParams = _env->GetByteArrayElements(paramArray, nullptr);
445 jsize jParamLength = _env->GetArrayLength(paramArray);
Yang Nibe392ad2015-01-23 17:16:02 -0800446 jlong* jFieldIDs = _env->GetLongArrayElements(fieldIDArray, nullptr);
447 jsize fieldIDs_length = _env->GetArrayLength(fieldIDArray);
Yang Ni4e90b9b2015-04-30 16:13:54 -0700448 jlong* jValues = _env->GetLongArrayElements(valueArray, nullptr);
449 jsize values_length = _env->GetArrayLength(valueArray);
450 jint* jSizes = _env->GetIntArrayElements(sizeArray, nullptr);
451 jsize sizes_length = _env->GetArrayLength(sizeArray);
452
453 size_t numValues;
454 RsScriptFieldID* fieldIDs;
455 uintptr_t* values;
456
457 if (fieldIDs_length != values_length || values_length != sizes_length) {
458 ALOGE("Unmatched field IDs, values, and sizes in closure creation.");
459 goto exit;
460 }
461
462 numValues = (size_t) fieldIDs_length;
463
464 if (numValues > kMaxNumberArgsAndBindings) {
465 ALOGE("Too many arguments or globals in closure creation");
466 goto exit;
467 }
468
469 fieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numValues);
470 if (fieldIDs == nullptr) {
471 goto exit;
472 }
473
474 for (size_t i = 0; i< numValues; i++) {
Yang Nibe392ad2015-01-23 17:16:02 -0800475 fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i];
476 }
477
Yang Ni4e90b9b2015-04-30 16:13:54 -0700478 values = (uintptr_t*)alloca(sizeof(uintptr_t) * numValues);
479 if (values == nullptr) {
480 goto exit;
481 }
482
483 for (size_t i = 0; i < numValues; i++) {
Yang Nibe392ad2015-01-23 17:16:02 -0800484 values[i] = (uintptr_t)jValues[i];
485 }
486
Yang Ni4e90b9b2015-04-30 16:13:54 -0700487 ret = (jlong)(uintptr_t)rsInvokeClosureCreate(
Yang Nibe392ad2015-01-23 17:16:02 -0800488 (RsContext)con, (RsScriptInvokeID)invokeID, jParams, jParamLength,
Yang Ni4e90b9b2015-04-30 16:13:54 -0700489 fieldIDs, numValues, values, numValues,
490 (int*)jSizes, numValues);
491
492exit:
493
494 _env->ReleaseIntArrayElements (sizeArray, jSizes, JNI_ABORT);
495 _env->ReleaseLongArrayElements(valueArray, jValues, JNI_ABORT);
496 _env->ReleaseLongArrayElements(fieldIDArray, jFieldIDs, JNI_ABORT);
497 _env->ReleaseByteArrayElements(paramArray, jParams, JNI_ABORT);
498
499 return ret;
Yang Nibe392ad2015-01-23 17:16:02 -0800500}
501
Yang Ni281c3252014-10-24 08:52:24 -0700502static void
503nClosureSetArg(JNIEnv *_env, jobject _this, jlong con, jlong closureID,
504 jint index, jlong value, jint size) {
505 rsClosureSetArg((RsContext)con, (RsClosure)closureID, (uint32_t)index,
506 (uintptr_t)value, (size_t)size);
507}
508
509static void
510nClosureSetGlobal(JNIEnv *_env, jobject _this, jlong con, jlong closureID,
511 jlong fieldID, jlong value, jint size) {
512 rsClosureSetGlobal((RsContext)con, (RsClosure)closureID,
513 (RsScriptFieldID)fieldID, (uintptr_t)value, (size_t)size);
514}
515
516static long
Yang Ni35be56c2015-04-02 17:47:56 -0700517nScriptGroup2Create(JNIEnv *_env, jobject _this, jlong con, jstring name,
Yang Niebf63402015-01-16 11:06:26 -0800518 jstring cacheDir, jlongArray closureArray) {
Yang Ni4e90b9b2015-04-30 16:13:54 -0700519 jlong ret = 0;
520
Yang Ni35be56c2015-04-02 17:47:56 -0700521 AutoJavaStringToUTF8 nameUTF(_env, name);
Yang Niebf63402015-01-16 11:06:26 -0800522 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
523
Yang Ni281c3252014-10-24 08:52:24 -0700524 jlong* jClosures = _env->GetLongArrayElements(closureArray, nullptr);
525 jsize numClosures = _env->GetArrayLength(closureArray);
Yang Ni4e90b9b2015-04-30 16:13:54 -0700526
527 RsClosure* closures;
528
529 if (numClosures > (jsize) kMaxNumberClosuresInScriptGroup) {
530 ALOGE("Too many closures in script group");
531 goto exit;
532 }
533
534 closures = (RsClosure*)alloca(sizeof(RsClosure) * numClosures);
535 if (closures == nullptr) {
536 goto exit;
537 }
538
Yang Ni281c3252014-10-24 08:52:24 -0700539 for (int i = 0; i < numClosures; i++) {
540 closures[i] = (RsClosure)jClosures[i];
541 }
542
Yang Ni4e90b9b2015-04-30 16:13:54 -0700543 ret = (jlong)(uintptr_t)rsScriptGroup2Create(
Yang Ni35be56c2015-04-02 17:47:56 -0700544 (RsContext)con, nameUTF.c_str(), nameUTF.length(),
545 cacheDirUTF.c_str(), cacheDirUTF.length(),
Yang Niebf63402015-01-16 11:06:26 -0800546 closures, numClosures);
Yang Ni4e90b9b2015-04-30 16:13:54 -0700547
548exit:
549
550 _env->ReleaseLongArrayElements(closureArray, jClosures, JNI_ABORT);
551
552 return ret;
Yang Ni281c3252014-10-24 08:52:24 -0700553}
554
555static void
556nScriptGroup2Execute(JNIEnv *_env, jobject _this, jlong con, jlong groupID) {
557 rsScriptGroupExecute((RsContext)con, (RsScriptGroup2)groupID);
558}
559
Jason Sams96ed4cf2010-06-15 12:15:57 -0700560static void
Tim Murray25207df2015-01-12 16:47:56 -0800561nScriptIntrinsicBLAS_Single(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
562 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
563 jfloat alpha, jlong A, jlong B, jfloat beta, jlong C, jint incX, jint incY,
564 jint KL, jint KU) {
565 RsBlasCall call;
566 memset(&call, 0, sizeof(call));
567 call.func = (RsBlasFunction)func;
568 call.transA = (RsBlasTranspose)TransA;
569 call.transB = (RsBlasTranspose)TransB;
570 call.side = (RsBlasSide)Side;
571 call.uplo = (RsBlasUplo)Uplo;
572 call.diag = (RsBlasDiag)Diag;
573 call.M = M;
574 call.N = N;
575 call.K = K;
576 call.alpha.f = alpha;
577 call.beta.f = beta;
578 call.incX = incX;
579 call.incY = incY;
580 call.KL = KL;
581 call.KU = KU;
582
583 RsAllocation in_allocs[3];
584 in_allocs[0] = (RsAllocation)A;
585 in_allocs[1] = (RsAllocation)B;
586 in_allocs[2] = (RsAllocation)C;
587
588 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
589 in_allocs, sizeof(in_allocs), nullptr,
590 &call, sizeof(call), nullptr, 0);
591}
592
593static void
594nScriptIntrinsicBLAS_Double(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
595 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
596 jdouble alpha, jlong A, jlong B, jdouble beta, jlong C, jint incX, jint incY,
597 jint KL, jint KU) {
598 RsBlasCall call;
599 memset(&call, 0, sizeof(call));
600 call.func = (RsBlasFunction)func;
601 call.transA = (RsBlasTranspose)TransA;
602 call.transB = (RsBlasTranspose)TransB;
603 call.side = (RsBlasSide)Side;
604 call.uplo = (RsBlasUplo)Uplo;
605 call.diag = (RsBlasDiag)Diag;
606 call.M = M;
607 call.N = N;
608 call.K = K;
609 call.alpha.d = alpha;
610 call.beta.d = beta;
611 call.incX = incX;
612 call.incY = incY;
613 call.KL = KL;
614 call.KU = KU;
615
616 RsAllocation in_allocs[3];
617 in_allocs[0] = (RsAllocation)A;
618 in_allocs[1] = (RsAllocation)B;
619 in_allocs[2] = (RsAllocation)C;
620
621 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
622 in_allocs, sizeof(in_allocs), nullptr,
623 &call, sizeof(call), nullptr, 0);
624}
625
626static void
627nScriptIntrinsicBLAS_Complex(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
628 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
629 jfloat alphaX, jfloat alphaY, jlong A, jlong B, jfloat betaX,
630 jfloat betaY, jlong C, jint incX, jint incY, jint KL, jint KU) {
631 RsBlasCall call;
632 memset(&call, 0, sizeof(call));
633 call.func = (RsBlasFunction)func;
634 call.transA = (RsBlasTranspose)TransA;
635 call.transB = (RsBlasTranspose)TransB;
636 call.side = (RsBlasSide)Side;
637 call.uplo = (RsBlasUplo)Uplo;
638 call.diag = (RsBlasDiag)Diag;
639 call.M = M;
640 call.N = N;
641 call.K = K;
642 call.alpha.c.r = alphaX;
643 call.alpha.c.i = alphaY;
644 call.beta.c.r = betaX;
645 call.beta.c.r = betaY;
646 call.incX = incX;
647 call.incY = incY;
648 call.KL = KL;
649 call.KU = KU;
650
651 RsAllocation in_allocs[3];
652 in_allocs[0] = (RsAllocation)A;
653 in_allocs[1] = (RsAllocation)B;
654 in_allocs[2] = (RsAllocation)C;
655
656 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
657 in_allocs, sizeof(in_allocs), nullptr,
658 &call, sizeof(call), nullptr, 0);
659}
660
661static void
662nScriptIntrinsicBLAS_Z(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
663 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
664 jdouble alphaX, jdouble alphaY, jlong A, jlong B, jdouble betaX,
665 jdouble betaY, jlong C, jint incX, jint incY, jint KL, jint KU) {
666 RsBlasCall call;
667 memset(&call, 0, sizeof(call));
668 call.func = (RsBlasFunction)func;
669 call.transA = (RsBlasTranspose)TransA;
670 call.transB = (RsBlasTranspose)TransB;
671 call.side = (RsBlasSide)Side;
672 call.uplo = (RsBlasUplo)Uplo;
673 call.diag = (RsBlasDiag)Diag;
674 call.M = M;
675 call.N = N;
676 call.K = K;
677 call.alpha.z.r = alphaX;
678 call.alpha.z.i = alphaY;
679 call.beta.z.r = betaX;
680 call.beta.z.r = betaY;
681 call.incX = incX;
682 call.incY = incY;
683 call.KL = KL;
684 call.KU = KU;
685
686 RsAllocation in_allocs[3];
687 in_allocs[0] = (RsAllocation)A;
688 in_allocs[1] = (RsAllocation)B;
689 in_allocs[2] = (RsAllocation)C;
690
691 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
692 in_allocs, sizeof(in_allocs), nullptr,
693 &call, sizeof(call), nullptr, 0);
694}
695
696
697static void
Tim Murray9cb16a22015-04-01 11:07:16 -0700698nScriptIntrinsicBLAS_BNNM(JNIEnv *_env, jobject _this, jlong con, jlong id, jint M, jint N, jint K,
699 jlong A, jint a_offset, jlong B, jint b_offset, jlong C, jint c_offset,
700 jint c_mult_int) {
701 RsBlasCall call;
702 memset(&call, 0, sizeof(call));
703 call.func = RsBlas_bnnm;
704 call.M = M;
705 call.N = N;
706 call.K = K;
707 call.a_offset = a_offset;
708 call.b_offset = b_offset;
709 call.c_offset = c_offset;
710 call.c_mult_int = c_mult_int;
711
712 RsAllocation in_allocs[3];
713 in_allocs[0] = (RsAllocation)A;
714 in_allocs[1] = (RsAllocation)B;
715 in_allocs[2] = (RsAllocation)C;
716
717 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
718 in_allocs, sizeof(in_allocs), nullptr,
719 &call, sizeof(call), nullptr, 0);
720}
721
722
723static void
Tim Murray460a0492013-11-19 12:45:54 -0800724nAssignName(JNIEnv *_env, jobject _this, jlong con, jlong obj, jbyteArray str)
Jason Sams3eaa338e2009-06-10 15:04:38 -0700725{
Andreas Gampe67333922014-11-10 20:35:59 -0800726 if (kLogApi) {
727 ALOGD("nAssignName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
728 }
Jason Sams3eaa338e2009-06-10 15:04:38 -0700729 jint len = _env->GetArrayLength(str);
730 jbyte * cptr = (jbyte *) _env->GetPrimitiveArrayCritical(str, 0);
Tim Murrayeff663f2013-11-15 13:08:30 -0800731 rsAssignName((RsContext)con, (void *)obj, (const char *)cptr, len);
Jason Sams3eaa338e2009-06-10 15:04:38 -0700732 _env->ReleasePrimitiveArrayCritical(str, cptr, JNI_ABORT);
733}
734
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700735static jstring
Tim Murray460a0492013-11-19 12:45:54 -0800736nGetName(JNIEnv *_env, jobject _this, jlong con, jlong obj)
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700737{
Andreas Gampe67333922014-11-10 20:35:59 -0800738 if (kLogApi) {
739 ALOGD("nGetName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
740 }
Chris Wailes488230c32014-08-14 11:22:40 -0700741 const char *name = nullptr;
Tim Murrayeff663f2013-11-15 13:08:30 -0800742 rsaGetName((RsContext)con, (void *)obj, &name);
Chris Wailes488230c32014-08-14 11:22:40 -0700743 if(name == nullptr || strlen(name) == 0) {
744 return nullptr;
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700745 }
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700746 return _env->NewStringUTF(name);
747}
748
Jason Sams7ce033d2009-08-18 14:14:24 -0700749static void
Tim Murray460a0492013-11-19 12:45:54 -0800750nObjDestroy(JNIEnv *_env, jobject _this, jlong con, jlong obj)
Jason Sams7ce033d2009-08-18 14:14:24 -0700751{
Andreas Gampe67333922014-11-10 20:35:59 -0800752 if (kLogApi) {
753 ALOGD("nObjDestroy, con(%p) obj(%p)", (RsContext)con, (void *)obj);
754 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800755 rsObjDestroy((RsContext)con, (void *)obj);
Jason Sams7ce033d2009-08-18 14:14:24 -0700756}
757
Jason Sams3eaa338e2009-06-10 15:04:38 -0700758// ---------------------------------------------------------------------------
759
Tim Murrayeff663f2013-11-15 13:08:30 -0800760static jlong
Jason Samsd19f10d2009-05-22 14:03:28 -0700761nDeviceCreate(JNIEnv *_env, jobject _this)
762{
Andreas Gampe67333922014-11-10 20:35:59 -0800763 if (kLogApi) {
764 ALOGD("nDeviceCreate");
765 }
Tim Murray3aa89c12014-08-18 17:51:22 -0700766 return (jlong)(uintptr_t)rsDeviceCreate();
Jason Samsd19f10d2009-05-22 14:03:28 -0700767}
768
769static void
Tim Murray5eaf4682014-01-10 11:25:52 -0800770nDeviceDestroy(JNIEnv *_env, jobject _this, jlong dev)
Jason Samsd19f10d2009-05-22 14:03:28 -0700771{
Andreas Gampe67333922014-11-10 20:35:59 -0800772 if (kLogApi) {
773 ALOGD("nDeviceDestroy");
774 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700775 return rsDeviceDestroy((RsDevice)dev);
776}
777
Jason Samsebfb4362009-09-23 13:57:02 -0700778static void
Tim Murray5eaf4682014-01-10 11:25:52 -0800779nDeviceSetConfig(JNIEnv *_env, jobject _this, jlong dev, jint p, jint value)
Jason Samsebfb4362009-09-23 13:57:02 -0700780{
Andreas Gampe67333922014-11-10 20:35:59 -0800781 if (kLogApi) {
782 ALOGD("nDeviceSetConfig dev(%p), param(%i), value(%i)", (void *)dev, p, value);
783 }
Jason Samsebfb4362009-09-23 13:57:02 -0700784 return rsDeviceSetConfig((RsDevice)dev, (RsDeviceParam)p, value);
785}
786
Tim Murrayeff663f2013-11-15 13:08:30 -0800787static jlong
Jason Sams81cd2b12014-12-02 12:36:43 -0800788nContextCreate(JNIEnv *_env, jobject _this, jlong dev, jint flags, jint sdkVer, jint contextType)
Jason Samsd19f10d2009-05-22 14:03:28 -0700789{
Andreas Gampe67333922014-11-10 20:35:59 -0800790 if (kLogApi) {
791 ALOGD("nContextCreate");
792 }
Jason Sams81cd2b12014-12-02 12:36:43 -0800793 return (jlong)(uintptr_t)rsContextCreate((RsDevice)dev, 0, sdkVer, (RsContextType)contextType, flags);
Jason Sams704ff642010-02-09 16:05:07 -0800794}
795
Tim Murrayeff663f2013-11-15 13:08:30 -0800796static jlong
Tim Murray5eaf4682014-01-10 11:25:52 -0800797nContextCreateGL(JNIEnv *_env, jobject _this, jlong dev, jint ver, jint sdkVer,
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000798 jint colorMin, jint colorPref,
799 jint alphaMin, jint alphaPref,
800 jint depthMin, jint depthPref,
801 jint stencilMin, jint stencilPref,
802 jint samplesMin, jint samplesPref, jfloat samplesQ,
803 jint dpi)
Jason Sams704ff642010-02-09 16:05:07 -0800804{
Jason Sams11c8af92010-10-13 15:31:10 -0700805 RsSurfaceConfig sc;
806 sc.alphaMin = alphaMin;
807 sc.alphaPref = alphaPref;
808 sc.colorMin = colorMin;
809 sc.colorPref = colorPref;
810 sc.depthMin = depthMin;
811 sc.depthPref = depthPref;
812 sc.samplesMin = samplesMin;
813 sc.samplesPref = samplesPref;
814 sc.samplesQ = samplesQ;
815
Andreas Gampe67333922014-11-10 20:35:59 -0800816 if (kLogApi) {
817 ALOGD("nContextCreateGL");
818 }
Tim Murray3aa89c12014-08-18 17:51:22 -0700819 return (jlong)(uintptr_t)rsContextCreateGL((RsDevice)dev, ver, sdkVer, sc, dpi);
Jason Samsd19f10d2009-05-22 14:03:28 -0700820}
821
822static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800823nContextSetPriority(JNIEnv *_env, jobject _this, jlong con, jint p)
Jason Sams7d787b42009-11-15 12:14:26 -0800824{
Andreas Gampe67333922014-11-10 20:35:59 -0800825 if (kLogApi) {
826 ALOGD("ContextSetPriority, con(%p), priority(%i)", (RsContext)con, p);
827 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800828 rsContextSetPriority((RsContext)con, p);
Jason Sams7d787b42009-11-15 12:14:26 -0800829}
830
Tim Murray47f31582015-04-07 15:43:24 -0700831static void
832nContextSetCacheDir(JNIEnv *_env, jobject _this, jlong con, jstring cacheDir)
833{
834 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
835
836 if (kLogApi) {
837 ALOGD("ContextSetCacheDir, con(%p), cacheDir(%s)", (RsContext)con, cacheDirUTF.c_str());
838 }
839 rsContextSetCacheDir((RsContext)con, cacheDirUTF.c_str(), cacheDirUTF.length());
840}
841
Jason Sams7d787b42009-11-15 12:14:26 -0800842
843
844static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800845nContextSetSurface(JNIEnv *_env, jobject _this, jlong con, jint width, jint height, jobject wnd)
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800846{
Andreas Gampe67333922014-11-10 20:35:59 -0800847 if (kLogApi) {
848 ALOGD("nContextSetSurface, con(%p), width(%i), height(%i), surface(%p)", (RsContext)con,
849 width, height, (Surface *)wnd);
850 }
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800851
Chris Wailes488230c32014-08-14 11:22:40 -0700852 ANativeWindow * window = nullptr;
853 if (wnd == nullptr) {
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800854
855 } else {
Jeff Brown64a55af2012-08-26 02:47:39 -0700856 window = android_view_Surface_getNativeWindow(_env, wnd).get();
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800857 }
858
Tim Murrayeff663f2013-11-15 13:08:30 -0800859 rsContextSetSurface((RsContext)con, width, height, window);
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800860}
861
862static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800863nContextDestroy(JNIEnv *_env, jobject _this, jlong con)
Jason Samsd19f10d2009-05-22 14:03:28 -0700864{
Andreas Gampe67333922014-11-10 20:35:59 -0800865 if (kLogApi) {
866 ALOGD("nContextDestroy, con(%p)", (RsContext)con);
867 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800868 rsContextDestroy((RsContext)con);
Jason Samsd19f10d2009-05-22 14:03:28 -0700869}
870
Jason Sams715333b2009-11-17 17:26:46 -0800871static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800872nContextDump(JNIEnv *_env, jobject _this, jlong con, jint bits)
Jason Sams715333b2009-11-17 17:26:46 -0800873{
Andreas Gampe67333922014-11-10 20:35:59 -0800874 if (kLogApi) {
875 ALOGD("nContextDump, con(%p) bits(%i)", (RsContext)con, bits);
876 }
Jason Sams715333b2009-11-17 17:26:46 -0800877 rsContextDump((RsContext)con, bits);
878}
Jason Samsd19f10d2009-05-22 14:03:28 -0700879
880static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800881nContextPause(JNIEnv *_env, jobject _this, jlong con)
Jason Sams65e7aa52009-09-24 17:38:20 -0700882{
Andreas Gampe67333922014-11-10 20:35:59 -0800883 if (kLogApi) {
884 ALOGD("nContextPause, con(%p)", (RsContext)con);
885 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800886 rsContextPause((RsContext)con);
Jason Sams65e7aa52009-09-24 17:38:20 -0700887}
888
889static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800890nContextResume(JNIEnv *_env, jobject _this, jlong con)
Jason Sams65e7aa52009-09-24 17:38:20 -0700891{
Andreas Gampe67333922014-11-10 20:35:59 -0800892 if (kLogApi) {
893 ALOGD("nContextResume, con(%p)", (RsContext)con);
894 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800895 rsContextResume((RsContext)con);
Jason Sams65e7aa52009-09-24 17:38:20 -0700896}
897
Jason Sams1c415172010-11-08 17:06:46 -0800898
899static jstring
Tim Murrayeff663f2013-11-15 13:08:30 -0800900nContextGetErrorMessage(JNIEnv *_env, jobject _this, jlong con)
Jason Sams1c415172010-11-08 17:06:46 -0800901{
Andreas Gampe67333922014-11-10 20:35:59 -0800902 if (kLogApi) {
903 ALOGD("nContextGetErrorMessage, con(%p)", (RsContext)con);
904 }
Jason Sams1c415172010-11-08 17:06:46 -0800905 char buf[1024];
906
907 size_t receiveLen;
908 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -0800909 int id = rsContextGetMessage((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -0700910 buf, sizeof(buf),
911 &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -0700912 &subID, sizeof(subID));
Jason Sams1c415172010-11-08 17:06:46 -0800913 if (!id && receiveLen) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +0100914 ALOGV("message receive buffer too small. %zu", receiveLen);
Jason Sams1c415172010-11-08 17:06:46 -0800915 }
916 return _env->NewStringUTF(buf);
917}
918
Jason Samsedbfabd2011-05-17 15:01:29 -0700919static jint
Tim Murrayeff663f2013-11-15 13:08:30 -0800920nContextGetUserMessage(JNIEnv *_env, jobject _this, jlong con, jintArray data)
Jason Sams516c3192009-10-06 13:58:47 -0700921{
Jason Sams516c3192009-10-06 13:58:47 -0700922 jint len = _env->GetArrayLength(data);
Andreas Gampe67333922014-11-10 20:35:59 -0800923 if (kLogApi) {
924 ALOGD("nContextGetMessage, con(%p), len(%i)", (RsContext)con, len);
925 }
Chris Wailes488230c32014-08-14 11:22:40 -0700926 jint *ptr = _env->GetIntArrayElements(data, nullptr);
Jason Sams516c3192009-10-06 13:58:47 -0700927 size_t receiveLen;
Jason Sams1c415172010-11-08 17:06:46 -0800928 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -0800929 int id = rsContextGetMessage((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -0700930 ptr, len * 4,
931 &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -0700932 &subID, sizeof(subID));
Jason Sams516c3192009-10-06 13:58:47 -0700933 if (!id && receiveLen) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +0100934 ALOGV("message receive buffer too small. %zu", receiveLen);
Jason Sams516c3192009-10-06 13:58:47 -0700935 }
936 _env->ReleaseIntArrayElements(data, ptr, 0);
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000937 return (jint)id;
Jason Sams1c415172010-11-08 17:06:46 -0800938}
939
940static jint
Tim Murrayeff663f2013-11-15 13:08:30 -0800941nContextPeekMessage(JNIEnv *_env, jobject _this, jlong con, jintArray auxData)
Jason Sams1c415172010-11-08 17:06:46 -0800942{
Andreas Gampe67333922014-11-10 20:35:59 -0800943 if (kLogApi) {
944 ALOGD("nContextPeekMessage, con(%p)", (RsContext)con);
945 }
Chris Wailes488230c32014-08-14 11:22:40 -0700946 jint *auxDataPtr = _env->GetIntArrayElements(auxData, nullptr);
Jason Sams1c415172010-11-08 17:06:46 -0800947 size_t receiveLen;
948 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -0800949 int id = rsContextPeekMessage((RsContext)con, &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -0700950 &subID, sizeof(subID));
Jason Sams1c415172010-11-08 17:06:46 -0800951 auxDataPtr[0] = (jint)subID;
952 auxDataPtr[1] = (jint)receiveLen;
953 _env->ReleaseIntArrayElements(auxData, auxDataPtr, 0);
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000954 return (jint)id;
Jason Sams516c3192009-10-06 13:58:47 -0700955}
956
Tim Murrayeff663f2013-11-15 13:08:30 -0800957static void nContextInitToClient(JNIEnv *_env, jobject _this, jlong con)
Jason Sams516c3192009-10-06 13:58:47 -0700958{
Andreas Gampe67333922014-11-10 20:35:59 -0800959 if (kLogApi) {
960 ALOGD("nContextInitToClient, con(%p)", (RsContext)con);
961 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800962 rsContextInitToClient((RsContext)con);
Jason Sams516c3192009-10-06 13:58:47 -0700963}
964
Tim Murrayeff663f2013-11-15 13:08:30 -0800965static void nContextDeinitToClient(JNIEnv *_env, jobject _this, jlong con)
Jason Sams516c3192009-10-06 13:58:47 -0700966{
Andreas Gampe67333922014-11-10 20:35:59 -0800967 if (kLogApi) {
968 ALOGD("nContextDeinitToClient, con(%p)", (RsContext)con);
969 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800970 rsContextDeinitToClient((RsContext)con);
Jason Sams516c3192009-10-06 13:58:47 -0700971}
972
Jason Sams455d6442013-02-05 19:20:18 -0800973static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800974nContextSendMessage(JNIEnv *_env, jobject _this, jlong con, jint id, jintArray data)
Jason Sams455d6442013-02-05 19:20:18 -0800975{
Chris Wailes488230c32014-08-14 11:22:40 -0700976 jint *ptr = nullptr;
Jason Sams455d6442013-02-05 19:20:18 -0800977 jint len = 0;
978 if (data) {
979 len = _env->GetArrayLength(data);
Stephen Hines4a043c12014-08-21 23:20:32 -0700980 ptr = _env->GetIntArrayElements(data, nullptr);
Jason Sams455d6442013-02-05 19:20:18 -0800981 }
Andreas Gampe67333922014-11-10 20:35:59 -0800982 if (kLogApi) {
983 ALOGD("nContextSendMessage, con(%p), id(%i), len(%i)", (RsContext)con, id, len);
984 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800985 rsContextSendMessage((RsContext)con, id, (const uint8_t *)ptr, len * sizeof(int));
Jason Sams455d6442013-02-05 19:20:18 -0800986 if (data) {
987 _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
988 }
989}
990
991
Jason Sams516c3192009-10-06 13:58:47 -0700992
Tim Murray460a0492013-11-19 12:45:54 -0800993static jlong
Andreas Gampe67333922014-11-10 20:35:59 -0800994nElementCreate(JNIEnv *_env, jobject _this, jlong con, jlong type, jint kind, jboolean norm,
995 jint size)
Jason Samsd19f10d2009-05-22 14:03:28 -0700996{
Andreas Gampe67333922014-11-10 20:35:59 -0800997 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +0100998 ALOGD("nElementCreate, con(%p), type(%" PRId64 "), kind(%i), norm(%i), size(%i)", (RsContext)con,
Andreas Gampe67333922014-11-10 20:35:59 -0800999 type, kind, norm, size);
1000 }
1001 return (jlong)(uintptr_t)rsElementCreate((RsContext)con, (RsDataType)type, (RsDataKind)kind,
1002 norm, size);
Jason Samsd19f10d2009-05-22 14:03:28 -07001003}
1004
Tim Murray460a0492013-11-19 12:45:54 -08001005static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001006nElementCreate2(JNIEnv *_env, jobject _this, jlong con,
Ashok Bhat98071552014-02-12 09:54:43 +00001007 jlongArray _ids, jobjectArray _names, jintArray _arraySizes)
Jason Samsd19f10d2009-05-22 14:03:28 -07001008{
Jason Sams718cd1f2009-12-23 14:35:29 -08001009 int fieldCount = _env->GetArrayLength(_ids);
Andreas Gampe67333922014-11-10 20:35:59 -08001010 if (kLogApi) {
1011 ALOGD("nElementCreate2, con(%p)", (RsContext)con);
1012 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001013
Chris Wailes488230c32014-08-14 11:22:40 -07001014 jlong *jIds = _env->GetLongArrayElements(_ids, nullptr);
1015 jint *jArraySizes = _env->GetIntArrayElements(_arraySizes, nullptr);
Ashok Bhat98071552014-02-12 09:54:43 +00001016
1017 RsElement *ids = (RsElement*)malloc(fieldCount * sizeof(RsElement));
1018 uint32_t *arraySizes = (uint32_t *)malloc(fieldCount * sizeof(uint32_t));
1019
1020 for(int i = 0; i < fieldCount; i ++) {
1021 ids[i] = (RsElement)jIds[i];
1022 arraySizes[i] = (uint32_t)jArraySizes[i];
1023 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001024
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08001025 AutoJavaStringArrayToUTF8 names(_env, _names, fieldCount);
1026
1027 const char **nameArray = names.c_str();
1028 size_t *sizeArray = names.c_str_len();
1029
Tim Murray3aa89c12014-08-18 17:51:22 -07001030 jlong id = (jlong)(uintptr_t)rsElementCreate2((RsContext)con,
Ashok Bhat98071552014-02-12 09:54:43 +00001031 (const RsElement *)ids, fieldCount,
Jason Sams7a22e102011-05-06 14:14:30 -07001032 nameArray, fieldCount * sizeof(size_t), sizeArray,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001033 (const uint32_t *)arraySizes, fieldCount);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08001034
Ashok Bhat98071552014-02-12 09:54:43 +00001035 free(ids);
1036 free(arraySizes);
1037 _env->ReleaseLongArrayElements(_ids, jIds, JNI_ABORT);
1038 _env->ReleaseIntArrayElements(_arraySizes, jArraySizes, JNI_ABORT);
1039
Tim Murray3aa89c12014-08-18 17:51:22 -07001040 return (jlong)(uintptr_t)id;
Jason Samsd19f10d2009-05-22 14:03:28 -07001041}
1042
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001043static void
Tim Murray460a0492013-11-19 12:45:54 -08001044nElementGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jintArray _elementData)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001045{
1046 int dataSize = _env->GetArrayLength(_elementData);
Andreas Gampe67333922014-11-10 20:35:59 -08001047 if (kLogApi) {
1048 ALOGD("nElementGetNativeData, con(%p)", (RsContext)con);
1049 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001050
1051 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
1052 assert(dataSize == 5);
1053
Narayan Kamath78c0ce52014-03-19 10:15:51 +00001054 uintptr_t elementData[5];
Tim Murrayeff663f2013-11-15 13:08:30 -08001055 rsaElementGetNativeData((RsContext)con, (RsElement)id, elementData, dataSize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001056
1057 for(jint i = 0; i < dataSize; i ++) {
Ashok Bhat98071552014-02-12 09:54:43 +00001058 const jint data = (jint)elementData[i];
1059 _env->SetIntArrayRegion(_elementData, i, 1, &data);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001060 }
1061}
1062
1063
1064static void
Tim Murray460a0492013-11-19 12:45:54 -08001065nElementGetSubElements(JNIEnv *_env, jobject _this, jlong con, jlong id,
Ashok Bhat98071552014-02-12 09:54:43 +00001066 jlongArray _IDs,
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -07001067 jobjectArray _names,
1068 jintArray _arraySizes)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001069{
Ashok Bhat98071552014-02-12 09:54:43 +00001070 uint32_t dataSize = _env->GetArrayLength(_IDs);
Andreas Gampe67333922014-11-10 20:35:59 -08001071 if (kLogApi) {
1072 ALOGD("nElementGetSubElements, con(%p)", (RsContext)con);
1073 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001074
Ashok Bhat98071552014-02-12 09:54:43 +00001075 uintptr_t *ids = (uintptr_t*)malloc(dataSize * sizeof(uintptr_t));
1076 const char **names = (const char **)malloc(dataSize * sizeof(const char *));
Narayan Kamath78c0ce52014-03-19 10:15:51 +00001077 uint32_t *arraySizes = (uint32_t *)malloc(dataSize * sizeof(uint32_t));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001078
Andreas Gampe67333922014-11-10 20:35:59 -08001079 rsaElementGetSubElements((RsContext)con, (RsElement)id, ids, names, arraySizes,
1080 (uint32_t)dataSize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001081
Ashok Bhat98071552014-02-12 09:54:43 +00001082 for(uint32_t i = 0; i < dataSize; i++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07001083 const jlong id = (jlong)(uintptr_t)ids[i];
Ashok Bhat98071552014-02-12 09:54:43 +00001084 const jint arraySize = (jint)arraySizes[i];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001085 _env->SetObjectArrayElement(_names, i, _env->NewStringUTF(names[i]));
Ashok Bhat98071552014-02-12 09:54:43 +00001086 _env->SetLongArrayRegion(_IDs, i, 1, &id);
1087 _env->SetIntArrayRegion(_arraySizes, i, 1, &arraySize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001088 }
1089
1090 free(ids);
1091 free(names);
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -07001092 free(arraySizes);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001093}
1094
Jason Samsd19f10d2009-05-22 14:03:28 -07001095// -----------------------------------
1096
Tim Murray460a0492013-11-19 12:45:54 -08001097static jlong
1098nTypeCreate(JNIEnv *_env, jobject _this, jlong con, jlong eid,
Jason Samsb109cc72013-01-07 18:20:12 -08001099 jint dimx, jint dimy, jint dimz, jboolean mips, jboolean faces, jint yuv)
Jason Samsd19f10d2009-05-22 14:03:28 -07001100{
Andreas Gampe67333922014-11-10 20:35:59 -08001101 if (kLogApi) {
1102 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 +01001103 (RsContext)con, (void*)eid, dimx, dimy, dimz, mips, faces, yuv);
Andreas Gampe67333922014-11-10 20:35:59 -08001104 }
Jason Sams3b9c52a2010-10-14 17:48:46 -07001105
Andreas Gampe67333922014-11-10 20:35:59 -08001106 return (jlong)(uintptr_t)rsTypeCreate((RsContext)con, (RsElement)eid, dimx, dimy, dimz, mips,
1107 faces, yuv);
Jason Samsd19f10d2009-05-22 14:03:28 -07001108}
1109
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001110static void
Ashok Bhat98071552014-02-12 09:54:43 +00001111nTypeGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jlongArray _typeData)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001112{
1113 // We are packing 6 items: mDimX; mDimY; mDimZ;
1114 // mDimLOD; mDimFaces; mElement; into typeData
1115 int elementCount = _env->GetArrayLength(_typeData);
1116
1117 assert(elementCount == 6);
Andreas Gampe67333922014-11-10 20:35:59 -08001118 if (kLogApi) {
1119 ALOGD("nTypeGetNativeData, con(%p)", (RsContext)con);
1120 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001121
Ashok Bhat98071552014-02-12 09:54:43 +00001122 uintptr_t typeData[6];
Tim Murrayeff663f2013-11-15 13:08:30 -08001123 rsaTypeGetNativeData((RsContext)con, (RsType)id, typeData, 6);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001124
1125 for(jint i = 0; i < elementCount; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07001126 const jlong data = (jlong)(uintptr_t)typeData[i];
Ashok Bhat98071552014-02-12 09:54:43 +00001127 _env->SetLongArrayRegion(_typeData, i, 1, &data);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001128 }
1129}
1130
Jason Samsd19f10d2009-05-22 14:03:28 -07001131// -----------------------------------
1132
Tim Murray460a0492013-11-19 12:45:54 -08001133static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001134nAllocationCreateTyped(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mips, jint usage,
1135 jlong pointer)
Jason Samsd19f10d2009-05-22 14:03:28 -07001136{
Andreas Gampe67333922014-11-10 20:35:59 -08001137 if (kLogApi) {
1138 ALOGD("nAllocationCreateTyped, con(%p), type(%p), mip(%i), usage(%i), ptr(%p)",
1139 (RsContext)con, (RsElement)type, mips, usage, (void *)pointer);
1140 }
1141 return (jlong)(uintptr_t) rsAllocationCreateTyped((RsContext)con, (RsType)type,
1142 (RsAllocationMipmapControl)mips,
1143 (uint32_t)usage, (uintptr_t)pointer);
Jason Samsd19f10d2009-05-22 14:03:28 -07001144}
1145
Jason Samsd19f10d2009-05-22 14:03:28 -07001146static void
Tim Murray460a0492013-11-19 12:45:54 -08001147nAllocationSyncAll(JNIEnv *_env, jobject _this, jlong con, jlong a, jint bits)
Jason Sams5476b452010-12-08 16:14:36 -08001148{
Andreas Gampe67333922014-11-10 20:35:59 -08001149 if (kLogApi) {
1150 ALOGD("nAllocationSyncAll, con(%p), a(%p), bits(0x%08x)", (RsContext)con, (RsAllocation)a,
1151 bits);
1152 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001153 rsAllocationSyncAll((RsContext)con, (RsAllocation)a, (RsAllocationUsageType)bits);
Jason Sams5476b452010-12-08 16:14:36 -08001154}
1155
Jason Sams72226e02013-02-22 12:45:54 -08001156static jobject
Tim Murray460a0492013-11-19 12:45:54 -08001157nAllocationGetSurface(JNIEnv *_env, jobject _this, jlong con, jlong a)
Jason Sams615e7ce2012-01-13 14:01:20 -08001158{
Andreas Gampe67333922014-11-10 20:35:59 -08001159 if (kLogApi) {
1160 ALOGD("nAllocationGetSurface, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
1161 }
Jason Sams615e7ce2012-01-13 14:01:20 -08001162
Andreas Gampe67333922014-11-10 20:35:59 -08001163 IGraphicBufferProducer *v = (IGraphicBufferProducer *)rsAllocationGetSurface((RsContext)con,
1164 (RsAllocation)a);
Jason Sams72226e02013-02-22 12:45:54 -08001165 sp<IGraphicBufferProducer> bp = v;
Chris Wailes488230c32014-08-14 11:22:40 -07001166 v->decStrong(nullptr);
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001167
Jason Sams72226e02013-02-22 12:45:54 -08001168 jobject o = android_view_Surface_createFromIGraphicBufferProducer(_env, bp);
1169 return o;
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001170}
1171
1172static void
Tim Murray460a0492013-11-19 12:45:54 -08001173nAllocationSetSurface(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject sur)
Jason Sams163766c2012-02-15 12:04:24 -08001174{
Andreas Gampe67333922014-11-10 20:35:59 -08001175 if (kLogApi) {
1176 ALOGD("nAllocationSetSurface, con(%p), alloc(%p), surface(%p)", (RsContext)con,
1177 (RsAllocation)alloc, (Surface *)sur);
1178 }
Jason Sams163766c2012-02-15 12:04:24 -08001179
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001180 sp<Surface> s;
Jason Sams163766c2012-02-15 12:04:24 -08001181 if (sur != 0) {
Jeff Brown64a55af2012-08-26 02:47:39 -07001182 s = android_view_Surface_getSurface(_env, sur);
Jason Sams163766c2012-02-15 12:04:24 -08001183 }
1184
Andreas Gampe67333922014-11-10 20:35:59 -08001185 rsAllocationSetSurface((RsContext)con, (RsAllocation)alloc,
1186 static_cast<ANativeWindow *>(s.get()));
Jason Sams163766c2012-02-15 12:04:24 -08001187}
1188
1189static void
Tim Murray460a0492013-11-19 12:45:54 -08001190nAllocationIoSend(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Sams163766c2012-02-15 12:04:24 -08001191{
Andreas Gampe67333922014-11-10 20:35:59 -08001192 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001193 ALOGD("nAllocationIoSend, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
Andreas Gampe67333922014-11-10 20:35:59 -08001194 }
Tim Murray460a0492013-11-19 12:45:54 -08001195 rsAllocationIoSend((RsContext)con, (RsAllocation)alloc);
Jason Sams163766c2012-02-15 12:04:24 -08001196}
1197
1198static void
Tim Murray460a0492013-11-19 12:45:54 -08001199nAllocationIoReceive(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Sams163766c2012-02-15 12:04:24 -08001200{
Andreas Gampe67333922014-11-10 20:35:59 -08001201 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001202 ALOGD("nAllocationIoReceive, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
Andreas Gampe67333922014-11-10 20:35:59 -08001203 }
Tim Murray460a0492013-11-19 12:45:54 -08001204 rsAllocationIoReceive((RsContext)con, (RsAllocation)alloc);
Jason Sams163766c2012-02-15 12:04:24 -08001205}
1206
1207
1208static void
Tim Murray460a0492013-11-19 12:45:54 -08001209nAllocationGenerateMipmaps(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Samsf7086092011-01-12 13:28:37 -08001210{
Andreas Gampe67333922014-11-10 20:35:59 -08001211 if (kLogApi) {
1212 ALOGD("nAllocationGenerateMipmaps, con(%p), a(%p)", (RsContext)con, (RsAllocation)alloc);
1213 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001214 rsAllocationGenerateMipmaps((RsContext)con, (RsAllocation)alloc);
Jason Samsf7086092011-01-12 13:28:37 -08001215}
1216
Tim Murray460a0492013-11-19 12:45:54 -08001217static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001218nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip,
1219 jobject jbitmap, jint usage)
Jason Samsffe9f482009-06-01 17:45:53 -07001220{
Jason Samsffe9f482009-06-01 17:45:53 -07001221 SkBitmap const * nativeBitmap =
Ashok Bhat36bef0b2014-01-20 20:08:01 +00001222 (SkBitmap const *)_env->GetLongField(jbitmap, gNativeBitmapID);
Jason Samsffe9f482009-06-01 17:45:53 -07001223 const SkBitmap& bitmap(*nativeBitmap);
Jason Samsffe9f482009-06-01 17:45:53 -07001224
Jason Sams5476b452010-12-08 16:14:36 -08001225 bitmap.lockPixels();
1226 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001227 jlong id = (jlong)(uintptr_t)rsAllocationCreateFromBitmap((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -07001228 (RsType)type, (RsAllocationMipmapControl)mip,
1229 ptr, bitmap.getSize(), usage);
Jason Sams5476b452010-12-08 16:14:36 -08001230 bitmap.unlockPixels();
1231 return id;
Jason Samsffe9f482009-06-01 17:45:53 -07001232}
Jason Samsfe08d992009-05-27 14:45:32 -07001233
Tim Murray460a0492013-11-19 12:45:54 -08001234static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001235nAllocationCreateBitmapBackedAllocation(JNIEnv *_env, jobject _this, jlong con, jlong type,
1236 jint mip, jobject jbitmap, jint usage)
Tim Murraya3145512012-12-04 17:59:29 -08001237{
1238 SkBitmap const * nativeBitmap =
Ashok Bhat36bef0b2014-01-20 20:08:01 +00001239 (SkBitmap const *)_env->GetLongField(jbitmap, gNativeBitmapID);
Tim Murraya3145512012-12-04 17:59:29 -08001240 const SkBitmap& bitmap(*nativeBitmap);
1241
1242 bitmap.lockPixels();
1243 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001244 jlong id = (jlong)(uintptr_t)rsAllocationCreateTyped((RsContext)con,
Tim Murraya3145512012-12-04 17:59:29 -08001245 (RsType)type, (RsAllocationMipmapControl)mip,
Ashok Bhat98071552014-02-12 09:54:43 +00001246 (uint32_t)usage, (uintptr_t)ptr);
Tim Murraya3145512012-12-04 17:59:29 -08001247 bitmap.unlockPixels();
1248 return id;
1249}
1250
Tim Murray460a0492013-11-19 12:45:54 -08001251static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001252nAllocationCubeCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip,
1253 jobject jbitmap, jint usage)
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001254{
1255 SkBitmap const * nativeBitmap =
Ashok Bhat36bef0b2014-01-20 20:08:01 +00001256 (SkBitmap const *)_env->GetLongField(jbitmap, gNativeBitmapID);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001257 const SkBitmap& bitmap(*nativeBitmap);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001258
Jason Sams5476b452010-12-08 16:14:36 -08001259 bitmap.lockPixels();
1260 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001261 jlong id = (jlong)(uintptr_t)rsAllocationCubeCreateFromBitmap((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -07001262 (RsType)type, (RsAllocationMipmapControl)mip,
1263 ptr, bitmap.getSize(), usage);
Jason Sams5476b452010-12-08 16:14:36 -08001264 bitmap.unlockPixels();
1265 return id;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001266}
1267
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001268static void
Tim Murray460a0492013-11-19 12:45:54 -08001269nAllocationCopyFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001270{
1271 SkBitmap const * nativeBitmap =
Ashok Bhat36bef0b2014-01-20 20:08:01 +00001272 (SkBitmap const *)_env->GetLongField(jbitmap, gNativeBitmapID);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001273 const SkBitmap& bitmap(*nativeBitmap);
Jason Samsf7086092011-01-12 13:28:37 -08001274 int w = bitmap.width();
1275 int h = bitmap.height();
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001276
Jason Sams4ef66502010-12-10 16:03:15 -08001277 bitmap.lockPixels();
1278 const void* ptr = bitmap.getPixels();
Tim Murrayeff663f2013-11-15 13:08:30 -08001279 rsAllocation2DData((RsContext)con, (RsAllocation)alloc, 0, 0,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001280 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Tim Murray38faea302012-11-27 14:55:08 -08001281 w, h, ptr, bitmap.getSize(), 0);
Jason Sams4ef66502010-12-10 16:03:15 -08001282 bitmap.unlockPixels();
1283}
1284
1285static void
Tim Murray460a0492013-11-19 12:45:54 -08001286nAllocationCopyToBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
Jason Sams4ef66502010-12-10 16:03:15 -08001287{
1288 SkBitmap const * nativeBitmap =
Ashok Bhat36bef0b2014-01-20 20:08:01 +00001289 (SkBitmap const *)_env->GetLongField(jbitmap, gNativeBitmapID);
Jason Sams4ef66502010-12-10 16:03:15 -08001290 const SkBitmap& bitmap(*nativeBitmap);
1291
1292 bitmap.lockPixels();
1293 void* ptr = bitmap.getPixels();
Tim Murrayeff663f2013-11-15 13:08:30 -08001294 rsAllocationCopyToBitmap((RsContext)con, (RsAllocation)alloc, ptr, bitmap.getSize());
Jason Sams4ef66502010-12-10 16:03:15 -08001295 bitmap.unlockPixels();
Alex Sakhartchouk835b8542011-07-20 14:33:10 -07001296 bitmap.notifyPixelsChanged();
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001297}
1298
Stephen Hines414fa2c2014-04-17 01:02:42 -07001299// Copies from the Java object data into the Allocation pointed to by _alloc.
Jason Samsd19f10d2009-05-22 14:03:28 -07001300static void
Tim Murray460a0492013-11-19 12:45:54 -08001301nAllocationData1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001302 jint count, jobject data, jint sizeBytes, jint dataType, jint mSize,
1303 jboolean usePadding)
Jason Samsd19f10d2009-05-22 14:03:28 -07001304{
Jason Samse729a942013-11-06 11:22:02 -08001305 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001306 if (kLogApi) {
1307 ALOGD("nAllocation1DData, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
1308 "dataType(%i)", (RsContext)con, (RsAllocation)alloc, offset, count, sizeBytes,
1309 dataType);
1310 }
Miao Wang87e908d2015-03-02 15:15:15 -08001311 PER_ARRAY_TYPE(nullptr, rsAllocation1DData, true,
1312 (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -07001313}
1314
1315static void
Miao Wangc8e237e2015-02-20 18:36:32 -08001316nAllocationElementData(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
1317 jint xoff, jint yoff, jint zoff,
1318 jint lod, jint compIdx, jbyteArray data, jint sizeBytes)
Jason Sams49bdaf02010-08-31 13:50:42 -07001319{
1320 jint len = _env->GetArrayLength(data);
Andreas Gampe67333922014-11-10 20:35:59 -08001321 if (kLogApi) {
Miao Wangc8e237e2015-02-20 18:36:32 -08001322 ALOGD("nAllocationElementData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), comp(%i), len(%i), "
1323 "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, compIdx, len,
Andreas Gampe67333922014-11-10 20:35:59 -08001324 sizeBytes);
1325 }
Chris Wailes488230c32014-08-14 11:22:40 -07001326 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangc8e237e2015-02-20 18:36:32 -08001327 rsAllocationElementData((RsContext)con, (RsAllocation)alloc,
1328 xoff, yoff, zoff,
1329 lod, ptr, sizeBytes, compIdx);
Jason Sams49bdaf02010-08-31 13:50:42 -07001330 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1331}
1332
Miao Wangc8e237e2015-02-20 18:36:32 -08001333
Stephen Hines414fa2c2014-04-17 01:02:42 -07001334// Copies from the Java object data into the Allocation pointed to by _alloc.
Jason Sams49bdaf02010-08-31 13:50:42 -07001335static void
Tim Murray460a0492013-11-19 12:45:54 -08001336nAllocationData2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
Miao Wang87e908d2015-03-02 15:15:15 -08001337 jint w, jint h, jobject data, jint sizeBytes, jint dataType, jint mSize,
1338 jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001339{
Jason Samse729a942013-11-06 11:22:02 -08001340 RsAllocation *alloc = (RsAllocation *)_alloc;
1341 RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
Andreas Gampe67333922014-11-10 20:35:59 -08001342 if (kLogApi) {
1343 ALOGD("nAllocation2DData, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1344 "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1345 }
Miao Wang87e908d2015-03-02 15:15:15 -08001346 int count = w * h;
1347 PER_ARRAY_TYPE(nullptr, rsAllocation2DData, true,
1348 (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
Jason Samsd19f10d2009-05-22 14:03:28 -07001349}
1350
Stephen Hines414fa2c2014-04-17 01:02:42 -07001351// Copies from the Allocation pointed to by srcAlloc into the Allocation
1352// pointed to by dstAlloc.
Jason Sams40a29e82009-08-10 14:55:26 -07001353static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001354nAllocationData2D_alloc(JNIEnv *_env, jobject _this, jlong con,
Tim Murray460a0492013-11-19 12:45:54 -08001355 jlong dstAlloc, jint dstXoff, jint dstYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001356 jint dstMip, jint dstFace,
1357 jint width, jint height,
Tim Murray460a0492013-11-19 12:45:54 -08001358 jlong srcAlloc, jint srcXoff, jint srcYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001359 jint srcMip, jint srcFace)
1360{
Andreas Gampe67333922014-11-10 20:35:59 -08001361 if (kLogApi) {
1362 ALOGD("nAllocation2DData_s, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
1363 " dstMip(%i), dstFace(%i), width(%i), height(%i),"
1364 " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i), srcFace(%i)",
1365 (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip, dstFace,
1366 width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip, srcFace);
1367 }
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001368
Tim Murrayeff663f2013-11-15 13:08:30 -08001369 rsAllocationCopy2DRange((RsContext)con,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001370 (RsAllocation)dstAlloc,
1371 dstXoff, dstYoff,
1372 dstMip, dstFace,
1373 width, height,
1374 (RsAllocation)srcAlloc,
1375 srcXoff, srcYoff,
1376 srcMip, srcFace);
1377}
1378
Stephen Hines414fa2c2014-04-17 01:02:42 -07001379// Copies from the Java object data into the Allocation pointed to by _alloc.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001380static void
Tim Murray460a0492013-11-19 12:45:54 -08001381nAllocationData3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001382 jint w, jint h, jint d, jobject data, jint sizeBytes, jint dataType,
1383 jint mSize, jboolean usePadding)
Jason Samsb05d6892013-04-09 15:59:24 -07001384{
Jason Samse729a942013-11-06 11:22:02 -08001385 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001386 if (kLogApi) {
1387 ALOGD("nAllocation3DData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i),"
1388 " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff,
1389 lod, w, h, d, sizeBytes);
1390 }
Miao Wang87e908d2015-03-02 15:15:15 -08001391 int count = w * h * d;
1392 PER_ARRAY_TYPE(nullptr, rsAllocation3DData, true,
1393 (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
Jason Samsb05d6892013-04-09 15:59:24 -07001394}
1395
Stephen Hines414fa2c2014-04-17 01:02:42 -07001396// Copies from the Allocation pointed to by srcAlloc into the Allocation
1397// pointed to by dstAlloc.
Jason Samsb05d6892013-04-09 15:59:24 -07001398static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001399nAllocationData3D_alloc(JNIEnv *_env, jobject _this, jlong con,
Tim Murray460a0492013-11-19 12:45:54 -08001400 jlong dstAlloc, jint dstXoff, jint dstYoff, jint dstZoff,
Jason Samsb05d6892013-04-09 15:59:24 -07001401 jint dstMip,
1402 jint width, jint height, jint depth,
Tim Murray460a0492013-11-19 12:45:54 -08001403 jlong srcAlloc, jint srcXoff, jint srcYoff, jint srcZoff,
Jason Samsb05d6892013-04-09 15:59:24 -07001404 jint srcMip)
1405{
Andreas Gampe67333922014-11-10 20:35:59 -08001406 if (kLogApi) {
1407 ALOGD("nAllocationData3D_alloc, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
1408 " dstMip(%i), width(%i), height(%i),"
1409 " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i)",
1410 (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip,
1411 width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip);
1412 }
Jason Samsb05d6892013-04-09 15:59:24 -07001413
Tim Murrayeff663f2013-11-15 13:08:30 -08001414 rsAllocationCopy3DRange((RsContext)con,
Jason Samsb05d6892013-04-09 15:59:24 -07001415 (RsAllocation)dstAlloc,
1416 dstXoff, dstYoff, dstZoff, dstMip,
1417 width, height, depth,
1418 (RsAllocation)srcAlloc,
1419 srcXoff, srcYoff, srcZoff, srcMip);
1420}
1421
Jason Sams21659ac2013-11-06 15:08:07 -08001422
Stephen Hines414fa2c2014-04-17 01:02:42 -07001423// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Samsb05d6892013-04-09 15:59:24 -07001424static void
Miao Wang87e908d2015-03-02 15:15:15 -08001425nAllocationRead(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jobject data, jint dataType,
1426 jint mSize, jboolean usePadding)
Jason Sams40a29e82009-08-10 14:55:26 -07001427{
Jason Sams21659ac2013-11-06 15:08:07 -08001428 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001429 if (kLogApi) {
1430 ALOGD("nAllocationRead, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
1431 }
Miao Wang87e908d2015-03-02 15:15:15 -08001432 int count = 0;
1433 PER_ARRAY_TYPE(0, rsAllocationRead, false,
1434 (RsContext)con, alloc, ptr, len * typeBytes);
Jason Sams40a29e82009-08-10 14:55:26 -07001435}
1436
Stephen Hines414fa2c2014-04-17 01:02:42 -07001437// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Sams40a29e82009-08-10 14:55:26 -07001438static void
Tim Murray460a0492013-11-19 12:45:54 -08001439nAllocationRead1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001440 jint count, jobject data, jint sizeBytes, jint dataType,
1441 jint mSize, jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001442{
Jason Sams21659ac2013-11-06 15:08:07 -08001443 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001444 if (kLogApi) {
1445 ALOGD("nAllocation1DRead, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
1446 "dataType(%i)", (RsContext)con, alloc, offset, count, sizeBytes, dataType);
1447 }
Miao Wang87e908d2015-03-02 15:15:15 -08001448 PER_ARRAY_TYPE(0, rsAllocation1DRead, false,
1449 (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
Jason Samsfb9f82c2011-01-12 14:53:25 -08001450}
1451
Miao Wangc8e237e2015-02-20 18:36:32 -08001452// Copies from the Element in the Allocation pointed to by _alloc into the Java array data.
1453static void
Miao Wang45cec0a2015-03-04 16:40:21 -08001454nAllocationElementRead(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
Miao Wangc8e237e2015-02-20 18:36:32 -08001455 jint xoff, jint yoff, jint zoff,
Miao Wang45cec0a2015-03-04 16:40:21 -08001456 jint lod, jint compIdx, jbyteArray data, jint sizeBytes)
Miao Wangc8e237e2015-02-20 18:36:32 -08001457{
Miao Wang45cec0a2015-03-04 16:40:21 -08001458 jint len = _env->GetArrayLength(data);
Miao Wangc8e237e2015-02-20 18:36:32 -08001459 if (kLogApi) {
Miao Wang45cec0a2015-03-04 16:40:21 -08001460 ALOGD("nAllocationElementRead, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), comp(%i), len(%i), "
1461 "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, compIdx, len,
1462 sizeBytes);
Miao Wangc8e237e2015-02-20 18:36:32 -08001463 }
Miao Wang45cec0a2015-03-04 16:40:21 -08001464 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
1465 rsAllocationElementRead((RsContext)con, (RsAllocation)alloc,
1466 xoff, yoff, zoff,
Jason Samsa7e25092015-03-11 11:00:00 -07001467 lod, ptr, sizeBytes, compIdx);
Miao Wang45cec0a2015-03-04 16:40:21 -08001468 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
Miao Wangc8e237e2015-02-20 18:36:32 -08001469}
1470
Stephen Hines414fa2c2014-04-17 01:02:42 -07001471// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Samsfb9f82c2011-01-12 14:53:25 -08001472static void
Tim Murray460a0492013-11-19 12:45:54 -08001473nAllocationRead2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
Miao Wang87e908d2015-03-02 15:15:15 -08001474 jint w, jint h, jobject data, jint sizeBytes, jint dataType,
1475 jint mSize, jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001476{
Jason Sams21659ac2013-11-06 15:08:07 -08001477 RsAllocation *alloc = (RsAllocation *)_alloc;
1478 RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
Andreas Gampe67333922014-11-10 20:35:59 -08001479 if (kLogApi) {
1480 ALOGD("nAllocation2DRead, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1481 "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1482 }
Miao Wang87e908d2015-03-02 15:15:15 -08001483 int count = w * h;
1484 PER_ARRAY_TYPE(0, rsAllocation2DRead, false,
1485 (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
Jason Sams40a29e82009-08-10 14:55:26 -07001486}
Miao Wang87e908d2015-03-02 15:15:15 -08001487
Miao Wangc8e237e2015-02-20 18:36:32 -08001488// Copies from the Allocation pointed to by _alloc into the Java object data.
1489static void
1490nAllocationRead3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001491 jint w, jint h, jint d, jobject data, int sizeBytes, int dataType,
1492 jint mSize, jboolean usePadding)
Miao Wangc8e237e2015-02-20 18:36:32 -08001493{
1494 RsAllocation *alloc = (RsAllocation *)_alloc;
1495 if (kLogApi) {
1496 ALOGD("nAllocation3DRead, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i),"
1497 " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff,
1498 lod, w, h, d, sizeBytes);
1499 }
Miao Wang87e908d2015-03-02 15:15:15 -08001500 int count = w * h * d;
1501 PER_ARRAY_TYPE(nullptr, rsAllocation3DRead, false,
1502 (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
Miao Wangc8e237e2015-02-20 18:36:32 -08001503}
Jason Samsd19f10d2009-05-22 14:03:28 -07001504
Tim Murray460a0492013-11-19 12:45:54 -08001505static jlong
1506nAllocationGetType(JNIEnv *_env, jobject _this, jlong con, jlong a)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001507{
Andreas Gampe67333922014-11-10 20:35:59 -08001508 if (kLogApi) {
1509 ALOGD("nAllocationGetType, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
1510 }
Tim Murray3aa89c12014-08-18 17:51:22 -07001511 return (jlong)(uintptr_t) rsaAllocationGetType((RsContext)con, (RsAllocation)a);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001512}
1513
Jason Sams5edc6082010-10-05 13:32:49 -07001514static void
Tim Murray460a0492013-11-19 12:45:54 -08001515nAllocationResize1D(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jint dimX)
Jason Sams5edc6082010-10-05 13:32:49 -07001516{
Andreas Gampe67333922014-11-10 20:35:59 -08001517 if (kLogApi) {
1518 ALOGD("nAllocationResize1D, con(%p), alloc(%p), sizeX(%i)", (RsContext)con,
1519 (RsAllocation)alloc, dimX);
1520 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001521 rsAllocationResize1D((RsContext)con, (RsAllocation)alloc, dimX);
Jason Sams5edc6082010-10-05 13:32:49 -07001522}
1523
Jason Sams46ba27e32015-02-06 17:45:15 -08001524
1525static jlong
1526nAllocationAdapterCreate(JNIEnv *_env, jobject _this, jlong con, jlong basealloc, jlong type)
1527{
1528 if (kLogApi) {
1529 ALOGD("nAllocationAdapterCreate, con(%p), base(%p), type(%p)",
1530 (RsContext)con, (RsAllocation)basealloc, (RsElement)type);
1531 }
1532 return (jlong)(uintptr_t) rsAllocationAdapterCreate((RsContext)con, (RsType)type,
1533 (RsAllocation)basealloc);
1534
1535}
1536
1537static void
1538nAllocationAdapterOffset(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
1539 jint x, jint y, jint z, jint face, jint lod,
1540 jint a1, jint a2, jint a3, jint a4)
1541{
1542 uint32_t params[] = {
1543 (uint32_t)x, (uint32_t)y, (uint32_t)z, (uint32_t)face,
1544 (uint32_t)lod, (uint32_t)a1, (uint32_t)a2, (uint32_t)a3, (uint32_t)a4
1545 };
1546 if (kLogApi) {
1547 ALOGD("nAllocationAdapterOffset, con(%p), alloc(%p), x(%i), y(%i), z(%i), face(%i), lod(%i), arrays(%i %i %i %i)",
1548 (RsContext)con, (RsAllocation)alloc, x, y, z, face, lod, a1, a2, a3, a4);
1549 }
1550 rsAllocationAdapterOffset((RsContext)con, (RsAllocation)alloc,
1551 params, sizeof(params));
1552}
1553
1554
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001555// -----------------------------------
1556
Tim Murray460a0492013-11-19 12:45:54 -08001557static jlong
1558nFileA3DCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con, jlong native_asset)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001559{
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001560 Asset* asset = reinterpret_cast<Asset*>(native_asset);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001561 ALOGV("______nFileA3D %p", asset);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001562
Tim Murray3aa89c12014-08-18 17:51:22 -07001563 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromMemory((RsContext)con, asset->getBuffer(false), asset->getLength());
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001564 return id;
1565}
1566
Tim Murray460a0492013-11-19 12:45:54 -08001567static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001568nFileA3DCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001569{
1570 AssetManager* mgr = assetManagerForJavaObject(_env, _assetMgr);
Chris Wailes488230c32014-08-14 11:22:40 -07001571 if (mgr == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001572 return 0;
1573 }
1574
1575 AutoJavaStringToUTF8 str(_env, _path);
1576 Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
Chris Wailes488230c32014-08-14 11:22:40 -07001577 if (asset == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001578 return 0;
1579 }
1580
Tim Murray3aa89c12014-08-18 17:51:22 -07001581 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromAsset((RsContext)con, asset);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001582 return id;
1583}
1584
Tim Murray460a0492013-11-19 12:45:54 -08001585static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001586nFileA3DCreateFromFile(JNIEnv *_env, jobject _this, jlong con, jstring fileName)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001587{
1588 AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
Tim Murray3aa89c12014-08-18 17:51:22 -07001589 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromFile((RsContext)con, fileNameUTF.c_str());
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001590
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001591 return id;
1592}
1593
Tim Murray460a0492013-11-19 12:45:54 -08001594static jint
1595nFileA3DGetNumIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001596{
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001597 int32_t numEntries = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001598 rsaFileA3DGetNumIndexEntries((RsContext)con, &numEntries, (RsFile)fileA3D);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001599 return (jint)numEntries;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001600}
1601
1602static void
Tim Murray460a0492013-11-19 12:45:54 -08001603nFileA3DGetIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint numEntries, jintArray _ids, jobjectArray _entries)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001604{
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001605 ALOGV("______nFileA3D %p", (RsFile) fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001606 RsFileIndexEntry *fileEntries = (RsFileIndexEntry*)malloc((uint32_t)numEntries * sizeof(RsFileIndexEntry));
1607
Tim Murrayeff663f2013-11-15 13:08:30 -08001608 rsaFileA3DGetIndexEntries((RsContext)con, fileEntries, (uint32_t)numEntries, (RsFile)fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001609
1610 for(jint i = 0; i < numEntries; i ++) {
1611 _env->SetObjectArrayElement(_entries, i, _env->NewStringUTF(fileEntries[i].objectName));
1612 _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&fileEntries[i].classID);
1613 }
1614
1615 free(fileEntries);
1616}
1617
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001618static jlong
Tim Murray460a0492013-11-19 12:45:54 -08001619nFileA3DGetEntryByIndex(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint index)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001620{
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001621 ALOGV("______nFileA3D %p", (RsFile) fileA3D);
Tim Murray3aa89c12014-08-18 17:51:22 -07001622 jlong id = (jlong)(uintptr_t)rsaFileA3DGetEntryByIndex((RsContext)con, (uint32_t)index, (RsFile)fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001623 return id;
1624}
Jason Samsd19f10d2009-05-22 14:03:28 -07001625
1626// -----------------------------------
1627
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001628static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001629nFontCreateFromFile(JNIEnv *_env, jobject _this, jlong con,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001630 jstring fileName, jfloat fontSize, jint dpi)
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001631{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001632 AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
Tim Murray3aa89c12014-08-18 17:51:22 -07001633 jlong id = (jlong)(uintptr_t)rsFontCreateFromFile((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001634 fileNameUTF.c_str(), fileNameUTF.length(),
1635 fontSize, dpi);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001636
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001637 return id;
1638}
1639
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001640static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001641nFontCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con,
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001642 jstring name, jfloat fontSize, jint dpi, jlong native_asset)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001643{
1644 Asset* asset = reinterpret_cast<Asset*>(native_asset);
1645 AutoJavaStringToUTF8 nameUTF(_env, name);
1646
Tim Murray3aa89c12014-08-18 17:51:22 -07001647 jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001648 nameUTF.c_str(), nameUTF.length(),
1649 fontSize, dpi,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001650 asset->getBuffer(false), asset->getLength());
1651 return id;
1652}
1653
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001654static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001655nFontCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001656 jfloat fontSize, jint dpi)
1657{
1658 AssetManager* mgr = assetManagerForJavaObject(_env, _assetMgr);
Chris Wailes488230c32014-08-14 11:22:40 -07001659 if (mgr == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001660 return 0;
1661 }
1662
1663 AutoJavaStringToUTF8 str(_env, _path);
1664 Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
Chris Wailes488230c32014-08-14 11:22:40 -07001665 if (asset == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001666 return 0;
1667 }
1668
Tim Murray3aa89c12014-08-18 17:51:22 -07001669 jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001670 str.c_str(), str.length(),
1671 fontSize, dpi,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001672 asset->getBuffer(false), asset->getLength());
1673 delete asset;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001674 return id;
1675}
1676
Jason Samsbd1c3ad2009-08-03 16:03:08 -07001677// -----------------------------------
1678
1679static void
Tim Murray460a0492013-11-19 12:45:54 -08001680nScriptBindAllocation(JNIEnv *_env, jobject _this, jlong con, jlong script, jlong alloc, jint slot)
Jason Samsd19f10d2009-05-22 14:03:28 -07001681{
Andreas Gampe67333922014-11-10 20:35:59 -08001682 if (kLogApi) {
1683 ALOGD("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", (RsContext)con,
1684 (RsScript)script, (RsAllocation)alloc, slot);
1685 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001686 rsScriptBindAllocation((RsContext)con, (RsScript)script, (RsAllocation)alloc, slot);
Jason Samsd19f10d2009-05-22 14:03:28 -07001687}
1688
1689static void
Tim Murray460a0492013-11-19 12:45:54 -08001690nScriptSetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jint val)
Jason Sams4d339932010-05-11 14:03:58 -07001691{
Andreas Gampe67333922014-11-10 20:35:59 -08001692 if (kLogApi) {
1693 ALOGD("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i)", (RsContext)con, (void *)script,
1694 slot, val);
1695 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001696 rsScriptSetVarI((RsContext)con, (RsScript)script, slot, val);
Jason Sams4d339932010-05-11 14:03:58 -07001697}
1698
Tim Murray7c4caad2013-04-10 16:21:40 -07001699static jint
Tim Murray460a0492013-11-19 12:45:54 -08001700nScriptGetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001701{
Andreas Gampe67333922014-11-10 20:35:59 -08001702 if (kLogApi) {
1703 ALOGD("nScriptGetVarI, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1704 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001705 int value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001706 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001707 return value;
1708}
1709
Jason Sams4d339932010-05-11 14:03:58 -07001710static void
Tim Murray460a0492013-11-19 12:45:54 -08001711nScriptSetVarObj(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
Jason Sams6f4cf0b2010-11-16 17:37:02 -08001712{
Andreas Gampe67333922014-11-10 20:35:59 -08001713 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001714 ALOGD("nScriptSetVarObj, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
Andreas Gampe67333922014-11-10 20:35:59 -08001715 slot, val);
1716 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001717 rsScriptSetVarObj((RsContext)con, (RsScript)script, slot, (RsObjectBase)val);
Jason Sams6f4cf0b2010-11-16 17:37:02 -08001718}
1719
1720static void
Tim Murray460a0492013-11-19 12:45:54 -08001721nScriptSetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
Stephen Hines031ec58c2010-10-11 10:54:21 -07001722{
Andreas Gampe67333922014-11-10 20:35:59 -08001723 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001724 ALOGD("nScriptSetVarJ, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
Andreas Gampe67333922014-11-10 20:35:59 -08001725 slot, val);
1726 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001727 rsScriptSetVarJ((RsContext)con, (RsScript)script, slot, val);
Stephen Hines031ec58c2010-10-11 10:54:21 -07001728}
1729
Tim Murray7c4caad2013-04-10 16:21:40 -07001730static jlong
Tim Murray460a0492013-11-19 12:45:54 -08001731nScriptGetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001732{
Andreas Gampe67333922014-11-10 20:35:59 -08001733 if (kLogApi) {
1734 ALOGD("nScriptGetVarJ, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1735 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001736 jlong value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001737 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001738 return value;
1739}
1740
Stephen Hines031ec58c2010-10-11 10:54:21 -07001741static void
Tim Murray460a0492013-11-19 12:45:54 -08001742nScriptSetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, float val)
Jason Sams4d339932010-05-11 14:03:58 -07001743{
Andreas Gampe67333922014-11-10 20:35:59 -08001744 if (kLogApi) {
1745 ALOGD("nScriptSetVarF, con(%p), s(%p), slot(%i), val(%f)", (RsContext)con, (void *)script,
1746 slot, val);
1747 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001748 rsScriptSetVarF((RsContext)con, (RsScript)script, slot, val);
Jason Sams4d339932010-05-11 14:03:58 -07001749}
1750
Tim Murray7c4caad2013-04-10 16:21:40 -07001751static jfloat
Tim Murray460a0492013-11-19 12:45:54 -08001752nScriptGetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001753{
Andreas Gampe67333922014-11-10 20:35:59 -08001754 if (kLogApi) {
1755 ALOGD("nScriptGetVarF, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1756 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001757 jfloat value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001758 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001759 return value;
1760}
1761
Jason Sams4d339932010-05-11 14:03:58 -07001762static void
Tim Murray460a0492013-11-19 12:45:54 -08001763nScriptSetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, double val)
Stephen Hinesca54ec32010-09-20 17:20:30 -07001764{
Andreas Gampe67333922014-11-10 20:35:59 -08001765 if (kLogApi) {
1766 ALOGD("nScriptSetVarD, con(%p), s(%p), slot(%i), val(%lf)", (RsContext)con, (void *)script,
1767 slot, val);
1768 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001769 rsScriptSetVarD((RsContext)con, (RsScript)script, slot, val);
Stephen Hinesca54ec32010-09-20 17:20:30 -07001770}
1771
Tim Murray7c4caad2013-04-10 16:21:40 -07001772static jdouble
Tim Murray460a0492013-11-19 12:45:54 -08001773nScriptGetVarD(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("nScriptGetVarD, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1777 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001778 jdouble 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
Stephen Hinesca54ec32010-09-20 17:20:30 -07001783static void
Tim Murray460a0492013-11-19 12:45:54 -08001784nScriptSetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Jason Sams4d339932010-05-11 14:03:58 -07001785{
Andreas Gampe67333922014-11-10 20:35:59 -08001786 if (kLogApi) {
1787 ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1788 }
Jason Sams4d339932010-05-11 14:03:58 -07001789 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001790 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Tim Murrayeff663f2013-11-15 13:08:30 -08001791 rsScriptSetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
Jason Sams4d339932010-05-11 14:03:58 -07001792 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1793}
1794
Stephen Hinesadeb8092012-04-20 14:26:06 -07001795static void
Tim Murray460a0492013-11-19 12:45:54 -08001796nScriptGetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Tim Murray7c4caad2013-04-10 16:21:40 -07001797{
Andreas Gampe67333922014-11-10 20:35:59 -08001798 if (kLogApi) {
1799 ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1800 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001801 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001802 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Tim Murrayeff663f2013-11-15 13:08:30 -08001803 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
Stephen Hines414fa2c2014-04-17 01:02:42 -07001804 _env->ReleaseByteArrayElements(data, ptr, 0);
Tim Murray7c4caad2013-04-10 16:21:40 -07001805}
1806
1807static void
Andreas Gampe67333922014-11-10 20:35:59 -08001808nScriptSetVarVE(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data,
1809 jlong elem, jintArray dims)
Stephen Hinesadeb8092012-04-20 14:26:06 -07001810{
Andreas Gampe67333922014-11-10 20:35:59 -08001811 if (kLogApi) {
1812 ALOGD("nScriptSetVarVE, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1813 }
Stephen Hinesadeb8092012-04-20 14:26:06 -07001814 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001815 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Stephen Hinesadeb8092012-04-20 14:26:06 -07001816 jint dimsLen = _env->GetArrayLength(dims) * sizeof(int);
Chris Wailes488230c32014-08-14 11:22:40 -07001817 jint *dimsPtr = _env->GetIntArrayElements(dims, nullptr);
Tim Murrayeff663f2013-11-15 13:08:30 -08001818 rsScriptSetVarVE((RsContext)con, (RsScript)script, slot, ptr, len, (RsElement)elem,
Stephen Hinesbc5d3ee2014-06-25 00:03:39 -07001819 (const uint32_t*) dimsPtr, dimsLen);
Stephen Hinesadeb8092012-04-20 14:26:06 -07001820 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1821 _env->ReleaseIntArrayElements(dims, dimsPtr, JNI_ABORT);
1822}
1823
Jason Samsd19f10d2009-05-22 14:03:28 -07001824
1825static void
Tim Murray460a0492013-11-19 12:45:54 -08001826nScriptSetTimeZone(JNIEnv *_env, jobject _this, jlong con, jlong script, jbyteArray timeZone)
Jason Samsd19f10d2009-05-22 14:03:28 -07001827{
Andreas Gampe67333922014-11-10 20:35:59 -08001828 if (kLogApi) {
1829 ALOGD("nScriptCSetTimeZone, con(%p), s(%p)", (RsContext)con, (void *)script);
1830 }
Romain Guy584a3752009-07-30 18:45:01 -07001831
1832 jint length = _env->GetArrayLength(timeZone);
1833 jbyte* timeZone_ptr;
1834 timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0);
1835
Tim Murrayeff663f2013-11-15 13:08:30 -08001836 rsScriptSetTimeZone((RsContext)con, (RsScript)script, (const char *)timeZone_ptr, length);
Romain Guy584a3752009-07-30 18:45:01 -07001837
1838 if (timeZone_ptr) {
1839 _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0);
1840 }
1841}
1842
Jason Samsfbf0b9e2009-08-13 12:59:04 -07001843static void
Tim Murray460a0492013-11-19 12:45:54 -08001844nScriptInvoke(JNIEnv *_env, jobject _this, jlong con, jlong obj, jint slot)
Jason Samsbe2e8412009-09-16 15:04:38 -07001845{
Andreas Gampe67333922014-11-10 20:35:59 -08001846 if (kLogApi) {
1847 ALOGD("nScriptInvoke, con(%p), script(%p)", (RsContext)con, (void *)obj);
1848 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001849 rsScriptInvoke((RsContext)con, (RsScript)obj, slot);
Jason Samsbe2e8412009-09-16 15:04:38 -07001850}
1851
1852static void
Tim Murray460a0492013-11-19 12:45:54 -08001853nScriptInvokeV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Jason Sams4d339932010-05-11 14:03:58 -07001854{
Andreas Gampe67333922014-11-10 20:35:59 -08001855 if (kLogApi) {
1856 ALOGD("nScriptInvokeV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1857 }
Jason Sams4d339932010-05-11 14:03:58 -07001858 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001859 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Tim Murrayeff663f2013-11-15 13:08:30 -08001860 rsScriptInvokeV((RsContext)con, (RsScript)script, slot, ptr, len);
Jason Sams4d339932010-05-11 14:03:58 -07001861 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1862}
1863
Jason Sams6e494d32011-04-27 16:33:11 -07001864static void
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001865nScriptForEach(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot,
1866 jlongArray ains, jlong aout, jbyteArray params,
1867 jintArray limits)
Jason Sams6e494d32011-04-27 16:33:11 -07001868{
Andreas Gampe67333922014-11-10 20:35:59 -08001869 if (kLogApi) {
Jason Samsbc5c64b2015-04-16 15:13:52 -07001870 ALOGD("nScriptForEach, con(%p), s(%p), slot(%i) ains(%p) aout(%lli)", (RsContext)con, (void *)script, slot, ains, aout);
Andreas Gampe67333922014-11-10 20:35:59 -08001871 }
Jason Sams6e494d32011-04-27 16:33:11 -07001872
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001873 jint in_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07001874 jlong *in_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07001875
Chris Wailes488230c32014-08-14 11:22:40 -07001876 RsAllocation *in_allocs = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07001877
Chris Wailes488230c32014-08-14 11:22:40 -07001878 if (ains != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001879 in_len = _env->GetArrayLength(ains);
Yang Ni4e90b9b2015-04-30 16:13:54 -07001880 if (in_len > (jint)kMaxNumberKernelArguments) {
1881 ALOGE("Too many arguments in kernel launch.");
1882 // TODO (b/20758983): Report back to Java and throw an exception
1883 return;
1884 }
Chris Wailes94961062014-06-11 12:01:28 -07001885
Yang Ni4e90b9b2015-04-30 16:13:54 -07001886 // TODO (b/20760800): Check in_ptr is not null
1887 in_ptr = _env->GetLongArrayElements(ains, nullptr);
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001888 if (sizeof(RsAllocation) == sizeof(jlong)) {
1889 in_allocs = (RsAllocation*)in_ptr;
Chris Wailes94961062014-06-11 12:01:28 -07001890
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001891 } else {
1892 // Convert from 64-bit jlong types to the native pointer type.
Chris Wailes94961062014-06-11 12:01:28 -07001893
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001894 in_allocs = (RsAllocation*)alloca(in_len * sizeof(RsAllocation));
Yang Ni4e90b9b2015-04-30 16:13:54 -07001895 if (in_allocs == nullptr) {
1896 ALOGE("Failed launching kernel for lack of memory.");
1897 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
1898 return;
1899 }
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001900
1901 for (int index = in_len; --index >= 0;) {
1902 in_allocs[index] = (RsAllocation)in_ptr[index];
1903 }
1904 }
Chris Wailes94961062014-06-11 12:01:28 -07001905 }
1906
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001907 jint param_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07001908 jbyte *param_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07001909
Chris Wailes488230c32014-08-14 11:22:40 -07001910 if (params != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001911 param_len = _env->GetArrayLength(params);
Chris Wailes488230c32014-08-14 11:22:40 -07001912 param_ptr = _env->GetByteArrayElements(params, nullptr);
Chris Wailes94961062014-06-11 12:01:28 -07001913 }
1914
Chris Wailes488230c32014-08-14 11:22:40 -07001915 RsScriptCall sc, *sca = nullptr;
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001916 uint32_t sc_size = 0;
Chris Wailes94961062014-06-11 12:01:28 -07001917
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001918 jint limit_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07001919 jint *limit_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07001920
Chris Wailes488230c32014-08-14 11:22:40 -07001921 if (limits != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001922 limit_len = _env->GetArrayLength(limits);
Chris Wailes488230c32014-08-14 11:22:40 -07001923 limit_ptr = _env->GetIntArrayElements(limits, nullptr);
Chris Wailes94961062014-06-11 12:01:28 -07001924
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001925 assert(limit_len == 6);
Andreas Gampe67333922014-11-10 20:35:59 -08001926 UNUSED(limit_len); // As the assert might not be compiled.
Chris Wailes94961062014-06-11 12:01:28 -07001927
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001928 sc.xStart = limit_ptr[0];
1929 sc.xEnd = limit_ptr[1];
1930 sc.yStart = limit_ptr[2];
1931 sc.yEnd = limit_ptr[3];
1932 sc.zStart = limit_ptr[4];
1933 sc.zEnd = limit_ptr[5];
1934 sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
Jason Sams14331ab2015-01-26 18:14:36 -08001935 sc.arrayStart = 0;
1936 sc.arrayEnd = 0;
1937 sc.array2Start = 0;
1938 sc.array2End = 0;
1939 sc.array3Start = 0;
1940 sc.array3End = 0;
1941 sc.array4Start = 0;
1942 sc.array4End = 0;
Chris Wailes94961062014-06-11 12:01:28 -07001943
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001944 sca = &sc;
Chris Wailes94961062014-06-11 12:01:28 -07001945 }
1946
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001947 rsScriptForEachMulti((RsContext)con, (RsScript)script, slot,
1948 in_allocs, in_len, (RsAllocation)aout,
1949 param_ptr, param_len, sca, sc_size);
Chris Wailes94961062014-06-11 12:01:28 -07001950
Chris Wailes488230c32014-08-14 11:22:40 -07001951 if (ains != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001952 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
Chris Wailes94961062014-06-11 12:01:28 -07001953 }
1954
Chris Wailes488230c32014-08-14 11:22:40 -07001955 if (params != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001956 _env->ReleaseByteArrayElements(params, param_ptr, JNI_ABORT);
1957 }
1958
Chris Wailes488230c32014-08-14 11:22:40 -07001959 if (limits != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001960 _env->ReleaseIntArrayElements(limits, limit_ptr, JNI_ABORT);
1961 }
Chris Wailes94961062014-06-11 12:01:28 -07001962}
1963
Jason Sams22534172009-08-04 16:58:20 -07001964// -----------------------------------
1965
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001966static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001967nScriptCCreate(JNIEnv *_env, jobject _this, jlong con,
Jason Samse4a06c52011-03-16 16:29:28 -07001968 jstring resName, jstring cacheDir,
1969 jbyteArray scriptRef, jint length)
Jason Sams22534172009-08-04 16:58:20 -07001970{
Andreas Gampe67333922014-11-10 20:35:59 -08001971 if (kLogApi) {
1972 ALOGD("nScriptCCreate, con(%p)", (RsContext)con);
1973 }
Jason Sams22534172009-08-04 16:58:20 -07001974
Jason Samse4a06c52011-03-16 16:29:28 -07001975 AutoJavaStringToUTF8 resNameUTF(_env, resName);
1976 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001977 jlong ret = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07001978 jbyte* script_ptr = nullptr;
Jack Palevich43702d82009-05-28 13:38:16 -07001979 jint _exception = 0;
1980 jint remaining;
Jack Palevich43702d82009-05-28 13:38:16 -07001981 if (!scriptRef) {
1982 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07001983 //jniThrowException(_env, "java/lang/IllegalArgumentException", "script == null");
Jack Palevich43702d82009-05-28 13:38:16 -07001984 goto exit;
1985 }
Jack Palevich43702d82009-05-28 13:38:16 -07001986 if (length < 0) {
1987 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07001988 //jniThrowException(_env, "java/lang/IllegalArgumentException", "length < 0");
Jack Palevich43702d82009-05-28 13:38:16 -07001989 goto exit;
1990 }
Jason Samse4a06c52011-03-16 16:29:28 -07001991 remaining = _env->GetArrayLength(scriptRef);
Jack Palevich43702d82009-05-28 13:38:16 -07001992 if (remaining < length) {
1993 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07001994 //jniThrowException(_env, "java/lang/IllegalArgumentException",
1995 // "length > script.length - offset");
Jack Palevich43702d82009-05-28 13:38:16 -07001996 goto exit;
1997 }
Jason Samse4a06c52011-03-16 16:29:28 -07001998 script_ptr = (jbyte *)
Jack Palevich43702d82009-05-28 13:38:16 -07001999 _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
Jack Palevich43702d82009-05-28 13:38:16 -07002000
Tim Murrayeff663f2013-11-15 13:08:30 -08002001 //rsScriptCSetText((RsContext)con, (const char *)script_ptr, length);
Jason Samse4a06c52011-03-16 16:29:28 -07002002
Tim Murray3aa89c12014-08-18 17:51:22 -07002003 ret = (jlong)(uintptr_t)rsScriptCCreate((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07002004 resNameUTF.c_str(), resNameUTF.length(),
2005 cacheDirUTF.c_str(), cacheDirUTF.length(),
Jason Samse4a06c52011-03-16 16:29:28 -07002006 (const char *)script_ptr, length);
Jason Sams39ddc9502009-06-05 17:35:09 -07002007
Jack Palevich43702d82009-05-28 13:38:16 -07002008exit:
Jason Samse4a06c52011-03-16 16:29:28 -07002009 if (script_ptr) {
2010 _env->ReleasePrimitiveArrayCritical(scriptRef, script_ptr,
Jack Palevich43702d82009-05-28 13:38:16 -07002011 _exception ? JNI_ABORT: 0);
2012 }
Jason Samsd19f10d2009-05-22 14:03:28 -07002013
Tim Murray3aa89c12014-08-18 17:51:22 -07002014 return (jlong)(uintptr_t)ret;
Jason Samsd19f10d2009-05-22 14:03:28 -07002015}
2016
Tim Murray460a0492013-11-19 12:45:54 -08002017static jlong
2018nScriptIntrinsicCreate(JNIEnv *_env, jobject _this, jlong con, jint id, jlong eid)
Jason Sams6ab97682012-08-10 12:09:43 -07002019{
Andreas Gampe67333922014-11-10 20:35:59 -08002020 if (kLogApi) {
2021 ALOGD("nScriptIntrinsicCreate, con(%p) id(%i) element(%p)", (RsContext)con, id,
2022 (void *)eid);
2023 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002024 return (jlong)(uintptr_t)rsScriptIntrinsicCreate((RsContext)con, id, (RsElement)eid);
Jason Sams6ab97682012-08-10 12:09:43 -07002025}
2026
Tim Murray460a0492013-11-19 12:45:54 -08002027static jlong
2028nScriptKernelIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot, jint sig)
Jason Sams08a81582012-09-18 12:32:10 -07002029{
Andreas Gampe67333922014-11-10 20:35:59 -08002030 if (kLogApi) {
2031 ALOGD("nScriptKernelIDCreate, con(%p) script(%p), slot(%i), sig(%i)", (RsContext)con,
2032 (void *)sid, slot, sig);
2033 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002034 return (jlong)(uintptr_t)rsScriptKernelIDCreate((RsContext)con, (RsScript)sid, slot, sig);
Jason Sams08a81582012-09-18 12:32:10 -07002035}
2036
Tim Murray460a0492013-11-19 12:45:54 -08002037static jlong
Yang Nibe392ad2015-01-23 17:16:02 -08002038nScriptInvokeIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
2039{
2040 if (kLogApi) {
Elliott Hughes7ff53fa2015-02-05 21:36:10 -08002041 ALOGD("nScriptInvokeIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con,
Yang Nibe392ad2015-01-23 17:16:02 -08002042 (void *)sid, slot);
2043 }
2044 return (jlong)(uintptr_t)rsScriptInvokeIDCreate((RsContext)con, (RsScript)sid, slot);
2045}
2046
2047static jlong
Tim Murray460a0492013-11-19 12:45:54 -08002048nScriptFieldIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
Jason Sams08a81582012-09-18 12:32:10 -07002049{
Andreas Gampe67333922014-11-10 20:35:59 -08002050 if (kLogApi) {
2051 ALOGD("nScriptFieldIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con, (void *)sid,
2052 slot);
2053 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002054 return (jlong)(uintptr_t)rsScriptFieldIDCreate((RsContext)con, (RsScript)sid, slot);
Jason Sams08a81582012-09-18 12:32:10 -07002055}
2056
Tim Murray460a0492013-11-19 12:45:54 -08002057static jlong
Ashok Bhat98071552014-02-12 09:54:43 +00002058nScriptGroupCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _kernels, jlongArray _src,
2059 jlongArray _dstk, jlongArray _dstf, jlongArray _types)
Jason Sams08a81582012-09-18 12:32:10 -07002060{
Andreas Gampe67333922014-11-10 20:35:59 -08002061 if (kLogApi) {
2062 ALOGD("nScriptGroupCreate, con(%p)", (RsContext)con);
2063 }
Jason Sams08a81582012-09-18 12:32:10 -07002064
Ashok Bhat98071552014-02-12 09:54:43 +00002065 jint kernelsLen = _env->GetArrayLength(_kernels);
Chris Wailes488230c32014-08-14 11:22:40 -07002066 jlong *jKernelsPtr = _env->GetLongArrayElements(_kernels, nullptr);
Ashok Bhat98071552014-02-12 09:54:43 +00002067 RsScriptKernelID* kernelsPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * kernelsLen);
2068 for(int i = 0; i < kernelsLen; ++i) {
2069 kernelsPtr[i] = (RsScriptKernelID)jKernelsPtr[i];
2070 }
Jason Sams08a81582012-09-18 12:32:10 -07002071
Ashok Bhat98071552014-02-12 09:54:43 +00002072 jint srcLen = _env->GetArrayLength(_src);
Chris Wailes488230c32014-08-14 11:22:40 -07002073 jlong *jSrcPtr = _env->GetLongArrayElements(_src, nullptr);
Ashok Bhat98071552014-02-12 09:54:43 +00002074 RsScriptKernelID* srcPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * srcLen);
2075 for(int i = 0; i < srcLen; ++i) {
2076 srcPtr[i] = (RsScriptKernelID)jSrcPtr[i];
2077 }
Jason Sams08a81582012-09-18 12:32:10 -07002078
Ashok Bhat98071552014-02-12 09:54:43 +00002079 jint dstkLen = _env->GetArrayLength(_dstk);
Chris Wailes488230c32014-08-14 11:22:40 -07002080 jlong *jDstkPtr = _env->GetLongArrayElements(_dstk, nullptr);
Ashok Bhat98071552014-02-12 09:54:43 +00002081 RsScriptKernelID* dstkPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstkLen);
2082 for(int i = 0; i < dstkLen; ++i) {
2083 dstkPtr[i] = (RsScriptKernelID)jDstkPtr[i];
2084 }
2085
2086 jint dstfLen = _env->GetArrayLength(_dstf);
Chris Wailes488230c32014-08-14 11:22:40 -07002087 jlong *jDstfPtr = _env->GetLongArrayElements(_dstf, nullptr);
Ashok Bhat98071552014-02-12 09:54:43 +00002088 RsScriptKernelID* dstfPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstfLen);
2089 for(int i = 0; i < dstfLen; ++i) {
2090 dstfPtr[i] = (RsScriptKernelID)jDstfPtr[i];
2091 }
2092
2093 jint typesLen = _env->GetArrayLength(_types);
Chris Wailes488230c32014-08-14 11:22:40 -07002094 jlong *jTypesPtr = _env->GetLongArrayElements(_types, nullptr);
Ashok Bhat98071552014-02-12 09:54:43 +00002095 RsType* typesPtr = (RsType*) malloc(sizeof(RsType) * typesLen);
2096 for(int i = 0; i < typesLen; ++i) {
2097 typesPtr[i] = (RsType)jTypesPtr[i];
2098 }
2099
Tim Murray3aa89c12014-08-18 17:51:22 -07002100 jlong id = (jlong)(uintptr_t)rsScriptGroupCreate((RsContext)con,
Ashok Bhat98071552014-02-12 09:54:43 +00002101 (RsScriptKernelID *)kernelsPtr, kernelsLen * sizeof(RsScriptKernelID),
2102 (RsScriptKernelID *)srcPtr, srcLen * sizeof(RsScriptKernelID),
2103 (RsScriptKernelID *)dstkPtr, dstkLen * sizeof(RsScriptKernelID),
2104 (RsScriptFieldID *)dstfPtr, dstfLen * sizeof(RsScriptKernelID),
2105 (RsType *)typesPtr, typesLen * sizeof(RsType));
2106
2107 free(kernelsPtr);
2108 free(srcPtr);
2109 free(dstkPtr);
2110 free(dstfPtr);
2111 free(typesPtr);
2112 _env->ReleaseLongArrayElements(_kernels, jKernelsPtr, 0);
2113 _env->ReleaseLongArrayElements(_src, jSrcPtr, 0);
2114 _env->ReleaseLongArrayElements(_dstk, jDstkPtr, 0);
2115 _env->ReleaseLongArrayElements(_dstf, jDstfPtr, 0);
2116 _env->ReleaseLongArrayElements(_types, jTypesPtr, 0);
Jason Sams08a81582012-09-18 12:32:10 -07002117 return id;
2118}
2119
2120static void
Tim Murray460a0492013-11-19 12:45:54 -08002121nScriptGroupSetInput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
Jason Sams08a81582012-09-18 12:32:10 -07002122{
Andreas Gampe67333922014-11-10 20:35:59 -08002123 if (kLogApi) {
2124 ALOGD("nScriptGroupSetInput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
2125 (void *)gid, (void *)kid, (void *)alloc);
2126 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002127 rsScriptGroupSetInput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
Jason Sams08a81582012-09-18 12:32:10 -07002128}
2129
2130static void
Tim Murray460a0492013-11-19 12:45:54 -08002131nScriptGroupSetOutput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
Jason Sams08a81582012-09-18 12:32:10 -07002132{
Andreas Gampe67333922014-11-10 20:35:59 -08002133 if (kLogApi) {
2134 ALOGD("nScriptGroupSetOutput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
2135 (void *)gid, (void *)kid, (void *)alloc);
2136 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002137 rsScriptGroupSetOutput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
Jason Sams08a81582012-09-18 12:32:10 -07002138}
2139
2140static void
Tim Murray460a0492013-11-19 12:45:54 -08002141nScriptGroupExecute(JNIEnv *_env, jobject _this, jlong con, jlong gid)
Jason Sams08a81582012-09-18 12:32:10 -07002142{
Andreas Gampe67333922014-11-10 20:35:59 -08002143 if (kLogApi) {
2144 ALOGD("nScriptGroupSetOutput, con(%p) group(%p)", (RsContext)con, (void *)gid);
2145 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002146 rsScriptGroupExecute((RsContext)con, (RsScriptGroup)gid);
Jason Sams08a81582012-09-18 12:32:10 -07002147}
2148
Jason Samsd19f10d2009-05-22 14:03:28 -07002149// ---------------------------------------------------------------------------
2150
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002151static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002152nProgramStoreCreate(JNIEnv *_env, jobject _this, jlong con,
Jason Sams331bf9b2011-04-06 11:23:54 -07002153 jboolean colorMaskR, jboolean colorMaskG, jboolean colorMaskB, jboolean colorMaskA,
2154 jboolean depthMask, jboolean ditherEnable,
2155 jint srcFunc, jint destFunc,
2156 jint depthFunc)
Jason Samsd19f10d2009-05-22 14:03:28 -07002157{
Andreas Gampe67333922014-11-10 20:35:59 -08002158 if (kLogApi) {
2159 ALOGD("nProgramStoreCreate, con(%p)", (RsContext)con);
2160 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002161 return (jlong)(uintptr_t)rsProgramStoreCreate((RsContext)con, colorMaskR, colorMaskG, colorMaskB, colorMaskA,
Jason Sams331bf9b2011-04-06 11:23:54 -07002162 depthMask, ditherEnable, (RsBlendSrcFunc)srcFunc,
2163 (RsBlendDstFunc)destFunc, (RsDepthFunc)depthFunc);
Jason Samsd19f10d2009-05-22 14:03:28 -07002164}
2165
Jason Sams0011bcf2009-12-15 12:58:36 -08002166// ---------------------------------------------------------------------------
2167
2168static void
Tim Murray460a0492013-11-19 12:45:54 -08002169nProgramBindConstants(JNIEnv *_env, jobject _this, jlong con, jlong vpv, jint slot, jlong a)
Jason Sams0011bcf2009-12-15 12:58:36 -08002170{
Andreas Gampe67333922014-11-10 20:35:59 -08002171 if (kLogApi) {
2172 ALOGD("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", (RsContext)con,
2173 (RsProgramVertex)vpv, slot, (RsAllocation)a);
2174 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002175 rsProgramBindConstants((RsContext)con, (RsProgram)vpv, slot, (RsAllocation)a);
Jason Sams0011bcf2009-12-15 12:58:36 -08002176}
Jason Sams54c0ec12009-11-30 14:49:55 -08002177
Jason Sams68afd012009-12-17 16:55:08 -08002178static void
Tim Murray460a0492013-11-19 12:45:54 -08002179nProgramBindTexture(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
Jason Sams68afd012009-12-17 16:55:08 -08002180{
Andreas Gampe67333922014-11-10 20:35:59 -08002181 if (kLogApi) {
2182 ALOGD("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
2183 (RsProgramFragment)vpf, slot, (RsAllocation)a);
2184 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002185 rsProgramBindTexture((RsContext)con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
Jason Sams68afd012009-12-17 16:55:08 -08002186}
2187
2188static void
Tim Murray460a0492013-11-19 12:45:54 -08002189nProgramBindSampler(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
Jason Sams68afd012009-12-17 16:55:08 -08002190{
Andreas Gampe67333922014-11-10 20:35:59 -08002191 if (kLogApi) {
2192 ALOGD("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
2193 (RsProgramFragment)vpf, slot, (RsSampler)a);
2194 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002195 rsProgramBindSampler((RsContext)con, (RsProgramFragment)vpf, slot, (RsSampler)a);
Jason Sams68afd012009-12-17 16:55:08 -08002196}
2197
Jason Samsd19f10d2009-05-22 14:03:28 -07002198// ---------------------------------------------------------------------------
2199
Tim Murray460a0492013-11-19 12:45:54 -08002200static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002201nProgramFragmentCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
Ashok Bhat98071552014-02-12 09:54:43 +00002202 jobjectArray texNames, jlongArray params)
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002203{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08002204 AutoJavaStringToUTF8 shaderUTF(_env, shader);
Chris Wailes488230c32014-08-14 11:22:40 -07002205 jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002206 jint paramLen = _env->GetArrayLength(params);
2207
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002208 int texCount = _env->GetArrayLength(texNames);
2209 AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
2210 const char ** nameArray = names.c_str();
2211 size_t* sizeArray = names.c_str_len();
2212
Andreas Gampe67333922014-11-10 20:35:59 -08002213 if (kLogApi) {
2214 ALOGD("nProgramFragmentCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
2215 }
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002216
Ashok Bhat98071552014-02-12 09:54:43 +00002217 uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
2218 for(int i = 0; i < paramLen; ++i) {
2219 paramPtr[i] = (uintptr_t)jParamPtr[i];
2220 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002221 jlong ret = (jlong)(uintptr_t)rsProgramFragmentCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002222 nameArray, texCount, sizeArray,
Ashok Bhat98071552014-02-12 09:54:43 +00002223 paramPtr, paramLen);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002224
Ashok Bhat98071552014-02-12 09:54:43 +00002225 free(paramPtr);
2226 _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002227 return ret;
2228}
2229
2230
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002231// ---------------------------------------------------------------------------
2232
Tim Murray460a0492013-11-19 12:45:54 -08002233static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002234nProgramVertexCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
Ashok Bhat98071552014-02-12 09:54:43 +00002235 jobjectArray texNames, jlongArray params)
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002236{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08002237 AutoJavaStringToUTF8 shaderUTF(_env, shader);
Chris Wailes488230c32014-08-14 11:22:40 -07002238 jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
Jason Sams0011bcf2009-12-15 12:58:36 -08002239 jint paramLen = _env->GetArrayLength(params);
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002240
Andreas Gampe67333922014-11-10 20:35:59 -08002241 if (kLogApi) {
2242 ALOGD("nProgramVertexCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
2243 }
Jason Sams0011bcf2009-12-15 12:58:36 -08002244
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002245 int texCount = _env->GetArrayLength(texNames);
2246 AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
2247 const char ** nameArray = names.c_str();
2248 size_t* sizeArray = names.c_str_len();
2249
Ashok Bhat98071552014-02-12 09:54:43 +00002250 uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
2251 for(int i = 0; i < paramLen; ++i) {
2252 paramPtr[i] = (uintptr_t)jParamPtr[i];
2253 }
2254
Tim Murray3aa89c12014-08-18 17:51:22 -07002255 jlong ret = (jlong)(uintptr_t)rsProgramVertexCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002256 nameArray, texCount, sizeArray,
Ashok Bhat98071552014-02-12 09:54:43 +00002257 paramPtr, paramLen);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002258
Ashok Bhat98071552014-02-12 09:54:43 +00002259 free(paramPtr);
2260 _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
Jason Sams0011bcf2009-12-15 12:58:36 -08002261 return ret;
2262}
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002263
Jason Samsebfb4362009-09-23 13:57:02 -07002264// ---------------------------------------------------------------------------
2265
Tim Murray460a0492013-11-19 12:45:54 -08002266static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002267nProgramRasterCreate(JNIEnv *_env, jobject _this, jlong con, jboolean pointSprite, jint cull)
Jason Samsebfb4362009-09-23 13:57:02 -07002268{
Andreas Gampe67333922014-11-10 20:35:59 -08002269 if (kLogApi) {
2270 ALOGD("nProgramRasterCreate, con(%p), pointSprite(%i), cull(%i)", (RsContext)con,
2271 pointSprite, cull);
2272 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002273 return (jlong)(uintptr_t)rsProgramRasterCreate((RsContext)con, pointSprite, (RsCullMode)cull);
Jason Samsebfb4362009-09-23 13:57:02 -07002274}
2275
Jason Samsd19f10d2009-05-22 14:03:28 -07002276
2277// ---------------------------------------------------------------------------
2278
2279static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002280nContextBindRootScript(JNIEnv *_env, jobject _this, jlong con, jlong script)
Jason Samsd19f10d2009-05-22 14:03:28 -07002281{
Andreas Gampe67333922014-11-10 20:35:59 -08002282 if (kLogApi) {
2283 ALOGD("nContextBindRootScript, con(%p), script(%p)", (RsContext)con, (RsScript)script);
2284 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002285 rsContextBindRootScript((RsContext)con, (RsScript)script);
Jason Samsd19f10d2009-05-22 14:03:28 -07002286}
2287
2288static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002289nContextBindProgramStore(JNIEnv *_env, jobject _this, jlong con, jlong pfs)
Jason Samsd19f10d2009-05-22 14:03:28 -07002290{
Andreas Gampe67333922014-11-10 20:35:59 -08002291 if (kLogApi) {
2292 ALOGD("nContextBindProgramStore, con(%p), pfs(%p)", (RsContext)con, (RsProgramStore)pfs);
2293 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002294 rsContextBindProgramStore((RsContext)con, (RsProgramStore)pfs);
Jason Samsd19f10d2009-05-22 14:03:28 -07002295}
2296
2297static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002298nContextBindProgramFragment(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Samsd19f10d2009-05-22 14:03:28 -07002299{
Andreas Gampe67333922014-11-10 20:35:59 -08002300 if (kLogApi) {
2301 ALOGD("nContextBindProgramFragment, con(%p), pf(%p)", (RsContext)con,
2302 (RsProgramFragment)pf);
2303 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002304 rsContextBindProgramFragment((RsContext)con, (RsProgramFragment)pf);
Jason Samsd19f10d2009-05-22 14:03:28 -07002305}
2306
Jason Sams0826a6f2009-06-15 19:04:56 -07002307static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002308nContextBindProgramVertex(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Sams0826a6f2009-06-15 19:04:56 -07002309{
Andreas Gampe67333922014-11-10 20:35:59 -08002310 if (kLogApi) {
2311 ALOGD("nContextBindProgramVertex, con(%p), pf(%p)", (RsContext)con, (RsProgramVertex)pf);
2312 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002313 rsContextBindProgramVertex((RsContext)con, (RsProgramVertex)pf);
Jason Sams0826a6f2009-06-15 19:04:56 -07002314}
2315
Joe Onoratod7b37742009-08-09 22:57:44 -07002316static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002317nContextBindProgramRaster(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Samsebfb4362009-09-23 13:57:02 -07002318{
Andreas Gampe67333922014-11-10 20:35:59 -08002319 if (kLogApi) {
2320 ALOGD("nContextBindProgramRaster, con(%p), pf(%p)", (RsContext)con, (RsProgramRaster)pf);
2321 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002322 rsContextBindProgramRaster((RsContext)con, (RsProgramRaster)pf);
Jason Samsebfb4362009-09-23 13:57:02 -07002323}
2324
Joe Onoratod7b37742009-08-09 22:57:44 -07002325
Jason Sams02fb2cb2009-05-28 15:37:57 -07002326// ---------------------------------------------------------------------------
2327
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002328static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002329nSamplerCreate(JNIEnv *_env, jobject _this, jlong con, jint magFilter, jint minFilter,
Alex Sakhartchouka89094a2011-05-04 17:45:36 -07002330 jint wrapS, jint wrapT, jint wrapR, jfloat aniso)
Jason Sams02fb2cb2009-05-28 15:37:57 -07002331{
Andreas Gampe67333922014-11-10 20:35:59 -08002332 if (kLogApi) {
2333 ALOGD("nSamplerCreate, con(%p)", (RsContext)con);
2334 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002335 return (jlong)(uintptr_t)rsSamplerCreate((RsContext)con,
Alex Sakhartchouka89094a2011-05-04 17:45:36 -07002336 (RsSamplerValue)magFilter,
2337 (RsSamplerValue)minFilter,
2338 (RsSamplerValue)wrapS,
2339 (RsSamplerValue)wrapT,
2340 (RsSamplerValue)wrapR,
2341 aniso);
Jason Sams02fb2cb2009-05-28 15:37:57 -07002342}
2343
Jason Samsbba134c2009-06-22 15:49:21 -07002344// ---------------------------------------------------------------------------
2345
Tim Murray460a0492013-11-19 12:45:54 -08002346static jlong
Ashok Bhat98071552014-02-12 09:54:43 +00002347nMeshCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _vtx, jlongArray _idx, jintArray _prim)
Jason Samsbba134c2009-06-22 15:49:21 -07002348{
Andreas Gampe67333922014-11-10 20:35:59 -08002349 if (kLogApi) {
2350 ALOGD("nMeshCreate, con(%p)", (RsContext)con);
2351 }
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002352
2353 jint vtxLen = _env->GetArrayLength(_vtx);
Chris Wailes488230c32014-08-14 11:22:40 -07002354 jlong *jVtxPtr = _env->GetLongArrayElements(_vtx, nullptr);
Ashok Bhat98071552014-02-12 09:54:43 +00002355 RsAllocation* vtxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * vtxLen);
2356 for(int i = 0; i < vtxLen; ++i) {
2357 vtxPtr[i] = (RsAllocation)(uintptr_t)jVtxPtr[i];
2358 }
2359
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002360 jint idxLen = _env->GetArrayLength(_idx);
Chris Wailes488230c32014-08-14 11:22:40 -07002361 jlong *jIdxPtr = _env->GetLongArrayElements(_idx, nullptr);
Ashok Bhat98071552014-02-12 09:54:43 +00002362 RsAllocation* idxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * idxLen);
2363 for(int i = 0; i < idxLen; ++i) {
2364 idxPtr[i] = (RsAllocation)(uintptr_t)jIdxPtr[i];
2365 }
2366
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002367 jint primLen = _env->GetArrayLength(_prim);
Chris Wailes488230c32014-08-14 11:22:40 -07002368 jint *primPtr = _env->GetIntArrayElements(_prim, nullptr);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002369
Tim Murray3aa89c12014-08-18 17:51:22 -07002370 jlong id = (jlong)(uintptr_t)rsMeshCreate((RsContext)con,
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002371 (RsAllocation *)vtxPtr, vtxLen,
2372 (RsAllocation *)idxPtr, idxLen,
2373 (uint32_t *)primPtr, primLen);
2374
Ashok Bhat98071552014-02-12 09:54:43 +00002375 free(vtxPtr);
2376 free(idxPtr);
2377 _env->ReleaseLongArrayElements(_vtx, jVtxPtr, 0);
2378 _env->ReleaseLongArrayElements(_idx, jIdxPtr, 0);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002379 _env->ReleaseIntArrayElements(_prim, primPtr, 0);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002380 return id;
2381}
2382
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002383static jint
Tim Murray460a0492013-11-19 12:45:54 -08002384nMeshGetVertexBufferCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002385{
Andreas Gampe67333922014-11-10 20:35:59 -08002386 if (kLogApi) {
2387 ALOGD("nMeshGetVertexBufferCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2388 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002389 jint vtxCount = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08002390 rsaMeshGetVertexBufferCount((RsContext)con, (RsMesh)mesh, &vtxCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002391 return vtxCount;
2392}
2393
2394static jint
Tim Murray460a0492013-11-19 12:45:54 -08002395nMeshGetIndexCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002396{
Andreas Gampe67333922014-11-10 20:35:59 -08002397 if (kLogApi) {
2398 ALOGD("nMeshGetIndexCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2399 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002400 jint idxCount = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08002401 rsaMeshGetIndexCount((RsContext)con, (RsMesh)mesh, &idxCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002402 return idxCount;
2403}
2404
2405static void
Ashok Bhat98071552014-02-12 09:54:43 +00002406nMeshGetVertices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _ids, jint numVtxIDs)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002407{
Andreas Gampe67333922014-11-10 20:35:59 -08002408 if (kLogApi) {
2409 ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2410 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002411
2412 RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numVtxIDs * sizeof(RsAllocation));
Tim Murrayeff663f2013-11-15 13:08:30 -08002413 rsaMeshGetVertices((RsContext)con, (RsMesh)mesh, allocs, (uint32_t)numVtxIDs);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002414
2415 for(jint i = 0; i < numVtxIDs; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07002416 const jlong alloc = (jlong)(uintptr_t)allocs[i];
Ashok Bhat98071552014-02-12 09:54:43 +00002417 _env->SetLongArrayRegion(_ids, i, 1, &alloc);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002418 }
2419
2420 free(allocs);
2421}
2422
2423static void
Ashok Bhat98071552014-02-12 09:54:43 +00002424nMeshGetIndices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _idxIds, jintArray _primitives, jint numIndices)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002425{
Andreas Gampe67333922014-11-10 20:35:59 -08002426 if (kLogApi) {
2427 ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2428 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002429
2430 RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numIndices * sizeof(RsAllocation));
2431 uint32_t *prims= (uint32_t*)malloc((uint32_t)numIndices * sizeof(uint32_t));
2432
Tim Murrayeff663f2013-11-15 13:08:30 -08002433 rsaMeshGetIndices((RsContext)con, (RsMesh)mesh, allocs, prims, (uint32_t)numIndices);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002434
2435 for(jint i = 0; i < numIndices; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07002436 const jlong alloc = (jlong)(uintptr_t)allocs[i];
Ashok Bhat98071552014-02-12 09:54:43 +00002437 const jint prim = (jint)prims[i];
2438 _env->SetLongArrayRegion(_idxIds, i, 1, &alloc);
2439 _env->SetIntArrayRegion(_primitives, i, 1, &prim);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002440 }
2441
2442 free(allocs);
2443 free(prims);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002444}
2445
Tim Murray56f9e6f2014-05-16 11:47:26 -07002446static jint
2447nSystemGetPointerSize(JNIEnv *_env, jobject _this) {
2448 return (jint)sizeof(void*);
2449}
2450
2451
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002452// ---------------------------------------------------------------------------
2453
Jason Samsd19f10d2009-05-22 14:03:28 -07002454
Jason Sams94d8e90a2009-06-10 16:09:05 -07002455static const char *classPathName = "android/renderscript/RenderScript";
Jason Samsd19f10d2009-05-22 14:03:28 -07002456
2457static JNINativeMethod methods[] = {
Jason Sams1c415172010-11-08 17:06:46 -08002458{"_nInit", "()V", (void*)_nInit },
Jason Samsea84a7c2009-09-04 14:42:41 -07002459
Tim Murrayeff663f2013-11-15 13:08:30 -08002460{"nDeviceCreate", "()J", (void*)nDeviceCreate },
2461{"nDeviceDestroy", "(J)V", (void*)nDeviceDestroy },
2462{"nDeviceSetConfig", "(JII)V", (void*)nDeviceSetConfig },
2463{"nContextGetUserMessage", "(J[I)I", (void*)nContextGetUserMessage },
2464{"nContextGetErrorMessage", "(J)Ljava/lang/String;", (void*)nContextGetErrorMessage },
2465{"nContextPeekMessage", "(J[I)I", (void*)nContextPeekMessage },
Jason Sams1c415172010-11-08 17:06:46 -08002466
Tim Murrayeff663f2013-11-15 13:08:30 -08002467{"nContextInitToClient", "(J)V", (void*)nContextInitToClient },
2468{"nContextDeinitToClient", "(J)V", (void*)nContextDeinitToClient },
Jason Samsd19f10d2009-05-22 14:03:28 -07002469
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002470
Jason Sams2e1872f2010-08-17 16:25:41 -07002471// All methods below are thread protected in java.
Tim Murrayeff663f2013-11-15 13:08:30 -08002472{"rsnContextCreate", "(JIII)J", (void*)nContextCreate },
2473{"rsnContextCreateGL", "(JIIIIIIIIIIIIFI)J", (void*)nContextCreateGL },
2474{"rsnContextFinish", "(J)V", (void*)nContextFinish },
2475{"rsnContextSetPriority", "(JI)V", (void*)nContextSetPriority },
Tim Murray47f31582015-04-07 15:43:24 -07002476{"rsnContextSetCacheDir", "(JLjava/lang/String;)V", (void*)nContextSetCacheDir },
Tim Murrayeff663f2013-11-15 13:08:30 -08002477{"rsnContextSetSurface", "(JIILandroid/view/Surface;)V", (void*)nContextSetSurface },
2478{"rsnContextDestroy", "(J)V", (void*)nContextDestroy },
2479{"rsnContextDump", "(JI)V", (void*)nContextDump },
2480{"rsnContextPause", "(J)V", (void*)nContextPause },
2481{"rsnContextResume", "(J)V", (void*)nContextResume },
2482{"rsnContextSendMessage", "(JI[I)V", (void*)nContextSendMessage },
Yang Ni281c3252014-10-24 08:52:24 -07002483{"rsnClosureCreate", "(JJJ[J[J[I[J[J)J", (void*)nClosureCreate },
Yang Nibe392ad2015-01-23 17:16:02 -08002484{"rsnInvokeClosureCreate", "(JJ[B[J[J[I)J", (void*)nInvokeClosureCreate },
Yang Ni281c3252014-10-24 08:52:24 -07002485{"rsnClosureSetArg", "(JJIJI)V", (void*)nClosureSetArg },
2486{"rsnClosureSetGlobal", "(JJJJI)V", (void*)nClosureSetGlobal },
Tim Murray460a0492013-11-19 12:45:54 -08002487{"rsnAssignName", "(JJ[B)V", (void*)nAssignName },
2488{"rsnGetName", "(JJ)Ljava/lang/String;", (void*)nGetName },
2489{"rsnObjDestroy", "(JJ)V", (void*)nObjDestroy },
Jason Sams64676f32009-07-08 18:01:53 -07002490
Tim Murray460a0492013-11-19 12:45:54 -08002491{"rsnFileA3DCreateFromFile", "(JLjava/lang/String;)J", (void*)nFileA3DCreateFromFile },
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002492{"rsnFileA3DCreateFromAssetStream", "(JJ)J", (void*)nFileA3DCreateFromAssetStream },
Tim Murray460a0492013-11-19 12:45:54 -08002493{"rsnFileA3DCreateFromAsset", "(JLandroid/content/res/AssetManager;Ljava/lang/String;)J", (void*)nFileA3DCreateFromAsset },
2494{"rsnFileA3DGetNumIndexEntries", "(JJ)I", (void*)nFileA3DGetNumIndexEntries },
2495{"rsnFileA3DGetIndexEntries", "(JJI[I[Ljava/lang/String;)V", (void*)nFileA3DGetIndexEntries },
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002496{"rsnFileA3DGetEntryByIndex", "(JJI)J", (void*)nFileA3DGetEntryByIndex },
Jason Samsd19f10d2009-05-22 14:03:28 -07002497
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002498{"rsnFontCreateFromFile", "(JLjava/lang/String;FI)J", (void*)nFontCreateFromFile },
2499{"rsnFontCreateFromAssetStream", "(JLjava/lang/String;FIJ)J", (void*)nFontCreateFromAssetStream },
2500{"rsnFontCreateFromAsset", "(JLandroid/content/res/AssetManager;Ljava/lang/String;FI)J", (void*)nFontCreateFromAsset },
Jason Samsd19f10d2009-05-22 14:03:28 -07002501
Tim Murray460a0492013-11-19 12:45:54 -08002502{"rsnElementCreate", "(JJIZI)J", (void*)nElementCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002503{"rsnElementCreate2", "(J[J[Ljava/lang/String;[I)J", (void*)nElementCreate2 },
Tim Murray460a0492013-11-19 12:45:54 -08002504{"rsnElementGetNativeData", "(JJ[I)V", (void*)nElementGetNativeData },
Ashok Bhat98071552014-02-12 09:54:43 +00002505{"rsnElementGetSubElements", "(JJ[J[Ljava/lang/String;[I)V", (void*)nElementGetSubElements },
Jason Samsd19f10d2009-05-22 14:03:28 -07002506
Tim Murray460a0492013-11-19 12:45:54 -08002507{"rsnTypeCreate", "(JJIIIZZI)J", (void*)nTypeCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002508{"rsnTypeGetNativeData", "(JJ[J)V", (void*)nTypeGetNativeData },
Jason Samsd19f10d2009-05-22 14:03:28 -07002509
Ashok Bhat98071552014-02-12 09:54:43 +00002510{"rsnAllocationCreateTyped", "(JJIIJ)J", (void*)nAllocationCreateTyped },
Tim Murray460a0492013-11-19 12:45:54 -08002511{"rsnAllocationCreateFromBitmap", "(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCreateFromBitmap },
2512{"rsnAllocationCreateBitmapBackedAllocation", "(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCreateBitmapBackedAllocation },
2513{"rsnAllocationCubeCreateFromBitmap","(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCubeCreateFromBitmap },
Jason Sams5476b452010-12-08 16:14:36 -08002514
Tim Murray460a0492013-11-19 12:45:54 -08002515{"rsnAllocationCopyFromBitmap", "(JJLandroid/graphics/Bitmap;)V", (void*)nAllocationCopyFromBitmap },
2516{"rsnAllocationCopyToBitmap", "(JJLandroid/graphics/Bitmap;)V", (void*)nAllocationCopyToBitmap },
Jason Sams4ef66502010-12-10 16:03:15 -08002517
Tim Murray460a0492013-11-19 12:45:54 -08002518{"rsnAllocationSyncAll", "(JJI)V", (void*)nAllocationSyncAll },
2519{"rsnAllocationGetSurface", "(JJ)Landroid/view/Surface;", (void*)nAllocationGetSurface },
2520{"rsnAllocationSetSurface", "(JJLandroid/view/Surface;)V", (void*)nAllocationSetSurface },
2521{"rsnAllocationIoSend", "(JJ)V", (void*)nAllocationIoSend },
2522{"rsnAllocationIoReceive", "(JJ)V", (void*)nAllocationIoReceive },
Miao Wang87e908d2015-03-02 15:15:15 -08002523{"rsnAllocationData1D", "(JJIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData1D },
Miao Wangc8e237e2015-02-20 18:36:32 -08002524{"rsnAllocationElementData", "(JJIIIII[BI)V", (void*)nAllocationElementData },
Miao Wang87e908d2015-03-02 15:15:15 -08002525{"rsnAllocationData2D", "(JJIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData2D },
Tim Murray460a0492013-11-19 12:45:54 -08002526{"rsnAllocationData2D", "(JJIIIIIIJIIII)V", (void*)nAllocationData2D_alloc },
Miao Wang87e908d2015-03-02 15:15:15 -08002527{"rsnAllocationData3D", "(JJIIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData3D },
Tim Murray460a0492013-11-19 12:45:54 -08002528{"rsnAllocationData3D", "(JJIIIIIIIJIIII)V", (void*)nAllocationData3D_alloc },
Miao Wang87e908d2015-03-02 15:15:15 -08002529{"rsnAllocationRead", "(JJLjava/lang/Object;IIZ)V", (void*)nAllocationRead },
2530{"rsnAllocationRead1D", "(JJIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead1D },
Miao Wang45cec0a2015-03-04 16:40:21 -08002531{"rsnAllocationElementRead", "(JJIIIII[BI)V", (void*)nAllocationElementRead },
Miao Wang87e908d2015-03-02 15:15:15 -08002532{"rsnAllocationRead2D", "(JJIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead2D },
2533{"rsnAllocationRead3D", "(JJIIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead3D },
Tim Murray460a0492013-11-19 12:45:54 -08002534{"rsnAllocationGetType", "(JJ)J", (void*)nAllocationGetType},
2535{"rsnAllocationResize1D", "(JJI)V", (void*)nAllocationResize1D },
2536{"rsnAllocationGenerateMipmaps", "(JJ)V", (void*)nAllocationGenerateMipmaps },
Jason Samsbd1c3ad2009-08-03 16:03:08 -07002537
Jason Sams46ba27e32015-02-06 17:45:15 -08002538{"rsnAllocationAdapterCreate", "(JJJ)J", (void*)nAllocationAdapterCreate },
2539{"rsnAllocationAdapterOffset", "(JJIIIIIIIII)V", (void*)nAllocationAdapterOffset },
2540
Tim Murray460a0492013-11-19 12:45:54 -08002541{"rsnScriptBindAllocation", "(JJJI)V", (void*)nScriptBindAllocation },
2542{"rsnScriptSetTimeZone", "(JJ[B)V", (void*)nScriptSetTimeZone },
2543{"rsnScriptInvoke", "(JJI)V", (void*)nScriptInvoke },
2544{"rsnScriptInvokeV", "(JJI[B)V", (void*)nScriptInvokeV },
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002545
2546{"rsnScriptForEach", "(JJI[JJ[B[I)V", (void*)nScriptForEach },
2547
Tim Murray460a0492013-11-19 12:45:54 -08002548{"rsnScriptSetVarI", "(JJII)V", (void*)nScriptSetVarI },
2549{"rsnScriptGetVarI", "(JJI)I", (void*)nScriptGetVarI },
2550{"rsnScriptSetVarJ", "(JJIJ)V", (void*)nScriptSetVarJ },
2551{"rsnScriptGetVarJ", "(JJI)J", (void*)nScriptGetVarJ },
2552{"rsnScriptSetVarF", "(JJIF)V", (void*)nScriptSetVarF },
2553{"rsnScriptGetVarF", "(JJI)F", (void*)nScriptGetVarF },
2554{"rsnScriptSetVarD", "(JJID)V", (void*)nScriptSetVarD },
2555{"rsnScriptGetVarD", "(JJI)D", (void*)nScriptGetVarD },
2556{"rsnScriptSetVarV", "(JJI[B)V", (void*)nScriptSetVarV },
2557{"rsnScriptGetVarV", "(JJI[B)V", (void*)nScriptGetVarV },
2558{"rsnScriptSetVarVE", "(JJI[BJ[I)V", (void*)nScriptSetVarVE },
2559{"rsnScriptSetVarObj", "(JJIJ)V", (void*)nScriptSetVarObj },
Jason Samsd19f10d2009-05-22 14:03:28 -07002560
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002561{"rsnScriptCCreate", "(JLjava/lang/String;Ljava/lang/String;[BI)J", (void*)nScriptCCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002562{"rsnScriptIntrinsicCreate", "(JIJ)J", (void*)nScriptIntrinsicCreate },
2563{"rsnScriptKernelIDCreate", "(JJII)J", (void*)nScriptKernelIDCreate },
Yang Nibe392ad2015-01-23 17:16:02 -08002564{"rsnScriptInvokeIDCreate", "(JJI)J", (void*)nScriptInvokeIDCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002565{"rsnScriptFieldIDCreate", "(JJI)J", (void*)nScriptFieldIDCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002566{"rsnScriptGroupCreate", "(J[J[J[J[J[J)J", (void*)nScriptGroupCreate },
Yang Ni35be56c2015-04-02 17:47:56 -07002567{"rsnScriptGroup2Create", "(JLjava/lang/String;Ljava/lang/String;[J)J", (void*)nScriptGroup2Create },
Tim Murray460a0492013-11-19 12:45:54 -08002568{"rsnScriptGroupSetInput", "(JJJJ)V", (void*)nScriptGroupSetInput },
2569{"rsnScriptGroupSetOutput", "(JJJJ)V", (void*)nScriptGroupSetOutput },
2570{"rsnScriptGroupExecute", "(JJ)V", (void*)nScriptGroupExecute },
Yang Ni281c3252014-10-24 08:52:24 -07002571{"rsnScriptGroup2Execute", "(JJ)V", (void*)nScriptGroup2Execute },
Jason Sams0011bcf2009-12-15 12:58:36 -08002572
Tim Murray25207df2015-01-12 16:47:56 -08002573{"rsnScriptIntrinsicBLAS_Single", "(JJIIIIIIIIIFJJFJIIII)V", (void*)nScriptIntrinsicBLAS_Single },
2574{"rsnScriptIntrinsicBLAS_Double", "(JJIIIIIIIIIDJJDJIIII)V", (void*)nScriptIntrinsicBLAS_Double },
2575{"rsnScriptIntrinsicBLAS_Complex", "(JJIIIIIIIIIFFJJFFJIIII)V", (void*)nScriptIntrinsicBLAS_Complex },
2576{"rsnScriptIntrinsicBLAS_Z", "(JJIIIIIIIIIDDJJDDJIIII)V", (void*)nScriptIntrinsicBLAS_Z },
2577
Tim Murray9cb16a22015-04-01 11:07:16 -07002578{"rsnScriptIntrinsicBLAS_BNNM", "(JJIIIJIJIJII)V", (void*)nScriptIntrinsicBLAS_BNNM },
2579
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002580{"rsnProgramStoreCreate", "(JZZZZZZIII)J", (void*)nProgramStoreCreate },
Jason Samsd19f10d2009-05-22 14:03:28 -07002581
Tim Murray460a0492013-11-19 12:45:54 -08002582{"rsnProgramBindConstants", "(JJIJ)V", (void*)nProgramBindConstants },
2583{"rsnProgramBindTexture", "(JJIJ)V", (void*)nProgramBindTexture },
2584{"rsnProgramBindSampler", "(JJIJ)V", (void*)nProgramBindSampler },
Jason Samsebfb4362009-09-23 13:57:02 -07002585
Ashok Bhat98071552014-02-12 09:54:43 +00002586{"rsnProgramFragmentCreate", "(JLjava/lang/String;[Ljava/lang/String;[J)J", (void*)nProgramFragmentCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002587{"rsnProgramRasterCreate", "(JZI)J", (void*)nProgramRasterCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002588{"rsnProgramVertexCreate", "(JLjava/lang/String;[Ljava/lang/String;[J)J", (void*)nProgramVertexCreate },
Jason Samsd19f10d2009-05-22 14:03:28 -07002589
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002590{"rsnContextBindRootScript", "(JJ)V", (void*)nContextBindRootScript },
2591{"rsnContextBindProgramStore", "(JJ)V", (void*)nContextBindProgramStore },
2592{"rsnContextBindProgramFragment", "(JJ)V", (void*)nContextBindProgramFragment },
2593{"rsnContextBindProgramVertex", "(JJ)V", (void*)nContextBindProgramVertex },
2594{"rsnContextBindProgramRaster", "(JJ)V", (void*)nContextBindProgramRaster },
Jason Sams02fb2cb2009-05-28 15:37:57 -07002595
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002596{"rsnSamplerCreate", "(JIIIIIF)J", (void*)nSamplerCreate },
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002597
Ashok Bhat98071552014-02-12 09:54:43 +00002598{"rsnMeshCreate", "(J[J[J[I)J", (void*)nMeshCreate },
Jason Sams2e1872f2010-08-17 16:25:41 -07002599
Tim Murray460a0492013-11-19 12:45:54 -08002600{"rsnMeshGetVertexBufferCount", "(JJ)I", (void*)nMeshGetVertexBufferCount },
2601{"rsnMeshGetIndexCount", "(JJ)I", (void*)nMeshGetIndexCount },
Ashok Bhat98071552014-02-12 09:54:43 +00002602{"rsnMeshGetVertices", "(JJ[JI)V", (void*)nMeshGetVertices },
2603{"rsnMeshGetIndices", "(JJ[J[II)V", (void*)nMeshGetIndices },
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002604
Tim Murray56f9e6f2014-05-16 11:47:26 -07002605{"rsnSystemGetPointerSize", "()I", (void*)nSystemGetPointerSize },
Jason Samsd19f10d2009-05-22 14:03:28 -07002606};
2607
2608static int registerFuncs(JNIEnv *_env)
2609{
2610 return android::AndroidRuntime::registerNativeMethods(
2611 _env, classPathName, methods, NELEM(methods));
2612}
2613
2614// ---------------------------------------------------------------------------
2615
2616jint JNI_OnLoad(JavaVM* vm, void* reserved)
2617{
Chris Wailes488230c32014-08-14 11:22:40 -07002618 JNIEnv* env = nullptr;
Jason Samsd19f10d2009-05-22 14:03:28 -07002619 jint result = -1;
2620
Jason Samsd19f10d2009-05-22 14:03:28 -07002621 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
Steve Block3762c312012-01-06 19:20:56 +00002622 ALOGE("ERROR: GetEnv failed\n");
Jason Samsd19f10d2009-05-22 14:03:28 -07002623 goto bail;
2624 }
Chris Wailes488230c32014-08-14 11:22:40 -07002625 assert(env != nullptr);
Jason Samsd19f10d2009-05-22 14:03:28 -07002626
2627 if (registerFuncs(env) < 0) {
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002628 ALOGE("ERROR: Renderscript native registration failed\n");
Jason Samsd19f10d2009-05-22 14:03:28 -07002629 goto bail;
2630 }
2631
2632 /* success -- return valid version number */
2633 result = JNI_VERSION_1_4;
2634
2635bail:
2636 return result;
2637}