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 "include/core/SkColor.h" 12 #include "include/core/SkImageInfo.h" 13 #include "include/core/SkMatrix.h" 14 #include "include/core/SkRefCnt.h" 15 #include "include/core/SkTileMode.h" 16 #include "include/core/SkTypes.h" 17 #include "include/private/SkTArray.h" 18 #include "src/core/SkArenaAlloc.h" 19 #include <functional> 20 #include <vector> // TODO: unused 21 22 class SkData; 23 24 /** 25 * SkRasterPipeline provides a cheap way to chain together a pixel processing pipeline. 26 * 27 * It's particularly designed for situations where the potential pipeline is extremely 28 * combinatoric: {N dst formats} x {M source formats} x {K mask formats} x {C transfer modes} ... 29 * No one wants to write specialized routines for all those combinations, and if we did, we'd 30 * end up bloating our code size dramatically. SkRasterPipeline stages can be chained together 31 * at runtime, so we can scale this problem linearly rather than combinatorically. 32 * 33 * Each stage is represented by a function conforming to a common interface and by an 34 * arbitrary context pointer. The stage funciton arguments and calling convention are 35 * designed to maximize the amount of data we can pass along the pipeline cheaply, and 36 * vary depending on CPU feature detection. 37 */ 38 39 #define SK_RASTER_PIPELINE_STAGES(M) \ 40 M(callback) \ 41 M(move_src_dst) M(move_dst_src) M(swap_src_dst) \ 42 M(clamp_0) M(clamp_1) M(clamp_a) M(clamp_gamut) \ 43 M(unpremul) M(premul) M(premul_dst) \ 44 M(force_opaque) M(force_opaque_dst) \ 45 M(set_rgb) M(unbounded_set_rgb) M(swap_rb) M(swap_rb_dst) \ 46 M(black_color) M(white_color) \ 47 M(uniform_color) M(unbounded_uniform_color) M(uniform_color_dst) \ 48 M(seed_shader) M(dither) \ 49 M(load_a8) M(load_a8_dst) M(store_a8) M(gather_a8) \ 50 M(load_565) M(load_565_dst) M(store_565) M(gather_565) \ 51 M(load_4444) M(load_4444_dst) M(store_4444) M(gather_4444) \ 52 M(load_f16) M(load_f16_dst) M(store_f16) M(gather_f16) \ 53 M(load_af16) M(load_af16_dst) M(store_af16) M(gather_af16) \ 54 M(load_rgf16) M(load_rgf16_dst) M(store_rgf16) M(gather_rgf16) \ 55 M(load_f32) M(load_f32_dst) M(store_f32) M(gather_f32) \ 56 M(load_rgf32) M(store_rgf32) \ 57 M(load_8888) M(load_8888_dst) M(store_8888) M(gather_8888) \ 58 M(load_rg88) M(load_rg88_dst) M(store_rg88) M(gather_rg88) \ 59 M(load_a16) M(load_a16_dst) M(store_a16) M(gather_a16) \ 60 M(load_rg1616) M(load_rg1616_dst) M(store_rg1616) M(gather_rg1616) \ 61 M(load_16161616) M(load_16161616_dst) M(store_16161616) M(gather_16161616) \ 62 M(load_1010102) M(load_1010102_dst) M(store_1010102) M(gather_1010102) \ 63 M(alpha_to_gray) M(alpha_to_gray_dst) \ 64 M(bt709_luminance_or_luma_to_alpha) M(bt709_luminance_or_luma_to_rgb) \ 65 M(bilerp_clamp_8888) M(bicubic_clamp_8888) \ 66 M(store_u16_be) \ 67 M(load_src) M(store_src) M(store_src_a) M(load_dst) M(store_dst) \ 68 M(scale_u8) M(scale_565) M(scale_1_float) M(scale_native) \ 69 M( lerp_u8) M( lerp_565) M( lerp_1_float) M(lerp_native) \ 70 M(dstatop) M(dstin) M(dstout) M(dstover) \ 71 M(srcatop) M(srcin) M(srcout) M(srcover) \ 72 M(clear) M(modulate) M(multiply) M(plus_) M(screen) M(xor_) \ 73 M(colorburn) M(colordodge) M(darken) M(difference) \ 74 M(exclusion) M(hardlight) M(lighten) M(overlay) M(softlight) \ 75 M(hue) M(saturation) M(color) M(luminosity) \ 76 M(srcover_rgba_8888) \ 77 M(matrix_translate) M(matrix_scale_translate) \ 78 M(matrix_2x3) M(matrix_3x3) M(matrix_3x4) M(matrix_4x5) M(matrix_4x3) \ 79 M(matrix_perspective) \ 80 M(parametric) M(gamma_) M(PQish) M(HLGish) M(HLGinvish) \ 81 M(mirror_x) M(repeat_x) \ 82 M(mirror_y) M(repeat_y) \ 83 M(decal_x) M(decal_y) M(decal_x_and_y) \ 84 M(check_decal_mask) \ 85 M(negate_x) \ 86 M(bilinear) M(bicubic) \ 87 M(bilinear_nx) M(bilinear_px) M(bilinear_ny) M(bilinear_py) \ 88 M(bicubic_n3x) M(bicubic_n1x) M(bicubic_p1x) M(bicubic_p3x) \ 89 M(bicubic_n3y) M(bicubic_n1y) M(bicubic_p1y) M(bicubic_p3y) \ 90 M(save_xy) M(accumulate) \ 91 M(clamp_x_1) M(mirror_x_1) M(repeat_x_1) \ 92 M(evenly_spaced_gradient) \ 93 M(gradient) \ 94 M(evenly_spaced_2_stop_gradient) \ 95 M(xy_to_unit_angle) \ 96 M(xy_to_radius) \ 97 M(xy_to_2pt_conical_strip) \ 98 M(xy_to_2pt_conical_focal_on_circle) \ 99 M(xy_to_2pt_conical_well_behaved) \ 100 M(xy_to_2pt_conical_smaller) \ 101 M(xy_to_2pt_conical_greater) \ 102 M(alter_2pt_conical_compensate_focal) \ 103 M(alter_2pt_conical_unswap) \ 104 M(mask_2pt_conical_nan) \ 105 M(mask_2pt_conical_degenerates) M(apply_vector_mask) \ 106 M(byte_tables) \ 107 M(rgb_to_hsl) M(hsl_to_rgb) \ 108 M(gauss_a_to_rgba) \ 109 M(emboss) \ 110 M(swizzle) 111 112 // The largest number of pixels we handle at a time. 113 static const int SkRasterPipeline_kMaxStride = 16; 114 115 // Structs representing the arguments to some common stages. 116 117 struct SkRasterPipeline_MemoryCtx { 118 void* pixels; 119 int stride; 120 }; 121 122 struct SkRasterPipeline_GatherCtx { 123 const void* pixels; 124 int stride; 125 float width; 126 float height; 127 }; 128 129 // State shared by save_xy, accumulate, and bilinear_* / bicubic_*. 130 struct SkRasterPipeline_SamplerCtx { 131 float x[SkRasterPipeline_kMaxStride]; 132 float y[SkRasterPipeline_kMaxStride]; 133 float fx[SkRasterPipeline_kMaxStride]; 134 float fy[SkRasterPipeline_kMaxStride]; 135 float scalex[SkRasterPipeline_kMaxStride]; 136 float scaley[SkRasterPipeline_kMaxStride]; 137 }; 138 139 struct SkRasterPipeline_TileCtx { 140 float scale; 141 float invScale; // cache of 1/scale 142 }; 143 144 struct SkRasterPipeline_DecalTileCtx { 145 uint32_t mask[SkRasterPipeline_kMaxStride]; 146 float limit_x; 147 float limit_y; 148 }; 149 150 struct SkRasterPipeline_SamplerCtx2 : public SkRasterPipeline_GatherCtx { 151 SkColorType ct; 152 SkTileMode tileX, tileY; 153 float invWidth, invHeight; 154 }; 155 156 struct SkRasterPipeline_CallbackCtx { 157 void (*fn)(SkRasterPipeline_CallbackCtx* self, int active_pixels/*<= SkRasterPipeline_kMaxStride*/); 158 159 // When called, fn() will have our active pixels available in rgba. 160 // When fn() returns, the pipeline will read back those active pixels from read_from. 161 float rgba[4*SkRasterPipeline_kMaxStride]; 162 float* read_from = rgba; 163 }; 164 165 struct SkRasterPipeline_GradientCtx { 166 size_t stopCount; 167 float* fs[4]; 168 float* bs[4]; 169 float* ts; 170 bool interpolatedInPremul; 171 }; 172 173 struct SkRasterPipeline_EvenlySpaced2StopGradientCtx { 174 float f[4]; 175 float b[4]; 176 bool interpolatedInPremul; 177 }; 178 179 struct SkRasterPipeline_2PtConicalCtx { 180 uint32_t fMask[SkRasterPipeline_kMaxStride]; 181 float fP0, 182 fP1; 183 }; 184 185 struct SkRasterPipeline_UniformColorCtx { 186 float r,g,b,a; 187 uint16_t rgba[4]; // [0,255] in a 16-bit lane. 188 }; 189 190 struct SkRasterPipeline_EmbossCtx { 191 SkRasterPipeline_MemoryCtx mul, 192 add; 193 }; 194 195 class SkRasterPipeline { 196 public: 197 explicit SkRasterPipeline(SkArenaAlloc*); 198 199 SkRasterPipeline(const SkRasterPipeline&) = delete; 200 SkRasterPipeline(SkRasterPipeline&&) = default; 201 202 SkRasterPipeline& operator=(const SkRasterPipeline&) = delete; 203 SkRasterPipeline& operator=(SkRasterPipeline&&) = default; 204 205 void reset(); 206 207 enum StockStage { 208 #define M(stage) stage, 209 SK_RASTER_PIPELINE_STAGES(M) 210 #undef M 211 }; 212 void append(StockStage, void* = nullptr); append(StockStage stage,const void * ctx)213 void append(StockStage stage, const void* ctx) { this->append(stage, const_cast<void*>(ctx)); } 214 void append(StockStage, uintptr_t ctx); 215 216 // Append all stages to this pipeline. 217 void extend(const SkRasterPipeline&); 218 219 // Runs the pipeline in 2d from (x,y) inclusive to (x+w,y+h) exclusive. 220 void run(size_t x, size_t y, size_t w, size_t h) const; 221 222 // Allocates a thunk which amortizes run() setup cost in alloc. 223 std::function<void(size_t, size_t, size_t, size_t)> compile() const; 224 225 void dump() const; 226 227 // Appends a stage for the specified matrix. 228 // Tries to optimize the stage by analyzing the type of matrix. 229 void append_matrix(SkArenaAlloc*, const SkMatrix&); 230 231 // Appends a stage for a constant uniform color. 232 // Tries to optimize the stage based on the color. 233 void append_constant_color(SkArenaAlloc*, const float rgba[4]); 234 append_constant_color(SkArenaAlloc * alloc,const SkColor4f & color)235 void append_constant_color(SkArenaAlloc* alloc, const SkColor4f& color) { 236 this->append_constant_color(alloc, color.vec()); 237 } 238 239 // Like append_constant_color() but only affecting r,g,b, ignoring the alpha channel. 240 void append_set_rgb(SkArenaAlloc*, const float rgb[3]); 241 append_set_rgb(SkArenaAlloc * alloc,const SkColor4f & color)242 void append_set_rgb(SkArenaAlloc* alloc, const SkColor4f& color) { 243 this->append_set_rgb(alloc, color.vec()); 244 } 245 246 void append_load (SkColorType, const SkRasterPipeline_MemoryCtx*); 247 void append_load_dst(SkColorType, const SkRasterPipeline_MemoryCtx*); 248 void append_store (SkColorType, const SkRasterPipeline_MemoryCtx*); 249 250 void append_gamut_clamp_if_normalized(const SkImageInfo&); 251 252 void append_transfer_function(const skcms_TransferFunction&); 253 empty()254 bool empty() const { return fStages == nullptr; } 255 256 private: 257 struct StageList { 258 StageList* prev; 259 StockStage stage; 260 void* ctx; 261 }; 262 263 using StartPipelineFn = void(*)(size_t,size_t,size_t,size_t, void** program); 264 StartPipelineFn build_pipeline(void**) const; 265 266 void unchecked_append(StockStage, void*); 267 268 // Used by old single-program void** style execution. 269 SkArenaAlloc* fAlloc; 270 StageList* fStages; 271 int fNumStages; 272 int fSlotsNeeded; 273 }; 274 275 template <size_t bytes> 276 class SkRasterPipeline_ : public SkRasterPipeline { 277 public: SkRasterPipeline_()278 SkRasterPipeline_() 279 : SkRasterPipeline(&fBuiltinAlloc) {} 280 281 private: 282 SkSTArenaAlloc<bytes> fBuiltinAlloc; 283 }; 284 285 286 #endif//SkRasterPipeline_DEFINED 287