Sk4px.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright 2015 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 Sk4px_DEFINED
  8. #define Sk4px_DEFINED
  9. #include "include/core/SkColor.h"
  10. #include "include/private/SkColorData.h"
  11. #include "include/private/SkNx.h"
  12. // This file may be included multiple times by .cpp files with different flags, leading
  13. // to different definitions. Usually that doesn't matter because it's all inlined, but
  14. // in Debug modes the compilers may not inline everything. So wrap everything in an
  15. // anonymous namespace to give each includer their own silo of this code (or the linker
  16. // will probably pick one randomly for us, which is rarely correct).
  17. namespace { // NOLINT(google-build-namespaces)
  18. // 1, 2 or 4 SkPMColors, generally vectorized.
  19. class Sk4px : public Sk16b {
  20. public:
  21. Sk4px(const Sk16b& v) : INHERITED(v) {}
  22. static Sk4px DupPMColor(SkPMColor c) {
  23. Sk4u splat(c);
  24. Sk4px v;
  25. memcpy(&v, &splat, 16);
  26. return v;
  27. }
  28. Sk4px alphas() const; // ARGB argb XYZW xyzw -> AAAA aaaa XXXX xxxx
  29. Sk4px inv() const { return Sk16b(255) - *this; }
  30. // When loading or storing fewer than 4 SkPMColors, we use the low lanes.
  31. static Sk4px Load4(const SkPMColor px[4]) {
  32. Sk4px v;
  33. memcpy(&v, px, 16);
  34. return v;
  35. }
  36. static Sk4px Load2(const SkPMColor px[2]) {
  37. Sk4px v;
  38. memcpy(&v, px, 8);
  39. return v;
  40. }
  41. static Sk4px Load1(const SkPMColor px[1]) {
  42. Sk4px v;
  43. memcpy(&v, px, 4);
  44. return v;
  45. }
  46. // Ditto for Alphas... Load2Alphas fills the low two lanes of Sk4px.
  47. static Sk4px Load4Alphas(const SkAlpha[4]); // AaXx -> AAAA aaaa XXXX xxxx
  48. static Sk4px Load2Alphas(const SkAlpha[2]); // Aa -> AAAA aaaa ???? ????
  49. void store4(SkPMColor px[4]) const { memcpy(px, this, 16); }
  50. void store2(SkPMColor px[2]) const { memcpy(px, this, 8); }
  51. void store1(SkPMColor px[1]) const { memcpy(px, this, 4); }
  52. // 1, 2, or 4 SkPMColors with 16-bit components.
  53. // This is most useful as the result of a multiply, e.g. from mulWiden().
  54. class Wide : public Sk16h {
  55. public:
  56. Wide(const Sk16h& v) : Sk16h(v) {}
  57. // Add, then pack the top byte of each component back down into 4 SkPMColors.
  58. Sk4px addNarrowHi(const Sk16h&) const;
  59. // Rounds, i.e. (x+127) / 255.
  60. Sk4px div255() const;
  61. // These just keep the types as Wide so the user doesn't have to keep casting.
  62. Wide operator * (const Wide& o) const { return INHERITED::operator*(o); }
  63. Wide operator + (const Wide& o) const { return INHERITED::operator+(o); }
  64. Wide operator - (const Wide& o) const { return INHERITED::operator-(o); }
  65. Wide operator >> (int bits) const { return INHERITED::operator>>(bits); }
  66. Wide operator << (int bits) const { return INHERITED::operator<<(bits); }
  67. private:
  68. typedef Sk16h INHERITED;
  69. };
  70. Wide widen() const; // Widen 8-bit values to low 8-bits of 16-bit lanes.
  71. Wide mulWiden(const Sk16b&) const; // 8-bit x 8-bit -> 16-bit components.
  72. // The only 8-bit multiply we use is 8-bit x 8-bit -> 16-bit. Might as well make it pithy.
  73. Wide operator * (const Sk4px& o) const { return this->mulWiden(o); }
  74. // These just keep the types as Sk4px so the user doesn't have to keep casting.
  75. Sk4px operator + (const Sk4px& o) const { return INHERITED::operator+(o); }
  76. Sk4px operator - (const Sk4px& o) const { return INHERITED::operator-(o); }
  77. Sk4px operator < (const Sk4px& o) const { return INHERITED::operator<(o); }
  78. Sk4px thenElse(const Sk4px& t, const Sk4px& e) const { return INHERITED::thenElse(t,e); }
  79. // Generally faster than (*this * o).div255().
  80. // May be incorrect by +-1, but is always exactly correct when *this or o is 0 or 255.
  81. Sk4px approxMulDiv255(const Sk16b& o) const {
  82. // (x*y + x) / 256 meets these criteria. (As of course does (x*y + y) / 256 by symmetry.)
  83. // FYI: (x*y + 255) / 256 also meets these criteria. In my brief testing, it was slower.
  84. return this->widen().addNarrowHi(*this * o);
  85. }
  86. // A generic driver that maps fn over a src array into a dst array.
  87. // fn should take an Sk4px (4 src pixels) and return an Sk4px (4 dst pixels).
  88. template <typename Fn>
  89. static void MapSrc(int n, SkPMColor* dst, const SkPMColor* src, const Fn& fn) {
  90. SkASSERT(dst);
  91. SkASSERT(src);
  92. // This looks a bit odd, but it helps loop-invariant hoisting across different calls to fn.
  93. // Basically, we need to make sure we keep things inside a single loop.
  94. while (n > 0) {
  95. if (n >= 8) {
  96. Sk4px dst0 = fn(Load4(src+0)),
  97. dst4 = fn(Load4(src+4));
  98. dst0.store4(dst+0);
  99. dst4.store4(dst+4);
  100. dst += 8; src += 8; n -= 8;
  101. continue; // Keep our stride at 8 pixels as long as possible.
  102. }
  103. SkASSERT(n <= 7);
  104. if (n >= 4) {
  105. fn(Load4(src)).store4(dst);
  106. dst += 4; src += 4; n -= 4;
  107. }
  108. if (n >= 2) {
  109. fn(Load2(src)).store2(dst);
  110. dst += 2; src += 2; n -= 2;
  111. }
  112. if (n >= 1) {
  113. fn(Load1(src)).store1(dst);
  114. }
  115. break;
  116. }
  117. }
  118. // As above, but with dst4' = fn(dst4, src4).
  119. template <typename Fn>
  120. static void MapDstSrc(int n, SkPMColor* dst, const SkPMColor* src, const Fn& fn) {
  121. SkASSERT(dst);
  122. SkASSERT(src);
  123. while (n > 0) {
  124. if (n >= 8) {
  125. Sk4px dst0 = fn(Load4(dst+0), Load4(src+0)),
  126. dst4 = fn(Load4(dst+4), Load4(src+4));
  127. dst0.store4(dst+0);
  128. dst4.store4(dst+4);
  129. dst += 8; src += 8; n -= 8;
  130. continue; // Keep our stride at 8 pixels as long as possible.
  131. }
  132. SkASSERT(n <= 7);
  133. if (n >= 4) {
  134. fn(Load4(dst), Load4(src)).store4(dst);
  135. dst += 4; src += 4; n -= 4;
  136. }
  137. if (n >= 2) {
  138. fn(Load2(dst), Load2(src)).store2(dst);
  139. dst += 2; src += 2; n -= 2;
  140. }
  141. if (n >= 1) {
  142. fn(Load1(dst), Load1(src)).store1(dst);
  143. }
  144. break;
  145. }
  146. }
  147. // As above, but with dst4' = fn(dst4, alpha4).
  148. template <typename Fn>
  149. static void MapDstAlpha(int n, SkPMColor* dst, const SkAlpha* a, const Fn& fn) {
  150. SkASSERT(dst);
  151. SkASSERT(a);
  152. while (n > 0) {
  153. if (n >= 8) {
  154. Sk4px dst0 = fn(Load4(dst+0), Load4Alphas(a+0)),
  155. dst4 = fn(Load4(dst+4), Load4Alphas(a+4));
  156. dst0.store4(dst+0);
  157. dst4.store4(dst+4);
  158. dst += 8; a += 8; n -= 8;
  159. continue; // Keep our stride at 8 pixels as long as possible.
  160. }
  161. SkASSERT(n <= 7);
  162. if (n >= 4) {
  163. fn(Load4(dst), Load4Alphas(a)).store4(dst);
  164. dst += 4; a += 4; n -= 4;
  165. }
  166. if (n >= 2) {
  167. fn(Load2(dst), Load2Alphas(a)).store2(dst);
  168. dst += 2; a += 2; n -= 2;
  169. }
  170. if (n >= 1) {
  171. fn(Load1(dst), Sk16b(*a)).store1(dst);
  172. }
  173. break;
  174. }
  175. }
  176. // As above, but with dst4' = fn(dst4, src4, alpha4).
  177. template <typename Fn>
  178. static void MapDstSrcAlpha(int n, SkPMColor* dst, const SkPMColor* src, const SkAlpha* a,
  179. const Fn& fn) {
  180. SkASSERT(dst);
  181. SkASSERT(src);
  182. SkASSERT(a);
  183. while (n > 0) {
  184. if (n >= 8) {
  185. Sk4px dst0 = fn(Load4(dst+0), Load4(src+0), Load4Alphas(a+0)),
  186. dst4 = fn(Load4(dst+4), Load4(src+4), Load4Alphas(a+4));
  187. dst0.store4(dst+0);
  188. dst4.store4(dst+4);
  189. dst += 8; src += 8; a += 8; n -= 8;
  190. continue; // Keep our stride at 8 pixels as long as possible.
  191. }
  192. SkASSERT(n <= 7);
  193. if (n >= 4) {
  194. fn(Load4(dst), Load4(src), Load4Alphas(a)).store4(dst);
  195. dst += 4; src += 4; a += 4; n -= 4;
  196. }
  197. if (n >= 2) {
  198. fn(Load2(dst), Load2(src), Load2Alphas(a)).store2(dst);
  199. dst += 2; src += 2; a += 2; n -= 2;
  200. }
  201. if (n >= 1) {
  202. fn(Load1(dst), Load1(src), Sk16b(*a)).store1(dst);
  203. }
  204. break;
  205. }
  206. }
  207. private:
  208. Sk4px() = default;
  209. typedef Sk16b INHERITED;
  210. };
  211. } // namespace
  212. #ifdef SKNX_NO_SIMD
  213. #include "src/opts/Sk4px_none.h"
  214. #else
  215. #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2
  216. #include "src/opts/Sk4px_SSE2.h"
  217. #elif defined(SK_ARM_HAS_NEON)
  218. #include "src/opts/Sk4px_NEON.h"
  219. #else
  220. #include "src/opts/Sk4px_none.h"
  221. #endif
  222. #endif
  223. #endif//Sk4px_DEFINED