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