commit-bot@chromium.org | 6c1ee2d | 2013-10-07 18:00:17 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkLumaColorFilter.h" |
| 9 | |
| 10 | #include "SkColorPriv.h" |
| 11 | #include "SkString.h" |
commit-bot@chromium.org | 6c1ee2d | 2013-10-07 18:00:17 +0000 | [diff] [blame] | 12 | |
| 13 | #if SK_SUPPORT_GPU |
| 14 | #include "gl/GrGLEffect.h" |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 15 | #include "gl/GrGLShaderBuilder.h" |
commit-bot@chromium.org | 6c1ee2d | 2013-10-07 18:00:17 +0000 | [diff] [blame] | 16 | #include "GrContext.h" |
| 17 | #include "GrTBackendEffectFactory.h" |
| 18 | #endif |
| 19 | |
| 20 | void SkLumaColorFilter::filterSpan(const SkPMColor src[], int count, |
| 21 | SkPMColor dst[]) const { |
commit-bot@chromium.org | 6c1ee2d | 2013-10-07 18:00:17 +0000 | [diff] [blame] | 22 | for (int i = 0; i < count; ++i) { |
| 23 | SkPMColor c = src[i]; |
| 24 | |
commit-bot@chromium.org | d494b09 | 2013-10-10 20:13:51 +0000 | [diff] [blame] | 25 | /* |
| 26 | * While LuminanceToAlpha is defined to operate on un-premultiplied |
| 27 | * inputs, due to the final alpha scaling it can be computed based on |
| 28 | * premultipled components: |
| 29 | * |
| 30 | * LumA = (k1 * r / a + k2 * g / a + k3 * b / a) * a |
| 31 | * LumA = (k1 * r + k2 * g + k3 * b) |
| 32 | */ |
| 33 | unsigned luma = SkComputeLuminance(SkGetPackedR32(c), |
| 34 | SkGetPackedG32(c), |
| 35 | SkGetPackedB32(c)); |
| 36 | dst[i] = SkPackARGB32(luma, 0, 0, 0); |
commit-bot@chromium.org | 6c1ee2d | 2013-10-07 18:00:17 +0000 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
| 40 | SkColorFilter* SkLumaColorFilter::Create() { |
| 41 | return SkNEW(SkLumaColorFilter); |
| 42 | } |
| 43 | |
| 44 | SkLumaColorFilter::SkLumaColorFilter() |
| 45 | : INHERITED() { |
| 46 | } |
| 47 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 48 | SkLumaColorFilter::SkLumaColorFilter(SkReadBuffer& buffer) |
commit-bot@chromium.org | 6c1ee2d | 2013-10-07 18:00:17 +0000 | [diff] [blame] | 49 | : INHERITED(buffer) { |
| 50 | } |
| 51 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 52 | void SkLumaColorFilter::flatten(SkWriteBuffer&) const { |
commit-bot@chromium.org | 6c1ee2d | 2013-10-07 18:00:17 +0000 | [diff] [blame] | 53 | } |
| 54 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 55 | #ifndef SK_IGNORE_TO_STRING |
commit-bot@chromium.org | 6c1ee2d | 2013-10-07 18:00:17 +0000 | [diff] [blame] | 56 | void SkLumaColorFilter::toString(SkString* str) const { |
| 57 | str->append("SkLumaColorFilter "); |
| 58 | } |
| 59 | #endif |
| 60 | |
| 61 | #if SK_SUPPORT_GPU |
| 62 | class LumaColorFilterEffect : public GrEffect { |
| 63 | public: |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 64 | static GrEffect* Create() { |
bsalomon | 55fad7a | 2014-07-08 07:34:20 -0700 | [diff] [blame] | 65 | GR_CREATE_STATIC_EFFECT(gLumaEffect, LumaColorFilterEffect, ()); |
| 66 | return SkRef(gLumaEffect); |
commit-bot@chromium.org | 6c1ee2d | 2013-10-07 18:00:17 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static const char* Name() { return "Luminance-to-Alpha"; } |
| 70 | |
| 71 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
| 72 | return GrTBackendEffectFactory<LumaColorFilterEffect>::getInstance(); |
| 73 | } |
| 74 | |
| 75 | virtual void getConstantColorComponents(GrColor* color, |
| 76 | uint32_t* validFlags) const SK_OVERRIDE { |
commit-bot@chromium.org | d494b09 | 2013-10-10 20:13:51 +0000 | [diff] [blame] | 77 | // The output is always black. |
| 78 | *color = GrColorPackRGBA(0, 0, 0, GrColorUnpackA(*color)); |
| 79 | *validFlags = kRGB_GrColorComponentFlags; |
commit-bot@chromium.org | 6c1ee2d | 2013-10-07 18:00:17 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | class GLEffect : public GrGLEffect { |
| 83 | public: |
| 84 | GLEffect(const GrBackendEffectFactory& factory, |
| 85 | const GrDrawEffect&) |
| 86 | : INHERITED(factory) { |
| 87 | } |
| 88 | |
| 89 | static EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&) { |
| 90 | // this class always generates the same code. |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | virtual void emitCode(GrGLShaderBuilder* builder, |
| 95 | const GrDrawEffect&, |
| 96 | EffectKey, |
| 97 | const char* outputColor, |
| 98 | const char* inputColor, |
| 99 | const TransformedCoordsArray&, |
| 100 | const TextureSamplerArray&) SK_OVERRIDE { |
| 101 | if (NULL == inputColor) { |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 102 | inputColor = "vec4(1)"; |
commit-bot@chromium.org | 6c1ee2d | 2013-10-07 18:00:17 +0000 | [diff] [blame] | 103 | } |
| 104 | |
commit-bot@chromium.org | 6c1ee2d | 2013-10-07 18:00:17 +0000 | [diff] [blame] | 105 | builder->fsCodeAppendf("\tfloat luma = dot(vec3(%f, %f, %f), %s.rgb);\n", |
| 106 | SK_ITU_BT709_LUM_COEFF_R, |
| 107 | SK_ITU_BT709_LUM_COEFF_G, |
| 108 | SK_ITU_BT709_LUM_COEFF_B, |
| 109 | inputColor); |
commit-bot@chromium.org | d494b09 | 2013-10-10 20:13:51 +0000 | [diff] [blame] | 110 | builder->fsCodeAppendf("\t%s = vec4(0, 0, 0, luma);\n", |
| 111 | outputColor); |
commit-bot@chromium.org | 6c1ee2d | 2013-10-07 18:00:17 +0000 | [diff] [blame] | 112 | |
| 113 | } |
| 114 | |
| 115 | private: |
| 116 | typedef GrGLEffect INHERITED; |
| 117 | }; |
| 118 | |
| 119 | private: |
| 120 | virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE { |
| 121 | return true; |
| 122 | } |
| 123 | }; |
| 124 | |
bsalomon | 97b9ab7 | 2014-07-08 06:52:35 -0700 | [diff] [blame] | 125 | GrEffect* SkLumaColorFilter::asNewEffect(GrContext*) const { |
commit-bot@chromium.org | 6c1ee2d | 2013-10-07 18:00:17 +0000 | [diff] [blame] | 126 | return LumaColorFilterEffect::Create(); |
| 127 | } |
| 128 | #endif |