Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1049)

Side by Side Diff: src/gpu/GrOvalRenderer.cpp

Issue 543623004: Removing vertex attrib indices (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: cleanup Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "gl/builders/GrGLProgramBuilder.h" 8 #include "gl/builders/GrGLProgramBuilder.h"
9 #include "GrOvalRenderer.h" 9 #include "GrOvalRenderer.h"
10 10
11 #include "GrEffect.h" 11 #include "GrEffect.h"
12 #include "gl/GrGLEffect.h" 12 #include "gl/GrGLEffect.h"
13 #include "gl/GrGLSL.h" 13 #include "gl/GrGLSL.h"
14 #include "gl/GrGLVertexEffect.h" 14 #include "gl/GrGLGeometryProcessor.h"
15 #include "GrTBackendEffectFactory.h" 15 #include "GrTBackendEffectFactory.h"
16 16
17 #include "GrDrawState.h" 17 #include "GrDrawState.h"
18 #include "GrDrawTarget.h" 18 #include "GrDrawTarget.h"
19 #include "GrGpu.h" 19 #include "GrGpu.h"
20 20
21 #include "SkRRect.h" 21 #include "SkRRect.h"
22 #include "SkStrokeRec.h" 22 #include "SkStrokeRec.h"
23 #include "SkTLazy.h" 23 #include "SkTLazy.h"
24 24
25 #include "effects/GrVertexEffect.h" 25 #include "effects/GrGeometryProcessor.h"
26 #include "effects/GrRRectEffect.h" 26 #include "effects/GrRRectEffect.h"
27 27
28 namespace { 28 namespace {
29 29
30 struct CircleVertex { 30 struct CircleVertex {
31 SkPoint fPos; 31 SkPoint fPos;
32 SkPoint fOffset; 32 SkPoint fOffset;
33 SkScalar fOuterRadius; 33 SkScalar fOuterRadius;
34 SkScalar fInnerRadius; 34 SkScalar fInnerRadius;
35 }; 35 };
(...skipping 17 matching lines...) Expand all
53 53
54 } 54 }
55 55
56 /////////////////////////////////////////////////////////////////////////////// 56 ///////////////////////////////////////////////////////////////////////////////
57 57
58 /** 58 /**
59 * The output of this effect is a modulation of the input color and coverage for a circle, 59 * The output of this effect is a modulation of the input color and coverage for a circle,
60 * specified as offset_x, offset_y (both from center point), outer radius and in ner radius. 60 * specified as offset_x, offset_y (both from center point), outer radius and in ner radius.
61 */ 61 */
62 62
63 class CircleEdgeEffect : public GrVertexEffect { 63 const GrShaderVar kAttrCircleEdge("aCircleEdge",
64 kVec4f_GrSLType,
65 GrShaderVar::kAttribute_TypeModifier);
66
67
68 class CircleEdgeEffect : public GrGeometryProcessor {
64 public: 69 public:
65 static GrEffect* Create(bool stroke) { 70 static GrEffect* Create(bool stroke) {
66 GR_CREATE_STATIC_EFFECT(gCircleStrokeEdge, CircleEdgeEffect, (true)); 71 GR_CREATE_STATIC_EFFECT(gCircleStrokeEdge, CircleEdgeEffect, (true));
67 GR_CREATE_STATIC_EFFECT(gCircleFillEdge, CircleEdgeEffect, (false)); 72 GR_CREATE_STATIC_EFFECT(gCircleFillEdge, CircleEdgeEffect, (false));
68 73
69 if (stroke) { 74 if (stroke) {
70 gCircleStrokeEdge->ref(); 75 gCircleStrokeEdge->ref();
71 return gCircleStrokeEdge; 76 return gCircleStrokeEdge;
72 } else { 77 } else {
73 gCircleFillEdge->ref(); 78 gCircleFillEdge->ref();
74 return gCircleFillEdge; 79 return gCircleFillEdge;
75 } 80 }
76 } 81 }
77 82
78 virtual void getConstantColorComponents(GrColor* color, 83 virtual void getConstantColorComponents(GrColor* color,
79 uint32_t* validFlags) const SK_OVERR IDE { 84 uint32_t* validFlags) const SK_OVERR IDE {
80 *validFlags = 0; 85 *validFlags = 0;
81 } 86 }
82 87
83 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { 88 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
84 return GrTBackendEffectFactory<CircleEdgeEffect>::getInstance(); 89 return GrTBackendEffectFactory<CircleEdgeEffect>::getInstance();
85 } 90 }
86 91
87 virtual ~CircleEdgeEffect() {} 92 virtual ~CircleEdgeEffect() {}
88 93
89 static const char* Name() { return "CircleEdge"; } 94 static const char* Name() { return "CircleEdge"; }
90 95
91 inline bool isStroked() const { return fStroke; } 96 inline bool isStroked() const { return fStroke; }
92 97
93 class GLEffect : public GrGLVertexEffect { 98 class GLEffect : public GrGLGeometryProcessor {
94 public: 99 public:
95 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) 100 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
96 : INHERITED (factory) {} 101 : INHERITED (factory) {}
97 102
98 virtual void emitCode(GrGLFullProgramBuilder* builder, 103 virtual void emitCode(GrGLFullProgramBuilder* builder,
99 const GrDrawEffect& drawEffect, 104 const GrDrawEffect& drawEffect,
100 const GrEffectKey& key, 105 const GrEffectKey& key,
101 const char* outputColor, 106 const char* outputColor,
102 const char* inputColor, 107 const char* inputColor,
103 const TransformedCoordsArray&, 108 const TransformedCoordsArray&,
104 const TextureSamplerArray& samplers) SK_OVERRIDE { 109 const TextureSamplerArray& samplers) SK_OVERRIDE {
105 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleE dgeEffect>(); 110 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleE dgeEffect>();
106 const char *vsName, *fsName; 111 const char *vsName, *fsName;
107 builder->addVarying(kVec4f_GrSLType, "CircleEdge", &vsName, &fsName) ; 112 builder->addVarying(kVec4f_GrSLType, "CircleEdge", &vsName, &fsName) ;
108 113
109 GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder (); 114 GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder ();;
110 const SkString* attr0Name = 115 vsBuilder->codeAppendf("\t%s = %s;\n", vsName, kAttrCircleEdge.c_str ());
111 vsBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndi ces()[0]);
112 vsBuilder->codeAppendf("\t%s = %s;\n", vsName, attr0Name->c_str());
113 116
114 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBui lder(); 117 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBui lder();
115 fsBuilder->codeAppendf("\tfloat d = length(%s.xy);\n", fsName); 118 fsBuilder->codeAppendf("\tfloat d = length(%s.xy);\n", fsName);
116 fsBuilder->codeAppendf("\tfloat edgeAlpha = clamp(%s.z - d, 0.0, 1.0 );\n", fsName); 119 fsBuilder->codeAppendf("\tfloat edgeAlpha = clamp(%s.z - d, 0.0, 1.0 );\n", fsName);
117 if (circleEffect.isStroked()) { 120 if (circleEffect.isStroked()) {
118 fsBuilder->codeAppendf("\tfloat innerAlpha = clamp(d - %s.w, 0.0 , 1.0);\n", fsName); 121 fsBuilder->codeAppendf("\tfloat innerAlpha = clamp(d - %s.w, 0.0 , 1.0);\n", fsName);
119 fsBuilder->codeAppend("\tedgeAlpha *= innerAlpha;\n"); 122 fsBuilder->codeAppend("\tedgeAlpha *= innerAlpha;\n");
120 } 123 }
121 124
122 fsBuilder->codeAppendf("\t%s = %s;\n", outputColor, 125 fsBuilder->codeAppendf("\t%s = %s;\n", outputColor,
123 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeA lpha")).c_str()); 126 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeA lpha")).c_str());
124 } 127 }
125 128
126 static void GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&, 129 static void GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&,
127 GrEffectKeyBuilder* b) { 130 GrEffectKeyBuilder* b) {
128 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleE dgeEffect>(); 131 const CircleEdgeEffect& circleEffect = drawEffect.castEffect<CircleE dgeEffect>();
129 b->add32(circleEffect.isStroked()); 132 b->add32(circleEffect.isStroked());
130 } 133 }
131 134
132 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_OVERRIDE {} 135 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_OVERRIDE {}
133 136
134 private: 137 private:
135 typedef GrGLVertexEffect INHERITED; 138 typedef GrGLGeometryProcessor INHERITED;
136 }; 139 };
137 140
138 141
139 private: 142 private:
140 CircleEdgeEffect(bool stroke) : GrVertexEffect() { 143 CircleEdgeEffect(bool stroke) : GrGeometryProcessor() {
141 this->addVertexAttrib(kVec4f_GrSLType); 144 this->addVertexAttrib(kAttrCircleEdge);
142 fStroke = stroke; 145 fStroke = stroke;
143 } 146 }
144 147
145 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { 148 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
146 const CircleEdgeEffect& cee = CastEffect<CircleEdgeEffect>(other); 149 const CircleEdgeEffect& cee = CastEffect<CircleEdgeEffect>(other);
147 return cee.fStroke == fStroke; 150 return cee.fStroke == fStroke;
148 } 151 }
149 152
150 bool fStroke; 153 bool fStroke;
151 154
152 GR_DECLARE_EFFECT_TEST; 155 GR_DECLARE_EFFECT_TEST;
153 156
154 typedef GrVertexEffect INHERITED; 157 typedef GrGeometryProcessor INHERITED;
155 }; 158 };
156 159
157 GR_DEFINE_EFFECT_TEST(CircleEdgeEffect); 160 GR_DEFINE_EFFECT_TEST(CircleEdgeEffect);
158 161
159 GrEffect* CircleEdgeEffect::TestCreate(SkRandom* random, 162 GrEffect* CircleEdgeEffect::TestCreate(SkRandom* random,
160 GrContext* context, 163 GrContext* context,
161 const GrDrawTargetCaps&, 164 const GrDrawTargetCaps&,
162 GrTexture* textures[]) { 165 GrTexture* textures[]) {
163 return CircleEdgeEffect::Create(random->nextBool()); 166 return CircleEdgeEffect::Create(random->nextBool());
164 } 167 }
165 168
166 /////////////////////////////////////////////////////////////////////////////// 169 ///////////////////////////////////////////////////////////////////////////////
167 170
168 /** 171 /**
169 * The output of this effect is a modulation of the input color and coverage for an axis-aligned 172 * The output of this effect is a modulation of the input color and coverage for an axis-aligned
170 * ellipse, specified as a 2D offset from center, and the reciprocals of the out er and inner radii, 173 * ellipse, specified as a 2D offset from center, and the reciprocals of the out er and inner radii,
171 * in both x and y directions. 174 * in both x and y directions.
172 * 175 *
173 * We are using an implicit function of x^2/a^2 + y^2/b^2 - 1 = 0. 176 * We are using an implicit function of x^2/a^2 + y^2/b^2 - 1 = 0.
174 */ 177 */
175 178
176 class EllipseEdgeEffect : public GrVertexEffect { 179
180 const GrShaderVar kAttrEllipseOffset("aEllipseOffset",
181 kVec2f_GrSLType,
182 GrShaderVar::kAttribute_TypeModifier);
183
184
185 const GrShaderVar kAttrEllipseRadii("aEllipseRadii",
186 kVec4f_GrSLType,
187 GrShaderVar::kAttribute_TypeModifier);
188
189 class EllipseEdgeEffect : public GrGeometryProcessor {
177 public: 190 public:
178 static GrEffect* Create(bool stroke) { 191 static GrEffect* Create(bool stroke) {
179 GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, EllipseEdgeEffect, (true)); 192 GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, EllipseEdgeEffect, (true));
180 GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, EllipseEdgeEffect, (false)); 193 GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, EllipseEdgeEffect, (false));
181 194
182 if (stroke) { 195 if (stroke) {
183 gEllipseStrokeEdge->ref(); 196 gEllipseStrokeEdge->ref();
184 return gEllipseStrokeEdge; 197 return gEllipseStrokeEdge;
185 } else { 198 } else {
186 gEllipseFillEdge->ref(); 199 gEllipseFillEdge->ref();
187 return gEllipseFillEdge; 200 return gEllipseFillEdge;
188 } 201 }
189 } 202 }
190 203
191 virtual void getConstantColorComponents(GrColor* color, 204 virtual void getConstantColorComponents(GrColor* color,
192 uint32_t* validFlags) const SK_OVERR IDE { 205 uint32_t* validFlags) const SK_OVERR IDE {
193 *validFlags = 0; 206 *validFlags = 0;
194 } 207 }
195 208
196 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { 209 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
197 return GrTBackendEffectFactory<EllipseEdgeEffect>::getInstance(); 210 return GrTBackendEffectFactory<EllipseEdgeEffect>::getInstance();
198 } 211 }
199 212
200 virtual ~EllipseEdgeEffect() {} 213 virtual ~EllipseEdgeEffect() {}
201 214
202 static const char* Name() { return "EllipseEdge"; } 215 static const char* Name() { return "EllipseEdge"; }
203 216
204 inline bool isStroked() const { return fStroke; } 217 inline bool isStroked() const { return fStroke; }
205 218
206 class GLEffect : public GrGLVertexEffect { 219 class GLEffect : public GrGLGeometryProcessor {
207 public: 220 public:
208 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) 221 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
209 : INHERITED (factory) {} 222 : INHERITED (factory) {}
210 223
211 virtual void emitCode(GrGLFullProgramBuilder* builder, 224 virtual void emitCode(GrGLFullProgramBuilder* builder,
212 const GrDrawEffect& drawEffect, 225 const GrDrawEffect& drawEffect,
213 const GrEffectKey& key, 226 const GrEffectKey& key,
214 const char* outputColor, 227 const char* outputColor,
215 const char* inputColor, 228 const char* inputColor,
216 const TransformedCoordsArray&, 229 const TransformedCoordsArray&,
217 const TextureSamplerArray& samplers) SK_OVERRIDE { 230 const TextureSamplerArray& samplers) SK_OVERRIDE {
218 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<Ellip seEdgeEffect>(); 231 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<Ellip seEdgeEffect>();
219 232
220 const char *vsOffsetName, *fsOffsetName; 233 const char *vsOffsetName, *fsOffsetName;
221 const char *vsRadiiName, *fsRadiiName; 234 const char *vsRadiiName, *fsRadiiName;
222 235
223 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets", &vsOffsetName , &fsOffsetName); 236 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets", &vsOffsetName , &fsOffsetName);
224 237
225 GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder (); 238 GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder ();
226 const SkString* attr0Name = 239 vsBuilder->codeAppendf("\t%s = %s;\n", vsOffsetName, kAttrEllipseOff set.c_str());
227 vsBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndi ces()[0]);
228 vsBuilder->codeAppendf("\t%s = %s;\n", vsOffsetName, attr0Name->c_st r());
229 240
230 builder->addVarying(kVec4f_GrSLType, "EllipseRadii", &vsRadiiName, & fsRadiiName); 241 builder->addVarying(kVec4f_GrSLType, "EllipseRadii", &vsRadiiName, & fsRadiiName);
231 const SkString* attr1Name = 242 vsBuilder->codeAppendf("\t%s = %s;\n", vsRadiiName, kAttrEllipseRadi i.c_str());
232 vsBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndi ces()[1]);
233 vsBuilder->codeAppendf("\t%s = %s;\n", vsRadiiName, attr1Name->c_str ());
234 243
235 // for outer curve 244 // for outer curve
236 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBui lder(); 245 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBui lder();
237 fsBuilder->codeAppendf("\tvec2 scaledOffset = %s*%s.xy;\n", fsOffset Name, fsRadiiName); 246 fsBuilder->codeAppendf("\tvec2 scaledOffset = %s*%s.xy;\n", fsOffset Name, fsRadiiName);
238 fsBuilder->codeAppend("\tfloat test = dot(scaledOffset, scaledOffset ) - 1.0;\n"); 247 fsBuilder->codeAppend("\tfloat test = dot(scaledOffset, scaledOffset ) - 1.0;\n");
239 fsBuilder->codeAppendf("\tvec2 grad = 2.0*scaledOffset*%s.xy;\n", fs RadiiName); 248 fsBuilder->codeAppendf("\tvec2 grad = 2.0*scaledOffset*%s.xy;\n", fs RadiiName);
240 fsBuilder->codeAppend("\tfloat grad_dot = dot(grad, grad);\n"); 249 fsBuilder->codeAppend("\tfloat grad_dot = dot(grad, grad);\n");
241 // avoid calling inversesqrt on zero. 250 // avoid calling inversesqrt on zero.
242 fsBuilder->codeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n"); 251 fsBuilder->codeAppend("\tgrad_dot = max(grad_dot, 1.0e-4);\n");
243 fsBuilder->codeAppend("\tfloat invlen = inversesqrt(grad_dot);\n"); 252 fsBuilder->codeAppend("\tfloat invlen = inversesqrt(grad_dot);\n");
(...skipping 15 matching lines...) Expand all
259 static void GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&, 268 static void GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&,
260 GrEffectKeyBuilder* b) { 269 GrEffectKeyBuilder* b) {
261 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<Ellip seEdgeEffect>(); 270 const EllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<Ellip seEdgeEffect>();
262 b->add32(ellipseEffect.isStroked()); 271 b->add32(ellipseEffect.isStroked());
263 } 272 }
264 273
265 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_OVERRIDE { 274 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_OVERRIDE {
266 } 275 }
267 276
268 private: 277 private:
269 typedef GrGLVertexEffect INHERITED; 278 typedef GrGLGeometryProcessor INHERITED;
270 }; 279 };
271 280
272 private: 281 private:
273 EllipseEdgeEffect(bool stroke) : GrVertexEffect() { 282 EllipseEdgeEffect(bool stroke) : GrGeometryProcessor() {
274 this->addVertexAttrib(kVec2f_GrSLType); 283 this->addVertexAttrib(kAttrEllipseOffset);
275 this->addVertexAttrib(kVec4f_GrSLType); 284 this->addVertexAttrib(kAttrEllipseRadii);
276 fStroke = stroke; 285 fStroke = stroke;
277 } 286 }
278 287
279 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { 288 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
280 const EllipseEdgeEffect& eee = CastEffect<EllipseEdgeEffect>(other); 289 const EllipseEdgeEffect& eee = CastEffect<EllipseEdgeEffect>(other);
281 return eee.fStroke == fStroke; 290 return eee.fStroke == fStroke;
282 } 291 }
283 292
284 bool fStroke; 293 bool fStroke;
285 294
286 GR_DECLARE_EFFECT_TEST; 295 GR_DECLARE_EFFECT_TEST;
287 296
288 typedef GrVertexEffect INHERITED; 297 typedef GrGeometryProcessor INHERITED;
289 }; 298 };
290 299
291 GR_DEFINE_EFFECT_TEST(EllipseEdgeEffect); 300 GR_DEFINE_EFFECT_TEST(EllipseEdgeEffect);
292 301
293 GrEffect* EllipseEdgeEffect::TestCreate(SkRandom* random, 302 GrEffect* EllipseEdgeEffect::TestCreate(SkRandom* random,
294 GrContext* context, 303 GrContext* context,
295 const GrDrawTargetCaps&, 304 const GrDrawTargetCaps&,
296 GrTexture* textures[]) { 305 GrTexture* textures[]) {
297 return EllipseEdgeEffect::Create(random->nextBool()); 306 return EllipseEdgeEffect::Create(random->nextBool());
298 } 307 }
299 308
300 /////////////////////////////////////////////////////////////////////////////// 309 ///////////////////////////////////////////////////////////////////////////////
301 310
302 /** 311 /**
303 * The output of this effect is a modulation of the input color and coverage for an ellipse, 312 * The output of this effect is a modulation of the input color and coverage for an ellipse,
304 * specified as a 2D offset from center for both the outer and inner paths (if s troked). The 313 * specified as a 2D offset from center for both the outer and inner paths (if s troked). The
305 * implict equation used is for a unit circle (x^2 + y^2 - 1 = 0) and the edge c orrected by 314 * implict equation used is for a unit circle (x^2 + y^2 - 1 = 0) and the edge c orrected by
306 * using differentials. 315 * using differentials.
307 * 316 *
308 * The result is device-independent and can be used with any affine matrix. 317 * The result is device-independent and can be used with any affine matrix.
309 */ 318 */
310 319
311 class DIEllipseEdgeEffect : public GrVertexEffect { 320 const GrShaderVar kAttrEllipseOffsets0("aEllipseOffsets0",
321 kVec2f_GrSLType,
322 GrShaderVar::kAttribute_TypeModifier);
323
324 const GrShaderVar kAttrEllipseOffsets1("aEllipseOffsets1",
325 kVec2f_GrSLType,
326 GrShaderVar::kAttribute_TypeModifier);
327
328
329 class DIEllipseEdgeEffect : public GrGeometryProcessor {
312 public: 330 public:
313 enum Mode { kStroke = 0, kHairline, kFill }; 331 enum Mode { kStroke = 0, kHairline, kFill };
314 332
315 static GrEffect* Create(Mode mode) { 333 static GrEffect* Create(Mode mode) {
316 GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, DIEllipseEdgeEffect, (kStrok e)); 334 GR_CREATE_STATIC_EFFECT(gEllipseStrokeEdge, DIEllipseEdgeEffect, (kStrok e));
317 GR_CREATE_STATIC_EFFECT(gEllipseHairlineEdge, DIEllipseEdgeEffect, (kHai rline)); 335 GR_CREATE_STATIC_EFFECT(gEllipseHairlineEdge, DIEllipseEdgeEffect, (kHai rline));
318 GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, DIEllipseEdgeEffect, (kFill)); 336 GR_CREATE_STATIC_EFFECT(gEllipseFillEdge, DIEllipseEdgeEffect, (kFill));
319 337
320 if (kStroke == mode) { 338 if (kStroke == mode) {
321 gEllipseStrokeEdge->ref(); 339 gEllipseStrokeEdge->ref();
(...skipping 15 matching lines...) Expand all
337 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { 355 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
338 return GrTBackendEffectFactory<DIEllipseEdgeEffect>::getInstance(); 356 return GrTBackendEffectFactory<DIEllipseEdgeEffect>::getInstance();
339 } 357 }
340 358
341 virtual ~DIEllipseEdgeEffect() {} 359 virtual ~DIEllipseEdgeEffect() {}
342 360
343 static const char* Name() { return "DIEllipseEdge"; } 361 static const char* Name() { return "DIEllipseEdge"; }
344 362
345 inline Mode getMode() const { return fMode; } 363 inline Mode getMode() const { return fMode; }
346 364
347 class GLEffect : public GrGLVertexEffect { 365 class GLEffect : public GrGLGeometryProcessor {
348 public: 366 public:
349 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) 367 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
350 : INHERITED (factory) {} 368 : INHERITED (factory) {}
351 369
352 virtual void emitCode(GrGLFullProgramBuilder* builder, 370 virtual void emitCode(GrGLFullProgramBuilder* builder,
353 const GrDrawEffect& drawEffect, 371 const GrDrawEffect& drawEffect,
354 const GrEffectKey& key, 372 const GrEffectKey& key,
355 const char* outputColor, 373 const char* outputColor,
356 const char* inputColor, 374 const char* inputColor,
357 const TransformedCoordsArray&, 375 const TransformedCoordsArray&,
358 const TextureSamplerArray& samplers) SK_OVERRIDE { 376 const TextureSamplerArray& samplers) SK_OVERRIDE {
359 const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<DIE llipseEdgeEffect>(); 377 const DIEllipseEdgeEffect& ellipseEffect = drawEffect.castEffect<DIE llipseEdgeEffect>();
360 378
361 const char *vsOffsetName0, *fsOffsetName0; 379 const char *vsOffsetName0, *fsOffsetName0;
362 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets0", 380 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets0",
363 &vsOffsetName0, &fsOffsetName0); 381 &vsOffsetName0, &fsOffsetName0);
364 382
365 GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder (); 383 GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder ();
366 const SkString* attr0Name = 384 vsBuilder->codeAppendf("\t%s = %s;\n", vsOffsetName0, kAttrEllipseOf fsets0.c_str());
367 vsBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndi ces()[0]);
368 vsBuilder->codeAppendf("\t%s = %s;\n", vsOffsetName0, attr0Name->c_s tr());
369 const char *vsOffsetName1, *fsOffsetName1; 385 const char *vsOffsetName1, *fsOffsetName1;
370 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets1", 386 builder->addVarying(kVec2f_GrSLType, "EllipseOffsets1",
371 &vsOffsetName1, &fsOffsetName1); 387 &vsOffsetName1, &fsOffsetName1);
372 const SkString* attr1Name = 388 vsBuilder->codeAppendf("\t%s = %s;\n", vsOffsetName1, kAttrEllipseOf fsets1.c_str());
373 vsBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndi ces()[1]);
374 vsBuilder->codeAppendf("\t%s = %s;\n", vsOffsetName1, attr1Name->c_s tr());
375 389
376 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBui lder(); 390 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBui lder();
377 SkAssertResult(fsBuilder->enableFeature( 391 SkAssertResult(fsBuilder->enableFeature(
378 GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature) ); 392 GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature) );
379 // for outer curve 393 // for outer curve
380 fsBuilder->codeAppendf("\tvec2 scaledOffset = %s.xy;\n", fsOffsetNam e0); 394 fsBuilder->codeAppendf("\tvec2 scaledOffset = %s.xy;\n", fsOffsetNam e0);
381 fsBuilder->codeAppend("\tfloat test = dot(scaledOffset, scaledOffset ) - 1.0;\n"); 395 fsBuilder->codeAppend("\tfloat test = dot(scaledOffset, scaledOffset ) - 1.0;\n");
382 fsBuilder->codeAppendf("\tvec2 duvdx = dFdx(%s);\n", fsOffsetName0); 396 fsBuilder->codeAppendf("\tvec2 duvdx = dFdx(%s);\n", fsOffsetName0);
383 fsBuilder->codeAppendf("\tvec2 duvdy = dFdy(%s);\n", fsOffsetName0); 397 fsBuilder->codeAppendf("\tvec2 duvdy = dFdy(%s);\n", fsOffsetName0);
384 fsBuilder->codeAppendf("\tvec2 grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s .y*duvdx.y,\n" 398 fsBuilder->codeAppendf("\tvec2 grad = vec2(2.0*%s.x*duvdx.x + 2.0*%s .y*duvdx.y,\n"
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
419 const DIEllipseEdgeEffect& ellipseEffect = 433 const DIEllipseEdgeEffect& ellipseEffect =
420 drawEffect.castEffect<DIEllipseEdgeEffect>(); 434 drawEffect.castEffect<DIEllipseEdgeEffect>();
421 435
422 b->add32(ellipseEffect.getMode()); 436 b->add32(ellipseEffect.getMode());
423 } 437 }
424 438
425 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_OVERRIDE { 439 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_OVERRIDE {
426 } 440 }
427 441
428 private: 442 private:
429 typedef GrGLVertexEffect INHERITED; 443 typedef GrGLGeometryProcessor INHERITED;
430 }; 444 };
431 445
432 private: 446 private:
433 DIEllipseEdgeEffect(Mode mode) : GrVertexEffect() { 447 DIEllipseEdgeEffect(Mode mode) : GrGeometryProcessor() {
434 this->addVertexAttrib(kVec2f_GrSLType); 448 this->addVertexAttrib(kAttrEllipseOffsets0);
435 this->addVertexAttrib(kVec2f_GrSLType); 449 this->addVertexAttrib(kAttrEllipseOffsets1);
436 fMode = mode; 450 fMode = mode;
437 } 451 }
438 452
439 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { 453 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
440 const DIEllipseEdgeEffect& eee = CastEffect<DIEllipseEdgeEffect>(other); 454 const DIEllipseEdgeEffect& eee = CastEffect<DIEllipseEdgeEffect>(other);
441 return eee.fMode == fMode; 455 return eee.fMode == fMode;
442 } 456 }
443 457
444 Mode fMode; 458 Mode fMode;
445 459
446 GR_DECLARE_EFFECT_TEST; 460 GR_DECLARE_EFFECT_TEST;
447 461
448 typedef GrVertexEffect INHERITED; 462 typedef GrGeometryProcessor INHERITED;
449 }; 463 };
450 464
451 GR_DEFINE_EFFECT_TEST(DIEllipseEdgeEffect); 465 GR_DEFINE_EFFECT_TEST(DIEllipseEdgeEffect);
452 466
453 GrEffect* DIEllipseEdgeEffect::TestCreate(SkRandom* random, 467 GrEffect* DIEllipseEdgeEffect::TestCreate(SkRandom* random,
454 GrContext* context, 468 GrContext* context,
455 const GrDrawTargetCaps&, 469 const GrDrawTargetCaps&,
456 GrTexture* textures[]) { 470 GrTexture* textures[]) {
457 return DIEllipseEdgeEffect::Create((Mode)(random->nextRangeU(0,2))); 471 return DIEllipseEdgeEffect::Create((Mode)(random->nextRangeU(0,2)));
458 } 472 }
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
545 halfWidth = SkScalarHalf(strokeWidth); 559 halfWidth = SkScalarHalf(strokeWidth);
546 } 560 }
547 561
548 outerRadius += halfWidth; 562 outerRadius += halfWidth;
549 if (isStrokeOnly) { 563 if (isStrokeOnly) {
550 innerRadius = radius - halfWidth; 564 innerRadius = radius - halfWidth;
551 } 565 }
552 } 566 }
553 567
554 GrEffect* effect = CircleEdgeEffect::Create(isStrokeOnly && innerRadius > 0) ; 568 GrEffect* effect = CircleEdgeEffect::Create(isStrokeOnly && innerRadius > 0) ;
555 static const int kCircleEdgeAttrIndex = 1; 569 drawState->setGeometryProcessor(effect)->unref();
556 drawState->setGeometryProcessor(effect, kCircleEdgeAttrIndex)->unref();
557 570
558 // The radii are outset for two reasons. First, it allows the shader to simp ly perform 571 // The radii are outset for two reasons. First, it allows the shader to simp ly perform
559 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is use d to compute the 572 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is use d to compute the
560 // verts of the bounding box that is rendered and the outset ensures the box will cover all 573 // verts of the bounding box that is rendered and the outset ensures the box will cover all
561 // pixels partially covered by the circle. 574 // pixels partially covered by the circle.
562 outerRadius += SK_ScalarHalf; 575 outerRadius += SK_ScalarHalf;
563 innerRadius -= SK_ScalarHalf; 576 innerRadius -= SK_ScalarHalf;
564 577
565 SkRect bounds = SkRect::MakeLTRB( 578 SkRect bounds = SkRect::MakeLTRB(
566 center.fX - outerRadius, 579 center.fX - outerRadius,
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
687 if (!geo.succeeded()) { 700 if (!geo.succeeded()) {
688 GrPrintf("Failed to get space for vertices!\n"); 701 GrPrintf("Failed to get space for vertices!\n");
689 return false; 702 return false;
690 } 703 }
691 704
692 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices()); 705 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
693 706
694 GrEffect* effect = EllipseEdgeEffect::Create(isStrokeOnly && 707 GrEffect* effect = EllipseEdgeEffect::Create(isStrokeOnly &&
695 innerXRadius > 0 && innerYRadiu s > 0); 708 innerXRadius > 0 && innerYRadiu s > 0);
696 709
697 static const int kEllipseCenterAttrIndex = 1; 710 drawState->setGeometryProcessor(effect)->unref();
698 static const int kEllipseEdgeAttrIndex = 2;
699 drawState->setGeometryProcessor(effect, kEllipseCenterAttrIndex, kEllipseEdg eAttrIndex)->unref();
700 711
701 // Compute the reciprocals of the radii here to save time in the shader 712 // Compute the reciprocals of the radii here to save time in the shader
702 SkScalar xRadRecip = SkScalarInvert(xRadius); 713 SkScalar xRadRecip = SkScalarInvert(xRadius);
703 SkScalar yRadRecip = SkScalarInvert(yRadius); 714 SkScalar yRadRecip = SkScalarInvert(yRadius);
704 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius); 715 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
705 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius); 716 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
706 717
707 // We've extended the outer x radius out half a pixel to antialias. 718 // We've extended the outer x radius out half a pixel to antialias.
708 // This will also expand the rect so all the pixels will be captured. 719 // This will also expand the rect so all the pixels will be captured.
709 // TODO: Consider if we should use sqrt(2)/2 instead 720 // TODO: Consider if we should use sqrt(2)/2 instead
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
805 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0); 816 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
806 if (!geo.succeeded()) { 817 if (!geo.succeeded()) {
807 GrPrintf("Failed to get space for vertices!\n"); 818 GrPrintf("Failed to get space for vertices!\n");
808 return false; 819 return false;
809 } 820 }
810 821
811 DIEllipseVertex* verts = reinterpret_cast<DIEllipseVertex*>(geo.vertices()); 822 DIEllipseVertex* verts = reinterpret_cast<DIEllipseVertex*>(geo.vertices());
812 823
813 GrEffect* effect = DIEllipseEdgeEffect::Create(mode); 824 GrEffect* effect = DIEllipseEdgeEffect::Create(mode);
814 825
815 static const int kEllipseOuterOffsetAttrIndex = 1; 826 drawState->setGeometryProcessor(effect)->unref();
816 static const int kEllipseInnerOffsetAttrIndex = 2;
817 drawState->setGeometryProcessor(effect, kEllipseOuterOffsetAttrIndex,
818 kEllipseInnerOffsetAttrIndex)->unref();
819 827
820 // This expands the outer rect so that after CTM we end up with a half-pixel border 828 // This expands the outer rect so that after CTM we end up with a half-pixel border
821 SkScalar a = vm[SkMatrix::kMScaleX]; 829 SkScalar a = vm[SkMatrix::kMScaleX];
822 SkScalar b = vm[SkMatrix::kMSkewX]; 830 SkScalar b = vm[SkMatrix::kMSkewX];
823 SkScalar c = vm[SkMatrix::kMSkewY]; 831 SkScalar c = vm[SkMatrix::kMSkewY];
824 SkScalar d = vm[SkMatrix::kMScaleY]; 832 SkScalar d = vm[SkMatrix::kMScaleY];
825 SkScalar geoDx = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(a*a + c*c)); 833 SkScalar geoDx = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(a*a + c*c));
826 SkScalar geoDy = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(b*b + d*d)); 834 SkScalar geoDy = SkScalarDiv(SK_ScalarHalf, SkScalarSqrt(b*b + d*d));
827 // This adjusts the "radius" to include the half-pixel border 835 // This adjusts the "radius" to include the half-pixel border
828 SkScalar offsetDx = SkScalarDiv(geoDx, xRadius); 836 SkScalar offsetDx = SkScalarDiv(geoDx, xRadius);
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
1055 if (isStrokeOnly) { 1063 if (isStrokeOnly) {
1056 innerRadius = xRadius - halfWidth; 1064 innerRadius = xRadius - halfWidth;
1057 } 1065 }
1058 outerRadius += halfWidth; 1066 outerRadius += halfWidth;
1059 bounds.outset(halfWidth, halfWidth); 1067 bounds.outset(halfWidth, halfWidth);
1060 } 1068 }
1061 1069
1062 isStrokeOnly = (isStrokeOnly && innerRadius >= 0); 1070 isStrokeOnly = (isStrokeOnly && innerRadius >= 0);
1063 1071
1064 GrEffect* effect = CircleEdgeEffect::Create(isStrokeOnly); 1072 GrEffect* effect = CircleEdgeEffect::Create(isStrokeOnly);
1065 static const int kCircleEdgeAttrIndex = 1; 1073 drawState->setGeometryProcessor(effect)->unref();
1066 drawState->setGeometryProcessor(effect, kCircleEdgeAttrIndex)->unref();
1067 1074
1068 // The radii are outset for two reasons. First, it allows the shader to simply perform 1075 // The radii are outset for two reasons. First, it allows the shader to simply perform
1069 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the 1076 // clamp(distance-to-center - radius, 0, 1). Second, the outer radius is used to compute the
1070 // verts of the bounding box that is rendered and the outset ensures the box will cover all 1077 // verts of the bounding box that is rendered and the outset ensures the box will cover all
1071 // pixels partially covered by the circle. 1078 // pixels partially covered by the circle.
1072 outerRadius += SK_ScalarHalf; 1079 outerRadius += SK_ScalarHalf;
1073 innerRadius -= SK_ScalarHalf; 1080 innerRadius -= SK_ScalarHalf;
1074 1081
1075 // Expand the rect so all the pixels will be captured. 1082 // Expand the rect so all the pixels will be captured.
1076 bounds.outset(SK_ScalarHalf, SK_ScalarHalf); 1083 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
1159 isStrokeOnly = (isStrokeOnly && innerXRadius >= 0 && innerYRadius >= 0); 1166 isStrokeOnly = (isStrokeOnly && innerXRadius >= 0 && innerYRadius >= 0);
1160 1167
1161 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0); 1168 GrDrawTarget::AutoReleaseGeometry geo(target, 16, 0);
1162 if (!geo.succeeded()) { 1169 if (!geo.succeeded()) {
1163 GrPrintf("Failed to get space for vertices!\n"); 1170 GrPrintf("Failed to get space for vertices!\n");
1164 return false; 1171 return false;
1165 } 1172 }
1166 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices()); 1173 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
1167 1174
1168 GrEffect* effect = EllipseEdgeEffect::Create(isStrokeOnly); 1175 GrEffect* effect = EllipseEdgeEffect::Create(isStrokeOnly);
1169 static const int kEllipseOffsetAttrIndex = 1; 1176 drawState->setGeometryProcessor(effect)->unref();
1170 static const int kEllipseRadiiAttrIndex = 2;
1171 drawState->setGeometryProcessor(effect,
1172 kEllipseOffsetAttrIndex,
1173 kEllipseRadiiAttrIndex)->unref();
1174 1177
1175 // Compute the reciprocals of the radii here to save time in the shader 1178 // Compute the reciprocals of the radii here to save time in the shader
1176 SkScalar xRadRecip = SkScalarInvert(xRadius); 1179 SkScalar xRadRecip = SkScalarInvert(xRadius);
1177 SkScalar yRadRecip = SkScalarInvert(yRadius); 1180 SkScalar yRadRecip = SkScalarInvert(yRadius);
1178 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius); 1181 SkScalar xInnerRadRecip = SkScalarInvert(innerXRadius);
1179 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius); 1182 SkScalar yInnerRadRecip = SkScalarInvert(innerYRadius);
1180 1183
1181 // Extend the radii out half a pixel to antialias. 1184 // Extend the radii out half a pixel to antialias.
1182 SkScalar xOuterRadius = xRadius + SK_ScalarHalf; 1185 SkScalar xOuterRadius = xRadius + SK_ScalarHalf;
1183 SkScalar yOuterRadius = yRadius + SK_ScalarHalf; 1186 SkScalar yOuterRadius = yRadius + SK_ScalarHalf;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
1226 1229
1227 // drop out the middle quad if we're stroked 1230 // drop out the middle quad if we're stroked
1228 int indexCnt = isStrokeOnly ? SK_ARRAY_COUNT(gRRectIndices) - 6 : 1231 int indexCnt = isStrokeOnly ? SK_ARRAY_COUNT(gRRectIndices) - 6 :
1229 SK_ARRAY_COUNT(gRRectIndices); 1232 SK_ARRAY_COUNT(gRRectIndices);
1230 target->setIndexSourceToBuffer(indexBuffer); 1233 target->setIndexSourceToBuffer(indexBuffer);
1231 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bou nds); 1234 target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 16, indexCnt, &bou nds);
1232 } 1235 }
1233 1236
1234 return true; 1237 return true;
1235 } 1238 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698