Sk4fGradientBase.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. #include "include/core/SkPaint.h"
  8. #include "src/shaders/gradients/Sk4fGradientBase.h"
  9. #include <functional>
  10. namespace {
  11. Sk4f pack_color(const SkColor4f& c4f, bool premul, const Sk4f& component_scale) {
  12. Sk4f pm4f = premul
  13. ? Sk4f::Load(c4f.premul().vec())
  14. : Sk4f::Load(c4f.vec());
  15. if (premul) {
  16. // If the stops are premul, we clamp them to gamut now.
  17. // If the stops are unpremul, the colors will eventually go through Sk4f_toL32(),
  18. // which ends up clamping to gamut then.
  19. pm4f = Sk4f::Max(0, Sk4f::Min(pm4f, pm4f[3]));
  20. }
  21. return pm4f * component_scale;
  22. }
  23. class IntervalIterator {
  24. public:
  25. IntervalIterator(const SkGradientShaderBase& shader, bool reverse)
  26. : fShader(shader)
  27. , fFirstPos(reverse ? SK_Scalar1 : 0)
  28. , fBegin(reverse ? shader.fColorCount - 1 : 0)
  29. , fAdvance(reverse ? -1 : 1) {
  30. SkASSERT(shader.fColorCount > 0);
  31. }
  32. void iterate(const SkColor4f* colors,
  33. std::function<void(const SkColor4f&, const SkColor4f&,
  34. SkScalar, SkScalar)> func) const {
  35. if (!fShader.fOrigPos) {
  36. this->iterateImplicitPos(colors, func);
  37. return;
  38. }
  39. const int end = fBegin + fAdvance * (fShader.fColorCount - 1);
  40. int prev = fBegin;
  41. SkScalar prevPos = fFirstPos;
  42. do {
  43. const int curr = prev + fAdvance;
  44. SkASSERT(curr >= 0 && curr < fShader.fColorCount);
  45. const SkScalar currPos = fShader.fOrigPos[curr];
  46. if (currPos != prevPos) {
  47. SkASSERT((currPos - prevPos > 0) == (fAdvance > 0));
  48. func(colors[prev], colors[curr], prevPos, currPos);
  49. }
  50. prev = curr;
  51. prevPos = currPos;
  52. } while (prev != end);
  53. }
  54. private:
  55. void iterateImplicitPos(const SkColor4f* colors,
  56. std::function<void(const SkColor4f&, const SkColor4f&,
  57. SkScalar, SkScalar)> func) const {
  58. // When clients don't provide explicit color stop positions (fPos == nullptr),
  59. // the color stops are distributed evenly across the unit interval
  60. // (implicit positioning).
  61. const SkScalar dt = fAdvance * SK_Scalar1 / (fShader.fColorCount - 1);
  62. const int end = fBegin + fAdvance * (fShader.fColorCount - 2);
  63. int prev = fBegin;
  64. SkScalar prevPos = fFirstPos;
  65. while (prev != end) {
  66. const int curr = prev + fAdvance;
  67. SkASSERT(curr >= 0 && curr < fShader.fColorCount);
  68. const SkScalar currPos = prevPos + dt;
  69. func(colors[prev], colors[curr], prevPos, currPos);
  70. prev = curr;
  71. prevPos = currPos;
  72. }
  73. // emit the last interval with a pinned end position, to avoid precision issues
  74. func(colors[prev], colors[prev + fAdvance], prevPos, 1 - fFirstPos);
  75. }
  76. const SkGradientShaderBase& fShader;
  77. const SkScalar fFirstPos;
  78. const int fBegin;
  79. const int fAdvance;
  80. };
  81. void addMirrorIntervals(const SkGradientShaderBase& shader,
  82. const SkColor4f* colors,
  83. const Sk4f& componentScale,
  84. bool premulColors, bool reverse,
  85. Sk4fGradientIntervalBuffer::BufferType* buffer) {
  86. const IntervalIterator iter(shader, reverse);
  87. iter.iterate(colors, [&] (const SkColor4f& c0, const SkColor4f& c1, SkScalar t0, SkScalar t1) {
  88. SkASSERT(buffer->empty() || buffer->back().fT1 == 2 - t0);
  89. const auto mirror_t0 = 2 - t0;
  90. const auto mirror_t1 = 2 - t1;
  91. // mirror_p1 & mirror_p1 may collapse for very small values - recheck to avoid
  92. // triggering Interval asserts.
  93. if (mirror_t0 != mirror_t1) {
  94. buffer->emplace_back(pack_color(c0, premulColors, componentScale), mirror_t0,
  95. pack_color(c1, premulColors, componentScale), mirror_t1);
  96. }
  97. });
  98. }
  99. } // anonymous namespace
  100. Sk4fGradientInterval::Sk4fGradientInterval(const Sk4f& c0, SkScalar t0,
  101. const Sk4f& c1, SkScalar t1)
  102. : fT0(t0)
  103. , fT1(t1) {
  104. SkASSERT(t0 != t1);
  105. // Either p0 or p1 can be (-)inf for synthetic clamp edge intervals.
  106. SkASSERT(SkScalarIsFinite(t0) || SkScalarIsFinite(t1));
  107. const auto dt = t1 - t0;
  108. // Clamp edge intervals are always zero-ramp.
  109. SkASSERT(SkScalarIsFinite(dt) || (c0 == c1).allTrue());
  110. SkASSERT(SkScalarIsFinite(t0) || (c0 == c1).allTrue());
  111. const Sk4f dc = SkScalarIsFinite(dt) ? (c1 - c0) / dt : 0;
  112. const Sk4f bias = c0 - (SkScalarIsFinite(t0) ? t0 * dc : 0);
  113. bias.store(fCb.vec());
  114. dc.store(fCg.vec());
  115. }
  116. void Sk4fGradientIntervalBuffer::init(const SkGradientShaderBase& shader, SkColorSpace* dstCS,
  117. SkTileMode tileMode, bool premulColors,
  118. SkScalar alpha, bool reverse) {
  119. // The main job here is to build a specialized interval list: a different
  120. // representation of the color stops data, optimized for efficient scan line
  121. // access during shading.
  122. //
  123. // [{P0,C0} , {P1,C1}) [{P1,C2} , {P2,c3}) ... [{Pn,C2n} , {Pn+1,C2n+1})
  124. //
  125. // The list may be inverted when requested (such that e.g. points are sorted
  126. // in increasing x order when dx < 0).
  127. //
  128. // Note: the current representation duplicates pos data; we could refactor to
  129. // avoid this if interval storage size becomes a concern.
  130. //
  131. // Aside from reordering, we also perform two more pre-processing steps at
  132. // this stage:
  133. //
  134. // 1) scale the color components depending on paint alpha and the requested
  135. // interpolation space (note: the interval color storage is SkPMColor4f, but
  136. // that doesn't necessarily mean the colors are premultiplied; that
  137. // property is tracked in fColorsArePremul)
  138. //
  139. // 2) inject synthetic intervals to support tiling.
  140. //
  141. // * for kRepeat, no extra intervals are needed - the iterator just
  142. // wraps around at the end:
  143. //
  144. // ->[P0,P1)->..[Pn-1,Pn)->
  145. //
  146. // * for kClamp, we add two "infinite" intervals before/after:
  147. //
  148. // [-/+inf , P0)->[P0 , P1)->..[Pn-1 , Pn)->[Pn , +/-inf)
  149. //
  150. // (the iterator should never run off the end in this mode)
  151. //
  152. // * for kMirror, we extend the range to [0..2] and add a flipped
  153. // interval series - then the iterator operates just as in the
  154. // kRepeat case:
  155. //
  156. // ->[P0,P1)->..[Pn-1,Pn)->[2 - Pn,2 - Pn-1)->..[2 - P1,2 - P0)->
  157. //
  158. // TODO: investigate collapsing intervals << 1px.
  159. const auto count = shader.fColorCount;
  160. SkASSERT(count > 0);
  161. fIntervals.reset();
  162. const Sk4f componentScale = premulColors
  163. ? Sk4f(alpha)
  164. : Sk4f(1.0f, 1.0f, 1.0f, alpha);
  165. const int first_index = reverse ? count - 1 : 0;
  166. const int last_index = count - 1 - first_index;
  167. const SkScalar first_pos = reverse ? SK_Scalar1 : 0;
  168. const SkScalar last_pos = SK_Scalar1 - first_pos;
  169. // Transform all of the colors to destination color space
  170. SkColor4fXformer xformedColors(shader.fOrigColors4f, count, shader.fColorSpace.get(), dstCS);
  171. if (tileMode == SkTileMode::kClamp) {
  172. // synthetic edge interval: -/+inf .. P0
  173. const Sk4f clamp_color = pack_color(xformedColors.fColors[first_index],
  174. premulColors, componentScale);
  175. const SkScalar clamp_pos = reverse ? SK_ScalarInfinity : SK_ScalarNegativeInfinity;
  176. fIntervals.emplace_back(clamp_color, clamp_pos,
  177. clamp_color, first_pos);
  178. } else if (tileMode == SkTileMode::kMirror && reverse) {
  179. // synthetic mirror intervals injected before main intervals: (2 .. 1]
  180. addMirrorIntervals(shader, xformedColors.fColors, componentScale, premulColors, false,
  181. &fIntervals);
  182. }
  183. const IntervalIterator iter(shader, reverse);
  184. iter.iterate(xformedColors.fColors,
  185. [&] (const SkColor4f& c0, const SkColor4f& c1, SkScalar t0, SkScalar t1) {
  186. SkASSERT(fIntervals.empty() || fIntervals.back().fT1 == t0);
  187. fIntervals.emplace_back(pack_color(c0, premulColors, componentScale), t0,
  188. pack_color(c1, premulColors, componentScale), t1);
  189. });
  190. if (tileMode == SkTileMode::kClamp) {
  191. // synthetic edge interval: Pn .. +/-inf
  192. const Sk4f clamp_color = pack_color(xformedColors.fColors[last_index],
  193. premulColors, componentScale);
  194. const SkScalar clamp_pos = reverse ? SK_ScalarNegativeInfinity : SK_ScalarInfinity;
  195. fIntervals.emplace_back(clamp_color, last_pos,
  196. clamp_color, clamp_pos);
  197. } else if (tileMode == SkTileMode::kMirror && !reverse) {
  198. // synthetic mirror intervals injected after main intervals: [1 .. 2)
  199. addMirrorIntervals(shader, xformedColors.fColors, componentScale, premulColors, true,
  200. &fIntervals);
  201. }
  202. }
  203. const Sk4fGradientInterval* Sk4fGradientIntervalBuffer::find(SkScalar t) const {
  204. // Binary search.
  205. const auto* i0 = fIntervals.begin();
  206. const auto* i1 = fIntervals.end() - 1;
  207. while (i0 != i1) {
  208. SkASSERT(i0 < i1);
  209. SkASSERT(t >= i0->fT0 && t <= i1->fT1);
  210. const auto* i = i0 + ((i1 - i0) >> 1);
  211. if (t > i->fT1) {
  212. i0 = i + 1;
  213. } else {
  214. i1 = i;
  215. }
  216. }
  217. SkASSERT(i0->contains(t));
  218. return i0;
  219. }
  220. const Sk4fGradientInterval* Sk4fGradientIntervalBuffer::findNext(
  221. SkScalar t, const Sk4fGradientInterval* prev, bool increasing) const {
  222. SkASSERT(!prev->contains(t));
  223. SkASSERT(prev >= fIntervals.begin() && prev < fIntervals.end());
  224. SkASSERT(t >= fIntervals.front().fT0 && t <= fIntervals.back().fT1);
  225. const auto* i = prev;
  226. // Use the |increasing| signal to figure which direction we should search for
  227. // the next interval, then perform a linear search.
  228. if (increasing) {
  229. do {
  230. i += 1;
  231. if (i >= fIntervals.end()) {
  232. i = fIntervals.begin();
  233. }
  234. } while (!i->contains(t));
  235. } else {
  236. do {
  237. i -= 1;
  238. if (i < fIntervals.begin()) {
  239. i = fIntervals.end() - 1;
  240. }
  241. } while (!i->contains(t));
  242. }
  243. return i;
  244. }
  245. SkGradientShaderBase::
  246. GradientShaderBase4fContext::GradientShaderBase4fContext(const SkGradientShaderBase& shader,
  247. const ContextRec& rec)
  248. : INHERITED(shader, rec)
  249. , fFlags(this->INHERITED::getFlags())
  250. , fDither(rec.fPaint->isDither())
  251. {
  252. const SkMatrix& inverse = this->getTotalInverse();
  253. fDstToPos.setConcat(shader.fPtsToUnit, inverse);
  254. SkASSERT(!fDstToPos.hasPerspective());
  255. fDstToPosProc = SkMatrixPriv::GetMapXYProc(fDstToPos);
  256. if (shader.fColorsAreOpaque && this->getPaintAlpha() == SK_AlphaOPAQUE) {
  257. fFlags |= kOpaqueAlpha_Flag;
  258. }
  259. fColorsArePremul =
  260. (shader.fGradFlags & SkGradientShader::kInterpolateColorsInPremul_Flag)
  261. || shader.fColorsAreOpaque;
  262. }
  263. bool SkGradientShaderBase::
  264. GradientShaderBase4fContext::isValid() const {
  265. return fDstToPos.isFinite();
  266. }