1 /* 2 * Copyright 2015 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #include "SkHalf.h" 9 #include "SkOnce.h" 10 #include "SkOpts.h" 11 12 #define SK_OPTS_NS sk_default 13 #include "SkBlitMask_opts.h" 14 #include "SkBlitRow_opts.h" 15 #include "SkBlurImageFilter_opts.h" 16 #include "SkColorCubeFilter_opts.h" 17 #include "SkMatrix_opts.h" 18 #include "SkMorphologyImageFilter_opts.h" 19 #include "SkSwizzler_opts.h" 20 #include "SkTextureCompressor_opts.h" 21 #include "SkXfermode_opts.h" 22 23 namespace SK_OPTS_NS { float_to_half(uint16_t dst[],const float src[],int n)24 static void float_to_half(uint16_t dst[], const float src[], int n) { 25 while (n-->0) { 26 *dst++ = SkFloatToHalf(*src++); 27 } 28 } half_to_float(float dst[],const uint16_t src[],int n)29 static void half_to_float(float dst[], const uint16_t src[], int n) { 30 while (n-->0) { 31 *dst++ = SkHalfToFloat(*src++); 32 } 33 } 34 } 35 36 #if defined(SK_CPU_X86) && !defined(SK_BUILD_FOR_IOS) 37 #if defined(SK_BUILD_FOR_WIN32) 38 #include <intrin.h> cpuid(uint32_t abcd[4])39 static void cpuid (uint32_t abcd[4]) { __cpuid ((int*)abcd, 1); } cpuid7(uint32_t abcd[4])40 static void cpuid7(uint32_t abcd[4]) { __cpuidex((int*)abcd, 7, 0); } xgetbv(uint32_t xcr)41 static uint64_t xgetbv(uint32_t xcr) { return _xgetbv(xcr); } 42 #else 43 #include <cpuid.h> 44 #if !defined(__cpuid_count) // Old Mac Clang doesn't have this defined. 45 #define __cpuid_count(eax, ecx, a, b, c, d) \ 46 __asm__("cpuid" : "=a"(a), "=b"(b), "=c"(c), "=d"(d) : "0"(eax), "2"(ecx)) 47 #endif cpuid(uint32_t abcd[4])48 static void cpuid (uint32_t abcd[4]) { __get_cpuid(1, abcd+0, abcd+1, abcd+2, abcd+3); } cpuid7(uint32_t abcd[4])49 static void cpuid7(uint32_t abcd[4]) { 50 __cpuid_count(7, 0, abcd[0], abcd[1], abcd[2], abcd[3]); 51 } xgetbv(uint32_t xcr)52 static uint64_t xgetbv(uint32_t xcr) { 53 uint32_t eax, edx; 54 __asm__ __volatile__ ( "xgetbv" : "=a"(eax), "=d"(edx) : "c"(xcr)); 55 return (uint64_t)(edx) << 32 | eax; 56 } 57 #endif 58 #elif !defined(SK_ARM_HAS_NEON) && \ 59 defined(SK_CPU_ARM32) && \ 60 defined(SK_BUILD_FOR_ANDROID) && \ 61 !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) 62 #include <cpu-features.h> 63 #endif 64 65 namespace SkOpts { 66 // Define default function pointer values here... 67 // If our global compile options are set high enough, these defaults might even be 68 // CPU-specialized, e.g. a typical x86-64 machine might start with SSE2 defaults. 69 // They'll still get a chance to be replaced with even better ones, e.g. using SSE4.1. 70 decltype(create_xfermode) create_xfermode = sk_default::create_xfermode; 71 decltype(color_cube_filter_span) color_cube_filter_span = sk_default::color_cube_filter_span; 72 73 decltype(box_blur_xx) box_blur_xx = sk_default::box_blur_xx; 74 decltype(box_blur_xy) box_blur_xy = sk_default::box_blur_xy; 75 decltype(box_blur_yx) box_blur_yx = sk_default::box_blur_yx; 76 77 decltype(dilate_x) dilate_x = sk_default::dilate_x; 78 decltype(dilate_y) dilate_y = sk_default::dilate_y; 79 decltype( erode_x) erode_x = sk_default::erode_x; 80 decltype( erode_y) erode_y = sk_default::erode_y; 81 82 decltype(texture_compressor) texture_compressor = sk_default::texture_compressor; 83 decltype(fill_block_dimensions) fill_block_dimensions = sk_default::fill_block_dimensions; 84 85 decltype(blit_mask_d32_a8) blit_mask_d32_a8 = sk_default::blit_mask_d32_a8; 86 87 decltype(blit_row_color32) blit_row_color32 = sk_default::blit_row_color32; 88 89 decltype(matrix_translate) matrix_translate = sk_default::matrix_translate; 90 decltype(matrix_scale_translate) matrix_scale_translate = sk_default::matrix_scale_translate; 91 decltype(matrix_affine) matrix_affine = sk_default::matrix_affine; 92 93 decltype(RGBA_to_BGRA) RGBA_to_BGRA = sk_default::RGBA_to_BGRA; 94 decltype(RGBA_to_rgbA) RGBA_to_rgbA = sk_default::RGBA_to_rgbA; 95 decltype(RGBA_to_bgrA) RGBA_to_bgrA = sk_default::RGBA_to_bgrA; 96 decltype(RGB_to_RGB1) RGB_to_RGB1 = sk_default::RGB_to_RGB1; 97 decltype(RGB_to_BGR1) RGB_to_BGR1 = sk_default::RGB_to_BGR1; 98 decltype(gray_to_RGB1) gray_to_RGB1 = sk_default::gray_to_RGB1; 99 decltype(grayA_to_RGBA) grayA_to_RGBA = sk_default::grayA_to_RGBA; 100 decltype(grayA_to_rgbA) grayA_to_rgbA = sk_default::grayA_to_rgbA; 101 decltype(inverted_CMYK_to_RGB1) inverted_CMYK_to_RGB1 = sk_default::inverted_CMYK_to_RGB1; 102 decltype(inverted_CMYK_to_BGR1) inverted_CMYK_to_BGR1 = sk_default::inverted_CMYK_to_BGR1; 103 104 decltype(half_to_float) half_to_float = sk_default::half_to_float; 105 decltype(float_to_half) float_to_half = sk_default::float_to_half; 106 107 // Each Init_foo() is defined in src/opts/SkOpts_foo.cpp. 108 void Init_ssse3(); 109 void Init_sse41(); Init_sse42()110 void Init_sse42() {} Init_avx()111 void Init_avx() {} Init_avx2()112 void Init_avx2() {} 113 void Init_neon(); 114 init()115 static void init() { 116 // TODO: Chrome's not linking _sse* opts on iOS simulator builds. Bug or feature? 117 #if defined(SK_CPU_X86) && !defined(SK_BUILD_FOR_IOS) 118 uint32_t abcd[] = {0,0,0,0}; 119 cpuid(abcd); 120 if (abcd[2] & (1<< 9)) { Init_ssse3(); } 121 if (abcd[2] & (1<<19)) { Init_sse41(); } 122 if (abcd[2] & (1<<20)) { Init_sse42(); } 123 124 // AVX detection's kind of a pain. This is cribbed from Chromium. 125 if ( ( abcd[2] & (7<<26)) == (7<<26) && // Check bits 26-28 of ecx are all set, 126 (xgetbv(0) & 6 ) == 6 ){ // and check the OS supports XSAVE. 127 Init_avx(); 128 129 // AVX2 additionally needs bit 5 set on ebx after calling cpuid(7). 130 uint32_t abcd7[] = {0,0,0,0}; 131 cpuid7(abcd7); 132 if (abcd7[1] & (1<<5)) { Init_avx2(); } 133 } 134 135 #elif !defined(SK_ARM_HAS_NEON) && \ 136 defined(SK_CPU_ARM32) && \ 137 defined(SK_BUILD_FOR_ANDROID) && \ 138 !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) 139 if (android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_NEON) { Init_neon(); } 140 #endif 141 } 142 143 SK_DECLARE_STATIC_ONCE(gInitOnce); Init()144 void Init() { SkOnce(&gInitOnce, init); } 145 146 #if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS 147 static struct AutoInit { AutoInitSkOpts::AutoInit148 AutoInit() { Init(); } 149 } gAutoInit; 150 #endif 151 } 152