OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 The Android Open Source Project | 2 * Copyright 2012 The Android Open Source Project |
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 "SkMorphologyImageFilter.h" | 8 #include "SkMorphologyImageFilter.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 | 271 |
272 GR_DECLARE_EFFECT_TEST; | 272 GR_DECLARE_EFFECT_TEST; |
273 | 273 |
274 typedef Gr1DKernelEffect INHERITED; | 274 typedef Gr1DKernelEffect INHERITED; |
275 }; | 275 }; |
276 | 276 |
277 /////////////////////////////////////////////////////////////////////////////// | 277 /////////////////////////////////////////////////////////////////////////////// |
278 | 278 |
279 class GrGLMorphologyEffect : public GrGLEffect { | 279 class GrGLMorphologyEffect : public GrGLEffect { |
280 public: | 280 public: |
281 GrGLMorphologyEffect (const GrBackendEffectFactory&, const GrEffectRef&); | 281 GrGLMorphologyEffect (const GrBackendEffectFactory&, const GrDrawEffect&); |
282 | 282 |
283 virtual void emitCode(GrGLShaderBuilder*, | 283 virtual void emitCode(GrGLShaderBuilder*, |
284 const GrEffectStage&, | 284 const GrDrawEffect&, |
285 EffectKey, | 285 EffectKey, |
286 const char* vertexCoords, | |
287 const char* outputColor, | 286 const char* outputColor, |
288 const char* inputColor, | 287 const char* inputColor, |
289 const TextureSamplerArray&) SK_OVERRIDE; | 288 const TextureSamplerArray&) SK_OVERRIDE; |
290 | 289 |
291 static inline EffectKey GenKey(const GrEffectStage&, const GrGLCaps&); | 290 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
292 | 291 |
293 virtual void setData(const GrGLUniformManager&, const GrEffectStage&) SK_OVE
RRIDE; | 292 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE; |
294 | 293 |
295 private: | 294 private: |
296 int width() const { return GrMorphologyEffect::WidthFromRadius(fRadius); } | 295 int width() const { return GrMorphologyEffect::WidthFromRadius(fRadius); } |
297 | 296 |
298 int fRadius; | 297 int fRadius; |
299 GrMorphologyEffect::MorphologyType fType; | 298 GrMorphologyEffect::MorphologyType fType; |
300 GrGLUniformManager::UniformHandle fImageIncrementUni; | 299 GrGLUniformManager::UniformHandle fImageIncrementUni; |
301 GrGLEffectMatrix fEffectMatrix; | 300 GrGLEffectMatrix fEffectMatrix; |
302 | 301 |
303 typedef GrGLEffect INHERITED; | 302 typedef GrGLEffect INHERITED; |
304 }; | 303 }; |
305 | 304 |
306 GrGLMorphologyEffect::GrGLMorphologyEffect(const GrBackendEffectFactory& factory
, | 305 GrGLMorphologyEffect::GrGLMorphologyEffect(const GrBackendEffectFactory& factory
, |
307 const GrEffectRef& effect) | 306 const GrDrawEffect& drawEffect) |
308 : INHERITED(factory) | 307 : INHERITED(factory) |
309 , fImageIncrementUni(GrGLUniformManager::kInvalidUniformHandle) { | 308 , fImageIncrementUni(GrGLUniformManager::kInvalidUniformHandle) |
310 const GrMorphologyEffect& m = CastEffect<GrMorphologyEffect>(effect); | 309 , fEffectMatrix(drawEffect.castEffect<GrMorphologyEffect>().coordsType()) { |
| 310 const GrMorphologyEffect& m = drawEffect.castEffect<GrMorphologyEffect>(); |
311 fRadius = m.radius(); | 311 fRadius = m.radius(); |
312 fType = m.type(); | 312 fType = m.type(); |
313 } | 313 } |
314 | 314 |
315 void GrGLMorphologyEffect::emitCode(GrGLShaderBuilder* builder, | 315 void GrGLMorphologyEffect::emitCode(GrGLShaderBuilder* builder, |
316 const GrEffectStage&, | 316 const GrDrawEffect&, |
317 EffectKey key, | 317 EffectKey key, |
318 const char* vertexCoords, | |
319 const char* outputColor, | 318 const char* outputColor, |
320 const char* inputColor, | 319 const char* inputColor, |
321 const TextureSamplerArray& samplers) { | 320 const TextureSamplerArray& samplers) { |
322 const char* coords; | 321 const char* coords; |
323 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, vertexCoords, &coords); | 322 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, &coords); |
324 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Shader
Type, | 323 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Shader
Type, |
325 kVec2f_GrSLType, "ImageIncrement"); | 324 kVec2f_GrSLType, "ImageIncrement"); |
326 | 325 |
327 const char* func; | 326 const char* func; |
328 switch (fType) { | 327 switch (fType) { |
329 case GrMorphologyEffect::kErode_MorphologyType: | 328 case GrMorphologyEffect::kErode_MorphologyType: |
330 builder->fsCodeAppendf("\t\t%s = vec4(1, 1, 1, 1);\n", outputColor); | 329 builder->fsCodeAppendf("\t\t%s = vec4(1, 1, 1, 1);\n", outputColor); |
331 func = "min"; | 330 func = "min"; |
332 break; | 331 break; |
333 case GrMorphologyEffect::kDilate_MorphologyType: | 332 case GrMorphologyEffect::kDilate_MorphologyType: |
(...skipping 12 matching lines...) Expand all Loading... |
346 builder->fsCodeAppendf("\t\t\t%s = %s(%s, ", outputColor, func, outputColor)
; | 345 builder->fsCodeAppendf("\t\t\t%s = %s(%s, ", outputColor, func, outputColor)
; |
347 builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType, sample
rs[0], "coord"); | 346 builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType, sample
rs[0], "coord"); |
348 builder->fsCodeAppend(");\n"); | 347 builder->fsCodeAppend(");\n"); |
349 builder->fsCodeAppendf("\t\t\tcoord += %s;\n", imgInc); | 348 builder->fsCodeAppendf("\t\t\tcoord += %s;\n", imgInc); |
350 builder->fsCodeAppend("\t\t}\n"); | 349 builder->fsCodeAppend("\t\t}\n"); |
351 SkString modulate; | 350 SkString modulate; |
352 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); | 351 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); |
353 builder->fsCodeAppend(modulate.c_str()); | 352 builder->fsCodeAppend(modulate.c_str()); |
354 } | 353 } |
355 | 354 |
356 GrGLEffect::EffectKey GrGLMorphologyEffect::GenKey(const GrEffectStage& s, const
GrGLCaps&) { | 355 GrGLEffect::EffectKey GrGLMorphologyEffect::GenKey(const GrDrawEffect& drawEffec
t, |
357 const GrMorphologyEffect& m = GetEffectFromStage<GrMorphologyEffect>(s); | 356 const GrGLCaps&) { |
| 357 const GrMorphologyEffect& m = drawEffect.castEffect<GrMorphologyEffect>(); |
358 EffectKey key = static_cast<EffectKey>(m.radius()); | 358 EffectKey key = static_cast<EffectKey>(m.radius()); |
359 key |= (m.type() << 8); | 359 key |= (m.type() << 8); |
360 key <<= GrGLEffectMatrix::kKeyBits; | 360 key <<= GrGLEffectMatrix::kKeyBits; |
361 EffectKey matrixKey = GrGLEffectMatrix::GenKey(m.getMatrix(), | 361 EffectKey matrixKey = GrGLEffectMatrix::GenKey(m.getMatrix(), |
362 s.getCoordChangeMatrix(), | 362 drawEffect, |
| 363 m.coordsType(), |
363 m.texture(0)); | 364 m.texture(0)); |
364 return key | matrixKey; | 365 return key | matrixKey; |
365 } | 366 } |
366 | 367 |
367 void GrGLMorphologyEffect::setData(const GrGLUniformManager& uman, const GrEffec
tStage& stage) { | 368 void GrGLMorphologyEffect::setData(const GrGLUniformManager& uman, |
368 const Gr1DKernelEffect& kern = GetEffectFromStage<Gr1DKernelEffect>(stage); | 369 const GrDrawEffect& drawEffect) { |
| 370 const Gr1DKernelEffect& kern = drawEffect.castEffect<Gr1DKernelEffect>(); |
369 GrTexture& texture = *kern.texture(0); | 371 GrTexture& texture = *kern.texture(0); |
370 // the code we generated was for a specific kernel radius | 372 // the code we generated was for a specific kernel radius |
371 GrAssert(kern.radius() == fRadius); | 373 GrAssert(kern.radius() == fRadius); |
372 float imageIncrement[2] = { 0 }; | 374 float imageIncrement[2] = { 0 }; |
373 switch (kern.direction()) { | 375 switch (kern.direction()) { |
374 case Gr1DKernelEffect::kX_Direction: | 376 case Gr1DKernelEffect::kX_Direction: |
375 imageIncrement[0] = 1.0f / texture.width(); | 377 imageIncrement[0] = 1.0f / texture.width(); |
376 break; | 378 break; |
377 case Gr1DKernelEffect::kY_Direction: | 379 case Gr1DKernelEffect::kY_Direction: |
378 imageIncrement[1] = 1.0f / texture.height(); | 380 imageIncrement[1] = 1.0f / texture.height(); |
379 break; | 381 break; |
380 default: | 382 default: |
381 GrCrash("Unknown filter direction."); | 383 GrCrash("Unknown filter direction."); |
382 } | 384 } |
383 uman.set2fv(fImageIncrementUni, 0, 1, imageIncrement); | 385 uman.set2fv(fImageIncrementUni, 0, 1, imageIncrement); |
384 fEffectMatrix.setData(uman, kern.getMatrix(), stage.getCoordChangeMatrix(),
kern.texture(0)); | 386 fEffectMatrix.setData(uman, kern.getMatrix(), drawEffect, kern.texture(0)); |
385 } | 387 } |
386 | 388 |
387 /////////////////////////////////////////////////////////////////////////////// | 389 /////////////////////////////////////////////////////////////////////////////// |
388 | 390 |
389 GrMorphologyEffect::GrMorphologyEffect(GrTexture* texture, | 391 GrMorphologyEffect::GrMorphologyEffect(GrTexture* texture, |
390 Direction direction, | 392 Direction direction, |
391 int radius, | 393 int radius, |
392 MorphologyType type) | 394 MorphologyType type) |
393 : Gr1DKernelEffect(texture, direction, radius) | 395 : Gr1DKernelEffect(texture, direction, radius) |
394 , fType(type) { | 396 , fType(type) { |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
512 } | 514 } |
513 GrTexture* input = (GrTexture*) inputBM.getTexture(); | 515 GrTexture* input = (GrTexture*) inputBM.getTexture(); |
514 SkIRect bounds; | 516 SkIRect bounds; |
515 src.getBounds(&bounds); | 517 src.getBounds(&bounds); |
516 SkAutoTUnref<GrTexture> resultTex(apply_morphology(input, bounds, | 518 SkAutoTUnref<GrTexture> resultTex(apply_morphology(input, bounds, |
517 GrMorphologyEffect::kErode_MorphologyType, radius())); | 519 GrMorphologyEffect::kErode_MorphologyType, radius())); |
518 return SkImageFilterUtils::WrapTexture(resultTex, src.width(), src.height(),
result); | 520 return SkImageFilterUtils::WrapTexture(resultTex, src.width(), src.height(),
result); |
519 } | 521 } |
520 | 522 |
521 #endif | 523 #endif |
OLD | NEW |