blob: ffc4fd82ab5ef47006f8f939f825c78e6308f79d [file] [log] [blame]
Jason Samsd19f10d2009-05-22 14:03:28 -07001/*
Stephen Hines4cbe25a2012-01-18 18:46:27 -08002 * Copyright (C) 2011-2012 The Android Open Source Project
Jason Samsd19f10d2009-05-22 14:03:28 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jason Samsd1516df2015-05-05 18:00:34 -070017#define LOG_TAG "RenderScript_jni"
Jason Samsf29ca502009-06-23 12:22:47 -070018
Jason Samsd19f10d2009-05-22 14:03:28 -070019#include <stdlib.h>
20#include <stdio.h>
21#include <fcntl.h>
22#include <unistd.h>
23#include <math.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070024#include <utils/misc.h>
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +010025#include <inttypes.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070026
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080027#include <androidfw/Asset.h>
28#include <androidfw/AssetManager.h>
29#include <androidfw/ResourceTypes.h>
Jason Samsf29ca502009-06-23 12:22:47 -070030
Jason Samsd19f10d2009-05-22 14:03:28 -070031#include "jni.h"
32#include "JNIHelp.h"
33#include "android_runtime/AndroidRuntime.h"
Jim Milleree956052010-08-19 18:56:00 -070034#include "android_runtime/android_view_Surface.h"
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -080035#include "android_runtime/android_util_AssetManager.h"
John Reckf4faeac2015-03-05 13:50:31 -080036#include "android/graphics/GraphicsJNI.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070037
Jason Sams1d6983a2012-02-16 16:07:49 -080038#include <rs.h>
39#include <rsEnv.h>
Jason Samsfb9aa9f2012-03-28 15:30:07 -070040#include <gui/Surface.h>
Andy McFaddend47f7d82012-12-18 09:48:38 -080041#include <gui/GLConsumer.h>
Jason Samsfaa32b32011-06-20 16:58:04 -070042#include <android_runtime/android_graphics_SurfaceTexture.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070043
Steve Block3762c312012-01-06 19:20:56 +000044//#define LOG_API ALOGE
Andreas Gampe67333922014-11-10 20:35:59 -080045static constexpr bool kLogApi = false;
Jason Samsd19f10d2009-05-22 14:03:28 -070046
47using namespace android;
48
Andreas Gampe67333922014-11-10 20:35:59 -080049template <typename... T>
50void UNUSED(T... t) {}
51
Stephen Hines414fa2c2014-04-17 01:02:42 -070052#define PER_ARRAY_TYPE(flag, fnc, readonly, ...) { \
Jason Samse729a942013-11-06 11:22:02 -080053 jint len = 0; \
Chris Wailes488230c32014-08-14 11:22:40 -070054 void *ptr = nullptr; \
Miao Wang87e908d2015-03-02 15:15:15 -080055 void *srcPtr = nullptr; \
Jason Sams21659ac2013-11-06 15:08:07 -080056 size_t typeBytes = 0; \
Stephen Hines414fa2c2014-04-17 01:02:42 -070057 jint relFlag = 0; \
58 if (readonly) { \
59 /* The on-release mode should only be JNI_ABORT for read-only accesses. */ \
Miao Wang87e908d2015-03-02 15:15:15 -080060 /* readonly = true, also indicates we are copying to the allocation . */ \
Stephen Hines414fa2c2014-04-17 01:02:42 -070061 relFlag = JNI_ABORT; \
62 } \
Jason Samse729a942013-11-06 11:22:02 -080063 switch(dataType) { \
64 case RS_TYPE_FLOAT_32: \
65 len = _env->GetArrayLength((jfloatArray)data); \
66 ptr = _env->GetFloatArrayElements((jfloatArray)data, flag); \
Jason Sams21659ac2013-11-06 15:08:07 -080067 typeBytes = 4; \
Miao Wang87e908d2015-03-02 15:15:15 -080068 if (usePadding) { \
69 srcPtr = ptr; \
70 len = len / 3 * 4; \
71 if (count == 0) { \
72 count = len / 4; \
73 } \
74 ptr = malloc (len * typeBytes); \
75 if (readonly) { \
76 copyWithPadding(ptr, srcPtr, mSize, count); \
77 fnc(__VA_ARGS__); \
78 } else { \
79 fnc(__VA_ARGS__); \
80 copyWithUnPadding(srcPtr, ptr, mSize, count); \
81 } \
82 free(ptr); \
83 ptr = srcPtr; \
84 } else { \
85 fnc(__VA_ARGS__); \
86 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -070087 _env->ReleaseFloatArrayElements((jfloatArray)data, (jfloat *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -080088 return; \
89 case RS_TYPE_FLOAT_64: \
90 len = _env->GetArrayLength((jdoubleArray)data); \
91 ptr = _env->GetDoubleArrayElements((jdoubleArray)data, flag); \
Jason Sams21659ac2013-11-06 15:08:07 -080092 typeBytes = 8; \
Miao Wang87e908d2015-03-02 15:15:15 -080093 if (usePadding) { \
94 srcPtr = ptr; \
95 len = len / 3 * 4; \
96 if (count == 0) { \
97 count = len / 4; \
98 } \
99 ptr = malloc (len * typeBytes); \
100 if (readonly) { \
101 copyWithPadding(ptr, srcPtr, mSize, count); \
102 fnc(__VA_ARGS__); \
103 } else { \
104 fnc(__VA_ARGS__); \
105 copyWithUnPadding(srcPtr, ptr, mSize, count); \
106 } \
107 free(ptr); \
108 ptr = srcPtr; \
109 } else { \
110 fnc(__VA_ARGS__); \
111 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700112 _env->ReleaseDoubleArrayElements((jdoubleArray)data, (jdouble *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800113 return; \
114 case RS_TYPE_SIGNED_8: \
115 case RS_TYPE_UNSIGNED_8: \
116 len = _env->GetArrayLength((jbyteArray)data); \
117 ptr = _env->GetByteArrayElements((jbyteArray)data, flag); \
Jason Sams21659ac2013-11-06 15:08:07 -0800118 typeBytes = 1; \
Miao Wang87e908d2015-03-02 15:15:15 -0800119 if (usePadding) { \
120 srcPtr = ptr; \
121 len = len / 3 * 4; \
122 if (count == 0) { \
123 count = len / 4; \
124 } \
125 ptr = malloc (len * typeBytes); \
126 if (readonly) { \
127 copyWithPadding(ptr, srcPtr, mSize, count); \
128 fnc(__VA_ARGS__); \
129 } else { \
130 fnc(__VA_ARGS__); \
131 copyWithUnPadding(srcPtr, ptr, mSize, count); \
132 } \
133 free(ptr); \
134 ptr = srcPtr; \
135 } else { \
136 fnc(__VA_ARGS__); \
137 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700138 _env->ReleaseByteArrayElements((jbyteArray)data, (jbyte*)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800139 return; \
140 case RS_TYPE_SIGNED_16: \
141 case RS_TYPE_UNSIGNED_16: \
142 len = _env->GetArrayLength((jshortArray)data); \
143 ptr = _env->GetShortArrayElements((jshortArray)data, flag); \
Jason Sams21659ac2013-11-06 15:08:07 -0800144 typeBytes = 2; \
Miao Wang87e908d2015-03-02 15:15:15 -0800145 if (usePadding) { \
146 srcPtr = ptr; \
147 len = len / 3 * 4; \
148 if (count == 0) { \
149 count = len / 4; \
150 } \
151 ptr = malloc (len * typeBytes); \
152 if (readonly) { \
153 copyWithPadding(ptr, srcPtr, mSize, count); \
154 fnc(__VA_ARGS__); \
155 } else { \
156 fnc(__VA_ARGS__); \
157 copyWithUnPadding(srcPtr, ptr, mSize, count); \
158 } \
159 free(ptr); \
160 ptr = srcPtr; \
161 } else { \
162 fnc(__VA_ARGS__); \
163 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700164 _env->ReleaseShortArrayElements((jshortArray)data, (jshort *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800165 return; \
166 case RS_TYPE_SIGNED_32: \
167 case RS_TYPE_UNSIGNED_32: \
168 len = _env->GetArrayLength((jintArray)data); \
169 ptr = _env->GetIntArrayElements((jintArray)data, flag); \
Jason Sams21659ac2013-11-06 15:08:07 -0800170 typeBytes = 4; \
Miao Wang87e908d2015-03-02 15:15:15 -0800171 if (usePadding) { \
172 srcPtr = ptr; \
173 len = len / 3 * 4; \
174 if (count == 0) { \
175 count = len / 4; \
176 } \
177 ptr = malloc (len * typeBytes); \
178 if (readonly) { \
179 copyWithPadding(ptr, srcPtr, mSize, count); \
180 fnc(__VA_ARGS__); \
181 } else { \
182 fnc(__VA_ARGS__); \
183 copyWithUnPadding(srcPtr, ptr, mSize, count); \
184 } \
185 free(ptr); \
186 ptr = srcPtr; \
187 } else { \
188 fnc(__VA_ARGS__); \
189 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700190 _env->ReleaseIntArrayElements((jintArray)data, (jint *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800191 return; \
192 case RS_TYPE_SIGNED_64: \
193 case RS_TYPE_UNSIGNED_64: \
194 len = _env->GetArrayLength((jlongArray)data); \
195 ptr = _env->GetLongArrayElements((jlongArray)data, flag); \
Jason Sams21659ac2013-11-06 15:08:07 -0800196 typeBytes = 8; \
Miao Wang87e908d2015-03-02 15:15:15 -0800197 if (usePadding) { \
198 srcPtr = ptr; \
199 len = len / 3 * 4; \
200 if (count == 0) { \
201 count = len / 4; \
202 } \
203 ptr = malloc (len * typeBytes); \
204 if (readonly) { \
205 copyWithPadding(ptr, srcPtr, mSize, count); \
206 fnc(__VA_ARGS__); \
207 } else { \
208 fnc(__VA_ARGS__); \
209 copyWithUnPadding(srcPtr, ptr, mSize, count); \
210 } \
211 free(ptr); \
212 ptr = srcPtr; \
213 } else { \
214 fnc(__VA_ARGS__); \
215 } \
Stephen Hines414fa2c2014-04-17 01:02:42 -0700216 _env->ReleaseLongArrayElements((jlongArray)data, (jlong *)ptr, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800217 return; \
218 default: \
219 break; \
220 } \
Miao Wang87e908d2015-03-02 15:15:15 -0800221 UNUSED(len, ptr, srcPtr, typeBytes, relFlag); \
Jason Samse729a942013-11-06 11:22:02 -0800222}
223
224
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800225class AutoJavaStringToUTF8 {
226public:
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800227 AutoJavaStringToUTF8(JNIEnv* env, jstring str) : fEnv(env), fJStr(str) {
Chris Wailes488230c32014-08-14 11:22:40 -0700228 fCStr = env->GetStringUTFChars(str, nullptr);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800229 fLength = env->GetStringUTFLength(str);
230 }
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800231 ~AutoJavaStringToUTF8() {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800232 fEnv->ReleaseStringUTFChars(fJStr, fCStr);
233 }
234 const char* c_str() const { return fCStr; }
235 jsize length() const { return fLength; }
236
237private:
238 JNIEnv* fEnv;
239 jstring fJStr;
240 const char* fCStr;
241 jsize fLength;
242};
243
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800244class AutoJavaStringArrayToUTF8 {
245public:
246 AutoJavaStringArrayToUTF8(JNIEnv* env, jobjectArray strings, jsize stringsLength)
247 : mEnv(env), mStrings(strings), mStringsLength(stringsLength) {
Chris Wailes488230c32014-08-14 11:22:40 -0700248 mCStrings = nullptr;
249 mSizeArray = nullptr;
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800250 if (stringsLength > 0) {
251 mCStrings = (const char **)calloc(stringsLength, sizeof(char *));
252 mSizeArray = (size_t*)calloc(stringsLength, sizeof(size_t));
253 for (jsize ct = 0; ct < stringsLength; ct ++) {
254 jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
Chris Wailes488230c32014-08-14 11:22:40 -0700255 mCStrings[ct] = mEnv->GetStringUTFChars(s, nullptr);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800256 mSizeArray[ct] = mEnv->GetStringUTFLength(s);
257 }
258 }
259 }
260 ~AutoJavaStringArrayToUTF8() {
261 for (jsize ct=0; ct < mStringsLength; ct++) {
262 jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
263 mEnv->ReleaseStringUTFChars(s, mCStrings[ct]);
264 }
265 free(mCStrings);
266 free(mSizeArray);
267 }
268 const char **c_str() const { return mCStrings; }
269 size_t *c_str_len() const { return mSizeArray; }
270 jsize length() const { return mStringsLength; }
271
272private:
273 JNIEnv *mEnv;
274 jobjectArray mStrings;
275 const char **mCStrings;
276 size_t *mSizeArray;
277 jsize mStringsLength;
278};
279
Jason Samsd19f10d2009-05-22 14:03:28 -0700280// ---------------------------------------------------------------------------
281
Jason Samsffe9f482009-06-01 17:45:53 -0700282static jfieldID gContextId = 0;
Jason Samsd19f10d2009-05-22 14:03:28 -0700283
284static void _nInit(JNIEnv *_env, jclass _this)
285{
Tim Murrayeff663f2013-11-15 13:08:30 -0800286 gContextId = _env->GetFieldID(_this, "mContext", "J");
Jason Samsd19f10d2009-05-22 14:03:28 -0700287}
288
Jason Samsd19f10d2009-05-22 14:03:28 -0700289// ---------------------------------------------------------------------------
290
Miao Wang87e908d2015-03-02 15:15:15 -0800291static void copyWithPadding(void* ptr, void* srcPtr, int mSize, int count) {
292 int sizeBytesPad = mSize * 4;
293 int sizeBytes = mSize * 3;
294 uint8_t *dst = static_cast<uint8_t *>(ptr);
295 uint8_t *src = static_cast<uint8_t *>(srcPtr);
296 for (int i = 0; i < count; i++) {
297 memcpy(dst, src, sizeBytes);
298 dst += sizeBytesPad;
299 src += sizeBytes;
300 }
301}
302
303static void copyWithUnPadding(void* ptr, void* srcPtr, int mSize, int count) {
304 int sizeBytesPad = mSize * 4;
305 int sizeBytes = mSize * 3;
306 uint8_t *dst = static_cast<uint8_t *>(ptr);
307 uint8_t *src = static_cast<uint8_t *>(srcPtr);
308 for (int i = 0; i < count; i++) {
309 memcpy(dst, src, sizeBytes);
310 dst += sizeBytes;
311 src += sizeBytesPad;
312 }
313}
314
315
316// ---------------------------------------------------------------------------
Jason Sams3eaa338e2009-06-10 15:04:38 -0700317static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800318nContextFinish(JNIEnv *_env, jobject _this, jlong con)
Jason Sams96ed4cf2010-06-15 12:15:57 -0700319{
Andreas Gampe67333922014-11-10 20:35:59 -0800320 if (kLogApi) {
321 ALOGD("nContextFinish, con(%p)", (RsContext)con);
322 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800323 rsContextFinish((RsContext)con);
Jason Sams96ed4cf2010-06-15 12:15:57 -0700324}
325
Yang Ni281c3252014-10-24 08:52:24 -0700326static jlong
327nClosureCreate(JNIEnv *_env, jobject _this, jlong con, jlong kernelID,
328 jlong returnValue, jlongArray fieldIDArray,
329 jlongArray valueArray, jintArray sizeArray,
330 jlongArray depClosureArray, jlongArray depFieldIDArray) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700331 jlong ret = 0;
332
Yang Ni281c3252014-10-24 08:52:24 -0700333 jlong* jFieldIDs = _env->GetLongArrayElements(fieldIDArray, nullptr);
334 jsize fieldIDs_length = _env->GetArrayLength(fieldIDArray);
Yang Ni281c3252014-10-24 08:52:24 -0700335 jlong* jValues = _env->GetLongArrayElements(valueArray, nullptr);
336 jsize values_length = _env->GetArrayLength(valueArray);
Yang Ni17c2d7a2015-04-30 16:13:54 -0700337 jint* jSizes = _env->GetIntArrayElements(sizeArray, nullptr);
Yang Ni281c3252014-10-24 08:52:24 -0700338 jsize sizes_length = _env->GetArrayLength(sizeArray);
Yang Ni281c3252014-10-24 08:52:24 -0700339 jlong* jDepClosures =
340 _env->GetLongArrayElements(depClosureArray, nullptr);
341 jsize depClosures_length = _env->GetArrayLength(depClosureArray);
Yang Ni281c3252014-10-24 08:52:24 -0700342 jlong* jDepFieldIDs =
343 _env->GetLongArrayElements(depFieldIDArray, nullptr);
344 jsize depFieldIDs_length = _env->GetArrayLength(depFieldIDArray);
Yang Ni17c2d7a2015-04-30 16:13:54 -0700345
346 size_t numValues, numDependencies;
347 RsScriptFieldID* fieldIDs;
348 uintptr_t* values;
349 RsClosure* depClosures;
350 RsScriptFieldID* depFieldIDs;
351
352 if (fieldIDs_length != values_length || values_length != sizes_length) {
353 ALOGE("Unmatched field IDs, values, and sizes in closure creation.");
354 goto exit;
355 }
356
357 numValues = (size_t)fieldIDs_length;
358
359 if (depClosures_length != depFieldIDs_length) {
360 ALOGE("Unmatched closures and field IDs for dependencies in closure creation.");
361 goto exit;
362 }
363
364 numDependencies = (size_t)depClosures_length;
365
366 if (numDependencies > numValues) {
367 ALOGE("Unexpected number of dependencies in closure creation");
368 goto exit;
369 }
370
Yang Ni9310e3d2015-05-05 12:41:19 -0700371 if (numValues > RS_CLOSURE_MAX_NUMBER_ARGS_AND_BINDINGS) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700372 ALOGE("Too many arguments or globals in closure creation");
373 goto exit;
374 }
375
376 fieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numValues);
377 if (fieldIDs == nullptr) {
378 goto exit;
379 }
380
381 for (size_t i = 0; i < numValues; i++) {
382 fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i];
383 }
384
385 values = (uintptr_t*)alloca(sizeof(uintptr_t) * numValues);
386 if (values == nullptr) {
387 goto exit;
388 }
389
390 for (size_t i = 0; i < numValues; i++) {
391 values[i] = (uintptr_t)jValues[i];
392 }
393
394 depClosures = (RsClosure*)alloca(sizeof(RsClosure) * numDependencies);
395 if (depClosures == nullptr) {
396 goto exit;
397 }
398
399 for (size_t i = 0; i < numDependencies; i++) {
400 depClosures[i] = (RsClosure)jDepClosures[i];
401 }
402
403 depFieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numDependencies);
404 if (depFieldIDs == nullptr) {
405 goto exit;
406 }
407
408 for (size_t i = 0; i < numDependencies; i++) {
Yang Ni281c3252014-10-24 08:52:24 -0700409 depFieldIDs[i] = (RsClosure)jDepFieldIDs[i];
410 }
411
Yang Ni17c2d7a2015-04-30 16:13:54 -0700412 ret = (jlong)(uintptr_t)rsClosureCreate(
Yang Ni281c3252014-10-24 08:52:24 -0700413 (RsContext)con, (RsScriptKernelID)kernelID, (RsAllocation)returnValue,
Yang Ni17c2d7a2015-04-30 16:13:54 -0700414 fieldIDs, numValues, values, numValues,
415 (int*)jSizes, numValues,
416 depClosures, numDependencies,
417 depFieldIDs, numDependencies);
418
419exit:
420
421 _env->ReleaseLongArrayElements(depFieldIDArray, jDepFieldIDs, JNI_ABORT);
422 _env->ReleaseLongArrayElements(depClosureArray, jDepClosures, JNI_ABORT);
423 _env->ReleaseIntArrayElements (sizeArray, jSizes, JNI_ABORT);
424 _env->ReleaseLongArrayElements(valueArray, jValues, JNI_ABORT);
425 _env->ReleaseLongArrayElements(fieldIDArray, jFieldIDs, JNI_ABORT);
426
427 return ret;
Yang Ni281c3252014-10-24 08:52:24 -0700428}
429
Yang Nibe392ad2015-01-23 17:16:02 -0800430static jlong
431nInvokeClosureCreate(JNIEnv *_env, jobject _this, jlong con, jlong invokeID,
432 jbyteArray paramArray, jlongArray fieldIDArray, jlongArray valueArray,
433 jintArray sizeArray) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700434 jlong ret = 0;
435
Yang Nibe392ad2015-01-23 17:16:02 -0800436 jbyte* jParams = _env->GetByteArrayElements(paramArray, nullptr);
437 jsize jParamLength = _env->GetArrayLength(paramArray);
Yang Nibe392ad2015-01-23 17:16:02 -0800438 jlong* jFieldIDs = _env->GetLongArrayElements(fieldIDArray, nullptr);
439 jsize fieldIDs_length = _env->GetArrayLength(fieldIDArray);
Yang Ni17c2d7a2015-04-30 16:13:54 -0700440 jlong* jValues = _env->GetLongArrayElements(valueArray, nullptr);
441 jsize values_length = _env->GetArrayLength(valueArray);
442 jint* jSizes = _env->GetIntArrayElements(sizeArray, nullptr);
443 jsize sizes_length = _env->GetArrayLength(sizeArray);
444
445 size_t numValues;
446 RsScriptFieldID* fieldIDs;
447 uintptr_t* values;
448
449 if (fieldIDs_length != values_length || values_length != sizes_length) {
450 ALOGE("Unmatched field IDs, values, and sizes in closure creation.");
451 goto exit;
452 }
453
454 numValues = (size_t) fieldIDs_length;
455
Yang Ni9310e3d2015-05-05 12:41:19 -0700456 if (numValues > RS_CLOSURE_MAX_NUMBER_ARGS_AND_BINDINGS) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700457 ALOGE("Too many arguments or globals in closure creation");
458 goto exit;
459 }
460
461 fieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numValues);
462 if (fieldIDs == nullptr) {
463 goto exit;
464 }
465
466 for (size_t i = 0; i< numValues; i++) {
Yang Nibe392ad2015-01-23 17:16:02 -0800467 fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i];
468 }
469
Yang Ni17c2d7a2015-04-30 16:13:54 -0700470 values = (uintptr_t*)alloca(sizeof(uintptr_t) * numValues);
471 if (values == nullptr) {
472 goto exit;
473 }
474
475 for (size_t i = 0; i < numValues; i++) {
Yang Nibe392ad2015-01-23 17:16:02 -0800476 values[i] = (uintptr_t)jValues[i];
477 }
478
Yang Ni17c2d7a2015-04-30 16:13:54 -0700479 ret = (jlong)(uintptr_t)rsInvokeClosureCreate(
Yang Nibe392ad2015-01-23 17:16:02 -0800480 (RsContext)con, (RsScriptInvokeID)invokeID, jParams, jParamLength,
Yang Ni17c2d7a2015-04-30 16:13:54 -0700481 fieldIDs, numValues, values, numValues,
482 (int*)jSizes, numValues);
483
484exit:
485
486 _env->ReleaseIntArrayElements (sizeArray, jSizes, JNI_ABORT);
487 _env->ReleaseLongArrayElements(valueArray, jValues, JNI_ABORT);
488 _env->ReleaseLongArrayElements(fieldIDArray, jFieldIDs, JNI_ABORT);
489 _env->ReleaseByteArrayElements(paramArray, jParams, JNI_ABORT);
490
491 return ret;
Yang Nibe392ad2015-01-23 17:16:02 -0800492}
493
Yang Ni281c3252014-10-24 08:52:24 -0700494static void
495nClosureSetArg(JNIEnv *_env, jobject _this, jlong con, jlong closureID,
496 jint index, jlong value, jint size) {
497 rsClosureSetArg((RsContext)con, (RsClosure)closureID, (uint32_t)index,
498 (uintptr_t)value, (size_t)size);
499}
500
501static void
502nClosureSetGlobal(JNIEnv *_env, jobject _this, jlong con, jlong closureID,
503 jlong fieldID, jlong value, jint size) {
504 rsClosureSetGlobal((RsContext)con, (RsClosure)closureID,
505 (RsScriptFieldID)fieldID, (uintptr_t)value, (size_t)size);
506}
507
508static long
Yang Ni35be56c2015-04-02 17:47:56 -0700509nScriptGroup2Create(JNIEnv *_env, jobject _this, jlong con, jstring name,
Yang Niebf63402015-01-16 11:06:26 -0800510 jstring cacheDir, jlongArray closureArray) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700511 jlong ret = 0;
512
Yang Ni35be56c2015-04-02 17:47:56 -0700513 AutoJavaStringToUTF8 nameUTF(_env, name);
Yang Niebf63402015-01-16 11:06:26 -0800514 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
515
Yang Ni281c3252014-10-24 08:52:24 -0700516 jlong* jClosures = _env->GetLongArrayElements(closureArray, nullptr);
517 jsize numClosures = _env->GetArrayLength(closureArray);
Yang Ni17c2d7a2015-04-30 16:13:54 -0700518
519 RsClosure* closures;
520
Yang Ni9310e3d2015-05-05 12:41:19 -0700521 if (numClosures > (jsize) RS_SCRIPT_GROUP_MAX_NUMBER_CLOSURES) {
Yang Ni17c2d7a2015-04-30 16:13:54 -0700522 ALOGE("Too many closures in script group");
523 goto exit;
524 }
525
526 closures = (RsClosure*)alloca(sizeof(RsClosure) * numClosures);
527 if (closures == nullptr) {
528 goto exit;
529 }
530
Yang Ni281c3252014-10-24 08:52:24 -0700531 for (int i = 0; i < numClosures; i++) {
532 closures[i] = (RsClosure)jClosures[i];
533 }
534
Yang Ni17c2d7a2015-04-30 16:13:54 -0700535 ret = (jlong)(uintptr_t)rsScriptGroup2Create(
Yang Ni35be56c2015-04-02 17:47:56 -0700536 (RsContext)con, nameUTF.c_str(), nameUTF.length(),
537 cacheDirUTF.c_str(), cacheDirUTF.length(),
Yang Niebf63402015-01-16 11:06:26 -0800538 closures, numClosures);
Yang Ni17c2d7a2015-04-30 16:13:54 -0700539
540exit:
541
542 _env->ReleaseLongArrayElements(closureArray, jClosures, JNI_ABORT);
543
544 return ret;
Yang Ni281c3252014-10-24 08:52:24 -0700545}
546
547static void
548nScriptGroup2Execute(JNIEnv *_env, jobject _this, jlong con, jlong groupID) {
549 rsScriptGroupExecute((RsContext)con, (RsScriptGroup2)groupID);
550}
551
Jason Sams96ed4cf2010-06-15 12:15:57 -0700552static void
Tim Murray25207df2015-01-12 16:47:56 -0800553nScriptIntrinsicBLAS_Single(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
554 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
555 jfloat alpha, jlong A, jlong B, jfloat beta, jlong C, jint incX, jint incY,
556 jint KL, jint KU) {
557 RsBlasCall call;
558 memset(&call, 0, sizeof(call));
559 call.func = (RsBlasFunction)func;
560 call.transA = (RsBlasTranspose)TransA;
561 call.transB = (RsBlasTranspose)TransB;
562 call.side = (RsBlasSide)Side;
563 call.uplo = (RsBlasUplo)Uplo;
564 call.diag = (RsBlasDiag)Diag;
565 call.M = M;
566 call.N = N;
567 call.K = K;
568 call.alpha.f = alpha;
569 call.beta.f = beta;
570 call.incX = incX;
571 call.incY = incY;
572 call.KL = KL;
573 call.KU = KU;
574
575 RsAllocation in_allocs[3];
576 in_allocs[0] = (RsAllocation)A;
577 in_allocs[1] = (RsAllocation)B;
578 in_allocs[2] = (RsAllocation)C;
579
580 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
581 in_allocs, sizeof(in_allocs), nullptr,
582 &call, sizeof(call), nullptr, 0);
583}
584
585static void
586nScriptIntrinsicBLAS_Double(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
587 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
588 jdouble alpha, jlong A, jlong B, jdouble beta, jlong C, jint incX, jint incY,
589 jint KL, jint KU) {
590 RsBlasCall call;
591 memset(&call, 0, sizeof(call));
592 call.func = (RsBlasFunction)func;
593 call.transA = (RsBlasTranspose)TransA;
594 call.transB = (RsBlasTranspose)TransB;
595 call.side = (RsBlasSide)Side;
596 call.uplo = (RsBlasUplo)Uplo;
597 call.diag = (RsBlasDiag)Diag;
598 call.M = M;
599 call.N = N;
600 call.K = K;
601 call.alpha.d = alpha;
602 call.beta.d = beta;
603 call.incX = incX;
604 call.incY = incY;
605 call.KL = KL;
606 call.KU = KU;
607
608 RsAllocation in_allocs[3];
609 in_allocs[0] = (RsAllocation)A;
610 in_allocs[1] = (RsAllocation)B;
611 in_allocs[2] = (RsAllocation)C;
612
613 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
614 in_allocs, sizeof(in_allocs), nullptr,
615 &call, sizeof(call), nullptr, 0);
616}
617
618static void
619nScriptIntrinsicBLAS_Complex(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
620 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
621 jfloat alphaX, jfloat alphaY, jlong A, jlong B, jfloat betaX,
622 jfloat betaY, jlong C, jint incX, jint incY, jint KL, jint KU) {
623 RsBlasCall call;
624 memset(&call, 0, sizeof(call));
625 call.func = (RsBlasFunction)func;
626 call.transA = (RsBlasTranspose)TransA;
627 call.transB = (RsBlasTranspose)TransB;
628 call.side = (RsBlasSide)Side;
629 call.uplo = (RsBlasUplo)Uplo;
630 call.diag = (RsBlasDiag)Diag;
631 call.M = M;
632 call.N = N;
633 call.K = K;
634 call.alpha.c.r = alphaX;
635 call.alpha.c.i = alphaY;
636 call.beta.c.r = betaX;
Miao Wang82585b32015-04-30 13:44:49 -0700637 call.beta.c.i = betaY;
Tim Murray25207df2015-01-12 16:47:56 -0800638 call.incX = incX;
639 call.incY = incY;
640 call.KL = KL;
641 call.KU = KU;
642
643 RsAllocation in_allocs[3];
644 in_allocs[0] = (RsAllocation)A;
645 in_allocs[1] = (RsAllocation)B;
646 in_allocs[2] = (RsAllocation)C;
647
648 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
649 in_allocs, sizeof(in_allocs), nullptr,
650 &call, sizeof(call), nullptr, 0);
651}
652
653static void
654nScriptIntrinsicBLAS_Z(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
655 jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
656 jdouble alphaX, jdouble alphaY, jlong A, jlong B, jdouble betaX,
657 jdouble betaY, jlong C, jint incX, jint incY, jint KL, jint KU) {
658 RsBlasCall call;
659 memset(&call, 0, sizeof(call));
660 call.func = (RsBlasFunction)func;
661 call.transA = (RsBlasTranspose)TransA;
662 call.transB = (RsBlasTranspose)TransB;
663 call.side = (RsBlasSide)Side;
664 call.uplo = (RsBlasUplo)Uplo;
665 call.diag = (RsBlasDiag)Diag;
666 call.M = M;
667 call.N = N;
668 call.K = K;
669 call.alpha.z.r = alphaX;
670 call.alpha.z.i = alphaY;
671 call.beta.z.r = betaX;
Miao Wang82585b32015-04-30 13:44:49 -0700672 call.beta.z.i = betaY;
Tim Murray25207df2015-01-12 16:47:56 -0800673 call.incX = incX;
674 call.incY = incY;
675 call.KL = KL;
676 call.KU = KU;
677
678 RsAllocation in_allocs[3];
679 in_allocs[0] = (RsAllocation)A;
680 in_allocs[1] = (RsAllocation)B;
681 in_allocs[2] = (RsAllocation)C;
682
683 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
684 in_allocs, sizeof(in_allocs), nullptr,
685 &call, sizeof(call), nullptr, 0);
686}
687
688
689static void
Tim Murray9cb16a22015-04-01 11:07:16 -0700690nScriptIntrinsicBLAS_BNNM(JNIEnv *_env, jobject _this, jlong con, jlong id, jint M, jint N, jint K,
691 jlong A, jint a_offset, jlong B, jint b_offset, jlong C, jint c_offset,
692 jint c_mult_int) {
693 RsBlasCall call;
694 memset(&call, 0, sizeof(call));
695 call.func = RsBlas_bnnm;
696 call.M = M;
697 call.N = N;
698 call.K = K;
Miao Wang6099ee62015-06-29 17:43:03 -0700699 call.a_offset = a_offset & 0xFF;
700 call.b_offset = b_offset & 0xFF;
Tim Murray9cb16a22015-04-01 11:07:16 -0700701 call.c_offset = c_offset;
702 call.c_mult_int = c_mult_int;
703
704 RsAllocation in_allocs[3];
705 in_allocs[0] = (RsAllocation)A;
706 in_allocs[1] = (RsAllocation)B;
707 in_allocs[2] = (RsAllocation)C;
708
709 rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
710 in_allocs, sizeof(in_allocs), nullptr,
711 &call, sizeof(call), nullptr, 0);
712}
713
714
715static void
Tim Murray460a0492013-11-19 12:45:54 -0800716nAssignName(JNIEnv *_env, jobject _this, jlong con, jlong obj, jbyteArray str)
Jason Sams3eaa338e2009-06-10 15:04:38 -0700717{
Andreas Gampe67333922014-11-10 20:35:59 -0800718 if (kLogApi) {
719 ALOGD("nAssignName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
720 }
Jason Sams3eaa338e2009-06-10 15:04:38 -0700721 jint len = _env->GetArrayLength(str);
722 jbyte * cptr = (jbyte *) _env->GetPrimitiveArrayCritical(str, 0);
Tim Murrayeff663f2013-11-15 13:08:30 -0800723 rsAssignName((RsContext)con, (void *)obj, (const char *)cptr, len);
Jason Sams3eaa338e2009-06-10 15:04:38 -0700724 _env->ReleasePrimitiveArrayCritical(str, cptr, JNI_ABORT);
725}
726
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700727static jstring
Tim Murray460a0492013-11-19 12:45:54 -0800728nGetName(JNIEnv *_env, jobject _this, jlong con, jlong obj)
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700729{
Andreas Gampe67333922014-11-10 20:35:59 -0800730 if (kLogApi) {
731 ALOGD("nGetName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
732 }
Chris Wailes488230c32014-08-14 11:22:40 -0700733 const char *name = nullptr;
Tim Murrayeff663f2013-11-15 13:08:30 -0800734 rsaGetName((RsContext)con, (void *)obj, &name);
Chris Wailes488230c32014-08-14 11:22:40 -0700735 if(name == nullptr || strlen(name) == 0) {
736 return nullptr;
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700737 }
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700738 return _env->NewStringUTF(name);
739}
740
Jason Sams7ce033d2009-08-18 14:14:24 -0700741static void
Tim Murray460a0492013-11-19 12:45:54 -0800742nObjDestroy(JNIEnv *_env, jobject _this, jlong con, jlong obj)
Jason Sams7ce033d2009-08-18 14:14:24 -0700743{
Andreas Gampe67333922014-11-10 20:35:59 -0800744 if (kLogApi) {
745 ALOGD("nObjDestroy, con(%p) obj(%p)", (RsContext)con, (void *)obj);
746 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800747 rsObjDestroy((RsContext)con, (void *)obj);
Jason Sams7ce033d2009-08-18 14:14:24 -0700748}
749
Jason Sams3eaa338e2009-06-10 15:04:38 -0700750// ---------------------------------------------------------------------------
751
Tim Murrayeff663f2013-11-15 13:08:30 -0800752static jlong
Jason Samsd19f10d2009-05-22 14:03:28 -0700753nDeviceCreate(JNIEnv *_env, jobject _this)
754{
Andreas Gampe67333922014-11-10 20:35:59 -0800755 if (kLogApi) {
756 ALOGD("nDeviceCreate");
757 }
Tim Murray3aa89c12014-08-18 17:51:22 -0700758 return (jlong)(uintptr_t)rsDeviceCreate();
Jason Samsd19f10d2009-05-22 14:03:28 -0700759}
760
761static void
Tim Murray5eaf4682014-01-10 11:25:52 -0800762nDeviceDestroy(JNIEnv *_env, jobject _this, jlong dev)
Jason Samsd19f10d2009-05-22 14:03:28 -0700763{
Andreas Gampe67333922014-11-10 20:35:59 -0800764 if (kLogApi) {
765 ALOGD("nDeviceDestroy");
766 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700767 return rsDeviceDestroy((RsDevice)dev);
768}
769
Jason Samsebfb4362009-09-23 13:57:02 -0700770static void
Tim Murray5eaf4682014-01-10 11:25:52 -0800771nDeviceSetConfig(JNIEnv *_env, jobject _this, jlong dev, jint p, jint value)
Jason Samsebfb4362009-09-23 13:57:02 -0700772{
Andreas Gampe67333922014-11-10 20:35:59 -0800773 if (kLogApi) {
774 ALOGD("nDeviceSetConfig dev(%p), param(%i), value(%i)", (void *)dev, p, value);
775 }
Jason Samsebfb4362009-09-23 13:57:02 -0700776 return rsDeviceSetConfig((RsDevice)dev, (RsDeviceParam)p, value);
777}
778
Tim Murrayeff663f2013-11-15 13:08:30 -0800779static jlong
Jason Sams81cd2b12014-12-02 12:36:43 -0800780nContextCreate(JNIEnv *_env, jobject _this, jlong dev, jint flags, jint sdkVer, jint contextType)
Jason Samsd19f10d2009-05-22 14:03:28 -0700781{
Andreas Gampe67333922014-11-10 20:35:59 -0800782 if (kLogApi) {
783 ALOGD("nContextCreate");
784 }
Jason Sams81cd2b12014-12-02 12:36:43 -0800785 return (jlong)(uintptr_t)rsContextCreate((RsDevice)dev, 0, sdkVer, (RsContextType)contextType, flags);
Jason Sams704ff642010-02-09 16:05:07 -0800786}
787
Tim Murrayeff663f2013-11-15 13:08:30 -0800788static jlong
Tim Murray5eaf4682014-01-10 11:25:52 -0800789nContextCreateGL(JNIEnv *_env, jobject _this, jlong dev, jint ver, jint sdkVer,
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000790 jint colorMin, jint colorPref,
791 jint alphaMin, jint alphaPref,
792 jint depthMin, jint depthPref,
793 jint stencilMin, jint stencilPref,
794 jint samplesMin, jint samplesPref, jfloat samplesQ,
795 jint dpi)
Jason Sams704ff642010-02-09 16:05:07 -0800796{
Jason Sams11c8af92010-10-13 15:31:10 -0700797 RsSurfaceConfig sc;
798 sc.alphaMin = alphaMin;
799 sc.alphaPref = alphaPref;
800 sc.colorMin = colorMin;
801 sc.colorPref = colorPref;
802 sc.depthMin = depthMin;
803 sc.depthPref = depthPref;
804 sc.samplesMin = samplesMin;
805 sc.samplesPref = samplesPref;
806 sc.samplesQ = samplesQ;
807
Andreas Gampe67333922014-11-10 20:35:59 -0800808 if (kLogApi) {
809 ALOGD("nContextCreateGL");
810 }
Tim Murray3aa89c12014-08-18 17:51:22 -0700811 return (jlong)(uintptr_t)rsContextCreateGL((RsDevice)dev, ver, sdkVer, sc, dpi);
Jason Samsd19f10d2009-05-22 14:03:28 -0700812}
813
814static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800815nContextSetPriority(JNIEnv *_env, jobject _this, jlong con, jint p)
Jason Sams7d787b42009-11-15 12:14:26 -0800816{
Andreas Gampe67333922014-11-10 20:35:59 -0800817 if (kLogApi) {
818 ALOGD("ContextSetPriority, con(%p), priority(%i)", (RsContext)con, p);
819 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800820 rsContextSetPriority((RsContext)con, p);
Jason Sams7d787b42009-11-15 12:14:26 -0800821}
822
Tim Murray47f31582015-04-07 15:43:24 -0700823static void
824nContextSetCacheDir(JNIEnv *_env, jobject _this, jlong con, jstring cacheDir)
825{
826 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
827
828 if (kLogApi) {
829 ALOGD("ContextSetCacheDir, con(%p), cacheDir(%s)", (RsContext)con, cacheDirUTF.c_str());
830 }
831 rsContextSetCacheDir((RsContext)con, cacheDirUTF.c_str(), cacheDirUTF.length());
832}
833
Jason Sams7d787b42009-11-15 12:14:26 -0800834
835
836static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800837nContextSetSurface(JNIEnv *_env, jobject _this, jlong con, jint width, jint height, jobject wnd)
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800838{
Andreas Gampe67333922014-11-10 20:35:59 -0800839 if (kLogApi) {
840 ALOGD("nContextSetSurface, con(%p), width(%i), height(%i), surface(%p)", (RsContext)con,
841 width, height, (Surface *)wnd);
842 }
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800843
Chris Wailes488230c32014-08-14 11:22:40 -0700844 ANativeWindow * window = nullptr;
845 if (wnd == nullptr) {
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800846
847 } else {
Jeff Brown64a55af2012-08-26 02:47:39 -0700848 window = android_view_Surface_getNativeWindow(_env, wnd).get();
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800849 }
850
Tim Murrayeff663f2013-11-15 13:08:30 -0800851 rsContextSetSurface((RsContext)con, width, height, window);
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800852}
853
854static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800855nContextDestroy(JNIEnv *_env, jobject _this, jlong con)
Jason Samsd19f10d2009-05-22 14:03:28 -0700856{
Andreas Gampe67333922014-11-10 20:35:59 -0800857 if (kLogApi) {
858 ALOGD("nContextDestroy, con(%p)", (RsContext)con);
859 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800860 rsContextDestroy((RsContext)con);
Jason Samsd19f10d2009-05-22 14:03:28 -0700861}
862
Jason Sams715333b2009-11-17 17:26:46 -0800863static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800864nContextDump(JNIEnv *_env, jobject _this, jlong con, jint bits)
Jason Sams715333b2009-11-17 17:26:46 -0800865{
Andreas Gampe67333922014-11-10 20:35:59 -0800866 if (kLogApi) {
867 ALOGD("nContextDump, con(%p) bits(%i)", (RsContext)con, bits);
868 }
Jason Sams715333b2009-11-17 17:26:46 -0800869 rsContextDump((RsContext)con, bits);
870}
Jason Samsd19f10d2009-05-22 14:03:28 -0700871
872static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800873nContextPause(JNIEnv *_env, jobject _this, jlong con)
Jason Sams65e7aa52009-09-24 17:38:20 -0700874{
Andreas Gampe67333922014-11-10 20:35:59 -0800875 if (kLogApi) {
876 ALOGD("nContextPause, con(%p)", (RsContext)con);
877 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800878 rsContextPause((RsContext)con);
Jason Sams65e7aa52009-09-24 17:38:20 -0700879}
880
881static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800882nContextResume(JNIEnv *_env, jobject _this, jlong con)
Jason Sams65e7aa52009-09-24 17:38:20 -0700883{
Andreas Gampe67333922014-11-10 20:35:59 -0800884 if (kLogApi) {
885 ALOGD("nContextResume, con(%p)", (RsContext)con);
886 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800887 rsContextResume((RsContext)con);
Jason Sams65e7aa52009-09-24 17:38:20 -0700888}
889
Jason Sams1c415172010-11-08 17:06:46 -0800890
891static jstring
Tim Murrayeff663f2013-11-15 13:08:30 -0800892nContextGetErrorMessage(JNIEnv *_env, jobject _this, jlong con)
Jason Sams1c415172010-11-08 17:06:46 -0800893{
Andreas Gampe67333922014-11-10 20:35:59 -0800894 if (kLogApi) {
895 ALOGD("nContextGetErrorMessage, con(%p)", (RsContext)con);
896 }
Jason Sams1c415172010-11-08 17:06:46 -0800897 char buf[1024];
898
899 size_t receiveLen;
900 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -0800901 int id = rsContextGetMessage((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -0700902 buf, sizeof(buf),
903 &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -0700904 &subID, sizeof(subID));
Jason Sams1c415172010-11-08 17:06:46 -0800905 if (!id && receiveLen) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +0100906 ALOGV("message receive buffer too small. %zu", receiveLen);
Jason Sams1c415172010-11-08 17:06:46 -0800907 }
908 return _env->NewStringUTF(buf);
909}
910
Jason Samsedbfabd2011-05-17 15:01:29 -0700911static jint
Tim Murrayeff663f2013-11-15 13:08:30 -0800912nContextGetUserMessage(JNIEnv *_env, jobject _this, jlong con, jintArray data)
Jason Sams516c3192009-10-06 13:58:47 -0700913{
Jason Sams516c3192009-10-06 13:58:47 -0700914 jint len = _env->GetArrayLength(data);
Andreas Gampe67333922014-11-10 20:35:59 -0800915 if (kLogApi) {
916 ALOGD("nContextGetMessage, con(%p), len(%i)", (RsContext)con, len);
917 }
Chris Wailes488230c32014-08-14 11:22:40 -0700918 jint *ptr = _env->GetIntArrayElements(data, nullptr);
Jason Sams516c3192009-10-06 13:58:47 -0700919 size_t receiveLen;
Jason Sams1c415172010-11-08 17:06:46 -0800920 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -0800921 int id = rsContextGetMessage((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -0700922 ptr, len * 4,
923 &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -0700924 &subID, sizeof(subID));
Jason Sams516c3192009-10-06 13:58:47 -0700925 if (!id && receiveLen) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +0100926 ALOGV("message receive buffer too small. %zu", receiveLen);
Jason Sams516c3192009-10-06 13:58:47 -0700927 }
928 _env->ReleaseIntArrayElements(data, ptr, 0);
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000929 return (jint)id;
Jason Sams1c415172010-11-08 17:06:46 -0800930}
931
932static jint
Tim Murrayeff663f2013-11-15 13:08:30 -0800933nContextPeekMessage(JNIEnv *_env, jobject _this, jlong con, jintArray auxData)
Jason Sams1c415172010-11-08 17:06:46 -0800934{
Andreas Gampe67333922014-11-10 20:35:59 -0800935 if (kLogApi) {
936 ALOGD("nContextPeekMessage, con(%p)", (RsContext)con);
937 }
Chris Wailes488230c32014-08-14 11:22:40 -0700938 jint *auxDataPtr = _env->GetIntArrayElements(auxData, nullptr);
Jason Sams1c415172010-11-08 17:06:46 -0800939 size_t receiveLen;
940 uint32_t subID;
Tim Murrayeff663f2013-11-15 13:08:30 -0800941 int id = rsContextPeekMessage((RsContext)con, &receiveLen, sizeof(receiveLen),
Jason Samsedbfabd2011-05-17 15:01:29 -0700942 &subID, sizeof(subID));
Jason Sams1c415172010-11-08 17:06:46 -0800943 auxDataPtr[0] = (jint)subID;
944 auxDataPtr[1] = (jint)receiveLen;
945 _env->ReleaseIntArrayElements(auxData, auxDataPtr, 0);
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000946 return (jint)id;
Jason Sams516c3192009-10-06 13:58:47 -0700947}
948
Tim Murrayeff663f2013-11-15 13:08:30 -0800949static void nContextInitToClient(JNIEnv *_env, jobject _this, jlong con)
Jason Sams516c3192009-10-06 13:58:47 -0700950{
Andreas Gampe67333922014-11-10 20:35:59 -0800951 if (kLogApi) {
952 ALOGD("nContextInitToClient, con(%p)", (RsContext)con);
953 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800954 rsContextInitToClient((RsContext)con);
Jason Sams516c3192009-10-06 13:58:47 -0700955}
956
Tim Murrayeff663f2013-11-15 13:08:30 -0800957static void nContextDeinitToClient(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("nContextDeinitToClient, con(%p)", (RsContext)con);
961 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800962 rsContextDeinitToClient((RsContext)con);
Jason Sams516c3192009-10-06 13:58:47 -0700963}
964
Jason Sams455d6442013-02-05 19:20:18 -0800965static void
Tim Murrayeff663f2013-11-15 13:08:30 -0800966nContextSendMessage(JNIEnv *_env, jobject _this, jlong con, jint id, jintArray data)
Jason Sams455d6442013-02-05 19:20:18 -0800967{
Chris Wailes488230c32014-08-14 11:22:40 -0700968 jint *ptr = nullptr;
Jason Sams455d6442013-02-05 19:20:18 -0800969 jint len = 0;
970 if (data) {
971 len = _env->GetArrayLength(data);
Stephen Hines4a043c12014-08-21 23:20:32 -0700972 ptr = _env->GetIntArrayElements(data, nullptr);
Jason Sams455d6442013-02-05 19:20:18 -0800973 }
Andreas Gampe67333922014-11-10 20:35:59 -0800974 if (kLogApi) {
975 ALOGD("nContextSendMessage, con(%p), id(%i), len(%i)", (RsContext)con, id, len);
976 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800977 rsContextSendMessage((RsContext)con, id, (const uint8_t *)ptr, len * sizeof(int));
Jason Sams455d6442013-02-05 19:20:18 -0800978 if (data) {
979 _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
980 }
981}
982
983
Jason Sams516c3192009-10-06 13:58:47 -0700984
Tim Murray460a0492013-11-19 12:45:54 -0800985static jlong
Andreas Gampe67333922014-11-10 20:35:59 -0800986nElementCreate(JNIEnv *_env, jobject _this, jlong con, jlong type, jint kind, jboolean norm,
987 jint size)
Jason Samsd19f10d2009-05-22 14:03:28 -0700988{
Andreas Gampe67333922014-11-10 20:35:59 -0800989 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +0100990 ALOGD("nElementCreate, con(%p), type(%" PRId64 "), kind(%i), norm(%i), size(%i)", (RsContext)con,
Andreas Gampe67333922014-11-10 20:35:59 -0800991 type, kind, norm, size);
992 }
993 return (jlong)(uintptr_t)rsElementCreate((RsContext)con, (RsDataType)type, (RsDataKind)kind,
994 norm, size);
Jason Samsd19f10d2009-05-22 14:03:28 -0700995}
996
Tim Murray460a0492013-11-19 12:45:54 -0800997static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -0800998nElementCreate2(JNIEnv *_env, jobject _this, jlong con,
Ashok Bhat98071552014-02-12 09:54:43 +0000999 jlongArray _ids, jobjectArray _names, jintArray _arraySizes)
Jason Samsd19f10d2009-05-22 14:03:28 -07001000{
Jason Sams718cd1f2009-12-23 14:35:29 -08001001 int fieldCount = _env->GetArrayLength(_ids);
Andreas Gampe67333922014-11-10 20:35:59 -08001002 if (kLogApi) {
1003 ALOGD("nElementCreate2, con(%p)", (RsContext)con);
1004 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001005
Chris Wailes488230c32014-08-14 11:22:40 -07001006 jlong *jIds = _env->GetLongArrayElements(_ids, nullptr);
1007 jint *jArraySizes = _env->GetIntArrayElements(_arraySizes, nullptr);
Ashok Bhat98071552014-02-12 09:54:43 +00001008
1009 RsElement *ids = (RsElement*)malloc(fieldCount * sizeof(RsElement));
1010 uint32_t *arraySizes = (uint32_t *)malloc(fieldCount * sizeof(uint32_t));
1011
1012 for(int i = 0; i < fieldCount; i ++) {
1013 ids[i] = (RsElement)jIds[i];
1014 arraySizes[i] = (uint32_t)jArraySizes[i];
1015 }
Jason Sams718cd1f2009-12-23 14:35:29 -08001016
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08001017 AutoJavaStringArrayToUTF8 names(_env, _names, fieldCount);
1018
1019 const char **nameArray = names.c_str();
1020 size_t *sizeArray = names.c_str_len();
1021
Tim Murray3aa89c12014-08-18 17:51:22 -07001022 jlong id = (jlong)(uintptr_t)rsElementCreate2((RsContext)con,
Ashok Bhat98071552014-02-12 09:54:43 +00001023 (const RsElement *)ids, fieldCount,
Jason Sams7a22e102011-05-06 14:14:30 -07001024 nameArray, fieldCount * sizeof(size_t), sizeArray,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001025 (const uint32_t *)arraySizes, fieldCount);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08001026
Ashok Bhat98071552014-02-12 09:54:43 +00001027 free(ids);
1028 free(arraySizes);
1029 _env->ReleaseLongArrayElements(_ids, jIds, JNI_ABORT);
1030 _env->ReleaseIntArrayElements(_arraySizes, jArraySizes, JNI_ABORT);
1031
Tim Murray3aa89c12014-08-18 17:51:22 -07001032 return (jlong)(uintptr_t)id;
Jason Samsd19f10d2009-05-22 14:03:28 -07001033}
1034
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001035static void
Tim Murray460a0492013-11-19 12:45:54 -08001036nElementGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jintArray _elementData)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001037{
1038 int dataSize = _env->GetArrayLength(_elementData);
Andreas Gampe67333922014-11-10 20:35:59 -08001039 if (kLogApi) {
1040 ALOGD("nElementGetNativeData, con(%p)", (RsContext)con);
1041 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001042
1043 // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
1044 assert(dataSize == 5);
1045
Narayan Kamath78c0ce52014-03-19 10:15:51 +00001046 uintptr_t elementData[5];
Tim Murrayeff663f2013-11-15 13:08:30 -08001047 rsaElementGetNativeData((RsContext)con, (RsElement)id, elementData, dataSize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001048
1049 for(jint i = 0; i < dataSize; i ++) {
Ashok Bhat98071552014-02-12 09:54:43 +00001050 const jint data = (jint)elementData[i];
1051 _env->SetIntArrayRegion(_elementData, i, 1, &data);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001052 }
1053}
1054
1055
1056static void
Tim Murray460a0492013-11-19 12:45:54 -08001057nElementGetSubElements(JNIEnv *_env, jobject _this, jlong con, jlong id,
Ashok Bhat98071552014-02-12 09:54:43 +00001058 jlongArray _IDs,
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -07001059 jobjectArray _names,
1060 jintArray _arraySizes)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001061{
Ashok Bhat98071552014-02-12 09:54:43 +00001062 uint32_t dataSize = _env->GetArrayLength(_IDs);
Andreas Gampe67333922014-11-10 20:35:59 -08001063 if (kLogApi) {
1064 ALOGD("nElementGetSubElements, con(%p)", (RsContext)con);
1065 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001066
Ashok Bhat98071552014-02-12 09:54:43 +00001067 uintptr_t *ids = (uintptr_t*)malloc(dataSize * sizeof(uintptr_t));
1068 const char **names = (const char **)malloc(dataSize * sizeof(const char *));
Narayan Kamath78c0ce52014-03-19 10:15:51 +00001069 uint32_t *arraySizes = (uint32_t *)malloc(dataSize * sizeof(uint32_t));
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001070
Andreas Gampe67333922014-11-10 20:35:59 -08001071 rsaElementGetSubElements((RsContext)con, (RsElement)id, ids, names, arraySizes,
1072 (uint32_t)dataSize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001073
Ashok Bhat98071552014-02-12 09:54:43 +00001074 for(uint32_t i = 0; i < dataSize; i++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07001075 const jlong id = (jlong)(uintptr_t)ids[i];
Ashok Bhat98071552014-02-12 09:54:43 +00001076 const jint arraySize = (jint)arraySizes[i];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001077 _env->SetObjectArrayElement(_names, i, _env->NewStringUTF(names[i]));
Ashok Bhat98071552014-02-12 09:54:43 +00001078 _env->SetLongArrayRegion(_IDs, i, 1, &id);
1079 _env->SetIntArrayRegion(_arraySizes, i, 1, &arraySize);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001080 }
1081
1082 free(ids);
1083 free(names);
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -07001084 free(arraySizes);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001085}
1086
Jason Samsd19f10d2009-05-22 14:03:28 -07001087// -----------------------------------
1088
Tim Murray460a0492013-11-19 12:45:54 -08001089static jlong
1090nTypeCreate(JNIEnv *_env, jobject _this, jlong con, jlong eid,
Jason Samsb109cc72013-01-07 18:20:12 -08001091 jint dimx, jint dimy, jint dimz, jboolean mips, jboolean faces, jint yuv)
Jason Samsd19f10d2009-05-22 14:03:28 -07001092{
Andreas Gampe67333922014-11-10 20:35:59 -08001093 if (kLogApi) {
1094 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 +01001095 (RsContext)con, (void*)eid, dimx, dimy, dimz, mips, faces, yuv);
Andreas Gampe67333922014-11-10 20:35:59 -08001096 }
Jason Sams3b9c52a2010-10-14 17:48:46 -07001097
Andreas Gampe67333922014-11-10 20:35:59 -08001098 return (jlong)(uintptr_t)rsTypeCreate((RsContext)con, (RsElement)eid, dimx, dimy, dimz, mips,
1099 faces, yuv);
Jason Samsd19f10d2009-05-22 14:03:28 -07001100}
1101
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001102static void
Ashok Bhat98071552014-02-12 09:54:43 +00001103nTypeGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jlongArray _typeData)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001104{
1105 // We are packing 6 items: mDimX; mDimY; mDimZ;
1106 // mDimLOD; mDimFaces; mElement; into typeData
1107 int elementCount = _env->GetArrayLength(_typeData);
1108
1109 assert(elementCount == 6);
Andreas Gampe67333922014-11-10 20:35:59 -08001110 if (kLogApi) {
1111 ALOGD("nTypeGetNativeData, con(%p)", (RsContext)con);
1112 }
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001113
Ashok Bhat98071552014-02-12 09:54:43 +00001114 uintptr_t typeData[6];
Tim Murrayeff663f2013-11-15 13:08:30 -08001115 rsaTypeGetNativeData((RsContext)con, (RsType)id, typeData, 6);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001116
1117 for(jint i = 0; i < elementCount; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07001118 const jlong data = (jlong)(uintptr_t)typeData[i];
Ashok Bhat98071552014-02-12 09:54:43 +00001119 _env->SetLongArrayRegion(_typeData, i, 1, &data);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001120 }
1121}
1122
Jason Samsd19f10d2009-05-22 14:03:28 -07001123// -----------------------------------
1124
Tim Murray460a0492013-11-19 12:45:54 -08001125static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001126nAllocationCreateTyped(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mips, jint usage,
1127 jlong pointer)
Jason Samsd19f10d2009-05-22 14:03:28 -07001128{
Andreas Gampe67333922014-11-10 20:35:59 -08001129 if (kLogApi) {
1130 ALOGD("nAllocationCreateTyped, con(%p), type(%p), mip(%i), usage(%i), ptr(%p)",
1131 (RsContext)con, (RsElement)type, mips, usage, (void *)pointer);
1132 }
1133 return (jlong)(uintptr_t) rsAllocationCreateTyped((RsContext)con, (RsType)type,
1134 (RsAllocationMipmapControl)mips,
1135 (uint32_t)usage, (uintptr_t)pointer);
Jason Samsd19f10d2009-05-22 14:03:28 -07001136}
1137
Jason Samsd19f10d2009-05-22 14:03:28 -07001138static void
Tim Murray460a0492013-11-19 12:45:54 -08001139nAllocationSyncAll(JNIEnv *_env, jobject _this, jlong con, jlong a, jint bits)
Jason Sams5476b452010-12-08 16:14:36 -08001140{
Andreas Gampe67333922014-11-10 20:35:59 -08001141 if (kLogApi) {
1142 ALOGD("nAllocationSyncAll, con(%p), a(%p), bits(0x%08x)", (RsContext)con, (RsAllocation)a,
1143 bits);
1144 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001145 rsAllocationSyncAll((RsContext)con, (RsAllocation)a, (RsAllocationUsageType)bits);
Jason Sams5476b452010-12-08 16:14:36 -08001146}
1147
Jason Sams72226e02013-02-22 12:45:54 -08001148static jobject
Tim Murray460a0492013-11-19 12:45:54 -08001149nAllocationGetSurface(JNIEnv *_env, jobject _this, jlong con, jlong a)
Jason Sams615e7ce2012-01-13 14:01:20 -08001150{
Andreas Gampe67333922014-11-10 20:35:59 -08001151 if (kLogApi) {
1152 ALOGD("nAllocationGetSurface, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
1153 }
Jason Sams615e7ce2012-01-13 14:01:20 -08001154
Andreas Gampe67333922014-11-10 20:35:59 -08001155 IGraphicBufferProducer *v = (IGraphicBufferProducer *)rsAllocationGetSurface((RsContext)con,
1156 (RsAllocation)a);
Jason Sams72226e02013-02-22 12:45:54 -08001157 sp<IGraphicBufferProducer> bp = v;
Chris Wailes488230c32014-08-14 11:22:40 -07001158 v->decStrong(nullptr);
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001159
Jason Sams72226e02013-02-22 12:45:54 -08001160 jobject o = android_view_Surface_createFromIGraphicBufferProducer(_env, bp);
1161 return o;
Jason Samsfe1d5ff2012-03-23 11:47:26 -07001162}
1163
1164static void
Tim Murray460a0492013-11-19 12:45:54 -08001165nAllocationSetSurface(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject sur)
Jason Sams163766c2012-02-15 12:04:24 -08001166{
Andreas Gampe67333922014-11-10 20:35:59 -08001167 if (kLogApi) {
1168 ALOGD("nAllocationSetSurface, con(%p), alloc(%p), surface(%p)", (RsContext)con,
1169 (RsAllocation)alloc, (Surface *)sur);
1170 }
Jason Sams163766c2012-02-15 12:04:24 -08001171
Jason Samsfb9aa9f2012-03-28 15:30:07 -07001172 sp<Surface> s;
Jason Sams163766c2012-02-15 12:04:24 -08001173 if (sur != 0) {
Jeff Brown64a55af2012-08-26 02:47:39 -07001174 s = android_view_Surface_getSurface(_env, sur);
Jason Sams163766c2012-02-15 12:04:24 -08001175 }
1176
Andreas Gampe67333922014-11-10 20:35:59 -08001177 rsAllocationSetSurface((RsContext)con, (RsAllocation)alloc,
1178 static_cast<ANativeWindow *>(s.get()));
Jason Sams163766c2012-02-15 12:04:24 -08001179}
1180
1181static void
Tim Murray460a0492013-11-19 12:45:54 -08001182nAllocationIoSend(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Sams163766c2012-02-15 12:04:24 -08001183{
Andreas Gampe67333922014-11-10 20:35:59 -08001184 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001185 ALOGD("nAllocationIoSend, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
Andreas Gampe67333922014-11-10 20:35:59 -08001186 }
Tim Murray460a0492013-11-19 12:45:54 -08001187 rsAllocationIoSend((RsContext)con, (RsAllocation)alloc);
Jason Sams163766c2012-02-15 12:04:24 -08001188}
1189
1190static void
Tim Murray460a0492013-11-19 12:45:54 -08001191nAllocationIoReceive(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Sams163766c2012-02-15 12:04:24 -08001192{
Andreas Gampe67333922014-11-10 20:35:59 -08001193 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001194 ALOGD("nAllocationIoReceive, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
Andreas Gampe67333922014-11-10 20:35:59 -08001195 }
Tim Murray460a0492013-11-19 12:45:54 -08001196 rsAllocationIoReceive((RsContext)con, (RsAllocation)alloc);
Jason Sams163766c2012-02-15 12:04:24 -08001197}
1198
1199
1200static void
Tim Murray460a0492013-11-19 12:45:54 -08001201nAllocationGenerateMipmaps(JNIEnv *_env, jobject _this, jlong con, jlong alloc)
Jason Samsf7086092011-01-12 13:28:37 -08001202{
Andreas Gampe67333922014-11-10 20:35:59 -08001203 if (kLogApi) {
1204 ALOGD("nAllocationGenerateMipmaps, con(%p), a(%p)", (RsContext)con, (RsAllocation)alloc);
1205 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001206 rsAllocationGenerateMipmaps((RsContext)con, (RsAllocation)alloc);
Jason Samsf7086092011-01-12 13:28:37 -08001207}
1208
Tim Murray460a0492013-11-19 12:45:54 -08001209static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001210nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip,
1211 jobject jbitmap, jint usage)
Jason Samsffe9f482009-06-01 17:45:53 -07001212{
John Recked207b92015-04-10 13:52:57 -07001213 SkBitmap bitmap;
1214 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Samsffe9f482009-06-01 17:45:53 -07001215
Jason Sams5476b452010-12-08 16:14:36 -08001216 bitmap.lockPixels();
1217 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001218 jlong id = (jlong)(uintptr_t)rsAllocationCreateFromBitmap((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -07001219 (RsType)type, (RsAllocationMipmapControl)mip,
1220 ptr, bitmap.getSize(), usage);
Jason Sams5476b452010-12-08 16:14:36 -08001221 bitmap.unlockPixels();
1222 return id;
Jason Samsffe9f482009-06-01 17:45:53 -07001223}
Jason Samsfe08d992009-05-27 14:45:32 -07001224
Tim Murray460a0492013-11-19 12:45:54 -08001225static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001226nAllocationCreateBitmapBackedAllocation(JNIEnv *_env, jobject _this, jlong con, jlong type,
1227 jint mip, jobject jbitmap, jint usage)
Tim Murraya3145512012-12-04 17:59:29 -08001228{
John Recked207b92015-04-10 13:52:57 -07001229 SkBitmap bitmap;
1230 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Tim Murraya3145512012-12-04 17:59:29 -08001231
1232 bitmap.lockPixels();
1233 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001234 jlong id = (jlong)(uintptr_t)rsAllocationCreateTyped((RsContext)con,
Tim Murraya3145512012-12-04 17:59:29 -08001235 (RsType)type, (RsAllocationMipmapControl)mip,
Ashok Bhat98071552014-02-12 09:54:43 +00001236 (uint32_t)usage, (uintptr_t)ptr);
Tim Murraya3145512012-12-04 17:59:29 -08001237 bitmap.unlockPixels();
1238 return id;
1239}
1240
Tim Murray460a0492013-11-19 12:45:54 -08001241static jlong
Andreas Gampe67333922014-11-10 20:35:59 -08001242nAllocationCubeCreateFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong type, jint mip,
1243 jobject jbitmap, jint usage)
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001244{
John Recked207b92015-04-10 13:52:57 -07001245 SkBitmap bitmap;
1246 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001247
Jason Sams5476b452010-12-08 16:14:36 -08001248 bitmap.lockPixels();
1249 const void* ptr = bitmap.getPixels();
Tim Murray3aa89c12014-08-18 17:51:22 -07001250 jlong id = (jlong)(uintptr_t)rsAllocationCubeCreateFromBitmap((RsContext)con,
Jason Sams65bdaf12011-04-26 14:50:00 -07001251 (RsType)type, (RsAllocationMipmapControl)mip,
1252 ptr, bitmap.getSize(), usage);
Jason Sams5476b452010-12-08 16:14:36 -08001253 bitmap.unlockPixels();
1254 return id;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -08001255}
1256
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001257static void
Tim Murray460a0492013-11-19 12:45:54 -08001258nAllocationCopyFromBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001259{
John Recked207b92015-04-10 13:52:57 -07001260 SkBitmap bitmap;
1261 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Samsf7086092011-01-12 13:28:37 -08001262 int w = bitmap.width();
1263 int h = bitmap.height();
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001264
Jason Sams4ef66502010-12-10 16:03:15 -08001265 bitmap.lockPixels();
1266 const void* ptr = bitmap.getPixels();
Tim Murrayeff663f2013-11-15 13:08:30 -08001267 rsAllocation2DData((RsContext)con, (RsAllocation)alloc, 0, 0,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001268 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
Tim Murray38faea302012-11-27 14:55:08 -08001269 w, h, ptr, bitmap.getSize(), 0);
Jason Sams4ef66502010-12-10 16:03:15 -08001270 bitmap.unlockPixels();
1271}
1272
1273static void
Tim Murray460a0492013-11-19 12:45:54 -08001274nAllocationCopyToBitmap(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jobject jbitmap)
Jason Sams4ef66502010-12-10 16:03:15 -08001275{
John Recked207b92015-04-10 13:52:57 -07001276 SkBitmap bitmap;
1277 GraphicsJNI::getSkBitmap(_env, jbitmap, &bitmap);
Jason Sams4ef66502010-12-10 16:03:15 -08001278
1279 bitmap.lockPixels();
1280 void* ptr = bitmap.getPixels();
Tim Murrayeff663f2013-11-15 13:08:30 -08001281 rsAllocationCopyToBitmap((RsContext)con, (RsAllocation)alloc, ptr, bitmap.getSize());
Jason Sams4ef66502010-12-10 16:03:15 -08001282 bitmap.unlockPixels();
Alex Sakhartchouk835b8542011-07-20 14:33:10 -07001283 bitmap.notifyPixelsChanged();
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -07001284}
1285
Stephen Hines414fa2c2014-04-17 01:02:42 -07001286// Copies from the Java object data into the Allocation pointed to by _alloc.
Jason Samsd19f10d2009-05-22 14:03:28 -07001287static void
Tim Murray460a0492013-11-19 12:45:54 -08001288nAllocationData1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001289 jint count, jobject data, jint sizeBytes, jint dataType, jint mSize,
1290 jboolean usePadding)
Jason Samsd19f10d2009-05-22 14:03:28 -07001291{
Jason Samse729a942013-11-06 11:22:02 -08001292 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001293 if (kLogApi) {
1294 ALOGD("nAllocation1DData, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
1295 "dataType(%i)", (RsContext)con, (RsAllocation)alloc, offset, count, sizeBytes,
1296 dataType);
1297 }
Miao Wang87e908d2015-03-02 15:15:15 -08001298 PER_ARRAY_TYPE(nullptr, rsAllocation1DData, true,
1299 (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
Jason Samsd19f10d2009-05-22 14:03:28 -07001300}
1301
1302static void
Miao Wangc8e237e2015-02-20 18:36:32 -08001303nAllocationElementData(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
1304 jint xoff, jint yoff, jint zoff,
1305 jint lod, jint compIdx, jbyteArray data, jint sizeBytes)
Jason Sams49bdaf02010-08-31 13:50:42 -07001306{
1307 jint len = _env->GetArrayLength(data);
Andreas Gampe67333922014-11-10 20:35:59 -08001308 if (kLogApi) {
Miao Wangc8e237e2015-02-20 18:36:32 -08001309 ALOGD("nAllocationElementData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), comp(%i), len(%i), "
1310 "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, compIdx, len,
Andreas Gampe67333922014-11-10 20:35:59 -08001311 sizeBytes);
1312 }
Chris Wailes488230c32014-08-14 11:22:40 -07001313 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Miao Wangc8e237e2015-02-20 18:36:32 -08001314 rsAllocationElementData((RsContext)con, (RsAllocation)alloc,
1315 xoff, yoff, zoff,
1316 lod, ptr, sizeBytes, compIdx);
Jason Sams49bdaf02010-08-31 13:50:42 -07001317 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1318}
1319
Miao Wangc8e237e2015-02-20 18:36:32 -08001320
Stephen Hines414fa2c2014-04-17 01:02:42 -07001321// Copies from the Java object data into the Allocation pointed to by _alloc.
Jason Sams49bdaf02010-08-31 13:50:42 -07001322static void
Tim Murray460a0492013-11-19 12:45:54 -08001323nAllocationData2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
Miao Wang87e908d2015-03-02 15:15:15 -08001324 jint w, jint h, jobject data, jint sizeBytes, jint dataType, jint mSize,
1325 jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001326{
Jason Samse729a942013-11-06 11:22:02 -08001327 RsAllocation *alloc = (RsAllocation *)_alloc;
1328 RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
Andreas Gampe67333922014-11-10 20:35:59 -08001329 if (kLogApi) {
1330 ALOGD("nAllocation2DData, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1331 "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1332 }
Miao Wang87e908d2015-03-02 15:15:15 -08001333 int count = w * h;
1334 PER_ARRAY_TYPE(nullptr, rsAllocation2DData, true,
1335 (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
Jason Samsd19f10d2009-05-22 14:03:28 -07001336}
1337
Stephen Hines414fa2c2014-04-17 01:02:42 -07001338// Copies from the Allocation pointed to by srcAlloc into the Allocation
1339// pointed to by dstAlloc.
Jason Sams40a29e82009-08-10 14:55:26 -07001340static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001341nAllocationData2D_alloc(JNIEnv *_env, jobject _this, jlong con,
Tim Murray460a0492013-11-19 12:45:54 -08001342 jlong dstAlloc, jint dstXoff, jint dstYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001343 jint dstMip, jint dstFace,
1344 jint width, jint height,
Tim Murray460a0492013-11-19 12:45:54 -08001345 jlong srcAlloc, jint srcXoff, jint srcYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001346 jint srcMip, jint srcFace)
1347{
Andreas Gampe67333922014-11-10 20:35:59 -08001348 if (kLogApi) {
1349 ALOGD("nAllocation2DData_s, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
1350 " dstMip(%i), dstFace(%i), width(%i), height(%i),"
1351 " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i), srcFace(%i)",
1352 (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip, dstFace,
1353 width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip, srcFace);
1354 }
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001355
Tim Murrayeff663f2013-11-15 13:08:30 -08001356 rsAllocationCopy2DRange((RsContext)con,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001357 (RsAllocation)dstAlloc,
1358 dstXoff, dstYoff,
1359 dstMip, dstFace,
1360 width, height,
1361 (RsAllocation)srcAlloc,
1362 srcXoff, srcYoff,
1363 srcMip, srcFace);
1364}
1365
Stephen Hines414fa2c2014-04-17 01:02:42 -07001366// Copies from the Java object data into the Allocation pointed to by _alloc.
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -07001367static void
Tim Murray460a0492013-11-19 12:45:54 -08001368nAllocationData3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001369 jint w, jint h, jint d, jobject data, jint sizeBytes, jint dataType,
1370 jint mSize, jboolean usePadding)
Jason Samsb05d6892013-04-09 15:59:24 -07001371{
Jason Samse729a942013-11-06 11:22:02 -08001372 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001373 if (kLogApi) {
1374 ALOGD("nAllocation3DData, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i),"
1375 " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff,
1376 lod, w, h, d, sizeBytes);
1377 }
Miao Wang87e908d2015-03-02 15:15:15 -08001378 int count = w * h * d;
1379 PER_ARRAY_TYPE(nullptr, rsAllocation3DData, true,
1380 (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
Jason Samsb05d6892013-04-09 15:59:24 -07001381}
1382
Stephen Hines414fa2c2014-04-17 01:02:42 -07001383// Copies from the Allocation pointed to by srcAlloc into the Allocation
1384// pointed to by dstAlloc.
Jason Samsb05d6892013-04-09 15:59:24 -07001385static void
Tim Murrayeff663f2013-11-15 13:08:30 -08001386nAllocationData3D_alloc(JNIEnv *_env, jobject _this, jlong con,
Tim Murray460a0492013-11-19 12:45:54 -08001387 jlong dstAlloc, jint dstXoff, jint dstYoff, jint dstZoff,
Jason Samsb05d6892013-04-09 15:59:24 -07001388 jint dstMip,
1389 jint width, jint height, jint depth,
Tim Murray460a0492013-11-19 12:45:54 -08001390 jlong srcAlloc, jint srcXoff, jint srcYoff, jint srcZoff,
Jason Samsb05d6892013-04-09 15:59:24 -07001391 jint srcMip)
1392{
Andreas Gampe67333922014-11-10 20:35:59 -08001393 if (kLogApi) {
1394 ALOGD("nAllocationData3D_alloc, con(%p), dstAlloc(%p), dstXoff(%i), dstYoff(%i),"
1395 " dstMip(%i), width(%i), height(%i),"
1396 " srcAlloc(%p), srcXoff(%i), srcYoff(%i), srcMip(%i)",
1397 (RsContext)con, (RsAllocation)dstAlloc, dstXoff, dstYoff, dstMip,
1398 width, height, (RsAllocation)srcAlloc, srcXoff, srcYoff, srcMip);
1399 }
Jason Samsb05d6892013-04-09 15:59:24 -07001400
Tim Murrayeff663f2013-11-15 13:08:30 -08001401 rsAllocationCopy3DRange((RsContext)con,
Jason Samsb05d6892013-04-09 15:59:24 -07001402 (RsAllocation)dstAlloc,
1403 dstXoff, dstYoff, dstZoff, dstMip,
1404 width, height, depth,
1405 (RsAllocation)srcAlloc,
1406 srcXoff, srcYoff, srcZoff, srcMip);
1407}
1408
Jason Sams21659ac2013-11-06 15:08:07 -08001409
Stephen Hines414fa2c2014-04-17 01:02:42 -07001410// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Samsb05d6892013-04-09 15:59:24 -07001411static void
Miao Wang87e908d2015-03-02 15:15:15 -08001412nAllocationRead(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jobject data, jint dataType,
1413 jint mSize, jboolean usePadding)
Jason Sams40a29e82009-08-10 14:55:26 -07001414{
Jason Sams21659ac2013-11-06 15:08:07 -08001415 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001416 if (kLogApi) {
1417 ALOGD("nAllocationRead, con(%p), alloc(%p)", (RsContext)con, (RsAllocation)alloc);
1418 }
Miao Wang87e908d2015-03-02 15:15:15 -08001419 int count = 0;
1420 PER_ARRAY_TYPE(0, rsAllocationRead, false,
1421 (RsContext)con, alloc, ptr, len * typeBytes);
Jason Sams40a29e82009-08-10 14:55:26 -07001422}
1423
Stephen Hines414fa2c2014-04-17 01:02:42 -07001424// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Sams40a29e82009-08-10 14:55:26 -07001425static void
Tim Murray460a0492013-11-19 12:45:54 -08001426nAllocationRead1D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint offset, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001427 jint count, jobject data, jint sizeBytes, jint dataType,
1428 jint mSize, jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001429{
Jason Sams21659ac2013-11-06 15:08:07 -08001430 RsAllocation *alloc = (RsAllocation *)_alloc;
Andreas Gampe67333922014-11-10 20:35:59 -08001431 if (kLogApi) {
1432 ALOGD("nAllocation1DRead, con(%p), adapter(%p), offset(%i), count(%i), sizeBytes(%i), "
1433 "dataType(%i)", (RsContext)con, alloc, offset, count, sizeBytes, dataType);
1434 }
Miao Wang87e908d2015-03-02 15:15:15 -08001435 PER_ARRAY_TYPE(0, rsAllocation1DRead, false,
1436 (RsContext)con, alloc, offset, lod, count, ptr, sizeBytes);
Jason Samsfb9f82c2011-01-12 14:53:25 -08001437}
1438
Miao Wangc8e237e2015-02-20 18:36:32 -08001439// Copies from the Element in the Allocation pointed to by _alloc into the Java array data.
1440static void
Miao Wang45cec0a2015-03-04 16:40:21 -08001441nAllocationElementRead(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
Miao Wangc8e237e2015-02-20 18:36:32 -08001442 jint xoff, jint yoff, jint zoff,
Miao Wang45cec0a2015-03-04 16:40:21 -08001443 jint lod, jint compIdx, jbyteArray data, jint sizeBytes)
Miao Wangc8e237e2015-02-20 18:36:32 -08001444{
Miao Wang45cec0a2015-03-04 16:40:21 -08001445 jint len = _env->GetArrayLength(data);
Miao Wangc8e237e2015-02-20 18:36:32 -08001446 if (kLogApi) {
Miao Wang45cec0a2015-03-04 16:40:21 -08001447 ALOGD("nAllocationElementRead, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), comp(%i), len(%i), "
1448 "sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff, compIdx, len,
1449 sizeBytes);
Miao Wangc8e237e2015-02-20 18:36:32 -08001450 }
Miao Wang45cec0a2015-03-04 16:40:21 -08001451 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
1452 rsAllocationElementRead((RsContext)con, (RsAllocation)alloc,
1453 xoff, yoff, zoff,
Jason Samsa7e25092015-03-11 11:00:00 -07001454 lod, ptr, sizeBytes, compIdx);
Miao Wang45cec0a2015-03-04 16:40:21 -08001455 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
Miao Wangc8e237e2015-02-20 18:36:32 -08001456}
1457
Stephen Hines414fa2c2014-04-17 01:02:42 -07001458// Copies from the Allocation pointed to by _alloc into the Java object data.
Jason Samsfb9f82c2011-01-12 14:53:25 -08001459static void
Tim Murray460a0492013-11-19 12:45:54 -08001460nAllocationRead2D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint lod, jint _face,
Miao Wang87e908d2015-03-02 15:15:15 -08001461 jint w, jint h, jobject data, jint sizeBytes, jint dataType,
1462 jint mSize, jboolean usePadding)
Jason Samsfb9f82c2011-01-12 14:53:25 -08001463{
Jason Sams21659ac2013-11-06 15:08:07 -08001464 RsAllocation *alloc = (RsAllocation *)_alloc;
1465 RsAllocationCubemapFace face = (RsAllocationCubemapFace)_face;
Andreas Gampe67333922014-11-10 20:35:59 -08001466 if (kLogApi) {
1467 ALOGD("nAllocation2DRead, con(%p), adapter(%p), xoff(%i), yoff(%i), w(%i), h(%i), len(%i) "
1468 "type(%i)", (RsContext)con, alloc, xoff, yoff, w, h, sizeBytes, dataType);
1469 }
Miao Wang87e908d2015-03-02 15:15:15 -08001470 int count = w * h;
1471 PER_ARRAY_TYPE(0, rsAllocation2DRead, false,
1472 (RsContext)con, alloc, xoff, yoff, lod, face, w, h, ptr, sizeBytes, 0);
Jason Sams40a29e82009-08-10 14:55:26 -07001473}
Miao Wang87e908d2015-03-02 15:15:15 -08001474
Miao Wangc8e237e2015-02-20 18:36:32 -08001475// Copies from the Allocation pointed to by _alloc into the Java object data.
1476static void
1477nAllocationRead3D(JNIEnv *_env, jobject _this, jlong con, jlong _alloc, jint xoff, jint yoff, jint zoff, jint lod,
Miao Wang87e908d2015-03-02 15:15:15 -08001478 jint w, jint h, jint d, jobject data, int sizeBytes, int dataType,
1479 jint mSize, jboolean usePadding)
Miao Wangc8e237e2015-02-20 18:36:32 -08001480{
1481 RsAllocation *alloc = (RsAllocation *)_alloc;
1482 if (kLogApi) {
1483 ALOGD("nAllocation3DRead, con(%p), alloc(%p), xoff(%i), yoff(%i), zoff(%i), lod(%i), w(%i),"
1484 " h(%i), d(%i), sizeBytes(%i)", (RsContext)con, (RsAllocation)alloc, xoff, yoff, zoff,
1485 lod, w, h, d, sizeBytes);
1486 }
Miao Wang87e908d2015-03-02 15:15:15 -08001487 int count = w * h * d;
1488 PER_ARRAY_TYPE(nullptr, rsAllocation3DRead, false,
1489 (RsContext)con, alloc, xoff, yoff, zoff, lod, w, h, d, ptr, sizeBytes, 0);
Miao Wangc8e237e2015-02-20 18:36:32 -08001490}
Jason Samsd19f10d2009-05-22 14:03:28 -07001491
Tim Murray460a0492013-11-19 12:45:54 -08001492static jlong
1493nAllocationGetType(JNIEnv *_env, jobject _this, jlong con, jlong a)
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001494{
Andreas Gampe67333922014-11-10 20:35:59 -08001495 if (kLogApi) {
1496 ALOGD("nAllocationGetType, con(%p), a(%p)", (RsContext)con, (RsAllocation)a);
1497 }
Tim Murray3aa89c12014-08-18 17:51:22 -07001498 return (jlong)(uintptr_t) rsaAllocationGetType((RsContext)con, (RsAllocation)a);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -07001499}
1500
Jason Sams5edc6082010-10-05 13:32:49 -07001501static void
Tim Murray460a0492013-11-19 12:45:54 -08001502nAllocationResize1D(JNIEnv *_env, jobject _this, jlong con, jlong alloc, jint dimX)
Jason Sams5edc6082010-10-05 13:32:49 -07001503{
Andreas Gampe67333922014-11-10 20:35:59 -08001504 if (kLogApi) {
1505 ALOGD("nAllocationResize1D, con(%p), alloc(%p), sizeX(%i)", (RsContext)con,
1506 (RsAllocation)alloc, dimX);
1507 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001508 rsAllocationResize1D((RsContext)con, (RsAllocation)alloc, dimX);
Jason Sams5edc6082010-10-05 13:32:49 -07001509}
1510
Jason Sams46ba27e32015-02-06 17:45:15 -08001511
1512static jlong
1513nAllocationAdapterCreate(JNIEnv *_env, jobject _this, jlong con, jlong basealloc, jlong type)
1514{
1515 if (kLogApi) {
1516 ALOGD("nAllocationAdapterCreate, con(%p), base(%p), type(%p)",
1517 (RsContext)con, (RsAllocation)basealloc, (RsElement)type);
1518 }
1519 return (jlong)(uintptr_t) rsAllocationAdapterCreate((RsContext)con, (RsType)type,
1520 (RsAllocation)basealloc);
1521
1522}
1523
1524static void
1525nAllocationAdapterOffset(JNIEnv *_env, jobject _this, jlong con, jlong alloc,
1526 jint x, jint y, jint z, jint face, jint lod,
1527 jint a1, jint a2, jint a3, jint a4)
1528{
1529 uint32_t params[] = {
1530 (uint32_t)x, (uint32_t)y, (uint32_t)z, (uint32_t)face,
1531 (uint32_t)lod, (uint32_t)a1, (uint32_t)a2, (uint32_t)a3, (uint32_t)a4
1532 };
1533 if (kLogApi) {
1534 ALOGD("nAllocationAdapterOffset, con(%p), alloc(%p), x(%i), y(%i), z(%i), face(%i), lod(%i), arrays(%i %i %i %i)",
1535 (RsContext)con, (RsAllocation)alloc, x, y, z, face, lod, a1, a2, a3, a4);
1536 }
1537 rsAllocationAdapterOffset((RsContext)con, (RsAllocation)alloc,
1538 params, sizeof(params));
1539}
1540
1541
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001542// -----------------------------------
1543
Tim Murray460a0492013-11-19 12:45:54 -08001544static jlong
1545nFileA3DCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con, jlong native_asset)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001546{
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001547 Asset* asset = reinterpret_cast<Asset*>(native_asset);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001548 ALOGV("______nFileA3D %p", asset);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001549
Tim Murray3aa89c12014-08-18 17:51:22 -07001550 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromMemory((RsContext)con, asset->getBuffer(false), asset->getLength());
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001551 return id;
1552}
1553
Tim Murray460a0492013-11-19 12:45:54 -08001554static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001555nFileA3DCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001556{
1557 AssetManager* mgr = assetManagerForJavaObject(_env, _assetMgr);
Chris Wailes488230c32014-08-14 11:22:40 -07001558 if (mgr == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001559 return 0;
1560 }
1561
1562 AutoJavaStringToUTF8 str(_env, _path);
1563 Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
Chris Wailes488230c32014-08-14 11:22:40 -07001564 if (asset == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001565 return 0;
1566 }
1567
Tim Murray3aa89c12014-08-18 17:51:22 -07001568 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromAsset((RsContext)con, asset);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001569 return id;
1570}
1571
Tim Murray460a0492013-11-19 12:45:54 -08001572static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001573nFileA3DCreateFromFile(JNIEnv *_env, jobject _this, jlong con, jstring fileName)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001574{
1575 AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
Tim Murray3aa89c12014-08-18 17:51:22 -07001576 jlong id = (jlong)(uintptr_t)rsaFileA3DCreateFromFile((RsContext)con, fileNameUTF.c_str());
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001577
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001578 return id;
1579}
1580
Tim Murray460a0492013-11-19 12:45:54 -08001581static jint
1582nFileA3DGetNumIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001583{
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001584 int32_t numEntries = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001585 rsaFileA3DGetNumIndexEntries((RsContext)con, &numEntries, (RsFile)fileA3D);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001586 return (jint)numEntries;
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001587}
1588
1589static void
Tim Murray460a0492013-11-19 12:45:54 -08001590nFileA3DGetIndexEntries(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint numEntries, jintArray _ids, jobjectArray _entries)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001591{
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001592 ALOGV("______nFileA3D %p", (RsFile) fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001593 RsFileIndexEntry *fileEntries = (RsFileIndexEntry*)malloc((uint32_t)numEntries * sizeof(RsFileIndexEntry));
1594
Tim Murrayeff663f2013-11-15 13:08:30 -08001595 rsaFileA3DGetIndexEntries((RsContext)con, fileEntries, (uint32_t)numEntries, (RsFile)fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001596
1597 for(jint i = 0; i < numEntries; i ++) {
1598 _env->SetObjectArrayElement(_entries, i, _env->NewStringUTF(fileEntries[i].objectName));
1599 _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&fileEntries[i].classID);
1600 }
1601
1602 free(fileEntries);
1603}
1604
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001605static jlong
Tim Murray460a0492013-11-19 12:45:54 -08001606nFileA3DGetEntryByIndex(JNIEnv *_env, jobject _this, jlong con, jlong fileA3D, jint index)
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001607{
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001608 ALOGV("______nFileA3D %p", (RsFile) fileA3D);
Tim Murray3aa89c12014-08-18 17:51:22 -07001609 jlong id = (jlong)(uintptr_t)rsaFileA3DGetEntryByIndex((RsContext)con, (uint32_t)index, (RsFile)fileA3D);
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -07001610 return id;
1611}
Jason Samsd19f10d2009-05-22 14:03:28 -07001612
1613// -----------------------------------
1614
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001615static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001616nFontCreateFromFile(JNIEnv *_env, jobject _this, jlong con,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001617 jstring fileName, jfloat fontSize, jint dpi)
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001618{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001619 AutoJavaStringToUTF8 fileNameUTF(_env, fileName);
Tim Murray3aa89c12014-08-18 17:51:22 -07001620 jlong id = (jlong)(uintptr_t)rsFontCreateFromFile((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001621 fileNameUTF.c_str(), fileNameUTF.length(),
1622 fontSize, dpi);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001623
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001624 return id;
1625}
1626
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001627static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001628nFontCreateFromAssetStream(JNIEnv *_env, jobject _this, jlong con,
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001629 jstring name, jfloat fontSize, jint dpi, jlong native_asset)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001630{
1631 Asset* asset = reinterpret_cast<Asset*>(native_asset);
1632 AutoJavaStringToUTF8 nameUTF(_env, name);
1633
Tim Murray3aa89c12014-08-18 17:51:22 -07001634 jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001635 nameUTF.c_str(), nameUTF.length(),
1636 fontSize, dpi,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001637 asset->getBuffer(false), asset->getLength());
1638 return id;
1639}
1640
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001641static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001642nFontCreateFromAsset(JNIEnv *_env, jobject _this, jlong con, jobject _assetMgr, jstring _path,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001643 jfloat fontSize, jint dpi)
1644{
1645 AssetManager* mgr = assetManagerForJavaObject(_env, _assetMgr);
Chris Wailes488230c32014-08-14 11:22:40 -07001646 if (mgr == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001647 return 0;
1648 }
1649
1650 AutoJavaStringToUTF8 str(_env, _path);
1651 Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
Chris Wailes488230c32014-08-14 11:22:40 -07001652 if (asset == nullptr) {
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001653 return 0;
1654 }
1655
Tim Murray3aa89c12014-08-18 17:51:22 -07001656 jlong id = (jlong)(uintptr_t)rsFontCreateFromMemory((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001657 str.c_str(), str.length(),
1658 fontSize, dpi,
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08001659 asset->getBuffer(false), asset->getLength());
1660 delete asset;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001661 return id;
1662}
1663
Jason Samsbd1c3ad2009-08-03 16:03:08 -07001664// -----------------------------------
1665
1666static void
Tim Murray460a0492013-11-19 12:45:54 -08001667nScriptBindAllocation(JNIEnv *_env, jobject _this, jlong con, jlong script, jlong alloc, jint slot)
Jason Samsd19f10d2009-05-22 14:03:28 -07001668{
Andreas Gampe67333922014-11-10 20:35:59 -08001669 if (kLogApi) {
1670 ALOGD("nScriptBindAllocation, con(%p), script(%p), alloc(%p), slot(%i)", (RsContext)con,
1671 (RsScript)script, (RsAllocation)alloc, slot);
1672 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001673 rsScriptBindAllocation((RsContext)con, (RsScript)script, (RsAllocation)alloc, slot);
Jason Samsd19f10d2009-05-22 14:03:28 -07001674}
1675
1676static void
Tim Murray460a0492013-11-19 12:45:54 -08001677nScriptSetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jint val)
Jason Sams4d339932010-05-11 14:03:58 -07001678{
Andreas Gampe67333922014-11-10 20:35:59 -08001679 if (kLogApi) {
1680 ALOGD("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i)", (RsContext)con, (void *)script,
1681 slot, val);
1682 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001683 rsScriptSetVarI((RsContext)con, (RsScript)script, slot, val);
Jason Sams4d339932010-05-11 14:03:58 -07001684}
1685
Tim Murray7c4caad2013-04-10 16:21:40 -07001686static jint
Tim Murray460a0492013-11-19 12:45:54 -08001687nScriptGetVarI(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001688{
Andreas Gampe67333922014-11-10 20:35:59 -08001689 if (kLogApi) {
1690 ALOGD("nScriptGetVarI, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1691 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001692 int value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001693 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001694 return value;
1695}
1696
Jason Sams4d339932010-05-11 14:03:58 -07001697static void
Tim Murray460a0492013-11-19 12:45:54 -08001698nScriptSetVarObj(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
Jason Sams6f4cf0b2010-11-16 17:37:02 -08001699{
Andreas Gampe67333922014-11-10 20:35:59 -08001700 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001701 ALOGD("nScriptSetVarObj, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
Andreas Gampe67333922014-11-10 20:35:59 -08001702 slot, val);
1703 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001704 rsScriptSetVarObj((RsContext)con, (RsScript)script, slot, (RsObjectBase)val);
Jason Sams6f4cf0b2010-11-16 17:37:02 -08001705}
1706
1707static void
Tim Murray460a0492013-11-19 12:45:54 -08001708nScriptSetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jlong val)
Stephen Hines031ec58c2010-10-11 10:54:21 -07001709{
Andreas Gampe67333922014-11-10 20:35:59 -08001710 if (kLogApi) {
Bernhard Rosenkränzer09993f72014-11-17 20:25:28 +01001711 ALOGD("nScriptSetVarJ, con(%p), s(%p), slot(%i), val(%" PRId64 ")", (RsContext)con, (void *)script,
Andreas Gampe67333922014-11-10 20:35:59 -08001712 slot, val);
1713 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001714 rsScriptSetVarJ((RsContext)con, (RsScript)script, slot, val);
Stephen Hines031ec58c2010-10-11 10:54:21 -07001715}
1716
Tim Murray7c4caad2013-04-10 16:21:40 -07001717static jlong
Tim Murray460a0492013-11-19 12:45:54 -08001718nScriptGetVarJ(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001719{
Andreas Gampe67333922014-11-10 20:35:59 -08001720 if (kLogApi) {
1721 ALOGD("nScriptGetVarJ, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1722 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001723 jlong value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001724 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001725 return value;
1726}
1727
Stephen Hines031ec58c2010-10-11 10:54:21 -07001728static void
Tim Murray460a0492013-11-19 12:45:54 -08001729nScriptSetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, float val)
Jason Sams4d339932010-05-11 14:03:58 -07001730{
Andreas Gampe67333922014-11-10 20:35:59 -08001731 if (kLogApi) {
1732 ALOGD("nScriptSetVarF, con(%p), s(%p), slot(%i), val(%f)", (RsContext)con, (void *)script,
1733 slot, val);
1734 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001735 rsScriptSetVarF((RsContext)con, (RsScript)script, slot, val);
Jason Sams4d339932010-05-11 14:03:58 -07001736}
1737
Tim Murray7c4caad2013-04-10 16:21:40 -07001738static jfloat
Tim Murray460a0492013-11-19 12:45:54 -08001739nScriptGetVarF(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001740{
Andreas Gampe67333922014-11-10 20:35:59 -08001741 if (kLogApi) {
1742 ALOGD("nScriptGetVarF, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1743 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001744 jfloat value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001745 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001746 return value;
1747}
1748
Jason Sams4d339932010-05-11 14:03:58 -07001749static void
Tim Murray460a0492013-11-19 12:45:54 -08001750nScriptSetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, double val)
Stephen Hinesca54ec32010-09-20 17:20:30 -07001751{
Andreas Gampe67333922014-11-10 20:35:59 -08001752 if (kLogApi) {
1753 ALOGD("nScriptSetVarD, con(%p), s(%p), slot(%i), val(%lf)", (RsContext)con, (void *)script,
1754 slot, val);
1755 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001756 rsScriptSetVarD((RsContext)con, (RsScript)script, slot, val);
Stephen Hinesca54ec32010-09-20 17:20:30 -07001757}
1758
Tim Murray7c4caad2013-04-10 16:21:40 -07001759static jdouble
Tim Murray460a0492013-11-19 12:45:54 -08001760nScriptGetVarD(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot)
Tim Murray7c4caad2013-04-10 16:21:40 -07001761{
Andreas Gampe67333922014-11-10 20:35:59 -08001762 if (kLogApi) {
1763 ALOGD("nScriptGetVarD, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1764 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001765 jdouble value = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08001766 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, &value, sizeof(value));
Tim Murray7c4caad2013-04-10 16:21:40 -07001767 return value;
1768}
1769
Stephen Hinesca54ec32010-09-20 17:20:30 -07001770static void
Tim Murray460a0492013-11-19 12:45:54 -08001771nScriptSetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Jason Sams4d339932010-05-11 14:03:58 -07001772{
Andreas Gampe67333922014-11-10 20:35:59 -08001773 if (kLogApi) {
1774 ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1775 }
Jason Sams4d339932010-05-11 14:03:58 -07001776 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001777 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Tim Murrayeff663f2013-11-15 13:08:30 -08001778 rsScriptSetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
Jason Sams4d339932010-05-11 14:03:58 -07001779 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1780}
1781
Stephen Hinesadeb8092012-04-20 14:26:06 -07001782static void
Tim Murray460a0492013-11-19 12:45:54 -08001783nScriptGetVarV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Tim Murray7c4caad2013-04-10 16:21:40 -07001784{
Andreas Gampe67333922014-11-10 20:35:59 -08001785 if (kLogApi) {
1786 ALOGD("nScriptSetVarV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1787 }
Tim Murray7c4caad2013-04-10 16:21:40 -07001788 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001789 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Tim Murrayeff663f2013-11-15 13:08:30 -08001790 rsScriptGetVarV((RsContext)con, (RsScript)script, slot, ptr, len);
Stephen Hines414fa2c2014-04-17 01:02:42 -07001791 _env->ReleaseByteArrayElements(data, ptr, 0);
Tim Murray7c4caad2013-04-10 16:21:40 -07001792}
1793
1794static void
Andreas Gampe67333922014-11-10 20:35:59 -08001795nScriptSetVarVE(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data,
1796 jlong elem, jintArray dims)
Stephen Hinesadeb8092012-04-20 14:26:06 -07001797{
Andreas Gampe67333922014-11-10 20:35:59 -08001798 if (kLogApi) {
1799 ALOGD("nScriptSetVarVE, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1800 }
Stephen Hinesadeb8092012-04-20 14:26:06 -07001801 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001802 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Stephen Hinesadeb8092012-04-20 14:26:06 -07001803 jint dimsLen = _env->GetArrayLength(dims) * sizeof(int);
Chris Wailes488230c32014-08-14 11:22:40 -07001804 jint *dimsPtr = _env->GetIntArrayElements(dims, nullptr);
Tim Murrayeff663f2013-11-15 13:08:30 -08001805 rsScriptSetVarVE((RsContext)con, (RsScript)script, slot, ptr, len, (RsElement)elem,
Stephen Hinesbc5d3ee2014-06-25 00:03:39 -07001806 (const uint32_t*) dimsPtr, dimsLen);
Stephen Hinesadeb8092012-04-20 14:26:06 -07001807 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1808 _env->ReleaseIntArrayElements(dims, dimsPtr, JNI_ABORT);
1809}
1810
Jason Samsd19f10d2009-05-22 14:03:28 -07001811
1812static void
Tim Murray460a0492013-11-19 12:45:54 -08001813nScriptSetTimeZone(JNIEnv *_env, jobject _this, jlong con, jlong script, jbyteArray timeZone)
Jason Samsd19f10d2009-05-22 14:03:28 -07001814{
Andreas Gampe67333922014-11-10 20:35:59 -08001815 if (kLogApi) {
1816 ALOGD("nScriptCSetTimeZone, con(%p), s(%p)", (RsContext)con, (void *)script);
1817 }
Romain Guy584a3752009-07-30 18:45:01 -07001818
1819 jint length = _env->GetArrayLength(timeZone);
1820 jbyte* timeZone_ptr;
1821 timeZone_ptr = (jbyte *) _env->GetPrimitiveArrayCritical(timeZone, (jboolean *)0);
1822
Tim Murrayeff663f2013-11-15 13:08:30 -08001823 rsScriptSetTimeZone((RsContext)con, (RsScript)script, (const char *)timeZone_ptr, length);
Romain Guy584a3752009-07-30 18:45:01 -07001824
1825 if (timeZone_ptr) {
1826 _env->ReleasePrimitiveArrayCritical(timeZone, timeZone_ptr, 0);
1827 }
1828}
1829
Jason Samsfbf0b9e2009-08-13 12:59:04 -07001830static void
Tim Murray460a0492013-11-19 12:45:54 -08001831nScriptInvoke(JNIEnv *_env, jobject _this, jlong con, jlong obj, jint slot)
Jason Samsbe2e8412009-09-16 15:04:38 -07001832{
Andreas Gampe67333922014-11-10 20:35:59 -08001833 if (kLogApi) {
1834 ALOGD("nScriptInvoke, con(%p), script(%p)", (RsContext)con, (void *)obj);
1835 }
Tim Murrayeff663f2013-11-15 13:08:30 -08001836 rsScriptInvoke((RsContext)con, (RsScript)obj, slot);
Jason Samsbe2e8412009-09-16 15:04:38 -07001837}
1838
1839static void
Tim Murray460a0492013-11-19 12:45:54 -08001840nScriptInvokeV(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot, jbyteArray data)
Jason Sams4d339932010-05-11 14:03:58 -07001841{
Andreas Gampe67333922014-11-10 20:35:59 -08001842 if (kLogApi) {
1843 ALOGD("nScriptInvokeV, con(%p), s(%p), slot(%i)", (RsContext)con, (void *)script, slot);
1844 }
Jason Sams4d339932010-05-11 14:03:58 -07001845 jint len = _env->GetArrayLength(data);
Chris Wailes488230c32014-08-14 11:22:40 -07001846 jbyte *ptr = _env->GetByteArrayElements(data, nullptr);
Tim Murrayeff663f2013-11-15 13:08:30 -08001847 rsScriptInvokeV((RsContext)con, (RsScript)script, slot, ptr, len);
Jason Sams4d339932010-05-11 14:03:58 -07001848 _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
1849}
1850
Jason Sams6e494d32011-04-27 16:33:11 -07001851static void
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001852nScriptForEach(JNIEnv *_env, jobject _this, jlong con, jlong script, jint slot,
1853 jlongArray ains, jlong aout, jbyteArray params,
1854 jintArray limits)
Jason Sams6e494d32011-04-27 16:33:11 -07001855{
Andreas Gampe67333922014-11-10 20:35:59 -08001856 if (kLogApi) {
Chih-Hung Hsieh9eb9dd32015-05-06 14:42:04 -07001857 ALOGD("nScriptForEach, con(%p), s(%p), slot(%i) ains(%p) aout(%" PRId64 ")", (RsContext)con, (void *)script, slot, ains, aout);
Andreas Gampe67333922014-11-10 20:35:59 -08001858 }
Jason Sams6e494d32011-04-27 16:33:11 -07001859
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001860 jint in_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07001861 jlong *in_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07001862
Chris Wailes488230c32014-08-14 11:22:40 -07001863 RsAllocation *in_allocs = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07001864
Chris Wailes488230c32014-08-14 11:22:40 -07001865 if (ains != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001866 in_len = _env->GetArrayLength(ains);
Yang Ni9310e3d2015-05-05 12:41:19 -07001867 if (in_len > (jint)RS_KERNEL_MAX_ARGUMENTS) {
Yang Ni17c2d7a2015-04-30 16:13:54 -07001868 ALOGE("Too many arguments in kernel launch.");
1869 // TODO (b/20758983): Report back to Java and throw an exception
1870 return;
1871 }
Chris Wailes94961062014-06-11 12:01:28 -07001872
Yang Ni17c2d7a2015-04-30 16:13:54 -07001873 // TODO (b/20760800): Check in_ptr is not null
1874 in_ptr = _env->GetLongArrayElements(ains, nullptr);
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001875 if (sizeof(RsAllocation) == sizeof(jlong)) {
1876 in_allocs = (RsAllocation*)in_ptr;
Chris Wailes94961062014-06-11 12:01:28 -07001877
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001878 } else {
1879 // Convert from 64-bit jlong types to the native pointer type.
Chris Wailes94961062014-06-11 12:01:28 -07001880
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001881 in_allocs = (RsAllocation*)alloca(in_len * sizeof(RsAllocation));
Yang Ni17c2d7a2015-04-30 16:13:54 -07001882 if (in_allocs == nullptr) {
1883 ALOGE("Failed launching kernel for lack of memory.");
1884 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
1885 return;
1886 }
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001887
1888 for (int index = in_len; --index >= 0;) {
1889 in_allocs[index] = (RsAllocation)in_ptr[index];
1890 }
1891 }
Chris Wailes94961062014-06-11 12:01:28 -07001892 }
1893
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001894 jint param_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07001895 jbyte *param_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07001896
Chris Wailes488230c32014-08-14 11:22:40 -07001897 if (params != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001898 param_len = _env->GetArrayLength(params);
Chris Wailes488230c32014-08-14 11:22:40 -07001899 param_ptr = _env->GetByteArrayElements(params, nullptr);
Chris Wailes94961062014-06-11 12:01:28 -07001900 }
1901
Chris Wailes488230c32014-08-14 11:22:40 -07001902 RsScriptCall sc, *sca = nullptr;
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001903 uint32_t sc_size = 0;
Chris Wailes94961062014-06-11 12:01:28 -07001904
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001905 jint limit_len = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07001906 jint *limit_ptr = nullptr;
Chris Wailes94961062014-06-11 12:01:28 -07001907
Chris Wailes488230c32014-08-14 11:22:40 -07001908 if (limits != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001909 limit_len = _env->GetArrayLength(limits);
Chris Wailes488230c32014-08-14 11:22:40 -07001910 limit_ptr = _env->GetIntArrayElements(limits, nullptr);
Chris Wailes94961062014-06-11 12:01:28 -07001911
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001912 assert(limit_len == 6);
Andreas Gampe67333922014-11-10 20:35:59 -08001913 UNUSED(limit_len); // As the assert might not be compiled.
Chris Wailes94961062014-06-11 12:01:28 -07001914
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001915 sc.xStart = limit_ptr[0];
1916 sc.xEnd = limit_ptr[1];
1917 sc.yStart = limit_ptr[2];
1918 sc.yEnd = limit_ptr[3];
1919 sc.zStart = limit_ptr[4];
1920 sc.zEnd = limit_ptr[5];
1921 sc.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
Jason Sams14331ab2015-01-26 18:14:36 -08001922 sc.arrayStart = 0;
1923 sc.arrayEnd = 0;
1924 sc.array2Start = 0;
1925 sc.array2End = 0;
1926 sc.array3Start = 0;
1927 sc.array3End = 0;
1928 sc.array4Start = 0;
1929 sc.array4End = 0;
Chris Wailes94961062014-06-11 12:01:28 -07001930
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001931 sca = &sc;
Chris Wailes94961062014-06-11 12:01:28 -07001932 }
1933
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001934 rsScriptForEachMulti((RsContext)con, (RsScript)script, slot,
1935 in_allocs, in_len, (RsAllocation)aout,
1936 param_ptr, param_len, sca, sc_size);
Chris Wailes94961062014-06-11 12:01:28 -07001937
Chris Wailes488230c32014-08-14 11:22:40 -07001938 if (ains != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001939 _env->ReleaseLongArrayElements(ains, in_ptr, JNI_ABORT);
Chris Wailes94961062014-06-11 12:01:28 -07001940 }
1941
Chris Wailes488230c32014-08-14 11:22:40 -07001942 if (params != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001943 _env->ReleaseByteArrayElements(params, param_ptr, JNI_ABORT);
1944 }
1945
Chris Wailes488230c32014-08-14 11:22:40 -07001946 if (limits != nullptr) {
Chris Wailesbe7b1de2014-07-15 10:56:14 -07001947 _env->ReleaseIntArrayElements(limits, limit_ptr, JNI_ABORT);
1948 }
Chris Wailes94961062014-06-11 12:01:28 -07001949}
1950
Jason Sams22534172009-08-04 16:58:20 -07001951// -----------------------------------
1952
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001953static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08001954nScriptCCreate(JNIEnv *_env, jobject _this, jlong con,
Jason Samse4a06c52011-03-16 16:29:28 -07001955 jstring resName, jstring cacheDir,
1956 jbyteArray scriptRef, jint length)
Jason Sams22534172009-08-04 16:58:20 -07001957{
Andreas Gampe67333922014-11-10 20:35:59 -08001958 if (kLogApi) {
1959 ALOGD("nScriptCCreate, con(%p)", (RsContext)con);
1960 }
Jason Sams22534172009-08-04 16:58:20 -07001961
Jason Samse4a06c52011-03-16 16:29:28 -07001962 AutoJavaStringToUTF8 resNameUTF(_env, resName);
1963 AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
Ashok Bhat0e0c0882014-02-04 14:57:58 +00001964 jlong ret = 0;
Chris Wailes488230c32014-08-14 11:22:40 -07001965 jbyte* script_ptr = nullptr;
Jack Palevich43702d82009-05-28 13:38:16 -07001966 jint _exception = 0;
1967 jint remaining;
Jack Palevich43702d82009-05-28 13:38:16 -07001968 if (!scriptRef) {
1969 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07001970 //jniThrowException(_env, "java/lang/IllegalArgumentException", "script == null");
Jack Palevich43702d82009-05-28 13:38:16 -07001971 goto exit;
1972 }
Jack Palevich43702d82009-05-28 13:38:16 -07001973 if (length < 0) {
1974 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07001975 //jniThrowException(_env, "java/lang/IllegalArgumentException", "length < 0");
Jack Palevich43702d82009-05-28 13:38:16 -07001976 goto exit;
1977 }
Jason Samse4a06c52011-03-16 16:29:28 -07001978 remaining = _env->GetArrayLength(scriptRef);
Jack Palevich43702d82009-05-28 13:38:16 -07001979 if (remaining < length) {
1980 _exception = 1;
Elliott Hughes8451b252011-04-07 19:17:57 -07001981 //jniThrowException(_env, "java/lang/IllegalArgumentException",
1982 // "length > script.length - offset");
Jack Palevich43702d82009-05-28 13:38:16 -07001983 goto exit;
1984 }
Jason Samse4a06c52011-03-16 16:29:28 -07001985 script_ptr = (jbyte *)
Jack Palevich43702d82009-05-28 13:38:16 -07001986 _env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
Jack Palevich43702d82009-05-28 13:38:16 -07001987
Tim Murrayeff663f2013-11-15 13:08:30 -08001988 //rsScriptCSetText((RsContext)con, (const char *)script_ptr, length);
Jason Samse4a06c52011-03-16 16:29:28 -07001989
Tim Murray3aa89c12014-08-18 17:51:22 -07001990 ret = (jlong)(uintptr_t)rsScriptCCreate((RsContext)con,
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -07001991 resNameUTF.c_str(), resNameUTF.length(),
1992 cacheDirUTF.c_str(), cacheDirUTF.length(),
Jason Samse4a06c52011-03-16 16:29:28 -07001993 (const char *)script_ptr, length);
Jason Sams39ddc9502009-06-05 17:35:09 -07001994
Jack Palevich43702d82009-05-28 13:38:16 -07001995exit:
Jason Samse4a06c52011-03-16 16:29:28 -07001996 if (script_ptr) {
1997 _env->ReleasePrimitiveArrayCritical(scriptRef, script_ptr,
Jack Palevich43702d82009-05-28 13:38:16 -07001998 _exception ? JNI_ABORT: 0);
1999 }
Jason Samsd19f10d2009-05-22 14:03:28 -07002000
Tim Murray3aa89c12014-08-18 17:51:22 -07002001 return (jlong)(uintptr_t)ret;
Jason Samsd19f10d2009-05-22 14:03:28 -07002002}
2003
Tim Murray460a0492013-11-19 12:45:54 -08002004static jlong
2005nScriptIntrinsicCreate(JNIEnv *_env, jobject _this, jlong con, jint id, jlong eid)
Jason Sams6ab97682012-08-10 12:09:43 -07002006{
Andreas Gampe67333922014-11-10 20:35:59 -08002007 if (kLogApi) {
2008 ALOGD("nScriptIntrinsicCreate, con(%p) id(%i) element(%p)", (RsContext)con, id,
2009 (void *)eid);
2010 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002011 return (jlong)(uintptr_t)rsScriptIntrinsicCreate((RsContext)con, id, (RsElement)eid);
Jason Sams6ab97682012-08-10 12:09:43 -07002012}
2013
Tim Murray460a0492013-11-19 12:45:54 -08002014static jlong
2015nScriptKernelIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot, jint sig)
Jason Sams08a81582012-09-18 12:32:10 -07002016{
Andreas Gampe67333922014-11-10 20:35:59 -08002017 if (kLogApi) {
2018 ALOGD("nScriptKernelIDCreate, con(%p) script(%p), slot(%i), sig(%i)", (RsContext)con,
2019 (void *)sid, slot, sig);
2020 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002021 return (jlong)(uintptr_t)rsScriptKernelIDCreate((RsContext)con, (RsScript)sid, slot, sig);
Jason Sams08a81582012-09-18 12:32:10 -07002022}
2023
Tim Murray460a0492013-11-19 12:45:54 -08002024static jlong
Yang Nibe392ad2015-01-23 17:16:02 -08002025nScriptInvokeIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
2026{
2027 if (kLogApi) {
Elliott Hughes7ff53fa2015-02-05 21:36:10 -08002028 ALOGD("nScriptInvokeIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con,
Yang Nibe392ad2015-01-23 17:16:02 -08002029 (void *)sid, slot);
2030 }
2031 return (jlong)(uintptr_t)rsScriptInvokeIDCreate((RsContext)con, (RsScript)sid, slot);
2032}
2033
2034static jlong
Tim Murray460a0492013-11-19 12:45:54 -08002035nScriptFieldIDCreate(JNIEnv *_env, jobject _this, jlong con, jlong sid, jint slot)
Jason Sams08a81582012-09-18 12:32:10 -07002036{
Andreas Gampe67333922014-11-10 20:35:59 -08002037 if (kLogApi) {
2038 ALOGD("nScriptFieldIDCreate, con(%p) script(%p), slot(%i)", (RsContext)con, (void *)sid,
2039 slot);
2040 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002041 return (jlong)(uintptr_t)rsScriptFieldIDCreate((RsContext)con, (RsScript)sid, slot);
Jason Sams08a81582012-09-18 12:32:10 -07002042}
2043
Tim Murray460a0492013-11-19 12:45:54 -08002044static jlong
Ashok Bhat98071552014-02-12 09:54:43 +00002045nScriptGroupCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _kernels, jlongArray _src,
2046 jlongArray _dstk, jlongArray _dstf, jlongArray _types)
Jason Sams08a81582012-09-18 12:32:10 -07002047{
Andreas Gampe67333922014-11-10 20:35:59 -08002048 if (kLogApi) {
2049 ALOGD("nScriptGroupCreate, con(%p)", (RsContext)con);
2050 }
Jason Sams08a81582012-09-18 12:32:10 -07002051
Ashok Bhat98071552014-02-12 09:54:43 +00002052 jint kernelsLen = _env->GetArrayLength(_kernels);
Chris Wailes488230c32014-08-14 11:22:40 -07002053 jlong *jKernelsPtr = _env->GetLongArrayElements(_kernels, nullptr);
Ashok Bhat98071552014-02-12 09:54:43 +00002054 RsScriptKernelID* kernelsPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * kernelsLen);
2055 for(int i = 0; i < kernelsLen; ++i) {
2056 kernelsPtr[i] = (RsScriptKernelID)jKernelsPtr[i];
2057 }
Jason Sams08a81582012-09-18 12:32:10 -07002058
Ashok Bhat98071552014-02-12 09:54:43 +00002059 jint srcLen = _env->GetArrayLength(_src);
Chris Wailes488230c32014-08-14 11:22:40 -07002060 jlong *jSrcPtr = _env->GetLongArrayElements(_src, nullptr);
Ashok Bhat98071552014-02-12 09:54:43 +00002061 RsScriptKernelID* srcPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * srcLen);
2062 for(int i = 0; i < srcLen; ++i) {
2063 srcPtr[i] = (RsScriptKernelID)jSrcPtr[i];
2064 }
Jason Sams08a81582012-09-18 12:32:10 -07002065
Ashok Bhat98071552014-02-12 09:54:43 +00002066 jint dstkLen = _env->GetArrayLength(_dstk);
Chris Wailes488230c32014-08-14 11:22:40 -07002067 jlong *jDstkPtr = _env->GetLongArrayElements(_dstk, nullptr);
Ashok Bhat98071552014-02-12 09:54:43 +00002068 RsScriptKernelID* dstkPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstkLen);
2069 for(int i = 0; i < dstkLen; ++i) {
2070 dstkPtr[i] = (RsScriptKernelID)jDstkPtr[i];
2071 }
2072
2073 jint dstfLen = _env->GetArrayLength(_dstf);
Chris Wailes488230c32014-08-14 11:22:40 -07002074 jlong *jDstfPtr = _env->GetLongArrayElements(_dstf, nullptr);
Ashok Bhat98071552014-02-12 09:54:43 +00002075 RsScriptKernelID* dstfPtr = (RsScriptKernelID*) malloc(sizeof(RsScriptKernelID) * dstfLen);
2076 for(int i = 0; i < dstfLen; ++i) {
2077 dstfPtr[i] = (RsScriptKernelID)jDstfPtr[i];
2078 }
2079
2080 jint typesLen = _env->GetArrayLength(_types);
Chris Wailes488230c32014-08-14 11:22:40 -07002081 jlong *jTypesPtr = _env->GetLongArrayElements(_types, nullptr);
Ashok Bhat98071552014-02-12 09:54:43 +00002082 RsType* typesPtr = (RsType*) malloc(sizeof(RsType) * typesLen);
2083 for(int i = 0; i < typesLen; ++i) {
2084 typesPtr[i] = (RsType)jTypesPtr[i];
2085 }
2086
Tim Murray3aa89c12014-08-18 17:51:22 -07002087 jlong id = (jlong)(uintptr_t)rsScriptGroupCreate((RsContext)con,
Ashok Bhat98071552014-02-12 09:54:43 +00002088 (RsScriptKernelID *)kernelsPtr, kernelsLen * sizeof(RsScriptKernelID),
2089 (RsScriptKernelID *)srcPtr, srcLen * sizeof(RsScriptKernelID),
2090 (RsScriptKernelID *)dstkPtr, dstkLen * sizeof(RsScriptKernelID),
2091 (RsScriptFieldID *)dstfPtr, dstfLen * sizeof(RsScriptKernelID),
2092 (RsType *)typesPtr, typesLen * sizeof(RsType));
2093
2094 free(kernelsPtr);
2095 free(srcPtr);
2096 free(dstkPtr);
2097 free(dstfPtr);
2098 free(typesPtr);
2099 _env->ReleaseLongArrayElements(_kernels, jKernelsPtr, 0);
2100 _env->ReleaseLongArrayElements(_src, jSrcPtr, 0);
2101 _env->ReleaseLongArrayElements(_dstk, jDstkPtr, 0);
2102 _env->ReleaseLongArrayElements(_dstf, jDstfPtr, 0);
2103 _env->ReleaseLongArrayElements(_types, jTypesPtr, 0);
Jason Sams08a81582012-09-18 12:32:10 -07002104 return id;
2105}
2106
2107static void
Tim Murray460a0492013-11-19 12:45:54 -08002108nScriptGroupSetInput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
Jason Sams08a81582012-09-18 12:32:10 -07002109{
Andreas Gampe67333922014-11-10 20:35:59 -08002110 if (kLogApi) {
2111 ALOGD("nScriptGroupSetInput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
2112 (void *)gid, (void *)kid, (void *)alloc);
2113 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002114 rsScriptGroupSetInput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
Jason Sams08a81582012-09-18 12:32:10 -07002115}
2116
2117static void
Tim Murray460a0492013-11-19 12:45:54 -08002118nScriptGroupSetOutput(JNIEnv *_env, jobject _this, jlong con, jlong gid, jlong kid, jlong alloc)
Jason Sams08a81582012-09-18 12:32:10 -07002119{
Andreas Gampe67333922014-11-10 20:35:59 -08002120 if (kLogApi) {
2121 ALOGD("nScriptGroupSetOutput, con(%p) group(%p), kernelId(%p), alloc(%p)", (RsContext)con,
2122 (void *)gid, (void *)kid, (void *)alloc);
2123 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002124 rsScriptGroupSetOutput((RsContext)con, (RsScriptGroup)gid, (RsScriptKernelID)kid, (RsAllocation)alloc);
Jason Sams08a81582012-09-18 12:32:10 -07002125}
2126
2127static void
Tim Murray460a0492013-11-19 12:45:54 -08002128nScriptGroupExecute(JNIEnv *_env, jobject _this, jlong con, jlong gid)
Jason Sams08a81582012-09-18 12:32:10 -07002129{
Andreas Gampe67333922014-11-10 20:35:59 -08002130 if (kLogApi) {
2131 ALOGD("nScriptGroupSetOutput, con(%p) group(%p)", (RsContext)con, (void *)gid);
2132 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002133 rsScriptGroupExecute((RsContext)con, (RsScriptGroup)gid);
Jason Sams08a81582012-09-18 12:32:10 -07002134}
2135
Jason Samsd19f10d2009-05-22 14:03:28 -07002136// ---------------------------------------------------------------------------
2137
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002138static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002139nProgramStoreCreate(JNIEnv *_env, jobject _this, jlong con,
Jason Sams331bf9b2011-04-06 11:23:54 -07002140 jboolean colorMaskR, jboolean colorMaskG, jboolean colorMaskB, jboolean colorMaskA,
2141 jboolean depthMask, jboolean ditherEnable,
2142 jint srcFunc, jint destFunc,
2143 jint depthFunc)
Jason Samsd19f10d2009-05-22 14:03:28 -07002144{
Andreas Gampe67333922014-11-10 20:35:59 -08002145 if (kLogApi) {
2146 ALOGD("nProgramStoreCreate, con(%p)", (RsContext)con);
2147 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002148 return (jlong)(uintptr_t)rsProgramStoreCreate((RsContext)con, colorMaskR, colorMaskG, colorMaskB, colorMaskA,
Jason Sams331bf9b2011-04-06 11:23:54 -07002149 depthMask, ditherEnable, (RsBlendSrcFunc)srcFunc,
2150 (RsBlendDstFunc)destFunc, (RsDepthFunc)depthFunc);
Jason Samsd19f10d2009-05-22 14:03:28 -07002151}
2152
Jason Sams0011bcf2009-12-15 12:58:36 -08002153// ---------------------------------------------------------------------------
2154
2155static void
Tim Murray460a0492013-11-19 12:45:54 -08002156nProgramBindConstants(JNIEnv *_env, jobject _this, jlong con, jlong vpv, jint slot, jlong a)
Jason Sams0011bcf2009-12-15 12:58:36 -08002157{
Andreas Gampe67333922014-11-10 20:35:59 -08002158 if (kLogApi) {
2159 ALOGD("nProgramBindConstants, con(%p), vpf(%p), sloat(%i), a(%p)", (RsContext)con,
2160 (RsProgramVertex)vpv, slot, (RsAllocation)a);
2161 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002162 rsProgramBindConstants((RsContext)con, (RsProgram)vpv, slot, (RsAllocation)a);
Jason Sams0011bcf2009-12-15 12:58:36 -08002163}
Jason Sams54c0ec12009-11-30 14:49:55 -08002164
Jason Sams68afd012009-12-17 16:55:08 -08002165static void
Tim Murray460a0492013-11-19 12:45:54 -08002166nProgramBindTexture(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
Jason Sams68afd012009-12-17 16:55:08 -08002167{
Andreas Gampe67333922014-11-10 20:35:59 -08002168 if (kLogApi) {
2169 ALOGD("nProgramBindTexture, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
2170 (RsProgramFragment)vpf, slot, (RsAllocation)a);
2171 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002172 rsProgramBindTexture((RsContext)con, (RsProgramFragment)vpf, slot, (RsAllocation)a);
Jason Sams68afd012009-12-17 16:55:08 -08002173}
2174
2175static void
Tim Murray460a0492013-11-19 12:45:54 -08002176nProgramBindSampler(JNIEnv *_env, jobject _this, jlong con, jlong vpf, jint slot, jlong a)
Jason Sams68afd012009-12-17 16:55:08 -08002177{
Andreas Gampe67333922014-11-10 20:35:59 -08002178 if (kLogApi) {
2179 ALOGD("nProgramBindSampler, con(%p), vpf(%p), slot(%i), a(%p)", (RsContext)con,
2180 (RsProgramFragment)vpf, slot, (RsSampler)a);
2181 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002182 rsProgramBindSampler((RsContext)con, (RsProgramFragment)vpf, slot, (RsSampler)a);
Jason Sams68afd012009-12-17 16:55:08 -08002183}
2184
Jason Samsd19f10d2009-05-22 14:03:28 -07002185// ---------------------------------------------------------------------------
2186
Tim Murray460a0492013-11-19 12:45:54 -08002187static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002188nProgramFragmentCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
Ashok Bhat98071552014-02-12 09:54:43 +00002189 jobjectArray texNames, jlongArray params)
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002190{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08002191 AutoJavaStringToUTF8 shaderUTF(_env, shader);
Chris Wailes488230c32014-08-14 11:22:40 -07002192 jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002193 jint paramLen = _env->GetArrayLength(params);
2194
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002195 int texCount = _env->GetArrayLength(texNames);
2196 AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
2197 const char ** nameArray = names.c_str();
2198 size_t* sizeArray = names.c_str_len();
2199
Andreas Gampe67333922014-11-10 20:35:59 -08002200 if (kLogApi) {
2201 ALOGD("nProgramFragmentCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
2202 }
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002203
Ashok Bhat98071552014-02-12 09:54:43 +00002204 uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
2205 for(int i = 0; i < paramLen; ++i) {
2206 paramPtr[i] = (uintptr_t)jParamPtr[i];
2207 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002208 jlong ret = (jlong)(uintptr_t)rsProgramFragmentCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002209 nameArray, texCount, sizeArray,
Ashok Bhat98071552014-02-12 09:54:43 +00002210 paramPtr, paramLen);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002211
Ashok Bhat98071552014-02-12 09:54:43 +00002212 free(paramPtr);
2213 _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
Jason Sams7e5ab3b2009-12-15 13:27:04 -08002214 return ret;
2215}
2216
2217
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002218// ---------------------------------------------------------------------------
2219
Tim Murray460a0492013-11-19 12:45:54 -08002220static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002221nProgramVertexCreate(JNIEnv *_env, jobject _this, jlong con, jstring shader,
Ashok Bhat98071552014-02-12 09:54:43 +00002222 jobjectArray texNames, jlongArray params)
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002223{
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -08002224 AutoJavaStringToUTF8 shaderUTF(_env, shader);
Chris Wailes488230c32014-08-14 11:22:40 -07002225 jlong *jParamPtr = _env->GetLongArrayElements(params, nullptr);
Jason Sams0011bcf2009-12-15 12:58:36 -08002226 jint paramLen = _env->GetArrayLength(params);
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002227
Andreas Gampe67333922014-11-10 20:35:59 -08002228 if (kLogApi) {
2229 ALOGD("nProgramVertexCreate, con(%p), paramLen(%i)", (RsContext)con, paramLen);
2230 }
Jason Sams0011bcf2009-12-15 12:58:36 -08002231
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002232 int texCount = _env->GetArrayLength(texNames);
2233 AutoJavaStringArrayToUTF8 names(_env, texNames, texCount);
2234 const char ** nameArray = names.c_str();
2235 size_t* sizeArray = names.c_str_len();
2236
Ashok Bhat98071552014-02-12 09:54:43 +00002237 uintptr_t * paramPtr = (uintptr_t*) malloc(sizeof(uintptr_t) * paramLen);
2238 for(int i = 0; i < paramLen; ++i) {
2239 paramPtr[i] = (uintptr_t)jParamPtr[i];
2240 }
2241
Tim Murray3aa89c12014-08-18 17:51:22 -07002242 jlong ret = (jlong)(uintptr_t)rsProgramVertexCreate((RsContext)con, shaderUTF.c_str(), shaderUTF.length(),
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002243 nameArray, texCount, sizeArray,
Ashok Bhat98071552014-02-12 09:54:43 +00002244 paramPtr, paramLen);
Alex Sakhartchouk2123b462012-02-15 16:21:46 -08002245
Ashok Bhat98071552014-02-12 09:54:43 +00002246 free(paramPtr);
2247 _env->ReleaseLongArrayElements(params, jParamPtr, JNI_ABORT);
Jason Sams0011bcf2009-12-15 12:58:36 -08002248 return ret;
2249}
Jason Sams1fe9b8c2009-06-11 14:46:10 -07002250
Jason Samsebfb4362009-09-23 13:57:02 -07002251// ---------------------------------------------------------------------------
2252
Tim Murray460a0492013-11-19 12:45:54 -08002253static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002254nProgramRasterCreate(JNIEnv *_env, jobject _this, jlong con, jboolean pointSprite, jint cull)
Jason Samsebfb4362009-09-23 13:57:02 -07002255{
Andreas Gampe67333922014-11-10 20:35:59 -08002256 if (kLogApi) {
2257 ALOGD("nProgramRasterCreate, con(%p), pointSprite(%i), cull(%i)", (RsContext)con,
2258 pointSprite, cull);
2259 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002260 return (jlong)(uintptr_t)rsProgramRasterCreate((RsContext)con, pointSprite, (RsCullMode)cull);
Jason Samsebfb4362009-09-23 13:57:02 -07002261}
2262
Jason Samsd19f10d2009-05-22 14:03:28 -07002263
2264// ---------------------------------------------------------------------------
2265
2266static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002267nContextBindRootScript(JNIEnv *_env, jobject _this, jlong con, jlong script)
Jason Samsd19f10d2009-05-22 14:03:28 -07002268{
Andreas Gampe67333922014-11-10 20:35:59 -08002269 if (kLogApi) {
2270 ALOGD("nContextBindRootScript, con(%p), script(%p)", (RsContext)con, (RsScript)script);
2271 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002272 rsContextBindRootScript((RsContext)con, (RsScript)script);
Jason Samsd19f10d2009-05-22 14:03:28 -07002273}
2274
2275static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002276nContextBindProgramStore(JNIEnv *_env, jobject _this, jlong con, jlong pfs)
Jason Samsd19f10d2009-05-22 14:03:28 -07002277{
Andreas Gampe67333922014-11-10 20:35:59 -08002278 if (kLogApi) {
2279 ALOGD("nContextBindProgramStore, con(%p), pfs(%p)", (RsContext)con, (RsProgramStore)pfs);
2280 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002281 rsContextBindProgramStore((RsContext)con, (RsProgramStore)pfs);
Jason Samsd19f10d2009-05-22 14:03:28 -07002282}
2283
2284static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002285nContextBindProgramFragment(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Samsd19f10d2009-05-22 14:03:28 -07002286{
Andreas Gampe67333922014-11-10 20:35:59 -08002287 if (kLogApi) {
2288 ALOGD("nContextBindProgramFragment, con(%p), pf(%p)", (RsContext)con,
2289 (RsProgramFragment)pf);
2290 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002291 rsContextBindProgramFragment((RsContext)con, (RsProgramFragment)pf);
Jason Samsd19f10d2009-05-22 14:03:28 -07002292}
2293
Jason Sams0826a6f2009-06-15 19:04:56 -07002294static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002295nContextBindProgramVertex(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Sams0826a6f2009-06-15 19:04:56 -07002296{
Andreas Gampe67333922014-11-10 20:35:59 -08002297 if (kLogApi) {
2298 ALOGD("nContextBindProgramVertex, con(%p), pf(%p)", (RsContext)con, (RsProgramVertex)pf);
2299 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002300 rsContextBindProgramVertex((RsContext)con, (RsProgramVertex)pf);
Jason Sams0826a6f2009-06-15 19:04:56 -07002301}
2302
Joe Onoratod7b37742009-08-09 22:57:44 -07002303static void
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002304nContextBindProgramRaster(JNIEnv *_env, jobject _this, jlong con, jlong pf)
Jason Samsebfb4362009-09-23 13:57:02 -07002305{
Andreas Gampe67333922014-11-10 20:35:59 -08002306 if (kLogApi) {
2307 ALOGD("nContextBindProgramRaster, con(%p), pf(%p)", (RsContext)con, (RsProgramRaster)pf);
2308 }
Tim Murrayeff663f2013-11-15 13:08:30 -08002309 rsContextBindProgramRaster((RsContext)con, (RsProgramRaster)pf);
Jason Samsebfb4362009-09-23 13:57:02 -07002310}
2311
Joe Onoratod7b37742009-08-09 22:57:44 -07002312
Jason Sams02fb2cb2009-05-28 15:37:57 -07002313// ---------------------------------------------------------------------------
2314
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002315static jlong
Tim Murrayeff663f2013-11-15 13:08:30 -08002316nSamplerCreate(JNIEnv *_env, jobject _this, jlong con, jint magFilter, jint minFilter,
Alex Sakhartchouka89094a2011-05-04 17:45:36 -07002317 jint wrapS, jint wrapT, jint wrapR, jfloat aniso)
Jason Sams02fb2cb2009-05-28 15:37:57 -07002318{
Andreas Gampe67333922014-11-10 20:35:59 -08002319 if (kLogApi) {
2320 ALOGD("nSamplerCreate, con(%p)", (RsContext)con);
2321 }
Tim Murray3aa89c12014-08-18 17:51:22 -07002322 return (jlong)(uintptr_t)rsSamplerCreate((RsContext)con,
Alex Sakhartchouka89094a2011-05-04 17:45:36 -07002323 (RsSamplerValue)magFilter,
2324 (RsSamplerValue)minFilter,
2325 (RsSamplerValue)wrapS,
2326 (RsSamplerValue)wrapT,
2327 (RsSamplerValue)wrapR,
2328 aniso);
Jason Sams02fb2cb2009-05-28 15:37:57 -07002329}
2330
Jason Samsbba134c2009-06-22 15:49:21 -07002331// ---------------------------------------------------------------------------
2332
Tim Murray460a0492013-11-19 12:45:54 -08002333static jlong
Ashok Bhat98071552014-02-12 09:54:43 +00002334nMeshCreate(JNIEnv *_env, jobject _this, jlong con, jlongArray _vtx, jlongArray _idx, jintArray _prim)
Jason Samsbba134c2009-06-22 15:49:21 -07002335{
Andreas Gampe67333922014-11-10 20:35:59 -08002336 if (kLogApi) {
2337 ALOGD("nMeshCreate, con(%p)", (RsContext)con);
2338 }
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002339
2340 jint vtxLen = _env->GetArrayLength(_vtx);
Chris Wailes488230c32014-08-14 11:22:40 -07002341 jlong *jVtxPtr = _env->GetLongArrayElements(_vtx, nullptr);
Ashok Bhat98071552014-02-12 09:54:43 +00002342 RsAllocation* vtxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * vtxLen);
2343 for(int i = 0; i < vtxLen; ++i) {
2344 vtxPtr[i] = (RsAllocation)(uintptr_t)jVtxPtr[i];
2345 }
2346
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002347 jint idxLen = _env->GetArrayLength(_idx);
Chris Wailes488230c32014-08-14 11:22:40 -07002348 jlong *jIdxPtr = _env->GetLongArrayElements(_idx, nullptr);
Ashok Bhat98071552014-02-12 09:54:43 +00002349 RsAllocation* idxPtr = (RsAllocation*) malloc(sizeof(RsAllocation) * idxLen);
2350 for(int i = 0; i < idxLen; ++i) {
2351 idxPtr[i] = (RsAllocation)(uintptr_t)jIdxPtr[i];
2352 }
2353
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002354 jint primLen = _env->GetArrayLength(_prim);
Chris Wailes488230c32014-08-14 11:22:40 -07002355 jint *primPtr = _env->GetIntArrayElements(_prim, nullptr);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002356
Tim Murray3aa89c12014-08-18 17:51:22 -07002357 jlong id = (jlong)(uintptr_t)rsMeshCreate((RsContext)con,
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002358 (RsAllocation *)vtxPtr, vtxLen,
2359 (RsAllocation *)idxPtr, idxLen,
2360 (uint32_t *)primPtr, primLen);
2361
Ashok Bhat98071552014-02-12 09:54:43 +00002362 free(vtxPtr);
2363 free(idxPtr);
2364 _env->ReleaseLongArrayElements(_vtx, jVtxPtr, 0);
2365 _env->ReleaseLongArrayElements(_idx, jIdxPtr, 0);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -07002366 _env->ReleaseIntArrayElements(_prim, primPtr, 0);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002367 return id;
2368}
2369
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002370static jint
Tim Murray460a0492013-11-19 12:45:54 -08002371nMeshGetVertexBufferCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002372{
Andreas Gampe67333922014-11-10 20:35:59 -08002373 if (kLogApi) {
2374 ALOGD("nMeshGetVertexBufferCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2375 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002376 jint vtxCount = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08002377 rsaMeshGetVertexBufferCount((RsContext)con, (RsMesh)mesh, &vtxCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002378 return vtxCount;
2379}
2380
2381static jint
Tim Murray460a0492013-11-19 12:45:54 -08002382nMeshGetIndexCount(JNIEnv *_env, jobject _this, jlong con, jlong mesh)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002383{
Andreas Gampe67333922014-11-10 20:35:59 -08002384 if (kLogApi) {
2385 ALOGD("nMeshGetIndexCount, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2386 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002387 jint idxCount = 0;
Tim Murrayeff663f2013-11-15 13:08:30 -08002388 rsaMeshGetIndexCount((RsContext)con, (RsMesh)mesh, &idxCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002389 return idxCount;
2390}
2391
2392static void
Ashok Bhat98071552014-02-12 09:54:43 +00002393nMeshGetVertices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _ids, jint numVtxIDs)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002394{
Andreas Gampe67333922014-11-10 20:35:59 -08002395 if (kLogApi) {
2396 ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2397 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002398
2399 RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numVtxIDs * sizeof(RsAllocation));
Tim Murrayeff663f2013-11-15 13:08:30 -08002400 rsaMeshGetVertices((RsContext)con, (RsMesh)mesh, allocs, (uint32_t)numVtxIDs);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002401
2402 for(jint i = 0; i < numVtxIDs; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07002403 const jlong alloc = (jlong)(uintptr_t)allocs[i];
Ashok Bhat98071552014-02-12 09:54:43 +00002404 _env->SetLongArrayRegion(_ids, i, 1, &alloc);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002405 }
2406
2407 free(allocs);
2408}
2409
2410static void
Ashok Bhat98071552014-02-12 09:54:43 +00002411nMeshGetIndices(JNIEnv *_env, jobject _this, jlong con, jlong mesh, jlongArray _idxIds, jintArray _primitives, jint numIndices)
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002412{
Andreas Gampe67333922014-11-10 20:35:59 -08002413 if (kLogApi) {
2414 ALOGD("nMeshGetVertices, con(%p), Mesh(%p)", (RsContext)con, (RsMesh)mesh);
2415 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002416
2417 RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numIndices * sizeof(RsAllocation));
2418 uint32_t *prims= (uint32_t*)malloc((uint32_t)numIndices * sizeof(uint32_t));
2419
Tim Murrayeff663f2013-11-15 13:08:30 -08002420 rsaMeshGetIndices((RsContext)con, (RsMesh)mesh, allocs, prims, (uint32_t)numIndices);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002421
2422 for(jint i = 0; i < numIndices; i ++) {
Tim Murray3aa89c12014-08-18 17:51:22 -07002423 const jlong alloc = (jlong)(uintptr_t)allocs[i];
Ashok Bhat98071552014-02-12 09:54:43 +00002424 const jint prim = (jint)prims[i];
2425 _env->SetLongArrayRegion(_idxIds, i, 1, &alloc);
2426 _env->SetIntArrayRegion(_primitives, i, 1, &prim);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002427 }
2428
2429 free(allocs);
2430 free(prims);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002431}
2432
Tim Murray56f9e6f2014-05-16 11:47:26 -07002433static jint
2434nSystemGetPointerSize(JNIEnv *_env, jobject _this) {
2435 return (jint)sizeof(void*);
2436}
2437
2438
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002439// ---------------------------------------------------------------------------
2440
Jason Samsd19f10d2009-05-22 14:03:28 -07002441
Jason Sams94d8e90a2009-06-10 16:09:05 -07002442static const char *classPathName = "android/renderscript/RenderScript";
Jason Samsd19f10d2009-05-22 14:03:28 -07002443
2444static JNINativeMethod methods[] = {
Jason Sams1c415172010-11-08 17:06:46 -08002445{"_nInit", "()V", (void*)_nInit },
Jason Samsea84a7c2009-09-04 14:42:41 -07002446
Tim Murrayeff663f2013-11-15 13:08:30 -08002447{"nDeviceCreate", "()J", (void*)nDeviceCreate },
2448{"nDeviceDestroy", "(J)V", (void*)nDeviceDestroy },
2449{"nDeviceSetConfig", "(JII)V", (void*)nDeviceSetConfig },
2450{"nContextGetUserMessage", "(J[I)I", (void*)nContextGetUserMessage },
2451{"nContextGetErrorMessage", "(J)Ljava/lang/String;", (void*)nContextGetErrorMessage },
2452{"nContextPeekMessage", "(J[I)I", (void*)nContextPeekMessage },
Jason Sams1c415172010-11-08 17:06:46 -08002453
Tim Murrayeff663f2013-11-15 13:08:30 -08002454{"nContextInitToClient", "(J)V", (void*)nContextInitToClient },
2455{"nContextDeinitToClient", "(J)V", (void*)nContextDeinitToClient },
Jason Samsd19f10d2009-05-22 14:03:28 -07002456
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07002457
Jason Sams2e1872f2010-08-17 16:25:41 -07002458// All methods below are thread protected in java.
Tim Murrayeff663f2013-11-15 13:08:30 -08002459{"rsnContextCreate", "(JIII)J", (void*)nContextCreate },
2460{"rsnContextCreateGL", "(JIIIIIIIIIIIIFI)J", (void*)nContextCreateGL },
2461{"rsnContextFinish", "(J)V", (void*)nContextFinish },
2462{"rsnContextSetPriority", "(JI)V", (void*)nContextSetPriority },
Tim Murray47f31582015-04-07 15:43:24 -07002463{"rsnContextSetCacheDir", "(JLjava/lang/String;)V", (void*)nContextSetCacheDir },
Tim Murrayeff663f2013-11-15 13:08:30 -08002464{"rsnContextSetSurface", "(JIILandroid/view/Surface;)V", (void*)nContextSetSurface },
2465{"rsnContextDestroy", "(J)V", (void*)nContextDestroy },
2466{"rsnContextDump", "(JI)V", (void*)nContextDump },
2467{"rsnContextPause", "(J)V", (void*)nContextPause },
2468{"rsnContextResume", "(J)V", (void*)nContextResume },
2469{"rsnContextSendMessage", "(JI[I)V", (void*)nContextSendMessage },
Yang Ni281c3252014-10-24 08:52:24 -07002470{"rsnClosureCreate", "(JJJ[J[J[I[J[J)J", (void*)nClosureCreate },
Yang Nibe392ad2015-01-23 17:16:02 -08002471{"rsnInvokeClosureCreate", "(JJ[B[J[J[I)J", (void*)nInvokeClosureCreate },
Yang Ni281c3252014-10-24 08:52:24 -07002472{"rsnClosureSetArg", "(JJIJI)V", (void*)nClosureSetArg },
2473{"rsnClosureSetGlobal", "(JJJJI)V", (void*)nClosureSetGlobal },
Tim Murray460a0492013-11-19 12:45:54 -08002474{"rsnAssignName", "(JJ[B)V", (void*)nAssignName },
2475{"rsnGetName", "(JJ)Ljava/lang/String;", (void*)nGetName },
2476{"rsnObjDestroy", "(JJ)V", (void*)nObjDestroy },
Jason Sams64676f32009-07-08 18:01:53 -07002477
Tim Murray460a0492013-11-19 12:45:54 -08002478{"rsnFileA3DCreateFromFile", "(JLjava/lang/String;)J", (void*)nFileA3DCreateFromFile },
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002479{"rsnFileA3DCreateFromAssetStream", "(JJ)J", (void*)nFileA3DCreateFromAssetStream },
Tim Murray460a0492013-11-19 12:45:54 -08002480{"rsnFileA3DCreateFromAsset", "(JLandroid/content/res/AssetManager;Ljava/lang/String;)J", (void*)nFileA3DCreateFromAsset },
2481{"rsnFileA3DGetNumIndexEntries", "(JJ)I", (void*)nFileA3DGetNumIndexEntries },
2482{"rsnFileA3DGetIndexEntries", "(JJI[I[Ljava/lang/String;)V", (void*)nFileA3DGetIndexEntries },
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002483{"rsnFileA3DGetEntryByIndex", "(JJI)J", (void*)nFileA3DGetEntryByIndex },
Jason Samsd19f10d2009-05-22 14:03:28 -07002484
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002485{"rsnFontCreateFromFile", "(JLjava/lang/String;FI)J", (void*)nFontCreateFromFile },
2486{"rsnFontCreateFromAssetStream", "(JLjava/lang/String;FIJ)J", (void*)nFontCreateFromAssetStream },
2487{"rsnFontCreateFromAsset", "(JLandroid/content/res/AssetManager;Ljava/lang/String;FI)J", (void*)nFontCreateFromAsset },
Jason Samsd19f10d2009-05-22 14:03:28 -07002488
Tim Murray460a0492013-11-19 12:45:54 -08002489{"rsnElementCreate", "(JJIZI)J", (void*)nElementCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002490{"rsnElementCreate2", "(J[J[Ljava/lang/String;[I)J", (void*)nElementCreate2 },
Tim Murray460a0492013-11-19 12:45:54 -08002491{"rsnElementGetNativeData", "(JJ[I)V", (void*)nElementGetNativeData },
Ashok Bhat98071552014-02-12 09:54:43 +00002492{"rsnElementGetSubElements", "(JJ[J[Ljava/lang/String;[I)V", (void*)nElementGetSubElements },
Jason Samsd19f10d2009-05-22 14:03:28 -07002493
Tim Murray460a0492013-11-19 12:45:54 -08002494{"rsnTypeCreate", "(JJIIIZZI)J", (void*)nTypeCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002495{"rsnTypeGetNativeData", "(JJ[J)V", (void*)nTypeGetNativeData },
Jason Samsd19f10d2009-05-22 14:03:28 -07002496
Ashok Bhat98071552014-02-12 09:54:43 +00002497{"rsnAllocationCreateTyped", "(JJIIJ)J", (void*)nAllocationCreateTyped },
Tim Murray460a0492013-11-19 12:45:54 -08002498{"rsnAllocationCreateFromBitmap", "(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCreateFromBitmap },
2499{"rsnAllocationCreateBitmapBackedAllocation", "(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCreateBitmapBackedAllocation },
2500{"rsnAllocationCubeCreateFromBitmap","(JJILandroid/graphics/Bitmap;I)J", (void*)nAllocationCubeCreateFromBitmap },
Jason Sams5476b452010-12-08 16:14:36 -08002501
Tim Murray460a0492013-11-19 12:45:54 -08002502{"rsnAllocationCopyFromBitmap", "(JJLandroid/graphics/Bitmap;)V", (void*)nAllocationCopyFromBitmap },
2503{"rsnAllocationCopyToBitmap", "(JJLandroid/graphics/Bitmap;)V", (void*)nAllocationCopyToBitmap },
Jason Sams4ef66502010-12-10 16:03:15 -08002504
Tim Murray460a0492013-11-19 12:45:54 -08002505{"rsnAllocationSyncAll", "(JJI)V", (void*)nAllocationSyncAll },
2506{"rsnAllocationGetSurface", "(JJ)Landroid/view/Surface;", (void*)nAllocationGetSurface },
2507{"rsnAllocationSetSurface", "(JJLandroid/view/Surface;)V", (void*)nAllocationSetSurface },
2508{"rsnAllocationIoSend", "(JJ)V", (void*)nAllocationIoSend },
2509{"rsnAllocationIoReceive", "(JJ)V", (void*)nAllocationIoReceive },
Miao Wang87e908d2015-03-02 15:15:15 -08002510{"rsnAllocationData1D", "(JJIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData1D },
Miao Wangc8e237e2015-02-20 18:36:32 -08002511{"rsnAllocationElementData", "(JJIIIII[BI)V", (void*)nAllocationElementData },
Miao Wang87e908d2015-03-02 15:15:15 -08002512{"rsnAllocationData2D", "(JJIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData2D },
Tim Murray460a0492013-11-19 12:45:54 -08002513{"rsnAllocationData2D", "(JJIIIIIIJIIII)V", (void*)nAllocationData2D_alloc },
Miao Wang87e908d2015-03-02 15:15:15 -08002514{"rsnAllocationData3D", "(JJIIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationData3D },
Tim Murray460a0492013-11-19 12:45:54 -08002515{"rsnAllocationData3D", "(JJIIIIIIIJIIII)V", (void*)nAllocationData3D_alloc },
Miao Wang87e908d2015-03-02 15:15:15 -08002516{"rsnAllocationRead", "(JJLjava/lang/Object;IIZ)V", (void*)nAllocationRead },
2517{"rsnAllocationRead1D", "(JJIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead1D },
Miao Wang45cec0a2015-03-04 16:40:21 -08002518{"rsnAllocationElementRead", "(JJIIIII[BI)V", (void*)nAllocationElementRead },
Miao Wang87e908d2015-03-02 15:15:15 -08002519{"rsnAllocationRead2D", "(JJIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead2D },
2520{"rsnAllocationRead3D", "(JJIIIIIIILjava/lang/Object;IIIZ)V", (void*)nAllocationRead3D },
Tim Murray460a0492013-11-19 12:45:54 -08002521{"rsnAllocationGetType", "(JJ)J", (void*)nAllocationGetType},
2522{"rsnAllocationResize1D", "(JJI)V", (void*)nAllocationResize1D },
2523{"rsnAllocationGenerateMipmaps", "(JJ)V", (void*)nAllocationGenerateMipmaps },
Jason Samsbd1c3ad2009-08-03 16:03:08 -07002524
Jason Sams46ba27e32015-02-06 17:45:15 -08002525{"rsnAllocationAdapterCreate", "(JJJ)J", (void*)nAllocationAdapterCreate },
2526{"rsnAllocationAdapterOffset", "(JJIIIIIIIII)V", (void*)nAllocationAdapterOffset },
2527
Tim Murray460a0492013-11-19 12:45:54 -08002528{"rsnScriptBindAllocation", "(JJJI)V", (void*)nScriptBindAllocation },
2529{"rsnScriptSetTimeZone", "(JJ[B)V", (void*)nScriptSetTimeZone },
2530{"rsnScriptInvoke", "(JJI)V", (void*)nScriptInvoke },
2531{"rsnScriptInvokeV", "(JJI[B)V", (void*)nScriptInvokeV },
Chris Wailesbe7b1de2014-07-15 10:56:14 -07002532
2533{"rsnScriptForEach", "(JJI[JJ[B[I)V", (void*)nScriptForEach },
2534
Tim Murray460a0492013-11-19 12:45:54 -08002535{"rsnScriptSetVarI", "(JJII)V", (void*)nScriptSetVarI },
2536{"rsnScriptGetVarI", "(JJI)I", (void*)nScriptGetVarI },
2537{"rsnScriptSetVarJ", "(JJIJ)V", (void*)nScriptSetVarJ },
2538{"rsnScriptGetVarJ", "(JJI)J", (void*)nScriptGetVarJ },
2539{"rsnScriptSetVarF", "(JJIF)V", (void*)nScriptSetVarF },
2540{"rsnScriptGetVarF", "(JJI)F", (void*)nScriptGetVarF },
2541{"rsnScriptSetVarD", "(JJID)V", (void*)nScriptSetVarD },
2542{"rsnScriptGetVarD", "(JJI)D", (void*)nScriptGetVarD },
2543{"rsnScriptSetVarV", "(JJI[B)V", (void*)nScriptSetVarV },
2544{"rsnScriptGetVarV", "(JJI[B)V", (void*)nScriptGetVarV },
2545{"rsnScriptSetVarVE", "(JJI[BJ[I)V", (void*)nScriptSetVarVE },
2546{"rsnScriptSetVarObj", "(JJIJ)V", (void*)nScriptSetVarObj },
Jason Samsd19f10d2009-05-22 14:03:28 -07002547
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002548{"rsnScriptCCreate", "(JLjava/lang/String;Ljava/lang/String;[BI)J", (void*)nScriptCCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002549{"rsnScriptIntrinsicCreate", "(JIJ)J", (void*)nScriptIntrinsicCreate },
2550{"rsnScriptKernelIDCreate", "(JJII)J", (void*)nScriptKernelIDCreate },
Yang Nibe392ad2015-01-23 17:16:02 -08002551{"rsnScriptInvokeIDCreate", "(JJI)J", (void*)nScriptInvokeIDCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002552{"rsnScriptFieldIDCreate", "(JJI)J", (void*)nScriptFieldIDCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002553{"rsnScriptGroupCreate", "(J[J[J[J[J[J)J", (void*)nScriptGroupCreate },
Yang Ni35be56c2015-04-02 17:47:56 -07002554{"rsnScriptGroup2Create", "(JLjava/lang/String;Ljava/lang/String;[J)J", (void*)nScriptGroup2Create },
Tim Murray460a0492013-11-19 12:45:54 -08002555{"rsnScriptGroupSetInput", "(JJJJ)V", (void*)nScriptGroupSetInput },
2556{"rsnScriptGroupSetOutput", "(JJJJ)V", (void*)nScriptGroupSetOutput },
2557{"rsnScriptGroupExecute", "(JJ)V", (void*)nScriptGroupExecute },
Yang Ni281c3252014-10-24 08:52:24 -07002558{"rsnScriptGroup2Execute", "(JJ)V", (void*)nScriptGroup2Execute },
Jason Sams0011bcf2009-12-15 12:58:36 -08002559
Tim Murray25207df2015-01-12 16:47:56 -08002560{"rsnScriptIntrinsicBLAS_Single", "(JJIIIIIIIIIFJJFJIIII)V", (void*)nScriptIntrinsicBLAS_Single },
2561{"rsnScriptIntrinsicBLAS_Double", "(JJIIIIIIIIIDJJDJIIII)V", (void*)nScriptIntrinsicBLAS_Double },
2562{"rsnScriptIntrinsicBLAS_Complex", "(JJIIIIIIIIIFFJJFFJIIII)V", (void*)nScriptIntrinsicBLAS_Complex },
2563{"rsnScriptIntrinsicBLAS_Z", "(JJIIIIIIIIIDDJJDDJIIII)V", (void*)nScriptIntrinsicBLAS_Z },
2564
Tim Murray9cb16a22015-04-01 11:07:16 -07002565{"rsnScriptIntrinsicBLAS_BNNM", "(JJIIIJIJIJII)V", (void*)nScriptIntrinsicBLAS_BNNM },
2566
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002567{"rsnProgramStoreCreate", "(JZZZZZZIII)J", (void*)nProgramStoreCreate },
Jason Samsd19f10d2009-05-22 14:03:28 -07002568
Tim Murray460a0492013-11-19 12:45:54 -08002569{"rsnProgramBindConstants", "(JJIJ)V", (void*)nProgramBindConstants },
2570{"rsnProgramBindTexture", "(JJIJ)V", (void*)nProgramBindTexture },
2571{"rsnProgramBindSampler", "(JJIJ)V", (void*)nProgramBindSampler },
Jason Samsebfb4362009-09-23 13:57:02 -07002572
Ashok Bhat98071552014-02-12 09:54:43 +00002573{"rsnProgramFragmentCreate", "(JLjava/lang/String;[Ljava/lang/String;[J)J", (void*)nProgramFragmentCreate },
Tim Murray460a0492013-11-19 12:45:54 -08002574{"rsnProgramRasterCreate", "(JZI)J", (void*)nProgramRasterCreate },
Ashok Bhat98071552014-02-12 09:54:43 +00002575{"rsnProgramVertexCreate", "(JLjava/lang/String;[Ljava/lang/String;[J)J", (void*)nProgramVertexCreate },
Jason Samsd19f10d2009-05-22 14:03:28 -07002576
Narayan Kamath78c0ce52014-03-19 10:15:51 +00002577{"rsnContextBindRootScript", "(JJ)V", (void*)nContextBindRootScript },
2578{"rsnContextBindProgramStore", "(JJ)V", (void*)nContextBindProgramStore },
2579{"rsnContextBindProgramFragment", "(JJ)V", (void*)nContextBindProgramFragment },
2580{"rsnContextBindProgramVertex", "(JJ)V", (void*)nContextBindProgramVertex },
2581{"rsnContextBindProgramRaster", "(JJ)V", (void*)nContextBindProgramRaster },
Jason Sams02fb2cb2009-05-28 15:37:57 -07002582
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002583{"rsnSamplerCreate", "(JIIIIIF)J", (void*)nSamplerCreate },
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07002584
Ashok Bhat98071552014-02-12 09:54:43 +00002585{"rsnMeshCreate", "(J[J[J[I)J", (void*)nMeshCreate },
Jason Sams2e1872f2010-08-17 16:25:41 -07002586
Tim Murray460a0492013-11-19 12:45:54 -08002587{"rsnMeshGetVertexBufferCount", "(JJ)I", (void*)nMeshGetVertexBufferCount },
2588{"rsnMeshGetIndexCount", "(JJ)I", (void*)nMeshGetIndexCount },
Ashok Bhat98071552014-02-12 09:54:43 +00002589{"rsnMeshGetVertices", "(JJ[JI)V", (void*)nMeshGetVertices },
2590{"rsnMeshGetIndices", "(JJ[J[II)V", (void*)nMeshGetIndices },
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -07002591
Tim Murray56f9e6f2014-05-16 11:47:26 -07002592{"rsnSystemGetPointerSize", "()I", (void*)nSystemGetPointerSize },
Jason Samsd19f10d2009-05-22 14:03:28 -07002593};
2594
2595static int registerFuncs(JNIEnv *_env)
2596{
2597 return android::AndroidRuntime::registerNativeMethods(
2598 _env, classPathName, methods, NELEM(methods));
2599}
2600
2601// ---------------------------------------------------------------------------
2602
2603jint JNI_OnLoad(JavaVM* vm, void* reserved)
2604{
Chris Wailes488230c32014-08-14 11:22:40 -07002605 JNIEnv* env = nullptr;
Jason Samsd19f10d2009-05-22 14:03:28 -07002606 jint result = -1;
2607
Jason Samsd19f10d2009-05-22 14:03:28 -07002608 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
Steve Block3762c312012-01-06 19:20:56 +00002609 ALOGE("ERROR: GetEnv failed\n");
Jason Samsd19f10d2009-05-22 14:03:28 -07002610 goto bail;
2611 }
Chris Wailes488230c32014-08-14 11:22:40 -07002612 assert(env != nullptr);
Jason Samsd19f10d2009-05-22 14:03:28 -07002613
2614 if (registerFuncs(env) < 0) {
Ashok Bhat0e0c0882014-02-04 14:57:58 +00002615 ALOGE("ERROR: Renderscript native registration failed\n");
Jason Samsd19f10d2009-05-22 14:03:28 -07002616 goto bail;
2617 }
2618
2619 /* success -- return valid version number */
2620 result = JNI_VERSION_1_4;
2621
2622bail:
2623 return result;
2624}