SkRasterPipeline.h revision cc63173634e773eac5de0b92f05d117ed2bdca84
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 "SkNx.h"
12#include "SkTArray.h"
13#include "SkTypes.h"
14#include <functional>
15#include <vector>
16
17/**
18 * SkRasterPipeline provides a cheap way to chain together a pixel processing pipeline.
19 *
20 * It's particularly designed for situations where the potential pipeline is extremely
21 * combinatoric: {N dst formats} x {M source formats} x {K mask formats} x {C transfer modes} ...
22 * No one wants to write specialized routines for all those combinations, and if we did, we'd
23 * end up bloating our code size dramatically.  SkRasterPipeline stages can be chained together
24 * at runtime, so we can scale this problem linearly rather than combinatorically.
25 *
26 * Each stage is represented by a function conforming to a common interface, SkRasterPipeline::Fn,
27 * and by an arbitrary context pointer.  Fn's arguments, and sometimes custom calling convention,
28 * are designed to maximize the amount of data we can pass along the pipeline cheaply.
29 * On many machines all arguments stay in registers the entire time.
30 *
31 * The meaning of the arguments to Fn are sometimes fixed:
32 *    - The Stage* always represents the current stage, mainly providing access to ctx().
33 *    - The first size_t is always the destination x coordinate.
34 *      (If you need y, put it in your context.)
35 *    - The second size_t is always tail: 0 when working on a full 4-pixel slab,
36 *      or 1..3 when using only the bottom 1..3 lanes of each register.
37 *    - By the time the shader's done, the first four vectors should hold source red,
38 *      green, blue, and alpha, up to 4 pixels' worth each.
39 *
40 * Sometimes arguments are flexible:
41 *    - In the shader, the first four vectors can be used for anything, e.g. sample coordinates.
42 *    - The last four vectors are scratch registers that can be used to communicate between
43 *      stages; transfer modes use these to hold the original destination pixel components.
44 *
45 * On some platforms the last four vectors are slower to work with than the other arguments.
46 *
47 * When done mutating its arguments and/or context, a stage can either:
48 *   1) call st->next() with its mutated arguments, chaining to the next stage of the pipeline; or
49 *   2) return, indicating the pipeline is complete for these pixels.
50 *
51 * Some stages that typically return are those that write a color to a destination pointer,
52 * but any stage can short-circuit the rest of the pipeline by returning instead of calling next().
53 */
54
55// TODO: There may be a better place to stuff tail, e.g. in the bottom alignment bits of
56// the Stage*.  This mostly matters on 64-bit Windows where every register is precious.
57
58#define SK_RASTER_PIPELINE_STAGES(M)                             \
59    M(trace) M(registers)                                        \
60    M(move_src_dst) M(move_dst_src) M(swap_rb) M(swap_rb_d)      \
61    M(clamp_0) M(clamp_a) M(clamp_1)                             \
62    M(unpremul) M(premul)                                        \
63    M(set_rgb)                                                   \
64    M(from_srgb) M(from_srgb_d) M(to_srgb)                       \
65    M(to_2dot2)                                                  \
66    M(constant_color) M(store_f32)                               \
67    M(load_565)  M(load_565_d)  M(store_565)                     \
68    M(load_f16)  M(load_f16_d)  M(store_f16)                     \
69    M(load_8888) M(load_8888_d) M(store_8888)                    \
70    M(load_tables) M(store_tables)                               \
71    M(scale_u8) M(scale_1_float)                                 \
72    M(lerp_u8) M(lerp_565) M(lerp_1_float)                       \
73    M(dstatop) M(dstin) M(dstout) M(dstover)                     \
74    M(srcatop) M(srcin) M(srcout) M(srcover)                     \
75    M(clear) M(modulate) M(multiply) M(plus_) M(screen) M(xor_)  \
76    M(colorburn) M(colordodge) M(darken) M(difference)           \
77    M(exclusion) M(hardlight) M(lighten) M(overlay) M(softlight) \
78    M(luminance_to_alpha)                                        \
79    M(matrix_2x3) M(matrix_3x4) M(matrix_4x5)                    \
80    M(matrix_perspective)                                        \
81    M(parametric_r) M(parametric_g) M(parametric_b)              \
82    M(parametric_a)                                              \
83    M(table_r) M(table_g) M(table_b) M(table_a)                  \
84    M(color_lookup_table) M(lab_to_xyz)                          \
85    M(clamp_x) M(mirror_x) M(repeat_x)                           \
86    M(clamp_y) M(mirror_y) M(repeat_y)                           \
87    M(gather_a8) M(gather_g8) M(gather_i8)                       \
88    M(gather_565) M(gather_4444) M(gather_8888) M(gather_f16)    \
89    M(top_left) M(top_right) M(bottom_left) M(bottom_right)      \
90    M(accumulate)
91
92class SkRasterPipeline {
93public:
94    SkRasterPipeline();
95
96    enum StockStage {
97    #define M(stage) stage,
98        SK_RASTER_PIPELINE_STAGES(M)
99    #undef M
100    };
101    void append(StockStage, void* = nullptr);
102    void append(StockStage stage, const void* ctx) { this->append(stage, const_cast<void*>(ctx)); }
103
104    // Append all stages to this pipeline.
105    void extend(const SkRasterPipeline&);
106
107    // Runs the pipeline walking x through [x,x+n), holding y constant.
108    void run(size_t x, size_t y, size_t n) const;
109
110    // If you're going to run() the pipeline more than once, it's best to compile it.
111    std::function<void(size_t x, size_t y, size_t n)> compile() const;
112
113    void dump() const;
114
115    struct Stage {
116        StockStage stage;
117        void*        ctx;
118    };
119
120private:
121    std::vector<Stage> fStages;
122};
123
124#endif//SkRasterPipeline_DEFINED
125