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