OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2012 Google Inc. | 3 * Copyright 2012 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 #include "SkSweepGradient.h" | 9 #include "SkSweepGradient.h" |
10 | 10 |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 *dstC++ = cache[toggle + index]; | 177 *dstC++ = cache[toggle + index]; |
178 toggle = next_dither_toggle16(toggle); | 178 toggle = next_dither_toggle16(toggle); |
179 } | 179 } |
180 } | 180 } |
181 } | 181 } |
182 | 182 |
183 ///////////////////////////////////////////////////////////////////// | 183 ///////////////////////////////////////////////////////////////////// |
184 | 184 |
185 #if SK_SUPPORT_GPU | 185 #if SK_SUPPORT_GPU |
186 | 186 |
187 #include "GrTBackendEffectFactory.h" | 187 #include "GrTBackendProcessorFactory.h" |
188 #include "gl/builders/GrGLProgramBuilder.h" | 188 #include "gl/builders/GrGLProgramBuilder.h" |
189 #include "SkGr.h" | 189 #include "SkGr.h" |
190 | 190 |
191 class GrGLSweepGradient : public GrGLGradientEffect { | 191 class GrGLSweepGradient : public GrGLGradientEffect { |
192 public: | 192 public: |
193 | 193 |
194 GrGLSweepGradient(const GrBackendEffectFactory& factory, | 194 GrGLSweepGradient(const GrBackendProcessorFactory& factory, |
195 const GrEffect&) : INHERITED (factory) { } | 195 const GrProcessor&) : INHERITED (factory) { } |
196 virtual ~GrGLSweepGradient() { } | 196 virtual ~GrGLSweepGradient() { } |
197 | 197 |
198 virtual void emitCode(GrGLProgramBuilder*, | 198 virtual void emitCode(GrGLProgramBuilder*, |
199 const GrEffect&, | 199 const GrFragmentProcessor&, |
200 const GrEffectKey&, | 200 const GrProcessorKey&, |
201 const char* outputColor, | 201 const char* outputColor, |
202 const char* inputColor, | 202 const char* inputColor, |
203 const TransformedCoordsArray&, | 203 const TransformedCoordsArray&, |
204 const TextureSamplerArray&) SK_OVERRIDE; | 204 const TextureSamplerArray&) SK_OVERRIDE; |
205 | 205 |
206 static void GenKey(const GrEffect& effect, const GrGLCaps&, GrEffectKeyBuild
er* b) { | 206 static void GenKey(const GrProcessor& processor, const GrGLCaps&, GrProcesso
rKeyBuilder* b) { |
207 b->add32(GenBaseGradientKey(effect)); | 207 b->add32(GenBaseGradientKey(processor)); |
208 } | 208 } |
209 | 209 |
210 private: | 210 private: |
211 | 211 |
212 typedef GrGLGradientEffect INHERITED; | 212 typedef GrGLGradientEffect INHERITED; |
213 | 213 |
214 }; | 214 }; |
215 | 215 |
216 ///////////////////////////////////////////////////////////////////// | 216 ///////////////////////////////////////////////////////////////////// |
217 | 217 |
218 class GrSweepGradient : public GrGradientEffect { | 218 class GrSweepGradient : public GrGradientEffect { |
219 public: | 219 public: |
220 static GrEffect* Create(GrContext* ctx, const SkSweepGradient& shader, const
SkMatrix& m) { | 220 static GrFragmentProcessor* Create(GrContext* ctx, const SkSweepGradient& sh
ader, |
| 221 const SkMatrix& m) { |
221 return SkNEW_ARGS(GrSweepGradient, (ctx, shader, m)); | 222 return SkNEW_ARGS(GrSweepGradient, (ctx, shader, m)); |
222 } | 223 } |
223 virtual ~GrSweepGradient() { } | 224 virtual ~GrSweepGradient() { } |
224 | 225 |
225 static const char* Name() { return "Sweep Gradient"; } | 226 static const char* Name() { return "Sweep Gradient"; } |
226 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { | 227 virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERR
IDE { |
227 return GrTBackendEffectFactory<GrSweepGradient>::getInstance(); | 228 return GrTBackendFragmentProcessorFactory<GrSweepGradient>::getInstance(
); |
228 } | 229 } |
229 | 230 |
230 typedef GrGLSweepGradient GLEffect; | 231 typedef GrGLSweepGradient GLProcessor; |
231 | 232 |
232 private: | 233 private: |
233 GrSweepGradient(GrContext* ctx, | 234 GrSweepGradient(GrContext* ctx, |
234 const SkSweepGradient& shader, | 235 const SkSweepGradient& shader, |
235 const SkMatrix& matrix) | 236 const SkMatrix& matrix) |
236 : INHERITED(ctx, shader, matrix, SkShader::kClamp_TileMode) { } | 237 : INHERITED(ctx, shader, matrix, SkShader::kClamp_TileMode) { } |
237 GR_DECLARE_EFFECT_TEST; | 238 GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
238 | 239 |
239 typedef GrGradientEffect INHERITED; | 240 typedef GrGradientEffect INHERITED; |
240 }; | 241 }; |
241 | 242 |
242 ///////////////////////////////////////////////////////////////////// | 243 ///////////////////////////////////////////////////////////////////// |
243 | 244 |
244 GR_DEFINE_EFFECT_TEST(GrSweepGradient); | 245 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrSweepGradient); |
245 | 246 |
246 GrEffect* GrSweepGradient::TestCreate(SkRandom* random, | 247 GrFragmentProcessor* GrSweepGradient::TestCreate(SkRandom* random, |
247 GrContext* context, | 248 GrContext* context, |
248 const GrDrawTargetCaps&, | 249 const GrDrawTargetCaps&, |
249 GrTexture**) { | 250 GrTexture**) { |
250 SkPoint center = {random->nextUScalar1(), random->nextUScalar1()}; | 251 SkPoint center = {random->nextUScalar1(), random->nextUScalar1()}; |
251 | 252 |
252 SkColor colors[kMaxRandomGradientColors]; | 253 SkColor colors[kMaxRandomGradientColors]; |
253 SkScalar stopsArray[kMaxRandomGradientColors]; | 254 SkScalar stopsArray[kMaxRandomGradientColors]; |
254 SkScalar* stops = stopsArray; | 255 SkScalar* stops = stopsArray; |
255 SkShader::TileMode tmIgnored; | 256 SkShader::TileMode tmIgnored; |
256 int colorCount = RandomGradientParams(random, colors, &stops, &tmIgnored); | 257 int colorCount = RandomGradientParams(random, colors, &stops, &tmIgnored); |
257 SkAutoTUnref<SkShader> shader(SkGradientShader::CreateSweep(center.fX, cente
r.fY, | 258 SkAutoTUnref<SkShader> shader(SkGradientShader::CreateSweep(center.fX, cente
r.fY, |
258 colors, stops, c
olorCount)); | 259 colors, stops, c
olorCount)); |
259 SkPaint paint; | 260 SkPaint paint; |
260 GrEffect* effect; | 261 GrFragmentProcessor* fp; |
261 GrColor paintColor; | 262 GrColor paintColor; |
262 SkAssertResult(shader->asNewEffect(context, paint, NULL, &paintColor, &effec
t)); | 263 SkAssertResult(shader->asFragmentProcessor(context, paint, NULL, &paintColor
, &fp)); |
263 return effect; | 264 return fp; |
264 } | 265 } |
265 | 266 |
266 ///////////////////////////////////////////////////////////////////// | 267 ///////////////////////////////////////////////////////////////////// |
267 | 268 |
268 void GrGLSweepGradient::emitCode(GrGLProgramBuilder* builder, | 269 void GrGLSweepGradient::emitCode(GrGLProgramBuilder* builder, |
269 const GrEffect&, | 270 const GrFragmentProcessor&, |
270 const GrEffectKey& key, | 271 const GrProcessorKey& key, |
271 const char* outputColor, | 272 const char* outputColor, |
272 const char* inputColor, | 273 const char* inputColor, |
273 const TransformedCoordsArray& coords, | 274 const TransformedCoordsArray& coords, |
274 const TextureSamplerArray& samplers) { | 275 const TextureSamplerArray& samplers) { |
275 uint32_t baseKey = key.get32(0); | 276 uint32_t baseKey = key.get32(0); |
276 this->emitUniforms(builder, baseKey); | 277 this->emitUniforms(builder, baseKey); |
277 SkString coords2D = builder->getFragmentShaderBuilder()->ensureFSCoords2D(co
ords, 0); | 278 SkString coords2D = builder->getFragmentShaderBuilder()->ensureFSCoords2D(co
ords, 0); |
278 const GrGLContextInfo ctxInfo = builder->ctxInfo(); | 279 const GrGLContextInfo ctxInfo = builder->ctxInfo(); |
279 SkString t; | 280 SkString t; |
280 // 0.1591549430918 is 1/(2*pi), used since atan returns values [-pi, pi] | 281 // 0.1591549430918 is 1/(2*pi), used since atan returns values [-pi, pi] |
281 // On Intel GPU there is an issue where it reads the second arguement to ata
n "- %s.x" as an int | 282 // On Intel GPU there is an issue where it reads the second arguement to ata
n "- %s.x" as an int |
282 // thus must us -1.0 * %s.x to work correctly | 283 // thus must us -1.0 * %s.x to work correctly |
283 if (kIntel_GrGLVendor != ctxInfo.vendor()){ | 284 if (kIntel_GrGLVendor != ctxInfo.vendor()){ |
284 t.printf("atan(- %s.y, - %s.x) * 0.1591549430918 + 0.5", | 285 t.printf("atan(- %s.y, - %s.x) * 0.1591549430918 + 0.5", |
285 coords2D.c_str(), coords2D.c_str()); | 286 coords2D.c_str(), coords2D.c_str()); |
286 } else { | 287 } else { |
287 t.printf("atan(- %s.y, -1.0 * %s.x) * 0.1591549430918 + 0.5", | 288 t.printf("atan(- %s.y, -1.0 * %s.x) * 0.1591549430918 + 0.5", |
288 coords2D.c_str(), coords2D.c_str()); | 289 coords2D.c_str(), coords2D.c_str()); |
289 } | 290 } |
290 this->emitColor(builder, t.c_str(), baseKey, outputColor, inputColor, sample
rs); | 291 this->emitColor(builder, t.c_str(), baseKey, outputColor, inputColor, sample
rs); |
291 } | 292 } |
292 | 293 |
293 ///////////////////////////////////////////////////////////////////// | 294 ///////////////////////////////////////////////////////////////////// |
294 | 295 |
295 bool SkSweepGradient::asNewEffect(GrContext* context, const SkPaint& paint, | 296 bool SkSweepGradient::asFragmentProcessor(GrContext* context, const SkPaint& pai
nt, |
296 const SkMatrix* localMatrix, GrColor* paintCol
or, | 297 const SkMatrix* localMatrix, GrColor*
paintColor, |
297 GrEffect** effect) const { | 298 GrFragmentProcessor** effect) const { |
298 | 299 |
299 SkMatrix matrix; | 300 SkMatrix matrix; |
300 if (!this->getLocalMatrix().invert(&matrix)) { | 301 if (!this->getLocalMatrix().invert(&matrix)) { |
301 return false; | 302 return false; |
302 } | 303 } |
303 if (localMatrix) { | 304 if (localMatrix) { |
304 SkMatrix inv; | 305 SkMatrix inv; |
305 if (!localMatrix->invert(&inv)) { | 306 if (!localMatrix->invert(&inv)) { |
306 return false; | 307 return false; |
307 } | 308 } |
308 matrix.postConcat(inv); | 309 matrix.postConcat(inv); |
309 } | 310 } |
310 matrix.postConcat(fPtsToUnit); | 311 matrix.postConcat(fPtsToUnit); |
311 | 312 |
312 *effect = GrSweepGradient::Create(context, *this, matrix); | 313 *effect = GrSweepGradient::Create(context, *this, matrix); |
313 *paintColor = SkColor2GrColorJustAlpha(paint.getColor()); | 314 *paintColor = SkColor2GrColorJustAlpha(paint.getColor()); |
314 | 315 |
315 return true; | 316 return true; |
316 } | 317 } |
317 | 318 |
318 #else | 319 #else |
319 | 320 |
320 bool SkSweepGradient::asNewEffect(GrContext* context, const SkPaint& paint, | 321 bool SkSweepGradient::asFragmentProcessor(GrContext*, const SkPaint&, const SkMa
trix*, GrColor*, |
321 const SkMatrix* localMatrix, GrColor* paintCol
or, | 322 GrFragmentProcessor**) const { |
322 GrEffect** effect) const { | |
323 SkDEBUGFAIL("Should not call in GPU-less build"); | 323 SkDEBUGFAIL("Should not call in GPU-less build"); |
324 return false; | 324 return false; |
325 } | 325 } |
326 | 326 |
327 #endif | 327 #endif |
328 | 328 |
329 #ifndef SK_IGNORE_TO_STRING | 329 #ifndef SK_IGNORE_TO_STRING |
330 void SkSweepGradient::toString(SkString* str) const { | 330 void SkSweepGradient::toString(SkString* str) const { |
331 str->append("SkSweepGradient: ("); | 331 str->append("SkSweepGradient: ("); |
332 | 332 |
333 str->append("center: ("); | 333 str->append("center: ("); |
334 str->appendScalar(fCenter.fX); | 334 str->appendScalar(fCenter.fX); |
335 str->append(", "); | 335 str->append(", "); |
336 str->appendScalar(fCenter.fY); | 336 str->appendScalar(fCenter.fY); |
337 str->append(") "); | 337 str->append(") "); |
338 | 338 |
339 this->INHERITED::toString(str); | 339 this->INHERITED::toString(str); |
340 | 340 |
341 str->append(")"); | 341 str->append(")"); |
342 } | 342 } |
343 #endif | 343 #endif |
OLD | NEW |