EdgeFlagPathRasterizer.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (c) 2023, MacDue <macdue@dueutil.tech>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Array.h>
  8. #include <AK/GenericShorthands.h>
  9. #include <AK/Vector.h>
  10. #include <LibGfx/Bitmap.h>
  11. #include <LibGfx/PaintStyle.h>
  12. #include <LibGfx/Painter.h>
  13. #include <LibGfx/Path.h>
  14. namespace Gfx {
  15. namespace Detail {
  16. static auto constexpr coverage_lut = [] {
  17. Array<u8, 256> coverage_lut {};
  18. for (u32 sample = 0; sample <= 255; sample++)
  19. coverage_lut[sample] = popcount(sample);
  20. return coverage_lut;
  21. }();
  22. template<unsigned SamplesPerPixel>
  23. struct Sample {
  24. static_assert(!first_is_one_of(SamplesPerPixel, 8u, 16u, 32u), "EdgeFlagPathRasterizer: Invalid samples per pixel!");
  25. };
  26. // See paper for diagrams for how these offsets work, but they allow for nicely spread out samples in each pixel.
  27. template<>
  28. struct Sample<8> {
  29. using Type = u8;
  30. static constexpr Array nrooks_subpixel_offsets {
  31. (5.0f / 8.0f),
  32. (0.0f / 8.0f),
  33. (3.0f / 8.0f),
  34. (6.0f / 8.0f),
  35. (1.0f / 8.0f),
  36. (4.0f / 8.0f),
  37. (7.0f / 8.0f),
  38. (2.0f / 8.0f),
  39. };
  40. static u8 compute_coverage(Type sample)
  41. {
  42. return coverage_lut[sample];
  43. }
  44. };
  45. template<>
  46. struct Sample<16> {
  47. using Type = u16;
  48. static constexpr Array nrooks_subpixel_offsets {
  49. (1.0f / 16.0f),
  50. (8.0f / 16.0f),
  51. (4.0f / 16.0f),
  52. (15.0f / 16.0f),
  53. (11.0f / 16.0f),
  54. (2.0f / 16.0f),
  55. (6.0f / 16.0f),
  56. (14.0f / 16.0f),
  57. (10.0f / 16.0f),
  58. (3.0f / 16.0f),
  59. (7.0f / 16.0f),
  60. (12.0f / 16.0f),
  61. (0.0f / 16.0f),
  62. (9.0f / 16.0f),
  63. (5.0f / 16.0f),
  64. (13.0f / 16.0f),
  65. };
  66. static u8 compute_coverage(Type sample)
  67. {
  68. return (
  69. coverage_lut[(sample >> 0) & 0xff]
  70. + coverage_lut[(sample >> 8) & 0xff]);
  71. }
  72. };
  73. template<>
  74. struct Sample<32> {
  75. using Type = u32;
  76. static constexpr Array nrooks_subpixel_offsets {
  77. (28.0f / 32.0f),
  78. (13.0f / 32.0f),
  79. (6.0f / 32.0f),
  80. (23.0f / 32.0f),
  81. (0.0f / 32.0f),
  82. (17.0f / 32.0f),
  83. (10.0f / 32.0f),
  84. (27.0f / 32.0f),
  85. (4.0f / 32.0f),
  86. (21.0f / 32.0f),
  87. (14.0f / 32.0f),
  88. (31.0f / 32.0f),
  89. (8.0f / 32.0f),
  90. (25.0f / 32.0f),
  91. (18.0f / 32.0f),
  92. (3.0f / 32.0f),
  93. (12.0f / 32.0f),
  94. (29.0f / 32.0f),
  95. (22.0f / 32.0f),
  96. (7.0f / 32.0f),
  97. (16.0f / 32.0f),
  98. (1.0f / 32.0f),
  99. (26.0f / 32.0f),
  100. (11.0f / 32.0f),
  101. (20.0f / 32.0f),
  102. (5.0f / 32.0f),
  103. (30.0f / 32.0f),
  104. (15.0f / 32.0f),
  105. (24.0f / 32.0f),
  106. (9.0f / 32.0f),
  107. (2.0f / 32.0f),
  108. (19.0f / 32.0f),
  109. };
  110. static u8 compute_coverage(Type sample)
  111. {
  112. return (
  113. coverage_lut[(sample >> 0) & 0xff]
  114. + coverage_lut[(sample >> 8) & 0xff]
  115. + coverage_lut[(sample >> 16) & 0xff]
  116. + coverage_lut[(sample >> 24) & 0xff]);
  117. }
  118. };
  119. struct Edge {
  120. float x;
  121. int min_y;
  122. int max_y;
  123. float dxdy;
  124. i8 winding;
  125. Edge* next_edge;
  126. };
  127. }
  128. template<unsigned SamplesPerPixel = 32>
  129. class EdgeFlagPathRasterizer {
  130. public:
  131. EdgeFlagPathRasterizer(IntSize);
  132. void fill(Painter&, Path const&, Color, Painter::WindingRule, FloatPoint offset = {});
  133. void fill(Painter&, Path const&, PaintStyle const&, float opacity, Painter::WindingRule, FloatPoint offset = {});
  134. private:
  135. using SubpixelSample = Detail::Sample<SamplesPerPixel>;
  136. using SampleType = typename SubpixelSample::Type;
  137. static u8 coverage_to_alpha(u8 coverage)
  138. {
  139. constexpr auto alpha_shift = AK::log2(256 / SamplesPerPixel);
  140. if (!coverage)
  141. return 0;
  142. return (coverage << alpha_shift) - 1;
  143. }
  144. struct EdgeExtent {
  145. int min_x;
  146. int max_x;
  147. template<typename T>
  148. void memset_extent(T* data, int value)
  149. {
  150. if (min_x <= max_x)
  151. memset(data + min_x, value, (max_x - min_x + 1) * sizeof(T));
  152. }
  153. };
  154. void fill_internal(Painter&, Path const&, auto color_or_function, Painter::WindingRule, FloatPoint offset);
  155. Detail::Edge* plot_edges_for_scanline(int scanline, auto plot_edge, EdgeExtent&, Detail::Edge* active_edges = nullptr);
  156. template<Painter::WindingRule>
  157. FLATTEN void write_scanline(Painter&, int scanline, EdgeExtent, auto& color_or_function);
  158. Color scanline_color(int scanline, int offset, u8 alpha, auto& color_or_function);
  159. void write_pixel(BitmapFormat format, ARGB32* scanline_ptr, int scanline, int offset, SampleType sample, auto& color_or_function);
  160. void fast_fill_solid_color_span(ARGB32* scanline_ptr, int start, int end, Color color);
  161. template<Painter::WindingRule, typename Callback>
  162. auto accumulate_scanline(EdgeExtent, auto, Callback);
  163. auto accumulate_even_odd_scanline(EdgeExtent, auto, auto sample_callback);
  164. auto accumulate_non_zero_scanline(EdgeExtent, auto, auto sample_callback);
  165. struct WindingCounts {
  166. // NOTE: This only allows up to 256 winding levels. Increase this if required (i.e. to an i16).
  167. i8 counts[SamplesPerPixel];
  168. };
  169. struct NonZeroAcc {
  170. SampleType sample;
  171. WindingCounts winding;
  172. };
  173. template<Painter::WindingRule WindingRule>
  174. constexpr auto initial_acc() const
  175. {
  176. if constexpr (WindingRule == Painter::WindingRule::EvenOdd)
  177. return SampleType {};
  178. else
  179. return NonZeroAcc {};
  180. }
  181. IntSize m_size;
  182. IntPoint m_blit_origin;
  183. IntRect m_clip;
  184. Vector<SampleType> m_scanline;
  185. Vector<WindingCounts> m_windings;
  186. class EdgeTable {
  187. public:
  188. EdgeTable() = default;
  189. void set_scanline_range(int min_scanline, int max_scanline)
  190. {
  191. this->min_scanline = min_scanline;
  192. edges.resize(max_scanline - min_scanline + 1);
  193. }
  194. auto& operator[](int scanline) { return edges[scanline - min_scanline]; }
  195. private:
  196. Vector<Detail::Edge*> edges;
  197. int min_scanline { 0 };
  198. } m_edge_table;
  199. };
  200. extern template class EdgeFlagPathRasterizer<8>;
  201. extern template class EdgeFlagPathRasterizer<16>;
  202. extern template class EdgeFlagPathRasterizer<32>;
  203. }