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 | d37d5d9 | 2016-12-14 13:38:24 +0000 | [diff] [blame] | 11 | #include "SkImageInfo.h" |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 12 | #include "SkNx.h" |
| 13 | #include "SkTArray.h" |
| 14 | #include "SkTypes.h" |
Mike Klein | e9f74b8 | 2016-10-25 13:31:21 -0400 | [diff] [blame] | 15 | #include <functional> |
Mike Klein | cc63173 | 2016-12-06 09:17:55 -0500 | [diff] [blame] | 16 | #include <vector> |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 17 | |
| 18 | /** |
| 19 | * SkRasterPipeline provides a cheap way to chain together a pixel processing pipeline. |
| 20 | * |
| 21 | * It's particularly designed for situations where the potential pipeline is extremely |
| 22 | * combinatoric: {N dst formats} x {M source formats} x {K mask formats} x {C transfer modes} ... |
| 23 | * No one wants to write specialized routines for all those combinations, and if we did, we'd |
| 24 | * end up bloating our code size dramatically. SkRasterPipeline stages can be chained together |
| 25 | * at runtime, so we can scale this problem linearly rather than combinatorically. |
| 26 | * |
| 27 | * Each stage is represented by a function conforming to a common interface, SkRasterPipeline::Fn, |
| 28 | * and by an arbitrary context pointer. Fn's arguments, and sometimes custom calling convention, |
| 29 | * are designed to maximize the amount of data we can pass along the pipeline cheaply. |
| 30 | * On many machines all arguments stay in registers the entire time. |
| 31 | * |
Mike Klein | c8dd6bc | 2016-09-28 10:43:53 -0400 | [diff] [blame] | 32 | * The meaning of the arguments to Fn are sometimes fixed: |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 33 | * - The Stage* always represents the current stage, mainly providing access to ctx(). |
Mike Klein | c8dd6bc | 2016-09-28 10:43:53 -0400 | [diff] [blame] | 34 | * - The first size_t is always the destination x coordinate. |
| 35 | * (If you need y, put it in your context.) |
| 36 | * - The second size_t is always tail: 0 when working on a full 4-pixel slab, |
| 37 | * 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] | 38 | * - By the time the shader's done, the first four vectors should hold source red, |
| 39 | * green, blue, and alpha, up to 4 pixels' worth each. |
| 40 | * |
Mike Klein | c8dd6bc | 2016-09-28 10:43:53 -0400 | [diff] [blame] | 41 | * Sometimes arguments are flexible: |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 42 | * - In the shader, the first four vectors can be used for anything, e.g. sample coordinates. |
| 43 | * - The last four vectors are scratch registers that can be used to communicate between |
| 44 | * stages; transfer modes use these to hold the original destination pixel components. |
| 45 | * |
| 46 | * On some platforms the last four vectors are slower to work with than the other arguments. |
| 47 | * |
| 48 | * When done mutating its arguments and/or context, a stage can either: |
| 49 | * 1) call st->next() with its mutated arguments, chaining to the next stage of the pipeline; or |
| 50 | * 2) return, indicating the pipeline is complete for these pixels. |
| 51 | * |
Mike Klein | c8dd6bc | 2016-09-28 10:43:53 -0400 | [diff] [blame] | 52 | * 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] | 53 | * but any stage can short-circuit the rest of the pipeline by returning instead of calling next(). |
| 54 | */ |
| 55 | |
Mike Klein | c8dd6bc | 2016-09-28 10:43:53 -0400 | [diff] [blame] | 56 | // TODO: There may be a better place to stuff tail, e.g. in the bottom alignment bits of |
| 57 | // the Stage*. This mostly matters on 64-bit Windows where every register is precious. |
| 58 | |
Mike Klein | 1f49f26 | 2016-10-31 19:49:27 -0400 | [diff] [blame] | 59 | #define SK_RASTER_PIPELINE_STAGES(M) \ |
Mike Klein | a9312fd | 2016-11-16 13:38:15 -0500 | [diff] [blame] | 60 | M(trace) M(registers) \ |
Mike Klein | 8c8cb5b | 2017-01-06 10:21:56 -0500 | [diff] [blame] | 61 | M(move_src_dst) M(move_dst_src) M(swap) \ |
| 62 | M(clamp_0) M(clamp_1) M(clamp_a) \ |
Mike Klein | d5de013 | 2016-11-28 09:33:02 -0500 | [diff] [blame] | 63 | M(unpremul) M(premul) \ |
Mike Klein | 8c8cb5b | 2017-01-06 10:21:56 -0500 | [diff] [blame] | 64 | M(set_rgb) M(swap_rb) \ |
| 65 | M(from_srgb) M(to_srgb) \ |
raftias | 9752454 | 2016-12-14 13:15:05 -0500 | [diff] [blame] | 66 | M(from_2dot2) M(to_2dot2) \ |
mtklein | a4a4488 | 2016-11-04 13:20:07 -0700 | [diff] [blame] | 67 | M(constant_color) M(store_f32) \ |
Mike Klein | e71b167 | 2017-01-13 07:59:23 -0500 | [diff] [blame] | 68 | M(load_a8) M(store_a8) \ |
Mike Klein | 8c8cb5b | 2017-01-06 10:21:56 -0500 | [diff] [blame] | 69 | M(load_565) M(store_565) \ |
| 70 | M(load_f16) M(store_f16) \ |
| 71 | M(load_8888) M(store_8888) \ |
Matt Sarett | 1da27ef | 2017-01-19 17:14:07 -0500 | [diff] [blame^] | 72 | 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] | 73 | M(load_tables_u16_be) M(load_tables_rgb_u16_be) \ |
| 74 | M(load_tables) M(store_tables) \ |
Mike Klein | babd93e | 2016-11-30 16:05:10 -0500 | [diff] [blame] | 75 | M(scale_u8) M(scale_1_float) \ |
| 76 | M(lerp_u8) M(lerp_565) M(lerp_1_float) \ |
Mike Klein | 1f49f26 | 2016-10-31 19:49:27 -0400 | [diff] [blame] | 77 | M(dstatop) M(dstin) M(dstout) M(dstover) \ |
| 78 | M(srcatop) M(srcin) M(srcout) M(srcover) \ |
| 79 | M(clear) M(modulate) M(multiply) M(plus_) M(screen) M(xor_) \ |
| 80 | M(colorburn) M(colordodge) M(darken) M(difference) \ |
| 81 | M(exclusion) M(hardlight) M(lighten) M(overlay) M(softlight) \ |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 82 | M(luminance_to_alpha) \ |
| 83 | M(matrix_2x3) M(matrix_3x4) M(matrix_4x5) \ |
Mike Klein | c01e7df | 2016-11-17 16:27:10 -0500 | [diff] [blame] | 84 | M(matrix_perspective) \ |
Mike Klein | cfcf624 | 2016-11-16 09:01:30 -0500 | [diff] [blame] | 85 | M(parametric_r) M(parametric_g) M(parametric_b) \ |
raftias | 5476128 | 2016-12-01 13:44:07 -0500 | [diff] [blame] | 86 | M(parametric_a) \ |
| 87 | M(table_r) M(table_g) M(table_b) M(table_a) \ |
Mike Klein | d5de013 | 2016-11-28 09:33:02 -0500 | [diff] [blame] | 88 | M(color_lookup_table) M(lab_to_xyz) \ |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 89 | M(clamp_x) M(mirror_x) M(repeat_x) \ |
| 90 | M(clamp_y) M(mirror_y) M(repeat_y) \ |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 91 | M(gather_a8) M(gather_g8) M(gather_i8) \ |
| 92 | M(gather_565) M(gather_4444) M(gather_8888) M(gather_f16) \ |
Mike Klein | b0b17d1 | 2016-12-09 16:25:44 -0500 | [diff] [blame] | 93 | M(bilinear_nx) M(bilinear_px) M(bilinear_ny) M(bilinear_py) \ |
| 94 | M(bicubic_n3x) M(bicubic_n1x) M(bicubic_p1x) M(bicubic_p3x) \ |
| 95 | M(bicubic_n3y) M(bicubic_n1y) M(bicubic_p1y) M(bicubic_p3y) \ |
Mike Klein | 886cf53 | 2016-12-06 11:31:25 -0500 | [diff] [blame] | 96 | M(save_xy) M(accumulate) |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 97 | |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 98 | class SkRasterPipeline { |
| 99 | public: |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 100 | SkRasterPipeline(); |
| 101 | |
Mike Klein | fa9f241 | 2016-09-29 13:40:01 -0400 | [diff] [blame] | 102 | enum StockStage { |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 103 | #define M(stage) stage, |
| 104 | SK_RASTER_PIPELINE_STAGES(M) |
| 105 | #undef M |
Mike Klein | fa9f241 | 2016-09-29 13:40:01 -0400 | [diff] [blame] | 106 | }; |
Mike Klein | 26bea5d | 2016-10-05 10:36:38 -0400 | [diff] [blame] | 107 | void append(StockStage, void* = nullptr); |
| 108 | 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] | 109 | |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 110 | // Append all stages to this pipeline. |
| 111 | void extend(const SkRasterPipeline&); |
| 112 | |
Mike Klein | af49b19 | 2016-11-15 08:52:04 -0500 | [diff] [blame] | 113 | // Runs the pipeline walking x through [x,x+n), holding y constant. |
Mike Klein | c789b61 | 2016-11-30 13:45:06 -0500 | [diff] [blame] | 114 | void run(size_t x, size_t y, size_t n) const; |
| 115 | |
| 116 | // If you're going to run() the pipeline more than once, it's best to compile it. |
Mike Klein | af49b19 | 2016-11-15 08:52:04 -0500 | [diff] [blame] | 117 | std::function<void(size_t x, size_t y, size_t n)> compile() const; |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 118 | |
Mike Klein | 3928c6b | 2016-11-15 16:18:38 -0500 | [diff] [blame] | 119 | void dump() const; |
| 120 | |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 121 | struct Stage { |
| 122 | StockStage stage; |
| 123 | void* ctx; |
| 124 | }; |
| 125 | |
Mike Klein | d37d5d9 | 2016-12-14 13:38:24 +0000 | [diff] [blame] | 126 | // Conversion from sRGB can be subtly tricky when premultiplication is involved. |
| 127 | // Use these helpers to keep things sane. |
Mike Klein | 8c8cb5b | 2017-01-06 10:21:56 -0500 | [diff] [blame] | 128 | void append_from_srgb(SkAlphaType); |
Mike Klein | d37d5d9 | 2016-12-14 13:38:24 +0000 | [diff] [blame] | 129 | |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 130 | private: |
Mike Klein | 7f71d88 | 2017-01-06 12:00:31 -0500 | [diff] [blame] | 131 | std::function<void(size_t, size_t, size_t)> jit() const; |
| 132 | |
Mike Klein | cc63173 | 2016-12-06 09:17:55 -0500 | [diff] [blame] | 133 | std::vector<Stage> fStages; |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
mtklein | 281b33f | 2016-07-12 15:01:26 -0700 | [diff] [blame] | 136 | #endif//SkRasterPipeline_DEFINED |