• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 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 SkRasterPipeline_DEFINED
9 #define SkRasterPipeline_DEFINED
10 
11 #include "include/core/SkColor.h"
12 #include "include/core/SkTypes.h"
13 #include "include/private/base/SkMacros.h"
14 #include "include/private/base/SkSpan_impl.h"
15 #include "include/private/base/SkTArray.h"
16 #include "src/base/SkArenaAlloc.h"
17 #include "src/core/SkRasterPipelineOpContexts.h"
18 
19 #include <cstddef>
20 #include <cstdint>
21 #include <functional>
22 
23 class SkMatrix;
24 enum class SkRasterPipelineOp;
25 enum SkColorType : int;
26 struct SkImageInfo;
27 struct skcms_TransferFunction;
28 
29 #if __has_cpp_attribute(clang::musttail) && !defined(__EMSCRIPTEN__) && !defined(SK_CPU_ARM32) && \
30         !defined(SK_CPU_LOONGARCH) && !defined(SK_CPU_PPC) && !defined(_WIN32)
31     // [[clang::musttail]] is disabled for the Android version of Skia running on Windows as it
32     // causes crashes (This is probably related to http://crbug.com/1505442).
33     #define SK_HAS_MUSTTAIL 1
34 #else
35     #define SK_HAS_MUSTTAIL 0
36 #endif
37 
38 /**
39  * SkRasterPipeline provides a cheap way to chain together a pixel processing pipeline.
40  *
41  * It's particularly designed for situations where the potential pipeline is extremely
42  * combinatoric: {N dst formats} x {M source formats} x {K mask formats} x {C transfer modes} ...
43  * No one wants to write specialized routines for all those combinations, and if we did, we'd
44  * end up bloating our code size dramatically.  SkRasterPipeline stages can be chained together
45  * at runtime, so we can scale this problem linearly rather than combinatorically.
46  *
47  * Each stage is represented by a function conforming to a common interface and by an
48  * arbitrary context pointer.  The stage function arguments and calling convention are
49  * designed to maximize the amount of data we can pass along the pipeline cheaply, and
50  * vary depending on CPU feature detection.
51  */
52 
53 // Raster pipeline programs are stored as a contiguous array of SkRasterPipelineStages.
54 SK_BEGIN_REQUIRE_DENSE
55 struct SkRasterPipelineStage {
56     // `fn` holds a function pointer from `ops_lowp` or `ops_highp` in SkOpts.cpp. These functions
57     // correspond to operations from the SkRasterPipelineOp enum in SkRasterPipelineOpList.h. The
58     // exact function pointer type varies depending on architecture (specifically, look for `using
59     // Stage =` in SkRasterPipeline_opts.h).
60     void (*fn)();
61 
62     // `ctx` holds data used by the stage function.
63     // Most context structures are declared in SkRasterPipelineOpContexts.h, and have names ending
64     // in Ctx (e.g. "SkRasterPipeline_SamplerCtx"). Some Raster Pipeline stages pack non-pointer
65     // data into this field using `SkRPCtxUtils::Pack`.
66     void* ctx;
67 };
68 SK_END_REQUIRE_DENSE
69 
70 class SkRasterPipeline {
71 public:
72     explicit SkRasterPipeline(SkArenaAlloc*);
73 
74     SkRasterPipeline(const SkRasterPipeline&) = delete;
75     SkRasterPipeline(SkRasterPipeline&&)      = default;
76 
77     SkRasterPipeline& operator=(const SkRasterPipeline&) = delete;
78     SkRasterPipeline& operator=(SkRasterPipeline&&)      = default;
79 
80     void reset();
81 
82     void append(SkRasterPipelineOp, void* = nullptr);
append(SkRasterPipelineOp op,const void * ctx)83     void append(SkRasterPipelineOp op, const void* ctx) { this->append(op,const_cast<void*>(ctx)); }
84     void append(SkRasterPipelineOp, uintptr_t ctx);
85 
86     // Append all stages to this pipeline.
87     void extend(const SkRasterPipeline&);
88 
89     // Runs the pipeline in 2d from (x,y) inclusive to (x+w,y+h) exclusive.
90     void run(size_t x, size_t y, size_t w, size_t h) const;
91 
92     // Allocates a thunk which amortizes run() setup cost in alloc.
93     std::function<void(size_t, size_t, size_t, size_t)> compile() const;
94 
95     // Callers can inspect the stage list for debugging purposes.
96     struct StageList {
97         StageList*          prev;
98         SkRasterPipelineOp  stage;
99         void*               ctx;
100     };
101 
102     static const char* GetOpName(SkRasterPipelineOp op);
getStageList()103     const StageList* getStageList() const { return fStages; }
getNumStages()104     int getNumStages() const { return fNumStages; }
105 
106     // Prints the entire StageList using SkDebugf.
107     void dump() const;
108 
109     // Appends a stage for the specified matrix.
110     // Tries to optimize the stage by analyzing the type of matrix.
111     void appendMatrix(SkArenaAlloc*, const SkMatrix&);
112 
113     // Appends a stage for a constant uniform color.
114     // Tries to optimize the stage based on the color.
115     void appendConstantColor(SkArenaAlloc*, const float rgba[4]);
116 
appendConstantColor(SkArenaAlloc * alloc,const SkColor4f & color)117     void appendConstantColor(SkArenaAlloc* alloc, const SkColor4f& color) {
118         this->appendConstantColor(alloc, color.vec());
119     }
120 
121     // Like appendConstantColor() but only affecting r,g,b, ignoring the alpha channel.
122     void appendSetRGB(SkArenaAlloc*, const float rgb[3]);
123 
appendSetRGB(SkArenaAlloc * alloc,const SkColor4f & color)124     void appendSetRGB(SkArenaAlloc* alloc, const SkColor4f& color) {
125         this->appendSetRGB(alloc, color.vec());
126     }
127 
128     void appendLoad   (SkColorType, const SkRasterPipeline_MemoryCtx*);
129     void appendLoadDst(SkColorType, const SkRasterPipeline_MemoryCtx*);
130     void appendStore  (SkColorType, const SkRasterPipeline_MemoryCtx*);
131 
132     void appendClampIfNormalized(const SkImageInfo&);
133 
134     void appendTransferFunction(const skcms_TransferFunction&);
135 
136     void appendStackRewind();
137 
empty()138     bool empty() const { return fStages == nullptr; }
139 
140 private:
141     bool buildLowpPipeline(SkRasterPipelineStage* ip) const;
142     void buildHighpPipeline(SkRasterPipelineStage* ip) const;
143 
144     using StartPipelineFn = void (*)(size_t, size_t, size_t, size_t,
145                                      SkRasterPipelineStage* program,
146                                      SkSpan<SkRasterPipeline_MemoryCtxPatch>,
147                                      uint8_t*);
148     StartPipelineFn buildPipeline(SkRasterPipelineStage*) const;
149 
150     void uncheckedAppend(SkRasterPipelineOp, void*);
151     int stagesNeeded() const;
152 
153     void addMemoryContext(SkRasterPipeline_MemoryCtx*, int bytesPerPixel, bool load, bool store);
154     uint8_t* tailPointer();
155 
156     SkArenaAlloc*               fAlloc;
157     SkRasterPipeline_RewindCtx* fRewindCtx;
158     StageList*                  fStages;
159     uint8_t*                    fTailPointer;
160     int                         fNumStages;
161 
162     // Only 1 in 2 million CPU-backend pipelines used more than two MemoryCtxs.
163     // (See the comment in SkRasterPipelineOpContexts.h for how MemoryCtx patching works)
164     skia_private::STArray<2, SkRasterPipeline_MemoryCtxInfo> fMemoryCtxInfos;
165 };
166 
167 template <size_t bytes>
168 class SkRasterPipeline_ : public SkRasterPipeline {
169 public:
SkRasterPipeline_()170     SkRasterPipeline_()
171         : SkRasterPipeline(&fBuiltinAlloc) {}
172 
173 private:
174     SkSTArenaAlloc<bytes> fBuiltinAlloc;
175 };
176 
177 
178 #endif//SkRasterPipeline_DEFINED
179