| 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 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 | 246 |
| 247 GR_DECLARE_FRAGMENT_PROCESSOR_TEST; | 247 GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
| 248 | 248 |
| 249 typedef Gr1DKernelEffect INHERITED; | 249 typedef Gr1DKernelEffect INHERITED; |
| 250 }; | 250 }; |
| 251 | 251 |
| 252 /////////////////////////////////////////////////////////////////////////////// | 252 /////////////////////////////////////////////////////////////////////////////// |
| 253 | 253 |
| 254 class GrGLMorphologyEffect : public GrGLSLFragmentProcessor { | 254 class GrGLMorphologyEffect : public GrGLSLFragmentProcessor { |
| 255 public: | 255 public: |
| 256 GrGLMorphologyEffect(const GrProcessor&); | |
| 257 | |
| 258 void emitCode(EmitArgs&) override; | 256 void emitCode(EmitArgs&) override; |
| 259 | 257 |
| 260 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessor
KeyBuilder* b); | 258 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessor
KeyBuilder*); |
| 261 | 259 |
| 262 protected: | 260 protected: |
| 263 void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override
; | 261 void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override
; |
| 264 | 262 |
| 265 private: | 263 private: |
| 266 int width() const { return GrMorphologyEffect::WidthFromRadius(fRadius); } | |
| 267 | |
| 268 int fRadius; | |
| 269 Gr1DKernelEffect::Direction fDirection; | |
| 270 bool fUseRange; | |
| 271 GrMorphologyEffect::MorphologyType fType; | |
| 272 GrGLSLProgramDataManager::UniformHandle fPixelSizeUni; | 264 GrGLSLProgramDataManager::UniformHandle fPixelSizeUni; |
| 273 GrGLSLProgramDataManager::UniformHandle fRangeUni; | 265 GrGLSLProgramDataManager::UniformHandle fRangeUni; |
| 274 | 266 |
| 275 typedef GrGLSLFragmentProcessor INHERITED; | 267 typedef GrGLSLFragmentProcessor INHERITED; |
| 276 }; | 268 }; |
| 277 | 269 |
| 278 GrGLMorphologyEffect::GrGLMorphologyEffect(const GrProcessor& proc) { | 270 void GrGLMorphologyEffect::emitCode(EmitArgs& args) { |
| 279 const GrMorphologyEffect& m = proc.cast<GrMorphologyEffect>(); | 271 const GrMorphologyEffect& me = args.fFp.cast<GrMorphologyEffect>(); |
| 280 fRadius = m.radius(); | |
| 281 fDirection = m.direction(); | |
| 282 fUseRange = m.useRange(); | |
| 283 fType = m.type(); | |
| 284 } | |
| 285 | 272 |
| 286 void GrGLMorphologyEffect::emitCode(EmitArgs& args) { | |
| 287 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; | 273 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 288 fPixelSizeUni = uniformHandler->addUniform(GrGLSLUniformHandler::kFragment_V
isibility, | 274 fPixelSizeUni = uniformHandler->addUniform(GrGLSLUniformHandler::kFragment_V
isibility, |
| 289 kFloat_GrSLType, kDefault_GrSLPre
cision, | 275 kFloat_GrSLType, kDefault_GrSLPre
cision, |
| 290 "PixelSize"); | 276 "PixelSize"); |
| 291 const char* pixelSizeInc = uniformHandler->getUniformCStr(fPixelSizeUni); | 277 const char* pixelSizeInc = uniformHandler->getUniformCStr(fPixelSizeUni); |
| 292 fRangeUni = uniformHandler->addUniform(GrGLSLUniformHandler::kFragment_Visib
ility, | 278 fRangeUni = uniformHandler->addUniform(GrGLSLUniformHandler::kFragment_Visib
ility, |
| 293 kVec2f_GrSLType, kDefault_GrSLPrecisi
on, | 279 kVec2f_GrSLType, kDefault_GrSLPrecisi
on, |
| 294 "Range"); | 280 "Range"); |
| 295 const char* range = uniformHandler->getUniformCStr(fRangeUni); | 281 const char* range = uniformHandler->getUniformCStr(fRangeUni); |
| 296 | 282 |
| 297 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; | 283 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 298 SkString coords2D = fragBuilder->ensureFSCoords2D(args.fCoords, 0); | 284 SkString coords2D = fragBuilder->ensureFSCoords2D(args.fCoords, 0); |
| 299 const char* func; | 285 const char* func; |
| 300 switch (fType) { | 286 switch (me.type()) { |
| 301 case GrMorphologyEffect::kErode_MorphologyType: | 287 case GrMorphologyEffect::kErode_MorphologyType: |
| 302 fragBuilder->codeAppendf("\t\t%s = vec4(1, 1, 1, 1);\n", args.fOutpu
tColor); | 288 fragBuilder->codeAppendf("\t\t%s = vec4(1, 1, 1, 1);\n", args.fOutpu
tColor); |
| 303 func = "min"; | 289 func = "min"; |
| 304 break; | 290 break; |
| 305 case GrMorphologyEffect::kDilate_MorphologyType: | 291 case GrMorphologyEffect::kDilate_MorphologyType: |
| 306 fragBuilder->codeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", args.fOutpu
tColor); | 292 fragBuilder->codeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", args.fOutpu
tColor); |
| 307 func = "max"; | 293 func = "max"; |
| 308 break; | 294 break; |
| 309 default: | 295 default: |
| 310 SkFAIL("Unexpected type"); | 296 SkFAIL("Unexpected type"); |
| 311 func = ""; // suppress warning | 297 func = ""; // suppress warning |
| 312 break; | 298 break; |
| 313 } | 299 } |
| 314 | 300 |
| 315 const char* dir; | 301 const char* dir; |
| 316 switch (fDirection) { | 302 switch (me.direction()) { |
| 317 case Gr1DKernelEffect::kX_Direction: | 303 case Gr1DKernelEffect::kX_Direction: |
| 318 dir = "x"; | 304 dir = "x"; |
| 319 break; | 305 break; |
| 320 case Gr1DKernelEffect::kY_Direction: | 306 case Gr1DKernelEffect::kY_Direction: |
| 321 dir = "y"; | 307 dir = "y"; |
| 322 break; | 308 break; |
| 323 default: | 309 default: |
| 324 SkFAIL("Unknown filter direction."); | 310 SkFAIL("Unknown filter direction."); |
| 325 dir = ""; // suppress warning | 311 dir = ""; // suppress warning |
| 326 } | 312 } |
| 327 | 313 |
| 314 int width = GrMorphologyEffect::WidthFromRadius(me.radius()); |
| 315 |
| 328 // vec2 coord = coord2D; | 316 // vec2 coord = coord2D; |
| 329 fragBuilder->codeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str()); | 317 fragBuilder->codeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str()); |
| 330 // coord.x -= radius * pixelSize; | 318 // coord.x -= radius * pixelSize; |
| 331 fragBuilder->codeAppendf("\t\tcoord.%s -= %d.0 * %s; \n", dir, fRadius, pixe
lSizeInc); | 319 fragBuilder->codeAppendf("\t\tcoord.%s -= %d.0 * %s; \n", dir, me.radius(),
pixelSizeInc); |
| 332 if (fUseRange) { | 320 if (me.useRange()) { |
| 333 // highBound = min(highBound, coord.x + (width-1) * pixelSize); | 321 // highBound = min(highBound, coord.x + (width-1) * pixelSize); |
| 334 fragBuilder->codeAppendf("\t\tfloat highBound = min(%s.y, coord.%s + %f
* %s);", | 322 fragBuilder->codeAppendf("\t\tfloat highBound = min(%s.y, coord.%s + %f
* %s);", |
| 335 range, dir, float(width() - 1), pixelSizeInc); | 323 range, dir, float(width - 1), pixelSizeInc); |
| 336 // coord.x = max(lowBound, coord.x); | 324 // coord.x = max(lowBound, coord.x); |
| 337 fragBuilder->codeAppendf("\t\tcoord.%s = max(%s.x, coord.%s);", dir, ran
ge, dir); | 325 fragBuilder->codeAppendf("\t\tcoord.%s = max(%s.x, coord.%s);", dir, ran
ge, dir); |
| 338 } | 326 } |
| 339 fragBuilder->codeAppendf("\t\tfor (int i = 0; i < %d; i++) {\n", width()); | 327 fragBuilder->codeAppendf("\t\tfor (int i = 0; i < %d; i++) {\n", width); |
| 340 fragBuilder->codeAppendf("\t\t\t%s = %s(%s, ", args.fOutputColor, func, args
.fOutputColor); | 328 fragBuilder->codeAppendf("\t\t\t%s = %s(%s, ", args.fOutputColor, func, args
.fOutputColor); |
| 341 fragBuilder->appendTextureLookup(args.fSamplers[0], "coord"); | 329 fragBuilder->appendTextureLookup(args.fSamplers[0], "coord"); |
| 342 fragBuilder->codeAppend(");\n"); | 330 fragBuilder->codeAppend(");\n"); |
| 343 // coord.x += pixelSize; | 331 // coord.x += pixelSize; |
| 344 fragBuilder->codeAppendf("\t\t\tcoord.%s += %s;\n", dir, pixelSizeInc); | 332 fragBuilder->codeAppendf("\t\t\tcoord.%s += %s;\n", dir, pixelSizeInc); |
| 345 if (fUseRange) { | 333 if (me.useRange()) { |
| 346 // coord.x = min(highBound, coord.x); | 334 // coord.x = min(highBound, coord.x); |
| 347 fragBuilder->codeAppendf("\t\t\tcoord.%s = min(highBound, coord.%s);", d
ir, dir); | 335 fragBuilder->codeAppendf("\t\t\tcoord.%s = min(highBound, coord.%s);", d
ir, dir); |
| 348 } | 336 } |
| 349 fragBuilder->codeAppend("\t\t}\n"); | 337 fragBuilder->codeAppend("\t\t}\n"); |
| 350 SkString modulate; | 338 SkString modulate; |
| 351 GrGLSLMulVarBy4f(&modulate, args.fOutputColor, args.fInputColor); | 339 GrGLSLMulVarBy4f(&modulate, args.fOutputColor, args.fInputColor); |
| 352 fragBuilder->codeAppend(modulate.c_str()); | 340 fragBuilder->codeAppend(modulate.c_str()); |
| 353 } | 341 } |
| 354 | 342 |
| 355 void GrGLMorphologyEffect::GenKey(const GrProcessor& proc, | 343 void GrGLMorphologyEffect::GenKey(const GrProcessor& proc, |
| 356 const GrGLSLCaps&, GrProcessorKeyBuilder* b) { | 344 const GrGLSLCaps&, GrProcessorKeyBuilder* b) { |
| 357 const GrMorphologyEffect& m = proc.cast<GrMorphologyEffect>(); | 345 const GrMorphologyEffect& m = proc.cast<GrMorphologyEffect>(); |
| 358 uint32_t key = static_cast<uint32_t>(m.radius()); | 346 uint32_t key = static_cast<uint32_t>(m.radius()); |
| 359 key |= (m.type() << 8); | 347 key |= (m.type() << 8); |
| 360 key |= (m.direction() << 9); | 348 key |= (m.direction() << 9); |
| 361 if (m.useRange()) key |= 1 << 10; | 349 if (m.useRange()) { |
| 350 key |= 1 << 10; |
| 351 } |
| 362 b->add32(key); | 352 b->add32(key); |
| 363 } | 353 } |
| 364 | 354 |
| 365 void GrGLMorphologyEffect::onSetData(const GrGLSLProgramDataManager& pdman, | 355 void GrGLMorphologyEffect::onSetData(const GrGLSLProgramDataManager& pdman, |
| 366 const GrProcessor& proc) { | 356 const GrProcessor& proc) { |
| 367 const GrMorphologyEffect& m = proc.cast<GrMorphologyEffect>(); | 357 const GrMorphologyEffect& m = proc.cast<GrMorphologyEffect>(); |
| 368 GrTexture& texture = *m.texture(0); | 358 GrTexture& texture = *m.texture(0); |
| 369 // the code we generated was for a specific kernel radius, direction and bou
nd usage | |
| 370 SkASSERT(m.radius() == fRadius); | |
| 371 SkASSERT(m.direction() == fDirection); | |
| 372 SkASSERT(m.useRange() == fUseRange); | |
| 373 | 359 |
| 374 float pixelSize = 0.0f; | 360 float pixelSize = 0.0f; |
| 375 switch (fDirection) { | 361 switch (m.direction()) { |
| 376 case Gr1DKernelEffect::kX_Direction: | 362 case Gr1DKernelEffect::kX_Direction: |
| 377 pixelSize = 1.0f / texture.width(); | 363 pixelSize = 1.0f / texture.width(); |
| 378 break; | 364 break; |
| 379 case Gr1DKernelEffect::kY_Direction: | 365 case Gr1DKernelEffect::kY_Direction: |
| 380 pixelSize = 1.0f / texture.height(); | 366 pixelSize = 1.0f / texture.height(); |
| 381 break; | 367 break; |
| 382 default: | 368 default: |
| 383 SkFAIL("Unknown filter direction."); | 369 SkFAIL("Unknown filter direction."); |
| 384 } | 370 } |
| 385 pdman.set1f(fPixelSizeUni, pixelSize); | 371 pdman.set1f(fPixelSizeUni, pixelSize); |
| 386 | 372 |
| 387 if (fUseRange) { | 373 if (m.useRange()) { |
| 388 const float* range = m.range(); | 374 const float* range = m.range(); |
| 389 if (fDirection && texture.origin() == kBottomLeft_GrSurfaceOrigin) { | 375 if (m.direction() && texture.origin() == kBottomLeft_GrSurfaceOrigin) { |
| 390 pdman.set2f(fRangeUni, 1.0f - range[1], 1.0f - range[0]); | 376 pdman.set2f(fRangeUni, 1.0f - range[1], 1.0f - range[0]); |
| 391 } else { | 377 } else { |
| 392 pdman.set2f(fRangeUni, range[0], range[1]); | 378 pdman.set2f(fRangeUni, range[0], range[1]); |
| 393 } | 379 } |
| 394 } | 380 } |
| 395 } | 381 } |
| 396 | 382 |
| 397 /////////////////////////////////////////////////////////////////////////////// | 383 /////////////////////////////////////////////////////////////////////////////// |
| 398 | 384 |
| 399 GrMorphologyEffect::GrMorphologyEffect(GrTexture* texture, | 385 GrMorphologyEffect::GrMorphologyEffect(GrTexture* texture, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 419 | 405 |
| 420 GrMorphologyEffect::~GrMorphologyEffect() { | 406 GrMorphologyEffect::~GrMorphologyEffect() { |
| 421 } | 407 } |
| 422 | 408 |
| 423 void GrMorphologyEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps, | 409 void GrMorphologyEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps, |
| 424 GrProcessorKeyBuilder* b) const { | 410 GrProcessorKeyBuilder* b) const { |
| 425 GrGLMorphologyEffect::GenKey(*this, caps, b); | 411 GrGLMorphologyEffect::GenKey(*this, caps, b); |
| 426 } | 412 } |
| 427 | 413 |
| 428 GrGLSLFragmentProcessor* GrMorphologyEffect::onCreateGLSLInstance() const { | 414 GrGLSLFragmentProcessor* GrMorphologyEffect::onCreateGLSLInstance() const { |
| 429 return new GrGLMorphologyEffect(*this); | 415 return new GrGLMorphologyEffect; |
| 430 } | 416 } |
| 431 bool GrMorphologyEffect::onIsEqual(const GrFragmentProcessor& sBase) const { | 417 bool GrMorphologyEffect::onIsEqual(const GrFragmentProcessor& sBase) const { |
| 432 const GrMorphologyEffect& s = sBase.cast<GrMorphologyEffect>(); | 418 const GrMorphologyEffect& s = sBase.cast<GrMorphologyEffect>(); |
| 433 return (this->radius() == s.radius() && | 419 return (this->radius() == s.radius() && |
| 434 this->direction() == s.direction() && | 420 this->direction() == s.direction() && |
| 435 this->useRange() == s.useRange() && | 421 this->useRange() == s.useRange() && |
| 436 this->type() == s.type()); | 422 this->type() == s.type()); |
| 437 } | 423 } |
| 438 | 424 |
| 439 void GrMorphologyEffect::onComputeInvariantOutput(GrInvariantOutput* inout) cons
t { | 425 void GrMorphologyEffect::onComputeInvariantOutput(GrInvariantOutput* inout) cons
t { |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 661 SkBitmap* result, SkIPoint* offset) con
st { | 647 SkBitmap* result, SkIPoint* offset) con
st { |
| 662 return this->filterImageGPUGeneric(true, proxy, src, ctx, result, offset); | 648 return this->filterImageGPUGeneric(true, proxy, src, ctx, result, offset); |
| 663 } | 649 } |
| 664 | 650 |
| 665 bool SkErodeImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const
Context& ctx, | 651 bool SkErodeImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const
Context& ctx, |
| 666 SkBitmap* result, SkIPoint* offset) cons
t { | 652 SkBitmap* result, SkIPoint* offset) cons
t { |
| 667 return this->filterImageGPUGeneric(false, proxy, src, ctx, result, offset); | 653 return this->filterImageGPUGeneric(false, proxy, src, ctx, result, offset); |
| 668 } | 654 } |
| 669 | 655 |
| 670 #endif | 656 #endif |
| OLD | NEW |