diff options
Diffstat (limited to 'platform/android')
-rw-r--r-- | platform/android/SCsub | 4 | ||||
-rw-r--r-- | platform/android/cpu-features.c | 1089 | ||||
-rw-r--r-- | platform/android/cpu-features.h | 214 | ||||
-rw-r--r-- | platform/android/detect.py | 2 | ||||
-rw-r--r-- | platform/android/export/export.cpp | 4 | ||||
-rw-r--r-- | platform/android/java/src/com/android/godot/GodotPaymentV3.java | 8 | ||||
-rw-r--r-- | platform/android/java_class_wrapper.cpp | 1332 | ||||
-rw-r--r-- | platform/android/java_class_wrapper.h | 168 | ||||
-rw-r--r-- | platform/android/java_glue.cpp | 5 |
9 files changed, 2819 insertions, 7 deletions
diff --git a/platform/android/SCsub b/platform/android/SCsub index 8e61b7d8e0..699db30cad 100644 --- a/platform/android/SCsub +++ b/platform/android/SCsub @@ -15,7 +15,9 @@ android_files = [ 'audio_driver_jandroid.cpp', 'ifaddrs_android.cpp', 'android_native_app_glue.c', - 'java_glue.cpp' + 'java_glue.cpp', + 'cpu-features.c', + 'java_class_wrapper.cpp' ] #env.Depends('#core/math/vector3.h', 'vector3_psp.h') diff --git a/platform/android/cpu-features.c b/platform/android/cpu-features.c new file mode 100644 index 0000000000..156d464729 --- /dev/null +++ b/platform/android/cpu-features.c @@ -0,0 +1,1089 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* ChangeLog for this library: + * + * NDK r8d: Add android_setCpu(). + * + * NDK r8c: Add new ARM CPU features: VFPv2, VFP_D32, VFP_FP16, + * VFP_FMA, NEON_FMA, IDIV_ARM, IDIV_THUMB2 and iWMMXt. + * + * Rewrite the code to parse /proc/self/auxv instead of + * the "Features" field in /proc/cpuinfo. + * + * Dynamically allocate the buffer that hold the content + * of /proc/cpuinfo to deal with newer hardware. + * + * NDK r7c: Fix CPU count computation. The old method only reported the + * number of _active_ CPUs when the library was initialized, + * which could be less than the real total. + * + * NDK r5: Handle buggy kernels which report a CPU Architecture number of 7 + * for an ARMv6 CPU (see below). + * + * Handle kernels that only report 'neon', and not 'vfpv3' + * (VFPv3 is mandated by the ARM architecture is Neon is implemented) + * + * Handle kernels that only report 'vfpv3d16', and not 'vfpv3' + * + * Fix x86 compilation. Report ANDROID_CPU_FAMILY_X86 in + * android_getCpuFamily(). + * + * NDK r4: Initial release + */ + +#if defined(__le32__) + +// When users enter this, we should only provide interface and +// libportable will give the implementations. + +#else // !__le32__ + +#include <sys/system_properties.h> +#include <pthread.h> +#include "cpu-features.h" +#include <stdio.h> +#include <stdlib.h> +#include <fcntl.h> +#include <errno.h> + +static pthread_once_t g_once; +static int g_inited; +static AndroidCpuFamily g_cpuFamily; +static uint64_t g_cpuFeatures; +static int g_cpuCount; + +#ifdef __arm__ +static uint32_t g_cpuIdArm; +#endif + +static const int android_cpufeatures_debug = 0; + +#ifdef __arm__ +# define DEFAULT_CPU_FAMILY ANDROID_CPU_FAMILY_ARM +#elif defined __i386__ +# define DEFAULT_CPU_FAMILY ANDROID_CPU_FAMILY_X86 +#else +# define DEFAULT_CPU_FAMILY ANDROID_CPU_FAMILY_UNKNOWN +#endif + +#define D(...) \ + do { \ + if (android_cpufeatures_debug) { \ + printf(__VA_ARGS__); fflush(stdout); \ + } \ + } while (0) + +#ifdef __i386__ +static __inline__ void x86_cpuid(int func, int values[4]) +{ + int a, b, c, d; + /* We need to preserve ebx since we're compiling PIC code */ + /* this means we can't use "=b" for the second output register */ + __asm__ __volatile__ ( \ + "push %%ebx\n" + "cpuid\n" \ + "mov %%ebx, %1\n" + "pop %%ebx\n" + : "=a" (a), "=r" (b), "=c" (c), "=d" (d) \ + : "a" (func) \ + ); + values[0] = a; + values[1] = b; + values[2] = c; + values[3] = d; +} +#endif + +/* Get the size of a file by reading it until the end. This is needed + * because files under /proc do not always return a valid size when + * using fseek(0, SEEK_END) + ftell(). Nor can they be mmap()-ed. + */ +static int +get_file_size(const char* pathname) +{ + int fd, ret, result = 0; + char buffer[256]; + + fd = open(pathname, O_RDONLY); + if (fd < 0) { + D("Can't open %s: %s\n", pathname, strerror(errno)); + return -1; + } + + for (;;) { + int ret = read(fd, buffer, sizeof buffer); + if (ret < 0) { + if (errno == EINTR) + continue; + D("Error while reading %s: %s\n", pathname, strerror(errno)); + break; + } + if (ret == 0) + break; + + result += ret; + } + close(fd); + return result; +} + +/* Read the content of /proc/cpuinfo into a user-provided buffer. + * Return the length of the data, or -1 on error. Does *not* + * zero-terminate the content. Will not read more + * than 'buffsize' bytes. + */ +static int +read_file(const char* pathname, char* buffer, size_t buffsize) +{ + int fd, count; + + fd = open(pathname, O_RDONLY); + if (fd < 0) { + D("Could not open %s: %s\n", pathname, strerror(errno)); + return -1; + } + count = 0; + while (count < (int)buffsize) { + int ret = read(fd, buffer + count, buffsize - count); + if (ret < 0) { + if (errno == EINTR) + continue; + D("Error while reading from %s: %s\n", pathname, strerror(errno)); + if (count == 0) + count = -1; + break; + } + if (ret == 0) + break; + count += ret; + } + close(fd); + return count; +} + +/* Extract the content of a the first occurence of a given field in + * the content of /proc/cpuinfo and return it as a heap-allocated + * string that must be freed by the caller. + * + * Return NULL if not found + */ +static char* +extract_cpuinfo_field(const char* buffer, int buflen, const char* field) +{ + int fieldlen = strlen(field); + const char* bufend = buffer + buflen; + char* result = NULL; + int len, ignore; + const char *p, *q; + + /* Look for first field occurence, and ensures it starts the line. */ + p = buffer; + for (;;) { + p = memmem(p, bufend-p, field, fieldlen); + if (p == NULL) + goto EXIT; + + if (p == buffer || p[-1] == '\n') + break; + + p += fieldlen; + } + + /* Skip to the first column followed by a space */ + p += fieldlen; + p = memchr(p, ':', bufend-p); + if (p == NULL || p[1] != ' ') + goto EXIT; + + /* Find the end of the line */ + p += 2; + q = memchr(p, '\n', bufend-p); + if (q == NULL) + q = bufend; + + /* Copy the line into a heap-allocated buffer */ + len = q-p; + result = malloc(len+1); + if (result == NULL) + goto EXIT; + + memcpy(result, p, len); + result[len] = '\0'; + +EXIT: + return result; +} + +/* Checks that a space-separated list of items contains one given 'item'. + * Returns 1 if found, 0 otherwise. + */ +static int +has_list_item(const char* list, const char* item) +{ + const char* p = list; + int itemlen = strlen(item); + + if (list == NULL) + return 0; + + while (*p) { + const char* q; + + /* skip spaces */ + while (*p == ' ' || *p == '\t') + p++; + + /* find end of current list item */ + q = p; + while (*q && *q != ' ' && *q != '\t') + q++; + + if (itemlen == q-p && !memcmp(p, item, itemlen)) + return 1; + + /* skip to next item */ + p = q; + } + return 0; +} + +/* Parse a number starting from 'input', but not going further + * than 'limit'. Return the value into '*result'. + * + * NOTE: Does not skip over leading spaces, or deal with sign characters. + * NOTE: Ignores overflows. + * + * The function returns NULL in case of error (bad format), or the new + * position after the decimal number in case of success (which will always + * be <= 'limit'). + */ +static const char* +parse_number(const char* input, const char* limit, int base, int* result) +{ + const char* p = input; + int val = 0; + while (p < limit) { + int d = (*p - '0'); + if ((unsigned)d >= 10U) { + d = (*p - 'a'); + if ((unsigned)d >= 6U) + d = (*p - 'A'); + if ((unsigned)d >= 6U) + break; + d += 10; + } + if (d >= base) + break; + val = val*base + d; + p++; + } + if (p == input) + return NULL; + + *result = val; + return p; +} + +static const char* +parse_decimal(const char* input, const char* limit, int* result) +{ + return parse_number(input, limit, 10, result); +} + +static const char* +parse_hexadecimal(const char* input, const char* limit, int* result) +{ + return parse_number(input, limit, 16, result); +} + +/* This small data type is used to represent a CPU list / mask, as read + * from sysfs on Linux. See http://www.kernel.org/doc/Documentation/cputopology.txt + * + * For now, we don't expect more than 32 cores on mobile devices, so keep + * everything simple. + */ +typedef struct { + uint32_t mask; +} CpuList; + +static __inline__ void +cpulist_init(CpuList* list) { + list->mask = 0; +} + +static __inline__ void +cpulist_and(CpuList* list1, CpuList* list2) { + list1->mask &= list2->mask; +} + +static __inline__ void +cpulist_set(CpuList* list, int index) { + if ((unsigned)index < 32) { + list->mask |= (uint32_t)(1U << index); + } +} + +static __inline__ int +cpulist_count(CpuList* list) { + return __builtin_popcount(list->mask); +} + +/* Parse a textual list of cpus and store the result inside a CpuList object. + * Input format is the following: + * - comma-separated list of items (no spaces) + * - each item is either a single decimal number (cpu index), or a range made + * of two numbers separated by a single dash (-). Ranges are inclusive. + * + * Examples: 0 + * 2,4-127,128-143 + * 0-1 + */ +static void +cpulist_parse(CpuList* list, const char* line, int line_len) +{ + const char* p = line; + const char* end = p + line_len; + const char* q; + + /* NOTE: the input line coming from sysfs typically contains a + * trailing newline, so take care of it in the code below + */ + while (p < end && *p != '\n') + { + int val, start_value, end_value; + + /* Find the end of current item, and put it into 'q' */ + q = memchr(p, ',', end-p); + if (q == NULL) { + q = end; + } + + /* Get first value */ + p = parse_decimal(p, q, &start_value); + if (p == NULL) + goto BAD_FORMAT; + + end_value = start_value; + + /* If we're not at the end of the item, expect a dash and + * and integer; extract end value. + */ + if (p < q && *p == '-') { + p = parse_decimal(p+1, q, &end_value); + if (p == NULL) + goto BAD_FORMAT; + } + + /* Set bits CPU list bits */ + for (val = start_value; val <= end_value; val++) { + cpulist_set(list, val); + } + + /* Jump to next item */ + p = q; + if (p < end) + p++; + } + +BAD_FORMAT: + ; +} + +/* Read a CPU list from one sysfs file */ +static void +cpulist_read_from(CpuList* list, const char* filename) +{ + char file[64]; + int filelen; + + cpulist_init(list); + + filelen = read_file(filename, file, sizeof file); + if (filelen < 0) { + D("Could not read %s: %s\n", filename, strerror(errno)); + return; + } + + cpulist_parse(list, file, filelen); +} + +// See <asm/hwcap.h> kernel header. +#define HWCAP_VFP (1 << 6) +#define HWCAP_IWMMXT (1 << 9) +#define HWCAP_NEON (1 << 12) +#define HWCAP_VFPv3 (1 << 13) +#define HWCAP_VFPv3D16 (1 << 14) +#define HWCAP_VFPv4 (1 << 16) +#define HWCAP_IDIVA (1 << 17) +#define HWCAP_IDIVT (1 << 18) + +#define AT_HWCAP 16 + +#if defined(__arm__) +/* Compute the ELF HWCAP flags. + */ +static uint32_t +get_elf_hwcap(const char* cpuinfo, int cpuinfo_len) +{ + /* IMPORTANT: + * Accessing /proc/self/auxv doesn't work anymore on all + * platform versions. More specifically, when running inside + * a regular application process, most of /proc/self/ will be + * non-readable, including /proc/self/auxv. This doesn't + * happen however if the application is debuggable, or when + * running under the "shell" UID, which is why this was not + * detected appropriately. + */ +#if 0 + uint32_t result = 0; + const char filepath[] = "/proc/self/auxv"; + int fd = open(filepath, O_RDONLY); + if (fd < 0) { + D("Could not open %s: %s\n", filepath, strerror(errno)); + return 0; + } + + struct { uint32_t tag; uint32_t value; } entry; + + for (;;) { + int ret = read(fd, (char*)&entry, sizeof entry); + if (ret < 0) { + if (errno == EINTR) + continue; + D("Error while reading %s: %s\n", filepath, strerror(errno)); + break; + } + // Detect end of list. + if (ret == 0 || (entry.tag == 0 && entry.value == 0)) + break; + if (entry.tag == AT_HWCAP) { + result = entry.value; + break; + } + } + close(fd); + return result; +#else + // Recreate ELF hwcaps by parsing /proc/cpuinfo Features tag. + uint32_t hwcaps = 0; + + char* cpuFeatures = extract_cpuinfo_field(cpuinfo, cpuinfo_len, "Features"); + + if (cpuFeatures != NULL) { + D("Found cpuFeatures = '%s'\n", cpuFeatures); + + if (has_list_item(cpuFeatures, "vfp")) + hwcaps |= HWCAP_VFP; + if (has_list_item(cpuFeatures, "vfpv3")) + hwcaps |= HWCAP_VFPv3; + if (has_list_item(cpuFeatures, "vfpv3d16")) + hwcaps |= HWCAP_VFPv3D16; + if (has_list_item(cpuFeatures, "vfpv4")) + hwcaps |= HWCAP_VFPv4; + if (has_list_item(cpuFeatures, "neon")) + hwcaps |= HWCAP_NEON; + if (has_list_item(cpuFeatures, "idiva")) + hwcaps |= HWCAP_IDIVA; + if (has_list_item(cpuFeatures, "idivt")) + hwcaps |= HWCAP_IDIVT; + if (has_list_item(cpuFeatures, "idiv")) + hwcaps |= HWCAP_IDIVA | HWCAP_IDIVT; + if (has_list_item(cpuFeatures, "iwmmxt")) + hwcaps |= HWCAP_IWMMXT; + + free(cpuFeatures); + } + return hwcaps; +#endif +} +#endif /* __arm__ */ + +/* Return the number of cpus present on a given device. + * + * To handle all weird kernel configurations, we need to compute the + * intersection of the 'present' and 'possible' CPU lists and count + * the result. + */ +static int +get_cpu_count(void) +{ + CpuList cpus_present[1]; + CpuList cpus_possible[1]; + + cpulist_read_from(cpus_present, "/sys/devices/system/cpu/present"); + cpulist_read_from(cpus_possible, "/sys/devices/system/cpu/possible"); + + /* Compute the intersection of both sets to get the actual number of + * CPU cores that can be used on this device by the kernel. + */ + cpulist_and(cpus_present, cpus_possible); + + return cpulist_count(cpus_present); +} + +static void +android_cpuInitFamily(void) +{ +#if defined(__arm__) + g_cpuFamily = ANDROID_CPU_FAMILY_ARM; +#elif defined(__i386__) + g_cpuFamily = ANDROID_CPU_FAMILY_X86; +#elif defined(__mips64) +/* Needs to be before __mips__ since the compiler defines both */ + g_cpuFamily = ANDROID_CPU_FAMILY_MIPS64; +#elif defined(__mips__) + g_cpuFamily = ANDROID_CPU_FAMILY_MIPS; +#elif defined(__aarch64__) + g_cpuFamily = ANDROID_CPU_FAMILY_ARM64; +#elif defined(__x86_64__) + g_cpuFamily = ANDROID_CPU_FAMILY_X86_64; +#else + g_cpuFamily = ANDROID_CPU_FAMILY_UNKNOWN; +#endif +} + +static void +android_cpuInit(void) +{ + char* cpuinfo = NULL; + int cpuinfo_len; + + android_cpuInitFamily(); + + g_cpuFeatures = 0; + g_cpuCount = 1; + g_inited = 1; + + cpuinfo_len = get_file_size("/proc/cpuinfo"); + if (cpuinfo_len < 0) { + D("cpuinfo_len cannot be computed!"); + return; + } + cpuinfo = malloc(cpuinfo_len); + if (cpuinfo == NULL) { + D("cpuinfo buffer could not be allocated"); + return; + } + cpuinfo_len = read_file("/proc/cpuinfo", cpuinfo, cpuinfo_len); + D("cpuinfo_len is (%d):\n%.*s\n", cpuinfo_len, + cpuinfo_len >= 0 ? cpuinfo_len : 0, cpuinfo); + + if (cpuinfo_len < 0) /* should not happen */ { + free(cpuinfo); + return; + } + + /* Count the CPU cores, the value may be 0 for single-core CPUs */ + g_cpuCount = get_cpu_count(); + if (g_cpuCount == 0) { + g_cpuCount = 1; + } + + D("found cpuCount = %d\n", g_cpuCount); + +#ifdef __arm__ + { + char* features = NULL; + char* architecture = NULL; + + /* Extract architecture from the "CPU Architecture" field. + * The list is well-known, unlike the the output of + * the 'Processor' field which can vary greatly. + * + * See the definition of the 'proc_arch' array in + * $KERNEL/arch/arm/kernel/setup.c and the 'c_show' function in + * same file. + */ + char* cpuArch = extract_cpuinfo_field(cpuinfo, cpuinfo_len, "CPU architecture"); + + if (cpuArch != NULL) { + char* end; + long archNumber; + int hasARMv7 = 0; + + D("found cpuArch = '%s'\n", cpuArch); + + /* read the initial decimal number, ignore the rest */ + archNumber = strtol(cpuArch, &end, 10); + + /* Here we assume that ARMv8 will be upwards compatible with v7 + * in the future. Unfortunately, there is no 'Features' field to + * indicate that Thumb-2 is supported. + */ + if (end > cpuArch && archNumber >= 7) { + hasARMv7 = 1; + } + + /* Unfortunately, it seems that certain ARMv6-based CPUs + * report an incorrect architecture number of 7! + * + * See http://code.google.com/p/android/issues/detail?id=10812 + * + * We try to correct this by looking at the 'elf_format' + * field reported by the 'Processor' field, which is of the + * form of "(v7l)" for an ARMv7-based CPU, and "(v6l)" for + * an ARMv6-one. + */ + if (hasARMv7) { + char* cpuProc = extract_cpuinfo_field(cpuinfo, cpuinfo_len, + "Processor"); + if (cpuProc != NULL) { + D("found cpuProc = '%s'\n", cpuProc); + if (has_list_item(cpuProc, "(v6l)")) { + D("CPU processor and architecture mismatch!!\n"); + hasARMv7 = 0; + } + free(cpuProc); + } + } + + if (hasARMv7) { + g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_ARMv7; + } + + /* The LDREX / STREX instructions are available from ARMv6 */ + if (archNumber >= 6) { + g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_LDREX_STREX; + } + + free(cpuArch); + } + + /* Extract the list of CPU features from ELF hwcaps */ + uint32_t hwcaps = get_elf_hwcap(cpuinfo, cpuinfo_len); + + if (hwcaps != 0) { + int has_vfp = (hwcaps & HWCAP_VFP); + int has_vfpv3 = (hwcaps & HWCAP_VFPv3); + int has_vfpv3d16 = (hwcaps & HWCAP_VFPv3D16); + int has_vfpv4 = (hwcaps & HWCAP_VFPv4); + int has_neon = (hwcaps & HWCAP_NEON); + int has_idiva = (hwcaps & HWCAP_IDIVA); + int has_idivt = (hwcaps & HWCAP_IDIVT); + int has_iwmmxt = (hwcaps & HWCAP_IWMMXT); + + // The kernel does a poor job at ensuring consistency when + // describing CPU features. So lots of guessing is needed. + + // 'vfpv4' implies VFPv3|VFP_FMA|FP16 + if (has_vfpv4) + g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv3 | + ANDROID_CPU_ARM_FEATURE_VFP_FP16 | + ANDROID_CPU_ARM_FEATURE_VFP_FMA; + + // 'vfpv3' or 'vfpv3d16' imply VFPv3. Note that unlike GCC, + // a value of 'vfpv3' doesn't necessarily mean that the D32 + // feature is present, so be conservative. All CPUs in the + // field that support D32 also support NEON, so this should + // not be a problem in practice. + if (has_vfpv3 || has_vfpv3d16) + g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv3; + + // 'vfp' is super ambiguous. Depending on the kernel, it can + // either mean VFPv2 or VFPv3. Make it depend on ARMv7. + if (has_vfp) { + if (g_cpuFeatures & ANDROID_CPU_ARM_FEATURE_ARMv7) + g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv3; + else + g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv2; + } + + // Neon implies VFPv3|D32, and if vfpv4 is detected, NEON_FMA + if (has_neon) { + g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv3 | + ANDROID_CPU_ARM_FEATURE_NEON | + ANDROID_CPU_ARM_FEATURE_VFP_D32; + if (has_vfpv4) + g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_NEON_FMA; + } + + // VFPv3 implies VFPv2 and ARMv7 + if (g_cpuFeatures & ANDROID_CPU_ARM_FEATURE_VFPv3) + g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv2 | + ANDROID_CPU_ARM_FEATURE_ARMv7; + + if (has_idiva) + g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_IDIV_ARM; + if (has_idivt) + g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_IDIV_THUMB2; + + if (has_iwmmxt) + g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_iWMMXt; + } + + /* Extract the cpuid value from various fields */ + // The CPUID value is broken up in several entries in /proc/cpuinfo. + // This table is used to rebuild it from the entries. + static const struct CpuIdEntry { + const char* field; + char format; + char bit_lshift; + char bit_length; + } cpu_id_entries[] = { + { "CPU implementer", 'x', 24, 8 }, + { "CPU variant", 'x', 20, 4 }, + { "CPU part", 'x', 4, 12 }, + { "CPU revision", 'd', 0, 4 }, + }; + size_t i; + D("Parsing /proc/cpuinfo to recover CPUID\n"); + for (i = 0; + i < sizeof(cpu_id_entries)/sizeof(cpu_id_entries[0]); + ++i) { + const struct CpuIdEntry* entry = &cpu_id_entries[i]; + char* value = extract_cpuinfo_field(cpuinfo, + cpuinfo_len, + entry->field); + if (value == NULL) + continue; + + D("field=%s value='%s'\n", entry->field, value); + char* value_end = value + strlen(value); + int val = 0; + const char* start = value; + const char* p; + if (value[0] == '0' && (value[1] == 'x' || value[1] == 'X')) { + start += 2; + p = parse_hexadecimal(start, value_end, &val); + } else if (entry->format == 'x') + p = parse_hexadecimal(value, value_end, &val); + else + p = parse_decimal(value, value_end, &val); + + if (p > (const char*)start) { + val &= ((1 << entry->bit_length)-1); + val <<= entry->bit_lshift; + g_cpuIdArm |= (uint32_t) val; + } + + free(value); + } + + // Handle kernel configuration bugs that prevent the correct + // reporting of CPU features. + static const struct CpuFix { + uint32_t cpuid; + uint64_t or_flags; + } cpu_fixes[] = { + /* The Nexus 4 (Qualcomm Krait) kernel configuration + * forgets to report IDIV support. */ + { 0x510006f2, ANDROID_CPU_ARM_FEATURE_IDIV_ARM | + ANDROID_CPU_ARM_FEATURE_IDIV_THUMB2 }, + { 0x510006f3, ANDROID_CPU_ARM_FEATURE_IDIV_ARM | + ANDROID_CPU_ARM_FEATURE_IDIV_THUMB2 }, + }; + size_t n; + for (n = 0; n < sizeof(cpu_fixes)/sizeof(cpu_fixes[0]); ++n) { + const struct CpuFix* entry = &cpu_fixes[n]; + + if (g_cpuIdArm == entry->cpuid) + g_cpuFeatures |= entry->or_flags; + } + + } +#endif /* __arm__ */ + +#ifdef __i386__ + int regs[4]; + +/* According to http://en.wikipedia.org/wiki/CPUID */ +#define VENDOR_INTEL_b 0x756e6547 +#define VENDOR_INTEL_c 0x6c65746e +#define VENDOR_INTEL_d 0x49656e69 + + x86_cpuid(0, regs); + int vendorIsIntel = (regs[1] == VENDOR_INTEL_b && + regs[2] == VENDOR_INTEL_c && + regs[3] == VENDOR_INTEL_d); + + x86_cpuid(1, regs); + if ((regs[2] & (1 << 9)) != 0) { + g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_SSSE3; + } + if ((regs[2] & (1 << 23)) != 0) { + g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_POPCNT; + } + if (vendorIsIntel && (regs[2] & (1 << 22)) != 0) { + g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_MOVBE; + } +#endif + + free(cpuinfo); +} + + +AndroidCpuFamily +android_getCpuFamily(void) +{ + pthread_once(&g_once, android_cpuInit); + return g_cpuFamily; +} + + +uint64_t +android_getCpuFeatures(void) +{ + pthread_once(&g_once, android_cpuInit); + return g_cpuFeatures; +} + + +int +android_getCpuCount(void) +{ + pthread_once(&g_once, android_cpuInit); + return g_cpuCount; +} + +static void +android_cpuInitDummy(void) +{ + g_inited = 1; +} + +int +android_setCpu(int cpu_count, uint64_t cpu_features) +{ + /* Fail if the library was already initialized. */ + if (g_inited) + return 0; + + android_cpuInitFamily(); + g_cpuCount = (cpu_count <= 0 ? 1 : cpu_count); + g_cpuFeatures = cpu_features; + pthread_once(&g_once, android_cpuInitDummy); + + return 1; +} + +#ifdef __arm__ +uint32_t +android_getCpuIdArm(void) +{ + pthread_once(&g_once, android_cpuInit); + return g_cpuIdArm; +} + +int +android_setCpuArm(int cpu_count, uint64_t cpu_features, uint32_t cpu_id) +{ + if (!android_setCpu(cpu_count, cpu_features)) + return 0; + + g_cpuIdArm = cpu_id; + return 1; +} +#endif /* __arm__ */ + +/* + * Technical note: Making sense of ARM's FPU architecture versions. + * + * FPA was ARM's first attempt at an FPU architecture. There is no Android + * device that actually uses it since this technology was already obsolete + * when the project started. If you see references to FPA instructions + * somewhere, you can be sure that this doesn't apply to Android at all. + * + * FPA was followed by "VFP", soon renamed "VFPv1" due to the emergence of + * new versions / additions to it. ARM considers this obsolete right now, + * and no known Android device implements it either. + * + * VFPv2 added a few instructions to VFPv1, and is an *optional* extension + * supported by some ARMv5TE, ARMv6 and ARMv6T2 CPUs. Note that a device + * supporting the 'armeabi' ABI doesn't necessarily support these. + * + * VFPv3-D16 adds a few instructions on top of VFPv2 and is typically used + * on ARMv7-A CPUs which implement a FPU. Note that it is also mandated + * by the Android 'armeabi-v7a' ABI. The -D16 suffix in its name means + * that it provides 16 double-precision FPU registers (d0-d15) and 32 + * single-precision ones (s0-s31) which happen to be mapped to the same + * register banks. + * + * VFPv3-D32 is the name of an extension to VFPv3-D16 that provides 16 + * additional double precision registers (d16-d31). Note that there are + * still only 32 single precision registers. + * + * VFPv3xD is a *subset* of VFPv3-D16 that only provides single-precision + * registers. It is only used on ARMv7-M (i.e. on micro-controllers) which + * are not supported by Android. Note that it is not compatible with VFPv2. + * + * NOTE: The term 'VFPv3' usually designate either VFPv3-D16 or VFPv3-D32 + * depending on context. For example GCC uses it for VFPv3-D32, but + * the Linux kernel code uses it for VFPv3-D16 (especially in + * /proc/cpuinfo). Always try to use the full designation when + * possible. + * + * NEON, a.k.a. "ARM Advanced SIMD" is an extension that provides + * instructions to perform parallel computations on vectors of 8, 16, + * 32, 64 and 128 bit quantities. NEON requires VFPv32-D32 since all + * NEON registers are also mapped to the same register banks. + * + * VFPv4-D16, adds a few instructions on top of VFPv3-D16 in order to + * perform fused multiply-accumulate on VFP registers, as well as + * half-precision (16-bit) conversion operations. + * + * VFPv4-D32 is VFPv4-D16 with 32, instead of 16, FPU double precision + * registers. + * + * VPFv4-NEON is VFPv4-D32 with NEON instructions. It also adds fused + * multiply-accumulate instructions that work on the NEON registers. + * + * NOTE: Similarly, "VFPv4" might either reference VFPv4-D16 or VFPv4-D32 + * depending on context. + * + * The following information was determined by scanning the binutils-2.22 + * sources: + * + * Basic VFP instruction subsets: + * + * #define FPU_VFP_EXT_V1xD 0x08000000 // Base VFP instruction set. + * #define FPU_VFP_EXT_V1 0x04000000 // Double-precision insns. + * #define FPU_VFP_EXT_V2 0x02000000 // ARM10E VFPr1. + * #define FPU_VFP_EXT_V3xD 0x01000000 // VFPv3 single-precision. + * #define FPU_VFP_EXT_V3 0x00800000 // VFPv3 double-precision. + * #define FPU_NEON_EXT_V1 0x00400000 // Neon (SIMD) insns. + * #define FPU_VFP_EXT_D32 0x00200000 // Registers D16-D31. + * #define FPU_VFP_EXT_FP16 0x00100000 // Half-precision extensions. + * #define FPU_NEON_EXT_FMA 0x00080000 // Neon fused multiply-add + * #define FPU_VFP_EXT_FMA 0x00040000 // VFP fused multiply-add + * + * FPU types (excluding NEON) + * + * FPU_VFP_V1xD (EXT_V1xD) + * | + * +--------------------------+ + * | | + * FPU_VFP_V1 (+EXT_V1) FPU_VFP_V3xD (+EXT_V2+EXT_V3xD) + * | | + * | | + * FPU_VFP_V2 (+EXT_V2) FPU_VFP_V4_SP_D16 (+EXT_FP16+EXT_FMA) + * | + * FPU_VFP_V3D16 (+EXT_Vx3D+EXT_V3) + * | + * +--------------------------+ + * | | + * FPU_VFP_V3 (+EXT_D32) FPU_VFP_V4D16 (+EXT_FP16+EXT_FMA) + * | | + * | FPU_VFP_V4 (+EXT_D32) + * | + * FPU_VFP_HARD (+EXT_FMA+NEON_EXT_FMA) + * + * VFP architectures: + * + * ARCH_VFP_V1xD (EXT_V1xD) + * | + * +------------------+ + * | | + * | ARCH_VFP_V3xD (+EXT_V2+EXT_V3xD) + * | | + * | ARCH_VFP_V3xD_FP16 (+EXT_FP16) + * | | + * | ARCH_VFP_V4_SP_D16 (+EXT_FMA) + * | + * ARCH_VFP_V1 (+EXT_V1) + * | + * ARCH_VFP_V2 (+EXT_V2) + * | + * ARCH_VFP_V3D16 (+EXT_V3xD+EXT_V3) + * | + * +-------------------+ + * | | + * | ARCH_VFP_V3D16_FP16 (+EXT_FP16) + * | + * +-------------------+ + * | | + * | ARCH_VFP_V4_D16 (+EXT_FP16+EXT_FMA) + * | | + * | ARCH_VFP_V4 (+EXT_D32) + * | | + * | ARCH_NEON_VFP_V4 (+EXT_NEON+EXT_NEON_FMA) + * | + * ARCH_VFP_V3 (+EXT_D32) + * | + * +-------------------+ + * | | + * | ARCH_VFP_V3_FP16 (+EXT_FP16) + * | + * ARCH_VFP_V3_PLUS_NEON_V1 (+EXT_NEON) + * | + * ARCH_NEON_FP16 (+EXT_FP16) + * + * -fpu=<name> values and their correspondance with FPU architectures above: + * + * {"vfp", FPU_ARCH_VFP_V2}, + * {"vfp9", FPU_ARCH_VFP_V2}, + * {"vfp3", FPU_ARCH_VFP_V3}, // For backwards compatbility. + * {"vfp10", FPU_ARCH_VFP_V2}, + * {"vfp10-r0", FPU_ARCH_VFP_V1}, + * {"vfpxd", FPU_ARCH_VFP_V1xD}, + * {"vfpv2", FPU_ARCH_VFP_V2}, + * {"vfpv3", FPU_ARCH_VFP_V3}, + * {"vfpv3-fp16", FPU_ARCH_VFP_V3_FP16}, + * {"vfpv3-d16", FPU_ARCH_VFP_V3D16}, + * {"vfpv3-d16-fp16", FPU_ARCH_VFP_V3D16_FP16}, + * {"vfpv3xd", FPU_ARCH_VFP_V3xD}, + * {"vfpv3xd-fp16", FPU_ARCH_VFP_V3xD_FP16}, + * {"neon", FPU_ARCH_VFP_V3_PLUS_NEON_V1}, + * {"neon-fp16", FPU_ARCH_NEON_FP16}, + * {"vfpv4", FPU_ARCH_VFP_V4}, + * {"vfpv4-d16", FPU_ARCH_VFP_V4D16}, + * {"fpv4-sp-d16", FPU_ARCH_VFP_V4_SP_D16}, + * {"neon-vfpv4", FPU_ARCH_NEON_VFP_V4}, + * + * + * Simplified diagram that only includes FPUs supported by Android: + * Only ARCH_VFP_V3D16 is actually mandated by the armeabi-v7a ABI, + * all others are optional and must be probed at runtime. + * + * ARCH_VFP_V3D16 (EXT_V1xD+EXT_V1+EXT_V2+EXT_V3xD+EXT_V3) + * | + * +-------------------+ + * | | + * | ARCH_VFP_V3D16_FP16 (+EXT_FP16) + * | + * +-------------------+ + * | | + * | ARCH_VFP_V4_D16 (+EXT_FP16+EXT_FMA) + * | | + * | ARCH_VFP_V4 (+EXT_D32) + * | | + * | ARCH_NEON_VFP_V4 (+EXT_NEON+EXT_NEON_FMA) + * | + * ARCH_VFP_V3 (+EXT_D32) + * | + * +-------------------+ + * | | + * | ARCH_VFP_V3_FP16 (+EXT_FP16) + * | + * ARCH_VFP_V3_PLUS_NEON_V1 (+EXT_NEON) + * | + * ARCH_NEON_FP16 (+EXT_FP16) + * + */ + +#endif // defined(__le32__) diff --git a/platform/android/cpu-features.h b/platform/android/cpu-features.h new file mode 100644 index 0000000000..01b7fe207c --- /dev/null +++ b/platform/android/cpu-features.h @@ -0,0 +1,214 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +#ifndef CPU_FEATURES_H +#define CPU_FEATURES_H + +#include <sys/cdefs.h> +#include <stdint.h> + +__BEGIN_DECLS + +typedef enum { + ANDROID_CPU_FAMILY_UNKNOWN = 0, + ANDROID_CPU_FAMILY_ARM, + ANDROID_CPU_FAMILY_X86, + ANDROID_CPU_FAMILY_MIPS, + ANDROID_CPU_FAMILY_ARM64, + ANDROID_CPU_FAMILY_X86_64, + ANDROID_CPU_FAMILY_MIPS64, + + ANDROID_CPU_FAMILY_MAX /* do not remove */ + +} AndroidCpuFamily; + +/* Return family of the device's CPU */ +extern AndroidCpuFamily android_getCpuFamily(void); + +/* The list of feature flags for ARM CPUs that can be recognized by the + * library. Value details are: + * + * VFPv2: + * CPU supports the VFPv2 instruction set. Many, but not all, ARMv6 CPUs + * support these instructions. VFPv2 is a subset of VFPv3 so this will + * be set whenever VFPv3 is set too. + * + * ARMv7: + * CPU supports the ARMv7-A basic instruction set. + * This feature is mandated by the 'armeabi-v7a' ABI. + * + * VFPv3: + * CPU supports the VFPv3-D16 instruction set, providing hardware FPU + * support for single and double precision floating point registers. + * Note that only 16 FPU registers are available by default, unless + * the D32 bit is set too. This feature is also mandated by the + * 'armeabi-v7a' ABI. + * + * VFP_D32: + * CPU VFP optional extension that provides 32 FPU registers, + * instead of 16. Note that ARM mandates this feature is the 'NEON' + * feature is implemented by the CPU. + * + * NEON: + * CPU FPU supports "ARM Advanced SIMD" instructions, also known as + * NEON. Note that this mandates the VFP_D32 feature as well, per the + * ARM Architecture specification. + * + * VFP_FP16: + * Half-width floating precision VFP extension. If set, the CPU + * supports instructions to perform floating-point operations on + * 16-bit registers. This is part of the VFPv4 specification, but + * not mandated by any Android ABI. + * + * VFP_FMA: + * Fused multiply-accumulate VFP instructions extension. Also part of + * the VFPv4 specification, but not mandated by any Android ABI. + * + * NEON_FMA: + * Fused multiply-accumulate NEON instructions extension. Optional + * extension from the VFPv4 specification, but not mandated by any + * Android ABI. + * + * IDIV_ARM: + * Integer division available in ARM mode. Only available + * on recent CPUs (e.g. Cortex-A15). + * + * IDIV_THUMB2: + * Integer division available in Thumb-2 mode. Only available + * on recent CPUs (e.g. Cortex-A15). + * + * iWMMXt: + * Optional extension that adds MMX registers and operations to an + * ARM CPU. This is only available on a few XScale-based CPU designs + * sold by Marvell. Pretty rare in practice. + * + * If you want to tell the compiler to generate code that targets one of + * the feature set above, you should probably use one of the following + * flags (for more details, see technical note at the end of this file): + * + * -mfpu=vfp + * -mfpu=vfpv2 + * These are equivalent and tell GCC to use VFPv2 instructions for + * floating-point operations. Use this if you want your code to + * run on *some* ARMv6 devices, and any ARMv7-A device supported + * by Android. + * + * Generated code requires VFPv2 feature. + * + * -mfpu=vfpv3-d16 + * Tell GCC to use VFPv3 instructions (using only 16 FPU registers). + * This should be generic code that runs on any CPU that supports the + * 'armeabi-v7a' Android ABI. Note that no ARMv6 CPU supports this. + * + * Generated code requires VFPv3 feature. + * + * -mfpu=vfpv3 + * Tell GCC to use VFPv3 instructions with 32 FPU registers. + * Generated code requires VFPv3|VFP_D32 features. + * + * -mfpu=neon + * Tell GCC to use VFPv3 instructions with 32 FPU registers, and + * also support NEON intrinsics (see <arm_neon.h>). + * Generated code requires VFPv3|VFP_D32|NEON features. + * + * -mfpu=vfpv4-d16 + * Generated code requires VFPv3|VFP_FP16|VFP_FMA features. + * + * -mfpu=vfpv4 + * Generated code requires VFPv3|VFP_FP16|VFP_FMA|VFP_D32 features. + * + * -mfpu=neon-vfpv4 + * Generated code requires VFPv3|VFP_FP16|VFP_FMA|VFP_D32|NEON|NEON_FMA + * features. + * + * -mcpu=cortex-a7 + * -mcpu=cortex-a15 + * Generated code requires VFPv3|VFP_FP16|VFP_FMA|VFP_D32| + * NEON|NEON_FMA|IDIV_ARM|IDIV_THUMB2 + * This flag implies -mfpu=neon-vfpv4. + * + * -mcpu=iwmmxt + * Allows the use of iWMMXt instrinsics with GCC. + */ +enum { + ANDROID_CPU_ARM_FEATURE_ARMv7 = (1 << 0), + ANDROID_CPU_ARM_FEATURE_VFPv3 = (1 << 1), + ANDROID_CPU_ARM_FEATURE_NEON = (1 << 2), + ANDROID_CPU_ARM_FEATURE_LDREX_STREX = (1 << 3), + ANDROID_CPU_ARM_FEATURE_VFPv2 = (1 << 4), + ANDROID_CPU_ARM_FEATURE_VFP_D32 = (1 << 5), + ANDROID_CPU_ARM_FEATURE_VFP_FP16 = (1 << 6), + ANDROID_CPU_ARM_FEATURE_VFP_FMA = (1 << 7), + ANDROID_CPU_ARM_FEATURE_NEON_FMA = (1 << 8), + ANDROID_CPU_ARM_FEATURE_IDIV_ARM = (1 << 9), + ANDROID_CPU_ARM_FEATURE_IDIV_THUMB2 = (1 << 10), + ANDROID_CPU_ARM_FEATURE_iWMMXt = (1 << 11), +}; + +enum { + ANDROID_CPU_X86_FEATURE_SSSE3 = (1 << 0), + ANDROID_CPU_X86_FEATURE_POPCNT = (1 << 1), + ANDROID_CPU_X86_FEATURE_MOVBE = (1 << 2), +}; + +extern uint64_t android_getCpuFeatures(void); +#define android_getCpuFeaturesExt android_getCpuFeatures + +/* Return the number of CPU cores detected on this device. */ +extern int android_getCpuCount(void); + +/* The following is used to force the CPU count and features + * mask in sandboxed processes. Under 4.1 and higher, these processes + * cannot access /proc, which is the only way to get information from + * the kernel about the current hardware (at least on ARM). + * + * It _must_ be called only once, and before any android_getCpuXXX + * function, any other case will fail. + * + * This function return 1 on success, and 0 on failure. + */ +extern int android_setCpu(int cpu_count, + uint64_t cpu_features); + +#ifdef __arm__ +/* Retrieve the ARM 32-bit CPUID value from the kernel. + * Note that this cannot work on sandboxed processes under 4.1 and + * higher, unless you called android_setCpuArm() before. + */ +extern uint32_t android_getCpuIdArm(void); + +/* An ARM-specific variant of android_setCpu() that also allows you + * to set the ARM CPUID field. + */ +extern int android_setCpuArm(int cpu_count, + uint64_t cpu_features, + uint32_t cpu_id); +#endif + +__END_DECLS + +#endif /* CPU_FEATURES_H */ diff --git a/platform/android/detect.py b/platform/android/detect.py index 26348be112..0c860c23b1 100644 --- a/platform/android/detect.py +++ b/platform/android/detect.py @@ -39,6 +39,8 @@ def get_flags(): ('nedmalloc', 'no'), ('builtin_zlib', 'no'), ('openssl','builtin'), #use builtin openssl + ('theora','no'), #use builtin openssl + ] diff --git a/platform/android/export/export.cpp b/platform/android/export/export.cpp index 22e6a5d864..aef223470a 100644 --- a/platform/android/export/export.cpp +++ b/platform/android/export/export.cpp @@ -378,8 +378,8 @@ bool EditorExportPlatformAndroid::_get(const StringName& p_name,Variant &r_ret) void EditorExportPlatformAndroid::_get_property_list( List<PropertyInfo> *p_list) const{ - p_list->push_back( PropertyInfo( Variant::STRING, "custom_package/debug", PROPERTY_HINT_FILE,"apk")); - p_list->push_back( PropertyInfo( Variant::STRING, "custom_package/release", PROPERTY_HINT_FILE,"apk")); + p_list->push_back( PropertyInfo( Variant::STRING, "custom_package/debug", PROPERTY_HINT_GLOBAL_FILE,"apk")); + p_list->push_back( PropertyInfo( Variant::STRING, "custom_package/release", PROPERTY_HINT_GLOBAL_FILE,"apk")); p_list->push_back( PropertyInfo( Variant::STRING, "command_line/extra_args")); p_list->push_back( PropertyInfo( Variant::INT, "version/code", PROPERTY_HINT_RANGE,"1,65535,1")); p_list->push_back( PropertyInfo( Variant::STRING, "version/name") ); diff --git a/platform/android/java/src/com/android/godot/GodotPaymentV3.java b/platform/android/java/src/com/android/godot/GodotPaymentV3.java index a459f8e15c..0fd102ac55 100644 --- a/platform/android/java/src/com/android/godot/GodotPaymentV3.java +++ b/platform/android/java/src/com/android/godot/GodotPaymentV3.java @@ -64,15 +64,15 @@ public class GodotPaymentV3 extends Godot.SingletonBase { public void callbackSuccess(String ticket, String signature){ - Log.d(this.getClass().getName(), "PRE-Send callback to purchase success"); +// Log.d(this.getClass().getName(), "PRE-Send callback to purchase success"); GodotLib.callobject(purchaseCallbackId, "purchase_success", new Object[]{ticket, signature}); - Log.d(this.getClass().getName(), "POST-Send callback to purchase success"); +// Log.d(this.getClass().getName(), "POST-Send callback to purchase success"); } public void callbackSuccessProductMassConsumed(String ticket, String signature, String sku){ - Log.d(this.getClass().getName(), "PRE-Send callback to consume success"); +// Log.d(this.getClass().getName(), "PRE-Send callback to consume success"); GodotLib.calldeferred(purchaseCallbackId, "consume_success", new Object[]{ticket, signature, sku}); - Log.d(this.getClass().getName(), "POST-Send callback to consume success"); +// Log.d(this.getClass().getName(), "POST-Send callback to consume success"); } public void callbackSuccessNoUnconsumedPurchases(){ diff --git a/platform/android/java_class_wrapper.cpp b/platform/android/java_class_wrapper.cpp new file mode 100644 index 0000000000..d4cf848484 --- /dev/null +++ b/platform/android/java_class_wrapper.cpp @@ -0,0 +1,1332 @@ +#include "java_class_wrapper.h" +#include "thread_jandroid.h" + + +bool JavaClass::_call_method(JavaObject* p_instance,const StringName& p_method,const Variant** p_args,int p_argcount,Variant::CallError &r_error,Variant& ret) { + + Map<StringName,List<MethodInfo> >::Element *M=methods.find(p_method); + if (!M) + return false; + + JNIEnv *env = ThreadAndroid::get_env(); + + MethodInfo *method=NULL; + for (List<MethodInfo>::Element *E=M->get().front();E;E=E->next()) { + + if (!p_instance && !E->get()._static) { + r_error.error=Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL; + continue; + } + + int pc = E->get().param_types.size(); + if (pc>p_argcount) { + + r_error.error=Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.argument=pc; + continue; + } + if (pc<p_argcount) { + + r_error.error=Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS; + r_error.argument=pc; + continue; + } + uint32_t *ptypes=E->get().param_types.ptr(); + bool valid=true; + + for(int i=0;i<pc;i++) { + + Variant::Type arg_expected=Variant::NIL; + switch(ptypes[i]) { + + case ARG_TYPE_VOID: { + //bug? + } break; + case ARG_TYPE_BOOLEAN: { + if (p_args[i]->get_type()!=Variant::BOOL) + arg_expected=Variant::BOOL; + } break; + case ARG_NUMBER_CLASS_BIT|ARG_TYPE_BYTE: + case ARG_NUMBER_CLASS_BIT|ARG_TYPE_CHAR: + case ARG_NUMBER_CLASS_BIT|ARG_TYPE_SHORT: + case ARG_NUMBER_CLASS_BIT|ARG_TYPE_INT: + case ARG_NUMBER_CLASS_BIT|ARG_TYPE_LONG: + case ARG_TYPE_BYTE: + case ARG_TYPE_CHAR: + case ARG_TYPE_SHORT: + case ARG_TYPE_INT: + case ARG_TYPE_LONG: { + + if (!p_args[i]->is_num()) + arg_expected=Variant::INT; + + } break; + case ARG_NUMBER_CLASS_BIT|ARG_TYPE_FLOAT: + case ARG_NUMBER_CLASS_BIT|ARG_TYPE_DOUBLE: + case ARG_TYPE_FLOAT: + case ARG_TYPE_DOUBLE: { + + if (!p_args[i]->is_num()) + arg_expected=Variant::REAL; + + } break; + case ARG_TYPE_STRING: { + + if (p_args[i]->get_type()!=Variant::STRING) + arg_expected=Variant::STRING; + + } break; + case ARG_TYPE_CLASS: { + + if (p_args[i]->get_type()!=Variant::OBJECT) + arg_expected=Variant::OBJECT; + else { + + Ref<Reference> ref = *p_args[i]; + if (!ref.is_null()) { + if (ref->cast_to<JavaObject>() ) { + + Ref<JavaObject> jo=ref; + //could be faster + jclass c = env->FindClass(E->get().param_sigs[i].operator String().utf8().get_data()); + if (!c || !env->IsInstanceOf(jo->instance,c)) { + + arg_expected=Variant::OBJECT; + } else { + //ok + } + } else { + arg_expected=Variant::OBJECT; + } + + } + } + + } break; + default: { + + if (p_args[i]->get_type()!=Variant::ARRAY) + arg_expected=Variant::ARRAY; + + } break; + + } + + if (arg_expected!=Variant::NIL) { + r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument=i; + r_error.expected=arg_expected; + valid=false; + break; + + } + + } + if (!valid) + continue; + + + method=&E->get(); + break; + + } + + if (!method) + return true; //no version convinces + + + + r_error.error=Variant::CallError::CALL_OK; + + jvalue *argv=NULL; + + if (method->param_types.size()) { + + argv=(jvalue*)alloca( sizeof(jvalue)*method->param_types.size() ); + } + + List<jobject> to_free; + for(int i=0;i<method->param_types.size();i++) { + + switch(method->param_types[i]) { + case ARG_TYPE_VOID: { + //can't happen + argv[i].l=NULL; //I hope this works + } break; + + case ARG_TYPE_BOOLEAN: { + argv[i].z=*p_args[i]; + } break; + case ARG_TYPE_BYTE: { + argv[i].b=*p_args[i]; + } break; + case ARG_TYPE_CHAR: { + argv[i].c=*p_args[i]; + } break; + case ARG_TYPE_SHORT: { + argv[i].s=*p_args[i]; + } break; + case ARG_TYPE_INT: { + argv[i].i=*p_args[i]; + } break; + case ARG_TYPE_LONG: { + argv[i].j=*p_args[i]; + } break; + case ARG_TYPE_FLOAT: { + argv[i].f=*p_args[i]; + } break; + case ARG_TYPE_DOUBLE: { + argv[i].d=*p_args[i]; + } break; + case ARG_NUMBER_CLASS_BIT|ARG_TYPE_BOOLEAN: { + jclass bclass = env->FindClass("java/lang/Boolean"); + jmethodID ctor = env->GetMethodID(bclass, "<init>", "(Z)V"); + jvalue val; + val.z = (bool)(*p_args[i]); + jobject obj = env->NewObjectA(bclass, ctor, &val); + argv[i].l = obj; + to_free.push_back(obj); + } break; + case ARG_NUMBER_CLASS_BIT|ARG_TYPE_BYTE: { + jclass bclass = env->FindClass("java/lang/Byte"); + jmethodID ctor = env->GetMethodID(bclass, "<init>", "(B)V"); + jvalue val; + val.b = (int)(*p_args[i]); + jobject obj = env->NewObjectA(bclass, ctor, &val); + argv[i].l = obj; + to_free.push_back(obj); + } break; + case ARG_NUMBER_CLASS_BIT|ARG_TYPE_CHAR: { + jclass bclass = env->FindClass("java/lang/Character"); + jmethodID ctor = env->GetMethodID(bclass, "<init>", "(C)V"); + jvalue val; + val.c = (int)(*p_args[i]); + jobject obj = env->NewObjectA(bclass, ctor, &val); + argv[i].l = obj; + to_free.push_back(obj); + } break; + case ARG_NUMBER_CLASS_BIT|ARG_TYPE_SHORT: { + jclass bclass = env->FindClass("java/lang/Short"); + jmethodID ctor = env->GetMethodID(bclass, "<init>", "(S)V"); + jvalue val; + val.s = (int)(*p_args[i]); + jobject obj = env->NewObjectA(bclass, ctor, &val); + argv[i].l = obj; + to_free.push_back(obj); + } break; + case ARG_NUMBER_CLASS_BIT|ARG_TYPE_INT: { + jclass bclass = env->FindClass("java/lang/Integer"); + jmethodID ctor = env->GetMethodID(bclass, "<init>", "(I)V"); + jvalue val; + val.i = (int)(*p_args[i]); + jobject obj = env->NewObjectA(bclass, ctor, &val); + argv[i].l = obj; + to_free.push_back(obj); + } break; + case ARG_NUMBER_CLASS_BIT|ARG_TYPE_LONG: { + jclass bclass = env->FindClass("java/lang/Long"); + jmethodID ctor = env->GetMethodID(bclass, "<init>", "(J)V"); + jvalue val; + val.j = (int64_t)(*p_args[i]); + jobject obj = env->NewObjectA(bclass, ctor, &val); + argv[i].l = obj; + to_free.push_back(obj); + } break; + case ARG_NUMBER_CLASS_BIT|ARG_TYPE_FLOAT: { + jclass bclass = env->FindClass("java/lang/Float"); + jmethodID ctor = env->GetMethodID(bclass, "<init>", "(F)V"); + jvalue val; + val.f = (float)(*p_args[i]); + jobject obj = env->NewObjectA(bclass, ctor, &val); + argv[i].l = obj; + to_free.push_back(obj); + } break; + case ARG_NUMBER_CLASS_BIT|ARG_TYPE_DOUBLE: { + jclass bclass = env->FindClass("java/lang/Double"); + jmethodID ctor = env->GetMethodID(bclass, "<init>", "(D)V"); + jvalue val; + val.d = (double)(*p_args[i]); + jobject obj = env->NewObjectA(bclass, ctor, &val); + argv[i].l = obj; + to_free.push_back(obj); + } break; + case ARG_TYPE_STRING: { + String s = *p_args[i]; + jstring jStr = env->NewStringUTF(s.utf8().get_data()); + argv[i].l=jStr; + to_free.push_back(jStr); + } break; + case ARG_TYPE_CLASS: { + + Ref<JavaObject> jo=*p_args[i]; + if (jo.is_valid()) { + + argv[i].l=jo->instance; + } else { + argv[i].l=NULL; //I hope this works + } + + } break; + case ARG_ARRAY_BIT|ARG_TYPE_BOOLEAN: { + + Array arr = *p_args[i]; + jbooleanArray a = env->NewBooleanArray(arr.size()); + for(int j=0;j<arr.size();j++) { + jboolean val = arr[j]; + env->SetBooleanArrayRegion(a,j,1,&val); + } + argv[i].l=a; + to_free.push_back(a); + + } break; + case ARG_ARRAY_BIT|ARG_TYPE_BYTE: { + + Array arr = *p_args[i]; + jbyteArray a = env->NewByteArray(arr.size()); + for(int j=0;j<arr.size();j++) { + jbyte val = arr[j]; + env->SetByteArrayRegion(a,j,1,&val); + } + argv[i].l=a; + to_free.push_back(a); + + + } break; + case ARG_ARRAY_BIT|ARG_TYPE_CHAR: { + + Array arr = *p_args[i]; + jcharArray a = env->NewCharArray(arr.size()); + for(int j=0;j<arr.size();j++) { + jchar val = arr[j]; + env->SetCharArrayRegion(a,j,1,&val); + } + argv[i].l=a; + to_free.push_back(a); + + } break; + case ARG_ARRAY_BIT|ARG_TYPE_SHORT: { + + Array arr = *p_args[i]; + jshortArray a = env->NewShortArray(arr.size()); + for(int j=0;j<arr.size();j++) { + jshort val = arr[j]; + env->SetShortArrayRegion(a,j,1,&val); + } + argv[i].l=a; + to_free.push_back(a); + + } break; + case ARG_ARRAY_BIT|ARG_TYPE_INT: { + + Array arr = *p_args[i]; + jintArray a = env->NewIntArray(arr.size()); + for(int j=0;j<arr.size();j++) { + jint val = arr[j]; + env->SetIntArrayRegion(a,j,1,&val); + } + argv[i].l=a; + to_free.push_back(a); + } break; + case ARG_ARRAY_BIT|ARG_TYPE_LONG: { + Array arr = *p_args[i]; + jlongArray a = env->NewLongArray(arr.size()); + for(int j=0;j<arr.size();j++) { + jlong val = arr[j]; + env->SetLongArrayRegion(a,j,1,&val); + } + argv[i].l=a; + to_free.push_back(a); + + } break; + case ARG_ARRAY_BIT|ARG_TYPE_FLOAT: { + + Array arr = *p_args[i]; + jfloatArray a = env->NewFloatArray(arr.size()); + for(int j=0;j<arr.size();j++) { + jfloat val = arr[j]; + env->SetFloatArrayRegion(a,j,1,&val); + } + argv[i].l=a; + to_free.push_back(a); + + + } break; + case ARG_ARRAY_BIT|ARG_TYPE_DOUBLE: { + + Array arr = *p_args[i]; + jdoubleArray a = env->NewDoubleArray(arr.size()); + for(int j=0;j<arr.size();j++) { + jdouble val = arr[j]; + env->SetDoubleArrayRegion(a,j,1,&val); + } + argv[i].l=a; + to_free.push_back(a); + + } break; + case ARG_ARRAY_BIT|ARG_TYPE_STRING: { + + Array arr = *p_args[i]; + jobjectArray a = env->NewObjectArray(arr.size(),env->FindClass("java/lang/String"),NULL); + for(int j=0;j<arr.size();j++) { + + String s = arr[j]; + jstring jStr = env->NewStringUTF(s.utf8().get_data()); + env->SetObjectArrayElement(a,j,jStr); + to_free.push_back(jStr); + } + + argv[i].l=a; + to_free.push_back(a); + } break; + case ARG_ARRAY_BIT|ARG_TYPE_CLASS: { + + argv[i].l=NULL; + } break; + } + } + + r_error.error=Variant::CallError::CALL_OK; + bool success=true; + + switch(method->return_type) { + + + case ARG_TYPE_VOID: { + if (method->_static) { + env->CallStaticVoidMethodA(_class,method->method,argv); + } else { + env->CallVoidMethodA(p_instance->instance,method->method,argv); + } + ret=Variant(); + + } break; + case ARG_TYPE_BOOLEAN: { + if (method->_static) { + ret=env->CallStaticBooleanMethodA(_class,method->method,argv); + } else { + ret=env->CallBooleanMethodA(p_instance->instance,method->method,argv); + } + } break; + case ARG_TYPE_BYTE: { + if (method->_static) { + ret=env->CallStaticByteMethodA(_class,method->method,argv); + } else { + ret=env->CallByteMethodA(p_instance->instance,method->method,argv); + } + } break; + case ARG_TYPE_CHAR: { + + if (method->_static) { + ret=env->CallStaticCharMethodA(_class,method->method,argv); + } else { + ret=env->CallCharMethodA(p_instance->instance,method->method,argv); + } + } break; + case ARG_TYPE_SHORT: { + + if (method->_static) { + ret=env->CallStaticShortMethodA(_class,method->method,argv); + } else { + ret=env->CallShortMethodA(p_instance->instance,method->method,argv); + } + + } break; + case ARG_TYPE_INT: { + + if (method->_static) { + ret=env->CallStaticIntMethodA(_class,method->method,argv); + } else { + ret=env->CallIntMethodA(p_instance->instance,method->method,argv); + } + + } break; + case ARG_TYPE_LONG: { + + if (method->_static) { + ret=env->CallStaticLongMethodA(_class,method->method,argv); + } else { + ret=env->CallLongMethodA(p_instance->instance,method->method,argv); + } + + } break; + case ARG_TYPE_FLOAT: { + + if (method->_static) { + ret=env->CallStaticFloatMethodA(_class,method->method,argv); + } else { + ret=env->CallFloatMethodA(p_instance->instance,method->method,argv); + } + + } break; + case ARG_TYPE_DOUBLE: { + + if (method->_static) { + ret=env->CallStaticDoubleMethodA(_class,method->method,argv); + } else { + ret=env->CallDoubleMethodA(p_instance->instance,method->method,argv); + } + + } break; + default: { + + jobject obj; + if (method->_static) { + obj=env->CallStaticObjectMethodA(_class,method->method,argv); + } else { + obj=env->CallObjectMethodA(p_instance->instance,method->method,argv); + } + + if (!obj) { + ret=Variant(); + } else { + + if (!_convert_object_to_variant(env, obj, ret,method->return_type)) { + ret=Variant(); + r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD; + success=false; + } + env->DeleteLocalRef(obj); + } + + } break; + + } + + for(List<jobject>::Element *E=to_free.front();E;E=E->next()) { + env->DeleteLocalRef(E->get()); + } + + return success; +} + +Variant JavaClass::call(const StringName& p_method,const Variant** p_args,int p_argcount,Variant::CallError &r_error) { + + Variant ret; + bool found = _call_method(NULL,p_method,p_args,p_argcount,r_error,ret); + if (found) { + return ret; + } + + return Reference::call(p_method,p_args,p_argcount,r_error); +} + +JavaClass::JavaClass() { + + +} + +///////////////////// + +Variant JavaObject::call(const StringName& p_method,const Variant** p_args,int p_argcount,Variant::CallError &r_error){ + + + return Variant(); +} + +JavaObject::JavaObject(const Ref<JavaClass>& p_base,jobject *p_instance) { + + +} + +JavaObject::~JavaObject(){ + + +} + + +//////////////////// + +void JavaClassWrapper::_bind_methods() { + + ObjectTypeDB::bind_method(_MD("wrap:JavaClass","name"),&JavaClassWrapper::wrap); +} + + +bool JavaClassWrapper::_get_type_sig(JNIEnv *env,jobject obj,uint32_t& sig,String&strsig) { + + jstring name2 = (jstring)env->CallObjectMethod(obj, Class_getName); + String str_type = env->GetStringUTFChars( name2, NULL ); + print_line("name: "+str_type); + env->DeleteLocalRef(name2); + uint32_t t=0; + + if (str_type.begins_with("[")) { + + t=JavaClass::ARG_ARRAY_BIT; + strsig="["; + str_type=str_type.substr(1,str_type.length()-1); + if (str_type.begins_with("[")) { + print_line("Nested arrays not supported for type: "+str_type); + return false; + } + if (str_type.begins_with("L")) { + str_type=str_type.substr(1,str_type.length()-2); //ok it's a class + } + } + + if (str_type=="void" || str_type=="V") { + t|=JavaClass::ARG_TYPE_VOID; + strsig+="V"; + } else if (str_type=="boolean" || str_type=="Z") { + t|=JavaClass::ARG_TYPE_BOOLEAN; + strsig+="Z"; + } else if (str_type=="byte" || str_type=="B") { + t|=JavaClass::ARG_TYPE_BYTE; + strsig+="B"; + } else if (str_type=="char" || str_type=="C") { + t|=JavaClass::ARG_TYPE_CHAR; + strsig+="C"; + } else if (str_type=="short" || str_type=="S") { + t|=JavaClass::ARG_TYPE_SHORT; + strsig+="S"; + } else if (str_type=="int" || str_type=="I") { + t|=JavaClass::ARG_TYPE_INT; + strsig+="I"; + } else if (str_type=="long" || str_type=="J") { + t|=JavaClass::ARG_TYPE_LONG; + strsig+="J"; + } else if (str_type=="float" || str_type=="F") { + t|=JavaClass::ARG_TYPE_FLOAT; + strsig+="F"; + } else if (str_type=="double" || str_type=="D") { + t|=JavaClass::ARG_TYPE_DOUBLE; + strsig+="D"; + } else if (str_type=="java.lang.String") { + t|=JavaClass::ARG_TYPE_STRING; + strsig+="Ljava/lang/String;"; + } else if (str_type=="java.lang.Boolean") { + t|=JavaClass::ARG_TYPE_BOOLEAN|JavaClass::ARG_NUMBER_CLASS_BIT; + strsig+="Ljava/lang/Boolean;"; + } else if (str_type=="java.lang.Byte") { + t|=JavaClass::ARG_TYPE_BYTE|JavaClass::ARG_NUMBER_CLASS_BIT; + strsig+="Ljava/lang/Byte;"; + } else if (str_type=="java.lang.Character") { + t|=JavaClass::ARG_TYPE_CHAR|JavaClass::ARG_NUMBER_CLASS_BIT; + strsig+="Ljava/lang/Character;"; + } else if (str_type=="java.lang.Short") { + t|=JavaClass::ARG_TYPE_SHORT|JavaClass::ARG_NUMBER_CLASS_BIT; + strsig+="Ljava/lang/Short;"; + } else if (str_type=="java.lang.Integer") { + t|=JavaClass::ARG_TYPE_INT|JavaClass::ARG_NUMBER_CLASS_BIT; + strsig+="Ljava/lang/Integer;"; + } else if (str_type=="java.lang.Long") { + t|=JavaClass::ARG_TYPE_LONG|JavaClass::ARG_NUMBER_CLASS_BIT; + strsig+="Ljava/lang/Long;"; + } else if (str_type=="java.lang.Float") { + t|=JavaClass::ARG_TYPE_FLOAT|JavaClass::ARG_NUMBER_CLASS_BIT; + strsig+="Ljava/lang/Float;"; + } else if (str_type=="java.lang.Double") { + t|=JavaClass::ARG_TYPE_DOUBLE|JavaClass::ARG_NUMBER_CLASS_BIT; + strsig+="Ljava/lang/Double;"; + } else { + //a class likely + strsig+="L"+str_type.replace(".","/")+";"; + t|=JavaClass::ARG_TYPE_CLASS; + } + + sig=t; + + + return true; + +} + +bool JavaClass::_convert_object_to_variant(JNIEnv * env, jobject obj, Variant& var,uint32_t p_sig) { + + if (!obj) { + var=Variant(); //seems null is just null... + return true; + } + + + switch(p_sig) { + + case ARG_TYPE_VOID: { + + return Variant(); + } break; + case ARG_TYPE_BOOLEAN|ARG_NUMBER_CLASS_BIT: { + + var = env->CallBooleanMethod(obj, JavaClassWrapper::singleton->Boolean_booleanValue); + return true; + } break; + case ARG_TYPE_BYTE|ARG_NUMBER_CLASS_BIT: { + + var = env->CallByteMethod(obj, JavaClassWrapper::singleton->Byte_byteValue); + return true; + + } break; + case ARG_TYPE_CHAR|ARG_NUMBER_CLASS_BIT: { + + var = env->CallCharMethod(obj, JavaClassWrapper::singleton->Character_characterValue); + return true; + + } break; + case ARG_TYPE_SHORT|ARG_NUMBER_CLASS_BIT: { + + var = env->CallShortMethod(obj, JavaClassWrapper::singleton->Short_shortValue); + return true; + + } break; + case ARG_TYPE_INT|ARG_NUMBER_CLASS_BIT: { + + var = env->CallIntMethod(obj, JavaClassWrapper::singleton->Integer_integerValue); + return true; + + } break; + case ARG_TYPE_LONG|ARG_NUMBER_CLASS_BIT: { + + var = env->CallLongMethod(obj, JavaClassWrapper::singleton->Long_longValue); + return true; + + } break; + case ARG_TYPE_FLOAT|ARG_NUMBER_CLASS_BIT: { + + var = env->CallFloatMethod(obj, JavaClassWrapper::singleton->Float_floatValue); + return true; + + } break; + case ARG_TYPE_DOUBLE|ARG_NUMBER_CLASS_BIT: { + + var = env->CallDoubleMethod(obj, JavaClassWrapper::singleton->Double_doubleValue); + return true; + } break; + case ARG_TYPE_STRING: { + + var = String::utf8(env->GetStringUTFChars( (jstring)obj, NULL )); + return true; + } break; + case ARG_TYPE_CLASS: { + + return false; + } break; + case ARG_ARRAY_BIT|ARG_TYPE_VOID: { + + var = Array(); // ? + return true; + } break; + case ARG_ARRAY_BIT|ARG_TYPE_BOOLEAN: { + + Array ret; + jobjectArray arr = (jobjectArray)obj; + + int count = env->GetArrayLength(arr); + + for (int i=0; i<count; i++) { + + jboolean val; + env->GetBooleanArrayRegion((jbooleanArray)arr,0,1,&val); + ret.push_back(val); + } + + var=ret; + return true; + + } break; + case ARG_ARRAY_BIT|ARG_TYPE_BYTE: { + + Array ret; + jobjectArray arr = (jobjectArray)obj; + + int count = env->GetArrayLength(arr); + + for (int i=0; i<count; i++) { + + jbyte val; + env->GetByteArrayRegion((jbyteArray)arr,0,1,&val); + ret.push_back(val); + } + + var=ret; + return true; + } break; + case ARG_ARRAY_BIT|ARG_TYPE_CHAR: { + Array ret; + jobjectArray arr = (jobjectArray)obj; + + int count = env->GetArrayLength(arr); + + for (int i=0; i<count; i++) { + + jchar val; + env->GetCharArrayRegion((jcharArray)arr,0,1,&val); + ret.push_back(val); + } + + var=ret; + return true; + } break; + case ARG_ARRAY_BIT|ARG_TYPE_SHORT: { + Array ret; + jobjectArray arr = (jobjectArray)obj; + + int count = env->GetArrayLength(arr); + + for (int i=0; i<count; i++) { + + jshort val; + env->GetShortArrayRegion((jshortArray)arr,0,1,&val); + ret.push_back(val); + } + + var=ret; + return true; + } break; + case ARG_ARRAY_BIT|ARG_TYPE_INT: { + Array ret; + jobjectArray arr = (jobjectArray)obj; + + int count = env->GetArrayLength(arr); + + for (int i=0; i<count; i++) { + + jint val; + env->GetIntArrayRegion((jintArray)arr,0,1,&val); + ret.push_back(val); + } + + var=ret; + return true; + } break; + case ARG_ARRAY_BIT|ARG_TYPE_LONG: { + Array ret; + jobjectArray arr = (jobjectArray)obj; + + int count = env->GetArrayLength(arr); + + for (int i=0; i<count; i++) { + + jlong val; + env->GetLongArrayRegion((jlongArray)arr,0,1,&val); + ret.push_back(val); + } + + var=ret; + return true; + } break; + case ARG_ARRAY_BIT|ARG_TYPE_FLOAT: { + Array ret; + jobjectArray arr = (jobjectArray)obj; + + int count = env->GetArrayLength(arr); + + for (int i=0; i<count; i++) { + + jfloat val; + env->GetFloatArrayRegion((jfloatArray)arr,0,1,&val); + ret.push_back(val); + } + + var=ret; + return true; + } break; + case ARG_ARRAY_BIT|ARG_TYPE_DOUBLE: { + Array ret; + jobjectArray arr = (jobjectArray)obj; + + int count = env->GetArrayLength(arr); + + for (int i=0; i<count; i++) { + + jdouble val; + env->GetDoubleArrayRegion((jdoubleArray)arr,0,1,&val); + ret.push_back(val); + } + + var=ret; + return true; + } break; + case ARG_NUMBER_CLASS_BIT|ARG_ARRAY_BIT|ARG_TYPE_BOOLEAN: { + + Array ret; + jobjectArray arr = (jobjectArray)obj; + + int count = env->GetArrayLength(arr); + + for (int i=0; i<count; i++) { + + jobject o = env->GetObjectArrayElement(arr, i); + if (!o) + ret.push_back(Variant()); + else { + bool val = env->CallBooleanMethod(o, JavaClassWrapper::singleton->Boolean_booleanValue); + ret.push_back(val); + + } + env->DeleteLocalRef(o); + } + + var=ret; + return true; + + } break; + case ARG_NUMBER_CLASS_BIT|ARG_ARRAY_BIT|ARG_TYPE_BYTE: { + + Array ret; + jobjectArray arr = (jobjectArray)obj; + + int count = env->GetArrayLength(arr); + + for (int i=0; i<count; i++) { + + jobject o = env->GetObjectArrayElement(arr, i); + if (!o) + ret.push_back(Variant()); + else { + int val = env->CallByteMethod(o, JavaClassWrapper::singleton->Byte_byteValue); + ret.push_back(val); + + } + env->DeleteLocalRef(o); + } + + var=ret; + return true; + } break; + case ARG_NUMBER_CLASS_BIT|ARG_ARRAY_BIT|ARG_TYPE_CHAR: { + + Array ret; + jobjectArray arr = (jobjectArray)obj; + + int count = env->GetArrayLength(arr); + + for (int i=0; i<count; i++) { + + jobject o = env->GetObjectArrayElement(arr, i); + if (!o) + ret.push_back(Variant()); + else { + int val = env->CallCharMethod(o, JavaClassWrapper::singleton->Character_characterValue); + ret.push_back(val); + + } + env->DeleteLocalRef(o); + } + + var=ret; + return true; + } break; + case ARG_NUMBER_CLASS_BIT|ARG_ARRAY_BIT|ARG_TYPE_SHORT: { + + Array ret; + jobjectArray arr = (jobjectArray)obj; + + int count = env->GetArrayLength(arr); + + for (int i=0; i<count; i++) { + + jobject o = env->GetObjectArrayElement(arr, i); + if (!o) + ret.push_back(Variant()); + else { + int val = env->CallShortMethod(o, JavaClassWrapper::singleton->Short_shortValue); + ret.push_back(val); + + } + env->DeleteLocalRef(o); + } + + var=ret; + return true; + } break; + case ARG_NUMBER_CLASS_BIT|ARG_ARRAY_BIT|ARG_TYPE_INT: { + + Array ret; + jobjectArray arr = (jobjectArray)obj; + + int count = env->GetArrayLength(arr); + + for (int i=0; i<count; i++) { + + jobject o = env->GetObjectArrayElement(arr, i); + if (!o) + ret.push_back(Variant()); + else { + int val = env->CallIntMethod(o, JavaClassWrapper::singleton->Integer_integerValue); + ret.push_back(val); + + } + env->DeleteLocalRef(o); + } + + var=ret; + return true; + } break; + case ARG_NUMBER_CLASS_BIT|ARG_ARRAY_BIT|ARG_TYPE_LONG: { + + Array ret; + jobjectArray arr = (jobjectArray)obj; + + int count = env->GetArrayLength(arr); + + for (int i=0; i<count; i++) { + + jobject o = env->GetObjectArrayElement(arr, i); + if (!o) + ret.push_back(Variant()); + else { + int64_t val = env->CallLongMethod(o, JavaClassWrapper::singleton->Long_longValue); + ret.push_back(val); + + } + env->DeleteLocalRef(o); + } + + var=ret; + return true; + } break; + case ARG_NUMBER_CLASS_BIT|ARG_ARRAY_BIT|ARG_TYPE_FLOAT: { + + Array ret; + jobjectArray arr = (jobjectArray)obj; + + int count = env->GetArrayLength(arr); + + for (int i=0; i<count; i++) { + + jobject o = env->GetObjectArrayElement(arr, i); + if (!o) + ret.push_back(Variant()); + else { + float val = env->CallFloatMethod(o, JavaClassWrapper::singleton->Float_floatValue); + ret.push_back(val); + + } + env->DeleteLocalRef(o); + } + + var=ret; + return true; + } break; + case ARG_NUMBER_CLASS_BIT|ARG_ARRAY_BIT|ARG_TYPE_DOUBLE: { + Array ret; + jobjectArray arr = (jobjectArray)obj; + + int count = env->GetArrayLength(arr); + + for (int i=0; i<count; i++) { + + jobject o = env->GetObjectArrayElement(arr, i); + if (!o) + ret.push_back(Variant()); + else { + double val = env->CallDoubleMethod(o, JavaClassWrapper::singleton->Double_doubleValue); + ret.push_back(val); + + } + env->DeleteLocalRef(o); + } + + var=ret; + return true; + } break; + + case ARG_ARRAY_BIT|ARG_TYPE_STRING: { + + Array ret; + jobjectArray arr = (jobjectArray)obj; + + int count = env->GetArrayLength(arr); + + for (int i=0; i<count; i++) { + + jobject o = env->GetObjectArrayElement(arr, i); + if (!o) + ret.push_back(Variant()); + else { + String val = String::utf8(env->GetStringUTFChars( (jstring)o, NULL )); + ret.push_back(val); + + } + env->DeleteLocalRef(o); + } + + var=ret; + return true; + } break; + case ARG_ARRAY_BIT|ARG_TYPE_CLASS: { + + } break; + } + + return false; + +} + + +Ref<JavaClass> JavaClassWrapper::wrap(const String& p_class) { + + if (class_cache.has(p_class)) + return class_cache[p_class]; + + + JNIEnv *env = ThreadAndroid::get_env(); + + jclass bclass = env->FindClass(p_class.utf8().get_data()); + ERR_FAIL_COND_V(!bclass,Ref<JavaClass>()); + + //jmethodID getDeclaredMethods = env->GetMethodID(bclass,"getDeclaredMethods", "()[Ljava/lang/reflect/Method;"); + + //ERR_FAIL_COND_V(!getDeclaredMethods,Ref<JavaClass>()); + + jobjectArray methods = (jobjectArray)env->CallObjectMethod(bclass, getDeclaredMethods); + + ERR_FAIL_COND_V(!methods,Ref<JavaClass>()); + + + Ref<JavaClass> java_class = memnew( JavaClass ); + + int count = env->GetArrayLength(methods); + + for (int i=0; i<count; i++) { + + jobject obj = env->GetObjectArrayElement(methods, i); + ERR_CONTINUE(!obj); + + + jstring name = (jstring)env->CallObjectMethod(obj, getName); + String str_method = env->GetStringUTFChars( name, NULL ); + env->DeleteLocalRef(name); + + Vector<String> params; + + jint mods = env->CallIntMethod(obj,getModifiers); + + if (!(mods&0x0001)) { + env->DeleteLocalRef(obj); + continue; //not public bye + } + + + + jobjectArray param_types = (jobjectArray)env->CallObjectMethod(obj, getParameterTypes); + int count2=env->GetArrayLength(param_types); + + if (!java_class->methods.has(str_method)) { + java_class->methods[str_method]=List<JavaClass::MethodInfo>(); + } + + JavaClass::MethodInfo mi; + mi._static = (mods&0x8)!=0; + bool valid=true; + String signature="("; + + for(int j=0;j<count2;j++) { + + jobject obj2 = env->GetObjectArrayElement(param_types, j); + String strsig; + uint32_t sig=0; + if (!_get_type_sig(env,obj2,sig,strsig)) { + valid=false; + env->DeleteLocalRef(obj2); + break; + } + signature+=strsig; + mi.param_types.push_back(sig); + mi.param_sigs.push_back(strsig); + env->DeleteLocalRef(obj2); + + } + + if (!valid) { + print_line("Method Can't be bound (unsupported arguments): "+p_class+"::"+str_method); + env->DeleteLocalRef(obj); + env->DeleteLocalRef(param_types); + continue; + } + + signature+=")"; + + jobject return_type = (jobject)env->CallObjectMethod(obj, getReturnType); + + + String strsig; + uint32_t sig=0; + if (!_get_type_sig(env,return_type,sig,strsig)) { + print_line("Method Can't be bound (unsupported return type): "+p_class+"::"+str_method); + env->DeleteLocalRef(obj); + env->DeleteLocalRef(param_types); + env->DeleteLocalRef(return_type); + continue; + } + + signature+=strsig; + mi.return_type=sig; + + print_line("METHOD: "+str_method+" SIG: "+signature+" static: "+itos(mi._static)); + + bool discard=false; + + for(List<JavaClass::MethodInfo>::Element *E=java_class->methods[str_method].front();E;E=E->next()) { + + float new_likeliness=0; + float existing_likeliness=0; + + if (E->get().param_types.size()!=mi.param_types.size()) + continue; + bool valid=true; + for(int j=0;j<E->get().param_types.size();j++) { + + Variant::Type _new; + float new_l; + Variant::Type existing; + float existing_l; + JavaClass::_convert_to_variant_type(E->get().param_types[j],existing,existing_l); + JavaClass::_convert_to_variant_type(mi.param_types[j],_new,new_l); + if (_new!=existing) { + valid=false; + break; + } + new_likeliness+=new_l; + existing_likeliness=existing_l; + + } + + if (!valid) + continue; + + if (new_likeliness>existing_likeliness) { + java_class->methods[str_method].erase(E); + print_line("replace old"); + break; + } else { + discard=true; + print_line("old is better"); + } + + + } + + if (!discard) { + if (mi._static) + mi.method = env->GetStaticMethodID(bclass, str_method.utf8().get_data(), signature.utf8().get_data()); + else + mi.method = env->GetMethodID(bclass, str_method.utf8().get_data(), signature.utf8().get_data()); + + ERR_CONTINUE(!mi.method); + + java_class->methods[str_method].push_back(mi); + } + + env->DeleteLocalRef(obj); + env->DeleteLocalRef(param_types); + env->DeleteLocalRef(return_type); + + + + + //args[i] = _jobject_to_variant(env, obj); +// print_line("\targ"+itos(i)+": "+Variant::get_type_name(args[i].get_type())); + + }; + + env->DeleteLocalRef(methods); + + jobjectArray fields = (jobjectArray)env->CallObjectMethod(bclass, getFields); + + count = env->GetArrayLength(fields); + + for (int i=0; i<count; i++) { + + jobject obj = env->GetObjectArrayElement(fields, i); + ERR_CONTINUE(!obj); + + jstring name = (jstring)env->CallObjectMethod(obj, Field_getName); + String str_field = env->GetStringUTFChars( name, NULL ); + env->DeleteLocalRef(name); + print_line("FIELD: "+str_field); + int mods = env->CallIntMethod(obj,Field_getModifiers); + if ((mods&0x8) && (mods&0x10) && (mods&0x1)) { //static final public! + + jobject objc = env->CallObjectMethod(obj, Field_get,NULL); + if (objc) { + + + uint32_t sig; + String strsig; + jclass cl = env->GetObjectClass(objc); + if (JavaClassWrapper::_get_type_sig(env,cl,sig,strsig)) { + + if ((sig&JavaClass::ARG_TYPE_MASK)<=JavaClass::ARG_TYPE_STRING) { + + Variant value; + if (JavaClass::_convert_object_to_variant(env,objc,value,sig)) { + + java_class->constant_map[str_field]=value; + } + } + } + + env->DeleteLocalRef(cl); + } + + + env->DeleteLocalRef(objc); + + } + env->DeleteLocalRef(obj); + } + + env->DeleteLocalRef(fields); + + + return Ref<JavaClass>(); +} + +JavaClassWrapper *JavaClassWrapper::singleton=NULL; + +JavaClassWrapper::JavaClassWrapper(jobject p_activity) { + + singleton=this; + + JNIEnv *env = ThreadAndroid::get_env(); + + jclass activityClass = env->FindClass("com/android/godot/Godot"); + jmethodID getClassLoader = env->GetMethodID(activityClass,"getClassLoader", "()Ljava/lang/ClassLoader;"); + classLoader = env->CallObjectMethod(p_activity, getClassLoader); + classLoader=(jclass)env->NewGlobalRef(classLoader); + jclass classLoaderClass = env->FindClass("java/lang/ClassLoader"); + findClass = env->GetMethodID(classLoaderClass, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"); + + jclass bclass = env->FindClass("java/lang/Class"); + getDeclaredMethods = env->GetMethodID(bclass,"getDeclaredMethods", "()[Ljava/lang/reflect/Method;"); + getFields = env->GetMethodID(bclass,"getFields", "()[Ljava/lang/reflect/Field;"); + Class_getName = env->GetMethodID(bclass,"getName", "()Ljava/lang/String;"); + // + bclass = env->FindClass("java/lang/reflect/Method"); + getParameterTypes = env->GetMethodID(bclass,"getParameterTypes", "()[Ljava/lang/Class;"); + getReturnType = env->GetMethodID(bclass,"getReturnType", "()Ljava/lang/Class;"); + getName = env->GetMethodID(bclass,"getName", "()Ljava/lang/String;"); + getModifiers = env->GetMethodID(bclass,"getModifiers", "()I"); + /// + bclass = env->FindClass("java/lang/reflect/Field"); + Field_getName = env->GetMethodID(bclass,"getName", "()Ljava/lang/String;"); + Field_getModifiers = env->GetMethodID(bclass,"getModifiers", "()I"); + Field_get = env->GetMethodID(bclass,"get", "(Ljava/lang/Object;)Ljava/lang/Object;"); + // each + bclass = env->FindClass("java/lang/Boolean"); + Boolean_booleanValue = env->GetMethodID(bclass, "booleanValue", "()Z"); + + bclass = env->FindClass("java/lang/Byte"); + Byte_byteValue = env->GetMethodID(bclass, "byteValue", "()B"); + + bclass = env->FindClass("java/lang/Character"); + Character_characterValue = env->GetMethodID(bclass, "charValue", "()C"); + + bclass = env->FindClass("java/lang/Short"); + Short_shortValue = env->GetMethodID(bclass, "shortValue", "()S"); + + bclass = env->FindClass("java/lang/Integer"); + Integer_integerValue = env->GetMethodID(bclass, "intValue", "()I"); + + bclass = env->FindClass("java/lang/Long"); + Long_longValue = env->GetMethodID(bclass, "longValue", "()J"); + + bclass = env->FindClass("java/lang/Float"); + Float_floatValue = env->GetMethodID(bclass, "floatValue", "()F"); + + bclass = env->FindClass("java/lang/Double"); + Double_doubleValue = env->GetMethodID(bclass, "doubleValue", "()D"); + + +} diff --git a/platform/android/java_class_wrapper.h b/platform/android/java_class_wrapper.h new file mode 100644 index 0000000000..d5d8bd5be8 --- /dev/null +++ b/platform/android/java_class_wrapper.h @@ -0,0 +1,168 @@ +#ifndef JAVA_CLASS_WRAPPER_H +#define JAVA_CLASS_WRAPPER_H + +#include "reference.h" +#include <jni.h> +#include <android/log.h> + +class JavaObject; + +class JavaClass : public Reference { + + OBJ_TYPE(JavaClass,Reference); + + enum ArgumentType { + + ARG_TYPE_VOID, + ARG_TYPE_BOOLEAN, + ARG_TYPE_BYTE, + ARG_TYPE_CHAR, + ARG_TYPE_SHORT, + ARG_TYPE_INT, + ARG_TYPE_LONG, + ARG_TYPE_FLOAT, + ARG_TYPE_DOUBLE, + ARG_TYPE_STRING, //special case + ARG_TYPE_CLASS, + ARG_ARRAY_BIT=1<<16, + ARG_NUMBER_CLASS_BIT=1<<17, + ARG_TYPE_MASK=(1<<16)-1 + }; + + + Map<StringName,Variant> constant_map; + + struct MethodInfo { + + bool _static; + Vector<uint32_t> param_types; + Vector<StringName> param_sigs; + uint32_t return_type; + jmethodID method; + + }; + + _FORCE_INLINE_ static void _convert_to_variant_type(int p_sig, Variant::Type& r_type, float& likelyhood) { + + likelyhood=1.0; + r_type=Variant::NIL; + + switch(p_sig) { + + case ARG_TYPE_VOID: r_type=Variant::NIL; break; + case ARG_TYPE_BOOLEAN|ARG_NUMBER_CLASS_BIT: + case ARG_TYPE_BOOLEAN: r_type=Variant::BOOL; break; + case ARG_TYPE_BYTE|ARG_NUMBER_CLASS_BIT: + case ARG_TYPE_BYTE: r_type=Variant::INT; likelyhood=0.1; break; + case ARG_TYPE_CHAR|ARG_NUMBER_CLASS_BIT: + case ARG_TYPE_CHAR: r_type=Variant::INT; likelyhood=0.2; break; + case ARG_TYPE_SHORT|ARG_NUMBER_CLASS_BIT: + case ARG_TYPE_SHORT: r_type=Variant::INT; likelyhood=0.3; break; + case ARG_TYPE_INT|ARG_NUMBER_CLASS_BIT: + case ARG_TYPE_INT: r_type=Variant::INT; likelyhood=1.0; break; + case ARG_TYPE_LONG|ARG_NUMBER_CLASS_BIT: + case ARG_TYPE_LONG: r_type=Variant::INT; likelyhood=0.5; break; + case ARG_TYPE_FLOAT|ARG_NUMBER_CLASS_BIT: + case ARG_TYPE_FLOAT: r_type=Variant::REAL; likelyhood=1.0; break; + case ARG_TYPE_DOUBLE|ARG_NUMBER_CLASS_BIT: + case ARG_TYPE_DOUBLE: r_type=Variant::REAL; likelyhood=0.5; break; + case ARG_TYPE_STRING: r_type=Variant::STRING; break; + case ARG_TYPE_CLASS: r_type=Variant::OBJECT; break; + case ARG_ARRAY_BIT|ARG_TYPE_VOID: r_type=Variant::NIL; break; + case ARG_ARRAY_BIT|ARG_TYPE_BOOLEAN: r_type=Variant::ARRAY; break; + case ARG_ARRAY_BIT|ARG_TYPE_BYTE: r_type=Variant::RAW_ARRAY; likelyhood=1.0; break; + case ARG_ARRAY_BIT|ARG_TYPE_CHAR: r_type=Variant::RAW_ARRAY; likelyhood=0.5; break; + case ARG_ARRAY_BIT|ARG_TYPE_SHORT: r_type=Variant::INT_ARRAY; likelyhood=0.3; break; + case ARG_ARRAY_BIT|ARG_TYPE_INT: r_type=Variant::INT_ARRAY; likelyhood=1.0; break; + case ARG_ARRAY_BIT|ARG_TYPE_LONG: r_type=Variant::INT_ARRAY; likelyhood=0.5; break; + case ARG_ARRAY_BIT|ARG_TYPE_FLOAT: r_type=Variant::REAL_ARRAY; likelyhood=1.0; break; + case ARG_ARRAY_BIT|ARG_TYPE_DOUBLE: r_type=Variant::REAL_ARRAY; likelyhood=0.5; break; + case ARG_ARRAY_BIT|ARG_TYPE_STRING: r_type=Variant::STRING_ARRAY; break; + case ARG_ARRAY_BIT|ARG_TYPE_CLASS: r_type=Variant::ARRAY; break; + } + } + + _FORCE_INLINE_ static bool _convert_object_to_variant(JNIEnv * env, jobject obj, Variant& var,uint32_t p_sig); + + + + bool _call_method(JavaObject* p_instance,const StringName& p_method,const Variant** p_args,int p_argcount,Variant::CallError &r_error,Variant& ret); + +friend class JavaClassWrapper; + Map<StringName,List<MethodInfo> > methods; + jclass _class; + +public: + + virtual Variant call(const StringName& p_method,const Variant** p_args,int p_argcount,Variant::CallError &r_error); + + JavaClass(); + +}; + + +class JavaObject : public Reference { + + OBJ_TYPE(JavaObject,Reference); + + Ref<JavaClass> base_class; +friend class JavaClass; + + jobject instance; + +public: + + virtual Variant call(const StringName& p_method,const Variant** p_args,int p_argcount,Variant::CallError &r_error); + + JavaObject(const Ref<JavaClass>& p_base,jobject *p_instance); + ~JavaObject(); + +}; + + +class JavaClassWrapper : public Object { + + OBJ_TYPE(JavaClassWrapper,Object); + + + Map<String,Ref<JavaClass> > class_cache; +friend class JavaClass; + jclass activityClass; + jmethodID findClass; + jmethodID getDeclaredMethods; + jmethodID getFields; + jmethodID getParameterTypes; + jmethodID getReturnType; + jmethodID getModifiers; + jmethodID getName; + jmethodID Class_getName; + jmethodID Field_getName; + jmethodID Field_getModifiers; + jmethodID Field_get; + jmethodID Boolean_booleanValue; + jmethodID Byte_byteValue; + jmethodID Character_characterValue; + jmethodID Short_shortValue; + jmethodID Integer_integerValue; + jmethodID Long_longValue; + jmethodID Float_floatValue; + jmethodID Double_doubleValue; + jobject classLoader; + + bool _get_type_sig(JNIEnv *env, jobject obj, uint32_t& sig, String&strsig); + + static JavaClassWrapper *singleton; + +protected: + + static void _bind_methods(); +public: + + static JavaClassWrapper *get_singleton() { return singleton; } + + Ref<JavaClass> wrap(const String& p_class); + + JavaClassWrapper(jobject p_activity=NULL); +}; + +#endif // JAVA_CLASS_WRAPPER_H diff --git a/platform/android/java_glue.cpp b/platform/android/java_glue.cpp index ae8174c35a..fdc6f1207d 100644 --- a/platform/android/java_glue.cpp +++ b/platform/android/java_glue.cpp @@ -38,7 +38,10 @@ #include "globals.h" #include "thread_jandroid.h" #include "core/os/keyboard.h" +#include "java_class_wrapper.h" + +static JavaClassWrapper *java_class_wrapper=NULL; static OS_Android *os_android=NULL; @@ -934,6 +937,8 @@ JNIEXPORT void JNICALL Java_com_android_godot_GodotLib_step(JNIEnv * env, jobjec // ugly hack to initialize the rest of the engine // because of the way android forces you to do everything with threads + java_class_wrapper = memnew( JavaClassWrapper(_godot_instance )); + Globals::get_singleton()->add_singleton(Globals::Singleton("JavaClassWrapper",java_class_wrapper)); _initialize_java_modules(); Main::setup2(); |