blob: a0fb9d74e588b4f772610cc83db6586e36793031 [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,
Jim Van Verthcbeae032018-05-16 14:54:41 -040059 int numActiveProxies,
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(
Jim Van Verthcbeae032018-05-16 14:54:41 -040063 proxies, numActiveProxies, 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,
Jim Van Verthcbeae032018-05-16 14:54:41 -040067 int numActiveProxies,
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(
Jim Van Verthcbeae032018-05-16 14:54:41 -040071 proxies, numActiveProxies, 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
Brian Salomon92be2f72018-06-19 14:33:47 -040079 const Attribute& inPosition() const { return fInPosition; }
80 const Attribute& inColor() const { return kInColor; }
81 const Attribute& inTextureCoords() const { return kInTextureCoords; }
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,
Jim Van Verthcbeae032018-05-16 14:54:41 -040096 int numActiveProxies,
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
Brian Salomon92be2f72018-06-19 14:33:47 -0400103 const Attribute& onVertexAttribute(int i) const override {
104 return IthAttribute(i, fInPosition, kInColor, kInTextureCoords);
105 }
106
Brian Salomonf7dcd762018-07-30 14:48:15 -0400107 const TextureSampler& onTextureSampler(int i) const override { return fTextureSamplers[i]; }
108
Robert Phillips4bc70112018-03-01 10:24:02 -0500109 static constexpr int kMaxTextures = 4;
110
Jim Van Vertha950b632017-09-12 11:54:11 -0400111 TextureSampler fTextureSamplers[kMaxTextures];
Brian Salomon92be2f72018-06-19 14:33:47 -0400112 SkMatrix fLocalMatrix;
113 Attribute fInPosition;
114 uint32_t fFlags;
jvanverth2d2a68c2014-06-10 06:42:56 -0700115#ifdef SK_GAMMA_APPLY_TO_A8
jvanverth21deace2015-04-01 12:43:48 -0700116 float fDistanceAdjust;
jvanverth2d2a68c2014-06-10 06:42:56 -0700117#endif
Brian Salomon92be2f72018-06-19 14:33:47 -0400118
119 static constexpr Attribute kInColor = {"inColor", kUByte4_norm_GrVertexAttribType};
120 static constexpr Attribute kInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType};
jvanverth@google.comd830d132013-11-11 20:54:09 +0000121
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400122 GR_DECLARE_GEOMETRY_PROCESSOR_TEST
jvanverth@google.comd830d132013-11-11 20:54:09 +0000123
jvanverthfa38a302014-10-06 05:59:05 -0700124 typedef GrGeometryProcessor INHERITED;
125};
126
jvanverthfa38a302014-10-06 05:59:05 -0700127/**
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400128 * The output color of this effect is a modulation of the input color and a sample from a
129 * distance field texture (using a smoothed step function near 0.5).
130 * It allows explicit specification of the filtering and wrap modes (GrSamplerState). The input
131 * coords are a custom attribute. No gamma correct blending is applied. Used for paths only.
132 */
jvanverth502286d2015-04-08 12:37:51 -0700133class GrDistanceFieldPathGeoProc : public GrGeometryProcessor {
jvanverthfa38a302014-10-06 05:59:05 -0700134public:
Jim Van Vertha950b632017-09-12 11:54:11 -0400135
Brian Salomon5c6ac642017-12-19 11:09:32 -0500136 /** The local matrix should be identity if local coords are not required by the GrPipeline. */
Brian Osman09068252018-01-03 09:57:29 -0500137 static sk_sp<GrGeometryProcessor> Make(const SkMatrix& matrix,
Robert Phillips4bc70112018-03-01 10:24:02 -0500138 const sk_sp<GrTextureProxy>* proxies,
Jim Van Verthcbeae032018-05-16 14:54:41 -0400139 int numActiveProxies,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400140 const GrSamplerState& params, uint32_t flags) {
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500141 return sk_sp<GrGeometryProcessor>(
Jim Van Verthcbeae032018-05-16 14:54:41 -0400142 new GrDistanceFieldPathGeoProc(matrix, proxies, numActiveProxies, params, flags));
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500143 }
144
145 ~GrDistanceFieldPathGeoProc() override {}
jvanverthfa38a302014-10-06 05:59:05 -0700146
bsalomon32d1e952016-09-15 07:29:52 -0700147 const char* name() const override { return "DistanceFieldPath"; }
jvanverthfa38a302014-10-06 05:59:05 -0700148
Brian Salomon92be2f72018-06-19 14:33:47 -0400149 const Attribute& inPosition() const { return kInPosition; }
150 const Attribute& inColor() const { return kInColor; }
151 const Attribute& inTextureCoords() const { return kInTextureCoords; }
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400152 const SkMatrix& matrix() const { return fMatrix; }
jvanverthfa38a302014-10-06 05:59:05 -0700153 uint32_t getFlags() const { return fFlags; }
154
Jim Van Verthcbeae032018-05-16 14:54:41 -0400155 void addNewProxies(const sk_sp<GrTextureProxy>*, int numActiveProxies, const GrSamplerState&);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400156
Brian Salomon94efbf52016-11-29 13:43:05 -0500157 void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override;
jvanverthfa38a302014-10-06 05:59:05 -0700158
Brian Salomon94efbf52016-11-29 13:43:05 -0500159 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override;
jvanverthfa38a302014-10-06 05:59:05 -0700160
161private:
Robert Phillips4bc70112018-03-01 10:24:02 -0500162 static constexpr int kMaxTextures = 4;
163
Brian Osman09068252018-01-03 09:57:29 -0500164 GrDistanceFieldPathGeoProc(const SkMatrix& matrix,
Robert Phillips4bc70112018-03-01 10:24:02 -0500165 const sk_sp<GrTextureProxy>* proxies,
Jim Van Verthcbeae032018-05-16 14:54:41 -0400166 int numActiveProxies,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400167 const GrSamplerState&, uint32_t flags);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500168
Brian Salomon92be2f72018-06-19 14:33:47 -0400169 const Attribute& onVertexAttribute(int i) const override;
Brian Salomonf7dcd762018-07-30 14:48:15 -0400170 const TextureSampler& onTextureSampler(int i) const override { return fTextureSamplers[i]; }
Brian Salomon92be2f72018-06-19 14:33:47 -0400171
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400172 SkMatrix fMatrix; // view matrix if perspective, local matrix otherwise
Jim Van Vertha950b632017-09-12 11:54:11 -0400173 TextureSampler fTextureSamplers[kMaxTextures];
joshualitt88c23fc2015-05-13 14:18:07 -0700174 uint32_t fFlags;
Brian Salomon92be2f72018-06-19 14:33:47 -0400175 static constexpr Attribute kInPosition = {"inPosition", kFloat2_GrVertexAttribType};
176 static constexpr Attribute kInColor = {"inColor", kUByte4_norm_GrVertexAttribType};
177 static constexpr Attribute kInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType};
jvanverthfa38a302014-10-06 05:59:05 -0700178
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400179 GR_DECLARE_GEOMETRY_PROCESSOR_TEST
jvanverthfa38a302014-10-06 05:59:05 -0700180
181 typedef GrGeometryProcessor INHERITED;
jvanverth@google.comd830d132013-11-11 20:54:09 +0000182};
183
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000184/**
185 * The output color of this effect is a modulation of the input color and samples from a
186 * distance field texture (using a smoothed step function near 0.5), adjusted for LCD displays.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400187 * It allows explicit specification of the filtering and wrap modes (GrSamplerState). The input
jvanverth2d2a68c2014-06-10 06:42:56 -0700188 * coords are a custom attribute. Gamma correction is handled via a texture LUT.
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000189 */
jvanverth502286d2015-04-08 12:37:51 -0700190class GrDistanceFieldLCDTextGeoProc : public GrGeometryProcessor {
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000191public:
jvanverth21deace2015-04-01 12:43:48 -0700192 struct DistanceAdjust {
193 SkScalar fR, fG, fB;
194 static DistanceAdjust Make(SkScalar r, SkScalar g, SkScalar b) {
195 DistanceAdjust result;
196 result.fR = r; result.fG = g; result.fB = b;
197 return result;
198 }
199 bool operator==(const DistanceAdjust& wa) const {
200 return (fR == wa.fR && fG == wa.fG && fB == wa.fB);
201 }
202 bool operator!=(const DistanceAdjust& wa) const {
203 return !(*this == wa);
204 }
205 };
206
Robert Phillips4bc70112018-03-01 10:24:02 -0500207 static sk_sp<GrGeometryProcessor> Make(const sk_sp<GrTextureProxy>* proxies,
Jim Van Verthcbeae032018-05-16 14:54:41 -0400208 int numActiveProxies,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400209 const GrSamplerState& params,
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400210 DistanceAdjust distanceAdjust,
211 uint32_t flags,
Brian Salomon5c6ac642017-12-19 11:09:32 -0500212 const SkMatrix& localMatrixIfUsesLocalCoords) {
Jim Van Verthcbeae032018-05-16 14:54:41 -0400213 return sk_sp<GrGeometryProcessor>(
214 new GrDistanceFieldLCDTextGeoProc(proxies, numActiveProxies, params, distanceAdjust,
215 flags, localMatrixIfUsesLocalCoords));
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500216 }
217
218 ~GrDistanceFieldLCDTextGeoProc() override {}
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000219
bsalomon32d1e952016-09-15 07:29:52 -0700220 const char* name() const override { return "DistanceFieldLCDText"; }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000221
Brian Salomon92be2f72018-06-19 14:33:47 -0400222 const Attribute& inPosition() const { return fInPosition; }
223 const Attribute& inColor() const { return kInColor; }
224 const Attribute& inTextureCoords() const { return kInTextureCoords; }
jvanverth21deace2015-04-01 12:43:48 -0700225 DistanceAdjust getDistanceAdjust() const { return fDistanceAdjust; }
jvanverth78f07182014-07-30 06:17:59 -0700226 uint32_t getFlags() const { return fFlags; }
Brian Salomon5c6ac642017-12-19 11:09:32 -0500227 const SkMatrix& localMatrix() const { return fLocalMatrix; }
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000228
Jim Van Verthcbeae032018-05-16 14:54:41 -0400229 void addNewProxies(const sk_sp<GrTextureProxy>*, int numActiveProxies, const GrSamplerState&);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400230
Brian Salomon94efbf52016-11-29 13:43:05 -0500231 void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000232
Brian Salomon94efbf52016-11-29 13:43:05 -0500233 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000234
235private:
Jim Van Verthcbeae032018-05-16 14:54:41 -0400236 GrDistanceFieldLCDTextGeoProc(const sk_sp<GrTextureProxy>* proxies, int numActiveProxies,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400237 const GrSamplerState& params, DistanceAdjust wa, uint32_t flags,
Brian Salomon5c6ac642017-12-19 11:09:32 -0500238 const SkMatrix& localMatrix);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500239
Brian Salomon92be2f72018-06-19 14:33:47 -0400240 const Attribute& onVertexAttribute(int) const override;
Brian Salomonf7dcd762018-07-30 14:48:15 -0400241 const TextureSampler& onTextureSampler(int i) const override { return fTextureSamplers[i]; }
Brian Salomon92be2f72018-06-19 14:33:47 -0400242
Robert Phillips4bc70112018-03-01 10:24:02 -0500243 static constexpr int kMaxTextures = 4;
244
Jim Van Vertha950b632017-09-12 11:54:11 -0400245 TextureSampler fTextureSamplers[kMaxTextures];
Mike Klein5045e502018-06-19 01:40:57 +0000246 const SkMatrix fLocalMatrix;
Brian Salomon92be2f72018-06-19 14:33:47 -0400247 DistanceAdjust fDistanceAdjust;
248 Attribute fInPosition;
249 uint32_t fFlags;
250
251 static constexpr Attribute kInColor = {"inColor", kUByte4_norm_GrVertexAttribType};
252 static constexpr Attribute kInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType};
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000253
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400254 GR_DECLARE_GEOMETRY_PROCESSOR_TEST
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000255
jvanverthfa38a302014-10-06 05:59:05 -0700256 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org609ced42014-04-03 18:25:48 +0000257};
258
jvanverth@google.comd830d132013-11-11 20:54:09 +0000259#endif