Debian Codegraph @

src/core/SkOpts.h

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