1c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main/*
2c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main * Copyright (C) 2011-2012 The Android Open Source Project
3c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main *
4c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main * Licensed under the Apache License, Version 2.0 (the "License");
5c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main * you may not use this file except in compliance with the License.
6c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main * You may obtain a copy of the License at
7c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main *
8c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main *      http://www.apache.org/licenses/LICENSE-2.0
9c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main *
10c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main * Unless required by applicable law or agreed to in writing, software
11c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main * distributed under the License is distributed on an "AS IS" BASIS,
12c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main * See the License for the specific language governing permissions and
14c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main * limitations under the License.
15c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main */
16c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main
17c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main#define LOG_TAG "libRS_jni"
18c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main
19c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main#include <stdlib.h>
20c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main#include <stdio.h>
21c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main#include <fcntl.h>
22c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main#include <unistd.h>
23c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main#include <math.h>
24c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main#include <android/bitmap.h>
25257cecb5e24c9af71ba4d621887e88e46b0ac411Stephen Hines#include <android/log.h>
26c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main
27dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang#include <rsEnv.h>
28470b3b5b70ec71042d30004747c625ca76c4007aMiao Wang#include "rsDispatch.h"
29470b3b5b70ec71042d30004747c625ca76c4007aMiao Wang#include <dlfcn.h>
30470b3b5b70ec71042d30004747c625ca76c4007aMiao Wang
31c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main//#define LOG_API ALOG
32c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main#define LOG_API(...)
33d3fdb27e85cd38ffacd963bced1f91a9297a68d8Miao Wang#define LOG_ERR(...) __android_log_print(ANDROID_LOG_ERROR, "RenderScript JNI", __VA_ARGS__);
34d3fdb27e85cd38ffacd963bced1f91a9297a68d8Miao Wang#define RS_JNI_VERSION 2301
35c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main
36257cecb5e24c9af71ba4d621887e88e46b0ac411Stephen Hines#define NELEM(m) (sizeof(m) / sizeof((m)[0]))
37c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main
388352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wangtemplate <typename... T>
398352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wangvoid UNUSED(T... t) {}
408352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang#define PER_ARRAY_TYPE(flag, fnc, readonly, ...) {                                      \
418352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    jint len = 0;                                                                       \
428352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    void *ptr = nullptr;                                                                \
432e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang    void *srcPtr = nullptr;                                                             \
448352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    size_t typeBytes = 0;                                                               \
458352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    jint relFlag = 0;                                                                   \
468352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    if (readonly) {                                                                     \
478352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        /* The on-release mode should only be JNI_ABORT for read-only accesses. */      \
482e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        /* readonly = true, also indicates we are copying to the allocation   . */      \
498352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        relFlag = JNI_ABORT;                                                            \
508352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    }                                                                                   \
518352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    switch(dataType) {                                                                  \
528352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    case RS_TYPE_FLOAT_32:                                                              \
538352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        len = _env->GetArrayLength((jfloatArray)data);                                  \
548352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        ptr = _env->GetFloatArrayElements((jfloatArray)data, flag);                     \
558352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        typeBytes = 4;                                                                  \
562e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        if (usePadding) {                                                               \
572e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            srcPtr = ptr;                                                               \
582e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            len = len / 3 * 4;                                                          \
592e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            if (count == 0) {                                                           \
602e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                count = len / 4;                                                        \
612e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            }                                                                           \
622e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            ptr = malloc (len * typeBytes);                                             \
632e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            if (readonly) {                                                             \
642e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                copyWithPadding(ptr, srcPtr, mSize, count);                             \
652e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                fnc(__VA_ARGS__);                                                       \
662e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            } else {                                                                    \
672e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                fnc(__VA_ARGS__);                                                       \
682e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                copyWithUnPadding(srcPtr, ptr, mSize, count);                           \
692e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            }                                                                           \
702e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            free(ptr);                                                                  \
712e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            ptr = srcPtr;                                                               \
722e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        } else {                                                                        \
732e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            fnc(__VA_ARGS__);                                                           \
742e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        }                                                                               \
758352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        _env->ReleaseFloatArrayElements((jfloatArray)data, (jfloat *)ptr, relFlag);     \
768352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        return;                                                                         \
778352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    case RS_TYPE_FLOAT_64:                                                              \
788352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        len = _env->GetArrayLength((jdoubleArray)data);                                 \
798352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        ptr = _env->GetDoubleArrayElements((jdoubleArray)data, flag);                   \
808352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        typeBytes = 8;                                                                  \
812e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        if (usePadding) {                                                               \
822e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            srcPtr = ptr;                                                               \
832e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            len = len / 3 * 4;                                                          \
842e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            if (count == 0) {                                                           \
852e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                count = len / 4;                                                        \
862e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            }                                                                           \
872e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            ptr = malloc (len * typeBytes);                                             \
882e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            if (readonly) {                                                             \
892e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                copyWithPadding(ptr, srcPtr, mSize, count);                             \
902e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                fnc(__VA_ARGS__);                                                       \
912e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            } else {                                                                    \
922e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                fnc(__VA_ARGS__);                                                       \
932e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                copyWithUnPadding(srcPtr, ptr, mSize, count);                           \
942e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            }                                                                           \
952e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            free(ptr);                                                                  \
962e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            ptr = srcPtr;                                                               \
972e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        } else {                                                                        \
982e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            fnc(__VA_ARGS__);                                                           \
992e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        }                                                                               \
1008352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        _env->ReleaseDoubleArrayElements((jdoubleArray)data, (jdouble *)ptr, relFlag);  \
1018352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        return;                                                                         \
1028352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    case RS_TYPE_SIGNED_8:                                                              \
1038352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    case RS_TYPE_UNSIGNED_8:                                                            \
1048352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        len = _env->GetArrayLength((jbyteArray)data);                                   \
1058352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        ptr = _env->GetByteArrayElements((jbyteArray)data, flag);                       \
1068352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        typeBytes = 1;                                                                  \
1072e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        if (usePadding) {                                                               \
1082e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            srcPtr = ptr;                                                               \
1092e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            len = len / 3 * 4;                                                          \
1102e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            if (count == 0) {                                                           \
1112e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                count = len / 4;                                                        \
1122e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            }                                                                           \
1132e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            ptr = malloc (len * typeBytes);                                             \
1142e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            if (readonly) {                                                             \
1152e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                copyWithPadding(ptr, srcPtr, mSize, count);                             \
1162e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                fnc(__VA_ARGS__);                                                       \
1172e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            } else {                                                                    \
1182e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                fnc(__VA_ARGS__);                                                       \
1192e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                copyWithUnPadding(srcPtr, ptr, mSize, count);                           \
1202e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            }                                                                           \
1212e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            free(ptr);                                                                  \
1222e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            ptr = srcPtr;                                                               \
1232e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        } else {                                                                        \
1242e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            fnc(__VA_ARGS__);                                                           \
1252e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        }                                                                               \
1268352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        _env->ReleaseByteArrayElements((jbyteArray)data, (jbyte*)ptr, relFlag);         \
1278352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        return;                                                                         \
1288352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    case RS_TYPE_SIGNED_16:                                                             \
1298352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    case RS_TYPE_UNSIGNED_16:                                                           \
1308352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        len = _env->GetArrayLength((jshortArray)data);                                  \
1318352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        ptr = _env->GetShortArrayElements((jshortArray)data, flag);                     \
1328352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        typeBytes = 2;                                                                  \
1332e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        if (usePadding) {                                                               \
1342e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            srcPtr = ptr;                                                               \
1352e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            len = len / 3 * 4;                                                          \
1362e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            if (count == 0) {                                                           \
1372e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                count = len / 4;                                                        \
1382e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            }                                                                           \
1392e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            ptr = malloc (len * typeBytes);                                             \
1402e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            if (readonly) {                                                             \
1412e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                copyWithPadding(ptr, srcPtr, mSize, count);                             \
1422e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                fnc(__VA_ARGS__);                                                       \
1432e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            } else {                                                                    \
1442e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                fnc(__VA_ARGS__);                                                       \
1452e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                copyWithUnPadding(srcPtr, ptr, mSize, count);                           \
1462e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            }                                                                           \
1472e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            free(ptr);                                                                  \
1482e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            ptr = srcPtr;                                                               \
1492e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        } else {                                                                        \
1502e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            fnc(__VA_ARGS__);                                                           \
1512e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        }                                                                               \
1528352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        _env->ReleaseShortArrayElements((jshortArray)data, (jshort *)ptr, relFlag);     \
1538352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        return;                                                                         \
1548352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    case RS_TYPE_SIGNED_32:                                                             \
1558352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    case RS_TYPE_UNSIGNED_32:                                                           \
1568352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        len = _env->GetArrayLength((jintArray)data);                                    \
1578352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        ptr = _env->GetIntArrayElements((jintArray)data, flag);                         \
1588352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        typeBytes = 4;                                                                  \
1592e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        if (usePadding) {                                                               \
1602e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            srcPtr = ptr;                                                               \
1612e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            len = len / 3 * 4;                                                          \
1622e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            if (count == 0) {                                                           \
1632e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                count = len / 4;                                                        \
1642e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            }                                                                           \
1652e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            ptr = malloc (len * typeBytes);                                             \
1662e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            if (readonly) {                                                             \
1672e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                copyWithPadding(ptr, srcPtr, mSize, count);                             \
1682e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                fnc(__VA_ARGS__);                                                       \
1692e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            } else {                                                                    \
1702e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                fnc(__VA_ARGS__);                                                       \
1712e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                copyWithUnPadding(srcPtr, ptr, mSize, count);                           \
1722e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            }                                                                           \
1732e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            free(ptr);                                                                  \
1742e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            ptr = srcPtr;                                                               \
1752e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        } else {                                                                        \
1762e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            fnc(__VA_ARGS__);                                                           \
1772e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        }                                                                               \
1788352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        _env->ReleaseIntArrayElements((jintArray)data, (jint *)ptr, relFlag);           \
1798352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        return;                                                                         \
1808352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    case RS_TYPE_SIGNED_64:                                                             \
1818352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    case RS_TYPE_UNSIGNED_64:                                                           \
1828352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        len = _env->GetArrayLength((jlongArray)data);                                   \
1838352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        ptr = _env->GetLongArrayElements((jlongArray)data, flag);                       \
1848352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        typeBytes = 8;                                                                  \
1852e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        if (usePadding) {                                                               \
1862e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            srcPtr = ptr;                                                               \
1872e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            len = len / 3 * 4;                                                          \
1882e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            if (count == 0) {                                                           \
1892e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                count = len / 4;                                                        \
1902e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            }                                                                           \
1912e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            ptr = malloc (len * typeBytes);                                             \
1922e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            if (readonly) {                                                             \
1932e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                copyWithPadding(ptr, srcPtr, mSize, count);                             \
1942e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                fnc(__VA_ARGS__);                                                       \
1952e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            } else {                                                                    \
1962e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                fnc(__VA_ARGS__);                                                       \
1972e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                copyWithUnPadding(srcPtr, ptr, mSize, count);                           \
1982e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            }                                                                           \
1992e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            free(ptr);                                                                  \
2002e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            ptr = srcPtr;                                                               \
2012e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        } else {                                                                        \
2022e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            fnc(__VA_ARGS__);                                                           \
2032e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        }                                                                               \
2048352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        _env->ReleaseLongArrayElements((jlongArray)data, (jlong *)ptr, relFlag);        \
2058352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        return;                                                                         \
2068352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    default:                                                                            \
2078352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        break;                                                                          \
2088352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    }                                                                                   \
2092e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang    UNUSED(len, ptr, srcPtr, typeBytes, relFlag);                                       \
2108352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang}
2118352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
2128352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
213c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Mainclass AutoJavaStringToUTF8 {
214c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Mainpublic:
215c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main    AutoJavaStringToUTF8(JNIEnv* env, jstring str) : fEnv(env), fJStr(str) {
216c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main        fCStr = env->GetStringUTFChars(str, NULL);
217c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main        fLength = env->GetStringUTFLength(str);
218c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main    }
219c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main    ~AutoJavaStringToUTF8() {
220c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main        fEnv->ReleaseStringUTFChars(fJStr, fCStr);
221c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main    }
222c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main    const char* c_str() const { return fCStr; }
223c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main    jsize length() const { return fLength; }
224c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main
225c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Mainprivate:
226c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main    JNIEnv*     fEnv;
227c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main    jstring     fJStr;
228c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main    const char* fCStr;
229c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main    jsize       fLength;
230c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main};
231c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main
232c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Mainclass AutoJavaStringArrayToUTF8 {
233c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Mainpublic:
234c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main    AutoJavaStringArrayToUTF8(JNIEnv* env, jobjectArray strings, jsize stringsLength)
235c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main    : mEnv(env), mStrings(strings), mStringsLength(stringsLength) {
236c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main        mCStrings = NULL;
237c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main        mSizeArray = NULL;
238c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main        if (stringsLength > 0) {
239c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main            mCStrings = (const char **)calloc(stringsLength, sizeof(char *));
240c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main            mSizeArray = (size_t*)calloc(stringsLength, sizeof(size_t));
241c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main            for (jsize ct = 0; ct < stringsLength; ct ++) {
242c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main                jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
243c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main                mCStrings[ct] = mEnv->GetStringUTFChars(s, NULL);
244c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main                mSizeArray[ct] = mEnv->GetStringUTFLength(s);
245c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main            }
246c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main        }
247c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main    }
248c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main    ~AutoJavaStringArrayToUTF8() {
249c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main        for (jsize ct=0; ct < mStringsLength; ct++) {
250c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main            jstring s = (jstring)mEnv->GetObjectArrayElement(mStrings, ct);
251c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main            mEnv->ReleaseStringUTFChars(s, mCStrings[ct]);
252c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main        }
253c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main        free(mCStrings);
254c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main        free(mSizeArray);
255c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main    }
256c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main    const char **c_str() const { return mCStrings; }
257c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main    size_t *c_str_len() const { return mSizeArray; }
258c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main    jsize length() const { return mStringsLength; }
259c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main
260c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Mainprivate:
261c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main    JNIEnv      *mEnv;
262c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main    jobjectArray mStrings;
263c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main    const char **mCStrings;
264c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main    size_t      *mSizeArray;
265c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main    jsize        mStringsLength;
266c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main};
267c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main
268470b3b5b70ec71042d30004747c625ca76c4007aMiao Wang
269470b3b5b70ec71042d30004747c625ca76c4007aMiao Wang// ---------------------------------------------------------------------------
270470b3b5b70ec71042d30004747c625ca76c4007aMiao Wangstatic dispatchTable dispatchTab;
2716f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang// Incremental Support lib
2726f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wangstatic dispatchTable dispatchTabInc;
273470b3b5b70ec71042d30004747c625ca76c4007aMiao Wang
274d3fdb27e85cd38ffacd963bced1f91a9297a68d8Miao Wangstatic jboolean nLoadSO(JNIEnv *_env, jobject _this, jboolean useNative, jint targetApi, jstring libPath) {
275470b3b5b70ec71042d30004747c625ca76c4007aMiao Wang    void* handle = NULL;
276470b3b5b70ec71042d30004747c625ca76c4007aMiao Wang    if (useNative) {
277470b3b5b70ec71042d30004747c625ca76c4007aMiao Wang        handle = dlopen("libRS.so", RTLD_LAZY | RTLD_LOCAL);
278470b3b5b70ec71042d30004747c625ca76c4007aMiao Wang    } else {
279d3fdb27e85cd38ffacd963bced1f91a9297a68d8Miao Wang        // For API 9+, dlopen the full path of libRSSupport.
280d3fdb27e85cd38ffacd963bced1f91a9297a68d8Miao Wang        if (libPath != NULL) {
281d3fdb27e85cd38ffacd963bced1f91a9297a68d8Miao Wang            const char * libPathJni = _env->GetStringUTFChars(libPath, JNI_FALSE);
282d3fdb27e85cd38ffacd963bced1f91a9297a68d8Miao Wang            handle = dlopen(libPathJni, RTLD_LAZY | RTLD_LOCAL);
283d3fdb27e85cd38ffacd963bced1f91a9297a68d8Miao Wang            _env->ReleaseStringUTFChars(libPath, libPathJni);
284d3fdb27e85cd38ffacd963bced1f91a9297a68d8Miao Wang        } else {
285d3fdb27e85cd38ffacd963bced1f91a9297a68d8Miao Wang            handle = dlopen("libRSSupport.so", RTLD_LAZY | RTLD_LOCAL);
286d3fdb27e85cd38ffacd963bced1f91a9297a68d8Miao Wang        }
287470b3b5b70ec71042d30004747c625ca76c4007aMiao Wang    }
288470b3b5b70ec71042d30004747c625ca76c4007aMiao Wang    if (handle == NULL) {
289d3fdb27e85cd38ffacd963bced1f91a9297a68d8Miao Wang        LOG_ERR("couldn't dlopen %s; librsjni version: %d", dlerror(), RS_JNI_VERSION);
290470b3b5b70ec71042d30004747c625ca76c4007aMiao Wang        return false;
291470b3b5b70ec71042d30004747c625ca76c4007aMiao Wang    }
292470b3b5b70ec71042d30004747c625ca76c4007aMiao Wang
293e27a340e3a55a3650e1f74bf1d350104596c4d4bMiao Wang    if (loadSymbols(handle, dispatchTab, targetApi) == false) {
294d3fdb27e85cd38ffacd963bced1f91a9297a68d8Miao Wang        LOG_ERR("Dispatch table init failed! librsjni version: %d", RS_JNI_VERSION);
295d3fdb27e85cd38ffacd963bced1f91a9297a68d8Miao Wang        dlclose(handle);
296470b3b5b70ec71042d30004747c625ca76c4007aMiao Wang        return false;
297470b3b5b70ec71042d30004747c625ca76c4007aMiao Wang    }
298d3fdb27e85cd38ffacd963bced1f91a9297a68d8Miao Wang    LOG_API("Successfully loaded runtime");
299470b3b5b70ec71042d30004747c625ca76c4007aMiao Wang    return true;
300470b3b5b70ec71042d30004747c625ca76c4007aMiao Wang}
301470b3b5b70ec71042d30004747c625ca76c4007aMiao Wang
302dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wangstatic ioSuppDT ioDispatch;
303dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wangstatic jboolean nLoadIOSO(JNIEnv *_env, jobject _this) {
304dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang    void* handleIO = NULL;
305dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang    handleIO = dlopen("libRSSupportIO.so", RTLD_LAZY | RTLD_LOCAL);
306dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang    if (handleIO == NULL) {
307d3fdb27e85cd38ffacd963bced1f91a9297a68d8Miao Wang        LOG_ERR("Couldn't load libRSSupportIO.so, librsjni version: %d", RS_JNI_VERSION);
308dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang        return false;
309dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang    }
310dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang    if (loadIOSuppSyms(handleIO, ioDispatch) == false) {
311d3fdb27e85cd38ffacd963bced1f91a9297a68d8Miao Wang        LOG_ERR("libRSSupportIO init failed! librsjni version: %d", RS_JNI_VERSION);
312dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang        return false;
313dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang    }
314dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang    return true;
315dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang}
316ecccf940f72679f7602c52b0b4bcb245acdad55bMiao Wang
317c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main// ---------------------------------------------------------------------------
318c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main
3192e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wangstatic void copyWithPadding(void* ptr, void* srcPtr, int mSize, int count) {
3202e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang    int sizeBytesPad = mSize * 4;
3212e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang    int sizeBytes = mSize * 3;
3222e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang    uint8_t *dst = static_cast<uint8_t *>(ptr);
3232e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang    uint8_t *src = static_cast<uint8_t *>(srcPtr);
3242e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang    for (int i = 0; i < count; i++) {
3252e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        memcpy(dst, src, sizeBytes);
3262e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        dst += sizeBytesPad;
3272e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        src += sizeBytes;
3282e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang    }
3292e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang}
3302e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang
3312e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wangstatic void copyWithUnPadding(void* ptr, void* srcPtr, int mSize, int count) {
3322e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang    int sizeBytesPad = mSize * 4;
3332e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang    int sizeBytes = mSize * 3;
3342e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang    uint8_t *dst = static_cast<uint8_t *>(ptr);
3352e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang    uint8_t *src = static_cast<uint8_t *>(srcPtr);
3362e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang    for (int i = 0; i < count; i++) {
3372e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        memcpy(dst, src, sizeBytes);
3382e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        dst += sizeBytes;
3392e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        src += sizeBytesPad;
3402e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang    }
3412e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang}
3422e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang
3432e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang
3442e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang// ---------------------------------------------------------------------------
3452e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang
346c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Mainstatic void
347bec39b6de685a7dddb1925c7e9f83fae20388de3Miao WangnContextFinish(JNIEnv *_env, jobject _this, jlong con)
348c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main{
349bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang    LOG_API("nContextFinish, con(%p)", (RsContext)con);
350bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang    dispatchTab.ContextFinish((RsContext)con);
351c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main}
352c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main
353936fbd696aac243a514f74b6fee6efd5959527a4Yang Nistatic jlong
354936fbd696aac243a514f74b6fee6efd5959527a4Yang NinClosureCreate(JNIEnv *_env, jobject _this, jlong con, jlong kernelID,
355936fbd696aac243a514f74b6fee6efd5959527a4Yang Ni               jlong returnValue, jlongArray fieldIDArray,
356936fbd696aac243a514f74b6fee6efd5959527a4Yang Ni               jlongArray valueArray, jintArray sizeArray,
357936fbd696aac243a514f74b6fee6efd5959527a4Yang Ni               jlongArray depClosureArray, jlongArray depFieldIDArray) {
358d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  jlong ret = 0;
359d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni
360936fbd696aac243a514f74b6fee6efd5959527a4Yang Ni  jlong* jFieldIDs = _env->GetLongArrayElements(fieldIDArray, nullptr);
361936fbd696aac243a514f74b6fee6efd5959527a4Yang Ni  jsize fieldIDs_length = _env->GetArrayLength(fieldIDArray);
362d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  jlong* jValues = _env->GetLongArrayElements(valueArray, nullptr);
363d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  jsize values_length = _env->GetArrayLength(valueArray);
364d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  jint* jSizes = _env->GetIntArrayElements(sizeArray, nullptr);
365d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  jsize sizes_length = _env->GetArrayLength(sizeArray);
366d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  jlong* jDepClosures =
367d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni      _env->GetLongArrayElements(depClosureArray, nullptr);
368d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  jsize depClosures_length = _env->GetArrayLength(depClosureArray);
369d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  jlong* jDepFieldIDs =
370d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni      _env->GetLongArrayElements(depFieldIDArray, nullptr);
371d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  jsize depFieldIDs_length = _env->GetArrayLength(depFieldIDArray);
372d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni
373d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  size_t numValues, numDependencies;
374d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  RsScriptFieldID* fieldIDs;
375d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  RsClosure* depClosures;
376d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  RsScriptFieldID* depFieldIDs;
377d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni
378d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  if (fieldIDs_length != values_length || values_length != sizes_length) {
379d3fdb27e85cd38ffacd963bced1f91a9297a68d8Miao Wang      LOG_ERR("Unmatched field IDs, values, and sizes in closure creation.");
380d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni      goto exit;
381d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  }
382d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni
383d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  numValues = (size_t)fieldIDs_length;
384d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni
385d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  if (depClosures_length != depFieldIDs_length) {
386d3fdb27e85cd38ffacd963bced1f91a9297a68d8Miao Wang      LOG_ERR("Unmatched closures and field IDs for dependencies in closure creation.");
387d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni      goto exit;
388d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  }
389d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni
390d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  numDependencies = (size_t)depClosures_length;
391d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni
392d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  if (numDependencies > numValues) {
393d3fdb27e85cd38ffacd963bced1f91a9297a68d8Miao Wang      LOG_ERR("Unexpected number of dependencies in closure creation");
394d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni      goto exit;
395d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  }
396d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni
397d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  if (numValues > RS_CLOSURE_MAX_NUMBER_ARGS_AND_BINDINGS) {
398d3fdb27e85cd38ffacd963bced1f91a9297a68d8Miao Wang      LOG_ERR("Too many arguments or globals in closure creation");
399d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni      goto exit;
400d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  }
401d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni
402e5125574a68ee6283c300732a11662d8f9945624Yang Ni  if (numValues > 0) {
403e5125574a68ee6283c300732a11662d8f9945624Yang Ni      fieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numValues);
404e5125574a68ee6283c300732a11662d8f9945624Yang Ni      if (fieldIDs == nullptr) {
405e5125574a68ee6283c300732a11662d8f9945624Yang Ni          goto exit;
406e5125574a68ee6283c300732a11662d8f9945624Yang Ni      }
407e5125574a68ee6283c300732a11662d8f9945624Yang Ni  } else {
408e5125574a68ee6283c300732a11662d8f9945624Yang Ni      // numValues == 0
409e5125574a68ee6283c300732a11662d8f9945624Yang Ni      // alloca(0) implementation is platform dependent
410e5125574a68ee6283c300732a11662d8f9945624Yang Ni      fieldIDs = nullptr;
411d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  }
412d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni
413d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  for (size_t i = 0; i < numValues; i++) {
414936fbd696aac243a514f74b6fee6efd5959527a4Yang Ni    fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i];
415936fbd696aac243a514f74b6fee6efd5959527a4Yang Ni  }
416936fbd696aac243a514f74b6fee6efd5959527a4Yang Ni
417e5125574a68ee6283c300732a11662d8f9945624Yang Ni  if (numDependencies > 0) {
418e5125574a68ee6283c300732a11662d8f9945624Yang Ni      depClosures = (RsClosure*)alloca(sizeof(RsClosure) * numDependencies);
419e5125574a68ee6283c300732a11662d8f9945624Yang Ni      if (depClosures == nullptr) {
420e5125574a68ee6283c300732a11662d8f9945624Yang Ni          goto exit;
421e5125574a68ee6283c300732a11662d8f9945624Yang Ni      }
422e5125574a68ee6283c300732a11662d8f9945624Yang Ni
423e5125574a68ee6283c300732a11662d8f9945624Yang Ni      for (size_t i = 0; i < numDependencies; i++) {
424e5125574a68ee6283c300732a11662d8f9945624Yang Ni          depClosures[i] = (RsClosure)jDepClosures[i];
425e5125574a68ee6283c300732a11662d8f9945624Yang Ni      }
426e5125574a68ee6283c300732a11662d8f9945624Yang Ni
427e5125574a68ee6283c300732a11662d8f9945624Yang Ni      depFieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numDependencies);
428e5125574a68ee6283c300732a11662d8f9945624Yang Ni      if (depFieldIDs == nullptr) {
429e5125574a68ee6283c300732a11662d8f9945624Yang Ni          goto exit;
430e5125574a68ee6283c300732a11662d8f9945624Yang Ni      }
431e5125574a68ee6283c300732a11662d8f9945624Yang Ni
432e5125574a68ee6283c300732a11662d8f9945624Yang Ni      for (size_t i = 0; i < numDependencies; i++) {
433e5125574a68ee6283c300732a11662d8f9945624Yang Ni          depFieldIDs[i] = (RsClosure)jDepFieldIDs[i];
434e5125574a68ee6283c300732a11662d8f9945624Yang Ni      }
435e5125574a68ee6283c300732a11662d8f9945624Yang Ni  } else {
436e5125574a68ee6283c300732a11662d8f9945624Yang Ni      // numDependencies == 0
437e5125574a68ee6283c300732a11662d8f9945624Yang Ni      // alloca(0) implementation is platform dependent
438e5125574a68ee6283c300732a11662d8f9945624Yang Ni      depClosures = nullptr;
439e5125574a68ee6283c300732a11662d8f9945624Yang Ni      depFieldIDs = nullptr;
440936fbd696aac243a514f74b6fee6efd5959527a4Yang Ni  }
441936fbd696aac243a514f74b6fee6efd5959527a4Yang Ni
442d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  ret = (jlong)(uintptr_t)dispatchTab.ClosureCreate(
443936fbd696aac243a514f74b6fee6efd5959527a4Yang Ni      (RsContext)con, (RsScriptKernelID)kernelID, (RsAllocation)returnValue,
44475c84f43017db82b547665436e7abda2caa2a645Yang Ni      fieldIDs, numValues, jValues, numValues,
445d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni      (int*)jSizes, numValues,
446d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni      depClosures, numDependencies,
447d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni      depFieldIDs, numDependencies);
448d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni
449d454f4541683a6bddfa1d00424a71f5855675e86Yang Niexit:
450d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni
451d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  _env->ReleaseLongArrayElements(depFieldIDArray, jDepFieldIDs, JNI_ABORT);
452d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  _env->ReleaseLongArrayElements(depClosureArray, jDepClosures, JNI_ABORT);
453d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  _env->ReleaseIntArrayElements (sizeArray,       jSizes,       JNI_ABORT);
454d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  _env->ReleaseLongArrayElements(valueArray,      jValues,      JNI_ABORT);
455d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  _env->ReleaseLongArrayElements(fieldIDArray,    jFieldIDs,    JNI_ABORT);
456d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni
457d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  return ret;
458936fbd696aac243a514f74b6fee6efd5959527a4Yang Ni}
459936fbd696aac243a514f74b6fee6efd5959527a4Yang Ni
460b1c365bfb0566142c49375f09f2b8289182f1f47Miao Wangstatic jlong
461b1c365bfb0566142c49375f09f2b8289182f1f47Miao WangnInvokeClosureCreate(JNIEnv *_env, jobject _this, jlong con, jlong invokeID,
462b1c365bfb0566142c49375f09f2b8289182f1f47Miao Wang                     jbyteArray paramArray, jlongArray fieldIDArray, jlongArray valueArray,
463b1c365bfb0566142c49375f09f2b8289182f1f47Miao Wang                     jintArray sizeArray) {
464d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  jlong ret = 0;
465d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni
466b1c365bfb0566142c49375f09f2b8289182f1f47Miao Wang  jbyte* jParams = _env->GetByteArrayElements(paramArray, nullptr);
467b1c365bfb0566142c49375f09f2b8289182f1f47Miao Wang  jsize jParamLength = _env->GetArrayLength(paramArray);
468b1c365bfb0566142c49375f09f2b8289182f1f47Miao Wang  jlong* jFieldIDs = _env->GetLongArrayElements(fieldIDArray, nullptr);
469b1c365bfb0566142c49375f09f2b8289182f1f47Miao Wang  jsize fieldIDs_length = _env->GetArrayLength(fieldIDArray);
470d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  jlong* jValues = _env->GetLongArrayElements(valueArray, nullptr);
471d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  jsize values_length = _env->GetArrayLength(valueArray);
472d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  jint* jSizes = _env->GetIntArrayElements(sizeArray, nullptr);
473d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  jsize sizes_length = _env->GetArrayLength(sizeArray);
474d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni
475d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  size_t numValues;
476d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  RsScriptFieldID* fieldIDs;
477d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni
478d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  if (fieldIDs_length != values_length || values_length != sizes_length) {
479d3fdb27e85cd38ffacd963bced1f91a9297a68d8Miao Wang      LOG_ERR("Unmatched field IDs, values, and sizes in closure creation.");
480d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni      goto exit;
481d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  }
482d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni
483d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  numValues = (size_t) fieldIDs_length;
484d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni
485d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  if (numValues > RS_CLOSURE_MAX_NUMBER_ARGS_AND_BINDINGS) {
486d3fdb27e85cd38ffacd963bced1f91a9297a68d8Miao Wang      LOG_ERR("Too many arguments or globals in closure creation");
487d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni      goto exit;
488d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  }
489d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni
490d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  fieldIDs = (RsScriptFieldID*)alloca(sizeof(RsScriptFieldID) * numValues);
491d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  if (fieldIDs == nullptr) {
492d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni      goto exit;
493d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  }
494d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni
495d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  for (size_t i = 0; i < numValues; i++) {
496b1c365bfb0566142c49375f09f2b8289182f1f47Miao Wang    fieldIDs[i] = (RsScriptFieldID)jFieldIDs[i];
497b1c365bfb0566142c49375f09f2b8289182f1f47Miao Wang  }
498b1c365bfb0566142c49375f09f2b8289182f1f47Miao Wang
499d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  ret = (jlong)(uintptr_t)dispatchTab.InvokeClosureCreate(
500b1c365bfb0566142c49375f09f2b8289182f1f47Miao Wang      (RsContext)con, (RsScriptInvokeID)invokeID, jParams, jParamLength,
50175c84f43017db82b547665436e7abda2caa2a645Yang Ni      fieldIDs, numValues, jValues, numValues,
502d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni      (int*)jSizes, numValues);
503d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni
504d454f4541683a6bddfa1d00424a71f5855675e86Yang Niexit:
505d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni
506d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  _env->ReleaseIntArrayElements (sizeArray,       jSizes,       JNI_ABORT);
507d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  _env->ReleaseLongArrayElements(valueArray,      jValues,      JNI_ABORT);
508d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  _env->ReleaseLongArrayElements(fieldIDArray,    jFieldIDs,    JNI_ABORT);
509d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  _env->ReleaseByteArrayElements(paramArray,      jParams,      JNI_ABORT);
510d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni
511d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  return ret;
512b1c365bfb0566142c49375f09f2b8289182f1f47Miao Wang}
513b1c365bfb0566142c49375f09f2b8289182f1f47Miao Wang
514936fbd696aac243a514f74b6fee6efd5959527a4Yang Nistatic void
515936fbd696aac243a514f74b6fee6efd5959527a4Yang NinClosureSetArg(JNIEnv *_env, jobject _this, jlong con, jlong closureID,
516936fbd696aac243a514f74b6fee6efd5959527a4Yang Ni               jint index, jlong value, jint size) {
51775c84f43017db82b547665436e7abda2caa2a645Yang Ni  // Size is signed with -1 indicating the values is an Allocation
518b1c365bfb0566142c49375f09f2b8289182f1f47Miao Wang  dispatchTab.ClosureSetArg((RsContext)con, (RsClosure)closureID, (uint32_t)index,
51975c84f43017db82b547665436e7abda2caa2a645Yang Ni                  (uintptr_t)value, size);
520936fbd696aac243a514f74b6fee6efd5959527a4Yang Ni}
521936fbd696aac243a514f74b6fee6efd5959527a4Yang Ni
522936fbd696aac243a514f74b6fee6efd5959527a4Yang Nistatic void
523936fbd696aac243a514f74b6fee6efd5959527a4Yang NinClosureSetGlobal(JNIEnv *_env, jobject _this, jlong con, jlong closureID,
524936fbd696aac243a514f74b6fee6efd5959527a4Yang Ni                  jlong fieldID, jlong value, jint size) {
52575c84f43017db82b547665436e7abda2caa2a645Yang Ni  // Size is signed with -1 indicating the values is an Allocation
526936fbd696aac243a514f74b6fee6efd5959527a4Yang Ni  dispatchTab.ClosureSetGlobal((RsContext)con, (RsClosure)closureID,
52775c84f43017db82b547665436e7abda2caa2a645Yang Ni                     (RsScriptFieldID)fieldID, (int64_t)value, size);
528936fbd696aac243a514f74b6fee6efd5959527a4Yang Ni}
529936fbd696aac243a514f74b6fee6efd5959527a4Yang Ni
530936fbd696aac243a514f74b6fee6efd5959527a4Yang Nistatic long
531e68c5973af1e80c77cab9aea0b77f46a801aef96Yang NinScriptGroup2Create(JNIEnv *_env, jobject _this, jlong con, jstring name,
532b1c365bfb0566142c49375f09f2b8289182f1f47Miao Wang                    jstring cacheDir, jlongArray closureArray) {
533d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  jlong ret = 0;
534d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni
535e68c5973af1e80c77cab9aea0b77f46a801aef96Yang Ni  AutoJavaStringToUTF8 nameUTF(_env, name);
536b1c365bfb0566142c49375f09f2b8289182f1f47Miao Wang  AutoJavaStringToUTF8 cacheDirUTF(_env, cacheDir);
537b1c365bfb0566142c49375f09f2b8289182f1f47Miao Wang
538936fbd696aac243a514f74b6fee6efd5959527a4Yang Ni  jlong* jClosures = _env->GetLongArrayElements(closureArray, nullptr);
539936fbd696aac243a514f74b6fee6efd5959527a4Yang Ni  jsize numClosures = _env->GetArrayLength(closureArray);
540d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni
541d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  RsClosure* closures;
542d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni
543d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  if (numClosures > (jsize) RS_SCRIPT_GROUP_MAX_NUMBER_CLOSURES) {
544d3fdb27e85cd38ffacd963bced1f91a9297a68d8Miao Wang    LOG_ERR("Too many closures in script group");
545d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni    goto exit;
546d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  }
547d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni
548d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  closures = (RsClosure*)alloca(sizeof(RsClosure) * numClosures);
549d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  if (closures == nullptr) {
550d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni      goto exit;
551d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  }
552d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni
553936fbd696aac243a514f74b6fee6efd5959527a4Yang Ni  for (int i = 0; i < numClosures; i++) {
554936fbd696aac243a514f74b6fee6efd5959527a4Yang Ni    closures[i] = (RsClosure)jClosures[i];
555936fbd696aac243a514f74b6fee6efd5959527a4Yang Ni  }
556936fbd696aac243a514f74b6fee6efd5959527a4Yang Ni
557d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  ret = (jlong)(uintptr_t)dispatchTab.ScriptGroup2Create(
558e68c5973af1e80c77cab9aea0b77f46a801aef96Yang Ni      (RsContext)con, nameUTF.c_str(), nameUTF.length(),
559e68c5973af1e80c77cab9aea0b77f46a801aef96Yang Ni      cacheDirUTF.c_str(), cacheDirUTF.length(),
560b1c365bfb0566142c49375f09f2b8289182f1f47Miao Wang      closures, numClosures);
561d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni
562d454f4541683a6bddfa1d00424a71f5855675e86Yang Niexit:
563d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni
564d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  _env->ReleaseLongArrayElements(closureArray, jClosures, JNI_ABORT);
565d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni
566d454f4541683a6bddfa1d00424a71f5855675e86Yang Ni  return ret;
567b1c365bfb0566142c49375f09f2b8289182f1f47Miao Wang}
568b1c365bfb0566142c49375f09f2b8289182f1f47Miao Wang
569b1c365bfb0566142c49375f09f2b8289182f1f47Miao Wangstatic void
570b1c365bfb0566142c49375f09f2b8289182f1f47Miao WangnScriptGroup2Execute(JNIEnv *_env, jobject _this, jlong con, jlong groupID) {
571b1c365bfb0566142c49375f09f2b8289182f1f47Miao Wang  dispatchTab.ScriptGroupExecute((RsContext)con, (RsScriptGroup2)groupID);
572936fbd696aac243a514f74b6fee6efd5959527a4Yang Ni}
573936fbd696aac243a514f74b6fee6efd5959527a4Yang Ni
574c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Mainstatic void
575bec39b6de685a7dddb1925c7e9f83fae20388de3Miao WangnObjDestroy(JNIEnv *_env, jobject _this, jlong con, jlong obj)
576c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main{
577bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang    LOG_API("nObjDestroy, con(%p) obj(%p)", (RsContext)con, (void *)obj);
578bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang    dispatchTab.ObjDestroy((RsContext)con, (void *)obj);
579c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main}
580c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main
5818919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang
5828919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wangstatic void
5838919a441f977cb787d244aa42cc2d4dda1cbfa11Miao WangnScriptIntrinsicBLAS_Single(JNIEnv *_env, jobject _this, jlong con, jlong incCon, jlong id, jint func, jint TransA,
5848919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang                            jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
5858919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang                            jfloat alpha, jlong A, jlong B, jfloat beta, jlong C, jint incX, jint incY,
5868919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang                            jint KL, jint KU, jboolean mUseInc) {
5878919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    RsBlasCall call;
5888919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    memset(&call, 0, sizeof(call));
5898919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.func = (RsBlasFunction)func;
5908919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.transA = (RsBlasTranspose)TransA;
5918919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.transB = (RsBlasTranspose)TransB;
5928919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.side = (RsBlasSide)Side;
5938919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.uplo = (RsBlasUplo)Uplo;
5948919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.diag = (RsBlasDiag)Diag;
5958919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.M = M;
5968919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.N = N;
5978919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.K = K;
5988919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.alpha.f = alpha;
5998919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.beta.f = beta;
6008919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.incX = incX;
6018919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.incY = incY;
6028919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.KL = KL;
6038919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.KU = KU;
6048919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang
6058919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    RsAllocation in_allocs[3];
6068919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    in_allocs[0] = (RsAllocation)A;
6078919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    in_allocs[1] = (RsAllocation)B;
6088919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    in_allocs[2] = (RsAllocation)C;
6098919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang
6108919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    if (mUseInc) {
6118919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang        dispatchTab.ContextFinish((RsContext)con);
6128919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang        dispatchTabInc.ScriptForEachMulti((RsContext)incCon, (RsScript)id, 0,
6131e46e4458cd167070301ecbc81ceea73c49eb0f6Miao Wang                                          in_allocs, NELEM(in_allocs), nullptr,
6148919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang                                          &call, sizeof(call), nullptr, 0);
6158919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    } else {
6168919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang        dispatchTab.ScriptForEachMulti((RsContext)con, (RsScript)id, 0,
6171e46e4458cd167070301ecbc81ceea73c49eb0f6Miao Wang                                       in_allocs, NELEM(in_allocs), nullptr,
6188919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang                                       &call, sizeof(call), nullptr, 0);
6198919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    }
6208919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang}
6218919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang
6228919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wangstatic void
6238919a441f977cb787d244aa42cc2d4dda1cbfa11Miao WangnScriptIntrinsicBLAS_Double(JNIEnv *_env, jobject _this, jlong con, jlong incCon, jlong id, jint func, jint TransA,
6248919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang                            jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
6258919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang                            jdouble alpha, jlong A, jlong B, jdouble beta, jlong C, jint incX, jint incY,
6268919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang                            jint KL, jint KU, jboolean mUseInc) {
6278919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    RsBlasCall call;
6288919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    memset(&call, 0, sizeof(call));
6298919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.func = (RsBlasFunction)func;
6308919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.transA = (RsBlasTranspose)TransA;
6318919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.transB = (RsBlasTranspose)TransB;
6328919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.side = (RsBlasSide)Side;
6338919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.uplo = (RsBlasUplo)Uplo;
6348919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.diag = (RsBlasDiag)Diag;
6358919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.M = M;
6368919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.N = N;
6378919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.K = K;
6388919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.alpha.d = alpha;
6398919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.beta.d = beta;
6408919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.incX = incX;
6418919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.incY = incY;
6428919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.KL = KL;
6438919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.KU = KU;
6448919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang
6458919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    RsAllocation in_allocs[3];
6468919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    in_allocs[0] = (RsAllocation)A;
6478919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    in_allocs[1] = (RsAllocation)B;
6488919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    in_allocs[2] = (RsAllocation)C;
6498919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang
6508919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    if (mUseInc) {
6518919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang        dispatchTab.ContextFinish((RsContext)con);
6528919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang        dispatchTabInc.ScriptForEachMulti((RsContext)incCon, (RsScript)id, 0,
6531e46e4458cd167070301ecbc81ceea73c49eb0f6Miao Wang                                          in_allocs, NELEM(in_allocs), nullptr,
6548919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang                                          &call, sizeof(call), nullptr, 0);
6558919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    } else {
6568919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang        dispatchTab.ScriptForEachMulti((RsContext)con, (RsScript)id, 0,
6571e46e4458cd167070301ecbc81ceea73c49eb0f6Miao Wang                                        in_allocs, NELEM(in_allocs), nullptr,
6588919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang                                        &call, sizeof(call), nullptr, 0);
6598919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    }
6608919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang}
6618919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang
6628919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wangstatic void
6638919a441f977cb787d244aa42cc2d4dda1cbfa11Miao WangnScriptIntrinsicBLAS_Complex(JNIEnv *_env, jobject _this, jlong con, jlong incCon, jlong id, jint func, jint TransA,
6648919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang                             jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
6658919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang                             jfloat alphaX, jfloat alphaY, jlong A, jlong B, jfloat betaX,
6668919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang                             jfloat betaY, jlong C, jint incX, jint incY, jint KL, jint KU, jboolean mUseInc) {
6678919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    RsBlasCall call;
6688919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    memset(&call, 0, sizeof(call));
6698919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.func = (RsBlasFunction)func;
6708919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.transA = (RsBlasTranspose)TransA;
6718919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.transB = (RsBlasTranspose)TransB;
6728919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.side = (RsBlasSide)Side;
6738919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.uplo = (RsBlasUplo)Uplo;
6748919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.diag = (RsBlasDiag)Diag;
6758919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.M = M;
6768919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.N = N;
6778919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.K = K;
6788919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.alpha.c.r = alphaX;
6798919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.alpha.c.i = alphaY;
6808919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.beta.c.r = betaX;
6818919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.beta.c.i = betaY;
6828919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.incX = incX;
6838919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.incY = incY;
6848919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.KL = KL;
6858919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.KU = KU;
6868919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang
6878919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    RsAllocation in_allocs[3];
6888919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    in_allocs[0] = (RsAllocation)A;
6898919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    in_allocs[1] = (RsAllocation)B;
6908919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    in_allocs[2] = (RsAllocation)C;
6918919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang
6928919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang
6938919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    if (mUseInc) {
6948919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang        dispatchTab.ContextFinish((RsContext)con);
6958919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang        dispatchTabInc.ScriptForEachMulti((RsContext)incCon, (RsScript)id, 0,
6961e46e4458cd167070301ecbc81ceea73c49eb0f6Miao Wang                                          in_allocs, NELEM(in_allocs), nullptr,
6978919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang                                          &call, sizeof(call), nullptr, 0);
6988919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    } else {
6998919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang        dispatchTab.ScriptForEachMulti((RsContext)con, (RsScript)id, 0,
7001e46e4458cd167070301ecbc81ceea73c49eb0f6Miao Wang                                       in_allocs, NELEM(in_allocs), nullptr,
7018919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang                                       &call, sizeof(call), nullptr, 0);
7028919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    }
7038919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang}
7048919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang
7058919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wangstatic void
7068919a441f977cb787d244aa42cc2d4dda1cbfa11Miao WangnScriptIntrinsicBLAS_Z(JNIEnv *_env, jobject _this, jlong con, jlong incCon, jlong id, jint func, jint TransA,
7078919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang                       jint TransB, jint Side, jint Uplo, jint Diag, jint M, jint N, jint K,
7088919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang                       jdouble alphaX, jdouble alphaY, jlong A, jlong B, jdouble betaX,
7098919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang                       jdouble betaY, jlong C, jint incX, jint incY, jint KL, jint KU, jboolean mUseInc) {
7108919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    RsBlasCall call;
7118919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    memset(&call, 0, sizeof(call));
7128919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.func = (RsBlasFunction)func;
7138919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.transA = (RsBlasTranspose)TransA;
7148919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.transB = (RsBlasTranspose)TransB;
7158919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.side = (RsBlasSide)Side;
7168919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.uplo = (RsBlasUplo)Uplo;
7178919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.diag = (RsBlasDiag)Diag;
7188919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.M = M;
7198919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.N = N;
7208919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.K = K;
7218919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.alpha.z.r = alphaX;
7228919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.alpha.z.i = alphaY;
7238919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.beta.z.r = betaX;
7248919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.beta.z.i = betaY;
7258919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.incX = incX;
7268919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.incY = incY;
7278919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.KL = KL;
7288919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.KU = KU;
7298919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang
7308919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    RsAllocation in_allocs[3];
7318919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    in_allocs[0] = (RsAllocation)A;
7328919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    in_allocs[1] = (RsAllocation)B;
7338919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    in_allocs[2] = (RsAllocation)C;
7348919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang
7358919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang
7368919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    if (mUseInc) {
7378919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang        dispatchTab.ContextFinish((RsContext)con);
7388919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang        dispatchTabInc.ScriptForEachMulti((RsContext)incCon, (RsScript)id, 0,
7391e46e4458cd167070301ecbc81ceea73c49eb0f6Miao Wang                                          in_allocs, NELEM(in_allocs), nullptr,
7408919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang                                          &call, sizeof(call), nullptr, 0);
7418919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    } else {
7428919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang        dispatchTab.ScriptForEachMulti((RsContext)con, (RsScript)id, 0,
7431e46e4458cd167070301ecbc81ceea73c49eb0f6Miao Wang                                        in_allocs, NELEM(in_allocs), nullptr,
7448919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang                                        &call, sizeof(call), nullptr, 0);
7458919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    }
7468919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang}
7478919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang
7488919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang
7498919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wangstatic void
7508919a441f977cb787d244aa42cc2d4dda1cbfa11Miao WangnScriptIntrinsicBLAS_BNNM(JNIEnv *_env, jobject _this, jlong con, jlong incCon, jlong id, jint M, jint N, jint K,
7518919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang                          jlong A, jint a_offset, jlong B, jint b_offset, jlong C, jint c_offset,
7528919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang                          jint c_mult_int, jboolean mUseInc) {
7538919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    RsBlasCall call;
7548919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    memset(&call, 0, sizeof(call));
7558919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.func = RsBlas_bnnm;
7568919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.M = M;
7578919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.N = N;
7588919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.K = K;
759341032cd8e6c1a8a8ddceccdafbf02687d0828acMiao Wang    call.a_offset = a_offset & 0xFF;
760341032cd8e6c1a8a8ddceccdafbf02687d0828acMiao Wang    call.b_offset = b_offset & 0xFF;
7618919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.c_offset = c_offset;
7628919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    call.c_mult_int = c_mult_int;
7638919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang
7648919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    RsAllocation in_allocs[3];
7658919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    in_allocs[0] = (RsAllocation)A;
7668919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    in_allocs[1] = (RsAllocation)B;
7678919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    in_allocs[2] = (RsAllocation)C;
7688919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang
7698919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    if (mUseInc) {
7708919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang        dispatchTab.ContextFinish((RsContext)con);
7718919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang        dispatchTabInc.ScriptForEachMulti((RsContext)incCon, (RsScript)id, 0,
7721e46e4458cd167070301ecbc81ceea73c49eb0f6Miao Wang                                          in_allocs, NELEM(in_allocs), nullptr,
7738919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang                                          &call, sizeof(call), nullptr, 0);
7748919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    } else {
7758919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang        dispatchTab.ScriptForEachMulti((RsContext)con, (RsScript)id, 0,
7761e46e4458cd167070301ecbc81ceea73c49eb0f6Miao Wang                                        in_allocs, NELEM(in_allocs), nullptr,
7778919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang                                        &call, sizeof(call), nullptr, 0);
7788919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang    }
7798919a441f977cb787d244aa42cc2d4dda1cbfa11Miao Wang}
780c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott Main// ---------------------------------------------------------------------------
781bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wangstatic jlong
782c89ad4606ae29e103b85bdfc40c57e36c8877dbaScott MainnDeviceCreate(JNIEnv *_env, jobject _this)
783