blob: 437e8308f518873090dafca954746b21b713dc75 [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
Florin Malita50b20842017-07-29 19:08:28 -0400236 virtual void appendGradientStages(SkArenaAlloc* alloc, SkRasterPipeline* tPipeline,
237 SkRasterPipeline* postPipeline) const = 0;
Mike Kleina3771842017-05-04 19:38:48 -0400238
fmalita088e21b2016-10-05 09:28:42 -0700239 template <typename T, typename... Args>
Herb Derby83e939b2017-02-07 14:25:11 -0500240 static Context* CheckedMakeContext(SkArenaAlloc* alloc, Args&&... args) {
241 auto* ctx = alloc->make<T>(std::forward<Args>(args)...);
fmalita088e21b2016-10-05 09:28:42 -0700242 if (!ctx->isValid()) {
fmalita088e21b2016-10-05 09:28:42 -0700243 return nullptr;
244 }
245 return ctx;
246 }
247
Mike Kleina3771842017-05-04 19:38:48 -0400248 const SkMatrix fPtsToUnit;
249 TileMode fTileMode;
250 TileProc fTileProc;
251 uint8_t fGradFlags;
252 Rec* fRecs;
253
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000254private:
255 enum {
256 kColorStorageCount = 4, // more than this many colors, and we'll use sk_malloc for the space
257
brianosmanb9c51372016-09-15 11:09:45 -0700258 kStorageSize = kColorStorageCount *
259 (sizeof(SkColor) + sizeof(SkScalar) + sizeof(Rec) + sizeof(SkColor4f))
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000260 };
brianosmanb9c51372016-09-15 11:09:45 -0700261 SkColor fStorage[(kStorageSize + 3) >> 2];
reedf3182eb2015-11-17 08:12:19 -0800262public:
brianosmanb9c51372016-09-15 11:09:45 -0700263 SkColor* fOrigColors; // original colors, before modulation by paint in context.
264 SkColor4f* fOrigColors4f; // original colors, as linear floats
265 SkScalar* fOrigPos; // original positions
266 int fColorCount;
267 sk_sp<SkColorSpace> fColorSpace; // color space of gradient stops
reedf3182eb2015-11-17 08:12:19 -0800268
269 bool colorsAreOpaque() const { return fColorsAreOpaque; }
270
fmenozzicd9a1d02016-08-15 07:03:47 -0700271 TileMode getTileMode() const { return fTileMode; }
272 Rec* getRecs() const { return fRecs; }
273
reedf3182eb2015-11-17 08:12:19 -0800274private:
brianosmanb9c51372016-09-15 11:09:45 -0700275 bool fColorsAreOpaque;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000276
Hal Canary67b39de2016-11-07 11:47:44 -0500277 sk_sp<GradientShaderCache> refCache(U8CPU alpha, bool dither) const;
278 mutable SkMutex fCacheMutex;
279 mutable sk_sp<GradientShaderCache> fCache;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000280
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000281 void initCommon();
282
Florin Malita4aed1382017-05-25 10:38:07 -0400283 typedef SkShaderBase INHERITED;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000284};
285
Mike Kleina3771842017-05-04 19:38:48 -0400286
reed@google.com55853db2013-02-01 19:34:59 +0000287static inline int init_dither_toggle(int x, int y) {
reed@google.com60040292013-02-04 18:21:23 +0000288 x &= 1;
289 y = (y & 1) << 1;
290 return (x | y) * SkGradientShaderBase::kDitherStride32;
reed@google.com55853db2013-02-01 19:34:59 +0000291}
292
293static inline int next_dither_toggle(int toggle) {
294 return toggle ^ SkGradientShaderBase::kDitherStride32;
295}
296
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000297///////////////////////////////////////////////////////////////////////////////
298
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000299#if SK_SUPPORT_GPU
300
brianosmanb9c51372016-09-15 11:09:45 -0700301#include "GrColorSpaceXform.h"
bsalomon@google.com77af6802013-10-02 13:04:56 +0000302#include "GrCoordTransform.h"
bsalomon6251d172014-10-15 10:50:36 -0700303#include "GrFragmentProcessor.h"
Brian Osmanc624d9d2017-03-08 11:42:02 -0500304#include "glsl/GrGLSLColorSpaceXformHelper.h"
egdaniel64c47282015-11-13 06:54:19 -0800305#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel018fb622015-10-28 07:26:40 -0700306#include "glsl/GrGLSLProgramDataManager.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000307
egdaniel605dd0f2014-11-12 08:35:25 -0800308class GrInvariantOutput;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000309
310/*
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000311 * The interpretation of the texture matrix depends on the sample mode. The
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000312 * texture matrix is applied both when the texture coordinates are explicit
313 * and when vertex positions are used as texture coordinates. In the latter
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000314 * case the texture matrix is applied to the pre-view-matrix position
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000315 * values.
316 *
317 * Normal SampleMode
318 * The post-matrix texture coordinates are in normalize space with (0,0) at
319 * the top-left and (1,1) at the bottom right.
320 * RadialGradient
321 * The matrix specifies the radial gradient parameters.
322 * (0,0) in the post-matrix space is center of the radial gradient.
323 * Radial2Gradient
324 * Matrix transforms to space where first circle is centered at the
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000325 * origin. The second circle will be centered (x, 0) where x may be
326 * 0 and is provided by setRadial2Params. The post-matrix space is
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000327 * normalized such that 1 is the second radius - first radius.
328 * SweepGradient
329 * The angle from the origin of texture coordinates in post-matrix space
330 * determines the gradient value.
331 */
332
rileya@google.comb3e50f22012-08-20 17:43:08 +0000333 class GrTextureStripAtlas;
334
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000335// Base class for Gr gradient effects
joshualittb0a8a372014-09-23 09:50:21 -0700336class GrGradientEffect : public GrFragmentProcessor {
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000337public:
brianosman9557c272016-09-15 06:59:15 -0700338 struct CreateArgs {
339 CreateArgs(GrContext* context,
340 const SkGradientShaderBase* shader,
341 const SkMatrix* matrix,
brianosmanb9c51372016-09-15 11:09:45 -0700342 SkShader::TileMode tileMode,
343 sk_sp<GrColorSpaceXform> colorSpaceXform,
344 bool gammaCorrect)
brianosman9557c272016-09-15 06:59:15 -0700345 : fContext(context)
346 , fShader(shader)
347 , fMatrix(matrix)
brianosmanb9c51372016-09-15 11:09:45 -0700348 , fTileMode(tileMode)
349 , fColorSpaceXform(std::move(colorSpaceXform))
350 , fGammaCorrect(gammaCorrect) {}
brianosman9557c272016-09-15 06:59:15 -0700351
352 GrContext* fContext;
353 const SkGradientShaderBase* fShader;
354 const SkMatrix* fMatrix;
355 SkShader::TileMode fTileMode;
brianosmanb9c51372016-09-15 11:09:45 -0700356 sk_sp<GrColorSpaceXform> fColorSpaceXform;
357 bool fGammaCorrect;
brianosman9557c272016-09-15 06:59:15 -0700358 };
359
fmenozzi55d318d2016-08-09 08:05:57 -0700360 class GLSLProcessor;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000361
Brian Salomond3b65972017-03-22 12:05:03 -0400362 ~GrGradientEffect() override;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000363
rileya@google.comb3e50f22012-08-20 17:43:08 +0000364 bool useAtlas() const { return SkToBool(-1 != fRow); }
fmenozzicd9a1d02016-08-15 07:03:47 -0700365 SkScalar getYCoord() const { return fYCoord; }
rileya@google.comb3e50f22012-08-20 17:43:08 +0000366
fmenozzicd9a1d02016-08-15 07:03:47 -0700367 enum ColorType {
368 kTwo_ColorType,
369 kThree_ColorType, // Symmetric three color
370 kTexture_ColorType,
Brian Salomon466ad992016-10-13 16:08:36 -0400371 kSingleHardStop_ColorType, // 0, t, t, 1
fmenozzicd9a1d02016-08-15 07:03:47 -0700372 kHardStopLeftEdged_ColorType, // 0, 0, 1
373 kHardStopRightEdged_ColorType, // 0, 1, 1
fmenozzicd9a1d02016-08-15 07:03:47 -0700374 };
375
376 ColorType getColorType() const { return fColorType; }
377
378 // Determines the type of gradient, one of:
379 // - Two-color
380 // - Symmetric three-color
381 // - Texture
382 // - Centered hard stop
383 // - Left-edged hard stop
384 // - Right-edged hard stop
385 ColorType determineColorType(const SkGradientShaderBase& shader);
bsalomon@google.com82d12232013-09-09 15:36:26 +0000386
387 enum PremulType {
388 kBeforeInterp_PremulType,
389 kAfterInterp_PremulType,
390 };
391
392 PremulType getPremulType() const { return fPremulType; }
393
394 const SkColor* getColors(int pos) const {
fmenozzicd9a1d02016-08-15 07:03:47 -0700395 SkASSERT(fColorType != kTexture_ColorType);
396 SkASSERT(pos < fColors.count());
bsalomon@google.com82d12232013-09-09 15:36:26 +0000397 return &fColors[pos];
398 }
bsalomon@google.com371e1052013-01-11 21:08:55 +0000399
brianosmanb9c51372016-09-15 11:09:45 -0700400 const SkColor4f* getColors4f(int pos) const {
401 SkASSERT(fColorType != kTexture_ColorType);
402 SkASSERT(pos < fColors4f.count());
403 return &fColors4f[pos];
404 }
405
bsalomon@google.comd4726202012-08-03 14:34:46 +0000406protected:
Brian Salomon587e08f2017-01-27 10:59:27 -0500407 GrGradientEffect(const CreateArgs&, bool isOpaque);
Brian Salomonf8480b92017-07-27 15:45:59 -0400408 explicit GrGradientEffect(const GrGradientEffect&); // facilitates clone() implementations
Brian Salomon587e08f2017-01-27 10:59:27 -0500409
Hal Canary6f6961e2017-01-31 13:50:44 -0500410 #if GR_TEST_UTILS
Brian Osmane75c19f2016-10-10 11:26:43 -0400411 /** Helper struct that stores (and populates) parameters to construct a random gradient.
Brian Osmana2196532016-10-17 12:48:13 -0400412 If fUseColors4f is true, then the SkColor4f factory should be called, with fColors4f and
413 fColorSpace. Otherwise, the SkColor factory should be called, with fColors. fColorCount
414 will be the number of color stops in either case, and fColors and fStops can be passed to
415 the gradient factory. (The constructor may decide not to use stops, in which case fStops
416 will be nullptr). */
Brian Osman3f748602016-10-03 18:29:03 -0400417 struct RandomGradientParams {
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500418 static const int kMaxRandomGradientColors = 5;
Brian Osmane75c19f2016-10-10 11:26:43 -0400419
Brian Osman3f748602016-10-03 18:29:03 -0400420 RandomGradientParams(SkRandom* r);
421
Brian Osmana2196532016-10-17 12:48:13 -0400422 bool fUseColors4f;
Brian Osman3f748602016-10-03 18:29:03 -0400423 SkColor fColors[kMaxRandomGradientColors];
Brian Osmana2196532016-10-17 12:48:13 -0400424 SkColor4f fColors4f[kMaxRandomGradientColors];
425 sk_sp<SkColorSpace> fColorSpace;
Brian Osman3f748602016-10-03 18:29:03 -0400426 SkScalar fStopStorage[kMaxRandomGradientColors];
427 SkShader::TileMode fTileMode;
428 int fColorCount;
429 SkScalar* fStops;
430 };
Hal Canary6f6961e2017-01-31 13:50:44 -0500431 #endif
bsalomon@google.comd4726202012-08-03 14:34:46 +0000432
mtklein36352bf2015-03-25 18:17:31 -0700433 bool onIsEqual(const GrFragmentProcessor&) const override;
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000434
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +0000435 const GrCoordTransform& getCoordTransform() const { return fCoordTransform; }
436
Brian Salomon6af27012017-06-09 08:21:42 -0400437 /** Checks whether the constructor failed to fully initialize the processor. */
Brian Salomon06e547c2017-06-09 16:11:32 -0400438 bool isValid() const {
439 return fColorType != kTexture_ColorType || fTextureSampler.isInitialized();
440 }
Brian Salomon6af27012017-06-09 08:21:42 -0400441
bsalomon@google.comd4726202012-08-03 14:34:46 +0000442private:
Brian Salomon587e08f2017-01-27 10:59:27 -0500443 static OptimizationFlags OptFlags(bool isOpaque);
444
brianosmanb9c51372016-09-15 11:09:45 -0700445 // If we're in legacy mode, then fColors will be populated. If we're gamma-correct, then
446 // fColors4f and fColorSpaceXform will be populated.
447 SkTDArray<SkColor> fColors;
448
449 SkTDArray<SkColor4f> fColors4f;
450 sk_sp<GrColorSpaceXform> fColorSpaceXform;
451
452 SkTDArray<SkScalar> fPositions;
453 SkShader::TileMode fTileMode;
fmenozzicd9a1d02016-08-15 07:03:47 -0700454
bsalomon@google.com77af6802013-10-02 13:04:56 +0000455 GrCoordTransform fCoordTransform;
Brian Salomon0bbecb22016-11-17 11:38:22 -0500456 TextureSampler fTextureSampler;
bsalomon@google.com81712882012-11-01 17:12:34 +0000457 SkScalar fYCoord;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000458 GrTextureStripAtlas* fAtlas;
459 int fRow;
bsalomon@google.com371e1052013-01-11 21:08:55 +0000460 bool fIsOpaque;
fmenozzicd9a1d02016-08-15 07:03:47 -0700461 ColorType fColorType;
462 PremulType fPremulType; // This is already baked into the table for texture gradients, and
463 // only changes behavior for gradients that don't use a texture.
joshualittb0a8a372014-09-23 09:50:21 -0700464 typedef GrFragmentProcessor INHERITED;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000465
466};
467
468///////////////////////////////////////////////////////////////////////////////
469
fmenozzicd9a1d02016-08-15 07:03:47 -0700470// Base class for GL gradient effects
fmenozzi55d318d2016-08-09 08:05:57 -0700471class GrGradientEffect::GLSLProcessor : public GrGLSLFragmentProcessor {
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000472public:
fmenozzicd9a1d02016-08-15 07:03:47 -0700473 GLSLProcessor() {
474 fCachedYCoord = SK_ScalarMax;
475 }
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000476
wangyixb1daa862015-08-18 11:29:31 -0700477protected:
Brian Salomonab015ef2017-04-04 10:15:51 -0400478 void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000479
bsalomon@google.comf78df332012-10-29 12:43:38 +0000480protected:
bsalomon63e99f72014-07-21 08:03:14 -0700481 /**
482 * Subclasses must call this. It will return a key for the part of the shader code controlled
483 * by the base class. The subclasses must stick it in their key and then pass it to the below
484 * emit* functions from their emitCode function.
485 */
joshualittb0a8a372014-09-23 09:50:21 -0700486 static uint32_t GenBaseGradientKey(const GrProcessor&);
bsalomon63e99f72014-07-21 08:03:14 -0700487
488 // Emits the uniform used as the y-coord to texture samples in derived classes. Subclasses
489 // should call this method from their emitCode().
egdaniel7ea439b2015-12-03 09:20:44 -0800490 void emitUniforms(GrGLSLUniformHandler*, const GrGradientEffect&);
bsalomon63e99f72014-07-21 08:03:14 -0700491
fmenozzicd9a1d02016-08-15 07:03:47 -0700492 // Emit code that gets a fragment's color from an expression for t; has branches for
493 // several control flows inside -- 2-color gradients, 3-color symmetric gradients, 4+
494 // color gradients that use the traditional texture lookup, as well as several varieties
495 // of hard stop gradients
cdalton85285412016-02-18 12:37:07 -0800496 void emitColor(GrGLSLFPFragmentBuilder* fragBuilder,
egdaniel7ea439b2015-12-03 09:20:44 -0800497 GrGLSLUniformHandler* uniformHandler,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500498 const GrShaderCaps* shaderCaps,
joshualitt60030bc2014-11-25 14:21:55 -0800499 const GrGradientEffect&,
bsalomon63e99f72014-07-21 08:03:14 -0700500 const char* gradientTValue,
bsalomon63e99f72014-07-21 08:03:14 -0700501 const char* outputColor,
502 const char* inputColor,
bsalomonb58a2b42016-09-26 06:55:02 -0700503 const TextureSamplers&);
bsalomon63e99f72014-07-21 08:03:14 -0700504
505private:
Florin Malitab81a8b92017-08-08 12:14:17 -0400506 void emitAnalyticalColor(GrGLSLFPFragmentBuilder* fragBuilder,
507 GrGLSLUniformHandler* uniformHandler,
508 const GrShaderCaps* shaderCaps,
509 const GrGradientEffect&,
510 const char* gradientTValue,
511 const char* outputColor,
512 const char* inputColor);
513
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000514 enum {
fmenozzi64e8e5d2016-07-19 10:45:57 -0700515 // First bit for premul before/after interp
fmenozzicd9a1d02016-08-15 07:03:47 -0700516 kPremulBeforeInterpKey = 1,
bsalomon@google.com82d12232013-09-09 15:36:26 +0000517
fmenozzicd9a1d02016-08-15 07:03:47 -0700518 // Next three bits for 2/3 color type or different special
519 // hard stop cases (neither means using texture atlas)
520 kTwoColorKey = 2,
521 kThreeColorKey = 4,
Brian Salomonf8480b92017-07-27 15:45:59 -0400522
fmenozzicd9a1d02016-08-15 07:03:47 -0700523 kHardStopCenteredKey = 6,
524 kHardStopZeroZeroOneKey = 8,
525 kHardStopZeroOneOneKey = 10,
526
527 // Next two bits for tile mode
528 kClampTileMode = 16,
529 kRepeatTileMode = 32,
530 kMirrorTileMode = 48,
531
532 // Lower six bits for premul, 2/3 color type, and tile mode
533 kReservedBits = 6,
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000534 };
535
bsalomon@google.com81712882012-11-01 17:12:34 +0000536 SkScalar fCachedYCoord;
fmenozzicd9a1d02016-08-15 07:03:47 -0700537 GrGLSLProgramDataManager::UniformHandle fColorsUni;
Brian Salomon466ad992016-10-13 16:08:36 -0400538 GrGLSLProgramDataManager::UniformHandle fHardStopT;
egdaniel018fb622015-10-28 07:26:40 -0700539 GrGLSLProgramDataManager::UniformHandle fFSYUni;
Brian Osmanc624d9d2017-03-08 11:42:02 -0500540 GrGLSLColorSpaceXformHelper fColorSpaceHelper;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000541
egdaniel64c47282015-11-13 06:54:19 -0800542 typedef GrGLSLFragmentProcessor INHERITED;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000543};
544
545#endif
546
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000547#endif