blob: 54dfc08c19f6f92e33db689bb2219f0a738b8490 [file] [log] [blame]
commit-bot@chromium.org6c1ee2d2013-10-07 18:00:17 +00001/*
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.org6c1ee2d2013-10-07 18:00:17 +000012
13#if SK_SUPPORT_GPU
14#include "gl/GrGLEffect.h"
15#include "GrContext.h"
16#include "GrTBackendEffectFactory.h"
17#endif
18
19void SkLumaColorFilter::filterSpan(const SkPMColor src[], int count,
20 SkPMColor dst[]) const {
commit-bot@chromium.org6c1ee2d2013-10-07 18:00:17 +000021 for (int i = 0; i < count; ++i) {
22 SkPMColor c = src[i];
23
commit-bot@chromium.orgd494b092013-10-10 20:13:51 +000024 /*
25 * While LuminanceToAlpha is defined to operate on un-premultiplied
26 * inputs, due to the final alpha scaling it can be computed based on
27 * premultipled components:
28 *
29 * LumA = (k1 * r / a + k2 * g / a + k3 * b / a) * a
30 * LumA = (k1 * r + k2 * g + k3 * b)
31 */
32 unsigned luma = SkComputeLuminance(SkGetPackedR32(c),
33 SkGetPackedG32(c),
34 SkGetPackedB32(c));
35 dst[i] = SkPackARGB32(luma, 0, 0, 0);
commit-bot@chromium.org6c1ee2d2013-10-07 18:00:17 +000036 }
37}
38
39SkColorFilter* SkLumaColorFilter::Create() {
40 return SkNEW(SkLumaColorFilter);
41}
42
43SkLumaColorFilter::SkLumaColorFilter()
44 : INHERITED() {
45}
46
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000047SkLumaColorFilter::SkLumaColorFilter(SkReadBuffer& buffer)
commit-bot@chromium.org6c1ee2d2013-10-07 18:00:17 +000048 : INHERITED(buffer) {
49}
50
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000051void SkLumaColorFilter::flatten(SkWriteBuffer&) const {
commit-bot@chromium.org6c1ee2d2013-10-07 18:00:17 +000052}
53
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +000054#ifndef SK_IGNORE_TO_STRING
commit-bot@chromium.org6c1ee2d2013-10-07 18:00:17 +000055void SkLumaColorFilter::toString(SkString* str) const {
56 str->append("SkLumaColorFilter ");
57}
58#endif
59
60#if SK_SUPPORT_GPU
61class LumaColorFilterEffect : public GrEffect {
62public:
63 static GrEffectRef* Create() {
bsalomon55fad7a2014-07-08 07:34:20 -070064 GR_CREATE_STATIC_EFFECT(gLumaEffect, LumaColorFilterEffect, ());
65 return SkRef(gLumaEffect);
commit-bot@chromium.org6c1ee2d2013-10-07 18:00:17 +000066 }
67
68 static const char* Name() { return "Luminance-to-Alpha"; }
69
70 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
71 return GrTBackendEffectFactory<LumaColorFilterEffect>::getInstance();
72 }
73
74 virtual void getConstantColorComponents(GrColor* color,
75 uint32_t* validFlags) const SK_OVERRIDE {
commit-bot@chromium.orgd494b092013-10-10 20:13:51 +000076 // The output is always black.
77 *color = GrColorPackRGBA(0, 0, 0, GrColorUnpackA(*color));
78 *validFlags = kRGB_GrColorComponentFlags;
commit-bot@chromium.org6c1ee2d2013-10-07 18:00:17 +000079 }
80
81 class GLEffect : public GrGLEffect {
82 public:
83 GLEffect(const GrBackendEffectFactory& factory,
84 const GrDrawEffect&)
85 : INHERITED(factory) {
86 }
87
88 static EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&) {
89 // this class always generates the same code.
90 return 0;
91 }
92
93 virtual void emitCode(GrGLShaderBuilder* builder,
94 const GrDrawEffect&,
95 EffectKey,
96 const char* outputColor,
97 const char* inputColor,
98 const TransformedCoordsArray&,
99 const TextureSamplerArray&) SK_OVERRIDE {
100 if (NULL == inputColor) {
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000101 inputColor = "vec4(1)";
commit-bot@chromium.org6c1ee2d2013-10-07 18:00:17 +0000102 }
103
commit-bot@chromium.org6c1ee2d2013-10-07 18:00:17 +0000104 builder->fsCodeAppendf("\tfloat luma = dot(vec3(%f, %f, %f), %s.rgb);\n",
105 SK_ITU_BT709_LUM_COEFF_R,
106 SK_ITU_BT709_LUM_COEFF_G,
107 SK_ITU_BT709_LUM_COEFF_B,
108 inputColor);
commit-bot@chromium.orgd494b092013-10-10 20:13:51 +0000109 builder->fsCodeAppendf("\t%s = vec4(0, 0, 0, luma);\n",
110 outputColor);
commit-bot@chromium.org6c1ee2d2013-10-07 18:00:17 +0000111
112 }
113
114 private:
115 typedef GrGLEffect INHERITED;
116 };
117
118private:
119 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE {
120 return true;
121 }
122};
123
bsalomon97b9ab72014-07-08 06:52:35 -0700124GrEffect* SkLumaColorFilter::asNewEffect(GrContext*) const {
commit-bot@chromium.org6c1ee2d2013-10-07 18:00:17 +0000125 return LumaColorFilterEffect::Create();
126}
127#endif