OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 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 "GrOvalEffect.h" | 8 #include "GrOvalEffect.h" |
9 | 9 |
10 #include "GrFragmentProcessor.h" | 10 #include "GrFragmentProcessor.h" |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 } | 121 } |
122 | 122 |
123 void GLCircleEffect::emitCode(GrGLFPBuilder* builder, | 123 void GLCircleEffect::emitCode(GrGLFPBuilder* builder, |
124 const GrFragmentProcessor& fp, | 124 const GrFragmentProcessor& fp, |
125 const char* outputColor, | 125 const char* outputColor, |
126 const char* inputColor, | 126 const char* inputColor, |
127 const TransformedCoordsArray&, | 127 const TransformedCoordsArray&, |
128 const TextureSamplerArray& samplers) { | 128 const TextureSamplerArray& samplers) { |
129 const CircleEffect& ce = fp.cast<CircleEffect>(); | 129 const CircleEffect& ce = fp.cast<CircleEffect>(); |
130 const char *circleName; | 130 const char *circleName; |
131 // The circle uniform is (center.x, center.y, radius + 0.5) for regular fill
s and | 131 // The circle uniform is (center.x, center.y, radius + 0.5, 1 / (radius + 0.
5)) for regular |
132 // (... ,radius - 0.5) for inverse fills. | 132 // fills and (..., radius - 0.5, 1 / (radius - 0.5)) for inverse fills. |
133 fCircleUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibilit
y, | 133 fCircleUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibilit
y, |
134 kVec3f_GrSLType, kDefault_GrSLPrecision
, | 134 kVec4f_GrSLType, kDefault_GrSLPrecision
, |
135 "circle", | 135 "circle", |
136 &circleName); | 136 &circleName); |
137 | 137 |
138 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); | 138 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
139 const char* fragmentPos = fsBuilder->fragmentPosition(); | 139 const char* fragmentPos = fsBuilder->fragmentPosition(); |
140 | 140 |
141 SkASSERT(kHairlineAA_GrProcessorEdgeType != ce.getEdgeType()); | 141 SkASSERT(kHairlineAA_GrProcessorEdgeType != ce.getEdgeType()); |
| 142 // TODO: Right now the distance to circle caclulation is performed in a spac
e normalized to the |
| 143 // radius and then denormalized. This is to prevent overflow on devices that
have a "real" |
| 144 // mediump. It'd be nice to only to this on mediump devices but we currently
don't have the |
| 145 // caps here. |
142 if (GrProcessorEdgeTypeIsInverseFill(ce.getEdgeType())) { | 146 if (GrProcessorEdgeTypeIsInverseFill(ce.getEdgeType())) { |
143 fsBuilder->codeAppendf("\t\tfloat d = length(%s.xy - %s.xy) - %s.z;\n", | 147 fsBuilder->codeAppendf("\t\tfloat d = (length((%s.xy - %s.xy) * %s.w) -
1.0) * %s.z;\n", |
144 circleName, fragmentPos, circleName); | 148 circleName, fragmentPos, circleName, circleName)
; |
145 } else { | 149 } else { |
146 fsBuilder->codeAppendf("\t\tfloat d = %s.z - length(%s.xy - %s.xy);\n", | 150 fsBuilder->codeAppendf("\t\tfloat d = (1.0 - length((%s.xy - %s.xy) * %
s.w)) * %s.z;\n", |
147 circleName, fragmentPos, circleName); | 151 circleName, fragmentPos, circleName, circleName); |
148 } | 152 } |
149 if (GrProcessorEdgeTypeIsAA(ce.getEdgeType())) { | 153 if (GrProcessorEdgeTypeIsAA(ce.getEdgeType())) { |
150 fsBuilder->codeAppend("\t\td = clamp(d, 0.0, 1.0);\n"); | 154 fsBuilder->codeAppend("\t\td = clamp(d, 0.0, 1.0);\n"); |
151 } else { | 155 } else { |
152 fsBuilder->codeAppend("\t\td = d > 0.5 ? 1.0 : 0.0;\n"); | 156 fsBuilder->codeAppend("\t\td = d > 0.5 ? 1.0 : 0.0;\n"); |
153 } | 157 } |
154 | 158 |
155 fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor, | 159 fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor, |
156 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("d")).c_str())
; | 160 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("d")).c_str())
; |
157 } | 161 } |
158 | 162 |
159 void GLCircleEffect::GenKey(const GrProcessor& processor, const GrGLCaps&, | 163 void GLCircleEffect::GenKey(const GrProcessor& processor, const GrGLCaps&, |
160 GrProcessorKeyBuilder* b) { | 164 GrProcessorKeyBuilder* b) { |
161 const CircleEffect& ce = processor.cast<CircleEffect>(); | 165 const CircleEffect& ce = processor.cast<CircleEffect>(); |
162 b->add32(ce.getEdgeType()); | 166 b->add32(ce.getEdgeType()); |
163 } | 167 } |
164 | 168 |
165 void GLCircleEffect::setData(const GrGLProgramDataManager& pdman, const GrProces
sor& processor) { | 169 void GLCircleEffect::setData(const GrGLProgramDataManager& pdman, const GrProces
sor& processor) { |
166 const CircleEffect& ce = processor.cast<CircleEffect>(); | 170 const CircleEffect& ce = processor.cast<CircleEffect>(); |
167 if (ce.getRadius() != fPrevRadius || ce.getCenter() != fPrevCenter) { | 171 if (ce.getRadius() != fPrevRadius || ce.getCenter() != fPrevCenter) { |
168 SkScalar radius = ce.getRadius(); | 172 SkScalar radius = ce.getRadius(); |
169 if (GrProcessorEdgeTypeIsInverseFill(ce.getEdgeType())) { | 173 if (GrProcessorEdgeTypeIsInverseFill(ce.getEdgeType())) { |
170 radius -= 0.5f; | 174 radius -= 0.5f; |
171 } else { | 175 } else { |
172 radius += 0.5f; | 176 radius += 0.5f; |
173 } | 177 } |
174 pdman.set3f(fCircleUniform, ce.getCenter().fX, ce.getCenter().fY, radius
); | 178 pdman.set4f(fCircleUniform, ce.getCenter().fX, ce.getCenter().fY, radius
, |
| 179 SkScalarInvert(radius)); |
175 fPrevCenter = ce.getCenter(); | 180 fPrevCenter = ce.getCenter(); |
176 fPrevRadius = ce.getRadius(); | 181 fPrevRadius = ce.getRadius(); |
177 } | 182 } |
178 } | 183 } |
179 | 184 |
180 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 185 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
181 | 186 |
182 void CircleEffect::getGLProcessorKey(const GrGLCaps& caps, | 187 void CircleEffect::getGLProcessorKey(const GrGLCaps& caps, |
183 GrProcessorKeyBuilder* b) const { | 188 GrProcessorKeyBuilder* b) const { |
184 GLCircleEffect::GenKey(*this, caps, b); | 189 GLCircleEffect::GenKey(*this, caps, b); |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 w /= 2; | 392 w /= 2; |
388 return CircleEffect::Create(edgeType, SkPoint::Make(oval.fLeft + w, oval
.fTop + w), w); | 393 return CircleEffect::Create(edgeType, SkPoint::Make(oval.fLeft + w, oval
.fTop + w), w); |
389 } else { | 394 } else { |
390 w /= 2; | 395 w /= 2; |
391 h /= 2; | 396 h /= 2; |
392 return EllipseEffect::Create(edgeType, SkPoint::Make(oval.fLeft + w, ova
l.fTop + h), w, h); | 397 return EllipseEffect::Create(edgeType, SkPoint::Make(oval.fLeft + w, ova
l.fTop + h), w, h); |
393 } | 398 } |
394 | 399 |
395 return NULL; | 400 return NULL; |
396 } | 401 } |
OLD | NEW |