blob: 07ba7e35210906bf7c940466c9cd1ac9c764a553 [file] [log] [blame]
rileya@google.com1c6d64b2012-07-27 15:49:05 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkGradientShaderPriv_DEFINED
9#define SkGradientShaderPriv_DEFINED
10
reeda6cac4c2014-08-21 10:50:25 -070011#include "SkGradientBitmapCache.h"
rileya@google.com1c6d64b2012-07-27 15:49:05 +000012#include "SkGradientShader.h"
Hal Canary95e3c052017-01-11 12:44:43 -050013
Herb Derby83e939b2017-02-07 14:25:11 -050014#include "SkArenaAlloc.h"
Hal Canary95e3c052017-01-11 12:44:43 -050015#include "SkAutoMalloc.h"
rileya@google.com1c6d64b2012-07-27 15:49:05 +000016#include "SkClampRange.h"
17#include "SkColorPriv.h"
brianosmanb9c51372016-09-15 11:09:45 -070018#include "SkColorSpace.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000019#include "SkOnce.h"
Mike Kleina3771842017-05-04 19:38:48 -040020#include "SkPM4fPriv.h"
21#include "SkRasterPipeline.h"
Hal Canary95e3c052017-01-11 12:44:43 -050022#include "SkReadBuffer.h"
Florin Malita4aed1382017-05-25 10:38:07 -040023#include "SkShaderBase.h"
Hal Canary95e3c052017-01-11 12:44:43 -050024#include "SkUtils.h"
25#include "SkWriteBuffer.h"
rileya@google.com1c6d64b2012-07-27 15:49:05 +000026
humper@google.com05af1af2013-01-07 16:47:43 +000027static inline void sk_memset32_dither(uint32_t dst[], uint32_t v0, uint32_t v1,
rileya@google.com1c6d64b2012-07-27 15:49:05 +000028 int count) {
29 if (count > 0) {
30 if (v0 == v1) {
31 sk_memset32(dst, v0, count);
32 } else {
33 int pairs = count >> 1;
34 for (int i = 0; i < pairs; i++) {
35 *dst++ = v0;
36 *dst++ = v1;
37 }
38 if (count & 1) {
39 *dst = v0;
40 }
41 }
42 }
43}
44
45// Clamp
46
humper@google.com05af1af2013-01-07 16:47:43 +000047static inline SkFixed clamp_tileproc(SkFixed x) {
rileya@google.com1c6d64b2012-07-27 15:49:05 +000048 return SkClampMax(x, 0xFFFF);
49}
50
51// Repeat
52
humper@google.com05af1af2013-01-07 16:47:43 +000053static inline SkFixed repeat_tileproc(SkFixed x) {
rileya@google.com1c6d64b2012-07-27 15:49:05 +000054 return x & 0xFFFF;
55}
56
57// Mirror
58
rileya@google.com1c6d64b2012-07-27 15:49:05 +000059static inline SkFixed mirror_tileproc(SkFixed x) {
caryclark3127c992015-12-09 12:02:30 -080060 int s = SkLeftShift(x, 15) >> 31;
rileya@google.com1c6d64b2012-07-27 15:49:05 +000061 return (x ^ s) & 0xFFFF;
62}
63
rileya@google.com1c6d64b2012-07-27 15:49:05 +000064///////////////////////////////////////////////////////////////////////////////
65
66typedef SkFixed (*TileProc)(SkFixed);
67
68///////////////////////////////////////////////////////////////////////////////
69
70static const TileProc gTileProcs[] = {
71 clamp_tileproc,
72 repeat_tileproc,
73 mirror_tileproc
74};
75
76///////////////////////////////////////////////////////////////////////////////
77
Florin Malita4aed1382017-05-25 10:38:07 -040078class SkGradientShaderBase : public SkShaderBase {
rileya@google.com1c6d64b2012-07-27 15:49:05 +000079public:
reed@google.com437d6eb2013-05-23 19:03:05 +000080 struct Descriptor {
81 Descriptor() {
82 sk_bzero(this, sizeof(*this));
83 fTileMode = SkShader::kClamp_TileMode;
84 }
skia.committer@gmail.com3e2345a2013-05-24 07:01:26 +000085
reedaddf2ed2014-08-11 08:28:24 -070086 const SkMatrix* fLocalMatrix;
brianosmane25d71c2016-09-28 11:27:28 -070087 const SkColor4f* fColors;
brianosmanb9c51372016-09-15 11:09:45 -070088 sk_sp<SkColorSpace> fColorSpace;
reed@google.com437d6eb2013-05-23 19:03:05 +000089 const SkScalar* fPos;
90 int fCount;
91 SkShader::TileMode fTileMode;
commit-bot@chromium.org6c5aea22014-04-22 16:25:15 +000092 uint32_t fGradFlags;
reed9fa60da2014-08-21 07:59:51 -070093
94 void flatten(SkWriteBuffer&) const;
95 };
96
97 class DescriptorScope : public Descriptor {
98 public:
99 DescriptorScope() {}
mtklein88fd0fb2014-12-01 06:56:38 -0800100
reed9fa60da2014-08-21 07:59:51 -0700101 bool unflatten(SkReadBuffer&);
102
103 // fColors and fPos always point into local memory, so they can be safely mutated
104 //
brianosmane25d71c2016-09-28 11:27:28 -0700105 SkColor4f* mutableColors() { return const_cast<SkColor4f*>(fColors); }
reed9fa60da2014-08-21 07:59:51 -0700106 SkScalar* mutablePos() { return const_cast<SkScalar*>(fPos); }
107
108 private:
109 enum {
110 kStorageCount = 16
111 };
brianosmane25d71c2016-09-28 11:27:28 -0700112 SkColor4f fColorStorage[kStorageCount];
reed9fa60da2014-08-21 07:59:51 -0700113 SkScalar fPosStorage[kStorageCount];
114 SkMatrix fLocalMatrixStorage;
115 SkAutoMalloc fDynamicStorage;
reed@google.com437d6eb2013-05-23 19:03:05 +0000116 };
117
mtkleincc695fe2014-12-10 10:29:19 -0800118 SkGradientShaderBase(const Descriptor& desc, const SkMatrix& ptsToUnit);
Brian Salomond3b65972017-03-22 12:05:03 -0400119 ~SkGradientShaderBase() override;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000120
brianosman93110a82016-09-15 08:40:21 -0700121 // The cache is initialized on-demand when getCache32 is called.
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000122 class GradientShaderCache : public SkRefCnt {
123 public:
fmalita37d86882015-10-09 10:22:46 -0700124 GradientShaderCache(U8CPU alpha, bool dither, const SkGradientShaderBase& shader);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000125 ~GradientShaderCache();
126
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000127 const SkPMColor* getCache32();
128
Mike Reed6b3155c2017-04-03 14:41:44 -0400129 SkPixelRef* getCache32PixelRef() const { return fCache32PixelRef.get(); }
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000130
131 unsigned getAlpha() const { return fCacheAlpha; }
fmalita37d86882015-10-09 10:22:46 -0700132 bool getDither() const { return fCacheDither; }
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000133
134 private:
brianosman93110a82016-09-15 08:40:21 -0700135 // Working pointer. If it's nullptr, we need to recompute the cache values.
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000136 SkPMColor* fCache32;
137
Mike Reed6b3155c2017-04-03 14:41:44 -0400138 sk_sp<SkPixelRef> fCache32PixelRef;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000139 const unsigned fCacheAlpha; // The alpha value we used when we computed the cache.
140 // Larger than 8bits so we can store uninitialized
141 // value.
fmalita37d86882015-10-09 10:22:46 -0700142 const bool fCacheDither; // The dither flag used when we computed the cache.
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000143
144 const SkGradientShaderBase& fShader;
145
brianosman93110a82016-09-15 08:40:21 -0700146 // Make sure we only initialize the cache once.
147 SkOnce fCache32InitOnce;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000148
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000149 static void initCache32(GradientShaderCache* cache);
150
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000151 static void Build32bitCache(SkPMColor[], SkColor c0, SkColor c1, int count,
fmalita37d86882015-10-09 10:22:46 -0700152 U8CPU alpha, uint32_t gradFlags, bool dither);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000153 };
154
Florin Malita4aed1382017-05-25 10:38:07 -0400155 class GradientShaderBaseContext : public Context {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000156 public:
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000157 GradientShaderBaseContext(const SkGradientShaderBase& shader, const ContextRec&);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000158
mtklein36352bf2015-03-25 18:17:31 -0700159 uint32_t getFlags() const override { return fFlags; }
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000160
fmalita088e21b2016-10-05 09:28:42 -0700161 bool isValid() const;
162
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000163 protected:
164 SkMatrix fDstToIndex;
165 SkMatrix::MapXYProc fDstToIndexProc;
166 uint8_t fDstToIndexClass;
167 uint8_t fFlags;
fmalita37d86882015-10-09 10:22:46 -0700168 bool fDither;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000169
Hal Canary67b39de2016-11-07 11:47:44 -0500170 sk_sp<GradientShaderCache> fCache;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000171
172 private:
Florin Malita4aed1382017-05-25 10:38:07 -0400173 typedef Context INHERITED;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000174 };
175
mtklein36352bf2015-03-25 18:17:31 -0700176 bool isOpaque() const override;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000177
brianosmand4546092016-09-22 12:31:58 -0700178 enum class GradientBitmapType : uint8_t {
179 kLegacy,
180 kSRGB,
181 kHalfFloat,
182 };
183
184 void getGradientTableBitmap(SkBitmap*, GradientBitmapType bitmapType) const;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000185
186 enum {
187 /// Seems like enough for visual accuracy. TODO: if pos[] deserves
188 /// it, use a larger cache.
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000189 kCache32Bits = 8,
reed@google.com60040292013-02-04 18:21:23 +0000190 kCache32Count = (1 << kCache32Bits),
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000191 kCache32Shift = 16 - kCache32Bits,
192 kSqrt32Shift = 8 - kCache32Bits,
193
194 /// This value is used to *read* the dither cache; it may be 0
195 /// if dithering is disabled.
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000196 kDitherStride32 = kCache32Count,
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000197 };
198
commit-bot@chromium.org6c5aea22014-04-22 16:25:15 +0000199 uint32_t getGradFlags() const { return fGradFlags; }
commit-bot@chromium.org53783b02014-04-17 21:09:49 +0000200
Florin Malita0e36b3f2017-06-05 23:33:45 -0400201 SkColor4f getXformedColor(size_t index, SkColorSpace*) const;
202
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000203protected:
Mike Kleina3771842017-05-04 19:38:48 -0400204 struct Rec {
205 SkFixed fPos; // 0...1
206 uint32_t fScale; // (1 << 24) / range
207 };
208
fmalitabc590c02016-02-22 09:12:33 -0800209 class GradientShaderBase4fContext;
210
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000211 SkGradientShaderBase(SkReadBuffer& );
mtklein36352bf2015-03-25 18:17:31 -0700212 void flatten(SkWriteBuffer&) const override;
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000213 SK_TO_STRING_OVERRIDE()
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000214
commit-bot@chromium.org44d83c12014-04-21 13:10:25 +0000215 void commonAsAGradient(GradientInfo*, bool flipGrad = false) const;
skia.committer@gmail.comd3b28e82014-04-22 03:05:17 +0000216
mtklein36352bf2015-03-25 18:17:31 -0700217 bool onAsLuminanceColor(SkColor*) const override;
reed8367b8c2014-08-22 08:30:20 -0700218
brianosmand4546092016-09-22 12:31:58 -0700219 void initLinearBitmap(SkBitmap* bitmap) const;
220
commit-bot@chromium.org44d83c12014-04-21 13:10:25 +0000221 /*
222 * Takes in pointers to gradient color and Rec info as colorSrc and recSrc respectively.
223 * Count is the number of colors in the gradient
224 * It will then flip all the color and rec information and return in their respective Dst
225 * pointers. It is assumed that space has already been allocated for the Dst pointers.
226 * The rec src and dst are only assumed to be valid if count > 2
227 */
228 static void FlipGradientColors(SkColor* colorDst, Rec* recDst,
229 SkColor* colorSrc, Rec* recSrc,
230 int count);
231
Mike Kleina3771842017-05-04 19:38:48 -0400232 bool onAppendStages(SkRasterPipeline* pipeline, SkColorSpace* dstCS, SkArenaAlloc* alloc,
233 const SkMatrix& ctm, const SkPaint& paint,
234 const SkMatrix* localM) const override;
235
236 virtual bool adjustMatrixAndAppendStages(SkArenaAlloc* alloc,
237 SkMatrix* matrix,
Florin Malita2e409002017-06-28 14:46:54 -0400238 SkRasterPipeline* tPipeline,
Florin Malita5b8e2b82017-07-12 11:31:22 -0400239 SkRasterPipeline* postPipeline) const = 0;
Mike Kleina3771842017-05-04 19:38:48 -0400240
fmalita088e21b2016-10-05 09:28:42 -0700241 template <typename T, typename... Args>
Herb Derby83e939b2017-02-07 14:25:11 -0500242 static Context* CheckedMakeContext(SkArenaAlloc* alloc, Args&&... args) {
243 auto* ctx = alloc->make<T>(std::forward<Args>(args)...);
fmalita088e21b2016-10-05 09:28:42 -0700244 if (!ctx->isValid()) {
fmalita088e21b2016-10-05 09:28:42 -0700245 return nullptr;
246 }
247 return ctx;
248 }
249
Mike Kleina3771842017-05-04 19:38:48 -0400250 const SkMatrix fPtsToUnit;
251 TileMode fTileMode;
252 TileProc fTileProc;
253 uint8_t fGradFlags;
254 Rec* fRecs;
255
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000256private:
257 enum {
258 kColorStorageCount = 4, // more than this many colors, and we'll use sk_malloc for the space
259
brianosmanb9c51372016-09-15 11:09:45 -0700260 kStorageSize = kColorStorageCount *
261 (sizeof(SkColor) + sizeof(SkScalar) + sizeof(Rec) + sizeof(SkColor4f))
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000262 };
brianosmanb9c51372016-09-15 11:09:45 -0700263 SkColor fStorage[(kStorageSize + 3) >> 2];
reedf3182eb2015-11-17 08:12:19 -0800264public:
brianosmanb9c51372016-09-15 11:09:45 -0700265 SkColor* fOrigColors; // original colors, before modulation by paint in context.
266 SkColor4f* fOrigColors4f; // original colors, as linear floats
267 SkScalar* fOrigPos; // original positions
268 int fColorCount;
269 sk_sp<SkColorSpace> fColorSpace; // color space of gradient stops
reedf3182eb2015-11-17 08:12:19 -0800270
271 bool colorsAreOpaque() const { return fColorsAreOpaque; }
272
fmenozzicd9a1d02016-08-15 07:03:47 -0700273 TileMode getTileMode() const { return fTileMode; }
274 Rec* getRecs() const { return fRecs; }
275
reedf3182eb2015-11-17 08:12:19 -0800276private:
brianosmanb9c51372016-09-15 11:09:45 -0700277 bool fColorsAreOpaque;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000278
Hal Canary67b39de2016-11-07 11:47:44 -0500279 sk_sp<GradientShaderCache> refCache(U8CPU alpha, bool dither) const;
280 mutable SkMutex fCacheMutex;
281 mutable sk_sp<GradientShaderCache> fCache;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000282
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000283 void initCommon();
284
Florin Malita4aed1382017-05-25 10:38:07 -0400285 typedef SkShaderBase INHERITED;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000286};
287
Mike Kleina3771842017-05-04 19:38:48 -0400288
reed@google.com55853db2013-02-01 19:34:59 +0000289static inline int init_dither_toggle(int x, int y) {
reed@google.com60040292013-02-04 18:21:23 +0000290 x &= 1;
291 y = (y & 1) << 1;
292 return (x | y) * SkGradientShaderBase::kDitherStride32;
reed@google.com55853db2013-02-01 19:34:59 +0000293}
294
295static inline int next_dither_toggle(int toggle) {
296 return toggle ^ SkGradientShaderBase::kDitherStride32;
297}
298
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000299///////////////////////////////////////////////////////////////////////////////
300
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000301#if SK_SUPPORT_GPU
302
brianosmanb9c51372016-09-15 11:09:45 -0700303#include "GrColorSpaceXform.h"
bsalomon@google.com77af6802013-10-02 13:04:56 +0000304#include "GrCoordTransform.h"
bsalomon6251d172014-10-15 10:50:36 -0700305#include "GrFragmentProcessor.h"
Brian Osmanc624d9d2017-03-08 11:42:02 -0500306#include "glsl/GrGLSLColorSpaceXformHelper.h"
egdaniel64c47282015-11-13 06:54:19 -0800307#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel018fb622015-10-28 07:26:40 -0700308#include "glsl/GrGLSLProgramDataManager.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000309
egdaniel605dd0f2014-11-12 08:35:25 -0800310class GrInvariantOutput;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000311
312/*
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000313 * The interpretation of the texture matrix depends on the sample mode. The
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000314 * texture matrix is applied both when the texture coordinates are explicit
315 * and when vertex positions are used as texture coordinates. In the latter
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000316 * case the texture matrix is applied to the pre-view-matrix position
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000317 * values.
318 *
319 * Normal SampleMode
320 * The post-matrix texture coordinates are in normalize space with (0,0) at
321 * the top-left and (1,1) at the bottom right.
322 * RadialGradient
323 * The matrix specifies the radial gradient parameters.
324 * (0,0) in the post-matrix space is center of the radial gradient.
325 * Radial2Gradient
326 * Matrix transforms to space where first circle is centered at the
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000327 * origin. The second circle will be centered (x, 0) where x may be
328 * 0 and is provided by setRadial2Params. The post-matrix space is
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000329 * normalized such that 1 is the second radius - first radius.
330 * SweepGradient
331 * The angle from the origin of texture coordinates in post-matrix space
332 * determines the gradient value.
333 */
334
rileya@google.comb3e50f22012-08-20 17:43:08 +0000335 class GrTextureStripAtlas;
336
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000337// Base class for Gr gradient effects
joshualittb0a8a372014-09-23 09:50:21 -0700338class GrGradientEffect : public GrFragmentProcessor {
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000339public:
brianosman9557c272016-09-15 06:59:15 -0700340 struct CreateArgs {
341 CreateArgs(GrContext* context,
342 const SkGradientShaderBase* shader,
343 const SkMatrix* matrix,
brianosmanb9c51372016-09-15 11:09:45 -0700344 SkShader::TileMode tileMode,
345 sk_sp<GrColorSpaceXform> colorSpaceXform,
346 bool gammaCorrect)
brianosman9557c272016-09-15 06:59:15 -0700347 : fContext(context)
348 , fShader(shader)
349 , fMatrix(matrix)
brianosmanb9c51372016-09-15 11:09:45 -0700350 , fTileMode(tileMode)
351 , fColorSpaceXform(std::move(colorSpaceXform))
352 , fGammaCorrect(gammaCorrect) {}
brianosman9557c272016-09-15 06:59:15 -0700353
354 GrContext* fContext;
355 const SkGradientShaderBase* fShader;
356 const SkMatrix* fMatrix;
357 SkShader::TileMode fTileMode;
brianosmanb9c51372016-09-15 11:09:45 -0700358 sk_sp<GrColorSpaceXform> fColorSpaceXform;
359 bool fGammaCorrect;
brianosman9557c272016-09-15 06:59:15 -0700360 };
361
fmenozzi55d318d2016-08-09 08:05:57 -0700362 class GLSLProcessor;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000363
Brian Salomond3b65972017-03-22 12:05:03 -0400364 ~GrGradientEffect() override;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000365
rileya@google.comb3e50f22012-08-20 17:43:08 +0000366 bool useAtlas() const { return SkToBool(-1 != fRow); }
fmenozzicd9a1d02016-08-15 07:03:47 -0700367 SkScalar getYCoord() const { return fYCoord; }
rileya@google.comb3e50f22012-08-20 17:43:08 +0000368
fmenozzicd9a1d02016-08-15 07:03:47 -0700369 enum ColorType {
370 kTwo_ColorType,
371 kThree_ColorType, // Symmetric three color
372 kTexture_ColorType,
Brian Salomon466ad992016-10-13 16:08:36 -0400373 kSingleHardStop_ColorType, // 0, t, t, 1
fmenozzicd9a1d02016-08-15 07:03:47 -0700374 kHardStopLeftEdged_ColorType, // 0, 0, 1
375 kHardStopRightEdged_ColorType, // 0, 1, 1
fmenozzicd9a1d02016-08-15 07:03:47 -0700376 };
377
378 ColorType getColorType() const { return fColorType; }
379
380 // Determines the type of gradient, one of:
381 // - Two-color
382 // - Symmetric three-color
383 // - Texture
384 // - Centered hard stop
385 // - Left-edged hard stop
386 // - Right-edged hard stop
387 ColorType determineColorType(const SkGradientShaderBase& shader);
bsalomon@google.com82d12232013-09-09 15:36:26 +0000388
389 enum PremulType {
390 kBeforeInterp_PremulType,
391 kAfterInterp_PremulType,
392 };
393
394 PremulType getPremulType() const { return fPremulType; }
395
396 const SkColor* getColors(int pos) const {
fmenozzicd9a1d02016-08-15 07:03:47 -0700397 SkASSERT(fColorType != kTexture_ColorType);
398 SkASSERT(pos < fColors.count());
bsalomon@google.com82d12232013-09-09 15:36:26 +0000399 return &fColors[pos];
400 }
bsalomon@google.com371e1052013-01-11 21:08:55 +0000401
brianosmanb9c51372016-09-15 11:09:45 -0700402 const SkColor4f* getColors4f(int pos) const {
403 SkASSERT(fColorType != kTexture_ColorType);
404 SkASSERT(pos < fColors4f.count());
405 return &fColors4f[pos];
406 }
407
bsalomon@google.comd4726202012-08-03 14:34:46 +0000408protected:
Brian Salomon587e08f2017-01-27 10:59:27 -0500409 GrGradientEffect(const CreateArgs&, bool isOpaque);
Brian Salomonf8480b92017-07-27 15:45:59 -0400410 explicit GrGradientEffect(const GrGradientEffect&); // facilitates clone() implementations
Brian Salomon587e08f2017-01-27 10:59:27 -0500411
Hal Canary6f6961e2017-01-31 13:50:44 -0500412 #if GR_TEST_UTILS
Brian Osmane75c19f2016-10-10 11:26:43 -0400413 /** Helper struct that stores (and populates) parameters to construct a random gradient.
Brian Osmana2196532016-10-17 12:48:13 -0400414 If fUseColors4f is true, then the SkColor4f factory should be called, with fColors4f and
415 fColorSpace. Otherwise, the SkColor factory should be called, with fColors. fColorCount
416 will be the number of color stops in either case, and fColors and fStops can be passed to
417 the gradient factory. (The constructor may decide not to use stops, in which case fStops
418 will be nullptr). */
Brian Osman3f748602016-10-03 18:29:03 -0400419 struct RandomGradientParams {
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500420 static const int kMaxRandomGradientColors = 5;
Brian Osmane75c19f2016-10-10 11:26:43 -0400421
Brian Osman3f748602016-10-03 18:29:03 -0400422 RandomGradientParams(SkRandom* r);
423
Brian Osmana2196532016-10-17 12:48:13 -0400424 bool fUseColors4f;
Brian Osman3f748602016-10-03 18:29:03 -0400425 SkColor fColors[kMaxRandomGradientColors];
Brian Osmana2196532016-10-17 12:48:13 -0400426 SkColor4f fColors4f[kMaxRandomGradientColors];
427 sk_sp<SkColorSpace> fColorSpace;
Brian Osman3f748602016-10-03 18:29:03 -0400428 SkScalar fStopStorage[kMaxRandomGradientColors];
429 SkShader::TileMode fTileMode;
430 int fColorCount;
431 SkScalar* fStops;
432 };
Hal Canary6f6961e2017-01-31 13:50:44 -0500433 #endif
bsalomon@google.comd4726202012-08-03 14:34:46 +0000434
mtklein36352bf2015-03-25 18:17:31 -0700435 bool onIsEqual(const GrFragmentProcessor&) const override;
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000436
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +0000437 const GrCoordTransform& getCoordTransform() const { return fCoordTransform; }
438
Brian Salomon6af27012017-06-09 08:21:42 -0400439 /** Checks whether the constructor failed to fully initialize the processor. */
Brian Salomon06e547c2017-06-09 16:11:32 -0400440 bool isValid() const {
441 return fColorType != kTexture_ColorType || fTextureSampler.isInitialized();
442 }
Brian Salomon6af27012017-06-09 08:21:42 -0400443
bsalomon@google.comd4726202012-08-03 14:34:46 +0000444private:
Brian Salomon587e08f2017-01-27 10:59:27 -0500445 static OptimizationFlags OptFlags(bool isOpaque);
446
brianosmanb9c51372016-09-15 11:09:45 -0700447 // If we're in legacy mode, then fColors will be populated. If we're gamma-correct, then
448 // fColors4f and fColorSpaceXform will be populated.
449 SkTDArray<SkColor> fColors;
450
451 SkTDArray<SkColor4f> fColors4f;
452 sk_sp<GrColorSpaceXform> fColorSpaceXform;
453
454 SkTDArray<SkScalar> fPositions;
455 SkShader::TileMode fTileMode;
fmenozzicd9a1d02016-08-15 07:03:47 -0700456
bsalomon@google.com77af6802013-10-02 13:04:56 +0000457 GrCoordTransform fCoordTransform;
Brian Salomon0bbecb22016-11-17 11:38:22 -0500458 TextureSampler fTextureSampler;
bsalomon@google.com81712882012-11-01 17:12:34 +0000459 SkScalar fYCoord;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000460 GrTextureStripAtlas* fAtlas;
461 int fRow;
bsalomon@google.com371e1052013-01-11 21:08:55 +0000462 bool fIsOpaque;
fmenozzicd9a1d02016-08-15 07:03:47 -0700463 ColorType fColorType;
464 PremulType fPremulType; // This is already baked into the table for texture gradients, and
465 // only changes behavior for gradients that don't use a texture.
joshualittb0a8a372014-09-23 09:50:21 -0700466 typedef GrFragmentProcessor INHERITED;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000467
468};
469
470///////////////////////////////////////////////////////////////////////////////
471
fmenozzicd9a1d02016-08-15 07:03:47 -0700472// Base class for GL gradient effects
fmenozzi55d318d2016-08-09 08:05:57 -0700473class GrGradientEffect::GLSLProcessor : public GrGLSLFragmentProcessor {
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000474public:
fmenozzicd9a1d02016-08-15 07:03:47 -0700475 GLSLProcessor() {
476 fCachedYCoord = SK_ScalarMax;
477 }
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000478
wangyixb1daa862015-08-18 11:29:31 -0700479protected:
Brian Salomonab015ef2017-04-04 10:15:51 -0400480 void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000481
bsalomon@google.comf78df332012-10-29 12:43:38 +0000482protected:
bsalomon63e99f72014-07-21 08:03:14 -0700483 /**
484 * Subclasses must call this. It will return a key for the part of the shader code controlled
485 * by the base class. The subclasses must stick it in their key and then pass it to the below
486 * emit* functions from their emitCode function.
487 */
joshualittb0a8a372014-09-23 09:50:21 -0700488 static uint32_t GenBaseGradientKey(const GrProcessor&);
bsalomon63e99f72014-07-21 08:03:14 -0700489
490 // Emits the uniform used as the y-coord to texture samples in derived classes. Subclasses
491 // should call this method from their emitCode().
egdaniel7ea439b2015-12-03 09:20:44 -0800492 void emitUniforms(GrGLSLUniformHandler*, const GrGradientEffect&);
bsalomon63e99f72014-07-21 08:03:14 -0700493
fmenozzicd9a1d02016-08-15 07:03:47 -0700494 // Emit code that gets a fragment's color from an expression for t; has branches for
495 // several control flows inside -- 2-color gradients, 3-color symmetric gradients, 4+
496 // color gradients that use the traditional texture lookup, as well as several varieties
497 // of hard stop gradients
cdalton85285412016-02-18 12:37:07 -0800498 void emitColor(GrGLSLFPFragmentBuilder* fragBuilder,
egdaniel7ea439b2015-12-03 09:20:44 -0800499 GrGLSLUniformHandler* uniformHandler,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500500 const GrShaderCaps* shaderCaps,
joshualitt60030bc2014-11-25 14:21:55 -0800501 const GrGradientEffect&,
bsalomon63e99f72014-07-21 08:03:14 -0700502 const char* gradientTValue,
bsalomon63e99f72014-07-21 08:03:14 -0700503 const char* outputColor,
504 const char* inputColor,
bsalomonb58a2b42016-09-26 06:55:02 -0700505 const TextureSamplers&);
bsalomon63e99f72014-07-21 08:03:14 -0700506
507private:
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000508 enum {
fmenozzi64e8e5d2016-07-19 10:45:57 -0700509 // First bit for premul before/after interp
fmenozzicd9a1d02016-08-15 07:03:47 -0700510 kPremulBeforeInterpKey = 1,
bsalomon@google.com82d12232013-09-09 15:36:26 +0000511
fmenozzicd9a1d02016-08-15 07:03:47 -0700512 // Next three bits for 2/3 color type or different special
513 // hard stop cases (neither means using texture atlas)
514 kTwoColorKey = 2,
515 kThreeColorKey = 4,
Brian Salomonf8480b92017-07-27 15:45:59 -0400516
fmenozzicd9a1d02016-08-15 07:03:47 -0700517 kHardStopCenteredKey = 6,
518 kHardStopZeroZeroOneKey = 8,
519 kHardStopZeroOneOneKey = 10,
520
521 // Next two bits for tile mode
522 kClampTileMode = 16,
523 kRepeatTileMode = 32,
524 kMirrorTileMode = 48,
525
526 // Lower six bits for premul, 2/3 color type, and tile mode
527 kReservedBits = 6,
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000528 };
529
bsalomon@google.com81712882012-11-01 17:12:34 +0000530 SkScalar fCachedYCoord;
fmenozzicd9a1d02016-08-15 07:03:47 -0700531 GrGLSLProgramDataManager::UniformHandle fColorsUni;
Brian Salomon466ad992016-10-13 16:08:36 -0400532 GrGLSLProgramDataManager::UniformHandle fHardStopT;
egdaniel018fb622015-10-28 07:26:40 -0700533 GrGLSLProgramDataManager::UniformHandle fFSYUni;
Brian Osmanc624d9d2017-03-08 11:42:02 -0500534 GrGLSLColorSpaceXformHelper fColorSpaceHelper;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000535
egdaniel64c47282015-11-13 06:54:19 -0800536 typedef GrGLSLFragmentProcessor INHERITED;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000537};
538
539#endif
540
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000541#endif