blob: c1d5f0cf495fe22bceb50b50630c87d62fad5dcd [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>
fmalitabc590c02016-02-22 09:12:33 -08009#include "Sk4fLinearGradient.h"
Mike Kleine28a6b52018-07-25 13:05:17 -040010#include "SkColorSpacePriv.h"
Florin Malita39d71de2017-10-31 11:33:49 -040011#include "SkColorSpaceXformer.h"
Brian Salomon5dfcf132018-10-12 14:39:32 +000012#include "SkConvertPixels.h"
Florin Malitacad3b8c2017-10-28 21:42:50 -040013#include "SkFloatBits.h"
rileya@google.com589708b2012-07-26 20:04:23 +000014#include "SkGradientShaderPriv.h"
brianosmand4546092016-09-22 12:31:58 -070015#include "SkHalf.h"
rileya@google.com589708b2012-07-26 20:04:23 +000016#include "SkLinearGradient.h"
Mike Reed6b3155c2017-04-03 14:41:44 -040017#include "SkMallocPixelRef.h"
rileya@google.com589708b2012-07-26 20:04:23 +000018#include "SkRadialGradient.h"
Florin Malitad4e9ec82017-10-25 18:00:26 -040019#include "SkReadBuffer.h"
Mike Klein02ab8cc2017-05-04 22:41:05 +000020#include "SkSweepGradient.h"
Mike Kleina3771842017-05-04 19:38:48 -040021#include "SkTwoPointConicalGradient.h"
Florin Malitad4e9ec82017-10-25 18:00:26 -040022#include "SkWriteBuffer.h"
rileya@google.com589708b2012-07-26 20:04:23 +000023
brianosmane25d71c2016-09-28 11:27:28 -070024enum GradientSerializationFlags {
25 // Bits 29:31 used for various boolean flags
26 kHasPosition_GSF = 0x80000000,
27 kHasLocalMatrix_GSF = 0x40000000,
28 kHasColorSpace_GSF = 0x20000000,
29
30 // Bits 12:28 unused
31
32 // Bits 8:11 for fTileMode
33 kTileModeShift_GSF = 8,
34 kTileModeMask_GSF = 0xF,
35
36 // Bits 0:7 for fGradFlags (note that kForce4fContext_PrivateFlag is 0x80)
37 kGradFlagsShift_GSF = 0,
38 kGradFlagsMask_GSF = 0xFF,
39};
40
reed9fa60da2014-08-21 07:59:51 -070041void SkGradientShaderBase::Descriptor::flatten(SkWriteBuffer& buffer) const {
brianosmane25d71c2016-09-28 11:27:28 -070042 uint32_t flags = 0;
reed9fa60da2014-08-21 07:59:51 -070043 if (fPos) {
brianosmane25d71c2016-09-28 11:27:28 -070044 flags |= kHasPosition_GSF;
reed9fa60da2014-08-21 07:59:51 -070045 }
reed9fa60da2014-08-21 07:59:51 -070046 if (fLocalMatrix) {
brianosmane25d71c2016-09-28 11:27:28 -070047 flags |= kHasLocalMatrix_GSF;
48 }
49 sk_sp<SkData> colorSpaceData = fColorSpace ? fColorSpace->serialize() : nullptr;
50 if (colorSpaceData) {
51 flags |= kHasColorSpace_GSF;
52 }
53 SkASSERT(static_cast<uint32_t>(fTileMode) <= kTileModeMask_GSF);
54 flags |= (fTileMode << kTileModeShift_GSF);
55 SkASSERT(fGradFlags <= kGradFlagsMask_GSF);
56 flags |= (fGradFlags << kGradFlagsShift_GSF);
57
58 buffer.writeUInt(flags);
59
60 buffer.writeColor4fArray(fColors, fCount);
61 if (colorSpaceData) {
62 buffer.writeDataAsByteArray(colorSpaceData.get());
63 }
64 if (fPos) {
65 buffer.writeScalarArray(fPos, fCount);
66 }
67 if (fLocalMatrix) {
reed9fa60da2014-08-21 07:59:51 -070068 buffer.writeMatrix(*fLocalMatrix);
reed9fa60da2014-08-21 07:59:51 -070069 }
70}
71
Florin Malitaf77db112018-05-10 09:52:27 -040072template <int N, typename T, bool MEM_MOVE>
73static bool validate_array(SkReadBuffer& buffer, size_t count, SkSTArray<N, T, MEM_MOVE>* array) {
Kevin Lubickdaebae92018-05-17 11:29:10 -040074 if (!buffer.validateCanReadN<T>(count)) {
Florin Malitaf77db112018-05-10 09:52:27 -040075 return false;
76 }
77
78 array->resize_back(count);
79 return true;
80}
81
reed9fa60da2014-08-21 07:59:51 -070082bool SkGradientShaderBase::DescriptorScope::unflatten(SkReadBuffer& buffer) {
Mike Reed70bc94f2017-06-08 12:45:52 -040083 // New gradient format. Includes floating point color, color space, densely packed flags
84 uint32_t flags = buffer.readUInt();
reed9fa60da2014-08-21 07:59:51 -070085
Mike Reed70bc94f2017-06-08 12:45:52 -040086 fTileMode = (SkShader::TileMode)((flags >> kTileModeShift_GSF) & kTileModeMask_GSF);
87 fGradFlags = (flags >> kGradFlagsShift_GSF) & kGradFlagsMask_GSF;
reed9fa60da2014-08-21 07:59:51 -070088
Mike Reed70bc94f2017-06-08 12:45:52 -040089 fCount = buffer.getArrayCount();
Florin Malitaf77db112018-05-10 09:52:27 -040090
91 if (!(validate_array(buffer, fCount, &fColorStorage) &&
92 buffer.readColor4fArray(fColorStorage.begin(), fCount))) {
Mike Reed70bc94f2017-06-08 12:45:52 -040093 return false;
94 }
Florin Malitaf77db112018-05-10 09:52:27 -040095 fColors = fColorStorage.begin();
96
Mike Reed70bc94f2017-06-08 12:45:52 -040097 if (SkToBool(flags & kHasColorSpace_GSF)) {
98 sk_sp<SkData> data = buffer.readByteArrayAsData();
Florin Malitac2ea3272018-05-10 09:41:38 -040099 fColorSpace = data ? SkColorSpace::Deserialize(data->data(), data->size()) : nullptr;
Mike Reed70bc94f2017-06-08 12:45:52 -0400100 } else {
brianosmane25d71c2016-09-28 11:27:28 -0700101 fColorSpace = nullptr;
Mike Reed70bc94f2017-06-08 12:45:52 -0400102 }
103 if (SkToBool(flags & kHasPosition_GSF)) {
Florin Malitaf77db112018-05-10 09:52:27 -0400104 if (!(validate_array(buffer, fCount, &fPosStorage) &&
105 buffer.readScalarArray(fPosStorage.begin(), fCount))) {
Mike Reed70bc94f2017-06-08 12:45:52 -0400106 return false;
brianosmane25d71c2016-09-28 11:27:28 -0700107 }
Florin Malitaf77db112018-05-10 09:52:27 -0400108 fPos = fPosStorage.begin();
reed9fa60da2014-08-21 07:59:51 -0700109 } else {
Mike Reed70bc94f2017-06-08 12:45:52 -0400110 fPos = nullptr;
111 }
112 if (SkToBool(flags & kHasLocalMatrix_GSF)) {
113 fLocalMatrix = &fLocalMatrixStorage;
114 buffer.readMatrix(&fLocalMatrixStorage);
115 } else {
116 fLocalMatrix = nullptr;
reed9fa60da2014-08-21 07:59:51 -0700117 }
118 return buffer.isValid();
119}
120
121////////////////////////////////////////////////////////////////////////////////////////////
122
mtkleincc695fe2014-12-10 10:29:19 -0800123SkGradientShaderBase::SkGradientShaderBase(const Descriptor& desc, const SkMatrix& ptsToUnit)
reedaddf2ed2014-08-11 08:28:24 -0700124 : INHERITED(desc.fLocalMatrix)
mtkleincc695fe2014-12-10 10:29:19 -0800125 , fPtsToUnit(ptsToUnit)
Brian Osman6667fb12018-07-03 16:44:02 -0400126 , fColorSpace(desc.fColorSpace ? desc.fColorSpace : SkColorSpace::MakeSRGB())
Florin Malita39d71de2017-10-31 11:33:49 -0400127 , fColorsAreOpaque(true)
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000128{
mtkleincc695fe2014-12-10 10:29:19 -0800129 fPtsToUnit.getType(); // Precache so reads are threadsafe.
reed@google.com437d6eb2013-05-23 19:03:05 +0000130 SkASSERT(desc.fCount > 1);
rileya@google.com589708b2012-07-26 20:04:23 +0000131
fmalita6d7e4e82016-09-20 06:55:16 -0700132 fGradFlags = static_cast<uint8_t>(desc.fGradFlags);
rileya@google.com589708b2012-07-26 20:04:23 +0000133
reed@google.com437d6eb2013-05-23 19:03:05 +0000134 SkASSERT((unsigned)desc.fTileMode < SkShader::kTileModeCount);
reed@google.com437d6eb2013-05-23 19:03:05 +0000135 fTileMode = desc.fTileMode;
rileya@google.com589708b2012-07-26 20:04:23 +0000136
rileya@google.com589708b2012-07-26 20:04:23 +0000137 /* Note: we let the caller skip the first and/or last position.
138 i.e. pos[0] = 0.3, pos[1] = 0.7
139 In these cases, we insert dummy entries to ensure that the final data
140 will be bracketed by [0, 1].
141 i.e. our_pos[0] = 0, our_pos[1] = 0.3, our_pos[2] = 0.7, our_pos[3] = 1
142
143 Thus colorCount (the caller's value, and fColorCount (our value) may
144 differ by up to 2. In the above example:
145 colorCount = 2
146 fColorCount = 4
147 */
reed@google.com437d6eb2013-05-23 19:03:05 +0000148 fColorCount = desc.fCount;
rileya@google.com589708b2012-07-26 20:04:23 +0000149 // check if we need to add in dummy start and/or end position/colors
150 bool dummyFirst = false;
151 bool dummyLast = false;
reed@google.com437d6eb2013-05-23 19:03:05 +0000152 if (desc.fPos) {
153 dummyFirst = desc.fPos[0] != 0;
154 dummyLast = desc.fPos[desc.fCount - 1] != SK_Scalar1;
rileya@google.com589708b2012-07-26 20:04:23 +0000155 fColorCount += dummyFirst + dummyLast;
156 }
157
Mike Reed62ce2ca2018-02-19 14:20:15 -0500158 size_t storageSize = fColorCount * (sizeof(SkColor4f) + (desc.fPos ? sizeof(SkScalar) : 0));
Florin Malita89ab2402017-11-01 10:14:57 -0400159 fOrigColors4f = reinterpret_cast<SkColor4f*>(fStorage.reset(storageSize));
Mike Reed62ce2ca2018-02-19 14:20:15 -0500160 fOrigPos = desc.fPos ? reinterpret_cast<SkScalar*>(fOrigColors4f + fColorCount)
161 : nullptr;
rileya@google.com589708b2012-07-26 20:04:23 +0000162
brianosmane25d71c2016-09-28 11:27:28 -0700163 // Now copy over the colors, adding the dummies as needed
164 SkColor4f* origColors = fOrigColors4f;
165 if (dummyFirst) {
166 *origColors++ = desc.fColors[0];
167 }
Florin Malita39d71de2017-10-31 11:33:49 -0400168 for (int i = 0; i < desc.fCount; ++i) {
Mike Reed62ce2ca2018-02-19 14:20:15 -0500169 origColors[i] = desc.fColors[i];
Florin Malita39d71de2017-10-31 11:33:49 -0400170 fColorsAreOpaque = fColorsAreOpaque && (desc.fColors[i].fA == 1);
171 }
brianosmane25d71c2016-09-28 11:27:28 -0700172 if (dummyLast) {
Mike Reed62ce2ca2018-02-19 14:20:15 -0500173 origColors += desc.fCount;
174 *origColors = desc.fColors[desc.fCount - 1];
brianosmane25d71c2016-09-28 11:27:28 -0700175 }
brianosmanb9c51372016-09-15 11:09:45 -0700176
Florin Malita89ab2402017-11-01 10:14:57 -0400177 if (desc.fPos) {
Florin Malita64bb78e2017-11-03 12:54:07 -0400178 SkScalar prev = 0;
Mike Reed62ce2ca2018-02-19 14:20:15 -0500179 SkScalar* origPosPtr = fOrigPos;
Florin Malita64bb78e2017-11-03 12:54:07 -0400180 *origPosPtr++ = prev; // force the first pos to 0
reed9fa60da2014-08-21 07:59:51 -0700181
Florin Malita89ab2402017-11-01 10:14:57 -0400182 int startIndex = dummyFirst ? 0 : 1;
183 int count = desc.fCount + dummyLast;
Florin Malita64bb78e2017-11-03 12:54:07 -0400184
185 bool uniformStops = true;
186 const SkScalar uniformStep = desc.fPos[startIndex] - prev;
Florin Malita89ab2402017-11-01 10:14:57 -0400187 for (int i = startIndex; i < count; i++) {
Florin Malita3e20d022017-11-03 12:11:38 -0400188 // Pin the last value to 1.0, and make sure pos is monotonic.
Florin Malita64bb78e2017-11-03 12:54:07 -0400189 auto curr = (i == desc.fCount) ? 1 : SkScalarPin(desc.fPos[i], prev, 1);
190 uniformStops &= SkScalarNearlyEqual(uniformStep, curr - prev);
191
192 *origPosPtr++ = prev = curr;
reed9fa60da2014-08-21 07:59:51 -0700193 }
Florin Malita64bb78e2017-11-03 12:54:07 -0400194
Florin Malita64bb78e2017-11-03 12:54:07 -0400195 // If the stops are uniform, treat them as implicit.
Mike Reed62ce2ca2018-02-19 14:20:15 -0500196 if (uniformStops) {
Florin Malita64bb78e2017-11-03 12:54:07 -0400197 fOrigPos = nullptr;
198 }
rileya@google.com589708b2012-07-26 20:04:23 +0000199 }
rileya@google.com589708b2012-07-26 20:04:23 +0000200}
201
Florin Malita89ab2402017-11-01 10:14:57 -0400202SkGradientShaderBase::~SkGradientShaderBase() {}
rileya@google.com589708b2012-07-26 20:04:23 +0000203
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000204void SkGradientShaderBase::flatten(SkWriteBuffer& buffer) const {
reed9fa60da2014-08-21 07:59:51 -0700205 Descriptor desc;
brianosmane25d71c2016-09-28 11:27:28 -0700206 desc.fColors = fOrigColors4f;
brianosmanb9c51372016-09-15 11:09:45 -0700207 desc.fColorSpace = fColorSpace;
reed9fa60da2014-08-21 07:59:51 -0700208 desc.fPos = fOrigPos;
209 desc.fCount = fColorCount;
210 desc.fTileMode = fTileMode;
211 desc.fGradFlags = fGradFlags;
212
213 const SkMatrix& m = this->getLocalMatrix();
halcanary96fcdcc2015-08-27 07:41:13 -0700214 desc.fLocalMatrix = m.isIdentity() ? nullptr : &m;
reed9fa60da2014-08-21 07:59:51 -0700215 desc.flatten(buffer);
rileya@google.com589708b2012-07-26 20:04:23 +0000216}
217
Mike Kleinb11ab572018-10-24 06:42:14 -0400218static void add_stop_color(SkRasterPipeline_GradientCtx* ctx, size_t stop, SkPMColor4f Fs, SkPMColor4f Bs) {
Brian Osman781e3502018-10-03 15:42:47 -0400219 (ctx->fs[0])[stop] = Fs.fR;
220 (ctx->fs[1])[stop] = Fs.fG;
221 (ctx->fs[2])[stop] = Fs.fB;
222 (ctx->fs[3])[stop] = Fs.fA;
223 (ctx->bs[0])[stop] = Bs.fR;
224 (ctx->bs[1])[stop] = Bs.fG;
225 (ctx->bs[2])[stop] = Bs.fB;
226 (ctx->bs[3])[stop] = Bs.fA;
Mike Kleinf945cbb2017-05-17 09:30:58 -0400227}
Herb Derbyeb99bfd2017-05-16 10:51:26 -0400228
Mike Kleinb11ab572018-10-24 06:42:14 -0400229static void add_const_color(SkRasterPipeline_GradientCtx* ctx, size_t stop, SkPMColor4f color) {
Brian Osman781e3502018-10-03 15:42:47 -0400230 add_stop_color(ctx, stop, { 0, 0, 0, 0 }, color);
Mike Kleinf945cbb2017-05-17 09:30:58 -0400231}
Herb Derbyeb99bfd2017-05-16 10:51:26 -0400232
233// Calculate a factor F and a bias B so that color = F*t + B when t is in range of
234// the stop. Assume that the distance between stops is 1/gapCount.
235static void init_stop_evenly(
Mike Kleinb11ab572018-10-24 06:42:14 -0400236 SkRasterPipeline_GradientCtx* ctx, float gapCount, size_t stop, SkPMColor4f c_l, SkPMColor4f c_r) {
Mike Klein68768172017-05-17 09:54:36 -0400237 // 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 -0400238 SkPMColor4f Fs = {
239 (c_r.fR - c_l.fR) * gapCount,
240 (c_r.fG - c_l.fG) * gapCount,
241 (c_r.fB - c_l.fB) * gapCount,
242 (c_r.fA - c_l.fA) * gapCount,
243 };
244 SkPMColor4f Bs = {
245 c_l.fR - Fs.fR*(stop/gapCount),
246 c_l.fG - Fs.fG*(stop/gapCount),
247 c_l.fB - Fs.fB*(stop/gapCount),
248 c_l.fA - Fs.fA*(stop/gapCount),
249 };
Herb Derbyeb99bfd2017-05-16 10:51:26 -0400250 add_stop_color(ctx, stop, Fs, Bs);
Mike Kleinf945cbb2017-05-17 09:30:58 -0400251}
Herb Derbyeb99bfd2017-05-16 10:51:26 -0400252
253// For each stop we calculate a bias B and a scale factor F, such that
254// for any t between stops n and n+1, the color we want is B[n] + F[n]*t.
255static void init_stop_pos(
Mike Kleinb11ab572018-10-24 06:42:14 -0400256 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 -0400257 // See note about Clankium's old compiler in init_stop_evenly().
Brian Osman781e3502018-10-03 15:42:47 -0400258 SkPMColor4f Fs = {
259 (c_r.fR - c_l.fR) / (t_r - t_l),
260 (c_r.fG - c_l.fG) / (t_r - t_l),
261 (c_r.fB - c_l.fB) / (t_r - t_l),
262 (c_r.fA - c_l.fA) / (t_r - t_l),
263 };
264 SkPMColor4f Bs = {
265 c_l.fR - Fs.fR*t_l,
266 c_l.fG - Fs.fG*t_l,
267 c_l.fB - Fs.fB*t_l,
268 c_l.fA - Fs.fA*t_l,
269 };
Herb Derbyeb99bfd2017-05-16 10:51:26 -0400270 ctx->ts[stop] = t_l;
271 add_stop_color(ctx, stop, Fs, Bs);
Mike Kleinf945cbb2017-05-17 09:30:58 -0400272}
Herb Derbyeb99bfd2017-05-16 10:51:26 -0400273
Mike Reed1d8c42e2017-08-29 14:58:19 -0400274bool SkGradientShaderBase::onAppendStages(const StageRec& rec) const {
275 SkRasterPipeline* p = rec.fPipeline;
276 SkArenaAlloc* alloc = rec.fAlloc;
Mike Kleinb11ab572018-10-24 06:42:14 -0400277 SkRasterPipeline_DecalTileCtx* decal_ctx = nullptr;
Mike Reed1d8c42e2017-08-29 14:58:19 -0400278
Mike Kleina3771842017-05-04 19:38:48 -0400279 SkMatrix matrix;
Mike Reed1d8c42e2017-08-29 14:58:19 -0400280 if (!this->computeTotalInverse(rec.fCTM, rec.fLocalM, &matrix)) {
Mike Kleina3771842017-05-04 19:38:48 -0400281 return false;
282 }
Florin Malita50b20842017-07-29 19:08:28 -0400283 matrix.postConcat(fPtsToUnit);
Mike Kleina3771842017-05-04 19:38:48 -0400284
Florin Malita2e409002017-06-28 14:46:54 -0400285 SkRasterPipeline_<256> postPipeline;
Mike Kleina3771842017-05-04 19:38:48 -0400286
Mike Kleine8de0242018-03-10 12:37:11 -0500287 p->append(SkRasterPipeline::seed_shader);
Mike Reed6b59bf42017-07-03 21:26:44 -0400288 p->append_matrix(alloc, matrix);
Florin Malita50b20842017-07-29 19:08:28 -0400289 this->appendGradientStages(alloc, p, &postPipeline);
Mike Kleine7598532017-05-11 11:29:29 -0400290
Mike Reed62ce2ca2018-02-19 14:20:15 -0500291 switch(fTileMode) {
Mike Klein9f85d682017-05-23 07:52:01 -0400292 case kMirror_TileMode: p->append(SkRasterPipeline::mirror_x_1); break;
293 case kRepeat_TileMode: p->append(SkRasterPipeline::repeat_x_1); break;
Mike Reeddfc0e912018-02-16 12:40:18 -0500294 case kDecal_TileMode:
Mike Kleinb11ab572018-10-24 06:42:14 -0400295 decal_ctx = alloc->make<SkRasterPipeline_DecalTileCtx>();
Mike Reed62ce2ca2018-02-19 14:20:15 -0500296 decal_ctx->limit_x = SkBits2Float(SkFloat2Bits(1.0f) + 1);
297 // reuse mask + limit_x stage, or create a custom decal_1 that just stores the mask
298 p->append(SkRasterPipeline::decal_x, decal_ctx);
299 // fall-through to clamp
Mike Kleine7598532017-05-11 11:29:29 -0400300 case kClamp_TileMode:
301 if (!fOrigPos) {
302 // We clamp only when the stops are evenly spaced.
303 // If not, there may be hard stops, and clamping ruins hard stops at 0 and/or 1.
Mike Klein5c7960b2017-05-11 10:59:22 -0400304 // In that case, we must make sure we're using the general "gradient" stage,
Mike Kleine7598532017-05-11 11:29:29 -0400305 // which is the only stage that will correctly handle unclamped t.
Mike Klein9f85d682017-05-23 07:52:01 -0400306 p->append(SkRasterPipeline::clamp_x_1);
Mike Kleine7598532017-05-11 11:29:29 -0400307 }
Mike Reed62ce2ca2018-02-19 14:20:15 -0500308 break;
Mike Kleine7598532017-05-11 11:29:29 -0400309 }
Mike Kleina3771842017-05-04 19:38:48 -0400310
311 const bool premulGrad = fGradFlags & SkGradientShader::kInterpolateColorsInPremul_Flag;
Brian Osman6667fb12018-07-03 16:44:02 -0400312
313 // Transform all of the colors to destination color space
314 SkColor4fXformer xformedColors(fOrigColors4f, fColorCount, fColorSpace.get(), rec.fDstCS);
315
316 auto prepareColor = [premulGrad, &xformedColors](int i) {
317 SkColor4f c = xformedColors.fColors[i];
Brian Osman781e3502018-10-03 15:42:47 -0400318 return premulGrad ? c.premul()
319 : SkPMColor4f{ c.fR, c.fG, c.fB, c.fA };
Mike Kleina3771842017-05-04 19:38:48 -0400320 };
321
322 // The two-stop case with stops at 0 and 1.
323 if (fColorCount == 2 && fOrigPos == nullptr) {
Brian Osman781e3502018-10-03 15:42:47 -0400324 const SkPMColor4f c_l = prepareColor(0),
325 c_r = prepareColor(1);
Mike Kleina3771842017-05-04 19:38:48 -0400326
327 // See F and B below.
Mike Kleinb11ab572018-10-24 06:42:14 -0400328 auto ctx = alloc->make<SkRasterPipeline_EvenlySpaced2StopGradientCtx>();
Brian Osman781e3502018-10-03 15:42:47 -0400329 (Sk4f::Load(c_r.vec()) - Sk4f::Load(c_l.vec())).store(ctx->f);
330 ( Sk4f::Load(c_l.vec())).store(ctx->b);
Mike Klein24de6482018-09-07 12:05:29 -0400331 ctx->interpolatedInPremul = premulGrad;
Mike Kleina3771842017-05-04 19:38:48 -0400332
Mike Klein24de6482018-09-07 12:05:29 -0400333 p->append(SkRasterPipeline::evenly_spaced_2_stop_gradient, ctx);
Mike Kleina3771842017-05-04 19:38:48 -0400334 } else {
Mike Kleinb11ab572018-10-24 06:42:14 -0400335 auto* ctx = alloc->make<SkRasterPipeline_GradientCtx>();
Mike Klein24de6482018-09-07 12:05:29 -0400336 ctx->interpolatedInPremul = premulGrad;
Herb Derby4de13042017-05-15 10:49:39 -0400337
338 // Note: In order to handle clamps in search, the search assumes a stop conceptully placed
339 // at -inf. Therefore, the max number of stops is fColorCount+1.
340 for (int i = 0; i < 4; i++) {
341 // Allocate at least at for the AVX2 gather from a YMM register.
342 ctx->fs[i] = alloc->makeArray<float>(std::max(fColorCount+1, 8));
343 ctx->bs[i] = alloc->makeArray<float>(std::max(fColorCount+1, 8));
344 }
345
Mike Kleina3771842017-05-04 19:38:48 -0400346 if (fOrigPos == nullptr) {
347 // Handle evenly distributed stops.
348
Herb Derby4de13042017-05-15 10:49:39 -0400349 size_t stopCount = fColorCount;
350 float gapCount = stopCount - 1;
Mike Kleina3771842017-05-04 19:38:48 -0400351
Brian Osman781e3502018-10-03 15:42:47 -0400352 SkPMColor4f c_l = prepareColor(0);
Herb Derby4de13042017-05-15 10:49:39 -0400353 for (size_t i = 0; i < stopCount - 1; i++) {
Brian Osman781e3502018-10-03 15:42:47 -0400354 SkPMColor4f c_r = prepareColor(i + 1);
Herb Derbyeb99bfd2017-05-16 10:51:26 -0400355 init_stop_evenly(ctx, gapCount, i, c_l, c_r);
Mike Kleina3771842017-05-04 19:38:48 -0400356 c_l = c_r;
357 }
Herb Derbyeb99bfd2017-05-16 10:51:26 -0400358 add_const_color(ctx, stopCount - 1, c_l);
Mike Kleina3771842017-05-04 19:38:48 -0400359
Herb Derby4de13042017-05-15 10:49:39 -0400360 ctx->stopCount = stopCount;
361 p->append(SkRasterPipeline::evenly_spaced_gradient, ctx);
Mike Kleina3771842017-05-04 19:38:48 -0400362 } else {
363 // Handle arbitrary stops.
364
Herb Derby4de13042017-05-15 10:49:39 -0400365 ctx->ts = alloc->makeArray<float>(fColorCount+1);
366
Mike Kleina3771842017-05-04 19:38:48 -0400367 // Remove the dummy stops inserted by SkGradientShaderBase::SkGradientShaderBase
368 // because they are naturally handled by the search method.
369 int firstStop;
370 int lastStop;
371 if (fColorCount > 2) {
372 firstStop = fOrigColors4f[0] != fOrigColors4f[1] ? 0 : 1;
373 lastStop = fOrigColors4f[fColorCount - 2] != fOrigColors4f[fColorCount - 1]
374 ? fColorCount - 1 : fColorCount - 2;
375 } else {
376 firstStop = 0;
377 lastStop = 1;
378 }
Mike Kleina3771842017-05-04 19:38:48 -0400379
Mike Kleina3771842017-05-04 19:38:48 -0400380 size_t stopCount = 0;
381 float t_l = fOrigPos[firstStop];
Brian Osman781e3502018-10-03 15:42:47 -0400382 SkPMColor4f c_l = prepareColor(firstStop);
Herb Derbyeb99bfd2017-05-16 10:51:26 -0400383 add_const_color(ctx, stopCount++, c_l);
Mike Kleina3771842017-05-04 19:38:48 -0400384 // N.B. lastStop is the index of the last stop, not one after.
385 for (int i = firstStop; i < lastStop; i++) {
386 float t_r = fOrigPos[i + 1];
Brian Osman781e3502018-10-03 15:42:47 -0400387 SkPMColor4f c_r = prepareColor(i + 1);
Florin Malita3e20d022017-11-03 12:11:38 -0400388 SkASSERT(t_l <= t_r);
Mike Kleina3771842017-05-04 19:38:48 -0400389 if (t_l < t_r) {
Herb Derbyeb99bfd2017-05-16 10:51:26 -0400390 init_stop_pos(ctx, stopCount, t_l, t_r, c_l, c_r);
Mike Kleina3771842017-05-04 19:38:48 -0400391 stopCount += 1;
392 }
393 t_l = t_r;
394 c_l = c_r;
395 }
396
Herb Derby4de13042017-05-15 10:49:39 -0400397 ctx->ts[stopCount] = t_l;
Herb Derbyeb99bfd2017-05-16 10:51:26 -0400398 add_const_color(ctx, stopCount++, c_l);
Mike Kleina3771842017-05-04 19:38:48 -0400399
Herb Derby4de13042017-05-15 10:49:39 -0400400 ctx->stopCount = stopCount;
401 p->append(SkRasterPipeline::gradient, ctx);
Mike Kleina3771842017-05-04 19:38:48 -0400402 }
Mike Kleina3771842017-05-04 19:38:48 -0400403 }
404
Mike Reed62ce2ca2018-02-19 14:20:15 -0500405 if (decal_ctx) {
406 p->append(SkRasterPipeline::check_decal_mask, decal_ctx);
407 }
408
Mike Kleina3771842017-05-04 19:38:48 -0400409 if (!premulGrad && !this->colorsAreOpaque()) {
Mike Kleine7598532017-05-11 11:29:29 -0400410 p->append(SkRasterPipeline::premul);
Mike Kleina3771842017-05-04 19:38:48 -0400411 }
412
Florin Malita2e409002017-06-28 14:46:54 -0400413 p->extend(postPipeline);
414
Mike Kleina3771842017-05-04 19:38:48 -0400415 return true;
416}
417
418
rileya@google.com589708b2012-07-26 20:04:23 +0000419bool SkGradientShaderBase::isOpaque() const {
Mike Reed62ce2ca2018-02-19 14:20:15 -0500420 return fColorsAreOpaque && (this->getTileMode() != SkShader::kDecal_TileMode);
421}
422
reed8367b8c2014-08-22 08:30:20 -0700423static unsigned rounded_divide(unsigned numer, unsigned denom) {
424 return (numer + (denom >> 1)) / denom;
425}
426
427bool SkGradientShaderBase::onAsLuminanceColor(SkColor* lum) const {
428 // we just compute an average color.
429 // possibly we could weight this based on the proportional width for each color
430 // assuming they are not evenly distributed in the fPos array.
431 int r = 0;
432 int g = 0;
433 int b = 0;
434 const int n = fColorCount;
Florin Malita39d71de2017-10-31 11:33:49 -0400435 // TODO: use linear colors?
reed8367b8c2014-08-22 08:30:20 -0700436 for (int i = 0; i < n; ++i) {
Florin Malita39d71de2017-10-31 11:33:49 -0400437 SkColor c = this->getLegacyColor(i);
reed8367b8c2014-08-22 08:30:20 -0700438 r += SkColorGetR(c);
439 g += SkColorGetG(c);
440 b += SkColorGetB(c);
441 }
442 *lum = SkColorSetRGB(rounded_divide(r, n), rounded_divide(g, n), rounded_divide(b, n));
443 return true;
444}
445
Florin Malita39d71de2017-10-31 11:33:49 -0400446SkGradientShaderBase::AutoXformColors::AutoXformColors(const SkGradientShaderBase& grad,
447 SkColorSpaceXformer* xformer)
448 : fColors(grad.fColorCount) {
449 // TODO: stay in 4f to preserve precision?
450
451 SkAutoSTMalloc<8, SkColor> origColors(grad.fColorCount);
452 for (int i = 0; i < grad.fColorCount; ++i) {
453 origColors[i] = grad.getLegacyColor(i);
454 }
455
456 xformer->apply(fColors.get(), origColors.get(), grad.fColorCount);
457}
458
Brian Osman6667fb12018-07-03 16:44:02 -0400459SkColor4fXformer::SkColor4fXformer(const SkColor4f* colors, int colorCount,
460 SkColorSpace* src, SkColorSpace* dst) {
Brian Osman6667fb12018-07-03 16:44:02 -0400461 fColors = colors;
Brian Osmanccd39952018-07-06 16:16:43 -0400462
Mike Kleinf9f68ff2018-10-12 14:23:06 -0400463 if (dst && !SkColorSpace::Equals(src, dst)) {
Brian Osman6667fb12018-07-03 16:44:02 -0400464 fStorage.reset(colorCount);
Brian Salomon5dfcf132018-10-12 14:39:32 +0000465
466 auto info = SkImageInfo::Make(colorCount,1, kRGBA_F32_SkColorType, kUnpremul_SkAlphaType);
467
468 SkConvertPixels(info.makeColorSpace(sk_ref_sp(dst)), fStorage.begin(), info.minRowBytes(),
469 info.makeColorSpace(sk_ref_sp(src)), fColors , info.minRowBytes());
470
Brian Osman6667fb12018-07-03 16:44:02 -0400471 fColors = fStorage.begin();
472 }
473}
474
Florin Malita5f379a82017-10-18 16:22:35 -0400475void SkGradientShaderBase::commonAsAGradient(GradientInfo* info) const {
rileya@google.com589708b2012-07-26 20:04:23 +0000476 if (info) {
477 if (info->fColorCount >= fColorCount) {
478 if (info->fColors) {
Florin Malita39d71de2017-10-31 11:33:49 -0400479 for (int i = 0; i < fColorCount; ++i) {
480 info->fColors[i] = this->getLegacyColor(i);
481 }
rileya@google.com589708b2012-07-26 20:04:23 +0000482 }
483 if (info->fColorOffsets) {
Florin Malitaed6ae562017-10-28 11:06:48 -0400484 for (int i = 0; i < fColorCount; ++i) {
485 info->fColorOffsets[i] = this->getPos(i);
rileya@google.com589708b2012-07-26 20:04:23 +0000486 }
487 }
488 }
489 info->fColorCount = fColorCount;
490 info->fTileMode = fTileMode;
reed@google.com3d3a8602013-05-24 14:58:44 +0000491 info->fGradientFlags = fGradFlags;
rileya@google.com589708b2012-07-26 20:04:23 +0000492 }
493}
494
495///////////////////////////////////////////////////////////////////////////////
496///////////////////////////////////////////////////////////////////////////////
497
reed1b747302015-01-06 07:13:19 -0800498// Return true if these parameters are valid/legal/safe to construct a gradient
499//
brianosmane25d71c2016-09-28 11:27:28 -0700500static bool valid_grad(const SkColor4f colors[], const SkScalar pos[], int count,
501 unsigned tileMode) {
halcanary96fcdcc2015-08-27 07:41:13 -0700502 return nullptr != colors && count >= 1 && tileMode < (unsigned)SkShader::kTileModeCount;
reed1b747302015-01-06 07:13:19 -0800503}
504
reed@google.com437d6eb2013-05-23 19:03:05 +0000505static void desc_init(SkGradientShaderBase::Descriptor* desc,
brianosmane25d71c2016-09-28 11:27:28 -0700506 const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace,
507 const SkScalar pos[], int colorCount,
reedaddf2ed2014-08-11 08:28:24 -0700508 SkShader::TileMode mode, uint32_t flags, const SkMatrix* localMatrix) {
fmalita748d6202016-05-11 11:39:58 -0700509 SkASSERT(colorCount > 1);
510
commit-bot@chromium.org6c5aea22014-04-22 16:25:15 +0000511 desc->fColors = colors;
brianosmane25d71c2016-09-28 11:27:28 -0700512 desc->fColorSpace = std::move(colorSpace);
commit-bot@chromium.org6c5aea22014-04-22 16:25:15 +0000513 desc->fPos = pos;
514 desc->fCount = colorCount;
515 desc->fTileMode = mode;
commit-bot@chromium.org6c5aea22014-04-22 16:25:15 +0000516 desc->fGradFlags = flags;
reedaddf2ed2014-08-11 08:28:24 -0700517 desc->fLocalMatrix = localMatrix;
reed@google.com437d6eb2013-05-23 19:03:05 +0000518}
519
Mike Klein024072a2018-11-11 00:26:30 +0000520static SkColor4f average_gradient_color(const SkColor4f colors[], const SkScalar pos[],
521 int colorCount) {
522 // The gradient is a piecewise linear interpolation between colors. For a given interval,
523 // the integral between the two endpoints is 0.5 * (ci + cj) * (pj - pi), which provides that
524 // intervals average color. The overall average color is thus the sum of each piece. The thing
525 // to keep in mind is that the provided gradient definition may implicitly use p=0 and p=1.
526 Sk4f blend(0.0);
527 // Bake 1/(colorCount - 1) uniform stop difference into this scale factor
528 SkScalar wScale = pos ? 0.5 : 0.5 / (colorCount - 1);
529 for (int i = 0; i < colorCount - 1; ++i) {
530 // Calculate the average color for the interval between pos(i) and pos(i+1)
531 Sk4f c0 = Sk4f::Load(&colors[i]);
532 Sk4f c1 = Sk4f::Load(&colors[i + 1]);
533 // when pos == null, there are colorCount uniformly distributed stops, going from 0 to 1,
534 // so pos[i + 1] - pos[i] = 1/(colorCount-1)
535 SkScalar w = pos ? (pos[i + 1] - pos[i]) : SK_Scalar1;
536 blend += wScale * w * (c1 + c0);
537 }
538
539 // Now account for any implicit intervals at the start or end of the stop definitions
540 if (pos) {
541 if (pos[0] > 0.0) {
542 // The first color is fixed between p = 0 to pos[0], so 0.5 * (ci + cj) * (pj - pi)
543 // becomes 0.5 * (c + c) * (pj - 0) = c * pj
544 Sk4f c = Sk4f::Load(&colors[0]);
545 blend += pos[0] * c;
546 }
547 if (pos[colorCount - 1] < SK_Scalar1) {
548 // The last color is fixed between pos[n-1] to p = 1, so 0.5 * (ci + cj) * (pj - pi)
549 // becomes 0.5 * (c + c) * (1 - pi) = c * (1 - pi)
550 Sk4f c = Sk4f::Load(&colors[colorCount - 1]);
551 blend += (1 - pos[colorCount - 1]) * c;
552 }
553 }
554
555 SkColor4f avg;
556 blend.store(&avg);
557 return avg;
558}
559
Michael Ludwigd431c722018-11-16 10:00:24 -0500560// The default SkScalarNearlyZero threshold of .0024 is too big and causes regressions for svg
561// gradients defined in the wild.
562static constexpr SkScalar kDegenerateThreshold = SK_Scalar1 / (1 << 15);
563
Mike Klein024072a2018-11-11 00:26:30 +0000564// Except for special circumstances of clamped gradients, every gradient shape--when degenerate--
565// can be mapped to the same fallbacks. The specific shape factories must account for special
566// clamped conditions separately, this will always return the last color for clamped gradients.
567static sk_sp<SkShader> make_degenerate_gradient(const SkColor4f colors[], const SkScalar pos[],
568 int colorCount, sk_sp<SkColorSpace> colorSpace,
569 SkShader::TileMode mode) {
570 switch(mode) {
571 case SkShader::kDecal_TileMode:
572 // normally this would reject the area outside of the interpolation region, so since
573 // inside region is empty when the radii are equal, the entire draw region is empty
574 return SkShader::MakeEmptyShader();
575 case SkShader::kRepeat_TileMode:
576 case SkShader::kMirror_TileMode:
577 // repeat and mirror are treated the same: the border colors are never visible,
578 // but approximate the final color as infinite repetitions of the colors, so
579 // it can be represented as the average color of the gradient.
580 return SkShader::MakeColorShader(
581 average_gradient_color(colors, pos, colorCount), std::move(colorSpace));
582 case SkShader::kClamp_TileMode:
583 // Depending on how the gradient shape degenerates, there may be a more specialized
584 // fallback representation for the factories to use, but this is a reasonable default.
585 return SkShader::MakeColorShader(colors[colorCount - 1], std::move(colorSpace));
586 default:
587 SkDEBUGFAIL("Should not be reached");
588 return nullptr;
589 }
590}
591
brianosmane25d71c2016-09-28 11:27:28 -0700592// assumes colors is SkColor4f* and pos is SkScalar*
fmenozzie9fd0f82016-08-19 07:50:57 -0700593#define EXPAND_1_COLOR(count) \
brianosmane25d71c2016-09-28 11:27:28 -0700594 SkColor4f tmp[2]; \
fmenozzie9fd0f82016-08-19 07:50:57 -0700595 do { \
596 if (1 == count) { \
597 tmp[0] = tmp[1] = colors[0]; \
598 colors = tmp; \
599 pos = nullptr; \
600 count = 2; \
601 } \
602 } while (0)
603
fmenozzi68d952c2016-08-19 08:56:56 -0700604struct ColorStopOptimizer {
brianosmane25d71c2016-09-28 11:27:28 -0700605 ColorStopOptimizer(const SkColor4f* colors, const SkScalar* pos,
fmenozzi68d952c2016-08-19 08:56:56 -0700606 int count, SkShader::TileMode mode)
607 : fColors(colors)
608 , fPos(pos)
609 , fCount(count) {
610
611 if (!pos || count != 3) {
612 return;
613 }
614
615 if (SkScalarNearlyEqual(pos[0], 0.0f) &&
616 SkScalarNearlyEqual(pos[1], 0.0f) &&
617 SkScalarNearlyEqual(pos[2], 1.0f)) {
618
619 if (SkShader::kRepeat_TileMode == mode ||
620 SkShader::kMirror_TileMode == mode ||
621 colors[0] == colors[1]) {
622
fmalita582a6562016-08-22 06:28:57 -0700623 // Ignore the leftmost color/pos.
624 fColors += 1;
625 fPos += 1;
626 fCount = 2;
fmenozzi68d952c2016-08-19 08:56:56 -0700627 }
628 } else if (SkScalarNearlyEqual(pos[0], 0.0f) &&
629 SkScalarNearlyEqual(pos[1], 1.0f) &&
630 SkScalarNearlyEqual(pos[2], 1.0f)) {
631
632 if (SkShader::kRepeat_TileMode == mode ||
633 SkShader::kMirror_TileMode == mode ||
634 colors[1] == colors[2]) {
635
fmalita582a6562016-08-22 06:28:57 -0700636 // Ignore the rightmost color/pos.
fmenozzi68d952c2016-08-19 08:56:56 -0700637 fCount = 2;
638 }
639 }
640 }
641
brianosmane25d71c2016-09-28 11:27:28 -0700642 const SkColor4f* fColors;
643 const SkScalar* fPos;
644 int fCount;
645};
646
647struct ColorConverter {
648 ColorConverter(const SkColor* colors, int count) {
Brian Osman6667fb12018-07-03 16:44:02 -0400649 const float ONE_OVER_255 = 1.f / 255;
brianosmane25d71c2016-09-28 11:27:28 -0700650 for (int i = 0; i < count; ++i) {
Brian Osman6667fb12018-07-03 16:44:02 -0400651 fColors4f.push_back({
652 SkColorGetR(colors[i]) * ONE_OVER_255,
653 SkColorGetG(colors[i]) * ONE_OVER_255,
654 SkColorGetB(colors[i]) * ONE_OVER_255,
655 SkColorGetA(colors[i]) * ONE_OVER_255 });
brianosmane25d71c2016-09-28 11:27:28 -0700656 }
657 }
658
659 SkSTArray<2, SkColor4f, true> fColors4f;
fmenozzi68d952c2016-08-19 08:56:56 -0700660};
661
reed8a21c9f2016-03-08 18:50:00 -0800662sk_sp<SkShader> SkGradientShader::MakeLinear(const SkPoint pts[2],
fmenozzi68d952c2016-08-19 08:56:56 -0700663 const SkColor colors[],
664 const SkScalar pos[], int colorCount,
665 SkShader::TileMode mode,
666 uint32_t flags,
667 const SkMatrix* localMatrix) {
brianosmane25d71c2016-09-28 11:27:28 -0700668 ColorConverter converter(colors, colorCount);
669 return MakeLinear(pts, converter.fColors4f.begin(), nullptr, pos, colorCount, mode, flags,
670 localMatrix);
671}
672
673sk_sp<SkShader> SkGradientShader::MakeLinear(const SkPoint pts[2],
674 const SkColor4f colors[],
675 sk_sp<SkColorSpace> colorSpace,
676 const SkScalar pos[], int colorCount,
677 SkShader::TileMode mode,
678 uint32_t flags,
679 const SkMatrix* localMatrix) {
fmalitac5231042016-08-10 05:45:50 -0700680 if (!pts || !SkScalarIsFinite((pts[1] - pts[0]).length())) {
halcanary96fcdcc2015-08-27 07:41:13 -0700681 return nullptr;
reed1b747302015-01-06 07:13:19 -0800682 }
683 if (!valid_grad(colors, pos, colorCount, mode)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700684 return nullptr;
rileya@google.com589708b2012-07-26 20:04:23 +0000685 }
fmenozzie9fd0f82016-08-19 07:50:57 -0700686 if (1 == colorCount) {
brianosmane25d71c2016-09-28 11:27:28 -0700687 return SkShader::MakeColorShader(colors[0], std::move(colorSpace));
fmenozzie9fd0f82016-08-19 07:50:57 -0700688 }
Florin Malita8d3ffad2017-02-03 18:21:17 +0000689 if (localMatrix && !localMatrix->invert(nullptr)) {
690 return nullptr;
691 }
rileya@google.com589708b2012-07-26 20:04:23 +0000692
Michael Ludwigd431c722018-11-16 10:00:24 -0500693 if (SkScalarNearlyZero((pts[1] - pts[0]).length(), kDegenerateThreshold)) {
Mike Klein024072a2018-11-11 00:26:30 +0000694 // Degenerate gradient, the only tricky complication is when in clamp mode, the limit of
695 // the gradient approaches two half planes of solid color (first and last). However, they
696 // are divided by the line perpendicular to the start and end point, which becomes undefined
697 // once start and end are exactly the same, so just use the end color for a stable solution.
698 return make_degenerate_gradient(colors, pos, colorCount, std::move(colorSpace), mode);
699 }
700
fmenozzi68d952c2016-08-19 08:56:56 -0700701 ColorStopOptimizer opt(colors, pos, colorCount, mode);
702
reed@google.com437d6eb2013-05-23 19:03:05 +0000703 SkGradientShaderBase::Descriptor desc;
brianosmane25d71c2016-09-28 11:27:28 -0700704 desc_init(&desc, opt.fColors, std::move(colorSpace), opt.fPos, opt.fCount, mode, flags,
705 localMatrix);
reed8a21c9f2016-03-08 18:50:00 -0800706 return sk_make_sp<SkLinearGradient>(pts, desc);
rileya@google.com589708b2012-07-26 20:04:23 +0000707}
708
reed8a21c9f2016-03-08 18:50:00 -0800709sk_sp<SkShader> SkGradientShader::MakeRadial(const SkPoint& center, SkScalar radius,
brianosmane25d71c2016-09-28 11:27:28 -0700710 const SkColor colors[],
711 const SkScalar pos[], int colorCount,
712 SkShader::TileMode mode,
713 uint32_t flags,
714 const SkMatrix* localMatrix) {
715 ColorConverter converter(colors, colorCount);
716 return MakeRadial(center, radius, converter.fColors4f.begin(), nullptr, pos, colorCount, mode,
717 flags, localMatrix);
718}
719
720sk_sp<SkShader> SkGradientShader::MakeRadial(const SkPoint& center, SkScalar radius,
721 const SkColor4f colors[],
722 sk_sp<SkColorSpace> colorSpace,
723 const SkScalar pos[], int colorCount,
724 SkShader::TileMode mode,
725 uint32_t flags,
726 const SkMatrix* localMatrix) {
Mike Klein024072a2018-11-11 00:26:30 +0000727 if (radius < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700728 return nullptr;
reed1b747302015-01-06 07:13:19 -0800729 }
730 if (!valid_grad(colors, pos, colorCount, mode)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700731 return nullptr;
rileya@google.com589708b2012-07-26 20:04:23 +0000732 }
fmenozzie9fd0f82016-08-19 07:50:57 -0700733 if (1 == colorCount) {
brianosmane25d71c2016-09-28 11:27:28 -0700734 return SkShader::MakeColorShader(colors[0], std::move(colorSpace));
fmenozzie9fd0f82016-08-19 07:50:57 -0700735 }
Florin Malita8d3ffad2017-02-03 18:21:17 +0000736 if (localMatrix && !localMatrix->invert(nullptr)) {
737 return nullptr;
738 }
rileya@google.com589708b2012-07-26 20:04:23 +0000739
Michael Ludwigd431c722018-11-16 10:00:24 -0500740 if (SkScalarNearlyZero(radius, kDegenerateThreshold)) {
Mike Klein024072a2018-11-11 00:26:30 +0000741 // Degenerate gradient optimization, and no special logic needed for clamped radial gradient
742 return make_degenerate_gradient(colors, pos, colorCount, std::move(colorSpace), mode);
743 }
744
fmenozzi68d952c2016-08-19 08:56:56 -0700745 ColorStopOptimizer opt(colors, pos, colorCount, mode);
746
reed@google.com437d6eb2013-05-23 19:03:05 +0000747 SkGradientShaderBase::Descriptor desc;
brianosmane25d71c2016-09-28 11:27:28 -0700748 desc_init(&desc, opt.fColors, std::move(colorSpace), opt.fPos, opt.fCount, mode, flags,
749 localMatrix);
reed8a21c9f2016-03-08 18:50:00 -0800750 return sk_make_sp<SkRadialGradient>(center, radius, desc);
rileya@google.com589708b2012-07-26 20:04:23 +0000751}
752
reed8a21c9f2016-03-08 18:50:00 -0800753sk_sp<SkShader> SkGradientShader::MakeTwoPointConical(const SkPoint& start,
brianosmane25d71c2016-09-28 11:27:28 -0700754 SkScalar startRadius,
755 const SkPoint& end,
756 SkScalar endRadius,
757 const SkColor colors[],
758 const SkScalar pos[],
759 int colorCount,
760 SkShader::TileMode mode,
761 uint32_t flags,
762 const SkMatrix* localMatrix) {
763 ColorConverter converter(colors, colorCount);
764 return MakeTwoPointConical(start, startRadius, end, endRadius, converter.fColors4f.begin(),
765 nullptr, pos, colorCount, mode, flags, localMatrix);
766}
767
768sk_sp<SkShader> SkGradientShader::MakeTwoPointConical(const SkPoint& start,
769 SkScalar startRadius,
770 const SkPoint& end,
771 SkScalar endRadius,
772 const SkColor4f colors[],
773 sk_sp<SkColorSpace> colorSpace,
774 const SkScalar pos[],
775 int colorCount,
776 SkShader::TileMode mode,
777 uint32_t flags,
778 const SkMatrix* localMatrix) {
reed1b747302015-01-06 07:13:19 -0800779 if (startRadius < 0 || endRadius < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700780 return nullptr;
reed1b747302015-01-06 07:13:19 -0800781 }
782 if (!valid_grad(colors, pos, colorCount, mode)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700783 return nullptr;
rileya@google.com589708b2012-07-26 20:04:23 +0000784 }
Michael Ludwigd431c722018-11-16 10:00:24 -0500785 if (SkScalarNearlyZero((start - end).length(), kDegenerateThreshold)) {
Mike Klein024072a2018-11-11 00:26:30 +0000786 // If the center positions are the same, then the gradient is the radial variant of a 2 pt
787 // conical gradient, an actual radial gradient (startRadius == 0), or it is fully degenerate
788 // (startRadius == endRadius).
Michael Ludwigd431c722018-11-16 10:00:24 -0500789 if (SkScalarNearlyEqual(startRadius, endRadius, kDegenerateThreshold)) {
Mike Klein024072a2018-11-11 00:26:30 +0000790 // Degenerate case, where the interpolation region area approaches zero. The proper
791 // behavior depends on the tile mode, which is consistent with the default degenerate
792 // gradient behavior, except when mode = clamp and the radii > 0.
Michael Ludwigd431c722018-11-16 10:00:24 -0500793 if (mode == SkShader::TileMode::kClamp_TileMode && endRadius > kDegenerateThreshold) {
Mike Klein024072a2018-11-11 00:26:30 +0000794 // The interpolation region becomes an infinitely thin ring at the radius, so the
795 // final gradient will be the first color repeated from p=0 to 1, and then a hard
796 // stop switching to the last color at p=1.
797 static constexpr SkScalar circlePos[3] = {0, 1, 1};
798 SkColor4f reColors[3] = {colors[0], colors[0], colors[colorCount - 1]};
799 return MakeRadial(start, endRadius, reColors, std::move(colorSpace),
800 circlePos, 3, mode, flags, localMatrix);
801 } else {
802 // Otherwise use the default degenerate case
803 return make_degenerate_gradient(
804 colors, pos, colorCount, std::move(colorSpace), mode);
805 }
Michael Ludwigd431c722018-11-16 10:00:24 -0500806 } else if (SkScalarNearlyZero(startRadius, kDegenerateThreshold)) {
Mike Klein024072a2018-11-11 00:26:30 +0000807 // We can treat this gradient as radial, which is faster. If we got here, we know
808 // that endRadius is not equal to 0, so this produces a meaningful gradient
809 return MakeRadial(start, endRadius, colors, std::move(colorSpace), pos, colorCount,
810 mode, flags, localMatrix);
Brian Osman2dfab272018-11-06 00:41:40 +0000811 }
Mike Klein024072a2018-11-11 00:26:30 +0000812 // Else it's the 2pt conical radial variant with no degenerate radii, so fall through to the
813 // regular 2pt constructor.
Brian Osman2dfab272018-11-06 00:41:40 +0000814 }
Mike Klein024072a2018-11-11 00:26:30 +0000815
Florin Malita8d3ffad2017-02-03 18:21:17 +0000816 if (localMatrix && !localMatrix->invert(nullptr)) {
817 return nullptr;
818 }
reed6b7a6c72016-08-18 16:13:50 -0700819 EXPAND_1_COLOR(colorCount);
rileya@google.com589708b2012-07-26 20:04:23 +0000820
fmenozzi68d952c2016-08-19 08:56:56 -0700821 ColorStopOptimizer opt(colors, pos, colorCount, mode);
822
reed@google.com437d6eb2013-05-23 19:03:05 +0000823 SkGradientShaderBase::Descriptor desc;
Florin Malita5f379a82017-10-18 16:22:35 -0400824 desc_init(&desc, opt.fColors, std::move(colorSpace), opt.fPos, opt.fCount, mode, flags,
825 localMatrix);
826 return SkTwoPointConicalGradient::Create(start, startRadius, end, endRadius, desc);
rileya@google.com589708b2012-07-26 20:04:23 +0000827}
828
reed8a21c9f2016-03-08 18:50:00 -0800829sk_sp<SkShader> SkGradientShader::MakeSweep(SkScalar cx, SkScalar cy,
brianosmane25d71c2016-09-28 11:27:28 -0700830 const SkColor colors[],
831 const SkScalar pos[],
832 int colorCount,
Florin Malita5a9a9812017-08-01 16:38:08 -0400833 SkShader::TileMode mode,
834 SkScalar startAngle,
835 SkScalar endAngle,
brianosmane25d71c2016-09-28 11:27:28 -0700836 uint32_t flags,
837 const SkMatrix* localMatrix) {
838 ColorConverter converter(colors, colorCount);
Florin Malita5a9a9812017-08-01 16:38:08 -0400839 return MakeSweep(cx, cy, converter.fColors4f.begin(), nullptr, pos, colorCount,
840 mode, startAngle, endAngle, flags, localMatrix);
brianosmane25d71c2016-09-28 11:27:28 -0700841}
842
843sk_sp<SkShader> SkGradientShader::MakeSweep(SkScalar cx, SkScalar cy,
844 const SkColor4f colors[],
845 sk_sp<SkColorSpace> colorSpace,
846 const SkScalar pos[],
847 int colorCount,
Florin Malita5a9a9812017-08-01 16:38:08 -0400848 SkShader::TileMode mode,
849 SkScalar startAngle,
850 SkScalar endAngle,
brianosmane25d71c2016-09-28 11:27:28 -0700851 uint32_t flags,
852 const SkMatrix* localMatrix) {
Florin Malita5a9a9812017-08-01 16:38:08 -0400853 if (!valid_grad(colors, pos, colorCount, mode)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700854 return nullptr;
rileya@google.com589708b2012-07-26 20:04:23 +0000855 }
fmenozzie9fd0f82016-08-19 07:50:57 -0700856 if (1 == colorCount) {
brianosmane25d71c2016-09-28 11:27:28 -0700857 return SkShader::MakeColorShader(colors[0], std::move(colorSpace));
fmenozzie9fd0f82016-08-19 07:50:57 -0700858 }
Mike Klein024072a2018-11-11 00:26:30 +0000859 if (!SkScalarIsFinite(startAngle) || !SkScalarIsFinite(endAngle) || startAngle > endAngle) {
Florin Malita5a9a9812017-08-01 16:38:08 -0400860 return nullptr;
861 }
Florin Malita8d3ffad2017-02-03 18:21:17 +0000862 if (localMatrix && !localMatrix->invert(nullptr)) {
863 return nullptr;
864 }
rileya@google.com589708b2012-07-26 20:04:23 +0000865
Michael Ludwigd431c722018-11-16 10:00:24 -0500866 if (SkScalarNearlyEqual(startAngle, endAngle, kDegenerateThreshold)) {
Mike Klein024072a2018-11-11 00:26:30 +0000867 // Degenerate gradient, which should follow default degenerate behavior unless it is
868 // clamped and the angle is greater than 0.
Michael Ludwigd431c722018-11-16 10:00:24 -0500869 if (mode == SkShader::kClamp_TileMode && endAngle > kDegenerateThreshold) {
Mike Klein024072a2018-11-11 00:26:30 +0000870 // In this case, the first color is repeated from 0 to the angle, then a hardstop
871 // switches to the last color (all other colors are compressed to the infinitely thin
872 // interpolation region).
873 static constexpr SkScalar clampPos[3] = {0, 1, 1};
874 SkColor4f reColors[3] = {colors[0], colors[0], colors[colorCount - 1]};
875 return MakeSweep(cx, cy, reColors, std::move(colorSpace), clampPos, 3, mode, 0,
876 endAngle, flags, localMatrix);
877 } else {
878 return make_degenerate_gradient(colors, pos, colorCount, std::move(colorSpace), mode);
879 }
880 }
881
Florin Malita5a9a9812017-08-01 16:38:08 -0400882 if (startAngle <= 0 && endAngle >= 360) {
883 // If the t-range includes [0,1], then we can always use clamping (presumably faster).
884 mode = SkShader::kClamp_TileMode;
885 }
fmenozzi68d952c2016-08-19 08:56:56 -0700886
887 ColorStopOptimizer opt(colors, pos, colorCount, mode);
888
reed@google.com437d6eb2013-05-23 19:03:05 +0000889 SkGradientShaderBase::Descriptor desc;
brianosmane25d71c2016-09-28 11:27:28 -0700890 desc_init(&desc, opt.fColors, std::move(colorSpace), opt.fPos, opt.fCount, mode, flags,
891 localMatrix);
Florin Malita5a9a9812017-08-01 16:38:08 -0400892
893 const SkScalar t0 = startAngle / 360,
894 t1 = endAngle / 360;
895
896 return sk_make_sp<SkSweepGradient>(SkPoint::Make(cx, cy), t0, t1, desc);
rileya@google.com589708b2012-07-26 20:04:23 +0000897}
898
Mike Kleinfa5f6ce2018-10-20 08:21:31 -0400899void SkGradientShader::RegisterFlattenables() {
Brian Salomon23356442018-11-30 15:33:19 -0500900 SK_REGISTER_FLATTENABLE(SkLinearGradient);
901 SK_REGISTER_FLATTENABLE(SkRadialGradient);
902 SK_REGISTER_FLATTENABLE(SkSweepGradient);
903 SK_REGISTER_FLATTENABLE(SkTwoPointConicalGradient);
Mike Klein12956722018-10-19 10:00:21 -0400904}