• 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/SkImageInfo.h"
13 #include "include/core/SkTileMode.h"
14 #include "include/core/SkTypes.h"
15 #include "include/private/SkNx.h"
16 #include "include/private/SkTArray.h"
17 #include "src/core/SkArenaAlloc.h"
18 #include <functional>
19 #include <vector>  // TODO: unused
20 
21 /**
22  * SkRasterPipeline provides a cheap way to chain together a pixel processing pipeline.
23  *
24  * It's particularly designed for situations where the potential pipeline is extremely
25  * combinatoric: {N dst formats} x {M source formats} x {K mask formats} x {C transfer modes} ...
26  * No one wants to write specialized routines for all those combinations, and if we did, we'd
27  * end up bloating our code size dramatically.  SkRasterPipeline stages can be chained together
28  * at runtime, so we can scale this problem linearly rather than combinatorically.
29  *
30  * Each stage is represented by a function conforming to a common interface and by an
31  * arbitrary context pointer.  The stage funciton arguments and calling convention are
32  * designed to maximize the amount of data we can pass along the pipeline cheaply, and
33  * vary depending on CPU feature detection.
34  */
35 
36 #define SK_RASTER_PIPELINE_STAGES(M)                               \
37     M(callback) M(interpreter)                                     \
38     M(move_src_dst) M(move_dst_src)                                \
39     M(clamp_0) M(clamp_1) M(clamp_a) M(clamp_gamut)                \
40     M(unpremul) M(premul) M(premul_dst)                            \
41     M(force_opaque) M(force_opaque_dst)                            \
42     M(set_rgb) M(unbounded_set_rgb) M(swap_rb) M(swap_rb_dst)      \
43     M(from_srgb) M(to_srgb)                                        \
44     M(black_color) M(white_color)                                  \
45     M(uniform_color) M(unbounded_uniform_color) M(uniform_color_dst) \
46     M(seed_shader) M(dither)                                       \
47     M(load_a8)     M(load_a8_dst)   M(store_a8)    M(gather_a8)    \
48     M(load_565)    M(load_565_dst)  M(store_565)   M(gather_565)   \
49     M(load_4444)   M(load_4444_dst) M(store_4444)  M(gather_4444)  \
50     M(load_f16)    M(load_f16_dst)  M(store_f16)   M(gather_f16)   \
51     M(load_af16)                    M(store_af16)                  \
52     M(load_rgf16)                   M(store_rgf16)                 \
53     M(load_f32)    M(load_f32_dst)  M(store_f32)   M(gather_f32)   \
54     M(load_rgf32)                   M(store_rgf32)                 \
55     M(load_8888)   M(load_8888_dst) M(store_8888)  M(gather_8888)  \
56     M(load_rg88)                    M(store_rg88)                  \
57     M(load_a16)                     M(store_a16)                   \
58     M(load_rg1616)                  M(store_rg1616)                \
59     M(load_16161616)                M(store_16161616)              \
60     M(load_1010102) M(load_1010102_dst) M(store_1010102) M(gather_1010102) \
61     M(alpha_to_gray) M(alpha_to_gray_dst) M(bt709_luminance_or_luma_to_alpha)         \
62     M(bilerp_clamp_8888) M(bicubic_clamp_8888)                     \
63     M(store_u16_be)                                                \
64     M(load_src) M(store_src) M(load_dst) M(store_dst)              \
65     M(scale_u8) M(scale_565) M(scale_1_float)                      \
66     M( lerp_u8) M( lerp_565) M( lerp_1_float) M(lerp_native)       \
67     M(dstatop) M(dstin) M(dstout) M(dstover)                       \
68     M(srcatop) M(srcin) M(srcout) M(srcover)                       \
69     M(clear) M(modulate) M(multiply) M(plus_) M(screen) M(xor_)    \
70     M(colorburn) M(colordodge) M(darken) M(difference)             \
71     M(exclusion) M(hardlight) M(lighten) M(overlay) M(softlight)   \
72     M(hue) M(saturation) M(color) M(luminosity)                    \
73     M(srcover_rgba_8888)                                           \
74     M(matrix_translate) M(matrix_scale_translate)                  \
75     M(matrix_2x3) M(matrix_3x3) M(matrix_3x4) M(matrix_4x5) M(matrix_4x3) \
76     M(matrix_perspective)                                          \
77     M(parametric) M(gamma_)                                        \
78     M(mirror_x)   M(repeat_x)                                      \
79     M(mirror_y)   M(repeat_y)                                      \
80     M(decal_x)    M(decal_y)   M(decal_x_and_y)                    \
81     M(check_decal_mask)                                            \
82     M(negate_x)                                                    \
83     M(bilinear) M(bicubic)                                         \
84     M(bilinear_nx) M(bilinear_px) M(bilinear_ny) M(bilinear_py)    \
85     M(bicubic_n3x) M(bicubic_n1x) M(bicubic_p1x) M(bicubic_p3x)    \
86     M(bicubic_n3y) M(bicubic_n1y) M(bicubic_p1y) M(bicubic_p3y)    \
87     M(save_xy) M(accumulate)                                       \
88     M(clamp_x_1) M(mirror_x_1) M(repeat_x_1)                       \
89     M(evenly_spaced_gradient)                                      \
90     M(gradient)                                                    \
91     M(evenly_spaced_2_stop_gradient)                               \
92     M(xy_to_unit_angle)                                            \
93     M(xy_to_radius)                                                \
94     M(xy_to_2pt_conical_strip)                                     \
95     M(xy_to_2pt_conical_focal_on_circle)                           \
96     M(xy_to_2pt_conical_well_behaved)                              \
97     M(xy_to_2pt_conical_smaller)                                   \
98     M(xy_to_2pt_conical_greater)                                   \
99     M(alter_2pt_conical_compensate_focal)                          \
100     M(alter_2pt_conical_unswap)                                    \
101     M(mask_2pt_conical_nan)                                        \
102     M(mask_2pt_conical_degenerates) M(apply_vector_mask)           \
103     M(byte_tables)                                                 \
104     M(rgb_to_hsl) M(hsl_to_rgb)                                    \
105     M(gauss_a_to_rgba)                                             \
106     M(emboss)                                                      \
107     M(swizzle)
108 
109 // The largest number of pixels we handle at a time.
110 static const int SkRasterPipeline_kMaxStride = 16;
111 
112 // Structs representing the arguments to some common stages.
113 
114 struct SkRasterPipeline_MemoryCtx {
115     void* pixels;
116     int   stride;
117 };
118 
119 struct SkRasterPipeline_GatherCtx {
120     const void* pixels;
121     int         stride;
122     float       width;
123     float       height;
124 };
125 
126 // State shared by save_xy, accumulate, and bilinear_* / bicubic_*.
127 struct SkRasterPipeline_SamplerCtx {
128     float      x[SkRasterPipeline_kMaxStride];
129     float      y[SkRasterPipeline_kMaxStride];
130     float     fx[SkRasterPipeline_kMaxStride];
131     float     fy[SkRasterPipeline_kMaxStride];
132     float scalex[SkRasterPipeline_kMaxStride];
133     float scaley[SkRasterPipeline_kMaxStride];
134 };
135 
136 struct SkRasterPipeline_TileCtx {
137     float scale;
138     float invScale; // cache of 1/scale
139 };
140 
141 struct SkRasterPipeline_DecalTileCtx {
142     uint32_t mask[SkRasterPipeline_kMaxStride];
143     float    limit_x;
144     float    limit_y;
145 };
146 
147 struct SkRasterPipeline_SamplerCtx2 : public SkRasterPipeline_GatherCtx {
148     SkColorType ct;
149     SkTileMode tileX, tileY;
150     float invWidth, invHeight;
151 };
152 
153 struct SkRasterPipeline_CallbackCtx {
154     void (*fn)(SkRasterPipeline_CallbackCtx* self, int active_pixels/*<= SkRasterPipeline_kMaxStride*/);
155 
156     // When called, fn() will have our active pixels available in rgba.
157     // When fn() returns, the pipeline will read back those active pixels from read_from.
158     float rgba[4*SkRasterPipeline_kMaxStride];
159     float* read_from = rgba;
160 };
161 
162 namespace SkSL {
163 struct ByteCode;
164 struct ByteCodeFunction;
165 }
166 
167 struct SkRasterPipeline_InterpreterCtx {
168     const SkSL::ByteCode*         byteCode;
169     const SkSL::ByteCodeFunction* fn;
170 
171     SkColor4f   paintColor;
172     const void* inputs;
173     int         ninputs;
174     bool        shaderConvention;  // if false, we're a colorfilter
175 };
176 
177 struct SkRasterPipeline_GradientCtx {
178     size_t stopCount;
179     float* fs[4];
180     float* bs[4];
181     float* ts;
182     bool interpolatedInPremul;
183 };
184 
185 struct SkRasterPipeline_EvenlySpaced2StopGradientCtx {
186     float f[4];
187     float b[4];
188     bool interpolatedInPremul;
189 };
190 
191 struct SkRasterPipeline_2PtConicalCtx {
192     uint32_t fMask[SkRasterPipeline_kMaxStride];
193     float    fP0,
194              fP1;
195 };
196 
197 struct SkRasterPipeline_UniformColorCtx {
198     float r,g,b,a;
199     uint16_t rgba[4];  // [0,255] in a 16-bit lane.
200 };
201 
202 struct SkRasterPipeline_EmbossCtx {
203     SkRasterPipeline_MemoryCtx mul,
204                                add;
205 };
206 
207 class SkRasterPipeline {
208 public:
209     explicit SkRasterPipeline(SkArenaAlloc*);
210 
211     SkRasterPipeline(const SkRasterPipeline&) = delete;
212     SkRasterPipeline(SkRasterPipeline&&)      = default;
213 
214     SkRasterPipeline& operator=(const SkRasterPipeline&) = delete;
215     SkRasterPipeline& operator=(SkRasterPipeline&&)      = default;
216 
217     void reset();
218 
219     enum StockStage {
220     #define M(stage) stage,
221         SK_RASTER_PIPELINE_STAGES(M)
222     #undef M
223     };
224     void append(StockStage, void* = nullptr);
append(StockStage stage,const void * ctx)225     void append(StockStage stage, const void* ctx) { this->append(stage, const_cast<void*>(ctx)); }
226     void append(StockStage, uintptr_t ctx);
227     // For raw functions (i.e. from a JIT).  Don't use this unless you know exactly what fn needs to
228     // be. :)
229     void append(void* fn, void* ctx);
230 
231     // Append all stages to this pipeline.
232     void extend(const SkRasterPipeline&);
233 
234     // Runs the pipeline in 2d from (x,y) inclusive to (x+w,y+h) exclusive.
235     void run(size_t x, size_t y, size_t w, size_t h) const;
236 
237     // Allocates a thunk which amortizes run() setup cost in alloc.
238     std::function<void(size_t, size_t, size_t, size_t)> compile() const;
239 
240     void dump() const;
241 
242     // Appends a stage for the specified matrix.
243     // Tries to optimize the stage by analyzing the type of matrix.
244     void append_matrix(SkArenaAlloc*, const SkMatrix&);
245 
246     // Appends a stage for a constant uniform color.
247     // Tries to optimize the stage based on the color.
248     void append_constant_color(SkArenaAlloc*, const float rgba[4]);
249 
append_constant_color(SkArenaAlloc * alloc,const SkColor4f & color)250     void append_constant_color(SkArenaAlloc* alloc, const SkColor4f& color) {
251         this->append_constant_color(alloc, color.vec());
252     }
253 
254     // Like append_constant_color() but only affecting r,g,b, ignoring the alpha channel.
255     void append_set_rgb(SkArenaAlloc*, const float rgb[3]);
256 
append_set_rgb(SkArenaAlloc * alloc,const SkColor4f & color)257     void append_set_rgb(SkArenaAlloc* alloc, const SkColor4f& color) {
258         this->append_set_rgb(alloc, color.vec());
259     }
260 
261     void append_load    (SkColorType, const SkRasterPipeline_MemoryCtx*);
262     void append_load_dst(SkColorType, const SkRasterPipeline_MemoryCtx*);
263     void append_store   (SkColorType, const SkRasterPipeline_MemoryCtx*);
264 
265     void append_gamut_clamp_if_normalized(const SkImageInfo&);
266 
empty()267     bool empty() const { return fStages == nullptr; }
268 
269 private:
270     struct StageList {
271         StageList* prev;
272         uint64_t   stage;
273         void*      ctx;
274         bool       rawFunction;
275     };
276 
277     using StartPipelineFn = void(*)(size_t,size_t,size_t,size_t, void** program);
278     StartPipelineFn build_pipeline(void**) const;
279 
280     void unchecked_append(StockStage, void*);
281 
282     // Used by old single-program void** style execution.
283     SkArenaAlloc* fAlloc;
284     StageList*    fStages;
285     int           fNumStages;
286     int           fSlotsNeeded;
287 };
288 
289 template <size_t bytes>
290 class SkRasterPipeline_ : public SkRasterPipeline {
291 public:
SkRasterPipeline_()292     SkRasterPipeline_()
293         : SkRasterPipeline(&fBuiltinAlloc) {}
294 
295 private:
296     SkSTArenaAlloc<bytes> fBuiltinAlloc;
297 };
298 
299 
300 #endif//SkRasterPipeline_DEFINED
301