SkRasterPipeline.h revision a9312fd56e2209f56222f822eb10ed0c25a42724
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(swap_src_dst)                              \
60    M(clamp_0) M(clamp_a) M(unpremul) M(premul)                  \
61    M(constant_color) M(store_f32)                               \
62    M(load_s_565)  M(load_d_565)  M(store_565)                   \
63    M(load_s_srgb) M(load_d_srgb) M(store_srgb)                  \
64    M(load_s_f16)  M(load_d_f16)  M(store_f16)                   \
65    M(load_s_8888) M(store_8888)                                 \
66    M(scale_u8) M(scale_constant_float)                          \
67    M(lerp_u8) M(lerp_565) M(lerp_constant_float)                \
68    M(dst)                                                       \
69    M(dstatop) M(dstin) M(dstout) M(dstover)                     \
70    M(srcatop) M(srcin) M(srcout) M(srcover)                     \
71    M(clear) M(modulate) M(multiply) M(plus_) M(screen) M(xor_)  \
72    M(colorburn) M(colordodge) M(darken) M(difference)           \
73    M(exclusion) M(hardlight) M(lighten) M(overlay) M(softlight) \
74    M(luminance_to_alpha) M(matrix_3x4) M(matrix_4x5)            \
75    M(fn_1_r) M(fn_1_g) M(fn_1_b)                                \
76    M(parametric_r) M(parametric_g) M(parametric_b)              \
77    M(table_r) M(table_g) M(table_b)                             \
78    M(color_lookup_table) M(lab_to_xyz) M(swap_rb)
79
80class SkRasterPipeline {
81public:
82    // No pipeline may be more than kMaxStages long.
83    static const int kMaxStages = 32;
84
85    SkRasterPipeline();
86
87    enum StockStage {
88    #define M(stage) stage,
89        SK_RASTER_PIPELINE_STAGES(M)
90    #undef M
91    };
92    void append(StockStage, void* = nullptr);
93    void append(StockStage stage, const void* ctx) { this->append(stage, const_cast<void*>(ctx)); }
94
95    // Append all stages to this pipeline.
96    void extend(const SkRasterPipeline&);
97
98    // Runs the pipeline walking x through [x,x+n), holding y constant.
99    std::function<void(size_t x, size_t y, size_t n)> compile() const;
100
101    void dump() const;
102
103    struct Stage {
104        StockStage stage;
105        void*        ctx;
106    };
107
108private:
109    int   fNum   = 0;
110    Stage fStages[kMaxStages];
111};
112
113#endif//SkRasterPipeline_DEFINED
114