digit@google.com | eec9dbc | 2012-05-30 13:54:41 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2012 The Android Open Source Project |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #include "SkUtilsArm.h" |
| 10 | |
| 11 | #if SK_ARM_NEON_IS_DYNAMIC |
| 12 | |
| 13 | #include <unistd.h> |
| 14 | #include <fcntl.h> |
| 15 | #include <errno.h> |
| 16 | #include <string.h> |
| 17 | #include <pthread.h> |
| 18 | |
| 19 | // Set NEON_DEBUG to 1 to allow debugging of the CPU features probing. |
| 20 | // For now, we always set it for SK_DEBUG builds. |
| 21 | #ifdef SK_DEBUG |
| 22 | # define NEON_DEBUG 1 |
| 23 | #else |
| 24 | # define NEON_DEBUG 0 |
| 25 | #endif |
| 26 | |
| 27 | #if NEON_DEBUG |
| 28 | # ifdef SK_BUILD_FOR_ANDROID |
| 29 | // used to declare PROP_VALUE_MAX and __system_property_get() |
| 30 | # include <sys/system_properties.h> |
| 31 | # endif |
| 32 | #endif |
| 33 | |
| 34 | // A function used to determine at runtime if the target CPU supports |
| 35 | // the ARM NEON instruction set. This implementation is Linux-specific. |
digit@google.com | fce02ac | 2012-08-01 14:25:07 +0000 | [diff] [blame] | 36 | static bool sk_cpu_arm_check_neon(void) { |
digit@google.com | eec9dbc | 2012-05-30 13:54:41 +0000 | [diff] [blame] | 37 | bool result = false; |
| 38 | |
| 39 | #if NEON_DEBUG |
| 40 | // Allow forcing the mode through the environment during debugging. |
| 41 | # ifdef SK_BUILD_FOR_ANDROID |
| 42 | // On Android, we use a system property |
| 43 | # define PROP_NAME "debug.skia.arm_neon_mode" |
| 44 | char prop[PROP_VALUE_MAX]; |
| 45 | if (__system_property_get(PROP_NAME, prop) > 0) { |
| 46 | # else |
| 47 | # define PROP_NAME "SKIA_ARM_NEON_MODE" |
| 48 | // On ARM Linux, we use an environment variable |
| 49 | const char* prop = getenv(PROP_NAME); |
| 50 | if (prop != NULL) { |
| 51 | # endif |
| 52 | SkDebugf("%s: %s", PROP_NAME, prop); |
| 53 | if (!strcmp(prop, "1")) { |
| 54 | SkDebugf("Forcing ARM Neon mode to full!\n"); |
| 55 | return true; |
| 56 | } |
| 57 | if (!strcmp(prop, "0")) { |
| 58 | SkDebugf("Disabling ARM NEON mode\n"); |
| 59 | return false; |
| 60 | } |
| 61 | } |
| 62 | SkDebugf("Running dynamic CPU feature detection\n"); |
| 63 | #endif |
| 64 | |
| 65 | // There is no user-accessible CPUID instruction on ARM that we can use. |
| 66 | // Instead, we must parse /proc/cpuinfo and look for the 'neon' feature. |
| 67 | // For example, here's a typical output (Nexus S running ICS 4.0.3): |
| 68 | /* |
| 69 | Processor : ARMv7 Processor rev 2 (v7l) |
| 70 | BogoMIPS : 994.65 |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 71 | Features : swp half thumb fastmult vfp edsp thumbee neon vfpv3 |
digit@google.com | eec9dbc | 2012-05-30 13:54:41 +0000 | [diff] [blame] | 72 | CPU implementer : 0x41 |
| 73 | CPU architecture: 7 |
| 74 | CPU variant : 0x2 |
| 75 | CPU part : 0xc08 |
| 76 | CPU revision : 2 |
| 77 | |
| 78 | Hardware : herring |
| 79 | Revision : 000b |
| 80 | Serial : 3833c77d6dc000ec |
| 81 | */ |
| 82 | char buffer[4096]; |
| 83 | |
| 84 | // If we fail any of the following, assume we don't have NEON instructions |
| 85 | // This allows us to return immediately in case of error. |
| 86 | result = false; |
| 87 | |
| 88 | do { |
| 89 | // open /proc/cpuinfo |
| 90 | int fd = TEMP_FAILURE_RETRY(open("/proc/cpuinfo", O_RDONLY)); |
| 91 | if (fd < 0) { |
| 92 | SkDebugf("Could not open /proc/cpuinfo: %s\n", strerror(errno)); |
| 93 | break; |
| 94 | } |
| 95 | |
| 96 | // Read the file. To simplify our search, we're going to place two |
| 97 | // sentinel '\n' characters: one at the start of the buffer, and one at |
| 98 | // the end. This means we reserve the first and last buffer bytes. |
| 99 | buffer[0] = '\n'; |
| 100 | int size = TEMP_FAILURE_RETRY(read(fd, buffer+1, sizeof(buffer)-2)); |
| 101 | close(fd); |
| 102 | |
| 103 | if (size < 0) { // should not happen |
| 104 | SkDebugf("Could not read /proc/cpuinfo: %s\n", strerror(errno)); |
| 105 | break; |
| 106 | } |
| 107 | |
| 108 | SkDebugf("START /proc/cpuinfo:\n%.*s\nEND /proc/cpuinfo\n", |
| 109 | size, buffer+1); |
| 110 | |
| 111 | // Compute buffer limit, and place final sentinel |
| 112 | char* buffer_end = buffer + 1 + size; |
| 113 | buffer_end[0] = '\n'; |
| 114 | |
| 115 | // Now, find a line that starts with "Features", i.e. look for |
| 116 | // '\nFeatures ' in our buffer. |
| 117 | const char features[] = "\nFeatures\t"; |
| 118 | const size_t features_len = sizeof(features)-1; |
| 119 | |
| 120 | char* line = (char*) memmem(buffer, buffer_end - buffer, |
| 121 | features, features_len); |
| 122 | if (line == NULL) { // Weird, no Features line, bad kernel? |
| 123 | SkDebugf("Could not find a line starting with 'Features'" |
| 124 | "in /proc/cpuinfo ?\n"); |
| 125 | break; |
| 126 | } |
| 127 | |
| 128 | line += features_len; // Skip the "\nFeatures\t" prefix |
| 129 | |
| 130 | // Find the end of the current line |
| 131 | char* line_end = (char*) memchr(line, '\n', buffer_end - line); |
| 132 | if (line_end == NULL) |
| 133 | line_end = buffer_end; |
| 134 | |
| 135 | // Now find an instance of 'neon' in the flags list. We want to |
| 136 | // ensure it's only 'neon' and not something fancy like 'noneon' |
| 137 | // so check that it follows a space. |
| 138 | const char neon[] = " neon"; |
| 139 | const size_t neon_len = sizeof(neon)-1; |
| 140 | const char* flag = (const char*) memmem(line, line_end - line, |
| 141 | neon, neon_len); |
| 142 | if (flag == NULL) |
| 143 | break; |
| 144 | |
| 145 | // Ensure it is followed by a space or a newline. |
| 146 | if (flag[neon_len] != ' ' && flag[neon_len] != '\n') |
| 147 | break; |
| 148 | |
| 149 | // Fine, we support Arm NEON ! |
| 150 | result = true; |
| 151 | |
| 152 | } while (0); |
| 153 | |
| 154 | if (result) { |
| 155 | SkDebugf("Device supports ARM NEON instructions!\n"); |
| 156 | } else { |
| 157 | SkDebugf("Device does NOT support ARM NEON instructions!\n"); |
| 158 | } |
| 159 | return result; |
| 160 | } |
| 161 | |
| 162 | static pthread_once_t sOnce; |
| 163 | static bool sHasArmNeon; |
| 164 | |
| 165 | // called through pthread_once() |
digit@google.com | fce02ac | 2012-08-01 14:25:07 +0000 | [diff] [blame] | 166 | void sk_cpu_arm_probe_features(void) { |
digit@google.com | eec9dbc | 2012-05-30 13:54:41 +0000 | [diff] [blame] | 167 | sHasArmNeon = sk_cpu_arm_check_neon(); |
| 168 | } |
| 169 | |
digit@google.com | fce02ac | 2012-08-01 14:25:07 +0000 | [diff] [blame] | 170 | bool sk_cpu_arm_has_neon(void) { |
digit@google.com | eec9dbc | 2012-05-30 13:54:41 +0000 | [diff] [blame] | 171 | pthread_once(&sOnce, sk_cpu_arm_probe_features); |
| 172 | return sHasArmNeon; |
| 173 | } |
| 174 | |
| 175 | #endif // SK_ARM_NEON_IS_DYNAMIC |