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