| 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" |
| 11 #include "SkReadBuffer.h" | 11 #include "SkReadBuffer.h" |
| 12 #include "SkWriteBuffer.h" | 12 #include "SkWriteBuffer.h" |
| 13 #include "SkRect.h" | 13 #include "SkRect.h" |
| 14 #include "SkMorphology_opts.h" | 14 #include "SkMorphology_opts.h" |
| 15 #if SK_SUPPORT_GPU | 15 #if SK_SUPPORT_GPU |
| 16 #include "GrContext.h" | 16 #include "GrContext.h" |
| 17 #include "GrTexture.h" | 17 #include "GrTexture.h" |
| 18 #include "GrTBackendEffectFactory.h" | 18 #include "GrTBackendEffectFactory.h" |
| 19 #include "gl/GrGLEffect.h" | 19 #include "gl/GrGLEffect.h" |
| 20 #include "gl/GrGLShaderBuilder.h" | 20 #include "gl/builders/GrGLProgramBuilder.h" |
| 21 #include "effects/Gr1DKernelEffect.h" | 21 #include "effects/Gr1DKernelEffect.h" |
| 22 #endif | 22 #endif |
| 23 | 23 |
| 24 SkMorphologyImageFilter::SkMorphologyImageFilter(SkReadBuffer& buffer) | 24 SkMorphologyImageFilter::SkMorphologyImageFilter(SkReadBuffer& buffer) |
| 25 : INHERITED(1, buffer) { | 25 : INHERITED(1, buffer) { |
| 26 fRadius.fWidth = buffer.readInt(); | 26 fRadius.fWidth = buffer.readInt(); |
| 27 fRadius.fHeight = buffer.readInt(); | 27 fRadius.fHeight = buffer.readInt(); |
| 28 buffer.validate((fRadius.fWidth >= 0) && | 28 buffer.validate((fRadius.fWidth >= 0) && |
| 29 (fRadius.fHeight >= 0)); | 29 (fRadius.fHeight >= 0)); |
| 30 } | 30 } |
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 | 307 |
| 308 typedef Gr1DKernelEffect INHERITED; | 308 typedef Gr1DKernelEffect INHERITED; |
| 309 }; | 309 }; |
| 310 | 310 |
| 311 /////////////////////////////////////////////////////////////////////////////// | 311 /////////////////////////////////////////////////////////////////////////////// |
| 312 | 312 |
| 313 class GrGLMorphologyEffect : public GrGLEffect { | 313 class GrGLMorphologyEffect : public GrGLEffect { |
| 314 public: | 314 public: |
| 315 GrGLMorphologyEffect (const GrBackendEffectFactory&, const GrDrawEffect&); | 315 GrGLMorphologyEffect (const GrBackendEffectFactory&, const GrDrawEffect&); |
| 316 | 316 |
| 317 virtual void emitCode(GrGLShaderBuilder*, | 317 virtual void emitCode(GrGLProgramBuilder*, |
| 318 const GrDrawEffect&, | 318 const GrDrawEffect&, |
| 319 const GrEffectKey&, | 319 const GrEffectKey&, |
| 320 const char* outputColor, | 320 const char* outputColor, |
| 321 const char* inputColor, | 321 const char* inputColor, |
| 322 const TransformedCoordsArray&, | 322 const TransformedCoordsArray&, |
| 323 const TextureSamplerArray&) SK_OVERRIDE; | 323 const TextureSamplerArray&) SK_OVERRIDE; |
| 324 | 324 |
| 325 static inline void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyB
uilder* b); | 325 static inline void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyB
uilder* b); |
| 326 | 326 |
| 327 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_
OVERRIDE; | 327 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_
OVERRIDE; |
| 328 | 328 |
| 329 private: | 329 private: |
| 330 int width() const { return GrMorphologyEffect::WidthFromRadius(fRadius); } | 330 int width() const { return GrMorphologyEffect::WidthFromRadius(fRadius); } |
| 331 | 331 |
| 332 int fRadius; | 332 int fRadius; |
| 333 GrMorphologyEffect::MorphologyType fType; | 333 GrMorphologyEffect::MorphologyType fType; |
| 334 GrGLProgramDataManager::UniformHandle fImageIncrementUni; | 334 GrGLProgramDataManager::UniformHandle fImageIncrementUni; |
| 335 | 335 |
| 336 typedef GrGLEffect INHERITED; | 336 typedef GrGLEffect INHERITED; |
| 337 }; | 337 }; |
| 338 | 338 |
| 339 GrGLMorphologyEffect::GrGLMorphologyEffect(const GrBackendEffectFactory& factory
, | 339 GrGLMorphologyEffect::GrGLMorphologyEffect(const GrBackendEffectFactory& factory
, |
| 340 const GrDrawEffect& drawEffect) | 340 const GrDrawEffect& drawEffect) |
| 341 : INHERITED(factory) { | 341 : INHERITED(factory) { |
| 342 const GrMorphologyEffect& m = drawEffect.castEffect<GrMorphologyEffect>(); | 342 const GrMorphologyEffect& m = drawEffect.castEffect<GrMorphologyEffect>(); |
| 343 fRadius = m.radius(); | 343 fRadius = m.radius(); |
| 344 fType = m.type(); | 344 fType = m.type(); |
| 345 } | 345 } |
| 346 | 346 |
| 347 void GrGLMorphologyEffect::emitCode(GrGLShaderBuilder* builder, | 347 void GrGLMorphologyEffect::emitCode(GrGLProgramBuilder* builder, |
| 348 const GrDrawEffect&, | 348 const GrDrawEffect&, |
| 349 const GrEffectKey& key, | 349 const GrEffectKey& key, |
| 350 const char* outputColor, | 350 const char* outputColor, |
| 351 const char* inputColor, | 351 const char* inputColor, |
| 352 const TransformedCoordsArray& coords, | 352 const TransformedCoordsArray& coords, |
| 353 const TextureSamplerArray& samplers) { | 353 const TextureSamplerArray& samplers) { |
| 354 SkString coords2D = builder->ensureFSCoords2D(coords, 0); | 354 fImageIncrementUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visib
ility, |
| 355 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibi
lity, | |
| 356 kVec2f_GrSLType, "ImageIncrement"); | 355 kVec2f_GrSLType, "ImageIncrement"); |
| 357 | 356 |
| 357 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
| 358 SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0); |
| 358 const char* func; | 359 const char* func; |
| 359 switch (fType) { | 360 switch (fType) { |
| 360 case GrMorphologyEffect::kErode_MorphologyType: | 361 case GrMorphologyEffect::kErode_MorphologyType: |
| 361 builder->fsCodeAppendf("\t\t%s = vec4(1, 1, 1, 1);\n", outputColor); | 362 fsBuilder->codeAppendf("\t\t%s = vec4(1, 1, 1, 1);\n", outputColor); |
| 362 func = "min"; | 363 func = "min"; |
| 363 break; | 364 break; |
| 364 case GrMorphologyEffect::kDilate_MorphologyType: | 365 case GrMorphologyEffect::kDilate_MorphologyType: |
| 365 builder->fsCodeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor); | 366 fsBuilder->codeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor); |
| 366 func = "max"; | 367 func = "max"; |
| 367 break; | 368 break; |
| 368 default: | 369 default: |
| 369 SkFAIL("Unexpected type"); | 370 SkFAIL("Unexpected type"); |
| 370 func = ""; // suppress warning | 371 func = ""; // suppress warning |
| 371 break; | 372 break; |
| 372 } | 373 } |
| 373 const char* imgInc = builder->getUniformCStr(fImageIncrementUni); | 374 const char* imgInc = builder->getUniformCStr(fImageIncrementUni); |
| 374 | 375 |
| 375 builder->fsCodeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords2D.c_str(
), fRadius, imgInc); | 376 fsBuilder->codeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords2D.c_str(
), fRadius, imgInc); |
| 376 builder->fsCodeAppendf("\t\tfor (int i = 0; i < %d; i++) {\n", this->width()
); | 377 fsBuilder->codeAppendf("\t\tfor (int i = 0; i < %d; i++) {\n", this->width()
); |
| 377 builder->fsCodeAppendf("\t\t\t%s = %s(%s, ", outputColor, func, outputColor)
; | 378 fsBuilder->codeAppendf("\t\t\t%s = %s(%s, ", outputColor, func, outputColor)
; |
| 378 builder->fsAppendTextureLookup(samplers[0], "coord"); | 379 fsBuilder->appendTextureLookup(samplers[0], "coord"); |
| 379 builder->fsCodeAppend(");\n"); | 380 fsBuilder->codeAppend(");\n"); |
| 380 builder->fsCodeAppendf("\t\t\tcoord += %s;\n", imgInc); | 381 fsBuilder->codeAppendf("\t\t\tcoord += %s;\n", imgInc); |
| 381 builder->fsCodeAppend("\t\t}\n"); | 382 fsBuilder->codeAppend("\t\t}\n"); |
| 382 SkString modulate; | 383 SkString modulate; |
| 383 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); | 384 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); |
| 384 builder->fsCodeAppend(modulate.c_str()); | 385 fsBuilder->codeAppend(modulate.c_str()); |
| 385 } | 386 } |
| 386 | 387 |
| 387 void GrGLMorphologyEffect::GenKey(const GrDrawEffect& drawEffect, | 388 void GrGLMorphologyEffect::GenKey(const GrDrawEffect& drawEffect, |
| 388 const GrGLCaps&, GrEffectKeyBuilder* b) { | 389 const GrGLCaps&, GrEffectKeyBuilder* b) { |
| 389 const GrMorphologyEffect& m = drawEffect.castEffect<GrMorphologyEffect>(); | 390 const GrMorphologyEffect& m = drawEffect.castEffect<GrMorphologyEffect>(); |
| 390 uint32_t key = static_cast<uint32_t>(m.radius()); | 391 uint32_t key = static_cast<uint32_t>(m.radius()); |
| 391 key |= (m.type() << 8); | 392 key |= (m.type() << 8); |
| 392 b->add32(key); | 393 b->add32(key); |
| 393 } | 394 } |
| 394 | 395 |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 584 SkBitmap* result, SkIPoint* offset) con
st { | 585 SkBitmap* result, SkIPoint* offset) con
st { |
| 585 return this->filterImageGPUGeneric(true, proxy, src, ctx, result, offset); | 586 return this->filterImageGPUGeneric(true, proxy, src, ctx, result, offset); |
| 586 } | 587 } |
| 587 | 588 |
| 588 bool SkErodeImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const
Context& ctx, | 589 bool SkErodeImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const
Context& ctx, |
| 589 SkBitmap* result, SkIPoint* offset) cons
t { | 590 SkBitmap* result, SkIPoint* offset) cons
t { |
| 590 return this->filterImageGPUGeneric(false, proxy, src, ctx, result, offset); | 591 return this->filterImageGPUGeneric(false, proxy, src, ctx, result, offset); |
| 591 } | 592 } |
| 592 | 593 |
| 593 #endif | 594 #endif |
| OLD | NEW |