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 #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING | 24 #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING |
25 SkMorphologyImageFilter::SkMorphologyImageFilter(SkReadBuffer& buffer) | 25 SkMorphologyImageFilter::SkMorphologyImageFilter(SkReadBuffer& buffer) |
26 : INHERITED(1, buffer) { | 26 : INHERITED(1, buffer) { |
27 fRadius.fWidth = buffer.readInt(); | 27 fRadius.fWidth = buffer.readInt(); |
28 fRadius.fHeight = buffer.readInt(); | 28 fRadius.fHeight = buffer.readInt(); |
29 buffer.validate((fRadius.fWidth >= 0) && | 29 buffer.validate((fRadius.fWidth >= 0) && |
30 (fRadius.fHeight >= 0)); | 30 (fRadius.fHeight >= 0)); |
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 | 322 |
323 typedef Gr1DKernelEffect INHERITED; | 323 typedef Gr1DKernelEffect INHERITED; |
324 }; | 324 }; |
325 | 325 |
326 /////////////////////////////////////////////////////////////////////////////// | 326 /////////////////////////////////////////////////////////////////////////////// |
327 | 327 |
328 class GrGLMorphologyEffect : public GrGLEffect { | 328 class GrGLMorphologyEffect : public GrGLEffect { |
329 public: | 329 public: |
330 GrGLMorphologyEffect (const GrBackendEffectFactory&, const GrDrawEffect&); | 330 GrGLMorphologyEffect (const GrBackendEffectFactory&, const GrDrawEffect&); |
331 | 331 |
332 virtual void emitCode(GrGLShaderBuilder*, | 332 virtual void emitCode(GrGLProgramBuilder*, |
333 const GrDrawEffect&, | 333 const GrDrawEffect&, |
334 const GrEffectKey&, | 334 const GrEffectKey&, |
335 const char* outputColor, | 335 const char* outputColor, |
336 const char* inputColor, | 336 const char* inputColor, |
337 const TransformedCoordsArray&, | 337 const TransformedCoordsArray&, |
338 const TextureSamplerArray&) SK_OVERRIDE; | 338 const TextureSamplerArray&) SK_OVERRIDE; |
339 | 339 |
340 static inline void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyB
uilder* b); | 340 static inline void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyB
uilder* b); |
341 | 341 |
342 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_
OVERRIDE; | 342 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_
OVERRIDE; |
343 | 343 |
344 private: | 344 private: |
345 int width() const { return GrMorphologyEffect::WidthFromRadius(fRadius); } | 345 int width() const { return GrMorphologyEffect::WidthFromRadius(fRadius); } |
346 | 346 |
347 int fRadius; | 347 int fRadius; |
348 GrMorphologyEffect::MorphologyType fType; | 348 GrMorphologyEffect::MorphologyType fType; |
349 GrGLProgramDataManager::UniformHandle fImageIncrementUni; | 349 GrGLProgramDataManager::UniformHandle fImageIncrementUni; |
350 | 350 |
351 typedef GrGLEffect INHERITED; | 351 typedef GrGLEffect INHERITED; |
352 }; | 352 }; |
353 | 353 |
354 GrGLMorphologyEffect::GrGLMorphologyEffect(const GrBackendEffectFactory& factory
, | 354 GrGLMorphologyEffect::GrGLMorphologyEffect(const GrBackendEffectFactory& factory
, |
355 const GrDrawEffect& drawEffect) | 355 const GrDrawEffect& drawEffect) |
356 : INHERITED(factory) { | 356 : INHERITED(factory) { |
357 const GrMorphologyEffect& m = drawEffect.castEffect<GrMorphologyEffect>(); | 357 const GrMorphologyEffect& m = drawEffect.castEffect<GrMorphologyEffect>(); |
358 fRadius = m.radius(); | 358 fRadius = m.radius(); |
359 fType = m.type(); | 359 fType = m.type(); |
360 } | 360 } |
361 | 361 |
362 void GrGLMorphologyEffect::emitCode(GrGLShaderBuilder* builder, | 362 void GrGLMorphologyEffect::emitCode(GrGLProgramBuilder* builder, |
363 const GrDrawEffect&, | 363 const GrDrawEffect&, |
364 const GrEffectKey& key, | 364 const GrEffectKey& key, |
365 const char* outputColor, | 365 const char* outputColor, |
366 const char* inputColor, | 366 const char* inputColor, |
367 const TransformedCoordsArray& coords, | 367 const TransformedCoordsArray& coords, |
368 const TextureSamplerArray& samplers) { | 368 const TextureSamplerArray& samplers) { |
369 SkString coords2D = builder->ensureFSCoords2D(coords, 0); | 369 fImageIncrementUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visib
ility, |
370 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibi
lity, | |
371 kVec2f_GrSLType, "ImageIncrement"); | 370 kVec2f_GrSLType, "ImageIncrement"); |
372 | 371 |
| 372 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
| 373 SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0); |
373 const char* func; | 374 const char* func; |
374 switch (fType) { | 375 switch (fType) { |
375 case GrMorphologyEffect::kErode_MorphologyType: | 376 case GrMorphologyEffect::kErode_MorphologyType: |
376 builder->fsCodeAppendf("\t\t%s = vec4(1, 1, 1, 1);\n", outputColor); | 377 fsBuilder->codeAppendf("\t\t%s = vec4(1, 1, 1, 1);\n", outputColor); |
377 func = "min"; | 378 func = "min"; |
378 break; | 379 break; |
379 case GrMorphologyEffect::kDilate_MorphologyType: | 380 case GrMorphologyEffect::kDilate_MorphologyType: |
380 builder->fsCodeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor); | 381 fsBuilder->codeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor); |
381 func = "max"; | 382 func = "max"; |
382 break; | 383 break; |
383 default: | 384 default: |
384 SkFAIL("Unexpected type"); | 385 SkFAIL("Unexpected type"); |
385 func = ""; // suppress warning | 386 func = ""; // suppress warning |
386 break; | 387 break; |
387 } | 388 } |
388 const char* imgInc = builder->getUniformCStr(fImageIncrementUni); | 389 const char* imgInc = builder->getUniformCStr(fImageIncrementUni); |
389 | 390 |
390 builder->fsCodeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords2D.c_str(
), fRadius, imgInc); | 391 fsBuilder->codeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords2D.c_str(
), fRadius, imgInc); |
391 builder->fsCodeAppendf("\t\tfor (int i = 0; i < %d; i++) {\n", this->width()
); | 392 fsBuilder->codeAppendf("\t\tfor (int i = 0; i < %d; i++) {\n", this->width()
); |
392 builder->fsCodeAppendf("\t\t\t%s = %s(%s, ", outputColor, func, outputColor)
; | 393 fsBuilder->codeAppendf("\t\t\t%s = %s(%s, ", outputColor, func, outputColor)
; |
393 builder->fsAppendTextureLookup(samplers[0], "coord"); | 394 fsBuilder->appendTextureLookup(samplers[0], "coord"); |
394 builder->fsCodeAppend(");\n"); | 395 fsBuilder->codeAppend(");\n"); |
395 builder->fsCodeAppendf("\t\t\tcoord += %s;\n", imgInc); | 396 fsBuilder->codeAppendf("\t\t\tcoord += %s;\n", imgInc); |
396 builder->fsCodeAppend("\t\t}\n"); | 397 fsBuilder->codeAppend("\t\t}\n"); |
397 SkString modulate; | 398 SkString modulate; |
398 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); | 399 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); |
399 builder->fsCodeAppend(modulate.c_str()); | 400 fsBuilder->codeAppend(modulate.c_str()); |
400 } | 401 } |
401 | 402 |
402 void GrGLMorphologyEffect::GenKey(const GrDrawEffect& drawEffect, | 403 void GrGLMorphologyEffect::GenKey(const GrDrawEffect& drawEffect, |
403 const GrGLCaps&, GrEffectKeyBuilder* b) { | 404 const GrGLCaps&, GrEffectKeyBuilder* b) { |
404 const GrMorphologyEffect& m = drawEffect.castEffect<GrMorphologyEffect>(); | 405 const GrMorphologyEffect& m = drawEffect.castEffect<GrMorphologyEffect>(); |
405 uint32_t key = static_cast<uint32_t>(m.radius()); | 406 uint32_t key = static_cast<uint32_t>(m.radius()); |
406 key |= (m.type() << 8); | 407 key |= (m.type() << 8); |
407 b->add32(key); | 408 b->add32(key); |
408 } | 409 } |
409 | 410 |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
599 SkBitmap* result, SkIPoint* offset) con
st { | 600 SkBitmap* result, SkIPoint* offset) con
st { |
600 return this->filterImageGPUGeneric(true, proxy, src, ctx, result, offset); | 601 return this->filterImageGPUGeneric(true, proxy, src, ctx, result, offset); |
601 } | 602 } |
602 | 603 |
603 bool SkErodeImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const
Context& ctx, | 604 bool SkErodeImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const
Context& ctx, |
604 SkBitmap* result, SkIPoint* offset) cons
t { | 605 SkBitmap* result, SkIPoint* offset) cons
t { |
605 return this->filterImageGPUGeneric(false, proxy, src, ctx, result, offset); | 606 return this->filterImageGPUGeneric(false, proxy, src, ctx, result, offset); |
606 } | 607 } |
607 | 608 |
608 #endif | 609 #endif |
OLD | NEW |