SkRasterPipeline.h 11 KB

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