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