mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 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 | |
Mike Klein | 0a76b41 | 2017-05-22 12:01:59 -0400 | [diff] [blame] | 11 | #include "SkArenaAlloc.h" |
Mike Klein | d37d5d9 | 2016-12-14 13:38:24 +0000 | [diff] [blame] | 12 | #include "SkImageInfo.h" |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 13 | #include "SkNx.h" |
| 14 | #include "SkTArray.h" |
| 15 | #include "SkTypes.h" |
Mike Klein | 0a76b41 | 2017-05-22 12:01:59 -0400 | [diff] [blame] | 16 | #include <functional> |
Mike Klein | cc63173 | 2016-12-06 09:17:55 -0500 | [diff] [blame] | 17 | #include <vector> |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 18 | |
Mike Klein | bba02c2 | 2017-06-02 10:03:30 -0400 | [diff] [blame] | 19 | struct SkJumper_constants; |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 20 | |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 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 | * |
Mike Klein | c8dd6bc | 2016-09-28 10:43:53 -0400 | [diff] [blame] | 35 | * The meaning of the arguments to Fn are sometimes fixed: |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 36 | * - The Stage* always represents the current stage, mainly providing access to ctx(). |
Mike Klein | c8dd6bc | 2016-09-28 10:43:53 -0400 | [diff] [blame] | 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. |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 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 | * |
Mike Klein | c8dd6bc | 2016-09-28 10:43:53 -0400 | [diff] [blame] | 44 | * Sometimes arguments are flexible: |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 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 | * |
Mike Klein | c8dd6bc | 2016-09-28 10:43:53 -0400 | [diff] [blame] | 55 | * Some stages that typically return are those that write a color to a destination pointer, |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 56 | * but any stage can short-circuit the rest of the pipeline by returning instead of calling next(). |
| 57 | */ |
| 58 | |
Mike Klein | c8dd6bc | 2016-09-28 10:43:53 -0400 | [diff] [blame] | 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 | |
Mike Klein | 1f49f26 | 2016-10-31 19:49:27 -0400 | [diff] [blame] | 62 | #define SK_RASTER_PIPELINE_STAGES(M) \ |
Mike Klein | 7fee90c | 2017-04-07 16:55:09 -0400 | [diff] [blame] | 63 | M(callback) \ |
Mike Klein | 8c8cb5b | 2017-01-06 10:21:56 -0500 | [diff] [blame] | 64 | M(move_src_dst) M(move_dst_src) M(swap) \ |
Mike Reed | 279091e | 2017-06-27 16:58:00 -0400 | [diff] [blame^] | 65 | M(clamp_0) M(clamp_1) M(clamp_a) M(clamp_a_dst) \ |
Mike Klein | d5de013 | 2016-11-28 09:33:02 -0500 | [diff] [blame] | 66 | M(unpremul) M(premul) \ |
Mike Reed | 279091e | 2017-06-27 16:58:00 -0400 | [diff] [blame^] | 67 | M(set_rgb) M(swap_rb) M(swap_rb_dst) \ |
| 68 | M(from_srgb) M(from_srgb_dst) M(to_srgb) \ |
Mike Klein | 581e698 | 2017-05-03 13:05:13 -0400 | [diff] [blame] | 69 | M(constant_color) M(seed_shader) M(dither) \ |
Mike Reed | 279091e | 2017-06-27 16:58:00 -0400 | [diff] [blame^] | 70 | M(load_a8) M(load_a8_dst) M(store_a8) \ |
| 71 | M(load_g8) M(load_g8_dst) \ |
| 72 | M(load_565) M(load_565_dst) M(store_565) \ |
| 73 | M(load_4444) M(load_4444_dst) M(store_4444) \ |
| 74 | M(load_f16) M(load_f16_dst) M(store_f16) \ |
| 75 | M(load_f32) M(load_f32_dst) M(store_f32) \ |
| 76 | M(load_8888) M(load_8888_dst) M(store_8888) \ |
Matt Sarett | 1da27ef | 2017-01-19 17:14:07 -0500 | [diff] [blame] | 77 | M(load_u16_be) M(load_rgb_u16_be) M(store_u16_be) \ |
Matt Sarett | 5bee0b6 | 2017-01-19 12:04:32 -0500 | [diff] [blame] | 78 | M(load_tables_u16_be) M(load_tables_rgb_u16_be) \ |
Mike Reed | 9959f72 | 2017-05-15 09:34:22 -0400 | [diff] [blame] | 79 | M(load_tables) M(load_rgba) M(store_rgba) \ |
Mike Klein | babd93e | 2016-11-30 16:05:10 -0500 | [diff] [blame] | 80 | M(scale_u8) M(scale_1_float) \ |
| 81 | M(lerp_u8) M(lerp_565) M(lerp_1_float) \ |
Mike Klein | 1f49f26 | 2016-10-31 19:49:27 -0400 | [diff] [blame] | 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) \ |
Mike Klein | bb33833 | 2017-05-04 12:42:52 -0400 | [diff] [blame] | 87 | M(hue) M(saturation) M(color) M(luminosity) \ |
Mike Klein | 5062626 | 2017-05-25 13:06:57 -0400 | [diff] [blame] | 88 | M(srcover_rgba_8888) \ |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 89 | M(luminance_to_alpha) \ |
Mike Reed | 0264095 | 2017-05-19 15:32:13 -0400 | [diff] [blame] | 90 | M(matrix_2x3) M(matrix_3x4) M(matrix_4x5) M(matrix_4x3) \ |
Mike Klein | c01e7df | 2016-11-17 16:27:10 -0500 | [diff] [blame] | 91 | M(matrix_perspective) \ |
Mike Klein | cfcf624 | 2016-11-16 09:01:30 -0500 | [diff] [blame] | 92 | M(parametric_r) M(parametric_g) M(parametric_b) \ |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 93 | M(parametric_a) \ |
| 94 | M(table_r) M(table_g) M(table_b) M(table_a) \ |
Mike Klein | c17dc24 | 2017-04-20 16:21:57 -0400 | [diff] [blame] | 95 | M(lab_to_xyz) \ |
Mike Klein | 9f85d68 | 2017-05-23 07:52:01 -0400 | [diff] [blame] | 96 | M(clamp_x) M(mirror_x) M(repeat_x) \ |
| 97 | M(clamp_y) M(mirror_y) M(repeat_y) \ |
| 98 | M(clamp_x_1) M(mirror_x_1) M(repeat_x_1) \ |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 99 | M(gather_a8) M(gather_g8) M(gather_i8) \ |
| 100 | M(gather_565) M(gather_4444) M(gather_8888) M(gather_f16) \ |
Mike Klein | b0b17d1 | 2016-12-09 16:25:44 -0500 | [diff] [blame] | 101 | M(bilinear_nx) M(bilinear_px) M(bilinear_ny) M(bilinear_py) \ |
| 102 | M(bicubic_n3x) M(bicubic_n1x) M(bicubic_p1x) M(bicubic_p3x) \ |
| 103 | M(bicubic_n3y) M(bicubic_n1y) M(bicubic_p1y) M(bicubic_p3y) \ |
Florin Malita | c86e470 | 2017-01-20 08:41:34 -0500 | [diff] [blame] | 104 | M(save_xy) M(accumulate) \ |
Herb Derby | 4de1304 | 2017-05-15 10:49:39 -0400 | [diff] [blame] | 105 | M(evenly_spaced_gradient) \ |
Mike Reed | 6533159 | 2017-05-24 16:45:34 -0400 | [diff] [blame] | 106 | M(gauss_a_to_rgba) M(gradient) \ |
Mike Klein | 5c7960b | 2017-05-11 10:59:22 -0400 | [diff] [blame] | 107 | M(evenly_spaced_2_stop_gradient) \ |
| 108 | M(xy_to_unit_angle) \ |
Herb Derby | 090fbf8 | 2017-05-08 15:10:36 -0400 | [diff] [blame] | 109 | M(xy_to_radius) \ |
Matt Sarett | e522f4c | 2017-02-22 13:02:31 -0500 | [diff] [blame] | 110 | M(byte_tables) M(byte_tables_rgb) \ |
Dominic Mazzoni | 394d414 | 2017-02-14 11:15:31 -0800 | [diff] [blame] | 111 | M(rgb_to_hsl) \ |
| 112 | M(hsl_to_rgb) |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 113 | |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 114 | class SkRasterPipeline { |
| 115 | public: |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 116 | explicit SkRasterPipeline(SkArenaAlloc*); |
| 117 | |
| 118 | SkRasterPipeline(const SkRasterPipeline&) = delete; |
| 119 | SkRasterPipeline(SkRasterPipeline&&) = default; |
| 120 | |
| 121 | SkRasterPipeline& operator=(const SkRasterPipeline&) = delete; |
| 122 | SkRasterPipeline& operator=(SkRasterPipeline&&) = default; |
| 123 | |
| 124 | void reset(); |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 125 | |
Mike Klein | fa9f241 | 2016-09-29 13:40:01 -0400 | [diff] [blame] | 126 | enum StockStage { |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 127 | #define M(stage) stage, |
| 128 | SK_RASTER_PIPELINE_STAGES(M) |
| 129 | #undef M |
Mike Klein | fa9f241 | 2016-09-29 13:40:01 -0400 | [diff] [blame] | 130 | }; |
Mike Klein | 26bea5d | 2016-10-05 10:36:38 -0400 | [diff] [blame] | 131 | void append(StockStage, void* = nullptr); |
| 132 | void append(StockStage stage, const void* ctx) { this->append(stage, const_cast<void*>(ctx)); } |
Mike Klein | fa9f241 | 2016-09-29 13:40:01 -0400 | [diff] [blame] | 133 | |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 134 | // Append all stages to this pipeline. |
| 135 | void extend(const SkRasterPipeline&); |
| 136 | |
Mike Klein | 319ba3d | 2017-01-20 15:11:54 -0500 | [diff] [blame] | 137 | // Runs the pipeline walking x through [x,x+n). |
Mike Klein | 761d27c | 2017-06-01 12:37:08 -0400 | [diff] [blame] | 138 | void run(size_t x, size_t y, size_t n) const; |
Mike Klein | c789b61 | 2016-11-30 13:45:06 -0500 | [diff] [blame] | 139 | |
Mike Klein | 0a76b41 | 2017-05-22 12:01:59 -0400 | [diff] [blame] | 140 | // Allocates a thunk which amortizes run() setup cost in alloc. |
Mike Klein | 761d27c | 2017-06-01 12:37:08 -0400 | [diff] [blame] | 141 | std::function<void(size_t, size_t, size_t)> compile() const; |
Mike Klein | 0a76b41 | 2017-05-22 12:01:59 -0400 | [diff] [blame] | 142 | |
Mike Klein | 3928c6b | 2016-11-15 16:18:38 -0500 | [diff] [blame] | 143 | void dump() const; |
| 144 | |
Mike Klein | d37d5d9 | 2016-12-14 13:38:24 +0000 | [diff] [blame] | 145 | // Conversion from sRGB can be subtly tricky when premultiplication is involved. |
| 146 | // Use these helpers to keep things sane. |
Mike Klein | 8c8cb5b | 2017-01-06 10:21:56 -0500 | [diff] [blame] | 147 | void append_from_srgb(SkAlphaType); |
Mike Reed | 279091e | 2017-06-27 16:58:00 -0400 | [diff] [blame^] | 148 | void append_from_srgb_dst(SkAlphaType); |
Mike Klein | d37d5d9 | 2016-12-14 13:38:24 +0000 | [diff] [blame] | 149 | |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 150 | bool empty() const { return fStages == nullptr; } |
Mike Klein | 1859f69 | 2017-05-22 08:28:45 -0400 | [diff] [blame] | 151 | |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 152 | private: |
Mike Klein | eeccbf7 | 2017-06-05 11:02:15 -0400 | [diff] [blame] | 153 | using StartPipelineFn = void(size_t,size_t,size_t,void**,const SkJumper_constants*); |
Mike Klein | bba02c2 | 2017-06-02 10:03:30 -0400 | [diff] [blame] | 154 | |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 155 | struct StageList { |
| 156 | StageList* prev; |
| 157 | StockStage stage; |
| 158 | void* ctx; |
| 159 | }; |
| 160 | |
Mike Klein | bba02c2 | 2017-06-02 10:03:30 -0400 | [diff] [blame] | 161 | StartPipelineFn* build_pipeline(void**) const; |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 162 | void unchecked_append(StockStage, void*); |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 163 | |
| 164 | SkArenaAlloc* fAlloc; |
| 165 | StageList* fStages; |
Mike Klein | 9fff111 | 2017-05-24 11:37:52 -0400 | [diff] [blame] | 166 | int fNumStages; |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 167 | int fSlotsNeeded; |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 168 | }; |
| 169 | |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 170 | template <size_t bytes> |
| 171 | class SkRasterPipeline_ : public SkRasterPipeline { |
| 172 | public: |
| 173 | SkRasterPipeline_() |
Florin Malita | 14a6430 | 2017-05-24 14:53:44 -0400 | [diff] [blame] | 174 | : SkRasterPipeline(&fBuiltinAlloc) {} |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 175 | |
| 176 | private: |
Florin Malita | 14a6430 | 2017-05-24 14:53:44 -0400 | [diff] [blame] | 177 | SkSTArenaAlloc<bytes> fBuiltinAlloc; |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 181 | #endif//SkRasterPipeline_DEFINED |