• 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 "src/base/SkArenaAlloc.h"
15 #include "src/core/SkRasterPipelineOpContexts.h"
16 #include "src/core/SkRasterPipelineOpList.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <functional>
21 
22 class SkMatrix;
23 enum SkColorType : int;
24 struct SkImageInfo;
25 struct skcms_TransferFunction;
26 
27 #if __has_cpp_attribute(clang::musttail) && !defined(__EMSCRIPTEN__) && !defined(SK_CPU_ARM32)
28     #define SK_HAS_MUSTTAIL 1
29 #else
30     #define SK_HAS_MUSTTAIL 0
31 #endif
32 
33 /**
34  * SkRasterPipeline provides a cheap way to chain together a pixel processing pipeline.
35  *
36  * It's particularly designed for situations where the potential pipeline is extremely
37  * combinatoric: {N dst formats} x {M source formats} x {K mask formats} x {C transfer modes} ...
38  * No one wants to write specialized routines for all those combinations, and if we did, we'd
39  * end up bloating our code size dramatically.  SkRasterPipeline stages can be chained together
40  * at runtime, so we can scale this problem linearly rather than combinatorically.
41  *
42  * Each stage is represented by a function conforming to a common interface and by an
43  * arbitrary context pointer.  The stage function arguments and calling convention are
44  * designed to maximize the amount of data we can pass along the pipeline cheaply, and
45  * vary depending on CPU feature detection.
46  */
47 
48 // Raster pipeline programs are stored as a contiguous array of SkRasterPipelineStages.
49 SK_BEGIN_REQUIRE_DENSE
50 struct SkRasterPipelineStage {
51     // A function pointer from `stages_lowp` or `stages_highp`. The exact function pointer type
52     // varies depending on architecture (specifically, see `Stage` in SkRasterPipeline_opts.h).
53     void (*fn)();
54 
55     // Data used by the stage function. Most context structures are declared at the top of
56     // SkRasterPipeline.h, and have names ending in Ctx (e.g. "SkRasterPipeline_SamplerCtx").
57     void* ctx;
58 };
59 SK_END_REQUIRE_DENSE
60 
61 class SkRasterPipeline {
62 public:
63     explicit SkRasterPipeline(SkArenaAlloc*);
64 
65     SkRasterPipeline(const SkRasterPipeline&) = delete;
66     SkRasterPipeline(SkRasterPipeline&&)      = default;
67 
68     SkRasterPipeline& operator=(const SkRasterPipeline&) = delete;
69     SkRasterPipeline& operator=(SkRasterPipeline&&)      = default;
70 
71     void reset();
72 
73     void append(SkRasterPipelineOp, void* = nullptr);
append(SkRasterPipelineOp op,const void * ctx)74     void append(SkRasterPipelineOp op, const void* ctx) { this->append(op,const_cast<void*>(ctx)); }
75     void append(SkRasterPipelineOp, uintptr_t ctx);
76 
77     // Append all stages to this pipeline.
78     void extend(const SkRasterPipeline&);
79 
80     // Runs the pipeline in 2d from (x,y) inclusive to (x+w,y+h) exclusive.
81     void run(size_t x, size_t y, size_t w, size_t h) const;
82 
83     // Allocates a thunk which amortizes run() setup cost in alloc.
84     std::function<void(size_t, size_t, size_t, size_t)> compile() const;
85 
86     // Callers can inspect the stage list for debugging purposes.
87     struct StageList {
88         StageList*          prev;
89         SkRasterPipelineOp  stage;
90         void*               ctx;
91     };
92 
93     static const char* GetOpName(SkRasterPipelineOp op);
getStageList()94     const StageList* getStageList() const { return fStages; }
getNumStages()95     int getNumStages() const { return fNumStages; }
96 
97     // Prints the entire StageList using SkDebugf.
98     void dump() const;
99 
100     // Appends a stage for the specified matrix.
101     // Tries to optimize the stage by analyzing the type of matrix.
102     void append_matrix(SkArenaAlloc*, const SkMatrix&);
103 
104     // Appends a stage for a constant uniform color.
105     // Tries to optimize the stage based on the color.
106     void append_constant_color(SkArenaAlloc*, const float rgba[4]);
107 
append_constant_color(SkArenaAlloc * alloc,const SkColor4f & color)108     void append_constant_color(SkArenaAlloc* alloc, const SkColor4f& color) {
109         this->append_constant_color(alloc, color.vec());
110     }
111 
112     // Like append_constant_color() but only affecting r,g,b, ignoring the alpha channel.
113     void append_set_rgb(SkArenaAlloc*, const float rgb[3]);
114 
append_set_rgb(SkArenaAlloc * alloc,const SkColor4f & color)115     void append_set_rgb(SkArenaAlloc* alloc, const SkColor4f& color) {
116         this->append_set_rgb(alloc, color.vec());
117     }
118 
119     void append_load    (SkColorType, const SkRasterPipeline_MemoryCtx*);
120     void append_load_dst(SkColorType, const SkRasterPipeline_MemoryCtx*);
121     void append_store   (SkColorType, const SkRasterPipeline_MemoryCtx*);
122 
123     void append_clamp_if_normalized(const SkImageInfo&);
124 
125     void append_transfer_function(const skcms_TransferFunction&);
126 
127     void append_stack_rewind();
128 
empty()129     bool empty() const { return fStages == nullptr; }
130 
131 private:
132     bool build_lowp_pipeline(SkRasterPipelineStage* ip) const;
133     void build_highp_pipeline(SkRasterPipelineStage* ip) const;
134 
135     using StartPipelineFn = void(*)(size_t,size_t,size_t,size_t, SkRasterPipelineStage* program);
136     StartPipelineFn build_pipeline(SkRasterPipelineStage*) const;
137 
138     void unchecked_append(SkRasterPipelineOp, void*);
139     int stages_needed() const;
140 
141     SkArenaAlloc*               fAlloc;
142     SkRasterPipeline_RewindCtx* fRewindCtx;
143     StageList*                  fStages;
144     int                         fNumStages;
145 };
146 
147 template <size_t bytes>
148 class SkRasterPipeline_ : public SkRasterPipeline {
149 public:
SkRasterPipeline_()150     SkRasterPipeline_()
151         : SkRasterPipeline(&fBuiltinAlloc) {}
152 
153 private:
154     SkSTArenaAlloc<bytes> fBuiltinAlloc;
155 };
156 
157 
158 #endif//SkRasterPipeline_DEFINED
159