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 #ifndef SkOpts_DEFINED 9 #define SkOpts_DEFINED 10 11 #include "include/private/base/SkSpan_impl.h" 12 #include "src/core/SkRasterPipelineOpContexts.h" 13 #include "src/core/SkRasterPipelineOpList.h" 14 15 #include <cstddef> 16 #include <cstdint> 17 18 /** 19 * SkOpts (short for SkOptimizations) is a mechanism where we can ship with multiple implementations 20 * of a set of functions and dynamically choose the best one at runtime (e.g. the call to 21 * SkGraphics::Init(), which calls SkOpts::Init()) depending on the detected CPU features. This is 22 * also referred to as having "specializations" of a given function. 23 * 24 * For example, Skia might be compiled to support CPUs that only have the sse2 instruction set 25 * (https://en.wikipedia.org/wiki/X86_instruction_listings#SSE2_instructions) 26 * but may be run on a more modern CPU that supports AVX2 instructions. 27 * (https://en.wikipedia.org/wiki/Advanced_Vector_Extensions) 28 * SkOpts allow Skia to have two versions of a row-blitting function, one that uses normal C++ 29 * code (e.g. loops, scalar integer math) and one that makes use of the AVX2 vector types and 30 * intrinsic functions. This function is declared here in the SkOpts namespace, and then the 31 * implementation (see SkOpts.cpp) is deferred to a function of the same name in the sse2:: 32 * namespace (the minimum Skia is compiled with) using DEFINE_DEFAULT. 33 * 34 * All implementations of this blit function are done in a header file file in //src/opts 35 * (e.g. //src/opts/SkBlitRow_opts.h). ifdefs guard each of the implementations, such that only 36 * one implementation is possible for a given SK_CPU_SSE_LEVEL. This header will be compiled 37 * *multiple* times with a different SK_CPU_SSE_LEVEL each compilation. 38 * 39 * Each CPU instruction set that we want specializations for has a .cpp file in //src/opts which 40 * defines an Init() function that replaces the function pointers in the SkOpts namespace with the 41 * ones from the specialized namespace (e.g. hsw::). These .cpp files don't implement the 42 * specializations, they just refer to the specialization created in the header files (e.g. 43 * SkBlitRow_opts.h). 44 * 45 * At compile time: 46 * - SkOpts.cpp is compiled with the minimum CPU level (e.g. SSE2). Because this 47 * file includes all the headers in //src/opts/, those headers add "the default implementation" 48 * of all their functions to the SK_OPTS_NS namespace (e.g. sse2::blit_row_color32). 49 * - Each of the specialized .cpp files in //src/opts/ are compiled with their respective 50 * compiler flags. Because the specialized .cpp file includes the headers that implement the 51 * functions using intrinsics or other CPU-specific code, those specialized functions end up 52 * in the specialized namespace, e.g. (hsw::blit_row_color32). 53 * 54 * At link time, the default implementations and all specializations of all SkOpts functions are 55 * included in the resulting library/binary file. 56 * 57 * At runtime, SkOpts::Init() will run the appropriate Init functions that the current CPU level 58 * supports specializations for (e.g. Init_hsw, Init_ssse3). Note multiple Init functions can 59 * be called as CPU instruction sets are typically super sets of older instruction sets 60 */ 61 62 struct SkRasterPipelineStage; 63 64 namespace SkOpts { 65 // Call to replace pointers to portable functions with pointers to CPU-specific functions. 66 // Thread-safe and idempotent. 67 // Called by SkGraphics::Init(). 68 void Init(); 69 70 // We can't necessarily express the type of SkRasterPipeline stage functions here, 71 // so we just use this void(*)(void) as a stand-in. 72 using StageFn = void(*)(void); 73 extern StageFn ops_highp[kNumRasterPipelineHighpOps], just_return_highp; 74 extern StageFn ops_lowp [kNumRasterPipelineLowpOps ], just_return_lowp; 75 76 extern void (*start_pipeline_highp)(size_t,size_t,size_t,size_t, SkRasterPipelineStage*, 77 SkSpan<SkRasterPipeline_MemoryCtxPatch>, 78 uint8_t*); 79 extern void (*start_pipeline_lowp )(size_t,size_t,size_t,size_t, SkRasterPipelineStage*, 80 SkSpan<SkRasterPipeline_MemoryCtxPatch>, 81 uint8_t*); 82 83 extern size_t raster_pipeline_lowp_stride; 84 extern size_t raster_pipeline_highp_stride; 85 } // namespace SkOpts 86 87 #endif // SkOpts_DEFINED 88