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