blob: ae1daef858db3cb02b68de09cff5a81b7d9ca943 [file] [log] [blame]
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +00001/*
2 * Copyright 2014 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
joshualitt30ba4362014-08-21 20:18:45 -07008#include "gl/builders/GrGLProgramBuilder.h"
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +00009#include "GrRRectEffect.h"
10
joshualittb0a8a372014-09-23 09:50:21 -070011#include "gl/GrGLProcessor.h"
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +000012#include "gl/GrGLSL.h"
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +000013#include "GrConvexPolyEffect.h"
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000014#include "GrOvalEffect.h"
joshualittb0a8a372014-09-23 09:50:21 -070015#include "GrTBackendProcessorFactory.h"
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +000016
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000017#include "SkRRect.h"
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +000018
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +000019// The effects defined here only handle rrect radii >= kRadiusMin.
20static const SkScalar kRadiusMin = SK_ScalarHalf;
21
22//////////////////////////////////////////////////////////////////////////////
23
commit-bot@chromium.org4355f212014-03-12 15:32:50 +000024class GLCircularRRectEffect;
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +000025
joshualittb0a8a372014-09-23 09:50:21 -070026class CircularRRectEffect : public GrFragmentProcessor {
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000027public:
skia.committer@gmail.comf1f66c02014-03-05 03:02:06 +000028
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +000029 enum CornerFlags {
30 kTopLeft_CornerFlag = (1 << SkRRect::kUpperLeft_Corner),
31 kTopRight_CornerFlag = (1 << SkRRect::kUpperRight_Corner),
32 kBottomRight_CornerFlag = (1 << SkRRect::kLowerRight_Corner),
33 kBottomLeft_CornerFlag = (1 << SkRRect::kLowerLeft_Corner),
34
35 kLeft_CornerFlags = kTopLeft_CornerFlag | kBottomLeft_CornerFlag,
36 kTop_CornerFlags = kTopLeft_CornerFlag | kTopRight_CornerFlag,
37 kRight_CornerFlags = kTopRight_CornerFlag | kBottomRight_CornerFlag,
38 kBottom_CornerFlags = kBottomLeft_CornerFlag | kBottomRight_CornerFlag,
39
40 kAll_CornerFlags = kTopLeft_CornerFlag | kTopRight_CornerFlag |
41 kBottomLeft_CornerFlag | kBottomRight_CornerFlag,
42
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +000043 kNone_CornerFlags = 0
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +000044 };
45
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +000046 // The flags are used to indicate which corners are circluar (unflagged corners are assumed to
47 // be square).
joshualittb0a8a372014-09-23 09:50:21 -070048 static GrFragmentProcessor* Create(GrPrimitiveEdgeType, uint32_t circularCornerFlags,
49 const SkRRect&);
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000050
commit-bot@chromium.org4355f212014-03-12 15:32:50 +000051 virtual ~CircularRRectEffect() {};
52 static const char* Name() { return "CircularRRect"; }
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000053
54 const SkRRect& getRRect() const { return fRRect; }
55
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +000056 uint32_t getCircularCornerFlags() const { return fCircularCornerFlags; }
skia.committer@gmail.com06acb582014-03-06 03:02:32 +000057
joshualittb0a8a372014-09-23 09:50:21 -070058 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +000059
joshualittb0a8a372014-09-23 09:50:21 -070060 typedef GLCircularRRectEffect GLProcessor;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000061
joshualittb0a8a372014-09-23 09:50:21 -070062 virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERRIDE;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000063
64private:
joshualittb0a8a372014-09-23 09:50:21 -070065 CircularRRectEffect(GrPrimitiveEdgeType, uint32_t circularCornerFlags, const SkRRect&);
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000066
joshualittb0a8a372014-09-23 09:50:21 -070067 virtual bool onIsEqual(const GrProcessor& other) const SK_OVERRIDE;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000068
egdaniel1a8ecdf2014-10-03 06:24:12 -070069 virtual void onComputeInvariantOutput(InvariantOutput* inout) const SK_OVERRIDE;
70
joshualittb0a8a372014-09-23 09:50:21 -070071 SkRRect fRRect;
72 GrPrimitiveEdgeType fEdgeType;
73 uint32_t fCircularCornerFlags;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000074
joshualittb0a8a372014-09-23 09:50:21 -070075 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000076
joshualittb0a8a372014-09-23 09:50:21 -070077 typedef GrFragmentProcessor INHERITED;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000078};
79
joshualittb0a8a372014-09-23 09:50:21 -070080GrFragmentProcessor* CircularRRectEffect::Create(GrPrimitiveEdgeType edgeType,
81 uint32_t circularCornerFlags,
82 const SkRRect& rrect) {
83 if (kFillAA_GrProcessorEdgeType != edgeType && kInverseFillAA_GrProcessorEdgeType != edgeType) {
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000084 return NULL;
85 }
bsalomon55fad7a2014-07-08 07:34:20 -070086 return SkNEW_ARGS(CircularRRectEffect, (edgeType, circularCornerFlags, rrect));
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000087}
88
egdaniel1a8ecdf2014-10-03 06:24:12 -070089void CircularRRectEffect::onComputeInvariantOutput(InvariantOutput* inout) const {
egdanielccb2e382014-10-13 12:53:46 -070090 inout->mulByUnknownAlpha();
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000091}
92
joshualittb0a8a372014-09-23 09:50:21 -070093const GrBackendFragmentProcessorFactory& CircularRRectEffect::getFactory() const {
94 return GrTBackendFragmentProcessorFactory<CircularRRectEffect>::getInstance();
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000095}
96
joshualittb0a8a372014-09-23 09:50:21 -070097CircularRRectEffect::CircularRRectEffect(GrPrimitiveEdgeType edgeType, uint32_t circularCornerFlags,
98 const SkRRect& rrect)
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +000099 : fRRect(rrect)
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000100 , fEdgeType(edgeType)
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000101 , fCircularCornerFlags(circularCornerFlags) {
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000102 this->setWillReadFragmentPosition();
103}
104
joshualittb0a8a372014-09-23 09:50:21 -0700105bool CircularRRectEffect::onIsEqual(const GrProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700106 const CircularRRectEffect& crre = other.cast<CircularRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000107 // The corner flags are derived from fRRect, so no need to check them.
108 return fEdgeType == crre.fEdgeType && fRRect == crre.fRRect;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000109}
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000110
111//////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000112
joshualittb0a8a372014-09-23 09:50:21 -0700113GR_DEFINE_FRAGMENT_PROCESSOR_TEST(CircularRRectEffect);
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000114
joshualittb0a8a372014-09-23 09:50:21 -0700115GrFragmentProcessor* CircularRRectEffect::TestCreate(SkRandom* random,
116 GrContext*,
117 const GrDrawTargetCaps& caps,
118 GrTexture*[]) {
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000119 SkScalar w = random->nextRangeScalar(20.f, 1000.f);
120 SkScalar h = random->nextRangeScalar(20.f, 1000.f);
121 SkScalar r = random->nextRangeF(kRadiusMin, 9.f);
122 SkRRect rrect;
123 rrect.setRectXY(SkRect::MakeWH(w, h), r, r);
joshualittb0a8a372014-09-23 09:50:21 -0700124 GrFragmentProcessor* fp;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000125 do {
joshualittb0a8a372014-09-23 09:50:21 -0700126 GrPrimitiveEdgeType et =
127 (GrPrimitiveEdgeType)random->nextULessThan(kGrProcessorEdgeTypeCnt);
128 fp = GrRRectEffect::Create(et, rrect);
129 } while (NULL == fp);
130 return fp;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000131}
132
133//////////////////////////////////////////////////////////////////////////////
134
joshualittb0a8a372014-09-23 09:50:21 -0700135class GLCircularRRectEffect : public GrGLFragmentProcessor {
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000136public:
joshualittb0a8a372014-09-23 09:50:21 -0700137 GLCircularRRectEffect(const GrBackendProcessorFactory&, const GrProcessor&);
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000138
joshualitt15988992014-10-09 15:04:05 -0700139 virtual void emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -0700140 const GrFragmentProcessor& fp,
141 const GrProcessorKey& key,
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000142 const char* outputColor,
143 const char* inputColor,
144 const TransformedCoordsArray&,
145 const TextureSamplerArray&) SK_OVERRIDE;
146
joshualittb0a8a372014-09-23 09:50:21 -0700147 static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000148
joshualittb0a8a372014-09-23 09:50:21 -0700149 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE;
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000150
151private:
kkinnunen7510b222014-07-30 00:04:16 -0700152 GrGLProgramDataManager::UniformHandle fInnerRectUniform;
153 GrGLProgramDataManager::UniformHandle fRadiusPlusHalfUniform;
154 SkRRect fPrevRRect;
joshualittb0a8a372014-09-23 09:50:21 -0700155 typedef GrGLFragmentProcessor INHERITED;
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000156};
157
joshualittb0a8a372014-09-23 09:50:21 -0700158GLCircularRRectEffect::GLCircularRRectEffect(const GrBackendProcessorFactory& factory,
159 const GrProcessor& )
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000160 : INHERITED (factory) {
161 fPrevRRect.setEmpty();
162}
163
joshualitt15988992014-10-09 15:04:05 -0700164void GLCircularRRectEffect::emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -0700165 const GrFragmentProcessor& fp,
166 const GrProcessorKey& key,
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000167 const char* outputColor,
168 const char* inputColor,
169 const TransformedCoordsArray&,
170 const TextureSamplerArray& samplers) {
joshualittb0a8a372014-09-23 09:50:21 -0700171 const CircularRRectEffect& crre = fp.cast<CircularRRectEffect>();
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000172 const char *rectName;
173 const char *radiusPlusHalfName;
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000174 // The inner rect is the rrect bounds inset by the radius. Its left, top, right, and bottom
175 // edges correspond to components x, y, z, and w, respectively. When a side of the rrect has
176 // only rectangular corners, that side's value corresponds to the rect edge's value outset by
177 // half a pixel.
joshualitt30ba4362014-08-21 20:18:45 -0700178 fInnerRectUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000179 kVec4f_GrSLType,
180 "innerRect",
181 &rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700182 fRadiusPlusHalfUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000183 kFloat_GrSLType,
184 "radiusPlusHalf",
185 &radiusPlusHalfName);
joshualitt30ba4362014-08-21 20:18:45 -0700186
joshualitt15988992014-10-09 15:04:05 -0700187 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700188 const char* fragmentPos = fsBuilder->fragmentPosition();
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000189 // At each quarter-circle corner we compute a vector that is the offset of the fragment position
190 // from the circle center. The vector is pinned in x and y to be in the quarter-plane relevant
191 // to that corner. This means that points near the interior near the rrect top edge will have
192 // a vector that points straight up for both the TL left and TR corners. Computing an
193 // alpha from this vector at either the TR or TL corner will give the correct result. Similarly,
194 // fragments near the other three edges will get the correct AA. Fragments in the interior of
195 // the rrect will have a (0,0) vector at all four corners. So long as the radius > 0.5 they will
196 // correctly produce an alpha value of 1 at all four corners. We take the min of all the alphas.
197 // The code below is a simplified version of the above that performs maxs on the vector
198 // components before computing distances and alpha values so that only one distance computation
199 // need be computed to determine the min alpha.
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000200 //
201 // For the cases where one half of the rrect is rectangular we drop one of the x or y
202 // computations, compute a separate rect edge alpha for the rect side, and mul the two computed
203 // alphas together.
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000204 switch (crre.getCircularCornerFlags()) {
205 case CircularRRectEffect::kAll_CornerFlags:
joshualitt30ba4362014-08-21 20:18:45 -0700206 fsBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos);
207 fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName);
208 fsBuilder->codeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n");
209 fsBuilder->codeAppendf("\t\tfloat alpha = clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000210 radiusPlusHalfName);
211 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000212 case CircularRRectEffect::kTopLeft_CornerFlag:
joshualitt30ba4362014-08-21 20:18:45 -0700213 fsBuilder->codeAppendf("\t\tvec2 dxy = max(%s.xy - %s.xy, 0.0);\n",
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000214 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700215 fsBuilder->codeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000216 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700217 fsBuilder->codeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000218 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700219 fsBuilder->codeAppendf("\t\tfloat alpha = bottomAlpha * rightAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000220 radiusPlusHalfName);
221 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000222 case CircularRRectEffect::kTopRight_CornerFlag:
joshualitt30ba4362014-08-21 20:18:45 -0700223 fsBuilder->codeAppendf("\t\tvec2 dxy = max(vec2(%s.x - %s.z, %s.y - %s.y), 0.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000224 fragmentPos, rectName, rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700225 fsBuilder->codeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000226 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700227 fsBuilder->codeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000228 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700229 fsBuilder->codeAppendf("\t\tfloat alpha = bottomAlpha * leftAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000230 radiusPlusHalfName);
231 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000232 case CircularRRectEffect::kBottomRight_CornerFlag:
joshualitt30ba4362014-08-21 20:18:45 -0700233 fsBuilder->codeAppendf("\t\tvec2 dxy = max(%s.xy - %s.zw, 0.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000234 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700235 fsBuilder->codeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000236 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700237 fsBuilder->codeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000238 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700239 fsBuilder->codeAppendf("\t\tfloat alpha = topAlpha * leftAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000240 radiusPlusHalfName);
241 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000242 case CircularRRectEffect::kBottomLeft_CornerFlag:
joshualitt30ba4362014-08-21 20:18:45 -0700243 fsBuilder->codeAppendf("\t\tvec2 dxy = max(vec2(%s.x - %s.x, %s.y - %s.w), 0.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000244 rectName, fragmentPos, fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700245 fsBuilder->codeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000246 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700247 fsBuilder->codeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000248 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700249 fsBuilder->codeAppendf("\t\tfloat alpha = topAlpha * rightAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000250 radiusPlusHalfName);
251 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000252 case CircularRRectEffect::kLeft_CornerFlags:
joshualitt30ba4362014-08-21 20:18:45 -0700253 fsBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos);
254 fsBuilder->codeAppendf("\t\tfloat dy1 = %s.y - %s.w;\n", fragmentPos, rectName);
255 fsBuilder->codeAppend("\t\tvec2 dxy = max(vec2(dxy0.x, max(dxy0.y, dy1)), 0.0);\n");
256 fsBuilder->codeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000257 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700258 fsBuilder->codeAppendf("\t\tfloat alpha = rightAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000259 radiusPlusHalfName);
260 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000261 case CircularRRectEffect::kTop_CornerFlags:
joshualitt30ba4362014-08-21 20:18:45 -0700262 fsBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos);
263 fsBuilder->codeAppendf("\t\tfloat dx1 = %s.x - %s.z;\n", fragmentPos, rectName);
264 fsBuilder->codeAppend("\t\tvec2 dxy = max(vec2(max(dxy0.x, dx1), dxy0.y), 0.0);\n");
265 fsBuilder->codeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000266 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700267 fsBuilder->codeAppendf("\t\tfloat alpha = bottomAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000268 radiusPlusHalfName);
269 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000270 case CircularRRectEffect::kRight_CornerFlags:
joshualitt30ba4362014-08-21 20:18:45 -0700271 fsBuilder->codeAppendf("\t\tfloat dy0 = %s.y - %s.y;\n", rectName, fragmentPos);
272 fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName);
273 fsBuilder->codeAppend("\t\tvec2 dxy = max(vec2(dxy1.x, max(dy0, dxy1.y)), 0.0);\n");
274 fsBuilder->codeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000275 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700276 fsBuilder->codeAppendf("\t\tfloat alpha = leftAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000277 radiusPlusHalfName);
278 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000279 case CircularRRectEffect::kBottom_CornerFlags:
joshualitt30ba4362014-08-21 20:18:45 -0700280 fsBuilder->codeAppendf("\t\tfloat dx0 = %s.x - %s.x;\n", rectName, fragmentPos);
281 fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName);
282 fsBuilder->codeAppend("\t\tvec2 dxy = max(vec2(max(dx0, dxy1.x), dxy1.y), 0.0);\n");
283 fsBuilder->codeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000284 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700285 fsBuilder->codeAppendf("\t\tfloat alpha = topAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000286 radiusPlusHalfName);
287 break;
288 }
skia.committer@gmail.com06acb582014-03-06 03:02:32 +0000289
joshualittb0a8a372014-09-23 09:50:21 -0700290 if (kInverseFillAA_GrProcessorEdgeType == crre.getEdgeType()) {
joshualitt30ba4362014-08-21 20:18:45 -0700291 fsBuilder->codeAppend("\t\talpha = 1.0 - alpha;\n");
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000292 }
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000293
joshualitt30ba4362014-08-21 20:18:45 -0700294 fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor,
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000295 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str());
296}
297
joshualittb0a8a372014-09-23 09:50:21 -0700298void GLCircularRRectEffect::GenKey(const GrProcessor& processor, const GrGLCaps&,
299 GrProcessorKeyBuilder* b) {
300 const CircularRRectEffect& crre = processor.cast<CircularRRectEffect>();
301 GR_STATIC_ASSERT(kGrProcessorEdgeTypeCnt <= 8);
bsalomon63e99f72014-07-21 08:03:14 -0700302 b->add32((crre.getCircularCornerFlags() << 3) | crre.getEdgeType());
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000303}
304
kkinnunen7510b222014-07-30 00:04:16 -0700305void GLCircularRRectEffect::setData(const GrGLProgramDataManager& pdman,
joshualittb0a8a372014-09-23 09:50:21 -0700306 const GrProcessor& processor) {
307 const CircularRRectEffect& crre = processor.cast<CircularRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000308 const SkRRect& rrect = crre.getRRect();
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000309 if (rrect != fPrevRRect) {
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000310 SkRect rect = rrect.getBounds();
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000311 SkScalar radius = 0;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000312 switch (crre.getCircularCornerFlags()) {
313 case CircularRRectEffect::kAll_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000314 SkASSERT(rrect.isSimpleCircular());
315 radius = rrect.getSimpleRadii().fX;
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000316 SkASSERT(radius >= kRadiusMin);
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000317 rect.inset(radius, radius);
318 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000319 case CircularRRectEffect::kTopLeft_CornerFlag:
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000320 radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX;
321 rect.fLeft += radius;
322 rect.fTop += radius;
323 rect.fRight += 0.5f;
324 rect.fBottom += 0.5f;
325 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000326 case CircularRRectEffect::kTopRight_CornerFlag:
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000327 radius = rrect.radii(SkRRect::kUpperRight_Corner).fX;
bsalomon@google.comde9f2512014-03-11 17:09:17 +0000328 rect.fLeft -= 0.5f;
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000329 rect.fTop += radius;
330 rect.fRight -= radius;
331 rect.fBottom += 0.5f;
332 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000333 case CircularRRectEffect::kBottomRight_CornerFlag:
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000334 radius = rrect.radii(SkRRect::kLowerRight_Corner).fX;
bsalomon@google.comde9f2512014-03-11 17:09:17 +0000335 rect.fLeft -= 0.5f;
336 rect.fTop -= 0.5f;
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000337 rect.fRight -= radius;
338 rect.fBottom -= radius;
339 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000340 case CircularRRectEffect::kBottomLeft_CornerFlag:
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000341 radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX;
342 rect.fLeft += radius;
bsalomon@google.comde9f2512014-03-11 17:09:17 +0000343 rect.fTop -= 0.5f;
344 rect.fRight += 0.5f;
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000345 rect.fBottom -= radius;
346 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000347 case CircularRRectEffect::kLeft_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000348 radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX;
349 rect.fLeft += radius;
350 rect.fTop += radius;
351 rect.fRight += 0.5f;
352 rect.fBottom -= radius;
353 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000354 case CircularRRectEffect::kTop_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000355 radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX;
356 rect.fLeft += radius;
357 rect.fTop += radius;
358 rect.fRight -= radius;
359 rect.fBottom += 0.5f;
360 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000361 case CircularRRectEffect::kRight_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000362 radius = rrect.radii(SkRRect::kUpperRight_Corner).fX;
363 rect.fLeft -= 0.5f;
364 rect.fTop += radius;
365 rect.fRight -= radius;
366 rect.fBottom -= radius;
367 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000368 case CircularRRectEffect::kBottom_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000369 radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX;
370 rect.fLeft += radius;
371 rect.fTop -= 0.5f;
372 rect.fRight -= radius;
373 rect.fBottom -= radius;
374 break;
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000375 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000376 SkFAIL("Should have been one of the above cases.");
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000377 }
kkinnunen7510b222014-07-30 00:04:16 -0700378 pdman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.fBottom);
379 pdman.set1f(fRadiusPlusHalfUniform, radius + 0.5f);
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000380 fPrevRRect = rrect;
381 }
382}
383
384//////////////////////////////////////////////////////////////////////////////
385
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000386class GLEllipticalRRectEffect;
387
joshualittb0a8a372014-09-23 09:50:21 -0700388class EllipticalRRectEffect : public GrFragmentProcessor {
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000389public:
joshualittb0a8a372014-09-23 09:50:21 -0700390 static GrFragmentProcessor* Create(GrPrimitiveEdgeType, const SkRRect&);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000391
392 virtual ~EllipticalRRectEffect() {};
393 static const char* Name() { return "EllipticalRRect"; }
394
395 const SkRRect& getRRect() const { return fRRect; }
396
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000397
joshualittb0a8a372014-09-23 09:50:21 -0700398 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000399
joshualittb0a8a372014-09-23 09:50:21 -0700400 typedef GLEllipticalRRectEffect GLProcessor;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000401
joshualittb0a8a372014-09-23 09:50:21 -0700402 virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERRIDE;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000403
404private:
joshualittb0a8a372014-09-23 09:50:21 -0700405 EllipticalRRectEffect(GrPrimitiveEdgeType, const SkRRect&);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000406
joshualittb0a8a372014-09-23 09:50:21 -0700407 virtual bool onIsEqual(const GrProcessor& other) const SK_OVERRIDE;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000408
egdaniel1a8ecdf2014-10-03 06:24:12 -0700409 virtual void onComputeInvariantOutput(InvariantOutput* inout) const SK_OVERRIDE;
410
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000411 SkRRect fRRect;
joshualittb0a8a372014-09-23 09:50:21 -0700412 GrPrimitiveEdgeType fEdgeType;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000413
joshualittb0a8a372014-09-23 09:50:21 -0700414 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000415
joshualittb0a8a372014-09-23 09:50:21 -0700416 typedef GrFragmentProcessor INHERITED;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000417};
418
joshualittb0a8a372014-09-23 09:50:21 -0700419GrFragmentProcessor*
420EllipticalRRectEffect::Create(GrPrimitiveEdgeType edgeType, const SkRRect& rrect) {
421 if (kFillAA_GrProcessorEdgeType != edgeType && kInverseFillAA_GrProcessorEdgeType != edgeType) {
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000422 return NULL;
423 }
bsalomon55fad7a2014-07-08 07:34:20 -0700424 return SkNEW_ARGS(EllipticalRRectEffect, (edgeType, rrect));
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000425}
426
egdaniel1a8ecdf2014-10-03 06:24:12 -0700427void EllipticalRRectEffect::onComputeInvariantOutput(InvariantOutput* inout) const {
egdanielccb2e382014-10-13 12:53:46 -0700428 inout->mulByUnknownAlpha();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000429}
430
joshualittb0a8a372014-09-23 09:50:21 -0700431const GrBackendFragmentProcessorFactory& EllipticalRRectEffect::getFactory() const {
432 return GrTBackendFragmentProcessorFactory<EllipticalRRectEffect>::getInstance();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000433}
434
joshualittb0a8a372014-09-23 09:50:21 -0700435EllipticalRRectEffect::EllipticalRRectEffect(GrPrimitiveEdgeType edgeType, const SkRRect& rrect)
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000436 : fRRect(rrect)
437 , fEdgeType(edgeType){
438 this->setWillReadFragmentPosition();
439}
440
joshualittb0a8a372014-09-23 09:50:21 -0700441bool EllipticalRRectEffect::onIsEqual(const GrProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700442 const EllipticalRRectEffect& erre = other.cast<EllipticalRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000443 return fEdgeType == erre.fEdgeType && fRRect == erre.fRRect;
444}
445
446//////////////////////////////////////////////////////////////////////////////
447
joshualittb0a8a372014-09-23 09:50:21 -0700448GR_DEFINE_FRAGMENT_PROCESSOR_TEST(EllipticalRRectEffect);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000449
joshualittb0a8a372014-09-23 09:50:21 -0700450GrFragmentProcessor* EllipticalRRectEffect::TestCreate(SkRandom* random,
451 GrContext*,
452 const GrDrawTargetCaps& caps,
453 GrTexture*[]) {
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000454 SkScalar w = random->nextRangeScalar(20.f, 1000.f);
455 SkScalar h = random->nextRangeScalar(20.f, 1000.f);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000456 SkVector r[4];
457 r[SkRRect::kUpperLeft_Corner].fX = random->nextRangeF(kRadiusMin, 9.f);
458 // ensure at least one corner really is elliptical
459 do {
460 r[SkRRect::kUpperLeft_Corner].fY = random->nextRangeF(kRadiusMin, 9.f);
461 } while (r[SkRRect::kUpperLeft_Corner].fY == r[SkRRect::kUpperLeft_Corner].fX);
462
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000463 SkRRect rrect;
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000464 if (random->nextBool()) {
465 // half the time create a four-radii rrect.
466 r[SkRRect::kLowerRight_Corner].fX = random->nextRangeF(kRadiusMin, 9.f);
467 r[SkRRect::kLowerRight_Corner].fY = random->nextRangeF(kRadiusMin, 9.f);
468
469 r[SkRRect::kUpperRight_Corner].fX = r[SkRRect::kLowerRight_Corner].fX;
470 r[SkRRect::kUpperRight_Corner].fY = r[SkRRect::kUpperLeft_Corner].fY;
471
472 r[SkRRect::kLowerLeft_Corner].fX = r[SkRRect::kUpperLeft_Corner].fX;
473 r[SkRRect::kLowerLeft_Corner].fY = r[SkRRect::kLowerRight_Corner].fY;
474
475 rrect.setRectRadii(SkRect::MakeWH(w, h), r);
476 } else {
477 rrect.setRectXY(SkRect::MakeWH(w, h), r[SkRRect::kUpperLeft_Corner].fX,
478 r[SkRRect::kUpperLeft_Corner].fY);
479 }
joshualittb0a8a372014-09-23 09:50:21 -0700480 GrFragmentProcessor* fp;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000481 do {
joshualittb0a8a372014-09-23 09:50:21 -0700482 GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)random->nextULessThan(kGrProcessorEdgeTypeCnt);
483 fp = GrRRectEffect::Create(et, rrect);
484 } while (NULL == fp);
485 return fp;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000486}
487
488//////////////////////////////////////////////////////////////////////////////
489
joshualittb0a8a372014-09-23 09:50:21 -0700490class GLEllipticalRRectEffect : public GrGLFragmentProcessor {
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000491public:
joshualittb0a8a372014-09-23 09:50:21 -0700492 GLEllipticalRRectEffect(const GrBackendProcessorFactory&, const GrProcessor&);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000493
joshualitt15988992014-10-09 15:04:05 -0700494 virtual void emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -0700495 const GrFragmentProcessor& effect,
496 const GrProcessorKey& key,
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000497 const char* outputColor,
498 const char* inputColor,
499 const TransformedCoordsArray&,
500 const TextureSamplerArray&) SK_OVERRIDE;
501
joshualittb0a8a372014-09-23 09:50:21 -0700502 static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000503
joshualittb0a8a372014-09-23 09:50:21 -0700504 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000505
506private:
kkinnunen7510b222014-07-30 00:04:16 -0700507 GrGLProgramDataManager::UniformHandle fInnerRectUniform;
508 GrGLProgramDataManager::UniformHandle fInvRadiiSqdUniform;
509 SkRRect fPrevRRect;
joshualittb0a8a372014-09-23 09:50:21 -0700510 typedef GrGLFragmentProcessor INHERITED;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000511};
512
joshualittb0a8a372014-09-23 09:50:21 -0700513GLEllipticalRRectEffect::GLEllipticalRRectEffect(const GrBackendProcessorFactory& factory,
514 const GrProcessor& effect)
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000515 : INHERITED (factory) {
516 fPrevRRect.setEmpty();
517}
518
joshualitt15988992014-10-09 15:04:05 -0700519void GLEllipticalRRectEffect::emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -0700520 const GrFragmentProcessor& effect,
521 const GrProcessorKey& key,
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000522 const char* outputColor,
523 const char* inputColor,
524 const TransformedCoordsArray&,
525 const TextureSamplerArray& samplers) {
joshualitt49586be2014-09-16 08:21:41 -0700526 const EllipticalRRectEffect& erre = effect.cast<EllipticalRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000527 const char *rectName;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000528 // The inner rect is the rrect bounds inset by the x/y radii
joshualitt30ba4362014-08-21 20:18:45 -0700529 fInnerRectUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000530 kVec4f_GrSLType,
531 "innerRect",
532 &rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700533
joshualitt15988992014-10-09 15:04:05 -0700534 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700535 const char* fragmentPos = fsBuilder->fragmentPosition();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000536 // At each quarter-ellipse corner we compute a vector that is the offset of the fragment pos
537 // to the ellipse center. The vector is pinned in x and y to be in the quarter-plane relevant
538 // to that corner. This means that points near the interior near the rrect top edge will have
539 // a vector that points straight up for both the TL left and TR corners. Computing an
540 // alpha from this vector at either the TR or TL corner will give the correct result. Similarly,
541 // fragments near the other three edges will get the correct AA. Fragments in the interior of
542 // the rrect will have a (0,0) vector at all four corners. So long as the radii > 0.5 they will
543 // correctly produce an alpha value of 1 at all four corners. We take the min of all the alphas.
544 // The code below is a simplified version of the above that performs maxs on the vector
545 // components before computing distances and alpha values so that only one distance computation
546 // need be computed to determine the min alpha.
joshualitt30ba4362014-08-21 20:18:45 -0700547 fsBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos);
548 fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName);
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000549 switch (erre.getRRect().getType()) {
550 case SkRRect::kSimple_Type: {
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000551 const char *invRadiiXYSqdName;
joshualitt30ba4362014-08-21 20:18:45 -0700552 fInvRadiiSqdUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000553 kVec2f_GrSLType,
554 "invRadiiXY",
555 &invRadiiXYSqdName);
joshualitt30ba4362014-08-21 20:18:45 -0700556 fsBuilder->codeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n");
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000557 // Z is the x/y offsets divided by squared radii.
joshualitt30ba4362014-08-21 20:18:45 -0700558 fsBuilder->codeAppendf("\t\tvec2 Z = dxy * %s;\n", invRadiiXYSqdName);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000559 break;
560 }
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000561 case SkRRect::kNinePatch_Type: {
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000562 const char *invRadiiLTRBSqdName;
joshualitt30ba4362014-08-21 20:18:45 -0700563 fInvRadiiSqdUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000564 kVec4f_GrSLType,
565 "invRadiiLTRB",
566 &invRadiiLTRBSqdName);
joshualitt30ba4362014-08-21 20:18:45 -0700567 fsBuilder->codeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n");
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000568 // Z is the x/y offsets divided by squared radii. We only care about the (at most) one
569 // corner where both the x and y offsets are positive, hence the maxes. (The inverse
570 // squared radii will always be positive.)
joshualitt30ba4362014-08-21 20:18:45 -0700571 fsBuilder->codeAppendf("\t\tvec2 Z = max(max(dxy0 * %s.xy, dxy1 * %s.zw), 0.0);\n",
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000572 invRadiiLTRBSqdName, invRadiiLTRBSqdName);
573 break;
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000574 }
575 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000576 SkFAIL("RRect should always be simple or nine-patch.");
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000577 }
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000578 // implicit is the evaluation of (x/a)^2 + (y/b)^2 - 1.
joshualitt30ba4362014-08-21 20:18:45 -0700579 fsBuilder->codeAppend("\t\tfloat implicit = dot(Z, dxy) - 1.0;\n");
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000580 // grad_dot is the squared length of the gradient of the implicit.
joshualitt30ba4362014-08-21 20:18:45 -0700581 fsBuilder->codeAppendf("\t\tfloat grad_dot = 4.0 * dot(Z, Z);\n");
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000582 // avoid calling inversesqrt on zero.
joshualitt30ba4362014-08-21 20:18:45 -0700583 fsBuilder->codeAppend("\t\tgrad_dot = max(grad_dot, 1.0e-4);\n");
584 fsBuilder->codeAppendf("\t\tfloat approx_dist = implicit * inversesqrt(grad_dot);\n");
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000585
joshualittb0a8a372014-09-23 09:50:21 -0700586 if (kFillAA_GrProcessorEdgeType == erre.getEdgeType()) {
joshualitt30ba4362014-08-21 20:18:45 -0700587 fsBuilder->codeAppend("\t\tfloat alpha = clamp(0.5 - approx_dist, 0.0, 1.0);\n");
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000588 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700589 fsBuilder->codeAppend("\t\tfloat alpha = clamp(0.5 + approx_dist, 0.0, 1.0);\n");
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000590 }
591
joshualitt30ba4362014-08-21 20:18:45 -0700592 fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor,
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000593 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str());
594}
595
joshualittb0a8a372014-09-23 09:50:21 -0700596void GLEllipticalRRectEffect::GenKey(const GrProcessor& effect, const GrGLCaps&,
597 GrProcessorKeyBuilder* b) {
joshualitt49586be2014-09-16 08:21:41 -0700598 const EllipticalRRectEffect& erre = effect.cast<EllipticalRRectEffect>();
joshualittb0a8a372014-09-23 09:50:21 -0700599 GR_STATIC_ASSERT(kLast_GrProcessorEdgeType < (1 << 3));
bsalomon63e99f72014-07-21 08:03:14 -0700600 b->add32(erre.getRRect().getType() | erre.getEdgeType() << 3);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000601}
602
kkinnunen7510b222014-07-30 00:04:16 -0700603void GLEllipticalRRectEffect::setData(const GrGLProgramDataManager& pdman,
joshualittb0a8a372014-09-23 09:50:21 -0700604 const GrProcessor& effect) {
joshualitt49586be2014-09-16 08:21:41 -0700605 const EllipticalRRectEffect& erre = effect.cast<EllipticalRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000606 const SkRRect& rrect = erre.getRRect();
607 if (rrect != fPrevRRect) {
608 SkRect rect = rrect.getBounds();
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000609 const SkVector& r0 = rrect.radii(SkRRect::kUpperLeft_Corner);
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000610 SkASSERT(r0.fX >= kRadiusMin);
611 SkASSERT(r0.fY >= kRadiusMin);
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000612 switch (erre.getRRect().getType()) {
613 case SkRRect::kSimple_Type:
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000614 rect.inset(r0.fX, r0.fY);
kkinnunen7510b222014-07-30 00:04:16 -0700615 pdman.set2f(fInvRadiiSqdUniform, 1.f / (r0.fX * r0.fX),
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000616 1.f / (r0.fY * r0.fY));
617 break;
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000618 case SkRRect::kNinePatch_Type: {
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000619 const SkVector& r1 = rrect.radii(SkRRect::kLowerRight_Corner);
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000620 SkASSERT(r1.fX >= kRadiusMin);
621 SkASSERT(r1.fY >= kRadiusMin);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000622 rect.fLeft += r0.fX;
623 rect.fTop += r0.fY;
624 rect.fRight -= r1.fX;
625 rect.fBottom -= r1.fY;
kkinnunen7510b222014-07-30 00:04:16 -0700626 pdman.set4f(fInvRadiiSqdUniform, 1.f / (r0.fX * r0.fX),
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000627 1.f / (r0.fY * r0.fY),
628 1.f / (r1.fX * r1.fX),
629 1.f / (r1.fY * r1.fY));
630 break;
631 }
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000632 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000633 SkFAIL("RRect should always be simple or nine-patch.");
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000634 }
kkinnunen7510b222014-07-30 00:04:16 -0700635 pdman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.fBottom);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000636 fPrevRRect = rrect;
637 }
638}
639
640//////////////////////////////////////////////////////////////////////////////
641
joshualittb0a8a372014-09-23 09:50:21 -0700642GrFragmentProcessor* GrRRectEffect::Create(GrPrimitiveEdgeType edgeType, const SkRRect& rrect) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000643 if (rrect.isRect()) {
644 return GrConvexPolyEffect::Create(edgeType, rrect.getBounds());
645 }
646
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000647 if (rrect.isOval()) {
648 return GrOvalEffect::Create(edgeType, rrect.getBounds());
649 }
650
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000651 if (rrect.isSimple()) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000652 if (rrect.getSimpleRadii().fX < kRadiusMin || rrect.getSimpleRadii().fY < kRadiusMin) {
653 // In this case the corners are extremely close to rectangular and we collapse the
654 // clip to a rectangular clip.
655 return GrConvexPolyEffect::Create(edgeType, rrect.getBounds());
656 }
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000657 if (rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY) {
skia.committer@gmail.com6e4eb212014-03-25 03:02:32 +0000658 return CircularRRectEffect::Create(edgeType, CircularRRectEffect::kAll_CornerFlags,
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000659 rrect);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000660 } else {
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000661 return EllipticalRRectEffect::Create(edgeType, rrect);
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000662 }
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000663 }
664
665 if (rrect.isComplex() || rrect.isNinePatch()) {
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000666 // Check for the "tab" cases - two adjacent circular corners and two square corners.
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000667 SkScalar circularRadius = 0;
668 uint32_t cornerFlags = 0;
669
670 SkVector radii[4];
671 bool squashedRadii = false;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000672 for (int c = 0; c < 4; ++c) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000673 radii[c] = rrect.radii((SkRRect::Corner)c);
674 SkASSERT((0 == radii[c].fX) == (0 == radii[c].fY));
675 if (0 == radii[c].fX) {
676 // The corner is square, so no need to squash or flag as circular.
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000677 continue;
678 }
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000679 if (radii[c].fX < kRadiusMin || radii[c].fY < kRadiusMin) {
680 radii[c].set(0, 0);
681 squashedRadii = true;
682 continue;
683 }
684 if (radii[c].fX != radii[c].fY) {
bsalomon@google.com44a435b2014-03-13 19:20:32 +0000685 cornerFlags = ~0U;
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000686 break;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000687 }
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000688 if (!cornerFlags) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000689 circularRadius = radii[c].fX;
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000690 cornerFlags = 1 << c;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000691 } else {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000692 if (radii[c].fX != circularRadius) {
bsalomon@google.com44a435b2014-03-13 19:20:32 +0000693 cornerFlags = ~0U;
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000694 break;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000695 }
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000696 cornerFlags |= 1 << c;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000697 }
698 }
699
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000700 switch (cornerFlags) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000701 case CircularRRectEffect::kAll_CornerFlags:
702 // This rrect should have been caught in the simple case above. Though, it would
703 // be correctly handled in the fallthrough code.
704 SkASSERT(false);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000705 case CircularRRectEffect::kTopLeft_CornerFlag:
706 case CircularRRectEffect::kTopRight_CornerFlag:
707 case CircularRRectEffect::kBottomRight_CornerFlag:
708 case CircularRRectEffect::kBottomLeft_CornerFlag:
709 case CircularRRectEffect::kLeft_CornerFlags:
710 case CircularRRectEffect::kTop_CornerFlags:
711 case CircularRRectEffect::kRight_CornerFlags:
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000712 case CircularRRectEffect::kBottom_CornerFlags: {
713 SkTCopyOnFirstWrite<SkRRect> rr(rrect);
714 if (squashedRadii) {
715 rr.writable()->setRectRadii(rrect.getBounds(), radii);
716 }
717 return CircularRRectEffect::Create(edgeType, cornerFlags, *rr);
718 }
719 case CircularRRectEffect::kNone_CornerFlags:
720 return GrConvexPolyEffect::Create(edgeType, rrect.getBounds());
721 default: {
722 if (squashedRadii) {
723 // If we got here then we squashed some but not all the radii to zero. (If all
724 // had been squashed cornerFlags would be 0.) The elliptical effect doesn't
725 // support some rounded and some square corners.
726 return NULL;
727 }
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000728 if (rrect.isNinePatch()) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000729 return EllipticalRRectEffect::Create(edgeType, rrect);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000730 }
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000731 return NULL;
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000732 }
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000733 }
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000734 }
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000735
736 return NULL;
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000737}