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 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 | 289 |
290 virtual void emitCode(GrGLShaderBuilder*, | 290 virtual void emitCode(GrGLShaderBuilder*, |
291 const GrDrawEffect&, | 291 const GrDrawEffect&, |
292 EffectKey, | 292 EffectKey, |
293 const char* outputColor, | 293 const char* outputColor, |
294 const char* inputColor, | 294 const char* inputColor, |
295 const TextureSamplerArray&) SK_OVERRIDE; | 295 const TextureSamplerArray&) SK_OVERRIDE; |
296 | 296 |
297 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); | 297 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
298 | 298 |
299 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE; | 299 virtual void setData(const GrGLContext&, const GrDrawEffect&) SK_OVERRIDE; |
300 | 300 |
301 private: | 301 private: |
302 int width() const { return GrMorphologyEffect::WidthFromRadius(fRadius); } | 302 int width() const { return GrMorphologyEffect::WidthFromRadius(fRadius); } |
303 | 303 |
304 int fRadius; | 304 int fRadius; |
305 GrMorphologyEffect::MorphologyType fType; | 305 GrMorphologyEffect::MorphologyType fType; |
306 GrGLUniformManager::UniformHandle fImageIncrementUni; | 306 GrGLUniform* fImageIncrementUni; |
307 GrGLEffectMatrix fEffectMatrix; | 307 GrGLEffectMatrix fEffectMatrix; |
308 | 308 |
309 typedef GrGLEffect INHERITED; | 309 typedef GrGLEffect INHERITED; |
310 }; | 310 }; |
311 | 311 |
312 GrGLMorphologyEffect::GrGLMorphologyEffect(const GrBackendEffectFactory& factory
, | 312 GrGLMorphologyEffect::GrGLMorphologyEffect(const GrBackendEffectFactory& factory
, |
313 const GrDrawEffect& drawEffect) | 313 const GrDrawEffect& drawEffect) |
314 : INHERITED(factory) | 314 : INHERITED(factory) |
315 , fImageIncrementUni(GrGLUniformManager::kInvalidUniformHandle) | 315 , fImageIncrementUni(NULL) |
316 , fEffectMatrix(drawEffect.castEffect<GrMorphologyEffect>().coordsType()) { | 316 , fEffectMatrix(drawEffect.castEffect<GrMorphologyEffect>().coordsType()) { |
317 const GrMorphologyEffect& m = drawEffect.castEffect<GrMorphologyEffect>(); | 317 const GrMorphologyEffect& m = drawEffect.castEffect<GrMorphologyEffect>(); |
318 fRadius = m.radius(); | 318 fRadius = m.radius(); |
319 fType = m.type(); | 319 fType = m.type(); |
320 } | 320 } |
321 | 321 |
322 void GrGLMorphologyEffect::emitCode(GrGLShaderBuilder* builder, | 322 void GrGLMorphologyEffect::emitCode(GrGLShaderBuilder* builder, |
323 const GrDrawEffect&, | 323 const GrDrawEffect&, |
324 EffectKey key, | 324 EffectKey key, |
325 const char* outputColor, | 325 const char* outputColor, |
326 const char* inputColor, | 326 const char* inputColor, |
327 const TextureSamplerArray& samplers) { | 327 const TextureSamplerArray& samplers) { |
328 const char* coords; | 328 const char* coords; |
329 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, &coords); | 329 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, &coords); |
330 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Shader
Type, | 330 |
331 kVec2f_GrSLType, "ImageIncrement"); | 331 GrGLShaderBuilder::Uniform* imageIncrementUni = |
| 332 builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| 333 kVec2f_GrSLType, "ImageIncrement"); |
| 334 fImageIncrementUni = imageIncrementUni->glUniform(); |
332 | 335 |
333 const char* func; | 336 const char* func; |
334 switch (fType) { | 337 switch (fType) { |
335 case GrMorphologyEffect::kErode_MorphologyType: | 338 case GrMorphologyEffect::kErode_MorphologyType: |
336 builder->fsCodeAppendf("\t\t%s = vec4(1, 1, 1, 1);\n", outputColor); | 339 builder->fsCodeAppendf("\t\t%s = vec4(1, 1, 1, 1);\n", outputColor); |
337 func = "min"; | 340 func = "min"; |
338 break; | 341 break; |
339 case GrMorphologyEffect::kDilate_MorphologyType: | 342 case GrMorphologyEffect::kDilate_MorphologyType: |
340 builder->fsCodeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor); | 343 builder->fsCodeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor); |
341 func = "max"; | 344 func = "max"; |
342 break; | 345 break; |
343 default: | 346 default: |
344 GrCrash("Unexpected type"); | 347 GrCrash("Unexpected type"); |
345 func = ""; // suppress warning | 348 func = ""; // suppress warning |
346 break; | 349 break; |
347 } | 350 } |
348 const char* imgInc = builder->getUniformCStr(fImageIncrementUni); | |
349 | 351 |
350 builder->fsCodeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords, fRadius
, imgInc); | 352 builder->fsCodeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords, fRadius
, imageIncrementUni->c_str()); |
351 builder->fsCodeAppendf("\t\tfor (int i = 0; i < %d; i++) {\n", this->width()
); | 353 builder->fsCodeAppendf("\t\tfor (int i = 0; i < %d; i++) {\n", this->width()
); |
352 builder->fsCodeAppendf("\t\t\t%s = %s(%s, ", outputColor, func, outputColor)
; | 354 builder->fsCodeAppendf("\t\t\t%s = %s(%s, ", outputColor, func, outputColor)
; |
353 builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType, sample
rs[0], "coord"); | 355 builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType, sample
rs[0], "coord"); |
354 builder->fsCodeAppend(");\n"); | 356 builder->fsCodeAppend(");\n"); |
355 builder->fsCodeAppendf("\t\t\tcoord += %s;\n", imgInc); | 357 builder->fsCodeAppendf("\t\t\tcoord += %s;\n", imageIncrementUni->c_str()); |
356 builder->fsCodeAppend("\t\t}\n"); | 358 builder->fsCodeAppend("\t\t}\n"); |
357 SkString modulate; | 359 SkString modulate; |
358 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); | 360 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); |
359 builder->fsCodeAppend(modulate.c_str()); | 361 builder->fsCodeAppend(modulate.c_str()); |
360 } | 362 } |
361 | 363 |
362 GrGLEffect::EffectKey GrGLMorphologyEffect::GenKey(const GrDrawEffect& drawEffec
t, | 364 GrGLEffect::EffectKey GrGLMorphologyEffect::GenKey(const GrDrawEffect& drawEffec
t, |
363 const GrGLCaps&) { | 365 const GrGLCaps&) { |
364 const GrMorphologyEffect& m = drawEffect.castEffect<GrMorphologyEffect>(); | 366 const GrMorphologyEffect& m = drawEffect.castEffect<GrMorphologyEffect>(); |
365 EffectKey key = static_cast<EffectKey>(m.radius()); | 367 EffectKey key = static_cast<EffectKey>(m.radius()); |
366 key |= (m.type() << 8); | 368 key |= (m.type() << 8); |
367 key <<= GrGLEffectMatrix::kKeyBits; | 369 key <<= GrGLEffectMatrix::kKeyBits; |
368 EffectKey matrixKey = GrGLEffectMatrix::GenKey(m.getMatrix(), | 370 EffectKey matrixKey = GrGLEffectMatrix::GenKey(m.getMatrix(), |
369 drawEffect, | 371 drawEffect, |
370 m.coordsType(), | 372 m.coordsType(), |
371 m.texture(0)); | 373 m.texture(0)); |
372 return key | matrixKey; | 374 return key | matrixKey; |
373 } | 375 } |
374 | 376 |
375 void GrGLMorphologyEffect::setData(const GrGLUniformManager& uman, | 377 void GrGLMorphologyEffect::setData(const GrGLContext& context, |
376 const GrDrawEffect& drawEffect) { | 378 const GrDrawEffect& drawEffect) { |
377 const Gr1DKernelEffect& kern = drawEffect.castEffect<Gr1DKernelEffect>(); | 379 const Gr1DKernelEffect& kern = drawEffect.castEffect<Gr1DKernelEffect>(); |
378 GrTexture& texture = *kern.texture(0); | 380 GrTexture& texture = *kern.texture(0); |
379 // the code we generated was for a specific kernel radius | 381 // the code we generated was for a specific kernel radius |
380 GrAssert(kern.radius() == fRadius); | 382 GrAssert(kern.radius() == fRadius); |
381 float imageIncrement[2] = { 0 }; | 383 float imageIncrement[2] = { 0 }; |
382 switch (kern.direction()) { | 384 switch (kern.direction()) { |
383 case Gr1DKernelEffect::kX_Direction: | 385 case Gr1DKernelEffect::kX_Direction: |
384 imageIncrement[0] = 1.0f / texture.width(); | 386 imageIncrement[0] = 1.0f / texture.width(); |
385 break; | 387 break; |
386 case Gr1DKernelEffect::kY_Direction: | 388 case Gr1DKernelEffect::kY_Direction: |
387 imageIncrement[1] = 1.0f / texture.height(); | 389 imageIncrement[1] = 1.0f / texture.height(); |
388 break; | 390 break; |
389 default: | 391 default: |
390 GrCrash("Unknown filter direction."); | 392 GrCrash("Unknown filter direction."); |
391 } | 393 } |
392 uman.set2fv(fImageIncrementUni, 0, 1, imageIncrement); | 394 fImageIncrementUni->set2fv(context, 0, 1, imageIncrement); |
393 fEffectMatrix.setData(uman, kern.getMatrix(), drawEffect, kern.texture(0)); | 395 fEffectMatrix.setData(context, kern.getMatrix(), drawEffect, kern.texture(0)
); |
394 } | 396 } |
395 | 397 |
396 /////////////////////////////////////////////////////////////////////////////// | 398 /////////////////////////////////////////////////////////////////////////////// |
397 | 399 |
398 GrMorphologyEffect::GrMorphologyEffect(GrTexture* texture, | 400 GrMorphologyEffect::GrMorphologyEffect(GrTexture* texture, |
399 Direction direction, | 401 Direction direction, |
400 int radius, | 402 int radius, |
401 MorphologyType type) | 403 MorphologyType type) |
402 : Gr1DKernelEffect(texture, direction, radius) | 404 : Gr1DKernelEffect(texture, direction, radius) |
403 , fType(type) { | 405 , fType(type) { |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
524 } | 526 } |
525 GrTexture* input = inputBM.getTexture(); | 527 GrTexture* input = inputBM.getTexture(); |
526 SkIRect bounds; | 528 SkIRect bounds; |
527 src.getBounds(&bounds); | 529 src.getBounds(&bounds); |
528 SkAutoTUnref<GrTexture> resultTex(apply_morphology(input, bounds, | 530 SkAutoTUnref<GrTexture> resultTex(apply_morphology(input, bounds, |
529 GrMorphologyEffect::kErode_MorphologyType, radius())); | 531 GrMorphologyEffect::kErode_MorphologyType, radius())); |
530 return SkImageFilterUtils::WrapTexture(resultTex, src.width(), src.height(),
result); | 532 return SkImageFilterUtils::WrapTexture(resultTex, src.width(), src.height(),
result); |
531 } | 533 } |
532 | 534 |
533 #endif | 535 #endif |
OLD | NEW |