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 "SkArenaAlloc.h" 12 #include "SkImageInfo.h" 13 #include "SkNx.h" 14 #include "SkTArray.h" 15 #include "SkTypes.h" 16 #include <functional> 17 #include <vector> 18 19 struct SkJumper_constants; 20 struct SkJumper_Engine; 21 struct SkPM4f; 22 23 /** 24 * SkRasterPipeline provides a cheap way to chain together a pixel processing pipeline. 25 * 26 * It's particularly designed for situations where the potential pipeline is extremely 27 * combinatoric: {N dst formats} x {M source formats} x {K mask formats} x {C transfer modes} ... 28 * No one wants to write specialized routines for all those combinations, and if we did, we'd 29 * end up bloating our code size dramatically. SkRasterPipeline stages can be chained together 30 * at runtime, so we can scale this problem linearly rather than combinatorically. 31 * 32 * Each stage is represented by a function conforming to a common interface and by an 33 * arbitrary context pointer. The stage funciton arguments and calling convention are 34 * designed to maximize the amount of data we can pass along the pipeline cheaply, and 35 * vary depending on CPU feature detection. 36 * 37 * If you'd like to see how this works internally, you want to start digging around src/jumper. 38 */ 39 40 #define SK_RASTER_PIPELINE_STAGES(M) \ 41 M(callback) \ 42 M(move_src_dst) M(move_dst_src) \ 43 M(clamp_0) M(clamp_1) M(clamp_a) M(clamp_a_dst) \ 44 M(unpremul) M(premul) M(premul_dst) \ 45 M(set_rgb) M(swap_rb) \ 46 M(from_srgb) M(from_srgb_dst) M(to_srgb) \ 47 M(black_color) M(white_color) M(uniform_color) \ 48 M(seed_shader) M(dither) \ 49 M(load_a8) M(load_a8_dst) M(store_a8) M(gather_a8) \ 50 M(load_g8) M(load_g8_dst) M(gather_g8) \ 51 M(load_565) M(load_565_dst) M(store_565) M(gather_565) \ 52 M(load_4444) M(load_4444_dst) M(store_4444) M(gather_4444) \ 53 M(load_f16) M(load_f16_dst) M(store_f16) M(gather_f16) \ 54 M(load_f32) M(load_f32_dst) M(store_f32) \ 55 M(load_8888) M(load_8888_dst) M(store_8888) M(gather_8888) \ 56 M(load_bgra) M(load_bgra_dst) M(store_bgra) M(gather_bgra) \ 57 M(load_u16_be) M(load_rgb_u16_be) M(store_u16_be) \ 58 M(load_tables_u16_be) M(load_tables_rgb_u16_be) \ 59 M(load_tables) M(load_rgba) M(store_rgba) \ 60 M(scale_u8) M(scale_1_float) \ 61 M(lerp_u8) M(lerp_565) M(lerp_1_float) \ 62 M(dstatop) M(dstin) M(dstout) M(dstover) \ 63 M(srcatop) M(srcin) M(srcout) M(srcover) \ 64 M(clear) M(modulate) M(multiply) M(plus_) M(screen) M(xor_) \ 65 M(colorburn) M(colordodge) M(darken) M(difference) \ 66 M(exclusion) M(hardlight) M(lighten) M(overlay) M(softlight) \ 67 M(hue) M(saturation) M(color) M(luminosity) \ 68 M(srcover_rgba_8888) \ 69 M(luminance_to_alpha) \ 70 M(matrix_translate) M(matrix_scale_translate) \ 71 M(matrix_2x3) M(matrix_3x4) M(matrix_4x5) M(matrix_4x3) \ 72 M(matrix_perspective) \ 73 M(parametric_r) M(parametric_g) M(parametric_b) \ 74 M(parametric_a) \ 75 M(table_r) M(table_g) M(table_b) M(table_a) \ 76 M(lab_to_xyz) \ 77 M(clamp_x) M(mirror_x) M(repeat_x) \ 78 M(clamp_y) M(mirror_y) M(repeat_y) \ 79 M(clamp_x_1) M(mirror_x_1) M(repeat_x_1) \ 80 M(bilinear_nx) M(bilinear_px) M(bilinear_ny) M(bilinear_py) \ 81 M(bicubic_n3x) M(bicubic_n1x) M(bicubic_p1x) M(bicubic_p3x) \ 82 M(bicubic_n3y) M(bicubic_n1y) M(bicubic_p1y) M(bicubic_p3y) \ 83 M(save_xy) M(accumulate) \ 84 M(evenly_spaced_gradient) \ 85 M(gauss_a_to_rgba) M(gradient) \ 86 M(evenly_spaced_2_stop_gradient) \ 87 M(xy_to_unit_angle) \ 88 M(xy_to_radius) \ 89 M(xy_to_2pt_conical_quadratic_min) \ 90 M(xy_to_2pt_conical_quadratic_max) \ 91 M(xy_to_2pt_conical_linear) \ 92 M(mask_2pt_conical_degenerates) M(apply_vector_mask) \ 93 M(byte_tables) M(byte_tables_rgb) \ 94 M(rgb_to_hsl) M(hsl_to_rgb) \ 95 M(store_8888_2d) 96 97 class SkRasterPipeline { 98 public: 99 explicit SkRasterPipeline(SkArenaAlloc*); 100 101 SkRasterPipeline(const SkRasterPipeline&) = delete; 102 SkRasterPipeline(SkRasterPipeline&&) = default; 103 104 SkRasterPipeline& operator=(const SkRasterPipeline&) = delete; 105 SkRasterPipeline& operator=(SkRasterPipeline&&) = default; 106 107 void reset(); 108 109 enum StockStage { 110 #define M(stage) stage, 111 SK_RASTER_PIPELINE_STAGES(M) 112 #undef M 113 }; 114 void append(StockStage, void* = nullptr); append(StockStage stage,const void * ctx)115 void append(StockStage stage, const void* ctx) { this->append(stage, const_cast<void*>(ctx)); } 116 117 // Append all stages to this pipeline. 118 void extend(const SkRasterPipeline&); 119 120 // Runs the pipeline walking x through [x,x+n). 121 void run(size_t x, size_t y, size_t n) const; 122 123 // Runs the pipeline in 2d from (x,y) inclusive to (x+w,y+h) exclusive. 124 void run_2d(size_t x, size_t y, size_t w, size_t h) const; 125 126 // Allocates a thunk which amortizes run() setup cost in alloc. 127 std::function<void(size_t, size_t, size_t)> compile() const; 128 129 void dump() const; 130 131 // Conversion from sRGB can be subtly tricky when premultiplication is involved. 132 // Use these helpers to keep things sane. 133 void append_from_srgb(SkAlphaType); 134 void append_from_srgb_dst(SkAlphaType); 135 136 // Appends a stage for the specified matrix. Tries to optimize the stage by analyzing 137 // the type of matrix. 138 void append_matrix(SkArenaAlloc*, const SkMatrix&); 139 140 // Appends a stage for the uniform color. Tries to optimize the stage based on the color. 141 void append_uniform_color(SkArenaAlloc*, const SkPM4f& color); 142 empty()143 bool empty() const { return fStages == nullptr; } 144 145 private: 146 struct StageList { 147 StageList* prev; 148 StockStage stage; 149 void* ctx; 150 }; 151 152 const SkJumper_Engine& build_pipeline(void**) const; 153 void unchecked_append(StockStage, void*); 154 155 SkArenaAlloc* fAlloc; 156 StageList* fStages; 157 int fNumStages; 158 int fSlotsNeeded; 159 }; 160 161 template <size_t bytes> 162 class SkRasterPipeline_ : public SkRasterPipeline { 163 public: SkRasterPipeline_()164 SkRasterPipeline_() 165 : SkRasterPipeline(&fBuiltinAlloc) {} 166 167 private: 168 SkSTArenaAlloc<bytes> fBuiltinAlloc; 169 }; 170 171 172 #endif//SkRasterPipeline_DEFINED 173