• 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/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) M(swap_src_dst)                \
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(store_r8) \
61     M(load_rg1616) M(load_rg1616_dst) M(store_rg1616) M(gather_rg1616) \
62     M(load_16161616) M(load_16161616_dst) M(store_16161616) M(gather_16161616) \
63     M(load_1010102) M(load_1010102_dst) M(store_1010102) M(gather_1010102) \
64     M(alpha_to_gray) M(alpha_to_gray_dst)                          \
65     M(alpha_to_red) M(alpha_to_red_dst)                            \
66     M(bt709_luminance_or_luma_to_alpha) M(bt709_luminance_or_luma_to_rgb) \
67     M(bilerp_clamp_8888) M(bicubic_clamp_8888)                     \
68     M(store_u16_be)                                                \
69     M(load_src) M(store_src) M(store_src_a) M(load_dst) M(store_dst) \
70     M(scale_u8) M(scale_565) M(scale_1_float) M(scale_native)      \
71     M( lerp_u8) M( lerp_565) M( lerp_1_float) M(lerp_native)       \
72     M(dstatop) M(dstin) M(dstout) M(dstover)                       \
73     M(srcatop) M(srcin) M(srcout) M(srcover)                       \
74     M(clear) M(modulate) M(multiply) M(plus_) M(screen) M(xor_)    \
75     M(colorburn) M(colordodge) M(darken) M(difference)             \
76     M(exclusion) M(hardlight) M(lighten) M(overlay) M(softlight)   \
77     M(hue) M(saturation) M(color) M(luminosity)                    \
78     M(srcover_rgba_8888)                                           \
79     M(matrix_translate) M(matrix_scale_translate)                  \
80     M(matrix_2x3) M(matrix_3x3) M(matrix_3x4) M(matrix_4x5) M(matrix_4x3) \
81     M(matrix_perspective)                                          \
82     M(parametric) M(gamma_) M(PQish) M(HLGish) M(HLGinvish)        \
83     M(mirror_x)   M(repeat_x)                                      \
84     M(mirror_y)   M(repeat_y)                                      \
85     M(decal_x)    M(decal_y)   M(decal_x_and_y)                    \
86     M(check_decal_mask)                                            \
87     M(negate_x)                                                    \
88     M(bilinear) M(bicubic)                                         \
89     M(bilinear_nx) M(bilinear_px) M(bilinear_ny) M(bilinear_py)    \
90     M(bicubic_n3x) M(bicubic_n1x) M(bicubic_p1x) M(bicubic_p3x)    \
91     M(bicubic_n3y) M(bicubic_n1y) M(bicubic_p1y) M(bicubic_p3y)    \
92     M(save_xy) M(accumulate)                                       \
93     M(clamp_x_1) M(mirror_x_1) M(repeat_x_1)                       \
94     M(evenly_spaced_gradient)                                      \
95     M(gradient)                                                    \
96     M(evenly_spaced_2_stop_gradient)                               \
97     M(xy_to_unit_angle)                                            \
98     M(xy_to_radius)                                                \
99     M(xy_to_2pt_conical_strip)                                     \
100     M(xy_to_2pt_conical_focal_on_circle)                           \
101     M(xy_to_2pt_conical_well_behaved)                              \
102     M(xy_to_2pt_conical_smaller)                                   \
103     M(xy_to_2pt_conical_greater)                                   \
104     M(alter_2pt_conical_compensate_focal)                          \
105     M(alter_2pt_conical_unswap)                                    \
106     M(mask_2pt_conical_nan)                                        \
107     M(mask_2pt_conical_degenerates) M(apply_vector_mask)           \
108     M(byte_tables)                                                 \
109     M(rgb_to_hsl) M(hsl_to_rgb)                                    \
110     M(gauss_a_to_rgba)                                             \
111     M(emboss)                                                      \
112     M(swizzle)
113 
114 // The largest number of pixels we handle at a time.
115 static const int SkRasterPipeline_kMaxStride = 16;
116 
117 // Structs representing the arguments to some common stages.
118 
119 struct SkRasterPipeline_MemoryCtx {
120     void* pixels;
121     int   stride;
122 };
123 
124 struct SkRasterPipeline_GatherCtx {
125     const void* pixels;
126     int         stride;
127     float       width;
128     float       height;
129 };
130 
131 // State shared by save_xy, accumulate, and bilinear_* / bicubic_*.
132 struct SkRasterPipeline_SamplerCtx {
133     float      x[SkRasterPipeline_kMaxStride];
134     float      y[SkRasterPipeline_kMaxStride];
135     float     fx[SkRasterPipeline_kMaxStride];
136     float     fy[SkRasterPipeline_kMaxStride];
137     float scalex[SkRasterPipeline_kMaxStride];
138     float scaley[SkRasterPipeline_kMaxStride];
139 };
140 
141 struct SkRasterPipeline_TileCtx {
142     float scale;
143     float invScale; // cache of 1/scale
144 };
145 
146 struct SkRasterPipeline_DecalTileCtx {
147     uint32_t mask[SkRasterPipeline_kMaxStride];
148     float    limit_x;
149     float    limit_y;
150 };
151 
152 struct SkRasterPipeline_SamplerCtx2 : public SkRasterPipeline_GatherCtx {
153     SkColorType ct;
154     SkTileMode tileX, tileY;
155     float invWidth, invHeight;
156 };
157 
158 struct SkRasterPipeline_CallbackCtx {
159     void (*fn)(SkRasterPipeline_CallbackCtx* self, int active_pixels/*<= SkRasterPipeline_kMaxStride*/);
160 
161     // When called, fn() will have our active pixels available in rgba.
162     // When fn() returns, the pipeline will read back those active pixels from read_from.
163     float rgba[4*SkRasterPipeline_kMaxStride];
164     float* read_from = rgba;
165 };
166 
167 struct SkRasterPipeline_GradientCtx {
168     size_t stopCount;
169     float* fs[4];
170     float* bs[4];
171     float* ts;
172     bool interpolatedInPremul;
173 };
174 
175 struct SkRasterPipeline_EvenlySpaced2StopGradientCtx {
176     float f[4];
177     float b[4];
178     bool interpolatedInPremul;
179 };
180 
181 struct SkRasterPipeline_2PtConicalCtx {
182     uint32_t fMask[SkRasterPipeline_kMaxStride];
183     float    fP0,
184              fP1;
185 };
186 
187 struct SkRasterPipeline_UniformColorCtx {
188     float r,g,b,a;
189     uint16_t rgba[4];  // [0,255] in a 16-bit lane.
190 };
191 
192 struct SkRasterPipeline_EmbossCtx {
193     SkRasterPipeline_MemoryCtx mul,
194                                add;
195 };
196 
197 class SkRasterPipeline {
198 public:
199     explicit SkRasterPipeline(SkArenaAlloc*);
200 
201     SkRasterPipeline(const SkRasterPipeline&) = delete;
202     SkRasterPipeline(SkRasterPipeline&&)      = default;
203 
204     SkRasterPipeline& operator=(const SkRasterPipeline&) = delete;
205     SkRasterPipeline& operator=(SkRasterPipeline&&)      = default;
206 
207     void reset();
208 
209     enum StockStage {
210     #define M(stage) stage,
211         SK_RASTER_PIPELINE_STAGES(M)
212     #undef M
213     };
214     void append(StockStage, void* = nullptr);
append(StockStage stage,const void * ctx)215     void append(StockStage stage, const void* ctx) { this->append(stage, const_cast<void*>(ctx)); }
216     void append(StockStage, uintptr_t ctx);
217 
218     // Append all stages to this pipeline.
219     void extend(const SkRasterPipeline&);
220 
221     // Runs the pipeline in 2d from (x,y) inclusive to (x+w,y+h) exclusive.
222     void run(size_t x, size_t y, size_t w, size_t h) const;
223 
224     // Allocates a thunk which amortizes run() setup cost in alloc.
225     std::function<void(size_t, size_t, size_t, size_t)> compile() const;
226 
227     void dump() const;
228 
229     // Appends a stage for the specified matrix.
230     // Tries to optimize the stage by analyzing the type of matrix.
231     void append_matrix(SkArenaAlloc*, const SkMatrix&);
232 
233     // Appends a stage for a constant uniform color.
234     // Tries to optimize the stage based on the color.
235     void append_constant_color(SkArenaAlloc*, const float rgba[4]);
236 
append_constant_color(SkArenaAlloc * alloc,const SkColor4f & color)237     void append_constant_color(SkArenaAlloc* alloc, const SkColor4f& color) {
238         this->append_constant_color(alloc, color.vec());
239     }
240 
241     // Like append_constant_color() but only affecting r,g,b, ignoring the alpha channel.
242     void append_set_rgb(SkArenaAlloc*, const float rgb[3]);
243 
append_set_rgb(SkArenaAlloc * alloc,const SkColor4f & color)244     void append_set_rgb(SkArenaAlloc* alloc, const SkColor4f& color) {
245         this->append_set_rgb(alloc, color.vec());
246     }
247 
248     void append_load    (SkColorType, const SkRasterPipeline_MemoryCtx*);
249     void append_load_dst(SkColorType, const SkRasterPipeline_MemoryCtx*);
250     void append_store   (SkColorType, const SkRasterPipeline_MemoryCtx*);
251 
252     void append_gamut_clamp_if_normalized(const SkImageInfo&);
253 
254     void append_transfer_function(const skcms_TransferFunction&);
255 
empty()256     bool empty() const { return fStages == nullptr; }
257 
258 private:
259     struct StageList {
260         StageList* prev;
261         StockStage stage;
262         void*      ctx;
263     };
264 
265     using StartPipelineFn = void(*)(size_t,size_t,size_t,size_t, void** program);
266     StartPipelineFn build_pipeline(void**) const;
267 
268     void unchecked_append(StockStage, void*);
269 
270     // Used by old single-program void** style execution.
271     SkArenaAlloc* fAlloc;
272     StageList*    fStages;
273     int           fNumStages;
274     int           fSlotsNeeded;
275 };
276 
277 template <size_t bytes>
278 class SkRasterPipeline_ : public SkRasterPipeline {
279 public:
SkRasterPipeline_()280     SkRasterPipeline_()
281         : SkRasterPipeline(&fBuiltinAlloc) {}
282 
283 private:
284     SkSTArenaAlloc<bytes> fBuiltinAlloc;
285 };
286 
287 
288 #endif//SkRasterPipeline_DEFINED
289