blob: 4ac6ae030423d3c4bb11f20019ae55a7385b4a84 [file] [log] [blame]
jvanverth@google.comd830d132013-11-11 20:54:09 +00001/*
2 * Copyright 2013 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
jvanverth502286d2015-04-08 12:37:51 -07008#ifndef GrDistanceFieldGeoProc_DEFINED
9#define GrDistanceFieldGeoProc_DEFINED
jvanverth@google.comd830d132013-11-11 20:54:09 +000010
joshualittb0a8a372014-09-23 09:50:21 -070011#include "GrProcessor.h"
joshualitt249af152014-09-15 11:41:13 -070012#include "GrGeometryProcessor.h"
jvanverth@google.comd830d132013-11-11 20:54:09 +000013
jvanverth502286d2015-04-08 12:37:51 -070014class GrGLDistanceFieldA8TextGeoProc;
15class GrGLDistanceFieldPathGeoProc;
16class GrGLDistanceFieldLCDTextGeoProc;
egdaniel605dd0f2014-11-12 08:35:25 -080017class GrInvariantOutput;
jvanverth@google.comd830d132013-11-11 20:54:09 +000018
jvanverth78f07182014-07-30 06:17:59 -070019enum GrDistanceFieldEffectFlags {
brianosmanb461d342016-04-13 13:10:14 -070020 kSimilarity_DistanceFieldEffectFlag = 0x01, // ctm is similarity matrix
21 kScaleOnly_DistanceFieldEffectFlag = 0x02, // ctm has only scale and translate
Brian Salomon5c6ac642017-12-19 11:09:32 -050022 kPerspective_DistanceFieldEffectFlag = 0x04, // ctm has perspective (and positions are x,y,w)
23 kUseLCD_DistanceFieldEffectFlag = 0x08, // use lcd text
24 kBGR_DistanceFieldEffectFlag = 0x10, // lcd display has bgr order
25 kPortrait_DistanceFieldEffectFlag = 0x20, // lcd display is in portrait mode (not used yet)
26 kGammaCorrect_DistanceFieldEffectFlag = 0x40, // assume gamma-correct output (linear blending)
27 kAliased_DistanceFieldEffectFlag = 0x80, // monochrome output
jvanverth6d22eca2014-10-28 11:10:48 -070028
Brian Salomon5c6ac642017-12-19 11:09:32 -050029 kInvalid_DistanceFieldEffectFlag = 0x100, // invalid state (for initialization)
halcanary9d524f22016-03-29 09:03:52 -070030
jvanverth78f07182014-07-30 06:17:59 -070031 kUniformScale_DistanceFieldEffectMask = kSimilarity_DistanceFieldEffectFlag |
jvanverthcf371bb2016-03-10 11:10:43 -080032 kScaleOnly_DistanceFieldEffectFlag,
jvanverth502286d2015-04-08 12:37:51 -070033 // The subset of the flags relevant to GrDistanceFieldA8TextGeoProc
jvanverthcf371bb2016-03-10 11:10:43 -080034 kNonLCD_DistanceFieldEffectMask = kSimilarity_DistanceFieldEffectFlag |
brianosman0586f5c2016-04-12 12:48:21 -070035 kScaleOnly_DistanceFieldEffectFlag |
Brian Salomon5c6ac642017-12-19 11:09:32 -050036 kPerspective_DistanceFieldEffectFlag |
Jim Van Verth90e89b32017-07-06 16:36:55 -040037 kGammaCorrect_DistanceFieldEffectFlag |
38 kAliased_DistanceFieldEffectFlag,
jvanverth502286d2015-04-08 12:37:51 -070039 // The subset of the flags relevant to GrDistanceFieldLCDTextGeoProc
jvanverth78f07182014-07-30 06:17:59 -070040 kLCD_DistanceFieldEffectMask = kSimilarity_DistanceFieldEffectFlag |
jvanverthcf371bb2016-03-10 11:10:43 -080041 kScaleOnly_DistanceFieldEffectFlag |
Brian Salomon5c6ac642017-12-19 11:09:32 -050042 kPerspective_DistanceFieldEffectFlag |
jvanverth78f07182014-07-30 06:17:59 -070043 kUseLCD_DistanceFieldEffectFlag |
brianosman0586f5c2016-04-12 12:48:21 -070044 kBGR_DistanceFieldEffectFlag |
brianosmanb461d342016-04-13 13:10:14 -070045 kGammaCorrect_DistanceFieldEffectFlag,
jvanverth78f07182014-07-30 06:17:59 -070046};
47
jvanverth@google.comd830d132013-11-11 20:54:09 +000048/**
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000049 * The output color of this effect is a modulation of the input color and a sample from a
jvanverth@google.comd830d132013-11-11 20:54:09 +000050 * distance field texture (using a smoothed step function near 0.5).
Brian Salomon2bbdcc42017-09-07 12:36:34 -040051 * It allows explicit specification of the filtering and wrap modes (GrSamplerState). The input
jvanverth2d2a68c2014-06-10 06:42:56 -070052 * coords are a custom attribute. Gamma correction is handled via a texture LUT.
jvanverth@google.comd830d132013-11-11 20:54:09 +000053 */
jvanverth502286d2015-04-08 12:37:51 -070054class GrDistanceFieldA8TextGeoProc : public GrGeometryProcessor {
jvanverth@google.comd830d132013-11-11 20:54:09 +000055public:
Brian Salomon5c6ac642017-12-19 11:09:32 -050056 /** The local matrix should be identity if local coords are not required by the GrPipeline. */
jvanverth2d2a68c2014-06-10 06:42:56 -070057#ifdef SK_GAMMA_APPLY_TO_A8
Robert Phillips4bc70112018-03-01 10:24:02 -050058 static sk_sp<GrGeometryProcessor> Make(const sk_sp<GrTextureProxy>* proxies,
59 int numProxies,
Brian Salomon2bbdcc42017-09-07 12:36:34 -040060 const GrSamplerState& params, float lum, uint32_t flags,
Brian Salomon5c6ac642017-12-19 11:09:32 -050061 const SkMatrix& localMatrixIfUsesLocalCoords) {
62 return sk_sp<GrGeometryProcessor>(new GrDistanceFieldA8TextGeoProc(
Robert Phillips4bc70112018-03-01 10:24:02 -050063 proxies, numProxies, params, lum, flags, localMatrixIfUsesLocalCoords));
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -050064 }
jvanverth2d2a68c2014-06-10 06:42:56 -070065#else
Robert Phillips4bc70112018-03-01 10:24:02 -050066 static sk_sp<GrGeometryProcessor> Make(const sk_sp<GrTextureProxy>* proxies,
67 int numProxies,
Brian Salomon2bbdcc42017-09-07 12:36:34 -040068 const GrSamplerState& params, uint32_t flags,
Brian Salomon5c6ac642017-12-19 11:09:32 -050069 const SkMatrix& localMatrixIfUsesLocalCoords) {
70 return sk_sp<GrGeometryProcessor>(new GrDistanceFieldA8TextGeoProc(
Robert Phillips4bc70112018-03-01 10:24:02 -050071 proxies, numProxies, params, flags, localMatrixIfUsesLocalCoords));
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -050072 }
bsalomon55fad7a2014-07-08 07:34:20 -070073#endif
jvanverth@google.comd830d132013-11-11 20:54:09 +000074
Brian Salomond3b65972017-03-22 12:05:03 -040075 ~GrDistanceFieldA8TextGeoProc() override {}
jvanverth@google.comd830d132013-11-11 20:54:09 +000076
bsalomon32d1e952016-09-15 07:29:52 -070077 const char* name() const override { return "DistanceFieldA8Text"; }
jvanverth@google.comd830d132013-11-11 20:54:09 +000078
joshualitt71c92602015-01-14 08:12:47 -080079 const Attribute* inPosition() const { return fInPosition; }
80 const Attribute* inColor() const { return fInColor; }
81 const Attribute* inTextureCoords() const { return fInTextureCoords; }
Brian Salomon5c6ac642017-12-19 11:09:32 -050082 const SkMatrix& localMatrix() const { return fLocalMatrix; }
jvanverth2d2a68c2014-06-10 06:42:56 -070083#ifdef SK_GAMMA_APPLY_TO_A8
jvanverth21deace2015-04-01 12:43:48 -070084 float getDistanceAdjust() const { return fDistanceAdjust; }
jvanverth2d2a68c2014-06-10 06:42:56 -070085#endif
jvanverth78f07182014-07-30 06:17:59 -070086 uint32_t getFlags() const { return fFlags; }
jvanverth@google.comd830d132013-11-11 20:54:09 +000087
Robert Phillips4bc70112018-03-01 10:24:02 -050088 void addNewProxies(const sk_sp<GrTextureProxy>* proxies, int numProxies, const GrSamplerState&);
Jim Van Vertheafa64b2017-09-18 10:05:00 -040089
Brian Salomon94efbf52016-11-29 13:43:05 -050090 void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override;
jvanverth@google.comd830d132013-11-11 20:54:09 +000091
Brian Salomon94efbf52016-11-29 13:43:05 -050092 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override;
jvanverth@google.comd830d132013-11-11 20:54:09 +000093
94private:
Robert Phillips4bc70112018-03-01 10:24:02 -050095 GrDistanceFieldA8TextGeoProc(const sk_sp<GrTextureProxy>* proxies,
96 int numProxies,
Brian Salomon2bbdcc42017-09-07 12:36:34 -040097 const GrSamplerState& params,
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -050098#ifdef SK_GAMMA_APPLY_TO_A8
99 float distanceAdjust,
100#endif
Brian Salomon5c6ac642017-12-19 11:09:32 -0500101 uint32_t flags, const SkMatrix& localMatrix);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500102
Robert Phillips4bc70112018-03-01 10:24:02 -0500103 static constexpr int kMaxTextures = 4;
104
Jim Van Vertha950b632017-09-12 11:54:11 -0400105 TextureSampler fTextureSamplers[kMaxTextures];
jvanverth2d2a68c2014-06-10 06:42:56 -0700106#ifdef SK_GAMMA_APPLY_TO_A8
jvanverth21deace2015-04-01 12:43:48 -0700107 float fDistanceAdjust;
jvanverth2d2a68c2014-06-10 06:42:56 -0700108#endif
jvanverth21deace2015-04-01 12:43:48 -0700109 uint32_t fFlags;
joshualitt71c92602015-01-14 08:12:47 -0800110 const Attribute* fInPosition;
111 const Attribute* fInColor;
112 const Attribute* fInTextureCoords;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500113 SkMatrix fLocalMatrix;
jvanverth@google.comd830d132013-11-11 20:54:09 +0000114
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400115 GR_DECLARE_GEOMETRY_PROCESSOR_TEST
jvanverth@google.comd830d132013-11-11 20:54:09 +0000116
jvanverthfa38a302014-10-06 05:59:05 -0700117 typedef GrGeometryProcessor INHERITED;
118};
119
jvanverthfa38a302014-10-06 05:59:05 -0700120/**
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400121 * The output color of this effect is a modulation of the input color and a sample from a
122 * distance field texture (using a smoothed step function near 0.5).
123 * It allows explicit specification of the filtering and wrap modes (GrSamplerState). The input
124 * coords are a custom attribute. No gamma correct blending is applied. Used for paths only.
125 */
jvanverth502286d2015-04-08 12:37:51 -0700126class GrDistanceFieldPathGeoProc : public GrGeometryProcessor {
jvanverthfa38a302014-10-06 05:59:05 -0700127public:
Jim Van Vertha950b632017-09-12 11:54:11 -0400128
Brian Salomon5c6ac642017-12-19 11:09:32 -0500129 /** The local matrix should be identity if local coords are not required by the GrPipeline. */
Brian Osman09068252018-01-03 09:57:29 -0500130 static sk_sp<GrGeometryProcessor> Make(const SkMatrix& matrix,
Robert Phillips4bc70112018-03-01 10:24:02 -0500131 const sk_sp<GrTextureProxy>* proxies,
132 int numProxies,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400133 const GrSamplerState& params, uint32_t flags) {
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500134 return sk_sp<GrGeometryProcessor>(
Robert Phillips4bc70112018-03-01 10:24:02 -0500135 new GrDistanceFieldPathGeoProc(matrix, proxies, numProxies, params, flags));
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500136 }
137
138 ~GrDistanceFieldPathGeoProc() override {}
jvanverthfa38a302014-10-06 05:59:05 -0700139
bsalomon32d1e952016-09-15 07:29:52 -0700140 const char* name() const override { return "DistanceFieldPath"; }
jvanverthfa38a302014-10-06 05:59:05 -0700141
joshualitt71c92602015-01-14 08:12:47 -0800142 const Attribute* inPosition() const { return fInPosition; }
143 const Attribute* inColor() const { return fInColor; }
144 const Attribute* inTextureCoords() const { return fInTextureCoords; }
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400145 const SkMatrix& matrix() const { return fMatrix; }
jvanverthfa38a302014-10-06 05:59:05 -0700146 uint32_t getFlags() const { return fFlags; }
147
Robert Phillips4bc70112018-03-01 10:24:02 -0500148 void addNewProxies(const sk_sp<GrTextureProxy>* proxies, int numProxies, const GrSamplerState&);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400149
Brian Salomon94efbf52016-11-29 13:43:05 -0500150 void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override;
jvanverthfa38a302014-10-06 05:59:05 -0700151
Brian Salomon94efbf52016-11-29 13:43:05 -0500152 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override;
jvanverthfa38a302014-10-06 05:59:05 -0700153
154private:
Robert Phillips4bc70112018-03-01 10:24:02 -0500155 static constexpr int kMaxTextures = 4;
156
Brian Osman09068252018-01-03 09:57:29 -0500157 GrDistanceFieldPathGeoProc(const SkMatrix& matrix,
Robert Phillips4bc70112018-03-01 10:24:02 -0500158 const sk_sp<GrTextureProxy>* proxies,
159 int numProxies,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400160 const GrSamplerState&, uint32_t flags);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500161
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400162 SkMatrix fMatrix; // view matrix if perspective, local matrix otherwise
Jim Van Vertha950b632017-09-12 11:54:11 -0400163 TextureSampler fTextureSamplers[kMaxTextures];
joshualitt88c23fc2015-05-13 14:18:07 -0700164 uint32_t fFlags;
joshualitt71c92602015-01-14 08:12:47 -0800165 const Attribute* fInPosition;
166 const Attribute* fInColor;
167 const Attribute* fInTextureCoords;
jvanverthfa38a302014-10-06 05:59:05 -0700168
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400169 GR_DECLARE_GEOMETRY_PROCESSOR_TEST
jvanverthfa38a302014-10-06 05:59:05 -0700170
171 typedef GrGeometryProcessor INHERITED;
jvanverth@google.comd830d132013-11-11 20:54:09 +0000172};
173
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000174/**
175 * The output color of this effect is a modulation of the input color and samples from a
176 * distance field texture (using a smoothed step function near 0.5), adjusted for LCD displays.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400177 * It allows explicit specification of the filtering and wrap modes (GrSamplerState). The input
jvanverth2d2a68c2014-06-10 06:42:56 -0700178 * coords are a custom attribute. Gamma correction is handled via a texture LUT.
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000179 */
jvanverth502286d2015-04-08 12:37:51 -0700180class GrDistanceFieldLCDTextGeoProc : public GrGeometryProcessor {
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000181public:
jvanverth21deace2015-04-01 12:43:48 -0700182 struct DistanceAdjust {
183 SkScalar fR, fG, fB;
184 static DistanceAdjust Make(SkScalar r, SkScalar g, SkScalar b) {
185 DistanceAdjust result;
186 result.fR = r; result.fG = g; result.fB = b;
187 return result;
188 }
189 bool operator==(const DistanceAdjust& wa) const {
190 return (fR == wa.fR && fG == wa.fG && fB == wa.fB);
191 }
192 bool operator!=(const DistanceAdjust& wa) const {
193 return !(*this == wa);
194 }
195 };
196
Robert Phillips4bc70112018-03-01 10:24:02 -0500197 static sk_sp<GrGeometryProcessor> Make(const sk_sp<GrTextureProxy>* proxies,
198 int numProxies,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400199 const GrSamplerState& params,
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400200 DistanceAdjust distanceAdjust,
201 uint32_t flags,
Brian Salomon5c6ac642017-12-19 11:09:32 -0500202 const SkMatrix& localMatrixIfUsesLocalCoords) {
203 return sk_sp<GrGeometryProcessor>(new GrDistanceFieldLCDTextGeoProc(
Robert Phillips4bc70112018-03-01 10:24:02 -0500204 proxies, numProxies, params, distanceAdjust, flags, localMatrixIfUsesLocalCoords));
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500205 }
206
207 ~GrDistanceFieldLCDTextGeoProc() override {}
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000208
bsalomon32d1e952016-09-15 07:29:52 -0700209 const char* name() const override { return "DistanceFieldLCDText"; }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000210
joshualitt71c92602015-01-14 08:12:47 -0800211 const Attribute* inPosition() const { return fInPosition; }
joshualittd9d30f72015-12-08 10:47:55 -0800212 const Attribute* inColor() const { return fInColor; }
joshualitt71c92602015-01-14 08:12:47 -0800213 const Attribute* inTextureCoords() const { return fInTextureCoords; }
jvanverth21deace2015-04-01 12:43:48 -0700214 DistanceAdjust getDistanceAdjust() const { return fDistanceAdjust; }
jvanverth78f07182014-07-30 06:17:59 -0700215 uint32_t getFlags() const { return fFlags; }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500216 const SkMatrix& localMatrix() const { return fLocalMatrix; }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000217
Robert Phillips4bc70112018-03-01 10:24:02 -0500218 void addNewProxies(const sk_sp<GrTextureProxy>* proxies, int numProxies, const GrSamplerState&);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400219
Brian Salomon94efbf52016-11-29 13:43:05 -0500220 void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000221
Brian Salomon94efbf52016-11-29 13:43:05 -0500222 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000223
224private:
Robert Phillips4bc70112018-03-01 10:24:02 -0500225 GrDistanceFieldLCDTextGeoProc(const sk_sp<GrTextureProxy>* proxies, int numProxies,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400226 const GrSamplerState& params, DistanceAdjust wa, uint32_t flags,
Brian Salomon5c6ac642017-12-19 11:09:32 -0500227 const SkMatrix& localMatrix);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500228
Robert Phillips4bc70112018-03-01 10:24:02 -0500229 static constexpr int kMaxTextures = 4;
230
Jim Van Vertha950b632017-09-12 11:54:11 -0400231 TextureSampler fTextureSamplers[kMaxTextures];
jvanverth21deace2015-04-01 12:43:48 -0700232 DistanceAdjust fDistanceAdjust;
233 uint32_t fFlags;
joshualitt71c92602015-01-14 08:12:47 -0800234 const Attribute* fInPosition;
joshualittd9d30f72015-12-08 10:47:55 -0800235 const Attribute* fInColor;
joshualitt71c92602015-01-14 08:12:47 -0800236 const Attribute* fInTextureCoords;
Brian Salomon5c6ac642017-12-19 11:09:32 -0500237 const SkMatrix fLocalMatrix;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000238
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400239 GR_DECLARE_GEOMETRY_PROCESSOR_TEST
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000240
jvanverthfa38a302014-10-06 05:59:05 -0700241 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000242};
243
jvanverth@google.comd830d132013-11-11 20:54:09 +0000244#endif