SkRasterPipeline.h revision dd19ac7d10c7c00dd6e9b1f4c4c6aae729c7e6d4
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(to_2dot2) \ 65 M(constant_color) M(store_f32) \ 66 M(load_565) M(load_565_d) M(store_565) \ 67 M(load_f16) M(load_f16_d) M(store_f16) \ 68 M(load_8888) M(load_8888_d) M(store_8888) \ 69 M(load_tables) M(store_tables) \ 70 M(scale_u8) M(scale_1_float) \ 71 M(lerp_u8) M(lerp_565) M(lerp_1_float) \ 72 M(dstatop) M(dstin) M(dstout) M(dstover) \ 73 M(srcatop) M(srcin) M(srcout) M(srcover) \ 74 M(clear) M(modulate) M(multiply) M(plus_) M(screen) M(xor_) \ 75 M(colorburn) M(colordodge) M(darken) M(difference) \ 76 M(exclusion) M(hardlight) M(lighten) M(overlay) M(softlight) \ 77 M(luminance_to_alpha) \ 78 M(matrix_2x3) M(matrix_3x4) M(matrix_4x5) \ 79 M(matrix_perspective) \ 80 M(parametric_r) M(parametric_g) M(parametric_b) \ 81 M(table_r) M(table_g) M(table_b) \ 82 M(color_lookup_table) M(lab_to_xyz) \ 83 M(clamp_x) M(mirror_x) M(repeat_x) \ 84 M(clamp_y) M(mirror_y) M(repeat_y) \ 85 M(gather_a8) M(gather_g8) M(gather_i8) \ 86 M(gather_565) M(gather_4444) M(gather_8888) M(gather_f16) \ 87 M(top_left) M(top_right) M(bottom_left) M(bottom_right) \ 88 M(accumulate) 89 90class SkRasterPipeline { 91public: 92 // No pipeline may be more than kMaxStages long. 93 static const int kMaxStages = 48; 94 95 SkRasterPipeline(); 96 97 enum StockStage { 98 #define M(stage) stage, 99 SK_RASTER_PIPELINE_STAGES(M) 100 #undef M 101 }; 102 void append(StockStage, void* = nullptr); 103 void append(StockStage stage, const void* ctx) { this->append(stage, const_cast<void*>(ctx)); } 104 105 // Append all stages to this pipeline. 106 void extend(const SkRasterPipeline&); 107 108 // Runs the pipeline walking x through [x,x+n), holding y constant. 109 void run(size_t x, size_t y, size_t n) const; 110 111 // If you're going to run() the pipeline more than once, it's best to compile it. 112 std::function<void(size_t x, size_t y, size_t n)> compile() const; 113 114 void dump() const; 115 116 struct Stage { 117 StockStage stage; 118 void* ctx; 119 }; 120 121private: 122 int fNum = 0; 123 Stage fStages[kMaxStages]; 124}; 125 126#endif//SkRasterPipeline_DEFINED 127