1d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams/*
24cbe25a86b4aa3353c8016a129028671e902611fStephen Hines * Copyright (C) 2011-2012 The Android Open Source Project
3d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams *
4d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams * Licensed under the Apache License, Version 2.0 (the "License");
5d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams * you may not use this file except in compliance with the License.
6d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams * You may obtain a copy of the License at
7d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams *
8d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams *      http://www.apache.org/licenses/LICENSE-2.0
9d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams *
10d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams * Unless required by applicable law or agreed to in writing, software
11d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams * distributed under the License is distributed on an "AS IS" BASIS,
12d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams * See the License for the specific language governing permissions and
14d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams * limitations under the License.
15d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams */
16d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
17d1516dffa8d676fcad092d9736163829e6d0fdceJason Sams#define LOG_TAG "RenderScript_jni"
18f29ca50d80e3dc379de1642e85b7963175b2ca38Jason Sams
19d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams#include <stdlib.h>
20d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams#include <stdio.h>
21d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams#include <fcntl.h>
22d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams#include <unistd.h>
23d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams#include <math.h>
24d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams#include <utils/misc.h>
2509993f768bcb6dd9ed76b5c655da2ad9d0ad8396Bernhard Rosenkränzer#include <inttypes.h>
26d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
27b13b9bdad2baf6ad1ec2e56b6b7598fa20f55fc4Mathias Agopian#include <androidfw/Asset.h>
28b13b9bdad2baf6ad1ec2e56b6b7598fa20f55fc4Mathias Agopian#include <androidfw/AssetManager.h>
29b13b9bdad2baf6ad1ec2e56b6b7598fa20f55fc4Mathias Agopian#include <androidfw/ResourceTypes.h>
30f29ca50d80e3dc379de1642e85b7963175b2ca38Jason Sams
31d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams#include "jni.h"
32d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams#include "JNIHelp.h"
33d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams#include "android_runtime/AndroidRuntime.h"
34ee956053d6e3f7a7a82e41853b9b251fdc226d7aJim Miller#include "android_runtime/android_view_Surface.h"
35b0253ea6969bdd27bf574e0da7fa91aa6d09f44fAlex Sakhartchouk#include "android_runtime/android_util_AssetManager.h"
36f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck#include "android/graphics/GraphicsJNI.h"
37d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
381d6983af336b2b1d3a24aece58ce0850aa79e8a0Jason Sams#include <rs.h>
391d6983af336b2b1d3a24aece58ce0850aa79e8a0Jason Sams#include <rsEnv.h>
40fb9aa9f3ef11878caaf6fcfb5ea49ffb9f7abf33Jason Sams#include <gui/Surface.h>
41d47f7d8b5fe3a3861d7cbdc5f912235407823c8eAndy McFadden#include <gui/GLConsumer.h>
42faa32b33b50ad941f631716dd0915e936bdc3ac5Jason Sams#include <android_runtime/android_graphics_SurfaceTexture.h>
43d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
443762c311729fe9f3af085c14c5c1fb471d994c03Steve Block//#define LOG_API ALOGE
4567333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampestatic constexpr bool kLogApi = false;
46d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
47d19f10d43aa400e1183aa21a97099d02074131a2Jason Samsusing namespace android;
48d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
4967333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampetemplate <typename... T>
5067333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampevoid UNUSED(T... t) {}
5167333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe
52414fa2c93a763dce6b3adf898fbc68cf13667c07Stephen Hines#define PER_ARRAY_TYPE(flag, fnc, readonly, ...) {                                      \
53e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams    jint len = 0;                                                                       \
54488230c3fec4fc55abc2ee1de7b8b51a4e89f9a5Chris Wailes    void *ptr = nullptr;                                                                \
5587e908dfdece91b5f504386d4901fa3342dc8083Miao Wang    void *srcPtr = nullptr;                                                             \
5621659ac4c83d272f3ffd2f9b6333acbe5f0b02a1Jason Sams    size_t typeBytes = 0;                                                               \
57414fa2c93a763dce6b3adf898fbc68cf13667c07Stephen Hines    jint relFlag = 0;                                                                   \
58414fa2c93a763dce6b3adf898fbc68cf13667c07Stephen Hines    if (readonly) {                                                                     \
59414fa2c93a763dce6b3adf898fbc68cf13667c07Stephen Hines        /* The on-release mode should only be JNI_ABORT for read-only accesses. */      \
6087e908dfdece91b5f504386d4901fa3342dc8083Miao Wang        /* readonly = true, also indicates we are copying to the allocation   . */      \
61414fa2c93a763dce6b3adf898fbc68cf13667c07Stephen Hines        relFlag = JNI_ABORT;                                                            \
62414fa2c93a763dce6b3adf898fbc68cf13667c07Stephen Hines    }                                                                                   \
63e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams    switch(dataType) {                                                                  \
64e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams    case RS_TYPE_FLOAT_32:                                                              \
65e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams        len = _env->GetArrayLength((jfloatArray)data);                                  \
66e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams        ptr = _env->GetFloatArrayElements((jfloatArray)data, flag);                     \
67ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang        if (ptr == nullptr) {                                                           \
68ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang            ALOGE("Failed to get Java array elements.");                                \
69ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang            return;                                                                     \
70ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang        }                                                                               \
7121659ac4c83d272f3ffd2f9b6333acbe5f0b02a1Jason Sams        typeBytes = 4;                                                                  \
7287e908dfdece91b5f504386d4901fa3342dc8083Miao Wang        if (usePadding) {                                                               \
7387e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            srcPtr = ptr;                                                               \
7487e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            len = len / 3 * 4;                                                          \
7587e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            if (count == 0) {                                                           \
7687e908dfdece91b5f504386d4901fa3342dc8083Miao Wang                count = len / 4;                                                        \
7787e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            }                                                                           \
7887e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            ptr = malloc (len * typeBytes);                                             \
7987e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            if (readonly) {                                                             \
8087e908dfdece91b5f504386d4901fa3342dc8083Miao Wang                copyWithPadding(ptr, srcPtr, mSize, count);                             \
8187e908dfdece91b5f504386d4901fa3342dc8083Miao Wang                fnc(__VA_ARGS__);                                                       \
8287e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            } else {                                                                    \
8387e908dfdece91b5f504386d4901fa3342dc8083Miao Wang                fnc(__VA_ARGS__);                                                       \
8487e908dfdece91b5f504386d4901fa3342dc8083Miao Wang                copyWithUnPadding(srcPtr, ptr, mSize, count);                           \
8587e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            }                                                                           \
8687e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            free(ptr);                                                                  \
8787e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            ptr = srcPtr;                                                               \
8887e908dfdece91b5f504386d4901fa3342dc8083Miao Wang        } else {                                                                        \
8987e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            fnc(__VA_ARGS__);                                                           \
9087e908dfdece91b5f504386d4901fa3342dc8083Miao Wang        }                                                                               \
91414fa2c93a763dce6b3adf898fbc68cf13667c07Stephen Hines        _env->ReleaseFloatArrayElements((jfloatArray)data, (jfloat *)ptr, relFlag);     \
92e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams        return;                                                                         \
93e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams    case RS_TYPE_FLOAT_64:                                                              \
94e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams        len = _env->GetArrayLength((jdoubleArray)data);                                 \
95e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams        ptr = _env->GetDoubleArrayElements((jdoubleArray)data, flag);                   \
96ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang        if (ptr == nullptr) {                                                           \
97ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang            ALOGE("Failed to get Java array elements.");                                \
98ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang            return;                                                                     \
99ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang        }                                                                               \
10021659ac4c83d272f3ffd2f9b6333acbe5f0b02a1Jason Sams        typeBytes = 8;                                                                  \
10187e908dfdece91b5f504386d4901fa3342dc8083Miao Wang        if (usePadding) {                                                               \
10287e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            srcPtr = ptr;                                                               \
10387e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            len = len / 3 * 4;                                                          \
10487e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            if (count == 0) {                                                           \
10587e908dfdece91b5f504386d4901fa3342dc8083Miao Wang                count = len / 4;                                                        \
10687e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            }                                                                           \
10787e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            ptr = malloc (len * typeBytes);                                             \
10887e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            if (readonly) {                                                             \
10987e908dfdece91b5f504386d4901fa3342dc8083Miao Wang                copyWithPadding(ptr, srcPtr, mSize, count);                             \
11087e908dfdece91b5f504386d4901fa3342dc8083Miao Wang                fnc(__VA_ARGS__);                                                       \
11187e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            } else {                                                                    \
11287e908dfdece91b5f504386d4901fa3342dc8083Miao Wang                fnc(__VA_ARGS__);                                                       \
11387e908dfdece91b5f504386d4901fa3342dc8083Miao Wang                copyWithUnPadding(srcPtr, ptr, mSize, count);                           \
11487e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            }                                                                           \
11587e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            free(ptr);                                                                  \
11687e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            ptr = srcPtr;                                                               \
11787e908dfdece91b5f504386d4901fa3342dc8083Miao Wang        } else {                                                                        \
11887e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            fnc(__VA_ARGS__);                                                           \
11987e908dfdece91b5f504386d4901fa3342dc8083Miao Wang        }                                                                               \
120414fa2c93a763dce6b3adf898fbc68cf13667c07Stephen Hines        _env->ReleaseDoubleArrayElements((jdoubleArray)data, (jdouble *)ptr, relFlag);  \
121e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams        return;                                                                         \
122e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams    case RS_TYPE_SIGNED_8:                                                              \
123e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams    case RS_TYPE_UNSIGNED_8:                                                            \
124e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams        len = _env->GetArrayLength((jbyteArray)data);                                   \
125e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams        ptr = _env->GetByteArrayElements((jbyteArray)data, flag);                       \
126ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang        if (ptr == nullptr) {                                                           \
127ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang            ALOGE("Failed to get Java array elements.");                                \
128ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang            return;                                                                     \
129ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang        }                                                                               \
13021659ac4c83d272f3ffd2f9b6333acbe5f0b02a1Jason Sams        typeBytes = 1;                                                                  \
13187e908dfdece91b5f504386d4901fa3342dc8083Miao Wang        if (usePadding) {                                                               \
13287e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            srcPtr = ptr;                                                               \
13387e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            len = len / 3 * 4;                                                          \
13487e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            if (count == 0) {                                                           \
13587e908dfdece91b5f504386d4901fa3342dc8083Miao Wang                count = len / 4;                                                        \
13687e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            }                                                                           \
13787e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            ptr = malloc (len * typeBytes);                                             \
13887e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            if (readonly) {                                                             \
13987e908dfdece91b5f504386d4901fa3342dc8083Miao Wang                copyWithPadding(ptr, srcPtr, mSize, count);                             \
14087e908dfdece91b5f504386d4901fa3342dc8083Miao Wang                fnc(__VA_ARGS__);                                                       \
14187e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            } else {                                                                    \
14287e908dfdece91b5f504386d4901fa3342dc8083Miao Wang                fnc(__VA_ARGS__);                                                       \
14387e908dfdece91b5f504386d4901fa3342dc8083Miao Wang                copyWithUnPadding(srcPtr, ptr, mSize, count);                           \
14487e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            }                                                                           \
14587e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            free(ptr);                                                                  \
14687e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            ptr = srcPtr;                                                               \
14787e908dfdece91b5f504386d4901fa3342dc8083Miao Wang        } else {                                                                        \
14887e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            fnc(__VA_ARGS__);                                                           \
14987e908dfdece91b5f504386d4901fa3342dc8083Miao Wang        }                                                                               \
150414fa2c93a763dce6b3adf898fbc68cf13667c07Stephen Hines        _env->ReleaseByteArrayElements((jbyteArray)data, (jbyte*)ptr, relFlag);         \
151e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams        return;                                                                         \
152e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams    case RS_TYPE_SIGNED_16:                                                             \
153e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams    case RS_TYPE_UNSIGNED_16:                                                           \
15485e8c51dbf8dc774ecfd09981ac6a8bf1b75f464Pirama Arumuga Nainar    case RS_TYPE_FLOAT_16:                                                              \
155e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams        len = _env->GetArrayLength((jshortArray)data);                                  \
156e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams        ptr = _env->GetShortArrayElements((jshortArray)data, flag);                     \
157ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang        if (ptr == nullptr) {                                                           \
158ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang            ALOGE("Failed to get Java array elements.");                                \
159ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang            return;                                                                     \
160ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang        }                                                                               \
16121659ac4c83d272f3ffd2f9b6333acbe5f0b02a1Jason Sams        typeBytes = 2;                                                                  \
16287e908dfdece91b5f504386d4901fa3342dc8083Miao Wang        if (usePadding) {                                                               \
16387e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            srcPtr = ptr;                                                               \
16487e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            len = len / 3 * 4;                                                          \
16587e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            if (count == 0) {                                                           \
16687e908dfdece91b5f504386d4901fa3342dc8083Miao Wang                count = len / 4;                                                        \
16787e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            }                                                                           \
16887e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            ptr = malloc (len * typeBytes);                                             \
16987e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            if (readonly) {                                                             \
17087e908dfdece91b5f504386d4901fa3342dc8083Miao Wang                copyWithPadding(ptr, srcPtr, mSize, count);                             \
17187e908dfdece91b5f504386d4901fa3342dc8083Miao Wang                fnc(__VA_ARGS__);                                                       \
17287e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            } else {                                                                    \
17387e908dfdece91b5f504386d4901fa3342dc8083Miao Wang                fnc(__VA_ARGS__);                                                       \
17487e908dfdece91b5f504386d4901fa3342dc8083Miao Wang                copyWithUnPadding(srcPtr, ptr, mSize, count);                           \
17587e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            }                                                                           \
17687e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            free(ptr);                                                                  \
17787e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            ptr = srcPtr;                                                               \
17887e908dfdece91b5f504386d4901fa3342dc8083Miao Wang        } else {                                                                        \
17987e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            fnc(__VA_ARGS__);                                                           \
18087e908dfdece91b5f504386d4901fa3342dc8083Miao Wang        }                                                                               \
181414fa2c93a763dce6b3adf898fbc68cf13667c07Stephen Hines        _env->ReleaseShortArrayElements((jshortArray)data, (jshort *)ptr, relFlag);     \
182e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams        return;                                                                         \
183e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams    case RS_TYPE_SIGNED_32:                                                             \
184e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams    case RS_TYPE_UNSIGNED_32:                                                           \
185e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams        len = _env->GetArrayLength((jintArray)data);                                    \
186e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams        ptr = _env->GetIntArrayElements((jintArray)data, flag);                         \
187ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang        if (ptr == nullptr) {                                                           \
188ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang            ALOGE("Failed to get Java array elements.");                                \
189ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang            return;                                                                     \
190ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang        }                                                                               \
19121659ac4c83d272f3ffd2f9b6333acbe5f0b02a1Jason Sams        typeBytes = 4;                                                                  \
19287e908dfdece91b5f504386d4901fa3342dc8083Miao Wang        if (usePadding) {                                                               \
19387e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            srcPtr = ptr;                                                               \
19487e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            len = len / 3 * 4;                                                          \
19587e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            if (count == 0) {                                                           \
19687e908dfdece91b5f504386d4901fa3342dc8083Miao Wang                count = len / 4;                                                        \
19787e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            }                                                                           \
19887e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            ptr = malloc (len * typeBytes);                                             \
19987e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            if (readonly) {                                                             \
20087e908dfdece91b5f504386d4901fa3342dc8083Miao Wang                copyWithPadding(ptr, srcPtr, mSize, count);                             \
20187e908dfdece91b5f504386d4901fa3342dc8083Miao Wang                fnc(__VA_ARGS__);                                                       \
20287e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            } else {                                                                    \
20387e908dfdece91b5f504386d4901fa3342dc8083Miao Wang                fnc(__VA_ARGS__);                                                       \
20487e908dfdece91b5f504386d4901fa3342dc8083Miao Wang                copyWithUnPadding(srcPtr, ptr, mSize, count);                           \
20587e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            }                                                                           \
20687e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            free(ptr);                                                                  \
20787e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            ptr = srcPtr;                                                               \
20887e908dfdece91b5f504386d4901fa3342dc8083Miao Wang        } else {                                                                        \
20987e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            fnc(__VA_ARGS__);                                                           \
21087e908dfdece91b5f504386d4901fa3342dc8083Miao Wang        }                                                                               \
211414fa2c93a763dce6b3adf898fbc68cf13667c07Stephen Hines        _env->ReleaseIntArrayElements((jintArray)data, (jint *)ptr, relFlag);           \
212e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams        return;                                                                         \
213e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams    case RS_TYPE_SIGNED_64:                                                             \
214e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams    case RS_TYPE_UNSIGNED_64:                                                           \
215e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams        len = _env->GetArrayLength((jlongArray)data);                                   \
216e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams        ptr = _env->GetLongArrayElements((jlongArray)data, flag);                       \
217ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang        if (ptr == nullptr) {                                                           \
218ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang            ALOGE("Failed to get Java array elements.");                                \
219ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang            return;                                                                     \
220ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang        }                                                                               \
22121659ac4c83d272f3ffd2f9b6333acbe5f0b02a1Jason Sams        typeBytes = 8;                                                                  \
22287e908dfdece91b5f504386d4901fa3342dc8083Miao Wang        if (usePadding) {                                                               \
22387e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            srcPtr = ptr;                                                               \
22487e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            len = len / 3 * 4;                                                          \
22587e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            if (count == 0) {                                                           \
22687e908dfdece91b5f504386d4901fa3342dc8083Miao Wang                count = len / 4;                                                        \
22787e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            }                                                                           \
22887e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            ptr = malloc (len * typeBytes);                                             \
22987e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            if (readonly) {                                                             \
23087e908dfdece91b5f504386d4901fa3342dc8083Miao Wang                copyWithPadding(ptr, srcPtr, mSize, count);                             \
23187e908dfdece91b5f504386d4901fa3342dc8083Miao Wang                fnc(__VA_ARGS__);                                                       \
23287e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            } else {                                                                    \
23387e908dfdece91b5f504386d4901fa3342dc8083Miao Wang                fnc(__VA_ARGS__);                                                       \
23487e908dfdece91b5f504386d4901fa3342dc8083Miao Wang                copyWithUnPadding(srcPtr, ptr, mSize, count);                           \
23587e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            }                                                                           \
23687e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            free(ptr);                                                                  \
23787e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            ptr = srcPtr;                                                               \
23887e908dfdece91b5f504386d4901fa3342dc8083Miao Wang        } else {                                                                        \
23987e908dfdece91b5f504386d4901fa3342dc8083Miao Wang            fnc(__VA_ARGS__);                                                           \
24087e908dfdece91b5f504386d4901fa3342dc8083Miao Wang        }                                                                               \
241414fa2c93a763dce6b3adf898fbc68cf13667c07Stephen Hines        _env->ReleaseLongArrayElements((jlongArray)data, (jlong *)ptr, relFlag);        \
242e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams        return;                                                                         \
243e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams    default:                                                                            \
244e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams        break;                                                                          \
245e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams    }                                                                                   \
24687e908dfdece91b5f504386d4901fa3342dc8083Miao Wang    UNUSED(len, ptr, srcPtr, typeBytes, relFlag);                                       \
247e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams}
248e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams
249e729a9400f3ed2234a95a53aadb3420b76436d63Jason Sams
250b0253ea6969bdd27bf574e0da7fa91aa6d09f44fAlex Sakhartchoukclass AutoJavaStringToUTF8 {
251b0253ea6969bdd27bf574e0da7fa91aa6d09f44fAlex Sakhartchoukpublic:
2522123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk    AutoJavaStringToUTF8(JNIEnv* env, jstring str) : fEnv(env), fJStr(str) {
253488230c3fec4fc55abc2ee1de7b8b51a4e89f9a5Chris Wailes        fCStr = env->GetStringUTFChars(str, nullptr);
254b0253ea6969bdd27bf574e0da7fa91aa6d09f44fAlex Sakhartchouk        fLength = env->GetStringUTFLength(str);
255b0253ea6969bdd27bf574e0da7fa91aa6d09f44fAlex Sakhartchouk    }
2562123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk    ~AutoJavaStringToUTF8() {
257b0253ea6969bdd27bf574e0da7fa91aa6d09f44fAlex Sakhartchouk        fEnv->ReleaseStringUTFChars(fJStr, fCStr);
258b0253ea6969bdd27bf574e0da7fa91aa6d09f44fAlex Sakhartchouk    }
259b0253ea6969bdd27bf574e0da7fa91aa6d09f44fAlex Sakhartchouk    const char* c_str() const { return fCStr; }
260b0253ea6969bdd27bf574e0da7fa91aa6d09f44fAlex Sakhartchouk    jsize length() const { return fLength; }
261b0253ea6969bdd27bf574e0da7fa91aa6d09f44fAlex Sakhartchouk
262b0253ea6969bdd27bf574e0da7fa91aa6d09f44fAlex Sakhartchoukprivate:
263b0253ea6969bdd27bf574e0da7fa91aa6d09f44fAlex Sakhartchouk    JNIEnv*     fEnv;
264b0253ea6969bdd27bf574e0da7fa91aa6d09f44fAlex Sakhartchouk    jstring     fJStr;
265b0253ea6969bdd27bf574e0da7fa91aa6d09f44fAlex Sakhartchouk    const char* fCStr;
266b0253ea6969bdd27bf574e0da7fa91aa6d09f44fAlex Sakhartchouk    jsize       fLength;
267b0253ea6969bdd27bf574e0da7fa91aa6d09f44fAlex Sakhartchouk};
268b0253ea6969bdd27bf574e0da7fa91aa6d09f44fAlex Sakhartchouk
2692123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchoukclass AutoJavaStringArrayToUTF8 {
2702123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchoukpublic:
2712123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk    AutoJavaStringArrayToUTF8(JNIEnv* env, jobjectArray strings, jsize stringsLength)
2722123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk    : mEnv(env), mStrings(strings), mStringsLength(stringsLength) {
273488230c3fec4fc55abc2ee1de7b8b51a4e89f9a5Chris Wailes        mCStrings = nullptr;
274488230c3fec4fc55abc2ee1de7b8b51a4e89f9a5Chris Wailes        mSizeArray = nullptr;
2752123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk        if (stringsLength > 0) {
2762123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk            mCStrings = (const char **)calloc(stringsLength, sizeof(char *));
2772123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk            mSizeArray = (size_t*)calloc(stringsLength, sizeof(size_t));
2782123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk            for (jsize ct = 0; ct < stringsLength; ct ++) {
2792123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk                jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
280488230c3fec4fc55abc2ee1de7b8b51a4e89f9a5Chris Wailes                mCStrings[ct] = mEnv->GetStringUTFChars(s, nullptr);
2812123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk                mSizeArray[ct] = mEnv->GetStringUTFLength(s);
2822123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk            }
2832123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk        }
2842123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk    }
2852123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk    ~AutoJavaStringArrayToUTF8() {
2862123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk        for (jsize ct=0; ct < mStringsLength; ct++) {
2872123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk            jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
2882123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk            mEnv->ReleaseStringUTFChars(s, mCStrings[ct]);
2892123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk        }
2902123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk        free(mCStrings);
2912123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk        free(mSizeArray);
2922123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk    }
2932123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk    const char **c_str() const { return mCStrings; }
2942123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk    size_t *c_str_len() const { return mSizeArray; }
2952123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk    jsize length() const { return mStringsLength; }
2962123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk
2972123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchoukprivate:
2982123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk    JNIEnv      *mEnv;
2992123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk    jobjectArray mStrings;
3002123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk    const char **mCStrings;
3012123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk    size_t      *mSizeArray;
3022123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk    jsize        mStringsLength;
3032123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk};
3042123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk
305d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams// ---------------------------------------------------------------------------
306d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
307ffe9f48890dde7173a0845d32887fdf94a49b0a7Jason Samsstatic jfieldID gContextId = 0;
308d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
309d19f10d43aa400e1183aa21a97099d02074131a2Jason Samsstatic void _nInit(JNIEnv *_env, jclass _this)
310d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams{
311eff663f391fa4f119685d5c14489b94661ea126fTim Murray    gContextId             = _env->GetFieldID(_this, "mContext", "J");
312d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams}
313d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
314d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams// ---------------------------------------------------------------------------
315d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
31687e908dfdece91b5f504386d4901fa3342dc8083Miao Wangstatic void copyWithPadding(void* ptr, void* srcPtr, int mSize, int count) {
31787e908dfdece91b5f504386d4901fa3342dc8083Miao Wang    int sizeBytesPad = mSize * 4;
31887e908dfdece91b5f504386d4901fa3342dc8083Miao Wang    int sizeBytes = mSize * 3;
31987e908dfdece91b5f504386d4901fa3342dc8083Miao Wang    uint8_t *dst = static_cast<uint8_t *>(ptr);
32087e908dfdece91b5f504386d4901fa3342dc8083Miao Wang    uint8_t *src = static_cast<uint8_t *>(srcPtr);
32187e908dfdece91b5f504386d4901fa3342dc8083Miao Wang    for (int i = 0; i < count; i++) {
32287e908dfdece91b5f504386d4901fa3342dc8083Miao Wang        memcpy(dst, src, sizeBytes);
32387e908dfdece91b5f504386d4901fa3342dc8083Miao Wang        dst += sizeBytesPad;
32487e908dfdece91b5f504386d4901fa3342dc8083Miao Wang        src += sizeBytes;
32587e908dfdece91b5f504386d4901fa3342dc8083Miao Wang    }
32687e908dfdece91b5f504386d4901fa3342dc8083Miao Wang}
32787e908dfdece91b5f504386d4901fa3342dc8083Miao Wang
32887e908dfdece91b5f504386d4901fa3342dc8083Miao Wangstatic void copyWithUnPadding(void* ptr, void* srcPtr, int mSize, int count) {
32987e908dfdece91b5f504386d4901fa3342dc8083Miao Wang    int sizeBytesPad = mSize * 4;
33087e908dfdece91b5f504386d4901fa3342dc8083Miao Wang    int sizeBytes = mSize * 3;
33187e908dfdece91b5f504386d4901fa3342dc8083Miao Wang    uint8_t *dst = static_cast<uint8_t *>(ptr);
33287e908dfdece91b5f504386d4901fa3342dc8083Miao Wang    uint8_t *src = static_cast<uint8_t *>(srcPtr);
33387e908dfdece91b5f504386d4901fa3342dc8083Miao Wang    for (int i = 0; i < count; i++) {
33487e908dfdece91b5f504386d4901fa3342dc8083Miao Wang        memcpy(dst, src, sizeBytes);
33587e908dfdece91b5f504386d4901fa3342dc8083Miao Wang        dst += sizeBytes;
33687e908dfdece91b5f504386d4901fa3342dc8083Miao Wang        src += sizeBytesPad;
33787e908dfdece91b5f504386d4901fa3342dc8083Miao Wang    }
33887e908dfdece91b5f504386d4901fa3342dc8083Miao Wang}
33987e908dfdece91b5f504386d4901fa3342dc8083Miao Wang
34087e908dfdece91b5f504386d4901fa3342dc8083Miao Wang
34187e908dfdece91b5f504386d4901fa3342dc8083Miao Wang// ---------------------------------------------------------------------------
3423eaa338e11a3b0d6b87d705e5bb95625e82347bdJason Samsstatic void
343eff663f391fa4f119685d5c14489b94661ea126fTim MurraynContextFinish(JNIEnv *_env, jobject _this, jlong con)
34496ed4cfa62dd09aafb3f9da01e047661b4fe3c95Jason Sams{
34567333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    if (kLogApi) {
34667333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe        ALOGD("nContextFinish, con(%p)", (RsContext)con);
34767333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    }
348eff663f391fa4f119685d5c14489b94661ea126fTim Murray    rsContextFinish((RsContext)con);
34996ed4cfa62dd09aafb3f9da01e047661b4fe3c95Jason Sams}
35096ed4cfa62dd09aafb3f9da01e047661b4fe3c95Jason Sams
351281c3252510bcf4120d17fa0125f369c2c78f162Yang Nistatic jlong
352281c3252510bcf4120d17fa0125f369c2c78f162Yang NinClosureCreate(JNIEnv *_env, jobject _this, jlong con, jlong kernelID,
353281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni               jlong returnValue, jlongArray fieldIDArray,
354281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni               jlongArray valueArray, jintArray sizeArray,
355281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni               jlongArray depClosureArray, jlongArray depFieldIDArray) {
35617c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  jlong ret = 0;
35717c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni
358281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni  jlong* jFieldIDs = _env->GetLongArrayElements(fieldIDArray, nullptr);
359281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni  jsize fieldIDs_length = _env->GetArrayLength(fieldIDArray);
360ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang  if (jFieldIDs == nullptr) {
361ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang      ALOGE("Failed to get Java array elements: fieldIDs.");
362ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang      return ret;
363ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang  }
364ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang
36517c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  jlong* jValues = _env->GetLongArrayElements(valueArray, nullptr);
36617c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  jsize values_length = _env->GetArrayLength(valueArray);
367ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang  if (jValues == nullptr) {
368ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang      ALOGE("Failed to get Java array elements: values.");
369ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang      return ret;
370ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang  }
371ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang
37217c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  jint* jSizes = _env->GetIntArrayElements(sizeArray, nullptr);
37317c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  jsize sizes_length = _env->GetArrayLength(sizeArray);
374ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang  if (jSizes == nullptr) {
375ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang      ALOGE("Failed to get Java array elements: sizes.");
376ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang      return ret;
377ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang  }
378ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang
37917c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  jlong* jDepClosures =
38017c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni      _env->GetLongArrayElements(depClosureArray, nullptr);
38117c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  jsize depClosures_length = _env->GetArrayLength(depClosureArray);
382ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang  if (jDepClosures == nullptr) {
383ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang      ALOGE("Failed to get Java array elements: depClosures.");
384ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang      return ret;
385ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang  }
386ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang
38717c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  jlong* jDepFieldIDs =
38817c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni      _env->GetLongArrayElements(depFieldIDArray, nullptr);
38917c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  jsize depFieldIDs_length = _env->GetArrayLength(depFieldIDArray);
390ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang  if (jDepFieldIDs == nullptr) {
391ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang      ALOGE("Failed to get Java array elements: depFieldIDs.");
392ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang      return ret;
393ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang  }
39417c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni
39517c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  size_t numValues, numDependencies;
39617c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  RsScriptFieldID* fieldIDs;
39717c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  RsClosure* depClosures;
39817c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  RsScriptFieldID* depFieldIDs;
39917c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni
40017c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  if (fieldIDs_length != values_length || values_length != sizes_length) {
40117c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni      ALOGE("Unmatched field IDs, values, and sizes in closure creation.");
40217c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni      goto exit;
40317c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  }
40417c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni
40517c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  numValues = (size_t)fieldIDs_length;
40617c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni
40717c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  if (depClosures_length != depFieldIDs_length) {
40817c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni      ALOGE("Unmatched closures and field IDs for dependencies in closure creation.");
40917c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni      goto exit;
41017c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  }
41117c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni
41217c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  numDependencies = (size_t)depClosures_length;
41317c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni
41417c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  if (numDependencies > numValues) {
41517c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni      ALOGE("Unexpected number of dependencies in closure creation");
41617c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni      goto exit;
41717c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  }
41817c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni
4197b2a46fee0f95b9ae2485db36e0d279730623c4aYang Ni  if (numValues > RS_CLOSURE_MAX_NUMBER_ARGS_AND_BINDINGS) {
42017c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni      ALOGE("Too many arguments or globals in closure creation");
42117c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni      goto exit;
42217c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  }
42317c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni
42417c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  fieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numValues);
42517c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  if (fieldIDs == nullptr) {
42617c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni      goto exit;
42717c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  }
42817c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni
42917c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  for (size_t i = 0; i < numValues; i++) {
430281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni    fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i];
431281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni  }
432281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni
43317c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  depClosures = (RsClosure*)alloca(sizeof(RsClosure) * numDependencies);
43417c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  if (depClosures == nullptr) {
43517c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni      goto exit;
43617c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  }
437281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni
43817c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  for (size_t i = 0; i < numDependencies; i++) {
439281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni    depClosures[i] = (RsClosure)jDepClosures[i];
440281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni  }
441281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni
44217c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  depFieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numDependencies);
44317c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  if (depFieldIDs == nullptr) {
44417c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni      goto exit;
44517c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  }
44617c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni
44717c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  for (size_t i = 0; i < numDependencies; i++) {
448281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni    depFieldIDs[i] = (RsClosure)jDepFieldIDs[i];
449281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni  }
450281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni
45117c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  ret = (jlong)(uintptr_t)rsClosureCreate(
452281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni      (RsContext)con, (RsScriptKernelID)kernelID, (RsAllocation)returnValue,
453263cc90345784c3f60bf57c0de91afc4d6c3d5dbYang Ni      fieldIDs, numValues, jValues, numValues,
45417c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni      (int*)jSizes, numValues,
45517c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni      depClosures, numDependencies,
45617c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni      depFieldIDs, numDependencies);
45717c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni
45817c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Niexit:
45917c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni
46017c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  _env->ReleaseLongArrayElements(depFieldIDArray, jDepFieldIDs, JNI_ABORT);
46117c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  _env->ReleaseLongArrayElements(depClosureArray, jDepClosures, JNI_ABORT);
46217c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  _env->ReleaseIntArrayElements (sizeArray,       jSizes,       JNI_ABORT);
46317c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  _env->ReleaseLongArrayElements(valueArray,      jValues,      JNI_ABORT);
46417c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  _env->ReleaseLongArrayElements(fieldIDArray,    jFieldIDs,    JNI_ABORT);
46517c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni
46617c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  return ret;
467281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni}
468281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni
469be392ad35e29b17ed54fdbbbb8dd3e80fc1022b9Yang Nistatic jlong
470be392ad35e29b17ed54fdbbbb8dd3e80fc1022b9Yang NinInvokeClosureCreate(JNIEnv *_env, jobject _this, jlong con, jlong invokeID,
471be392ad35e29b17ed54fdbbbb8dd3e80fc1022b9Yang Ni                     jbyteArray paramArray, jlongArray fieldIDArray, jlongArray valueArray,
472be392ad35e29b17ed54fdbbbb8dd3e80fc1022b9Yang Ni                     jintArray sizeArray) {
47317c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  jlong ret = 0;
47417c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni
475be392ad35e29b17ed54fdbbbb8dd3e80fc1022b9Yang Ni  jbyte* jParams = _env->GetByteArrayElements(paramArray, nullptr);
476be392ad35e29b17ed54fdbbbb8dd3e80fc1022b9Yang Ni  jsize jParamLength = _env->GetArrayLength(paramArray);
477ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang  if (jParams == nullptr) {
478ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang      ALOGE("Failed to get Java array elements: params.");
479ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang      return ret;
480ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang  }
481ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang
482be392ad35e29b17ed54fdbbbb8dd3e80fc1022b9Yang Ni  jlong* jFieldIDs = _env->GetLongArrayElements(fieldIDArray, nullptr);
483be392ad35e29b17ed54fdbbbb8dd3e80fc1022b9Yang Ni  jsize fieldIDs_length = _env->GetArrayLength(fieldIDArray);
484ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang  if (jFieldIDs == nullptr) {
485ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang      ALOGE("Failed to get Java array elements: fieldIDs.");
486ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang      return ret;
487ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang  }
488ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang
48917c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  jlong* jValues = _env->GetLongArrayElements(valueArray, nullptr);
49017c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  jsize values_length = _env->GetArrayLength(valueArray);
491ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang  if (jValues == nullptr) {
492ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang      ALOGE("Failed to get Java array elements: values.");
493ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang      return ret;
494ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang  }
495ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang
49617c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  jint* jSizes = _env->GetIntArrayElements(sizeArray, nullptr);
49717c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  jsize sizes_length = _env->GetArrayLength(sizeArray);
498ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang  if (jSizes == nullptr) {
499ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang      ALOGE("Failed to get Java array elements: sizes.");
500ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang      return ret;
501ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang  }
50217c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni
50317c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  size_t numValues;
50417c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  RsScriptFieldID* fieldIDs;
50517c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni
50617c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  if (fieldIDs_length != values_length || values_length != sizes_length) {
50717c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni      ALOGE("Unmatched field IDs, values, and sizes in closure creation.");
50817c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni      goto exit;
50917c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  }
51017c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni
51117c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  numValues = (size_t) fieldIDs_length;
51217c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni
5137b2a46fee0f95b9ae2485db36e0d279730623c4aYang Ni  if (numValues > RS_CLOSURE_MAX_NUMBER_ARGS_AND_BINDINGS) {
51417c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni      ALOGE("Too many arguments or globals in closure creation");
51517c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni      goto exit;
51617c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  }
51717c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni
51817c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  fieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numValues);
51917c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  if (fieldIDs == nullptr) {
52017c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni      goto exit;
52117c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  }
52217c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni
52317c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  for (size_t i = 0; i< numValues; i++) {
524be392ad35e29b17ed54fdbbbb8dd3e80fc1022b9Yang Ni    fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i];
525be392ad35e29b17ed54fdbbbb8dd3e80fc1022b9Yang Ni  }
526be392ad35e29b17ed54fdbbbb8dd3e80fc1022b9Yang Ni
52717c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  ret = (jlong)(uintptr_t)rsInvokeClosureCreate(
528be392ad35e29b17ed54fdbbbb8dd3e80fc1022b9Yang Ni      (RsContext)con, (RsScriptInvokeID)invokeID, jParams, jParamLength,
529263cc90345784c3f60bf57c0de91afc4d6c3d5dbYang Ni      fieldIDs, numValues, jValues, numValues,
53017c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni      (int*)jSizes, numValues);
53117c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni
53217c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Niexit:
53317c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni
53417c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  _env->ReleaseIntArrayElements (sizeArray,       jSizes,       JNI_ABORT);
53517c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  _env->ReleaseLongArrayElements(valueArray,      jValues,      JNI_ABORT);
53617c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  _env->ReleaseLongArrayElements(fieldIDArray,    jFieldIDs,    JNI_ABORT);
53717c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  _env->ReleaseByteArrayElements(paramArray,      jParams,      JNI_ABORT);
53817c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni
53917c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  return ret;
540be392ad35e29b17ed54fdbbbb8dd3e80fc1022b9Yang Ni}
541be392ad35e29b17ed54fdbbbb8dd3e80fc1022b9Yang Ni
542281c3252510bcf4120d17fa0125f369c2c78f162Yang Nistatic void
543281c3252510bcf4120d17fa0125f369c2c78f162Yang NinClosureSetArg(JNIEnv *_env, jobject _this, jlong con, jlong closureID,
544281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni               jint index, jlong value, jint size) {
545263cc90345784c3f60bf57c0de91afc4d6c3d5dbYang Ni  // Size is signed with -1 indicating the value is an Allocation
546281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni  rsClosureSetArg((RsContext)con, (RsClosure)closureID, (uint32_t)index,
547263cc90345784c3f60bf57c0de91afc4d6c3d5dbYang Ni                  (uintptr_t)value, size);
548281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni}
549281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni
550281c3252510bcf4120d17fa0125f369c2c78f162Yang Nistatic void
551281c3252510bcf4120d17fa0125f369c2c78f162Yang NinClosureSetGlobal(JNIEnv *_env, jobject _this, jlong con, jlong closureID,
552281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni                  jlong fieldID, jlong value, jint size) {
553263cc90345784c3f60bf57c0de91afc4d6c3d5dbYang Ni  // Size is signed with -1 indicating the value is an Allocation
554281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni  rsClosureSetGlobal((RsContext)con, (RsClosure)closureID,
555263cc90345784c3f60bf57c0de91afc4d6c3d5dbYang Ni                     (RsScriptFieldID)fieldID, (int64_t)value, size);
556281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni}
557281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni
558281c3252510bcf4120d17fa0125f369c2c78f162Yang Nistatic long
55935be56cce821d86f69026398ff9433e2d173b02fYang NinScriptGroup2Create(JNIEnv *_env, jobject _this, jlong con, jstring name,
560ebf63408d2b5181aeb7d83adf0f7ab95946a16d4Yang Ni                    jstring cacheDir, jlongArray closureArray) {
56117c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  jlong ret = 0;
56217c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni
56335be56cce821d86f69026398ff9433e2d173b02fYang Ni  AutoJavaStringToUTF8 nameUTF(_env, name);
564ebf63408d2b5181aeb7d83adf0f7ab95946a16d4Yang Ni  AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
565ebf63408d2b5181aeb7d83adf0f7ab95946a16d4Yang Ni
566281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni  jlong* jClosures = _env->GetLongArrayElements(closureArray, nullptr);
567281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni  jsize numClosures = _env->GetArrayLength(closureArray);
568ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang  if (jClosures == nullptr) {
569ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang      ALOGE("Failed to get Java array elements: closures.");
570ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang      return ret;
571ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang  }
57217c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni
57317c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  RsClosure* closures;
57417c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni
5757b2a46fee0f95b9ae2485db36e0d279730623c4aYang Ni  if (numClosures > (jsize) RS_SCRIPT_GROUP_MAX_NUMBER_CLOSURES) {
57617c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni    ALOGE("Too many closures in script group");
57717c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni    goto exit;
57817c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  }
57917c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni
58017c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  closures = (RsClosure*)alloca(sizeof(RsClosure) * numClosures);
58117c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  if (closures == nullptr) {
58217c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni      goto exit;
58317c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  }
58417c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni
585281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni  for (int i = 0; i < numClosures; i++) {
586281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni    closures[i] = (RsClosure)jClosures[i];
587281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni  }
588281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni
58917c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  ret = (jlong)(uintptr_t)rsScriptGroup2Create(
59035be56cce821d86f69026398ff9433e2d173b02fYang Ni      (RsContext)con, nameUTF.c_str(), nameUTF.length(),
59135be56cce821d86f69026398ff9433e2d173b02fYang Ni      cacheDirUTF.c_str(), cacheDirUTF.length(),
592ebf63408d2b5181aeb7d83adf0f7ab95946a16d4Yang Ni      closures, numClosures);
59317c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni
59417c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Niexit:
59517c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni
59617c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  _env->ReleaseLongArrayElements(closureArray, jClosures, JNI_ABORT);
59717c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni
59817c2d7a3e1fac8d8876262255eb24f2f81c8fbacYang Ni  return ret;
599281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni}
600281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni
601281c3252510bcf4120d17fa0125f369c2c78f162Yang Nistatic void
602281c3252510bcf4120d17fa0125f369c2c78f162Yang NinScriptGroup2Execute(JNIEnv *_env, jobject _this, jlong con, jlong groupID) {
603281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni  rsScriptGroupExecute((RsContext)con, (RsScriptGroup2)groupID);
604281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni}
605281c3252510bcf4120d17fa0125f369c2c78f162Yang Ni
60696ed4cfa62dd09aafb3f9da01e047661b4fe3c95Jason Samsstatic void
60725207df658d6a8a3e885c7017fcc25702363583cTim MurraynScriptIntrinsicBLAS_Single(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
60825207df658d6a8a3e885c7017fcc25702363583cTim Murray                            jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
60925207df658d6a8a3e885c7017fcc25702363583cTim Murray                            jfloat alpha, jlong A, jlong B, jfloat beta, jlong C, jint incX, jint incY,
61025207df658d6a8a3e885c7017fcc25702363583cTim Murray                            jint KL, jint KU) {
61125207df658d6a8a3e885c7017fcc25702363583cTim Murray    RsBlasCall call;
61225207df658d6a8a3e885c7017fcc25702363583cTim Murray    memset(&call, 0, sizeof(call));
61325207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.func = (RsBlasFunction)func;
61425207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.transA = (RsBlasTranspose)TransA;
61525207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.transB = (RsBlasTranspose)TransB;
61625207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.side = (RsBlasSide)Side;
61725207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.uplo = (RsBlasUplo)Uplo;
61825207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.diag = (RsBlasDiag)Diag;
61925207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.M = M;
62025207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.N = N;
62125207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.K = K;
62225207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.alpha.f = alpha;
62325207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.beta.f = beta;
62425207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.incX = incX;
62525207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.incY = incY;
62625207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.KL = KL;
62725207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.KU = KU;
62825207df658d6a8a3e885c7017fcc25702363583cTim Murray
62925207df658d6a8a3e885c7017fcc25702363583cTim Murray    RsAllocation in_allocs[3];
63025207df658d6a8a3e885c7017fcc25702363583cTim Murray    in_allocs[0] = (RsAllocation)A;
63125207df658d6a8a3e885c7017fcc25702363583cTim Murray    in_allocs[1] = (RsAllocation)B;
63225207df658d6a8a3e885c7017fcc25702363583cTim Murray    in_allocs[2] = (RsAllocation)C;
63325207df658d6a8a3e885c7017fcc25702363583cTim Murray
63425207df658d6a8a3e885c7017fcc25702363583cTim Murray    rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
63525207df658d6a8a3e885c7017fcc25702363583cTim Murray                         in_allocs, sizeof(in_allocs), nullptr,
63625207df658d6a8a3e885c7017fcc25702363583cTim Murray                         &call, sizeof(call), nullptr, 0);
63725207df658d6a8a3e885c7017fcc25702363583cTim Murray}
63825207df658d6a8a3e885c7017fcc25702363583cTim Murray
63925207df658d6a8a3e885c7017fcc25702363583cTim Murraystatic void
64025207df658d6a8a3e885c7017fcc25702363583cTim MurraynScriptIntrinsicBLAS_Double(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
64125207df658d6a8a3e885c7017fcc25702363583cTim Murray                            jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
64225207df658d6a8a3e885c7017fcc25702363583cTim Murray                            jdouble alpha, jlong A, jlong B, jdouble beta, jlong C, jint incX, jint incY,
64325207df658d6a8a3e885c7017fcc25702363583cTim Murray                            jint KL, jint KU) {
64425207df658d6a8a3e885c7017fcc25702363583cTim Murray    RsBlasCall call;
64525207df658d6a8a3e885c7017fcc25702363583cTim Murray    memset(&call, 0, sizeof(call));
64625207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.func = (RsBlasFunction)func;
64725207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.transA = (RsBlasTranspose)TransA;
64825207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.transB = (RsBlasTranspose)TransB;
64925207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.side = (RsBlasSide)Side;
65025207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.uplo = (RsBlasUplo)Uplo;
65125207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.diag = (RsBlasDiag)Diag;
65225207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.M = M;
65325207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.N = N;
65425207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.K = K;
65525207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.alpha.d = alpha;
65625207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.beta.d = beta;
65725207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.incX = incX;
65825207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.incY = incY;
65925207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.KL = KL;
66025207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.KU = KU;
66125207df658d6a8a3e885c7017fcc25702363583cTim Murray
66225207df658d6a8a3e885c7017fcc25702363583cTim Murray    RsAllocation in_allocs[3];
66325207df658d6a8a3e885c7017fcc25702363583cTim Murray    in_allocs[0] = (RsAllocation)A;
66425207df658d6a8a3e885c7017fcc25702363583cTim Murray    in_allocs[1] = (RsAllocation)B;
66525207df658d6a8a3e885c7017fcc25702363583cTim Murray    in_allocs[2] = (RsAllocation)C;
66625207df658d6a8a3e885c7017fcc25702363583cTim Murray
66725207df658d6a8a3e885c7017fcc25702363583cTim Murray    rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
6680b34f2a3c545cb112a23072c92214e359a8df8c6Miao Wang                         in_allocs, NELEM(in_allocs), nullptr,
66925207df658d6a8a3e885c7017fcc25702363583cTim Murray                         &call, sizeof(call), nullptr, 0);
67025207df658d6a8a3e885c7017fcc25702363583cTim Murray}
67125207df658d6a8a3e885c7017fcc25702363583cTim Murray
67225207df658d6a8a3e885c7017fcc25702363583cTim Murraystatic void
67325207df658d6a8a3e885c7017fcc25702363583cTim MurraynScriptIntrinsicBLAS_Complex(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
67425207df658d6a8a3e885c7017fcc25702363583cTim Murray                             jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
67525207df658d6a8a3e885c7017fcc25702363583cTim Murray                             jfloat alphaX, jfloat alphaY, jlong A, jlong B, jfloat betaX,
67625207df658d6a8a3e885c7017fcc25702363583cTim Murray                             jfloat betaY, jlong C, jint incX, jint incY, jint KL, jint KU) {
67725207df658d6a8a3e885c7017fcc25702363583cTim Murray    RsBlasCall call;
67825207df658d6a8a3e885c7017fcc25702363583cTim Murray    memset(&call, 0, sizeof(call));
67925207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.func = (RsBlasFunction)func;
68025207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.transA = (RsBlasTranspose)TransA;
68125207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.transB = (RsBlasTranspose)TransB;
68225207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.side = (RsBlasSide)Side;
68325207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.uplo = (RsBlasUplo)Uplo;
68425207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.diag = (RsBlasDiag)Diag;
68525207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.M = M;
68625207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.N = N;
68725207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.K = K;
68825207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.alpha.c.r = alphaX;
68925207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.alpha.c.i = alphaY;
69025207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.beta.c.r = betaX;
69182585b3906955e57e199d962b8fe11b2bab87fbaMiao Wang    call.beta.c.i = betaY;
69225207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.incX = incX;
69325207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.incY = incY;
69425207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.KL = KL;
69525207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.KU = KU;
69625207df658d6a8a3e885c7017fcc25702363583cTim Murray
69725207df658d6a8a3e885c7017fcc25702363583cTim Murray    RsAllocation in_allocs[3];
69825207df658d6a8a3e885c7017fcc25702363583cTim Murray    in_allocs[0] = (RsAllocation)A;
69925207df658d6a8a3e885c7017fcc25702363583cTim Murray    in_allocs[1] = (RsAllocation)B;
70025207df658d6a8a3e885c7017fcc25702363583cTim Murray    in_allocs[2] = (RsAllocation)C;
70125207df658d6a8a3e885c7017fcc25702363583cTim Murray
70225207df658d6a8a3e885c7017fcc25702363583cTim Murray    rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
7030b34f2a3c545cb112a23072c92214e359a8df8c6Miao Wang                         in_allocs, NELEM(in_allocs), nullptr,
70425207df658d6a8a3e885c7017fcc25702363583cTim Murray                         &call, sizeof(call), nullptr, 0);
70525207df658d6a8a3e885c7017fcc25702363583cTim Murray}
70625207df658d6a8a3e885c7017fcc25702363583cTim Murray
70725207df658d6a8a3e885c7017fcc25702363583cTim Murraystatic void
70825207df658d6a8a3e885c7017fcc25702363583cTim MurraynScriptIntrinsicBLAS_Z(JNIEnv *_env, jobject _this, jlong con, jlong id, jint func, jint TransA,
70925207df658d6a8a3e885c7017fcc25702363583cTim Murray                       jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
71025207df658d6a8a3e885c7017fcc25702363583cTim Murray                       jdouble alphaX, jdouble alphaY, jlong A, jlong B, jdouble betaX,
71125207df658d6a8a3e885c7017fcc25702363583cTim Murray                       jdouble betaY, jlong C, jint incX, jint incY, jint KL, jint KU) {
71225207df658d6a8a3e885c7017fcc25702363583cTim Murray    RsBlasCall call;
71325207df658d6a8a3e885c7017fcc25702363583cTim Murray    memset(&call, 0, sizeof(call));
71425207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.func = (RsBlasFunction)func;
71525207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.transA = (RsBlasTranspose)TransA;
71625207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.transB = (RsBlasTranspose)TransB;
71725207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.side = (RsBlasSide)Side;
71825207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.uplo = (RsBlasUplo)Uplo;
71925207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.diag = (RsBlasDiag)Diag;
72025207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.M = M;
72125207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.N = N;
72225207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.K = K;
72325207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.alpha.z.r = alphaX;
72425207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.alpha.z.i = alphaY;
72525207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.beta.z.r = betaX;
72682585b3906955e57e199d962b8fe11b2bab87fbaMiao Wang    call.beta.z.i = betaY;
72725207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.incX = incX;
72825207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.incY = incY;
72925207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.KL = KL;
73025207df658d6a8a3e885c7017fcc25702363583cTim Murray    call.KU = KU;
73125207df658d6a8a3e885c7017fcc25702363583cTim Murray
73225207df658d6a8a3e885c7017fcc25702363583cTim Murray    RsAllocation in_allocs[3];
73325207df658d6a8a3e885c7017fcc25702363583cTim Murray    in_allocs[0] = (RsAllocation)A;
73425207df658d6a8a3e885c7017fcc25702363583cTim Murray    in_allocs[1] = (RsAllocation)B;
73525207df658d6a8a3e885c7017fcc25702363583cTim Murray    in_allocs[2] = (RsAllocation)C;
73625207df658d6a8a3e885c7017fcc25702363583cTim Murray
73725207df658d6a8a3e885c7017fcc25702363583cTim Murray    rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
7380b34f2a3c545cb112a23072c92214e359a8df8c6Miao Wang                         in_allocs, NELEM(in_allocs), nullptr,
73925207df658d6a8a3e885c7017fcc25702363583cTim Murray                         &call, sizeof(call), nullptr, 0);
74025207df658d6a8a3e885c7017fcc25702363583cTim Murray}
74125207df658d6a8a3e885c7017fcc25702363583cTim Murray
74225207df658d6a8a3e885c7017fcc25702363583cTim Murray
74325207df658d6a8a3e885c7017fcc25702363583cTim Murraystatic void
7449cb16a2f91c79845e5425fbf55f3b679fb18a4ddTim MurraynScriptIntrinsicBLAS_BNNM(JNIEnv *_env, jobject _this, jlong con, jlong id, jint M, jint N, jint K,
7459cb16a2f91c79845e5425fbf55f3b679fb18a4ddTim Murray                                             jlong A, jint a_offset, jlong B, jint b_offset, jlong C, jint c_offset,
7469cb16a2f91c79845e5425fbf55f3b679fb18a4ddTim Murray                                             jint c_mult_int) {
7479cb16a2f91c79845e5425fbf55f3b679fb18a4ddTim Murray    RsBlasCall call;
7489cb16a2f91c79845e5425fbf55f3b679fb18a4ddTim Murray    memset(&call, 0, sizeof(call));
7499cb16a2f91c79845e5425fbf55f3b679fb18a4ddTim Murray    call.func = RsBlas_bnnm;
7509cb16a2f91c79845e5425fbf55f3b679fb18a4ddTim Murray    call.M = M;
7519cb16a2f91c79845e5425fbf55f3b679fb18a4ddTim Murray    call.N = N;
7529cb16a2f91c79845e5425fbf55f3b679fb18a4ddTim Murray    call.K = K;
7532514806817ec394d334595d76e20f3129117da6eMiao Wang    call.a_offset = a_offset & 0xFF;
7542514806817ec394d334595d76e20f3129117da6eMiao Wang    call.b_offset = b_offset & 0xFF;
7559cb16a2f91c79845e5425fbf55f3b679fb18a4ddTim Murray    call.c_offset = c_offset;
7569cb16a2f91c79845e5425fbf55f3b679fb18a4ddTim Murray    call.c_mult_int = c_mult_int;
7579cb16a2f91c79845e5425fbf55f3b679fb18a4ddTim Murray
7589cb16a2f91c79845e5425fbf55f3b679fb18a4ddTim Murray    RsAllocation in_allocs[3];
7599cb16a2f91c79845e5425fbf55f3b679fb18a4ddTim Murray    in_allocs[0] = (RsAllocation)A;
7609cb16a2f91c79845e5425fbf55f3b679fb18a4ddTim Murray    in_allocs[1] = (RsAllocation)B;
7619cb16a2f91c79845e5425fbf55f3b679fb18a4ddTim Murray    in_allocs[2] = (RsAllocation)C;
7629cb16a2f91c79845e5425fbf55f3b679fb18a4ddTim Murray
7639cb16a2f91c79845e5425fbf55f3b679fb18a4ddTim Murray    rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
7640b34f2a3c545cb112a23072c92214e359a8df8c6Miao Wang                         in_allocs, NELEM(in_allocs), nullptr,
7659cb16a2f91c79845e5425fbf55f3b679fb18a4ddTim Murray                         &call, sizeof(call), nullptr, 0);
7669cb16a2f91c79845e5425fbf55f3b679fb18a4ddTim Murray}
7679cb16a2f91c79845e5425fbf55f3b679fb18a4ddTim Murray
7689cb16a2f91c79845e5425fbf55f3b679fb18a4ddTim Murray
7699cb16a2f91c79845e5425fbf55f3b679fb18a4ddTim Murraystatic void
770460a04971c494fec39ffcb38e873bb8fdd82d113Tim MurraynAssignName(JNIEnv *_env, jobject _this, jlong con, jlong obj, jbyteArray str)
7713eaa338e11a3b0d6b87d705e5bb95625e82347bdJason Sams{
77267333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    if (kLogApi) {
77367333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe        ALOGD("nAssignName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
77467333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    }
7753eaa338e11a3b0d6b87d705e5bb95625e82347bdJason Sams    jint len = _env->GetArrayLength(str);
7763eaa338e11a3b0d6b87d705e5bb95625e82347bdJason Sams    jbyte * cptr = (jbyte *) _env->GetPrimitiveArrayCritical(str, 0);
777ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang    if (cptr == nullptr) {
778ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang        ALOGE("Failed to get Java array elements");
779ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang        return;
780ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang    }
781ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang
782eff663f391fa4f119685d5c14489b94661ea126fTim Murray    rsAssignName((RsContext)con, (void *)obj, (const char *)cptr, len);
7833eaa338e11a3b0d6b87d705e5bb95625e82347bdJason Sams    _env->ReleasePrimitiveArrayCritical(str, cptr, JNI_ABORT);
7843eaa338e11a3b0d6b87d705e5bb95625e82347bdJason Sams}
7853eaa338e11a3b0d6b87d705e5bb95625e82347bdJason Sams
786fb10c16a0528a418053e4b8e75eebe57476b86efAlex Sakhartchoukstatic jstring
787460a04971c494fec39ffcb38e873bb8fdd82d113Tim MurraynGetName(JNIEnv *_env, jobject _this, jlong con, jlong obj)
788fb10c16a0528a418053e4b8e75eebe57476b86efAlex Sakhartchouk{
78967333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    if (kLogApi) {
79067333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe        ALOGD("nGetName, con(%p), obj(%p)", (RsContext)con, (void *)obj);
79167333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    }
792488230c3fec4fc55abc2ee1de7b8b51a4e89f9a5Chris Wailes    const char *name = nullptr;
793eff663f391fa4f119685d5c14489b94661ea126fTim Murray    rsaGetName((RsContext)con, (void *)obj, &name);
794488230c3fec4fc55abc2ee1de7b8b51a4e89f9a5Chris Wailes    if(name == nullptr || strlen(name) == 0) {
795488230c3fec4fc55abc2ee1de7b8b51a4e89f9a5Chris Wailes        return nullptr;
796581cc64028e8b8f66cec8105bf530b16d5fb34ebAlex Sakhartchouk    }
797fb10c16a0528a418053e4b8e75eebe57476b86efAlex Sakhartchouk    return _env->NewStringUTF(name);
798fb10c16a0528a418053e4b8e75eebe57476b86efAlex Sakhartchouk}
799fb10c16a0528a418053e4b8e75eebe57476b86efAlex Sakhartchouk
8007ce033d797e5df5e2131e2ed459fba181eaf4658Jason Samsstatic void
801460a04971c494fec39ffcb38e873bb8fdd82d113Tim MurraynObjDestroy(JNIEnv *_env, jobject _this, jlong con, jlong obj)
8027ce033d797e5df5e2131e2ed459fba181eaf4658Jason Sams{
80367333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    if (kLogApi) {
80467333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe        ALOGD("nObjDestroy, con(%p) obj(%p)", (RsContext)con, (void *)obj);
80567333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    }
806eff663f391fa4f119685d5c14489b94661ea126fTim Murray    rsObjDestroy((RsContext)con, (void *)obj);
8077ce033d797e5df5e2131e2ed459fba181eaf4658Jason Sams}
8087ce033d797e5df5e2131e2ed459fba181eaf4658Jason Sams
8093eaa338e11a3b0d6b87d705e5bb95625e82347bdJason Sams// ---------------------------------------------------------------------------
8103eaa338e11a3b0d6b87d705e5bb95625e82347bdJason Sams
811eff663f391fa4f119685d5c14489b94661ea126fTim Murraystatic jlong
812d19f10d43aa400e1183aa21a97099d02074131a2Jason SamsnDeviceCreate(JNIEnv *_env, jobject _this)
813d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams{
81467333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    if (kLogApi) {
81567333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe        ALOGD("nDeviceCreate");
81667333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    }
8173aa89c1e243a2fae2b9d98fdbad574227c5b5bfaTim Murray    return (jlong)(uintptr_t)rsDeviceCreate();
818d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams}
819d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
820d19f10d43aa400e1183aa21a97099d02074131a2Jason Samsstatic void
8215eaf468434e96bc7c8d4cfeb138699a1c7b256caTim MurraynDeviceDestroy(JNIEnv *_env, jobject _this, jlong dev)
822d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams{
82367333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    if (kLogApi) {
82467333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe        ALOGD("nDeviceDestroy");
82567333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    }
826d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    return rsDeviceDestroy((RsDevice)dev);
827d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams}
828d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
829ebfb436a49673693b98469683451bd9ede797557Jason Samsstatic void
8305eaf468434e96bc7c8d4cfeb138699a1c7b256caTim MurraynDeviceSetConfig(JNIEnv *_env, jobject _this, jlong dev, jint p, jint value)
831ebfb436a49673693b98469683451bd9ede797557Jason Sams{
83267333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    if (kLogApi) {
83367333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe        ALOGD("nDeviceSetConfig  dev(%p), param(%i), value(%i)", (void *)dev, p, value);
83467333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    }
835ebfb436a49673693b98469683451bd9ede797557Jason Sams    return rsDeviceSetConfig((RsDevice)dev, (RsDeviceParam)p, value);
836ebfb436a49673693b98469683451bd9ede797557Jason Sams}
837ebfb436a49673693b98469683451bd9ede797557Jason Sams
838eff663f391fa4f119685d5c14489b94661ea126fTim Murraystatic jlong
83981cd2b18bd36285d1843f78418282e1dad156fbdJason SamsnContextCreate(JNIEnv *_env, jobject _this, jlong dev, jint flags, jint sdkVer, jint contextType)
840d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams{
84167333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    if (kLogApi) {
84267333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe        ALOGD("nContextCreate");
84367333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    }
84481cd2b18bd36285d1843f78418282e1dad156fbdJason Sams    return (jlong)(uintptr_t)rsContextCreate((RsDevice)dev, 0, sdkVer, (RsContextType)contextType, flags);
845704ff64b099406bb328898a7443921f22dbffd6dJason Sams}
846704ff64b099406bb328898a7443921f22dbffd6dJason Sams
847eff663f391fa4f119685d5c14489b94661ea126fTim Murraystatic jlong
8485eaf468434e96bc7c8d4cfeb138699a1c7b256caTim MurraynContextCreateGL(JNIEnv *_env, jobject _this, jlong dev, jint ver, jint sdkVer,
8490e0c0885aed99a119052a792becb5a0c5a93632dAshok Bhat                 jint colorMin, jint colorPref,
8500e0c0885aed99a119052a792becb5a0c5a93632dAshok Bhat                 jint alphaMin, jint alphaPref,
8510e0c0885aed99a119052a792becb5a0c5a93632dAshok Bhat                 jint depthMin, jint depthPref,
8520e0c0885aed99a119052a792becb5a0c5a93632dAshok Bhat                 jint stencilMin, jint stencilPref,
8530e0c0885aed99a119052a792becb5a0c5a93632dAshok Bhat                 jint samplesMin, jint samplesPref, jfloat samplesQ,
8540e0c0885aed99a119052a792becb5a0c5a93632dAshok Bhat                 jint dpi)
85511c8af9ded3a319635b4e91a639a616ec97fc7e3Jason Sams{
85611c8af9ded3a319635b4e91a639a616ec97fc7e3Jason Sams    RsSurfaceConfig sc;
85711c8af9ded3a319635b4e91a639a616ec97fc7e3Jason Sams    sc.alphaMin = alphaMin;
85811c8af9ded3a319635b4e91a639a616ec97fc7e3Jason Sams    sc.alphaPref = alphaPref;
85911c8af9ded3a319635b4e91a639a616ec97fc7e3Jason Sams    sc.colorMin = colorMin;
86011c8af9ded3a319635b4e91a639a616ec97fc7e3Jason Sams    sc.colorPref = colorPref;
86111c8af9ded3a319635b4e91a639a616ec97fc7e3Jason Sams    sc.depthMin = depthMin;
86211c8af9ded3a319635b4e91a639a616ec97fc7e3Jason Sams    sc.depthPref = depthPref;
86311c8af9ded3a319635b4e91a639a616ec97fc7e3Jason Sams    sc.samplesMin = samplesMin;
86411c8af9ded3a319635b4e91a639a616ec97fc7e3Jason Sams    sc.samplesPref = samplesPref;
86511c8af9ded3a319635b4e91a639a616ec97fc7e3Jason Sams    sc.samplesQ = samplesQ;
86611c8af9ded3a319635b4e91a639a616ec97fc7e3Jason Sams
86767333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    if (kLogApi) {
86867333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe        ALOGD("nContextCreateGL");
86967333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    }
8703aa89c1e243a2fae2b9d98fdbad574227c5b5bfaTim Murray    return (jlong)(uintptr_t)rsContextCreateGL((RsDevice)dev, ver, sdkVer, sc, dpi);
871d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams}
872d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
873d19f10d43aa400e1183aa21a97099d02074131a2Jason Samsstatic void
874eff663f391fa4f119685d5c14489b94661ea126fTim MurraynContextSetPriority(JNIEnv *_env, jobject _this, jlong con, jint p)
8757d787b4722eaeb79cab42c36060336e092b77b5fJason Sams{
87667333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    if (kLogApi) {
87767333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe        ALOGD("ContextSetPriority, con(%p), priority(%i)", (RsContext)con, p);
87867333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    }
879eff663f391fa4f119685d5c14489b94661ea126fTim Murray    rsContextSetPriority((RsContext)con, p);
8807d787b4722eaeb79cab42c36060336e092b77b5fJason Sams}
8817d787b4722eaeb79cab42c36060336e092b77b5fJason Sams
88247f31582b67f492d9e1523abd0d8b86c38ec007dTim Murraystatic void
88347f31582b67f492d9e1523abd0d8b86c38ec007dTim MurraynContextSetCacheDir(JNIEnv *_env, jobject _this, jlong con, jstring cacheDir)
88447f31582b67f492d9e1523abd0d8b86c38ec007dTim Murray{
88547f31582b67f492d9e1523abd0d8b86c38ec007dTim Murray    AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
88647f31582b67f492d9e1523abd0d8b86c38ec007dTim Murray
88747f31582b67f492d9e1523abd0d8b86c38ec007dTim Murray    if (kLogApi) {
88847f31582b67f492d9e1523abd0d8b86c38ec007dTim Murray        ALOGD("ContextSetCacheDir, con(%p), cacheDir(%s)", (RsContext)con, cacheDirUTF.c_str());
88947f31582b67f492d9e1523abd0d8b86c38ec007dTim Murray    }
89047f31582b67f492d9e1523abd0d8b86c38ec007dTim Murray    rsContextSetCacheDir((RsContext)con, cacheDirUTF.c_str(), cacheDirUTF.length());
89147f31582b67f492d9e1523abd0d8b86c38ec007dTim Murray}
89247f31582b67f492d9e1523abd0d8b86c38ec007dTim Murray
8937d787b4722eaeb79cab42c36060336e092b77b5fJason Sams
8947d787b4722eaeb79cab42c36060336e092b77b5fJason Sams
8957d787b4722eaeb79cab42c36060336e092b77b5fJason Samsstatic void
896eff663f391fa4f119685d5c14489b94661ea126fTim MurraynContextSetSurface(JNIEnv *_env, jobject _this, jlong con, jint width, jint height, jobject wnd)
897efd9b6fb2e0f31b50db089352118e5daeb268879Jason Sams{
89867333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    if (kLogApi) {
89967333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe        ALOGD("nContextSetSurface, con(%p), width(%i), height(%i), surface(%p)", (RsContext)con,
90067333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe              width, height, (Surface *)wnd);
90167333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    }
902efd9b6fb2e0f31b50db089352118e5daeb268879Jason Sams
903488230c3fec4fc55abc2ee1de7b8b51a4e89f9a5Chris Wailes    ANativeWindow * window = nullptr;
904488230c3fec4fc55abc2ee1de7b8b51a4e89f9a5Chris Wailes    if (wnd == nullptr) {
905efd9b6fb2e0f31b50db089352118e5daeb268879Jason Sams
906efd9b6fb2e0f31b50db089352118e5daeb268879Jason Sams    } else {
90764a55af0ac700baecb0877235eb42caac59a3560Jeff Brown        window = android_view_Surface_getNativeWindow(_env, wnd).get();
908efd9b6fb2e0f31b50db089352118e5daeb268879Jason Sams    }
909efd9b6fb2e0f31b50db089352118e5daeb268879Jason Sams
910eff663f391fa4f119685d5c14489b94661ea126fTim Murray    rsContextSetSurface((RsContext)con, width, height, window);
911efd9b6fb2e0f31b50db089352118e5daeb268879Jason Sams}
912efd9b6fb2e0f31b50db089352118e5daeb268879Jason Sams
913efd9b6fb2e0f31b50db089352118e5daeb268879Jason Samsstatic void
914eff663f391fa4f119685d5c14489b94661ea126fTim MurraynContextDestroy(JNIEnv *_env, jobject _this, jlong con)
915d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams{
91667333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    if (kLogApi) {
91767333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe        ALOGD("nContextDestroy, con(%p)", (RsContext)con);
91867333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    }
919eff663f391fa4f119685d5c14489b94661ea126fTim Murray    rsContextDestroy((RsContext)con);
920d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams}
921d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
922715333b832fb448c32165c7d97d408a3fa43f7cbJason Samsstatic void
923eff663f391fa4f119685d5c14489b94661ea126fTim MurraynContextDump(JNIEnv *_env, jobject _this, jlong con, jint bits)
924715333b832fb448c32165c7d97d408a3fa43f7cbJason Sams{
92567333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    if (kLogApi) {
92667333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe        ALOGD("nContextDump, con(%p)  bits(%i)", (RsContext)con, bits);
92767333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    }
928715333b832fb448c32165c7d97d408a3fa43f7cbJason Sams    rsContextDump((RsContext)con, bits);
929715333b832fb448c32165c7d97d408a3fa43f7cbJason Sams}
930d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
931d19f10d43aa400e1183aa21a97099d02074131a2Jason Samsstatic void
932eff663f391fa4f119685d5c14489b94661ea126fTim MurraynContextPause(JNIEnv *_env, jobject _this, jlong con)
93365e7aa56f56097418d617663683544c25b3988eaJason Sams{
93467333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    if (kLogApi) {
93567333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe        ALOGD("nContextPause, con(%p)", (RsContext)con);
93667333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    }
937eff663f391fa4f119685d5c14489b94661ea126fTim Murray    rsContextPause((RsContext)con);
93865e7aa56f56097418d617663683544c25b3988eaJason Sams}
93965e7aa56f56097418d617663683544c25b3988eaJason Sams
94065e7aa56f56097418d617663683544c25b3988eaJason Samsstatic void
941eff663f391fa4f119685d5c14489b94661ea126fTim MurraynContextResume(JNIEnv *_env, jobject _this, jlong con)
94265e7aa56f56097418d617663683544c25b3988eaJason Sams{
94367333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    if (kLogApi) {
94467333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe        ALOGD("nContextResume, con(%p)", (RsContext)con);
94567333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    }
946eff663f391fa4f119685d5c14489b94661ea126fTim Murray    rsContextResume((RsContext)con);
94765e7aa56f56097418d617663683544c25b3988eaJason Sams}
94865e7aa56f56097418d617663683544c25b3988eaJason Sams
9491c41517124a90fcfdb95dc069fc492c6fcf1ff25Jason Sams
9501c41517124a90fcfdb95dc069fc492c6fcf1ff25Jason Samsstatic jstring
951eff663f391fa4f119685d5c14489b94661ea126fTim MurraynContextGetErrorMessage(JNIEnv *_env, jobject _this, jlong con)
9521c41517124a90fcfdb95dc069fc492c6fcf1ff25Jason Sams{
95367333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    if (kLogApi) {
95467333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe        ALOGD("nContextGetErrorMessage, con(%p)", (RsContext)con);
95567333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    }
9561c41517124a90fcfdb95dc069fc492c6fcf1ff25Jason Sams    char buf[1024];
9571c41517124a90fcfdb95dc069fc492c6fcf1ff25Jason Sams
9581c41517124a90fcfdb95dc069fc492c6fcf1ff25Jason Sams    size_t receiveLen;
9591c41517124a90fcfdb95dc069fc492c6fcf1ff25Jason Sams    uint32_t subID;
960eff663f391fa4f119685d5c14489b94661ea126fTim Murray    int id = rsContextGetMessage((RsContext)con,
96165bdaf1c6d463d9441125d2c87a36015bfef2d95Jason Sams                                 buf, sizeof(buf),
96265bdaf1c6d463d9441125d2c87a36015bfef2d95Jason Sams                                 &receiveLen, sizeof(receiveLen),
963edbfabdb98a2974f973d6c042e6efd547dc02fc5Jason Sams                                 &subID, sizeof(subID));
9641c41517124a90fcfdb95dc069fc492c6fcf1ff25Jason Sams    if (!id && receiveLen) {
96509993f768bcb6dd9ed76b5c655da2ad9d0ad8396Bernhard Rosenkränzer        ALOGV("message receive buffer too small.  %zu", receiveLen);
9661c41517124a90fcfdb95dc069fc492c6fcf1ff25Jason Sams    }
9671c41517124a90fcfdb95dc069fc492c6fcf1ff25Jason Sams    return _env->NewStringUTF(buf);
9681c41517124a90fcfdb95dc069fc492c6fcf1ff25Jason Sams}
9691c41517124a90fcfdb95dc069fc492c6fcf1ff25Jason Sams
970edbfabdb98a2974f973d6c042e6efd547dc02fc5Jason Samsstatic jint
971eff663f391fa4f119685d5c14489b94661ea126fTim MurraynContextGetUserMessage(JNIEnv *_env, jobject _this, jlong con, jintArray data)
972516c31911578db8ce53529483c3ded918ac7dc6bJason Sams{
973516c31911578db8ce53529483c3ded918ac7dc6bJason Sams    jint len = _env->GetArrayLength(data);
97467333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    if (kLogApi) {
97567333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe        ALOGD("nContextGetMessage, con(%p), len(%i)", (RsContext)con, len);
97667333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    }
977488230c3fec4fc55abc2ee1de7b8b51a4e89f9a5Chris Wailes    jint *ptr = _env->GetIntArrayElements(data, nullptr);
978ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang    if (ptr == nullptr) {
979ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang        ALOGE("Failed to get Java array elements");
980ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang        return 0;
981ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang    }
982516c31911578db8ce53529483c3ded918ac7dc6bJason Sams    size_t receiveLen;
9831c41517124a90fcfdb95dc069fc492c6fcf1ff25Jason Sams    uint32_t subID;
984eff663f391fa4f119685d5c14489b94661ea126fTim Murray    int id = rsContextGetMessage((RsContext)con,
98565bdaf1c6d463d9441125d2c87a36015bfef2d95Jason Sams                                 ptr, len * 4,
98665bdaf1c6d463d9441125d2c87a36015bfef2d95Jason Sams                                 &receiveLen, sizeof(receiveLen),
987edbfabdb98a2974f973d6c042e6efd547dc02fc5Jason Sams                                 &subID, sizeof(subID));
988516c31911578db8ce53529483c3ded918ac7dc6bJason Sams    if (!id && receiveLen) {
98909993f768bcb6dd9ed76b5c655da2ad9d0ad8396Bernhard Rosenkränzer        ALOGV("message receive buffer too small.  %zu", receiveLen);
990516c31911578db8ce53529483c3ded918ac7dc6bJason Sams    }
991516c31911578db8ce53529483c3ded918ac7dc6bJason Sams    _env->ReleaseIntArrayElements(data, ptr, 0);
9920e0c0885aed99a119052a792becb5a0c5a93632dAshok Bhat    return (jint)id;
9931c41517124a90fcfdb95dc069fc492c6fcf1ff25Jason Sams}
9941c41517124a90fcfdb95dc069fc492c6fcf1ff25Jason Sams
9951c41517124a90fcfdb95dc069fc492c6fcf1ff25Jason Samsstatic jint
996eff663f391fa4f119685d5c14489b94661ea126fTim MurraynContextPeekMessage(JNIEnv *_env, jobject _this, jlong con, jintArray auxData)
9971c41517124a90fcfdb95dc069fc492c6fcf1ff25Jason Sams{
99867333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    if (kLogApi) {
99967333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe        ALOGD("nContextPeekMessage, con(%p)", (RsContext)con);
100067333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    }
1001488230c3fec4fc55abc2ee1de7b8b51a4e89f9a5Chris Wailes    jint *auxDataPtr = _env->GetIntArrayElements(auxData, nullptr);
1002ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang    if (auxDataPtr == nullptr) {
1003ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang        ALOGE("Failed to get Java array elements");
1004ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang        return 0;
1005ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang    }
10061c41517124a90fcfdb95dc069fc492c6fcf1ff25Jason Sams    size_t receiveLen;
10071c41517124a90fcfdb95dc069fc492c6fcf1ff25Jason Sams    uint32_t subID;
1008eff663f391fa4f119685d5c14489b94661ea126fTim Murray    int id = rsContextPeekMessage((RsContext)con, &receiveLen, sizeof(receiveLen),
1009edbfabdb98a2974f973d6c042e6efd547dc02fc5Jason Sams                                  &subID, sizeof(subID));
10101c41517124a90fcfdb95dc069fc492c6fcf1ff25Jason Sams    auxDataPtr[0] = (jint)subID;
10111c41517124a90fcfdb95dc069fc492c6fcf1ff25Jason Sams    auxDataPtr[1] = (jint)receiveLen;
10121c41517124a90fcfdb95dc069fc492c6fcf1ff25Jason Sams    _env->ReleaseIntArrayElements(auxData, auxDataPtr, 0);
10130e0c0885aed99a119052a792becb5a0c5a93632dAshok Bhat    return (jint)id;
1014516c31911578db8ce53529483c3ded918ac7dc6bJason Sams}
1015516c31911578db8ce53529483c3ded918ac7dc6bJason Sams
1016eff663f391fa4f119685d5c14489b94661ea126fTim Murraystatic void nContextInitToClient(JNIEnv *_env, jobject _this, jlong con)
1017516c31911578db8ce53529483c3ded918ac7dc6bJason Sams{
101867333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    if (kLogApi) {
101967333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe        ALOGD("nContextInitToClient, con(%p)", (RsContext)con);
102067333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    }
1021eff663f391fa4f119685d5c14489b94661ea126fTim Murray    rsContextInitToClient((RsContext)con);
1022516c31911578db8ce53529483c3ded918ac7dc6bJason Sams}
1023516c31911578db8ce53529483c3ded918ac7dc6bJason Sams
1024eff663f391fa4f119685d5c14489b94661ea126fTim Murraystatic void nContextDeinitToClient(JNIEnv *_env, jobject _this, jlong con)
1025516c31911578db8ce53529483c3ded918ac7dc6bJason Sams{
102667333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    if (kLogApi) {
102767333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe        ALOGD("nContextDeinitToClient, con(%p)", (RsContext)con);
102867333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    }
1029eff663f391fa4f119685d5c14489b94661ea126fTim Murray    rsContextDeinitToClient((RsContext)con);
1030516c31911578db8ce53529483c3ded918ac7dc6bJason Sams}
1031516c31911578db8ce53529483c3ded918ac7dc6bJason Sams
1032455d64426615e5d269a3bd05cd91b67c3811fcdfJason Samsstatic void
1033eff663f391fa4f119685d5c14489b94661ea126fTim MurraynContextSendMessage(JNIEnv *_env, jobject _this, jlong con, jint id, jintArray data)
1034455d64426615e5d269a3bd05cd91b67c3811fcdfJason Sams{
1035488230c3fec4fc55abc2ee1de7b8b51a4e89f9a5Chris Wailes    jint *ptr = nullptr;
1036455d64426615e5d269a3bd05cd91b67c3811fcdfJason Sams    jint len = 0;
1037455d64426615e5d269a3bd05cd91b67c3811fcdfJason Sams    if (data) {
1038455d64426615e5d269a3bd05cd91b67c3811fcdfJason Sams        len = _env->GetArrayLength(data);
10394a043c1853c117c1b4c4342e76cd16a5abdc1045Stephen Hines        ptr = _env->GetIntArrayElements(data, nullptr);
1040ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang        if (ptr == nullptr) {
1041ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang            ALOGE("Failed to get Java array elements");
1042ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang            return;
1043ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang        }
1044455d64426615e5d269a3bd05cd91b67c3811fcdfJason Sams    }
104567333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    if (kLogApi) {
104667333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe        ALOGD("nContextSendMessage, con(%p), id(%i), len(%i)", (RsContext)con, id, len);
104767333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    }
1048eff663f391fa4f119685d5c14489b94661ea126fTim Murray    rsContextSendMessage((RsContext)con, id, (const uint8_t *)ptr, len * sizeof(int));
1049455d64426615e5d269a3bd05cd91b67c3811fcdfJason Sams    if (data) {
1050455d64426615e5d269a3bd05cd91b67c3811fcdfJason Sams        _env->ReleaseIntArrayElements(data, ptr, JNI_ABORT);
1051455d64426615e5d269a3bd05cd91b67c3811fcdfJason Sams    }
1052455d64426615e5d269a3bd05cd91b67c3811fcdfJason Sams}
1053455d64426615e5d269a3bd05cd91b67c3811fcdfJason Sams
1054455d64426615e5d269a3bd05cd91b67c3811fcdfJason Sams
1055516c31911578db8ce53529483c3ded918ac7dc6bJason Sams
1056460a04971c494fec39ffcb38e873bb8fdd82d113Tim Murraystatic jlong
105767333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas GampenElementCreate(JNIEnv *_env, jobject _this, jlong con, jlong type, jint kind, jboolean norm,
105867333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe               jint size)
1059d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams{
106067333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    if (kLogApi) {
106109993f768bcb6dd9ed76b5c655da2ad9d0ad8396Bernhard Rosenkränzer        ALOGD("nElementCreate, con(%p), type(%" PRId64 "), kind(%i), norm(%i), size(%i)", (RsContext)con,
106267333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe              type, kind, norm, size);
106367333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    }
106467333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    return (jlong)(uintptr_t)rsElementCreate((RsContext)con, (RsDataType)type, (RsDataKind)kind,
10653f3965ec13c2dd22d0bc5c37c2d189c7c10c87e5Yang Ni                                             norm, size);
1066d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams}
1067d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
1068460a04971c494fec39ffcb38e873bb8fdd82d113Tim Murraystatic jlong
1069eff663f391fa4f119685d5c14489b94661ea126fTim MurraynElementCreate2(JNIEnv *_env, jobject _this, jlong con,
10709807155b11a25fb6068edc9b1cd82928ac2f05deAshok Bhat                jlongArray _ids, jobjectArray _names, jintArray _arraySizes)
1071d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams{
1072718cd1f322ee5b62b6a49cb36195bcb18a5ab711Jason Sams    int fieldCount = _env->GetArrayLength(_ids);
107367333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    if (kLogApi) {
107467333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe        ALOGD("nElementCreate2, con(%p)", (RsContext)con);
107567333928bfea7d53d8bc1b83599532f6fe5e2b2cAndreas Gampe    }
1076718cd1f322ee5b62b6a49cb36195bcb18a5ab711Jason Sams
1077488230c3fec4fc55abc2ee1de7b8b51a4e89f9a5Chris Wailes    jlong *jIds = _env->GetLongArrayElements(_ids, nullptr);
1078ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang    if (jIds == nullptr) {
1079ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang        ALOGE("Failed to get Java array elements: ids");
1080ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang        return 0;
1081ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang    }
1082488230c3fec4fc55abc2ee1de7b8b51a4e89f9a5Chris Wailes    jint *jArraySizes = _env->GetIntArrayElements(_arraySizes, nullptr);
1083ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang    if (jArraySizes == nullptr) {
1084ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang        ALOGE("Failed to get Java array elements: arraySizes");
1085ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang        return 0;
1086ba8766c5db4b2c35f3461abf221e723e3bbbfa99Miao Wang    }
10879807155b11a25fb6068edc9b1cd82928ac2f05deAshok Bhat
10889807155b11a25fb6068edc9b1cd82928ac2f05deAshok Bhat    RsElement *ids = (RsElement*)malloc(fieldCount * sizeof(RsElement));
10899807155b11a25fb6068edc9b1cd82928ac2f05deAshok Bhat    uint32_t *arraySizes = (uint32_t *)malloc(fieldCount * sizeof(uint32_t));
10909807155b11a25fb6068edc9b1cd82928ac2f05deAshok Bhat
10919807155b11a25fb6068edc9b1cd82928ac2f05deAshok Bhat    for(int i = 0; i < fieldCount; i ++) {
10929807155b11a25fb6068edc9b1cd82928ac2f05deAshok Bhat        ids[i] = (RsElement)jIds[i];
10939807155b11a25fb6068edc9b1cd82928ac2f05deAshok Bhat        arraySizes[i] = (uint32_t)jArraySizes[i];
10949807155b11a25fb6068edc9b1cd82928ac2f05deAshok Bhat    }
1095718cd1f322ee5b62b6a49cb36195bcb18a5ab711Jason Sams
10962123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk    AutoJavaStringArrayToUTF8 names(_env, _names, fieldCount);
10972123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk
10982123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk    const char **nameArray = names.c_str();
10992123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk    size_t *sizeArray = names.c_str_len();
11002123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk
11013aa89c1e243a2fae2b9d98fdbad574227c5b5bfaTim Murray    jlong id = (jlong)(uintptr_t)rsElementCreate2((RsContext)con,
11029807155b11a25fb6068edc9b1cd82928ac2f05deAshok Bhat                                     (const RsElement *)ids, fieldCount,
11037a22e107d100caea2a661ec73e4525d69f0f9759Jason Sams                                     nameArray, fieldCount * sizeof(size_t),  sizeArray,
11043f3965ec13c2dd22d0bc5c37c2d189c7c10c87e5Yang Ni                                     (const uint32_t *)arraySizes, fieldCount);
11052123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk
11069807155b11a25fb6068edc9b1cd82928ac2f05deAshok Bhat    free(ids);
11079807155b11a25fb6068edc9b1cd82928ac2f05deAshok Bhat    free(arraySizes);
11089807155b11a25fb6068edc9b1cd82928ac2f05deAshok Bhat    _env->ReleaseLongArrayElements(_ids, jIds, JNI_ABORT);
11099807155b11a25fb6068edc9b1cd82928ac2f05deAshok Bhat    _env->ReleaseIntArrayElements(_arraySizes, jArraySizes, JNI_ABORT);
11109807155b11a25fb6068edc9b1cd82928ac2f05deAshok Bhat
11113aa89c1e243a2fae2b9d98fdbad574227c5b5bfaTim Murray    return (jlong)(uintptr_t)id;
1112d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams}
1113d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
1114dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchoukstatic void
1115460a04971c494fec39ffcb38e873bb8fdd82d113Tim MurraynElementGetNativeData(JNIEnv *_env, jobject _this, jlong con, jlong id, jintArray _elementData)
1116dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk{
1117dfac814c18f73dd7289f