SkRasterPipeline in SkArenaAlloc
[platform/upstream/libSkiaSharp.git] / src / core / SkRasterPipeline.h
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 "SkImageInfo.h"
13 #include "SkNx.h"
14 #include "SkTArray.h"
15 #include "SkTypes.h"
16 #include <functional>
17 #include <vector>
18
19 struct SkJumper_Engine;
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, SkRasterPipeline::Fn,
31  * and by an arbitrary context pointer.  Fn's arguments, and sometimes custom calling convention,
32  * are designed to maximize the amount of data we can pass along the pipeline cheaply.
33  * On many machines all arguments stay in registers the entire time.
34  *
35  * The meaning of the arguments to Fn are sometimes fixed:
36  *    - The Stage* always represents the current stage, mainly providing access to ctx().
37  *    - The first size_t is always the destination x coordinate.
38  *      (If you need y, put it in your context.)
39  *    - The second size_t is always tail: 0 when working on a full 4-pixel slab,
40  *      or 1..3 when using only the bottom 1..3 lanes of each register.
41  *    - By the time the shader's done, the first four vectors should hold source red,
42  *      green, blue, and alpha, up to 4 pixels' worth each.
43  *
44  * Sometimes arguments are flexible:
45  *    - In the shader, the first four vectors can be used for anything, e.g. sample coordinates.
46  *    - The last four vectors are scratch registers that can be used to communicate between
47  *      stages; transfer modes use these to hold the original destination pixel components.
48  *
49  * On some platforms the last four vectors are slower to work with than the other arguments.
50  *
51  * When done mutating its arguments and/or context, a stage can either:
52  *   1) call st->next() with its mutated arguments, chaining to the next stage of the pipeline; or
53  *   2) return, indicating the pipeline is complete for these pixels.
54  *
55  * Some stages that typically return are those that write a color to a destination pointer,
56  * but any stage can short-circuit the rest of the pipeline by returning instead of calling next().
57  */
58
59 // TODO: There may be a better place to stuff tail, e.g. in the bottom alignment bits of
60 // the Stage*.  This mostly matters on 64-bit Windows where every register is precious.
61
62 #define SK_RASTER_PIPELINE_STAGES(M)                             \
63     M(callback)                                                  \
64     M(move_src_dst) M(move_dst_src) M(swap)                      \
65     M(clamp_0) M(clamp_1) M(clamp_a)                             \
66     M(unpremul) M(premul)                                        \
67     M(set_rgb) M(swap_rb)                                        \
68     M(from_srgb) M(to_srgb)                                      \
69     M(constant_color) M(seed_shader) M(dither)                   \
70     M(load_a8)   M(store_a8)                                     \
71     M(load_g8)                                                   \
72     M(load_565)  M(store_565)                                    \
73     M(load_4444) M(store_4444)                                   \
74     M(load_f16)  M(store_f16)                                    \
75     M(load_f32)  M(store_f32)                                    \
76     M(load_8888) M(store_8888)                                   \
77     M(load_u16_be) M(load_rgb_u16_be) M(store_u16_be)            \
78     M(load_tables_u16_be) M(load_tables_rgb_u16_be)              \
79     M(load_tables) M(load_rgba) M(store_rgba)                    \
80     M(scale_u8) M(scale_1_float)                                 \
81     M(lerp_u8) M(lerp_565) M(lerp_1_float)                       \
82     M(dstatop) M(dstin) M(dstout) M(dstover)                     \
83     M(srcatop) M(srcin) M(srcout) M(srcover)                     \
84     M(clear) M(modulate) M(multiply) M(plus_) M(screen) M(xor_)  \
85     M(colorburn) M(colordodge) M(darken) M(difference)           \
86     M(exclusion) M(hardlight) M(lighten) M(overlay) M(softlight) \
87     M(hue) M(saturation) M(color) M(luminosity)                  \
88     M(luminance_to_alpha)                                        \
89     M(matrix_2x3) M(matrix_3x4) M(matrix_4x5) M(matrix_4x3)      \
90     M(matrix_perspective)                                        \
91     M(parametric_r) M(parametric_g) M(parametric_b)              \
92     M(parametric_a)                                              \
93     M(table_r) M(table_g) M(table_b) M(table_a)                  \
94     M(lab_to_xyz)                                                \
95     M(clamp_x)   M(mirror_x)   M(repeat_x)                       \
96     M(clamp_y)   M(mirror_y)   M(repeat_y)                       \
97     M(clamp_x_1) M(mirror_x_1) M(repeat_x_1)                     \
98     M(gather_a8) M(gather_g8) M(gather_i8)                       \
99     M(gather_565) M(gather_4444) M(gather_8888) M(gather_f16)    \
100     M(bilinear_nx) M(bilinear_px) M(bilinear_ny) M(bilinear_py)  \
101     M(bicubic_n3x) M(bicubic_n1x) M(bicubic_p1x) M(bicubic_p3x)  \
102     M(bicubic_n3y) M(bicubic_n1y) M(bicubic_p1y) M(bicubic_p3y)  \
103     M(save_xy) M(accumulate)                                     \
104     M(evenly_spaced_gradient)                                    \
105     M(gradient)                                                  \
106     M(evenly_spaced_2_stop_gradient)                             \
107     M(xy_to_unit_angle)                                          \
108     M(xy_to_radius)                                              \
109     M(byte_tables) M(byte_tables_rgb)                            \
110     M(rgb_to_hsl)                                                \
111     M(hsl_to_rgb)
112
113 class SkRasterPipeline {
114 public:
115     explicit SkRasterPipeline(SkArenaAlloc*);
116
117     SkRasterPipeline(const SkRasterPipeline&) = delete;
118     SkRasterPipeline(SkRasterPipeline&&)      = default;
119
120     SkRasterPipeline& operator=(const SkRasterPipeline&) = delete;
121     SkRasterPipeline& operator=(SkRasterPipeline&&)      = default;
122
123     void reset();
124
125     enum StockStage {
126     #define M(stage) stage,
127         SK_RASTER_PIPELINE_STAGES(M)
128     #undef M
129     };
130     void append(StockStage, void* = nullptr);
131     void append(StockStage stage, const void* ctx) { this->append(stage, const_cast<void*>(ctx)); }
132
133     // Append all stages to this pipeline.
134     void extend(const SkRasterPipeline&);
135
136     // Runs the pipeline walking x through [x,x+n).
137     void run(size_t x, size_t n) const;
138
139     // Allocates a thunk which amortizes run() setup cost in alloc.
140     std::function<void(size_t, size_t)> compile() const;
141
142     void dump() const;
143
144     // Conversion from sRGB can be subtly tricky when premultiplication is involved.
145     // Use these helpers to keep things sane.
146     void append_from_srgb(SkAlphaType);
147
148     bool empty() const { return fStages == nullptr; }
149
150 private:
151     struct StageList {
152         StageList* prev;
153         StockStage stage;
154         void*      ctx;
155     };
156
157     static void BuildPipeline(const StageList*, const SkJumper_Engine&, void**);
158     void unchecked_append(StockStage, void*);
159     void extend(const StageList*);
160
161     SkArenaAlloc* fAlloc;
162     StageList*    fStages;
163     int           fSlotsNeeded;
164 };
165
166 template <size_t bytes>
167 class SkRasterPipeline_ : public SkRasterPipeline {
168 public:
169     SkRasterPipeline_()
170         : SkRasterPipeline(&fBuiltinAlloc)
171         , fBuiltinAlloc(fBuffer) {}
172
173 private:
174     char         fBuffer[bytes];
175     SkArenaAlloc fBuiltinAlloc;
176 };
177
178
179 #endif//SkRasterPipeline_DEFINED