From 8a63a4d024aa5108d1553fcc74ce27199d9c673b Mon Sep 17 00:00:00 2001 From: Brian Osman Date: Thu, 14 Mar 2019 10:11:24 -0400 Subject: [PATCH] SkLumaColorFilter: Keep alpha in [0,1] Bug: skia: Change-Id: I2287ae75745509269e3dee2b1d31ab513580e6c3 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/201221 Reviewed-by: Mike Klein Commit-Queue: Brian Osman --- src/effects/SkLumaColorFilter.cpp | 2 ++ src/gpu/effects/GrLumaColorFilterEffect.cpp | 4 ++-- src/gpu/effects/GrLumaColorFilterEffect.fp | 4 ++-- src/gpu/effects/GrLumaColorFilterEffect.h | 2 +- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/effects/SkLumaColorFilter.cpp b/src/effects/SkLumaColorFilter.cpp index 92486793b5..8c58549fda 100644 --- a/src/effects/SkLumaColorFilter.cpp +++ b/src/effects/SkLumaColorFilter.cpp @@ -20,6 +20,8 @@ void SkLumaColorFilter::onAppendStages(const SkStageRec& rec, bool shaderIsOpaque) const { rec.fPipeline->append(SkRasterPipeline::luminance_to_alpha); + rec.fPipeline->append(SkRasterPipeline::clamp_0); + rec.fPipeline->append(SkRasterPipeline::clamp_1); } sk_sp SkLumaColorFilter::Make() { diff --git a/src/gpu/effects/GrLumaColorFilterEffect.cpp b/src/gpu/effects/GrLumaColorFilterEffect.cpp index b21dbc647b..d0b1e1c814 100644 --- a/src/gpu/effects/GrLumaColorFilterEffect.cpp +++ b/src/gpu/effects/GrLumaColorFilterEffect.cpp @@ -23,8 +23,8 @@ public: const GrLumaColorFilterEffect& _outer = args.fFp.cast(); (void)_outer; fragBuilder->codeAppendf( - "\nhalf luma = dot(half3(0.21260000000000001, 0.71519999999999995, 0.0722), " - "%s.xyz);\n%s = half4(0.0, 0.0, 0.0, luma);\n", + "\nhalf luma = clamp(dot(half3(0.21260000000000001, 0.71519999999999995, 0.0722), " + "%s.xyz), 0.0, 1.0);\n%s = half4(0.0, 0.0, 0.0, luma);\n", args.fInputColor, args.fOutputColor); } diff --git a/src/gpu/effects/GrLumaColorFilterEffect.fp b/src/gpu/effects/GrLumaColorFilterEffect.fp index f53139ee25..4082d21a50 100644 --- a/src/gpu/effects/GrLumaColorFilterEffect.fp +++ b/src/gpu/effects/GrLumaColorFilterEffect.fp @@ -12,12 +12,12 @@ float luma = SK_ITU_BT709_LUM_COEFF_R * input.fR + SK_ITU_BT709_LUM_COEFF_G * input.fG + SK_ITU_BT709_LUM_COEFF_B * input.fB; - return { 0, 0, 0, luma }; + return { 0, 0, 0, SkTPin(luma, 0.0f, 1.0f) }; } } void main() { const half3 SK_ITU_BT709_LUM_COEFF = half3(0.2126, 0.7152, 0.0722); - half luma = dot(SK_ITU_BT709_LUM_COEFF, sk_InColor.rgb); + half luma = saturate(dot(SK_ITU_BT709_LUM_COEFF, sk_InColor.rgb)); sk_OutColor = half4(0, 0, 0, luma); } \ No newline at end of file diff --git a/src/gpu/effects/GrLumaColorFilterEffect.h b/src/gpu/effects/GrLumaColorFilterEffect.h index bc4b8c408e..c93f6a3acf 100644 --- a/src/gpu/effects/GrLumaColorFilterEffect.h +++ b/src/gpu/effects/GrLumaColorFilterEffect.h @@ -20,7 +20,7 @@ public: SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override { float luma = SK_ITU_BT709_LUM_COEFF_R * input.fR + SK_ITU_BT709_LUM_COEFF_G * input.fG + SK_ITU_BT709_LUM_COEFF_B * input.fB; - return {0, 0, 0, luma}; + return {0, 0, 0, SkTPin(luma, 0.0f, 1.0f)}; } static std::unique_ptr Make() { return std::unique_ptr(new GrLumaColorFilterEffect());