blob: a09938242f4c1d23182fc7d248bcb6aedaf52609 [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
22 kUseLCD_DistanceFieldEffectFlag = 0x04, // use lcd text
23 kBGR_DistanceFieldEffectFlag = 0x08, // lcd display has bgr order
24 kPortrait_DistanceFieldEffectFlag = 0x10, // lcd display is in portrait mode (not used yet)
25 kGammaCorrect_DistanceFieldEffectFlag = 0x20, // assume gamma-correct output (linear blending)
Jim Van Verth90e89b32017-07-06 16:36:55 -040026 kAliased_DistanceFieldEffectFlag = 0x40, // monochrome output
jvanverth6d22eca2014-10-28 11:10:48 -070027
28 kInvalid_DistanceFieldEffectFlag = 0x80, // invalid state (for initialization)
halcanary9d524f22016-03-29 09:03:52 -070029
jvanverth78f07182014-07-30 06:17:59 -070030 kUniformScale_DistanceFieldEffectMask = kSimilarity_DistanceFieldEffectFlag |
jvanverthcf371bb2016-03-10 11:10:43 -080031 kScaleOnly_DistanceFieldEffectFlag,
jvanverth502286d2015-04-08 12:37:51 -070032 // The subset of the flags relevant to GrDistanceFieldA8TextGeoProc
jvanverthcf371bb2016-03-10 11:10:43 -080033 kNonLCD_DistanceFieldEffectMask = kSimilarity_DistanceFieldEffectFlag |
brianosman0586f5c2016-04-12 12:48:21 -070034 kScaleOnly_DistanceFieldEffectFlag |
Jim Van Verth90e89b32017-07-06 16:36:55 -040035 kGammaCorrect_DistanceFieldEffectFlag |
36 kAliased_DistanceFieldEffectFlag,
jvanverth502286d2015-04-08 12:37:51 -070037 // The subset of the flags relevant to GrDistanceFieldLCDTextGeoProc
jvanverth78f07182014-07-30 06:17:59 -070038 kLCD_DistanceFieldEffectMask = kSimilarity_DistanceFieldEffectFlag |
jvanverthcf371bb2016-03-10 11:10:43 -080039 kScaleOnly_DistanceFieldEffectFlag |
jvanverth78f07182014-07-30 06:17:59 -070040 kUseLCD_DistanceFieldEffectFlag |
brianosman0586f5c2016-04-12 12:48:21 -070041 kBGR_DistanceFieldEffectFlag |
brianosmanb461d342016-04-13 13:10:14 -070042 kGammaCorrect_DistanceFieldEffectFlag,
jvanverth78f07182014-07-30 06:17:59 -070043};
44
jvanverth@google.comd830d132013-11-11 20:54:09 +000045/**
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000046 * 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 +000047 * distance field texture (using a smoothed step function near 0.5).
Brian Salomon2bbdcc42017-09-07 12:36:34 -040048 * It allows explicit specification of the filtering and wrap modes (GrSamplerState). The input
jvanverth2d2a68c2014-06-10 06:42:56 -070049 * coords are a custom attribute. Gamma correction is handled via a texture LUT.
jvanverth@google.comd830d132013-11-11 20:54:09 +000050 */
jvanverth502286d2015-04-08 12:37:51 -070051class GrDistanceFieldA8TextGeoProc : public GrGeometryProcessor {
jvanverth@google.comd830d132013-11-11 20:54:09 +000052public:
Jim Van Vertha950b632017-09-12 11:54:11 -040053 static constexpr int kMaxTextures = 4;
54
jvanverth2d2a68c2014-06-10 06:42:56 -070055#ifdef SK_GAMMA_APPLY_TO_A8
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040056 static sk_sp<GrGeometryProcessor> Make(GrColor color, const SkMatrix& viewMatrix,
Jim Van Vertha950b632017-09-12 11:54:11 -040057 const sk_sp<GrTextureProxy> proxies[kMaxTextures],
Brian Salomon2bbdcc42017-09-07 12:36:34 -040058 const GrSamplerState& params, float lum, uint32_t flags,
59 bool usesLocalCoords) {
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -050060 return sk_sp<GrGeometryProcessor>(
Jim Van Vertha950b632017-09-12 11:54:11 -040061 new GrDistanceFieldA8TextGeoProc(color, viewMatrix, proxies,
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -050062 params, lum, flags, usesLocalCoords));
63 }
jvanverth2d2a68c2014-06-10 06:42:56 -070064#else
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040065 static sk_sp<GrGeometryProcessor> Make(GrColor color, const SkMatrix& viewMatrix,
Jim Van Vertha950b632017-09-12 11:54:11 -040066 const sk_sp<GrTextureProxy> proxies[kMaxTextures],
Brian Salomon2bbdcc42017-09-07 12:36:34 -040067 const GrSamplerState& params, uint32_t flags,
68 bool usesLocalCoords) {
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -050069 return sk_sp<GrGeometryProcessor>(
Jim Van Vertha950b632017-09-12 11:54:11 -040070 new GrDistanceFieldA8TextGeoProc(color, viewMatrix, proxies,
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -050071 params, flags, usesLocalCoords));
72 }
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; }
joshualitt88c23fc2015-05-13 14:18:07 -070082 GrColor color() const { return fColor; }
joshualitte578a952015-05-14 10:09:13 -070083 const SkMatrix& viewMatrix() const { return fViewMatrix; }
joshualittb8c241a2015-05-19 08:23:30 -070084 bool usesLocalCoords() const { return fUsesLocalCoords; }
jvanverth2d2a68c2014-06-10 06:42:56 -070085#ifdef SK_GAMMA_APPLY_TO_A8
jvanverth21deace2015-04-01 12:43:48 -070086 float getDistanceAdjust() const { return fDistanceAdjust; }
jvanverth2d2a68c2014-06-10 06:42:56 -070087#endif
jvanverth78f07182014-07-30 06:17:59 -070088 uint32_t getFlags() const { return fFlags; }
jvanverth@google.comd830d132013-11-11 20:54:09 +000089
Jim Van Vertheafa64b2017-09-18 10:05:00 -040090 void addNewProxies(const sk_sp<GrTextureProxy> proxies[kMaxTextures], const GrSamplerState& p);
91
Brian Salomon94efbf52016-11-29 13:43:05 -050092 void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override;
jvanverth@google.comd830d132013-11-11 20:54:09 +000093
Brian Salomon94efbf52016-11-29 13:43:05 -050094 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override;
jvanverth@google.comd830d132013-11-11 20:54:09 +000095
96private:
Jim Van Vertha950b632017-09-12 11:54:11 -040097 GrDistanceFieldA8TextGeoProc(GrColor, const SkMatrix& viewMatrix,
98 const sk_sp<GrTextureProxy> proxies[kMaxTextures],
Brian Salomon2bbdcc42017-09-07 12:36:34 -040099 const GrSamplerState& params,
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500100#ifdef SK_GAMMA_APPLY_TO_A8
101 float distanceAdjust,
102#endif
103 uint32_t flags, bool usesLocalCoords);
104
joshualitt88c23fc2015-05-13 14:18:07 -0700105 GrColor fColor;
joshualitte578a952015-05-14 10:09:13 -0700106 SkMatrix fViewMatrix;
Jim Van Vertha950b632017-09-12 11:54:11 -0400107 TextureSampler fTextureSamplers[kMaxTextures];
jvanverth2d2a68c2014-06-10 06:42:56 -0700108#ifdef SK_GAMMA_APPLY_TO_A8
jvanverth21deace2015-04-01 12:43:48 -0700109 float fDistanceAdjust;
jvanverth2d2a68c2014-06-10 06:42:56 -0700110#endif
jvanverth21deace2015-04-01 12:43:48 -0700111 uint32_t fFlags;
joshualitt71c92602015-01-14 08:12:47 -0800112 const Attribute* fInPosition;
113 const Attribute* fInColor;
114 const Attribute* fInTextureCoords;
joshualittb8c241a2015-05-19 08:23:30 -0700115 bool fUsesLocalCoords;
jvanverth@google.comd830d132013-11-11 20:54:09 +0000116
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400117 GR_DECLARE_GEOMETRY_PROCESSOR_TEST
jvanverth@google.comd830d132013-11-11 20:54:09 +0000118
jvanverthfa38a302014-10-06 05:59:05 -0700119 typedef GrGeometryProcessor INHERITED;
120};
121
jvanverthfa38a302014-10-06 05:59:05 -0700122/**
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400123 * The output color of this effect is a modulation of the input color and a sample from a
124 * distance field texture (using a smoothed step function near 0.5).
125 * It allows explicit specification of the filtering and wrap modes (GrSamplerState). The input
126 * coords are a custom attribute. No gamma correct blending is applied. Used for paths only.
127 */
jvanverth502286d2015-04-08 12:37:51 -0700128class GrDistanceFieldPathGeoProc : public GrGeometryProcessor {
jvanverthfa38a302014-10-06 05:59:05 -0700129public:
Jim Van Vertha950b632017-09-12 11:54:11 -0400130 static constexpr int kMaxTextures = 4;
131
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400132 static sk_sp<GrGeometryProcessor> Make(GrColor color, const SkMatrix& matrix,
Jim Van Vertha950b632017-09-12 11:54:11 -0400133 const sk_sp<GrTextureProxy> proxies[kMaxTextures],
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400134 const GrSamplerState& params, uint32_t flags) {
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500135 return sk_sp<GrGeometryProcessor>(
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400136 new GrDistanceFieldPathGeoProc(color, matrix, proxies, params, flags));
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500137 }
138
139 ~GrDistanceFieldPathGeoProc() override {}
jvanverthfa38a302014-10-06 05:59:05 -0700140
bsalomon32d1e952016-09-15 07:29:52 -0700141 const char* name() const override { return "DistanceFieldPath"; }
jvanverthfa38a302014-10-06 05:59:05 -0700142
joshualitt71c92602015-01-14 08:12:47 -0800143 const Attribute* inPosition() const { return fInPosition; }
144 const Attribute* inColor() const { return fInColor; }
145 const Attribute* inTextureCoords() const { return fInTextureCoords; }
joshualitt88c23fc2015-05-13 14:18:07 -0700146 GrColor color() const { return fColor; }
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400147 const SkMatrix& matrix() const { return fMatrix; }
jvanverthfa38a302014-10-06 05:59:05 -0700148 uint32_t getFlags() const { return fFlags; }
149
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400150 void addNewProxies(const sk_sp<GrTextureProxy> proxies[kMaxTextures], const GrSamplerState& p);
151
Brian Salomon94efbf52016-11-29 13:43:05 -0500152 void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override;
jvanverthfa38a302014-10-06 05:59:05 -0700153
Brian Salomon94efbf52016-11-29 13:43:05 -0500154 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override;
jvanverthfa38a302014-10-06 05:59:05 -0700155
156private:
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400157 GrDistanceFieldPathGeoProc(GrColor, const SkMatrix& matrix,
Jim Van Vertha950b632017-09-12 11:54:11 -0400158 const sk_sp<GrTextureProxy> proxies[kMaxTextures],
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400159 const GrSamplerState&, uint32_t flags);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500160
joshualitt88c23fc2015-05-13 14:18:07 -0700161 GrColor fColor;
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
Jim Van Vertha950b632017-09-12 11:54:11 -0400197 static constexpr int kMaxTextures = 4;
198
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400199 static sk_sp<GrGeometryProcessor> Make(GrColor color,
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500200 const SkMatrix& viewMatrix,
Jim Van Vertha950b632017-09-12 11:54:11 -0400201 const sk_sp<GrTextureProxy> proxies[kMaxTextures],
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400202 const GrSamplerState& params,
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400203 DistanceAdjust distanceAdjust,
204 uint32_t flags,
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500205 bool usesLocalCoords) {
206 return sk_sp<GrGeometryProcessor>(
Jim Van Vertha950b632017-09-12 11:54:11 -0400207 new GrDistanceFieldLCDTextGeoProc(color, viewMatrix, proxies,
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500208 params, distanceAdjust,
209 flags, usesLocalCoords));
210 }
211
212 ~GrDistanceFieldLCDTextGeoProc() override {}
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000213
bsalomon32d1e952016-09-15 07:29:52 -0700214 const char* name() const override { return "DistanceFieldLCDText"; }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000215
joshualitt71c92602015-01-14 08:12:47 -0800216 const Attribute* inPosition() const { return fInPosition; }
joshualittd9d30f72015-12-08 10:47:55 -0800217 const Attribute* inColor() const { return fInColor; }
joshualitt71c92602015-01-14 08:12:47 -0800218 const Attribute* inTextureCoords() const { return fInTextureCoords; }
jvanverth21deace2015-04-01 12:43:48 -0700219 DistanceAdjust getDistanceAdjust() const { return fDistanceAdjust; }
joshualitt88c23fc2015-05-13 14:18:07 -0700220 GrColor color() const { return fColor; }
joshualitte578a952015-05-14 10:09:13 -0700221 const SkMatrix& viewMatrix() const { return fViewMatrix; }
jvanverth78f07182014-07-30 06:17:59 -0700222 uint32_t getFlags() const { return fFlags; }
joshualittb8c241a2015-05-19 08:23:30 -0700223 bool usesLocalCoords() const { return fUsesLocalCoords; }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000224
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400225 void addNewProxies(const sk_sp<GrTextureProxy> proxies[kMaxTextures], const GrSamplerState& p);
226
Brian Salomon94efbf52016-11-29 13:43:05 -0500227 void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000228
Brian Salomon94efbf52016-11-29 13:43:05 -0500229 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000230
231private:
Jim Van Vertha950b632017-09-12 11:54:11 -0400232 GrDistanceFieldLCDTextGeoProc(GrColor, const SkMatrix& viewMatrix,
233 const sk_sp<GrTextureProxy> proxies[kMaxTextures],
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400234 const GrSamplerState& params, DistanceAdjust wa, uint32_t flags,
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500235 bool usesLocalCoords);
236
joshualitt88c23fc2015-05-13 14:18:07 -0700237 GrColor fColor;
joshualitte578a952015-05-14 10:09:13 -0700238 SkMatrix fViewMatrix;
Jim Van Vertha950b632017-09-12 11:54:11 -0400239 TextureSampler fTextureSamplers[kMaxTextures];
jvanverth21deace2015-04-01 12:43:48 -0700240 DistanceAdjust fDistanceAdjust;
241 uint32_t fFlags;
joshualitt71c92602015-01-14 08:12:47 -0800242 const Attribute* fInPosition;
joshualittd9d30f72015-12-08 10:47:55 -0800243 const Attribute* fInColor;
joshualitt71c92602015-01-14 08:12:47 -0800244 const Attribute* fInTextureCoords;
joshualittb8c241a2015-05-19 08:23:30 -0700245 bool fUsesLocalCoords;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000246
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400247 GR_DECLARE_GEOMETRY_PROCESSOR_TEST
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000248
jvanverthfa38a302014-10-06 05:59:05 -0700249 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000250};
251
jvanverth@google.comd830d132013-11-11 20:54:09 +0000252#endif