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 "SkPM4f.h" 15 #include "SkTArray.h" 16 #include "SkTypes.h" 17 #include <functional> 18 #include <vector> 19 20 struct SkJumper_Engine; 21 22 /** 23 * SkRasterPipeline provides a cheap way to chain together a pixel processing pipeline. 24 * 25 * It's particularly designed for situations where the potential pipeline is extremely 26 * combinatoric: {N dst formats} x {M source formats} x {K mask formats} x {C transfer modes} ... 27 * No one wants to write specialized routines for all those combinations, and if we did, we'd 28 * end up bloating our code size dramatically. SkRasterPipeline stages can be chained together 29 * at runtime, so we can scale this problem linearly rather than combinatorically. 30 * 31 * Each stage is represented by a function conforming to a common interface and by an 32 * arbitrary context pointer. The stage funciton arguments and calling convention are 33 * designed to maximize the amount of data we can pass along the pipeline cheaply, and 34 * vary depending on CPU feature detection. 35 * 36 * If you'd like to see how this works internally, you want to start digging around src/jumper. 37 */ 38 39 #define SK_RASTER_PIPELINE_STAGES(M) \ 40 M(callback) \ 41 M(move_src_dst) M(move_dst_src) \ 42 M(clamp_0) M(clamp_1) M(clamp_a) M(clamp_a_dst) \ 43 M(unpremul) M(premul) M(premul_dst) \ 44 M(force_opaque) M(force_opaque_dst) \ 45 M(set_rgb) M(swap_rb) M(invert) \ 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_1010102) M(load_1010102_dst) M(store_1010102) M(gather_1010102) \ 58 M(bilerp_clamp_8888) \ 59 M(load_u16_be) M(load_rgb_u16_be) M(store_u16_be) \ 60 M(load_tables_u16_be) M(load_tables_rgb_u16_be) M(load_tables) \ 61 M(load_rgba) M(store_rgba) \ 62 M(scale_u8) M(scale_565) M(scale_1_float) \ 63 M( lerp_u8) M( lerp_565) M( lerp_1_float) \ 64 M(dstatop) M(dstin) M(dstout) M(dstover) \ 65 M(srcatop) M(srcin) M(srcout) M(srcover) \ 66 M(clear) M(modulate) M(multiply) M(plus_) M(screen) M(xor_) \ 67 M(colorburn) M(colordodge) M(darken) M(difference) \ 68 M(exclusion) M(hardlight) M(lighten) M(overlay) M(softlight) \ 69 M(hue) M(saturation) M(color) M(luminosity) \ 70 M(srcover_rgba_8888) M(srcover_bgra_8888) \ 71 M(luminance_to_alpha) \ 72 M(matrix_translate) M(matrix_scale_translate) \ 73 M(matrix_2x3) M(matrix_3x4) M(matrix_4x5) M(matrix_4x3) \ 74 M(matrix_perspective) \ 75 M(parametric_r) M(parametric_g) M(parametric_b) \ 76 M(parametric_a) M(gamma) M(gamma_dst) \ 77 M(table_r) M(table_g) M(table_b) M(table_a) \ 78 M(lab_to_xyz) \ 79 M(mirror_x) M(repeat_x) \ 80 M(mirror_y) M(repeat_y) \ 81 M(negate_x) \ 82 M(bilinear_nx) M(bilinear_px) M(bilinear_ny) M(bilinear_py) \ 83 M(bicubic_n3x) M(bicubic_n1x) M(bicubic_p1x) M(bicubic_p3x) \ 84 M(bicubic_n3y) M(bicubic_n1y) M(bicubic_p1y) M(bicubic_p3y) \ 85 M(save_xy) M(accumulate) \ 86 M(clamp_x_1) M(mirror_x_1) M(repeat_x_1) \ 87 M(evenly_spaced_gradient) \ 88 M(gradient) \ 89 M(evenly_spaced_2_stop_gradient) \ 90 M(xy_to_unit_angle) \ 91 M(xy_to_radius) \ 92 M(xy_to_2pt_conical_strip) \ 93 M(xy_to_2pt_conical_focal_on_circle) \ 94 M(xy_to_2pt_conical_well_behaved) \ 95 M(xy_to_2pt_conical_smaller) \ 96 M(xy_to_2pt_conical_greater) \ 97 M(alter_2pt_conical_compensate_focal) \ 98 M(alter_2pt_conical_unswap) \ 99 M(mask_2pt_conical_nan) \ 100 M(mask_2pt_conical_degenerates) M(apply_vector_mask) \ 101 M(byte_tables) M(byte_tables_rgb) \ 102 M(rgb_to_hsl) M(hsl_to_rgb) \ 103 M(clut_3D) M(clut_4D) \ 104 M(gauss_a_to_rgba) 105 106 class SkRasterPipeline { 107 public: 108 explicit SkRasterPipeline(SkArenaAlloc*); 109 110 SkRasterPipeline(const SkRasterPipeline&) = delete; 111 SkRasterPipeline(SkRasterPipeline&&) = default; 112 113 SkRasterPipeline& operator=(const SkRasterPipeline&) = delete; 114 SkRasterPipeline& operator=(SkRasterPipeline&&) = default; 115 116 void reset(); 117 118 enum StockStage { 119 #define M(stage) stage, 120 SK_RASTER_PIPELINE_STAGES(M) 121 #undef M 122 }; 123 void append(StockStage, void* = nullptr); append(StockStage stage,const void * ctx)124 void append(StockStage stage, const void* ctx) { this->append(stage, const_cast<void*>(ctx)); } 125 126 // Append all stages to this pipeline. 127 void extend(const SkRasterPipeline&); 128 129 // Runs the pipeline in 2d from (x,y) inclusive to (x+w,y+h) exclusive. 130 void run(size_t x, size_t y, size_t w, size_t h) const; 131 132 // Allocates a thunk which amortizes run() setup cost in alloc. 133 std::function<void(size_t, size_t, size_t, size_t)> compile() const; 134 135 void dump() const; 136 137 // Appends a stage for the specified matrix. 138 // Tries to optimize the stage by analyzing the type of matrix. 139 void append_matrix(SkArenaAlloc*, const SkMatrix&); 140 141 // Appends a stage for a constant uniform color. 142 // Tries to optimize the stage based on the color. 143 void append_constant_color(SkArenaAlloc*, const float rgba[4]); 144 append_constant_color(SkArenaAlloc * alloc,const SkPM4f & color)145 void append_constant_color(SkArenaAlloc* alloc, const SkPM4f& color) { 146 this->append_constant_color(alloc, color.fVec); 147 } append_constant_color(SkArenaAlloc * alloc,const SkColor4f & color)148 void append_constant_color(SkArenaAlloc* alloc, const SkColor4f& color) { 149 this->append_constant_color(alloc, color.vec()); 150 } 151 152 // Helper to append(seed_shader) with the normal {+0.5,+1.5,+2.5,...} argument it expects. 153 void append_seed_shader(); 154 empty()155 bool empty() const { return fStages == nullptr; } 156 157 private: 158 struct StageList { 159 StageList* prev; 160 StockStage stage; 161 void* ctx; 162 }; 163 164 const SkJumper_Engine& build_pipeline(void**) const; 165 void unchecked_append(StockStage, void*); 166 167 SkArenaAlloc* fAlloc; 168 StageList* fStages; 169 int fNumStages; 170 int fSlotsNeeded; 171 }; 172 173 template <size_t bytes> 174 class SkRasterPipeline_ : public SkRasterPipeline { 175 public: SkRasterPipeline_()176 SkRasterPipeline_() 177 : SkRasterPipeline(&fBuiltinAlloc) {} 178 179 private: 180 SkSTArenaAlloc<bytes> fBuiltinAlloc; 181 }; 182 183 184 #endif//SkRasterPipeline_DEFINED 185