blob: 6e2fbe586edaf78a6fc0e4520856f50409f57196 [file] [log] [blame]
rileya@google.com589708b2012-07-26 20:04:23 +00001/*
2 * Copyright 2006 The Android Open Source Project
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
Herb Derby4de13042017-05-15 10:49:39 -04008#include <algorithm>
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/core/SkMallocPixelRef.h"
10#include "include/private/SkFloatBits.h"
11#include "include/private/SkHalf.h"
Mike Klein8aa0edf2020-10-16 11:04:18 -050012#include "include/private/SkTPin.h"
Mike Klein85754d52020-01-22 10:04:11 -060013#include "include/private/SkVx.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/core/SkColorSpacePriv.h"
15#include "src/core/SkConvertPixels.h"
Brian Osman9aaec362020-05-08 14:54:37 -040016#include "src/core/SkMatrixProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/core/SkReadBuffer.h"
Mike Klein85754d52020-01-22 10:04:11 -060018#include "src/core/SkVM.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/core/SkWriteBuffer.h"
20#include "src/shaders/gradients/Sk4fLinearGradient.h"
21#include "src/shaders/gradients/SkGradientShaderPriv.h"
22#include "src/shaders/gradients/SkLinearGradient.h"
23#include "src/shaders/gradients/SkRadialGradient.h"
24#include "src/shaders/gradients/SkSweepGradient.h"
25#include "src/shaders/gradients/SkTwoPointConicalGradient.h"
rileya@google.com589708b2012-07-26 20:04:23 +000026
brianosmane25d71c2016-09-28 11:27:28 -070027enum GradientSerializationFlags {
28 // Bits 29:31 used for various boolean flags
29 kHasPosition_GSF = 0x80000000,
30 kHasLocalMatrix_GSF = 0x40000000,
31 kHasColorSpace_GSF = 0x20000000,
32
33 // Bits 12:28 unused
34
35 // Bits 8:11 for fTileMode
36 kTileModeShift_GSF = 8,
37 kTileModeMask_GSF = 0xF,
38
39 // Bits 0:7 for fGradFlags (note that kForce4fContext_PrivateFlag is 0x80)
40 kGradFlagsShift_GSF = 0,
41 kGradFlagsMask_GSF = 0xFF,
42};
43
reed9fa60da2014-08-21 07:59:51 -070044void SkGradientShaderBase::Descriptor::flatten(SkWriteBuffer& buffer) const {
brianosmane25d71c2016-09-28 11:27:28 -070045 uint32_t flags = 0;
reed9fa60da2014-08-21 07:59:51 -070046 if (fPos) {
brianosmane25d71c2016-09-28 11:27:28 -070047 flags |= kHasPosition_GSF;
reed9fa60da2014-08-21 07:59:51 -070048 }
reed9fa60da2014-08-21 07:59:51 -070049 if (fLocalMatrix) {
brianosmane25d71c2016-09-28 11:27:28 -070050 flags |= kHasLocalMatrix_GSF;
51 }
52 sk_sp<SkData> colorSpaceData = fColorSpace ? fColorSpace->serialize() : nullptr;
53 if (colorSpaceData) {
54 flags |= kHasColorSpace_GSF;
55 }
56 SkASSERT(static_cast<uint32_t>(fTileMode) <= kTileModeMask_GSF);
Mike Reedfae8fce2019-04-03 10:27:45 -040057 flags |= ((unsigned)fTileMode << kTileModeShift_GSF);
brianosmane25d71c2016-09-28 11:27:28 -070058 SkASSERT(fGradFlags <= kGradFlagsMask_GSF);
59 flags |= (fGradFlags << kGradFlagsShift_GSF);
60
61 buffer.writeUInt(flags);
62
63 buffer.writeColor4fArray(fColors, fCount);
64 if (colorSpaceData) {
65 buffer.writeDataAsByteArray(colorSpaceData.get());
66 }
67 if (fPos) {
68 buffer.writeScalarArray(fPos, fCount);
69 }
70 if (fLocalMatrix) {
reed9fa60da2014-08-21 07:59:51 -070071 buffer.writeMatrix(*fLocalMatrix);
reed9fa60da2014-08-21 07:59:51 -070072 }
73}
74
Florin Malitaf77db112018-05-10 09:52:27 -040075template <int N, typename T, bool MEM_MOVE>
76static bool validate_array(SkReadBuffer& buffer, size_t count, SkSTArray<N, T, MEM_MOVE>* array) {
Kevin Lubickdaebae92018-05-17 11:29:10 -040077 if (!buffer.validateCanReadN<T>(count)) {
Florin Malitaf77db112018-05-10 09:52:27 -040078 return false;
79 }
80
81 array->resize_back(count);
82 return true;
83}
84
reed9fa60da2014-08-21 07:59:51 -070085bool SkGradientShaderBase::DescriptorScope::unflatten(SkReadBuffer& buffer) {
Mike Reed70bc94f2017-06-08 12:45:52 -040086 // New gradient format. Includes floating point color, color space, densely packed flags
87 uint32_t flags = buffer.readUInt();
reed9fa60da2014-08-21 07:59:51 -070088
Mike Reedfae8fce2019-04-03 10:27:45 -040089 fTileMode = (SkTileMode)((flags >> kTileModeShift_GSF) & kTileModeMask_GSF);
Mike Reed70bc94f2017-06-08 12:45:52 -040090 fGradFlags = (flags >> kGradFlagsShift_GSF) & kGradFlagsMask_GSF;
reed9fa60da2014-08-21 07:59:51 -070091
Mike Reed70bc94f2017-06-08 12:45:52 -040092 fCount = buffer.getArrayCount();
Florin Malitaf77db112018-05-10 09:52:27 -040093
94 if (!(validate_array(buffer, fCount, &fColorStorage) &&
95 buffer.readColor4fArray(fColorStorage.begin(), fCount))) {
Mike Reed70bc94f2017-06-08 12:45:52 -040096 return false;
97 }
Florin Malitaf77db112018-05-10 09:52:27 -040098 fColors = fColorStorage.begin();
99
Mike Reed70bc94f2017-06-08 12:45:52 -0400100 if (SkToBool(flags & kHasColorSpace_GSF)) {
101 sk_sp<SkData> data = buffer.readByteArrayAsData();
Florin Malitac2ea3272018-05-10 09:41:38 -0400102 fColorSpace = data ? SkColorSpace::Deserialize(data->data(), data->size()) : nullptr;
Mike Reed70bc94f2017-06-08 12:45:52 -0400103 } else {
brianosmane25d71c2016-09-28 11:27:28 -0700104 fColorSpace = nullptr;
Mike Reed70bc94f2017-06-08 12:45:52 -0400105 }
106 if (SkToBool(flags & kHasPosition_GSF)) {
Florin Malitaf77db112018-05-10 09:52:27 -0400107 if (!(validate_array(buffer, fCount, &fPosStorage) &&
108 buffer.readScalarArray(fPosStorage.begin(), fCount))) {
Mike Reed70bc94f2017-06-08 12:45:52 -0400109 return false;
brianosmane25d71c2016-09-28 11:27:28 -0700110 }
Florin Malitaf77db112018-05-10 09:52:27 -0400111 fPos = fPosStorage.begin();
reed9fa60da2014-08-21 07:59:51 -0700112 } else {
Mike Reed70bc94f2017-06-08 12:45:52 -0400113 fPos = nullptr;
114 }
115 if (SkToBool(flags & kHasLocalMatrix_GSF)) {
116 fLocalMatrix = &fLocalMatrixStorage;
117 buffer.readMatrix(&fLocalMatrixStorage);
118 } else {
119 fLocalMatrix = nullptr;
reed9fa60da2014-08-21 07:59:51 -0700120 }
121 return buffer.isValid();
122}
123
124////////////////////////////////////////////////////////////////////////////////////////////
125
mtkleincc695fe2014-12-10 10:29:19 -0800126SkGradientShaderBase::SkGradientShaderBase(const Descriptor& desc, const SkMatrix& ptsToUnit)
reedaddf2ed2014-08-11 08:28:24 -0700127 : INHERITED(desc.fLocalMatrix)
mtkleincc695fe2014-12-10 10:29:19 -0800128 , fPtsToUnit(ptsToUnit)
Brian Osman6667fb12018-07-03 16:44:02 -0400129 , fColorSpace(desc.fColorSpace ? desc.fColorSpace : SkColorSpace::MakeSRGB())
Florin Malita39d71de2017-10-31 11:33:49 -0400130 , fColorsAreOpaque(true)
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000131{
mtkleincc695fe2014-12-10 10:29:19 -0800132 fPtsToUnit.getType(); // Precache so reads are threadsafe.
reed@google.com437d6eb2013-05-23 19:03:05 +0000133 SkASSERT(desc.fCount > 1);
rileya@google.com589708b2012-07-26 20:04:23 +0000134
fmalita6d7e4e82016-09-20 06:55:16 -0700135 fGradFlags = static_cast<uint8_t>(desc.fGradFlags);
rileya@google.com589708b2012-07-26 20:04:23 +0000136
Mike Reedfae8fce2019-04-03 10:27:45 -0400137 SkASSERT((unsigned)desc.fTileMode < kSkTileModeCount);
reed@google.com437d6eb2013-05-23 19:03:05 +0000138 fTileMode = desc.fTileMode;
rileya@google.com589708b2012-07-26 20:04:23 +0000139
rileya@google.com589708b2012-07-26 20:04:23 +0000140 /* Note: we let the caller skip the first and/or last position.
141 i.e. pos[0] = 0.3, pos[1] = 0.7
142 In these cases, we insert dummy entries to ensure that the final data
143 will be bracketed by [0, 1].
144 i.e. our_pos[0] = 0, our_pos[1] = 0.3, our_pos[2] = 0.7, our_pos[3] = 1
145
146 Thus colorCount (the caller's value, and fColorCount (our value) may
147 differ by up to 2. In the above example:
148 colorCount = 2
149 fColorCount = 4
150 */
reed@google.com437d6eb2013-05-23 19:03:05 +0000151 fColorCount = desc.fCount;
rileya@google.com589708b2012-07-26 20:04:23 +0000152 // check if we need to add in dummy start and/or end position/colors
153 bool dummyFirst = false;
154 bool dummyLast = false;
reed@google.com437d6eb2013-05-23 19:03:05 +0000155 if (desc.fPos) {
156 dummyFirst = desc.fPos[0] != 0;
157 dummyLast = desc.fPos[desc.fCount - 1] != SK_Scalar1;
rileya@google.com589708b2012-07-26 20:04:23 +0000158 fColorCount += dummyFirst + dummyLast;
159 }
160
Mike Reed62ce2ca2018-02-19 14:20:15 -0500161 size_t storageSize = fColorCount * (sizeof(SkColor4f) + (desc.fPos ? sizeof(SkScalar) : 0));
Florin Malita89ab2402017-11-01 10:14:57 -0400162 fOrigColors4f = reinterpret_cast<SkColor4f*>(fStorage.reset(storageSize));
Mike Reed62ce2ca2018-02-19 14:20:15 -0500163 fOrigPos = desc.fPos ? reinterpret_cast<SkScalar*>(fOrigColors4f + fColorCount)
164 : nullptr;
rileya@google.com589708b2012-07-26 20:04:23 +0000165
brianosmane25d71c2016-09-28 11:27:28 -0700166 // Now copy over the colors, adding the dummies as needed
167 SkColor4f* origColors = fOrigColors4f;
168 if (dummyFirst) {
169 *origColors++ = desc.fColors[0];
170 }
Florin Malita39d71de2017-10-31 11:33:49 -0400171 for (int i = 0; i < desc.fCount; ++i) {
Mike Reed62ce2ca2018-02-19 14:20:15 -0500172 origColors[i] = desc.fColors[i];
Florin Malita39d71de2017-10-31 11:33:49 -0400173 fColorsAreOpaque = fColorsAreOpaque && (desc.fColors[i].fA == 1);
174 }
brianosmane25d71c2016-09-28 11:27:28 -0700175 if (dummyLast) {
Mike Reed62ce2ca2018-02-19 14:20:15 -0500176 origColors += desc.fCount;
177 *origColors = desc.fColors[desc.fCount - 1];
brianosmane25d71c2016-09-28 11:27:28 -0700178 }
brianosmanb9c51372016-09-15 11:09:45 -0700179
Florin Malita89ab2402017-11-01 10:14:57 -0400180 if (desc.fPos) {
Florin Malita64bb78e2017-11-03 12:54:07 -0400181 SkScalar prev = 0;
Mike Reed62ce2ca2018-02-19 14:20:15 -0500182 SkScalar* origPosPtr = fOrigPos;
Florin Malita64bb78e2017-11-03 12:54:07 -0400183 *origPosPtr++ = prev; // force the first pos to 0
reed9fa60da2014-08-21 07:59:51 -0700184
Florin Malita89ab2402017-11-01 10:14:57 -0400185 int startIndex = dummyFirst ? 0 : 1;
186 int count = desc.fCount + dummyLast;
Florin Malita64bb78e2017-11-03 12:54:07 -0400187
188 bool uniformStops = true;
189 const SkScalar uniformStep = desc.fPos[startIndex] - prev;
Florin Malita89ab2402017-11-01 10:14:57 -0400190 for (int i = startIndex; i < count; i++) {
Florin Malita3e20d022017-11-03 12:11:38 -0400191 // Pin the last value to 1.0, and make sure pos is monotonic.
Brian Osmanaba642c2020-02-06 12:52:25 -0500192 auto curr = (i == desc.fCount) ? 1 : SkTPin(desc.fPos[i], prev, 1.0f);
Florin Malita64bb78e2017-11-03 12:54:07 -0400193 uniformStops &= SkScalarNearlyEqual(uniformStep, curr - prev);
194
195 *origPosPtr++ = prev = curr;
reed9fa60da2014-08-21 07:59:51 -0700196 }
Florin Malita64bb78e2017-11-03 12:54:07 -0400197
Florin Malita64bb78e2017-11-03 12:54:07 -0400198 // If the stops are uniform, treat them as implicit.
Mike Reed62ce2ca2018-02-19 14:20:15 -0500199 if (uniformStops) {
Florin Malita64bb78e2017-11-03 12:54:07 -0400200 fOrigPos = nullptr;
201 }
rileya@google.com589708b2012-07-26 20:04:23 +0000202 }
rileya@google.com589708b2012-07-26 20:04:23 +0000203}
204
Florin Malita89ab2402017-11-01 10:14:57 -0400205SkGradientShaderBase::~SkGradientShaderBase() {}
rileya@google.com589708b2012-07-26 20:04:23 +0000206
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000207void SkGradientShaderBase::flatten(SkWriteBuffer& buffer) const {
reed9fa60da2014-08-21 07:59:51 -0700208 Descriptor desc;
brianosmane25d71c2016-09-28 11:27:28 -0700209 desc.fColors = fOrigColors4f;
brianosmanb9c51372016-09-15 11:09:45 -0700210 desc.fColorSpace = fColorSpace;
reed9fa60da2014-08-21 07:59:51 -0700211 desc.fPos = fOrigPos;
212 desc.fCount = fColorCount;
213 desc.fTileMode = fTileMode;
214 desc.fGradFlags = fGradFlags;
215
216 const SkMatrix& m = this->getLocalMatrix();
halcanary96fcdcc2015-08-27 07:41:13 -0700217 desc.fLocalMatrix = m.isIdentity() ? nullptr : &m;
reed9fa60da2014-08-21 07:59:51 -0700218 desc.flatten(buffer);
rileya@google.com589708b2012-07-26 20:04:23 +0000219}
220
Mike Kleinb11ab572018-10-24 06:42:14 -0400221static void add_stop_color(SkRasterPipeline_GradientCtx* ctx, size_t stop, SkPMColor4f Fs, SkPMColor4f Bs) {
Brian Osman781e3502018-10-03 15:42:47 -0400222 (ctx->fs[0])[stop] = Fs.fR;
223 (ctx->fs[1])[stop] = Fs.fG;
224 (ctx->fs[2])[stop] = Fs.fB;
225 (ctx->fs[3])[stop] = Fs.fA;
Mike Klein85754d52020-01-22 10:04:11 -0600226
Brian Osman781e3502018-10-03 15:42:47 -0400227 (ctx->bs[0])[stop] = Bs.fR;
228 (ctx->bs[1])[stop] = Bs.fG;
229 (ctx->bs[2])[stop] = Bs.fB;
230 (ctx->bs[3])[stop] = Bs.fA;
Mike Kleinf945cbb2017-05-17 09:30:58 -0400231}
Herb Derbyeb99bfd2017-05-16 10:51:26 -0400232
Mike Kleinb11ab572018-10-24 06:42:14 -0400233static void add_const_color(SkRasterPipeline_GradientCtx* ctx, size_t stop, SkPMColor4f color) {
Brian Osman781e3502018-10-03 15:42:47 -0400234 add_stop_color(ctx, stop, { 0, 0, 0, 0 }, color);
Mike Kleinf945cbb2017-05-17 09:30:58 -0400235}
Herb Derbyeb99bfd2017-05-16 10:51:26 -0400236
237// Calculate a factor F and a bias B so that color = F*t + B when t is in range of
238// the stop. Assume that the distance between stops is 1/gapCount.
239static void init_stop_evenly(
Mike Kleinb11ab572018-10-24 06:42:14 -0400240 SkRasterPipeline_GradientCtx* ctx, float gapCount, size_t stop, SkPMColor4f c_l, SkPMColor4f c_r) {
Mike Klein68768172017-05-17 09:54:36 -0400241 // Clankium's GCC 4.9 targeting ARMv7 is barfing when we use Sk4f math here, so go scalar...
Brian Osman781e3502018-10-03 15:42:47 -0400242 SkPMColor4f Fs = {
243 (c_r.fR - c_l.fR) * gapCount,
244 (c_r.fG - c_l.fG) * gapCount,
245 (c_r.fB - c_l.fB) * gapCount,
246 (c_r.fA - c_l.fA) * gapCount,
247 };
248 SkPMColor4f Bs = {
249 c_l.fR - Fs.fR*(stop/gapCount),
250 c_l.fG - Fs.fG*(stop/gapCount),
251 c_l.fB - Fs.fB*(stop/gapCount),
252 c_l.fA - Fs.fA*(stop/gapCount),
253 };
Herb Derbyeb99bfd2017-05-16 10:51:26 -0400254 add_stop_color(ctx, stop, Fs, Bs);
Mike Kleinf945cbb2017-05-17 09:30:58 -0400255}
Herb Derbyeb99bfd2017-05-16 10:51:26 -0400256
257// For each stop we calculate a bias B and a scale factor F, such that
258// for any t between stops n and n+1, the color we want is B[n] + F[n]*t.
259static void init_stop_pos(
Mike Kleinb11ab572018-10-24 06:42:14 -0400260 SkRasterPipeline_GradientCtx* ctx, size_t stop, float t_l, float t_r, SkPMColor4f c_l, SkPMColor4f c_r) {
Mike Klein68768172017-05-17 09:54:36 -0400261 // See note about Clankium's old compiler in init_stop_evenly().
Brian Osman781e3502018-10-03 15:42:47 -0400262 SkPMColor4f Fs = {
263 (c_r.fR - c_l.fR) / (t_r - t_l),
264 (c_r.fG - c_l.fG) / (t_r - t_l),
265 (c_r.fB - c_l.fB) / (t_r - t_l),
266 (c_r.fA - c_l.fA) / (t_r - t_l),
267 };
268 SkPMColor4f Bs = {
269 c_l.fR - Fs.fR*t_l,
270 c_l.fG - Fs.fG*t_l,
271 c_l.fB - Fs.fB*t_l,
272 c_l.fA - Fs.fA*t_l,
273 };
Herb Derbyeb99bfd2017-05-16 10:51:26 -0400274 ctx->ts[stop] = t_l;
275 add_stop_color(ctx, stop, Fs, Bs);
Mike Kleinf945cbb2017-05-17 09:30:58 -0400276}
Herb Derbyeb99bfd2017-05-16 10:51:26 -0400277
Mike Reed1386b2d2019-03-13 21:15:05 -0400278bool SkGradientShaderBase::onAppendStages(const SkStageRec& rec) const {
Mike Reed1d8c42e2017-08-29 14:58:19 -0400279 SkRasterPipeline* p = rec.fPipeline;
280 SkArenaAlloc* alloc = rec.fAlloc;
Mike Kleinb11ab572018-10-24 06:42:14 -0400281 SkRasterPipeline_DecalTileCtx* decal_ctx = nullptr;
Mike Reed1d8c42e2017-08-29 14:58:19 -0400282
Mike Kleina3771842017-05-04 19:38:48 -0400283 SkMatrix matrix;
Brian Osman9aaec362020-05-08 14:54:37 -0400284 if (!this->computeTotalInverse(rec.fMatrixProvider.localToDevice(), rec.fLocalM, &matrix)) {
Mike Kleina3771842017-05-04 19:38:48 -0400285 return false;
286 }
Florin Malita50b20842017-07-29 19:08:28 -0400287 matrix.postConcat(fPtsToUnit);
Mike Kleina3771842017-05-04 19:38:48 -0400288
Florin Malita2e409002017-06-28 14:46:54 -0400289 SkRasterPipeline_<256> postPipeline;
Mike Kleina3771842017-05-04 19:38:48 -0400290
Mike Kleine8de0242018-03-10 12:37:11 -0500291 p->append(SkRasterPipeline::seed_shader);
Mike Reed6b59bf42017-07-03 21:26:44 -0400292 p->append_matrix(alloc, matrix);
Florin Malita50b20842017-07-29 19:08:28 -0400293 this->appendGradientStages(alloc, p, &postPipeline);
Mike Kleine7598532017-05-11 11:29:29 -0400294
Mike Reed62ce2ca2018-02-19 14:20:15 -0500295 switch(fTileMode) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400296 case SkTileMode::kMirror: p->append(SkRasterPipeline::mirror_x_1); break;
297 case SkTileMode::kRepeat: p->append(SkRasterPipeline::repeat_x_1); break;
298 case SkTileMode::kDecal:
Mike Kleinb11ab572018-10-24 06:42:14 -0400299 decal_ctx = alloc->make<SkRasterPipeline_DecalTileCtx>();
Mike Reed62ce2ca2018-02-19 14:20:15 -0500300 decal_ctx->limit_x = SkBits2Float(SkFloat2Bits(1.0f) + 1);
301 // reuse mask + limit_x stage, or create a custom decal_1 that just stores the mask
302 p->append(SkRasterPipeline::decal_x, decal_ctx);
John Stiles30212b72020-06-11 17:55:07 -0400303 [[fallthrough]];
304
Mike Reedfae8fce2019-04-03 10:27:45 -0400305 case SkTileMode::kClamp:
Mike Kleine7598532017-05-11 11:29:29 -0400306 if (!fOrigPos) {
307 // We clamp only when the stops are evenly spaced.
308 // If not, there may be hard stops, and clamping ruins hard stops at 0 and/or 1.
Mike Klein5c7960b2017-05-11 10:59:22 -0400309 // In that case, we must make sure we're using the general "gradient" stage,
Mike Kleine7598532017-05-11 11:29:29 -0400310 // which is the only stage that will correctly handle unclamped t.
Mike Klein9f85d682017-05-23 07:52:01 -0400311 p->append(SkRasterPipeline::clamp_x_1);
Mike Kleine7598532017-05-11 11:29:29 -0400312 }
Mike Reed62ce2ca2018-02-19 14:20:15 -0500313 break;
Mike Kleine7598532017-05-11 11:29:29 -0400314 }
Mike Kleina3771842017-05-04 19:38:48 -0400315
316 const bool premulGrad = fGradFlags & SkGradientShader::kInterpolateColorsInPremul_Flag;
Brian Osman6667fb12018-07-03 16:44:02 -0400317
318 // Transform all of the colors to destination color space
319 SkColor4fXformer xformedColors(fOrigColors4f, fColorCount, fColorSpace.get(), rec.fDstCS);
320
321 auto prepareColor = [premulGrad, &xformedColors](int i) {
322 SkColor4f c = xformedColors.fColors[i];
Brian Osman781e3502018-10-03 15:42:47 -0400323 return premulGrad ? c.premul()
324 : SkPMColor4f{ c.fR, c.fG, c.fB, c.fA };
Mike Kleina3771842017-05-04 19:38:48 -0400325 };
326
327 // The two-stop case with stops at 0 and 1.
328 if (fColorCount == 2 && fOrigPos == nullptr) {
Brian Osman781e3502018-10-03 15:42:47 -0400329 const SkPMColor4f c_l = prepareColor(0),
330 c_r = prepareColor(1);
Mike Kleina3771842017-05-04 19:38:48 -0400331
332 // See F and B below.
Mike Kleinb11ab572018-10-24 06:42:14 -0400333 auto ctx = alloc->make<SkRasterPipeline_EvenlySpaced2StopGradientCtx>();
Brian Osman781e3502018-10-03 15:42:47 -0400334 (Sk4f::Load(c_r.vec()) - Sk4f::Load(c_l.vec())).store(ctx->f);
335 ( Sk4f::Load(c_l.vec())).store(ctx->b);
Mike Klein24de6482018-09-07 12:05:29 -0400336 ctx->interpolatedInPremul = premulGrad;
Mike Kleina3771842017-05-04 19:38:48 -0400337
Mike Klein24de6482018-09-07 12:05:29 -0400338 p->append(SkRasterPipeline::evenly_spaced_2_stop_gradient, ctx);
Mike Kleina3771842017-05-04 19:38:48 -0400339 } else {
Mike Kleinb11ab572018-10-24 06:42:14 -0400340 auto* ctx = alloc->make<SkRasterPipeline_GradientCtx>();
Mike Klein24de6482018-09-07 12:05:29 -0400341 ctx->interpolatedInPremul = premulGrad;
Herb Derby4de13042017-05-15 10:49:39 -0400342
343 // Note: In order to handle clamps in search, the search assumes a stop conceptully placed
344 // at -inf. Therefore, the max number of stops is fColorCount+1.
345 for (int i = 0; i < 4; i++) {
346 // Allocate at least at for the AVX2 gather from a YMM register.
347 ctx->fs[i] = alloc->makeArray<float>(std::max(fColorCount+1, 8));
348 ctx->bs[i] = alloc->makeArray<float>(std::max(fColorCount+1, 8));
349 }
350
Mike Kleina3771842017-05-04 19:38:48 -0400351 if (fOrigPos == nullptr) {
352 // Handle evenly distributed stops.
353
Herb Derby4de13042017-05-15 10:49:39 -0400354 size_t stopCount = fColorCount;
355 float gapCount = stopCount - 1;
Mike Kleina3771842017-05-04 19:38:48 -0400356
Brian Osman781e3502018-10-03 15:42:47 -0400357 SkPMColor4f c_l = prepareColor(0);
Herb Derby4de13042017-05-15 10:49:39 -0400358 for (size_t i = 0; i < stopCount - 1; i++) {
Brian Osman781e3502018-10-03 15:42:47 -0400359 SkPMColor4f c_r = prepareColor(i + 1);
Herb Derbyeb99bfd2017-05-16 10:51:26 -0400360 init_stop_evenly(ctx, gapCount, i, c_l, c_r);
Mike Kleina3771842017-05-04 19:38:48 -0400361 c_l = c_r;
362 }
Herb Derbyeb99bfd2017-05-16 10:51:26 -0400363 add_const_color(ctx, stopCount - 1, c_l);
Mike Kleina3771842017-05-04 19:38:48 -0400364
Herb Derby4de13042017-05-15 10:49:39 -0400365 ctx->stopCount = stopCount;
366 p->append(SkRasterPipeline::evenly_spaced_gradient, ctx);
Mike Kleina3771842017-05-04 19:38:48 -0400367 } else {
368 // Handle arbitrary stops.
369
Herb Derby4de13042017-05-15 10:49:39 -0400370 ctx->ts = alloc->makeArray<float>(fColorCount+1);
371
Mike Kleina3771842017-05-04 19:38:48 -0400372 // Remove the dummy stops inserted by SkGradientShaderBase::SkGradientShaderBase
373 // because they are naturally handled by the search method.
374 int firstStop;
375 int lastStop;
376 if (fColorCount > 2) {
377 firstStop = fOrigColors4f[0] != fOrigColors4f[1] ? 0 : 1;
378 lastStop = fOrigColors4f[fColorCount - 2] != fOrigColors4f[fColorCount - 1]
379 ? fColorCount - 1 : fColorCount - 2;
380 } else {
381 firstStop = 0;
382 lastStop = 1;
383 }
Mike Kleina3771842017-05-04 19:38:48 -0400384
Mike Kleina3771842017-05-04 19:38:48 -0400385 size_t stopCount = 0;
386 float t_l = fOrigPos[firstStop];
Brian Osman781e3502018-10-03 15:42:47 -0400387 SkPMColor4f c_l = prepareColor(firstStop);
Herb Derbyeb99bfd2017-05-16 10:51:26 -0400388 add_const_color(ctx, stopCount++, c_l);
Mike Kleina3771842017-05-04 19:38:48 -0400389 // N.B. lastStop is the index of the last stop, not one after.
390 for (int i = firstStop; i < lastStop; i++) {
391 float t_r = fOrigPos[i + 1];
Brian Osman781e3502018-10-03 15:42:47 -0400392 SkPMColor4f c_r = prepareColor(i + 1);
Florin Malita3e20d022017-11-03 12:11:38 -0400393 SkASSERT(t_l <= t_r);
Mike Kleina3771842017-05-04 19:38:48 -0400394 if (t_l < t_r) {
Herb Derbyeb99bfd2017-05-16 10:51:26 -0400395 init_stop_pos(ctx, stopCount, t_l, t_r, c_l, c_r);
Mike Kleina3771842017-05-04 19:38:48 -0400396 stopCount += 1;
397 }
398 t_l = t_r;
399 c_l = c_r;
400 }
401
Herb Derby4de13042017-05-15 10:49:39 -0400402 ctx->ts[stopCount] = t_l;
Herb Derbyeb99bfd2017-05-16 10:51:26 -0400403 add_const_color(ctx, stopCount++, c_l);
Mike Kleina3771842017-05-04 19:38:48 -0400404
Herb Derby4de13042017-05-15 10:49:39 -0400405 ctx->stopCount = stopCount;
406 p->append(SkRasterPipeline::gradient, ctx);
Mike Kleina3771842017-05-04 19:38:48 -0400407 }
Mike Kleina3771842017-05-04 19:38:48 -0400408 }
409
Mike Reed62ce2ca2018-02-19 14:20:15 -0500410 if (decal_ctx) {
411 p->append(SkRasterPipeline::check_decal_mask, decal_ctx);
412 }
413
Mike Kleina3771842017-05-04 19:38:48 -0400414 if (!premulGrad && !this->colorsAreOpaque()) {
Mike Kleine7598532017-05-11 11:29:29 -0400415 p->append(SkRasterPipeline::premul);
Mike Kleina3771842017-05-04 19:38:48 -0400416 }
417
Florin Malita2e409002017-06-28 14:46:54 -0400418 p->extend(postPipeline);
419
Mike Kleina3771842017-05-04 19:38:48 -0400420 return true;
421}
422
Mike Kleina434e0f2020-03-23 09:33:48 -0500423skvm::Color SkGradientShaderBase::onProgram(skvm::Builder* p,
Brian Osman5aaaeea2020-06-22 14:26:03 -0400424 skvm::Coord device, skvm::Coord local,
425 skvm::Color /*paint*/,
Mike Kleine81b1082020-06-19 11:29:13 -0500426 const SkMatrixProvider& mats, const SkMatrix* localM,
Mike Kleina434e0f2020-03-23 09:33:48 -0500427 SkFilterQuality quality, const SkColorInfo& dstInfo,
Mike Klein276a7852020-03-15 08:46:09 -0500428 skvm::Uniforms* uniforms, SkArenaAlloc* alloc) const {
Mike Klein85754d52020-01-22 10:04:11 -0600429 SkMatrix inv;
Mike Kleine81b1082020-06-19 11:29:13 -0500430 if (!this->computeTotalInverse(mats.localToDevice(), localM, &inv)) {
Mike Reed6352f002020-03-14 23:30:10 -0400431 return {};
Mike Klein85754d52020-01-22 10:04:11 -0600432 }
433 inv.postConcat(fPtsToUnit);
434 inv.normalizePerspective();
435
Mike Kleinb8219e42020-06-24 07:01:40 -0500436 local = SkShaderBase::ApplyMatrix(p, inv, local, uniforms);
Mike Klein85754d52020-01-22 10:04:11 -0600437
Mike Kleince9e0602020-01-29 09:47:44 -0600438 skvm::I32 mask = p->splat(~0);
Brian Osman5aaaeea2020-06-22 14:26:03 -0400439 skvm::F32 t = this->transformT(p,uniforms, local, &mask);
Mike Kleincaf5ee42020-01-28 16:11:34 -0600440
441 // Perhaps unexpectedly, clamping is handled naturally by our search, so we
442 // don't explicitly clamp t to [0,1]. That clamp would break hard stops
443 // right at 0 or 1 boundaries in kClamp mode. (kRepeat and kMirror always
444 // produce values in [0,1].)
Mike Klein85754d52020-01-22 10:04:11 -0600445 switch(fTileMode) {
Mike Kleincaf5ee42020-01-28 16:11:34 -0600446 case SkTileMode::kClamp:
447 break;
448
449 case SkTileMode::kDecal:
Mike Reedb6e7ef12020-04-03 13:34:26 -0400450 mask &= (t == clamp01(t));
Mike Kleincaf5ee42020-01-28 16:11:34 -0600451 break;
452
453 case SkTileMode::kRepeat:
Mike Reedb6e7ef12020-04-03 13:34:26 -0400454 t = fract(t);
Mike Kleincaf5ee42020-01-28 16:11:34 -0600455 break;
456
Mike Klein85754d52020-01-22 10:04:11 -0600457 case SkTileMode::kMirror: {
458 // t = | (t-1) - 2*(floor( (t-1)*0.5 )) - 1 |
459 // {-A-} {--------B-------}
Mike Reedb6e7ef12020-04-03 13:34:26 -0400460 skvm::F32 A = t - 1.0f,
461 B = floor(A * 0.5f);
462 t = abs(A - (B + B) - 1.0f);
Mike Klein85754d52020-01-22 10:04:11 -0600463 } break;
464 }
465
466 // Transform our colors as we want them interpolated, in dst color space, possibly premul.
467 SkImageInfo common = SkImageInfo::Make(fColorCount,1, kRGBA_F32_SkColorType
468 , kUnpremul_SkAlphaType),
Mike Kleina434e0f2020-03-23 09:33:48 -0500469 src = common.makeColorSpace(fColorSpace),
470 dst = common.makeColorSpace(dstInfo.refColorSpace());
Mike Klein85754d52020-01-22 10:04:11 -0600471 if (fGradFlags & SkGradientShader::kInterpolateColorsInPremul_Flag) {
472 dst = dst.makeAlphaType(kPremul_SkAlphaType);
473 }
474
475 std::vector<float> rgba(4*fColorCount); // TODO: SkSTArray?
476 SkConvertPixels(dst, rgba.data(), dst.minRowBytes(),
477 src, fOrigColors4f, src.minRowBytes());
478
479 // Transform our colors into a scale factor f and bias b such that for
480 // any t between stops i and i+1, the color we want is mad(t, f[i], b[i]).
481 using F4 = skvx::Vec<4,float>;
482 struct FB { F4 f,b; };
Mike Reed6352f002020-03-14 23:30:10 -0400483 skvm::Color color;
Mike Klein85754d52020-01-22 10:04:11 -0600484
Mike Reedb6e7ef12020-04-03 13:34:26 -0400485 auto uniformF = [&](float x) { return p->uniformF(uniforms->pushF(x)); };
486
Mike Klein85754d52020-01-22 10:04:11 -0600487 if (fColorCount == 2) {
488 // 2-stop gradients have colors at 0 and 1, and so must be evenly spaced.
489 SkASSERT(fOrigPos == nullptr);
490
491 // With 2 stops, we upload the single FB as uniforms and interpolate directly with t.
492 F4 lo = F4::Load(rgba.data() + 0),
493 hi = F4::Load(rgba.data() + 4);
494 F4 F = hi - lo,
495 B = lo;
496
Mike Reedb6e7ef12020-04-03 13:34:26 -0400497 auto T = clamp01(t);
Mike Reed6352f002020-03-14 23:30:10 -0400498 color = {
Mike Reedb6e7ef12020-04-03 13:34:26 -0400499 T * uniformF(F[0]) + uniformF(B[0]),
500 T * uniformF(F[1]) + uniformF(B[1]),
501 T * uniformF(F[2]) + uniformF(B[2]),
502 T * uniformF(F[3]) + uniformF(B[3]),
Mike Reed6352f002020-03-14 23:30:10 -0400503 };
Mike Klein85754d52020-01-22 10:04:11 -0600504 } else {
505 // To handle clamps in search we add a conceptual stop at t=-inf, so we
506 // may need up to fColorCount+1 FBs and fColorCount t stops between them:
507 //
508 // FBs: [color 0] [color 0->1] [color 1->2] [color 2->3] ...
509 // stops: (-inf) t0 t1 t2 ...
510 //
511 // Both these arrays could end up shorter if any hard stops share the same t.
512 FB* fb = alloc->makeArrayDefault<FB>(fColorCount+1);
513 std::vector<float> stops; // TODO: SkSTArray?
514 stops.reserve(fColorCount);
515
516 // Here's our conceptual stop at t=-inf covering all t<=0, clamping to our first color.
517 float t_lo = this->getPos(0);
518 F4 color_lo = F4::Load(rgba.data());
519 fb[0] = { 0.0f, color_lo };
520 // N.B. No stops[] entry for this implicit -inf.
521
522 // Now the non-edge cases, calculating scale and bias between adjacent normal stops.
523 for (int i = 1; i < fColorCount; i++) {
524 float t_hi = this->getPos(i);
525 F4 color_hi = F4::Load(rgba.data() + 4*i);
526
527 // If t_lo == t_hi, we're on a hard stop, and transition immediately to the next color.
528 SkASSERT(t_lo <= t_hi);
529 if (t_lo < t_hi) {
530 F4 f = (color_hi - color_lo) / (t_hi - t_lo),
531 b = color_lo - f*t_lo;
532 stops.push_back(t_lo);
533 fb[stops.size()] = {f,b};
534 }
535
536 t_lo = t_hi;
537 color_lo = color_hi;
538 }
539 // Anything >= our final t clamps to our final color.
540 stops.push_back(t_lo);
541 fb[stops.size()] = { 0.0f, color_lo };
542
543 // We'll gather FBs from that array we just created.
Mike Klein8b99b9e2020-03-31 12:28:41 -0500544 skvm::Uniform fbs = uniforms->pushPtr(fb);
Mike Klein85754d52020-01-22 10:04:11 -0600545
546 // Find the two stops we need to interpolate.
547 skvm::I32 ix;
548 if (fOrigPos == nullptr) {
549 // Evenly spaced stops... we can calculate ix directly.
550 // Of note: we need to clamp t and skip over that conceptual -inf stop we made up.
Mike Reedb6e7ef12020-04-03 13:34:26 -0400551 ix = trunc(clamp01(t) * uniformF(stops.size() - 1) + 1.0f);
Mike Klein85754d52020-01-22 10:04:11 -0600552 } else {
553 // Starting ix at 0 bakes in our conceptual first stop at -inf.
554 // TODO: good place to experiment with a loop in skvm.... stops.size() can be huge.
555 ix = p->splat(0);
556 for (float stop : stops) {
557 // ix += (t >= stop) ? +1 : 0 ~~>
558 // ix -= (t >= stop) ? -1 : 0
Mike Reedb6e7ef12020-04-03 13:34:26 -0400559 ix -= (t >= uniformF(stop));
Mike Klein85754d52020-01-22 10:04:11 -0600560 }
561 // TODO: we could skip any of the dummy stops GradientShaderBase's ctor added
562 // to ensure the full [0,1] span is covered. This linear search doesn't need
563 // them for correctness, and it'd be up to two fewer stops to check.
564 // N.B. we do still need those stops for the fOrigPos == nullptr direct math path.
565 }
566
567 // A scale factor and bias for each lane, 8 total.
568 // TODO: simpler, faster, tidier to push 8 uniform pointers, one for each struct lane?
Mike Reedb6e7ef12020-04-03 13:34:26 -0400569 ix = shl(ix, 3);
570 skvm::F32 Fr = gatherF(fbs, ix + 0);
571 skvm::F32 Fg = gatherF(fbs, ix + 1);
572 skvm::F32 Fb = gatherF(fbs, ix + 2);
573 skvm::F32 Fa = gatherF(fbs, ix + 3);
Mike Klein85754d52020-01-22 10:04:11 -0600574
Mike Reedb6e7ef12020-04-03 13:34:26 -0400575 skvm::F32 Br = gatherF(fbs, ix + 4);
576 skvm::F32 Bg = gatherF(fbs, ix + 5);
577 skvm::F32 Bb = gatherF(fbs, ix + 6);
578 skvm::F32 Ba = gatherF(fbs, ix + 7);
Mike Klein85754d52020-01-22 10:04:11 -0600579
580 // This is what we've been building towards!
Mike Reed6352f002020-03-14 23:30:10 -0400581 color = {
Mike Reedb6e7ef12020-04-03 13:34:26 -0400582 t * Fr + Br,
583 t * Fg + Bg,
584 t * Fb + Bb,
585 t * Fa + Ba,
Mike Reed6352f002020-03-14 23:30:10 -0400586 };
Mike Klein85754d52020-01-22 10:04:11 -0600587 }
588
589 // If we interpolated unpremul, premul now to match our output convention.
590 if (0 == (fGradFlags & SkGradientShader::kInterpolateColorsInPremul_Flag)
591 && !fColorsAreOpaque) {
Mike Reedb6e7ef12020-04-03 13:34:26 -0400592 color = premul(color);
Mike Klein85754d52020-01-22 10:04:11 -0600593 }
594
Mike Reed6352f002020-03-14 23:30:10 -0400595 return {
Mike Klein5ec9c4e2020-12-01 10:43:46 -0600596 pun_to_F32(mask & pun_to_I32(color.r)),
597 pun_to_F32(mask & pun_to_I32(color.g)),
598 pun_to_F32(mask & pun_to_I32(color.b)),
599 pun_to_F32(mask & pun_to_I32(color.a)),
Mike Reed6352f002020-03-14 23:30:10 -0400600 };
Mike Klein85754d52020-01-22 10:04:11 -0600601}
602
Mike Kleina3771842017-05-04 19:38:48 -0400603
rileya@google.com589708b2012-07-26 20:04:23 +0000604bool SkGradientShaderBase::isOpaque() const {
Mike Reedfae8fce2019-04-03 10:27:45 -0400605 return fColorsAreOpaque && (this->getTileMode() != SkTileMode::kDecal);
Mike Reed62ce2ca2018-02-19 14:20:15 -0500606}
607
reed8367b8c2014-08-22 08:30:20 -0700608static unsigned rounded_divide(unsigned numer, unsigned denom) {
609 return (numer + (denom >> 1)) / denom;
610}
611
612bool SkGradientShaderBase::onAsLuminanceColor(SkColor* lum) const {
613 // we just compute an average color.
614 // possibly we could weight this based on the proportional width for each color
615 // assuming they are not evenly distributed in the fPos array.
616 int r = 0;
617 int g = 0;
618 int b = 0;
619 const int n = fColorCount;
Florin Malita39d71de2017-10-31 11:33:49 -0400620 // TODO: use linear colors?
reed8367b8c2014-08-22 08:30:20 -0700621 for (int i = 0; i < n; ++i) {
Florin Malita39d71de2017-10-31 11:33:49 -0400622 SkColor c = this->getLegacyColor(i);
reed8367b8c2014-08-22 08:30:20 -0700623 r += SkColorGetR(c);
624 g += SkColorGetG(c);
625 b += SkColorGetB(c);
626 }
627 *lum = SkColorSetRGB(rounded_divide(r, n), rounded_divide(g, n), rounded_divide(b, n));
628 return true;
629}
630
Brian Osman6667fb12018-07-03 16:44:02 -0400631SkColor4fXformer::SkColor4fXformer(const SkColor4f* colors, int colorCount,
632 SkColorSpace* src, SkColorSpace* dst) {
Brian Osman6667fb12018-07-03 16:44:02 -0400633 fColors = colors;
Brian Osmanccd39952018-07-06 16:16:43 -0400634
Mike Kleinf9f68ff2018-10-12 14:23:06 -0400635 if (dst && !SkColorSpace::Equals(src, dst)) {
Brian Osman6667fb12018-07-03 16:44:02 -0400636 fStorage.reset(colorCount);
Brian Salomon5dfcf132018-10-12 14:39:32 +0000637
638 auto info = SkImageInfo::Make(colorCount,1, kRGBA_F32_SkColorType, kUnpremul_SkAlphaType);
639
640 SkConvertPixels(info.makeColorSpace(sk_ref_sp(dst)), fStorage.begin(), info.minRowBytes(),
641 info.makeColorSpace(sk_ref_sp(src)), fColors , info.minRowBytes());
642
Brian Osman6667fb12018-07-03 16:44:02 -0400643 fColors = fStorage.begin();
644 }
645}
646
Florin Malita5f379a82017-10-18 16:22:35 -0400647void SkGradientShaderBase::commonAsAGradient(GradientInfo* info) const {
rileya@google.com589708b2012-07-26 20:04:23 +0000648 if (info) {
649 if (info->fColorCount >= fColorCount) {
650 if (info->fColors) {
Florin Malita39d71de2017-10-31 11:33:49 -0400651 for (int i = 0; i < fColorCount; ++i) {
652 info->fColors[i] = this->getLegacyColor(i);
653 }
rileya@google.com589708b2012-07-26 20:04:23 +0000654 }
655 if (info->fColorOffsets) {
Florin Malitaed6ae562017-10-28 11:06:48 -0400656 for (int i = 0; i < fColorCount; ++i) {
657 info->fColorOffsets[i] = this->getPos(i);
rileya@google.com589708b2012-07-26 20:04:23 +0000658 }
659 }
660 }
661 info->fColorCount = fColorCount;
662 info->fTileMode = fTileMode;
reed@google.com3d3a8602013-05-24 14:58:44 +0000663 info->fGradientFlags = fGradFlags;
rileya@google.com589708b2012-07-26 20:04:23 +0000664 }
665}
666
667///////////////////////////////////////////////////////////////////////////////
668///////////////////////////////////////////////////////////////////////////////
669
reed1b747302015-01-06 07:13:19 -0800670// Return true if these parameters are valid/legal/safe to construct a gradient
671//
brianosmane25d71c2016-09-28 11:27:28 -0700672static bool valid_grad(const SkColor4f colors[], const SkScalar pos[], int count,
Mike Reedfae8fce2019-04-03 10:27:45 -0400673 SkTileMode tileMode) {
674 return nullptr != colors && count >= 1 && (unsigned)tileMode < kSkTileModeCount;
reed1b747302015-01-06 07:13:19 -0800675}
676
reed@google.com437d6eb2013-05-23 19:03:05 +0000677static void desc_init(SkGradientShaderBase::Descriptor* desc,
brianosmane25d71c2016-09-28 11:27:28 -0700678 const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace,
679 const SkScalar pos[], int colorCount,
Mike Reedfae8fce2019-04-03 10:27:45 -0400680 SkTileMode mode, uint32_t flags, const SkMatrix* localMatrix) {
fmalita748d6202016-05-11 11:39:58 -0700681 SkASSERT(colorCount > 1);
682
commit-bot@chromium.org6c5aea22014-04-22 16:25:15 +0000683 desc->fColors = colors;
brianosmane25d71c2016-09-28 11:27:28 -0700684 desc->fColorSpace = std::move(colorSpace);
commit-bot@chromium.org6c5aea22014-04-22 16:25:15 +0000685 desc->fPos = pos;
686 desc->fCount = colorCount;
687 desc->fTileMode = mode;
commit-bot@chromium.org6c5aea22014-04-22 16:25:15 +0000688 desc->fGradFlags = flags;
reedaddf2ed2014-08-11 08:28:24 -0700689 desc->fLocalMatrix = localMatrix;
reed@google.com437d6eb2013-05-23 19:03:05 +0000690}
691
Mike Klein024072a2018-11-11 00:26:30 +0000692static SkColor4f average_gradient_color(const SkColor4f colors[], const SkScalar pos[],
693 int colorCount) {
694 // The gradient is a piecewise linear interpolation between colors. For a given interval,
695 // the integral between the two endpoints is 0.5 * (ci + cj) * (pj - pi), which provides that
696 // intervals average color. The overall average color is thus the sum of each piece. The thing
697 // to keep in mind is that the provided gradient definition may implicitly use p=0 and p=1.
Mike Kleina17799a2020-07-20 09:59:16 -0500698 Sk4f blend(0.0f);
Mike Klein024072a2018-11-11 00:26:30 +0000699 for (int i = 0; i < colorCount - 1; ++i) {
700 // Calculate the average color for the interval between pos(i) and pos(i+1)
701 Sk4f c0 = Sk4f::Load(&colors[i]);
702 Sk4f c1 = Sk4f::Load(&colors[i + 1]);
Mike Kleina17799a2020-07-20 09:59:16 -0500703
Mike Klein024072a2018-11-11 00:26:30 +0000704 // when pos == null, there are colorCount uniformly distributed stops, going from 0 to 1,
705 // so pos[i + 1] - pos[i] = 1/(colorCount-1)
Michael Ludwigad5ec1a2020-11-19 14:15:05 -0500706 SkScalar w;
707 if (pos) {
708 // Match position fixing in SkGradientShader's constructor, clamping positions outside
709 // [0, 1] and forcing the sequence to be monotonic
710 SkScalar p0 = SkTPin(pos[i], 0.f, 1.f);
711 SkScalar p1 = SkTPin(pos[i + 1], p0, 1.f);
712 w = p1 - p0;
713
714 // And account for any implicit intervals at the start or end of the positions
715 if (i == 0) {
716 if (p0 > 0.0f) {
717 // The first color is fixed between p = 0 to pos[0], so 0.5*(ci + cj)*(pj - pi)
718 // becomes 0.5*(c + c)*(pj - 0) = c * pj
719 Sk4f c = Sk4f::Load(&colors[0]);
720 blend += p0 * c;
721 }
722 }
723 if (i == colorCount - 2) {
724 if (p1 < 1.f) {
725 // The last color is fixed between pos[n-1] to p = 1, so 0.5*(ci + cj)*(pj - pi)
726 // becomes 0.5*(c + c)*(1 - pi) = c * (1 - pi)
727 Sk4f c = Sk4f::Load(&colors[colorCount - 1]);
728 blend += (1.f - p1) * c;
729 }
730 }
731 } else {
732 w = 1.f / (colorCount - 1);
733 }
Mike Kleina17799a2020-07-20 09:59:16 -0500734
735 blend += 0.5f * w * (c1 + c0);
Mike Klein024072a2018-11-11 00:26:30 +0000736 }
737
Mike Klein024072a2018-11-11 00:26:30 +0000738 SkColor4f avg;
739 blend.store(&avg);
740 return avg;
741}
742
Michael Ludwigd431c722018-11-16 10:00:24 -0500743// The default SkScalarNearlyZero threshold of .0024 is too big and causes regressions for svg
744// gradients defined in the wild.
745static constexpr SkScalar kDegenerateThreshold = SK_Scalar1 / (1 << 15);
746
Mike Klein024072a2018-11-11 00:26:30 +0000747// Except for special circumstances of clamped gradients, every gradient shape--when degenerate--
748// can be mapped to the same fallbacks. The specific shape factories must account for special
749// clamped conditions separately, this will always return the last color for clamped gradients.
750static sk_sp<SkShader> make_degenerate_gradient(const SkColor4f colors[], const SkScalar pos[],
751 int colorCount, sk_sp<SkColorSpace> colorSpace,
Mike Reedfae8fce2019-04-03 10:27:45 -0400752 SkTileMode mode) {
Mike Klein024072a2018-11-11 00:26:30 +0000753 switch(mode) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400754 case SkTileMode::kDecal:
Mike Klein024072a2018-11-11 00:26:30 +0000755 // normally this would reject the area outside of the interpolation region, so since
756 // inside region is empty when the radii are equal, the entire draw region is empty
Mike Reedc8bea7d2019-04-09 13:55:36 -0400757 return SkShaders::Empty();
Mike Reedfae8fce2019-04-03 10:27:45 -0400758 case SkTileMode::kRepeat:
759 case SkTileMode::kMirror:
Mike Klein024072a2018-11-11 00:26:30 +0000760 // repeat and mirror are treated the same: the border colors are never visible,
761 // but approximate the final color as infinite repetitions of the colors, so
762 // it can be represented as the average color of the gradient.
Mike Reedc8bea7d2019-04-09 13:55:36 -0400763 return SkShaders::Color(
Mike Klein024072a2018-11-11 00:26:30 +0000764 average_gradient_color(colors, pos, colorCount), std::move(colorSpace));
Mike Reedfae8fce2019-04-03 10:27:45 -0400765 case SkTileMode::kClamp:
Mike Klein024072a2018-11-11 00:26:30 +0000766 // Depending on how the gradient shape degenerates, there may be a more specialized
767 // fallback representation for the factories to use, but this is a reasonable default.
Mike Reedc8bea7d2019-04-09 13:55:36 -0400768 return SkShaders::Color(colors[colorCount - 1], std::move(colorSpace));
Mike Klein024072a2018-11-11 00:26:30 +0000769 }
Mike Reedfae8fce2019-04-03 10:27:45 -0400770 SkDEBUGFAIL("Should not be reached");
771 return nullptr;
Mike Klein024072a2018-11-11 00:26:30 +0000772}
773
brianosmane25d71c2016-09-28 11:27:28 -0700774// assumes colors is SkColor4f* and pos is SkScalar*
fmenozzie9fd0f82016-08-19 07:50:57 -0700775#define EXPAND_1_COLOR(count) \
brianosmane25d71c2016-09-28 11:27:28 -0700776 SkColor4f tmp[2]; \
fmenozzie9fd0f82016-08-19 07:50:57 -0700777 do { \
778 if (1 == count) { \
779 tmp[0] = tmp[1] = colors[0]; \
780 colors = tmp; \
781 pos = nullptr; \
782 count = 2; \
783 } \
784 } while (0)
785
fmenozzi68d952c2016-08-19 08:56:56 -0700786struct ColorStopOptimizer {
Mike Reedfae8fce2019-04-03 10:27:45 -0400787 ColorStopOptimizer(const SkColor4f* colors, const SkScalar* pos, int count, SkTileMode mode)
fmenozzi68d952c2016-08-19 08:56:56 -0700788 : fColors(colors)
789 , fPos(pos)
790 , fCount(count) {
791
792 if (!pos || count != 3) {
793 return;
794 }
795
796 if (SkScalarNearlyEqual(pos[0], 0.0f) &&
797 SkScalarNearlyEqual(pos[1], 0.0f) &&
798 SkScalarNearlyEqual(pos[2], 1.0f)) {
799
Mike Reedfae8fce2019-04-03 10:27:45 -0400800 if (SkTileMode::kRepeat == mode || SkTileMode::kMirror == mode ||
fmenozzi68d952c2016-08-19 08:56:56 -0700801 colors[0] == colors[1]) {
802
fmalita582a6562016-08-22 06:28:57 -0700803 // Ignore the leftmost color/pos.
804 fColors += 1;
805 fPos += 1;
806 fCount = 2;
fmenozzi68d952c2016-08-19 08:56:56 -0700807 }
808 } else if (SkScalarNearlyEqual(pos[0], 0.0f) &&
809 SkScalarNearlyEqual(pos[1], 1.0f) &&
810 SkScalarNearlyEqual(pos[2], 1.0f)) {
811
Mike Reedfae8fce2019-04-03 10:27:45 -0400812 if (SkTileMode::kRepeat == mode || SkTileMode::kMirror == mode ||
fmenozzi68d952c2016-08-19 08:56:56 -0700813 colors[1] == colors[2]) {
814
fmalita582a6562016-08-22 06:28:57 -0700815 // Ignore the rightmost color/pos.
fmenozzi68d952c2016-08-19 08:56:56 -0700816 fCount = 2;
817 }
818 }
819 }
820
brianosmane25d71c2016-09-28 11:27:28 -0700821 const SkColor4f* fColors;
822 const SkScalar* fPos;
823 int fCount;
824};
825
826struct ColorConverter {
827 ColorConverter(const SkColor* colors, int count) {
Brian Osman6667fb12018-07-03 16:44:02 -0400828 const float ONE_OVER_255 = 1.f / 255;
brianosmane25d71c2016-09-28 11:27:28 -0700829 for (int i = 0; i < count; ++i) {
Brian Osman6667fb12018-07-03 16:44:02 -0400830 fColors4f.push_back({
831 SkColorGetR(colors[i]) * ONE_OVER_255,
832 SkColorGetG(colors[i]) * ONE_OVER_255,
833 SkColorGetB(colors[i]) * ONE_OVER_255,
834 SkColorGetA(colors[i]) * ONE_OVER_255 });
brianosmane25d71c2016-09-28 11:27:28 -0700835 }
836 }
837
838 SkSTArray<2, SkColor4f, true> fColors4f;
fmenozzi68d952c2016-08-19 08:56:56 -0700839};
840
reed8a21c9f2016-03-08 18:50:00 -0800841sk_sp<SkShader> SkGradientShader::MakeLinear(const SkPoint pts[2],
fmenozzi68d952c2016-08-19 08:56:56 -0700842 const SkColor colors[],
843 const SkScalar pos[], int colorCount,
Mike Reedfae8fce2019-04-03 10:27:45 -0400844 SkTileMode mode,
fmenozzi68d952c2016-08-19 08:56:56 -0700845 uint32_t flags,
846 const SkMatrix* localMatrix) {
brianosmane25d71c2016-09-28 11:27:28 -0700847 ColorConverter converter(colors, colorCount);
848 return MakeLinear(pts, converter.fColors4f.begin(), nullptr, pos, colorCount, mode, flags,
849 localMatrix);
850}
851
852sk_sp<SkShader> SkGradientShader::MakeLinear(const SkPoint pts[2],
853 const SkColor4f colors[],
854 sk_sp<SkColorSpace> colorSpace,
855 const SkScalar pos[], int colorCount,
Mike Reedfae8fce2019-04-03 10:27:45 -0400856 SkTileMode mode,
brianosmane25d71c2016-09-28 11:27:28 -0700857 uint32_t flags,
858 const SkMatrix* localMatrix) {
fmalitac5231042016-08-10 05:45:50 -0700859 if (!pts || !SkScalarIsFinite((pts[1] - pts[0]).length())) {
halcanary96fcdcc2015-08-27 07:41:13 -0700860 return nullptr;
reed1b747302015-01-06 07:13:19 -0800861 }
862 if (!valid_grad(colors, pos, colorCount, mode)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700863 return nullptr;
rileya@google.com589708b2012-07-26 20:04:23 +0000864 }
fmenozzie9fd0f82016-08-19 07:50:57 -0700865 if (1 == colorCount) {
Mike Reedc8bea7d2019-04-09 13:55:36 -0400866 return SkShaders::Color(colors[0], std::move(colorSpace));
fmenozzie9fd0f82016-08-19 07:50:57 -0700867 }
Florin Malita8d3ffad2017-02-03 18:21:17 +0000868 if (localMatrix && !localMatrix->invert(nullptr)) {
869 return nullptr;
870 }
rileya@google.com589708b2012-07-26 20:04:23 +0000871
Michael Ludwigd431c722018-11-16 10:00:24 -0500872 if (SkScalarNearlyZero((pts[1] - pts[0]).length(), kDegenerateThreshold)) {
Mike Klein024072a2018-11-11 00:26:30 +0000873 // Degenerate gradient, the only tricky complication is when in clamp mode, the limit of
874 // the gradient approaches two half planes of solid color (first and last). However, they
875 // are divided by the line perpendicular to the start and end point, which becomes undefined
876 // once start and end are exactly the same, so just use the end color for a stable solution.
877 return make_degenerate_gradient(colors, pos, colorCount, std::move(colorSpace), mode);
878 }
879
fmenozzi68d952c2016-08-19 08:56:56 -0700880 ColorStopOptimizer opt(colors, pos, colorCount, mode);
881
reed@google.com437d6eb2013-05-23 19:03:05 +0000882 SkGradientShaderBase::Descriptor desc;
brianosmane25d71c2016-09-28 11:27:28 -0700883 desc_init(&desc, opt.fColors, std::move(colorSpace), opt.fPos, opt.fCount, mode, flags,
884 localMatrix);
reed8a21c9f2016-03-08 18:50:00 -0800885 return sk_make_sp<SkLinearGradient>(pts, desc);
rileya@google.com589708b2012-07-26 20:04:23 +0000886}
887
reed8a21c9f2016-03-08 18:50:00 -0800888sk_sp<SkShader> SkGradientShader::MakeRadial(const SkPoint& center, SkScalar radius,
brianosmane25d71c2016-09-28 11:27:28 -0700889 const SkColor colors[],
890 const SkScalar pos[], int colorCount,
Mike Reedfae8fce2019-04-03 10:27:45 -0400891 SkTileMode mode,
brianosmane25d71c2016-09-28 11:27:28 -0700892 uint32_t flags,
893 const SkMatrix* localMatrix) {
894 ColorConverter converter(colors, colorCount);
895 return MakeRadial(center, radius, converter.fColors4f.begin(), nullptr, pos, colorCount, mode,
896 flags, localMatrix);
897}
898
899sk_sp<SkShader> SkGradientShader::MakeRadial(const SkPoint& center, SkScalar radius,
900 const SkColor4f colors[],
901 sk_sp<SkColorSpace> colorSpace,
902 const SkScalar pos[], int colorCount,
Mike Reedfae8fce2019-04-03 10:27:45 -0400903 SkTileMode mode,
brianosmane25d71c2016-09-28 11:27:28 -0700904 uint32_t flags,
905 const SkMatrix* localMatrix) {
Mike Klein024072a2018-11-11 00:26:30 +0000906 if (radius < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700907 return nullptr;
reed1b747302015-01-06 07:13:19 -0800908 }
909 if (!valid_grad(colors, pos, colorCount, mode)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700910 return nullptr;
rileya@google.com589708b2012-07-26 20:04:23 +0000911 }
fmenozzie9fd0f82016-08-19 07:50:57 -0700912 if (1 == colorCount) {
Mike Reedc8bea7d2019-04-09 13:55:36 -0400913 return SkShaders::Color(colors[0], std::move(colorSpace));
fmenozzie9fd0f82016-08-19 07:50:57 -0700914 }
Florin Malita8d3ffad2017-02-03 18:21:17 +0000915 if (localMatrix && !localMatrix->invert(nullptr)) {
916 return nullptr;
917 }
rileya@google.com589708b2012-07-26 20:04:23 +0000918
Michael Ludwigd431c722018-11-16 10:00:24 -0500919 if (SkScalarNearlyZero(radius, kDegenerateThreshold)) {
Mike Klein024072a2018-11-11 00:26:30 +0000920 // Degenerate gradient optimization, and no special logic needed for clamped radial gradient
921 return make_degenerate_gradient(colors, pos, colorCount, std::move(colorSpace), mode);
922 }
923
fmenozzi68d952c2016-08-19 08:56:56 -0700924 ColorStopOptimizer opt(colors, pos, colorCount, mode);
925
reed@google.com437d6eb2013-05-23 19:03:05 +0000926 SkGradientShaderBase::Descriptor desc;
brianosmane25d71c2016-09-28 11:27:28 -0700927 desc_init(&desc, opt.fColors, std::move(colorSpace), opt.fPos, opt.fCount, mode, flags,
928 localMatrix);
reed8a21c9f2016-03-08 18:50:00 -0800929 return sk_make_sp<SkRadialGradient>(center, radius, desc);
rileya@google.com589708b2012-07-26 20:04:23 +0000930}
931
reed8a21c9f2016-03-08 18:50:00 -0800932sk_sp<SkShader> SkGradientShader::MakeTwoPointConical(const SkPoint& start,
brianosmane25d71c2016-09-28 11:27:28 -0700933 SkScalar startRadius,
934 const SkPoint& end,
935 SkScalar endRadius,
936 const SkColor colors[],
937 const SkScalar pos[],
938 int colorCount,
Mike Reedfae8fce2019-04-03 10:27:45 -0400939 SkTileMode mode,
brianosmane25d71c2016-09-28 11:27:28 -0700940 uint32_t flags,
941 const SkMatrix* localMatrix) {
942 ColorConverter converter(colors, colorCount);
943 return MakeTwoPointConical(start, startRadius, end, endRadius, converter.fColors4f.begin(),
944 nullptr, pos, colorCount, mode, flags, localMatrix);
945}
946
947sk_sp<SkShader> SkGradientShader::MakeTwoPointConical(const SkPoint& start,
948 SkScalar startRadius,
949 const SkPoint& end,
950 SkScalar endRadius,
951 const SkColor4f colors[],
952 sk_sp<SkColorSpace> colorSpace,
953 const SkScalar pos[],
954 int colorCount,
Mike Reedfae8fce2019-04-03 10:27:45 -0400955 SkTileMode mode,
brianosmane25d71c2016-09-28 11:27:28 -0700956 uint32_t flags,
957 const SkMatrix* localMatrix) {
reed1b747302015-01-06 07:13:19 -0800958 if (startRadius < 0 || endRadius < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700959 return nullptr;
reed1b747302015-01-06 07:13:19 -0800960 }
961 if (!valid_grad(colors, pos, colorCount, mode)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700962 return nullptr;
rileya@google.com589708b2012-07-26 20:04:23 +0000963 }
Michael Ludwigd431c722018-11-16 10:00:24 -0500964 if (SkScalarNearlyZero((start - end).length(), kDegenerateThreshold)) {
Mike Klein024072a2018-11-11 00:26:30 +0000965 // If the center positions are the same, then the gradient is the radial variant of a 2 pt
966 // conical gradient, an actual radial gradient (startRadius == 0), or it is fully degenerate
967 // (startRadius == endRadius).
Michael Ludwigd431c722018-11-16 10:00:24 -0500968 if (SkScalarNearlyEqual(startRadius, endRadius, kDegenerateThreshold)) {
Mike Klein024072a2018-11-11 00:26:30 +0000969 // Degenerate case, where the interpolation region area approaches zero. The proper
970 // behavior depends on the tile mode, which is consistent with the default degenerate
971 // gradient behavior, except when mode = clamp and the radii > 0.
Mike Reedfae8fce2019-04-03 10:27:45 -0400972 if (mode == SkTileMode::kClamp && endRadius > kDegenerateThreshold) {
Mike Klein024072a2018-11-11 00:26:30 +0000973 // The interpolation region becomes an infinitely thin ring at the radius, so the
974 // final gradient will be the first color repeated from p=0 to 1, and then a hard
975 // stop switching to the last color at p=1.
976 static constexpr SkScalar circlePos[3] = {0, 1, 1};
977 SkColor4f reColors[3] = {colors[0], colors[0], colors[colorCount - 1]};
978 return MakeRadial(start, endRadius, reColors, std::move(colorSpace),
979 circlePos, 3, mode, flags, localMatrix);
980 } else {
981 // Otherwise use the default degenerate case
982 return make_degenerate_gradient(
983 colors, pos, colorCount, std::move(colorSpace), mode);
984 }
Michael Ludwigd431c722018-11-16 10:00:24 -0500985 } else if (SkScalarNearlyZero(startRadius, kDegenerateThreshold)) {
Mike Klein024072a2018-11-11 00:26:30 +0000986 // We can treat this gradient as radial, which is faster. If we got here, we know
987 // that endRadius is not equal to 0, so this produces a meaningful gradient
988 return MakeRadial(start, endRadius, colors, std::move(colorSpace), pos, colorCount,
989 mode, flags, localMatrix);
Brian Osman2dfab272018-11-06 00:41:40 +0000990 }
Mike Klein024072a2018-11-11 00:26:30 +0000991 // Else it's the 2pt conical radial variant with no degenerate radii, so fall through to the
992 // regular 2pt constructor.
Brian Osman2dfab272018-11-06 00:41:40 +0000993 }
Mike Klein024072a2018-11-11 00:26:30 +0000994
Florin Malita8d3ffad2017-02-03 18:21:17 +0000995 if (localMatrix && !localMatrix->invert(nullptr)) {
996 return nullptr;
997 }
reed6b7a6c72016-08-18 16:13:50 -0700998 EXPAND_1_COLOR(colorCount);
rileya@google.com589708b2012-07-26 20:04:23 +0000999
fmenozzi68d952c2016-08-19 08:56:56 -07001000 ColorStopOptimizer opt(colors, pos, colorCount, mode);
1001
reed@google.com437d6eb2013-05-23 19:03:05 +00001002 SkGradientShaderBase::Descriptor desc;
Florin Malita5f379a82017-10-18 16:22:35 -04001003 desc_init(&desc, opt.fColors, std::move(colorSpace), opt.fPos, opt.fCount, mode, flags,
1004 localMatrix);
1005 return SkTwoPointConicalGradient::Create(start, startRadius, end, endRadius, desc);
rileya@google.com589708b2012-07-26 20:04:23 +00001006}
1007
reed8a21c9f2016-03-08 18:50:00 -08001008sk_sp<SkShader> SkGradientShader::MakeSweep(SkScalar cx, SkScalar cy,
brianosmane25d71c2016-09-28 11:27:28 -07001009 const SkColor colors[],
1010 const SkScalar pos[],
1011 int colorCount,
Mike Reedfae8fce2019-04-03 10:27:45 -04001012 SkTileMode mode,
Florin Malita5a9a9812017-08-01 16:38:08 -04001013 SkScalar startAngle,
1014 SkScalar endAngle,
brianosmane25d71c2016-09-28 11:27:28 -07001015 uint32_t flags,
1016 const SkMatrix* localMatrix) {
1017 ColorConverter converter(colors, colorCount);
Florin Malita5a9a9812017-08-01 16:38:08 -04001018 return MakeSweep(cx, cy, converter.fColors4f.begin(), nullptr, pos, colorCount,
1019 mode, startAngle, endAngle, flags, localMatrix);
brianosmane25d71c2016-09-28 11:27:28 -07001020}
1021
1022sk_sp<SkShader> SkGradientShader::MakeSweep(SkScalar cx, SkScalar cy,
1023 const SkColor4f colors[],
1024 sk_sp<SkColorSpace> colorSpace,
1025 const SkScalar pos[],
1026 int colorCount,
Mike Reedfae8fce2019-04-03 10:27:45 -04001027 SkTileMode mode,
Florin Malita5a9a9812017-08-01 16:38:08 -04001028 SkScalar startAngle,
1029 SkScalar endAngle,
brianosmane25d71c2016-09-28 11:27:28 -07001030 uint32_t flags,
1031 const SkMatrix* localMatrix) {
Florin Malita5a9a9812017-08-01 16:38:08 -04001032 if (!valid_grad(colors, pos, colorCount, mode)) {
halcanary96fcdcc2015-08-27 07:41:13 -07001033 return nullptr;
rileya@google.com589708b2012-07-26 20:04:23 +00001034 }
fmenozzie9fd0f82016-08-19 07:50:57 -07001035 if (1 == colorCount) {
Mike Reedc8bea7d2019-04-09 13:55:36 -04001036 return SkShaders::Color(colors[0], std::move(colorSpace));
fmenozzie9fd0f82016-08-19 07:50:57 -07001037 }
Mike Klein024072a2018-11-11 00:26:30 +00001038 if (!SkScalarIsFinite(startAngle) || !SkScalarIsFinite(endAngle) || startAngle > endAngle) {
Florin Malita5a9a9812017-08-01 16:38:08 -04001039 return nullptr;
1040 }
Florin Malita8d3ffad2017-02-03 18:21:17 +00001041 if (localMatrix && !localMatrix->invert(nullptr)) {
1042 return nullptr;
1043 }
rileya@google.com589708b2012-07-26 20:04:23 +00001044
Michael Ludwigd431c722018-11-16 10:00:24 -05001045 if (SkScalarNearlyEqual(startAngle, endAngle, kDegenerateThreshold)) {
Mike Klein024072a2018-11-11 00:26:30 +00001046 // Degenerate gradient, which should follow default degenerate behavior unless it is
1047 // clamped and the angle is greater than 0.
Mike Reedfae8fce2019-04-03 10:27:45 -04001048 if (mode == SkTileMode::kClamp && endAngle > kDegenerateThreshold) {
Mike Klein024072a2018-11-11 00:26:30 +00001049 // In this case, the first color is repeated from 0 to the angle, then a hardstop
1050 // switches to the last color (all other colors are compressed to the infinitely thin
1051 // interpolation region).
1052 static constexpr SkScalar clampPos[3] = {0, 1, 1};
1053 SkColor4f reColors[3] = {colors[0], colors[0], colors[colorCount - 1]};
1054 return MakeSweep(cx, cy, reColors, std::move(colorSpace), clampPos, 3, mode, 0,
1055 endAngle, flags, localMatrix);
1056 } else {
1057 return make_degenerate_gradient(colors, pos, colorCount, std::move(colorSpace), mode);
1058 }
1059 }
1060
Florin Malita5a9a9812017-08-01 16:38:08 -04001061 if (startAngle <= 0 && endAngle >= 360) {
1062 // If the t-range includes [0,1], then we can always use clamping (presumably faster).
Mike Reedfae8fce2019-04-03 10:27:45 -04001063 mode = SkTileMode::kClamp;
Florin Malita5a9a9812017-08-01 16:38:08 -04001064 }
fmenozzi68d952c2016-08-19 08:56:56 -07001065
1066 ColorStopOptimizer opt(colors, pos, colorCount, mode);
1067
reed@google.com437d6eb2013-05-23 19:03:05 +00001068 SkGradientShaderBase::Descriptor desc;
brianosmane25d71c2016-09-28 11:27:28 -07001069 desc_init(&desc, opt.fColors, std::move(colorSpace), opt.fPos, opt.fCount, mode, flags,
1070 localMatrix);
Florin Malita5a9a9812017-08-01 16:38:08 -04001071
1072 const SkScalar t0 = startAngle / 360,
1073 t1 = endAngle / 360;
1074
1075 return sk_make_sp<SkSweepGradient>(SkPoint::Make(cx, cy), t0, t1, desc);
rileya@google.com589708b2012-07-26 20:04:23 +00001076}
1077
Mike Kleinfa5f6ce2018-10-20 08:21:31 -04001078void SkGradientShader::RegisterFlattenables() {
Brian Salomon23356442018-11-30 15:33:19 -05001079 SK_REGISTER_FLATTENABLE(SkLinearGradient);
1080 SK_REGISTER_FLATTENABLE(SkRadialGradient);
1081 SK_REGISTER_FLATTENABLE(SkSweepGradient);
1082 SK_REGISTER_FLATTENABLE(SkTwoPointConicalGradient);
Mike Klein12956722018-10-19 10:00:21 -04001083}