3 * Copyright 2012 The Android Open Source Project
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
9 #include "SkUtilsArm.h"
11 #if SK_ARM_NEON_IS_DYNAMIC
19 // Set USE_ANDROID_NDK_CPU_FEATURES to use the Android NDK's
20 // cpu-features helper library to detect NEON at runtime. See
21 // http://crbug.com/164154 to see why this is needed in Chromium
23 #if defined(SK_BUILD_FOR_ANDROID)
24 # define USE_ANDROID_NDK_CPU_FEATURES 1
26 # define USE_ANDROID_NDK_CPU_FEATURES 0
29 #if USE_ANDROID_NDK_CPU_FEATURES
30 # include <cpu-features.h>
33 // Set NEON_DEBUG to 1 to allow debugging of the CPU features probing.
34 // For now, we always set it for SK_DEBUG builds.
42 # ifdef SK_BUILD_FOR_ANDROID
43 // used to declare PROP_VALUE_MAX and __system_property_get()
44 # include <sys/system_properties.h>
48 // A function used to determine at runtime if the target CPU supports
49 // the ARM NEON instruction set. This implementation is Linux-specific.
50 static bool sk_cpu_arm_check_neon(void) {
54 // Allow forcing the mode through the environment during debugging.
55 # ifdef SK_BUILD_FOR_ANDROID
56 // On Android, we use a system property
57 # define PROP_NAME "debug.skia.arm_neon_mode"
58 char prop[PROP_VALUE_MAX];
59 if (__system_property_get(PROP_NAME, prop) > 0) {
61 # define PROP_NAME "SKIA_ARM_NEON_MODE"
62 // On ARM Linux, we use an environment variable
63 const char* prop = getenv(PROP_NAME);
66 SkDebugf("%s: %s", PROP_NAME, prop);
67 if (!strcmp(prop, "1")) {
68 SkDebugf("Forcing ARM Neon mode to full!\n");
71 if (!strcmp(prop, "0")) {
72 SkDebugf("Disabling ARM NEON mode\n");
76 SkDebugf("Running dynamic CPU feature detection\n");
79 #if USE_ANDROID_NDK_CPU_FEATURES
81 result = (android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_NEON) != 0;
83 #else // USE_ANDROID_NDK_CPU_FEATURES
85 // There is no user-accessible CPUID instruction on ARM that we can use.
86 // Instead, we must parse /proc/cpuinfo and look for the 'neon' feature.
87 // For example, here's a typical output (Nexus S running ICS 4.0.3):
89 Processor : ARMv7 Processor rev 2 (v7l)
91 Features : swp half thumb fastmult vfp edsp thumbee neon vfpv3
92 CPU implementer : 0x41
100 Serial : 3833c77d6dc000ec
104 // If we fail any of the following, assume we don't have NEON instructions
105 // This allows us to return immediately in case of error.
109 // open /proc/cpuinfo
110 int fd = TEMP_FAILURE_RETRY(open("/proc/cpuinfo", O_RDONLY));
112 SkDebugf("Could not open /proc/cpuinfo: %s\n", strerror(errno));
116 // Read the file. To simplify our search, we're going to place two
117 // sentinel '\n' characters: one at the start of the buffer, and one at
118 // the end. This means we reserve the first and last buffer bytes.
120 int size = TEMP_FAILURE_RETRY(read(fd, buffer+1, sizeof(buffer)-2));
123 if (size < 0) { // should not happen
124 SkDebugf("Could not read /proc/cpuinfo: %s\n", strerror(errno));
128 SkDebugf("START /proc/cpuinfo:\n%.*s\nEND /proc/cpuinfo\n",
131 // Compute buffer limit, and place final sentinel
132 char* buffer_end = buffer + 1 + size;
133 buffer_end[0] = '\n';
135 // Now, find a line that starts with "Features", i.e. look for
136 // '\nFeatures ' in our buffer.
137 const char features[] = "\nFeatures\t";
138 const size_t features_len = sizeof(features)-1;
140 char* line = (char*) memmem(buffer, buffer_end - buffer,
141 features, features_len);
142 if (line == NULL) { // Weird, no Features line, bad kernel?
143 SkDebugf("Could not find a line starting with 'Features'"
144 "in /proc/cpuinfo ?\n");
148 line += features_len; // Skip the "\nFeatures\t" prefix
150 // Find the end of the current line
151 char* line_end = (char*) memchr(line, '\n', buffer_end - line);
152 if (line_end == NULL)
153 line_end = buffer_end;
155 // Now find an instance of 'neon' in the flags list. We want to
156 // ensure it's only 'neon' and not something fancy like 'noneon'
157 // so check that it follows a space.
158 const char neon[] = " neon";
159 const size_t neon_len = sizeof(neon)-1;
160 const char* flag = (const char*) memmem(line, line_end - line,
165 // Ensure it is followed by a space or a newline.
166 if (flag[neon_len] != ' ' && flag[neon_len] != '\n')
169 // Fine, we support Arm NEON !
174 #endif // USE_ANDROID_NDK_CPU_FEATURES
177 SkDebugf("Device supports ARM NEON instructions!\n");
179 SkDebugf("Device does NOT support ARM NEON instructions!\n");
184 static pthread_once_t sOnce;
185 static bool sHasArmNeon;
187 // called through pthread_once()
188 void sk_cpu_arm_probe_features(void) {
189 sHasArmNeon = sk_cpu_arm_check_neon();
192 bool sk_cpu_arm_has_neon(void) {
193 pthread_once(&sOnce, sk_cpu_arm_probe_features);
197 #endif // SK_ARM_NEON_IS_DYNAMIC