blob: 7564307f45efd214b33dc82cba68d2fb1400b5fd [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"
bsalomon848faf02014-07-11 10:01:02 -070015#include "gl/GrGLShaderBuilder.h"
commit-bot@chromium.org6c1ee2d2013-10-07 18:00:17 +000016#include "GrContext.h"
17#include "GrTBackendEffectFactory.h"
18#endif
19
20void SkLumaColorFilter::filterSpan(const SkPMColor src[], int count,
21 SkPMColor dst[]) const {
commit-bot@chromium.org6c1ee2d2013-10-07 18:00:17 +000022 for (int i = 0; i < count; ++i) {
23 SkPMColor c = src[i];
24
commit-bot@chromium.orgd494b092013-10-10 20:13:51 +000025 /*
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.org6c1ee2d2013-10-07 18:00:17 +000037 }
38}
39
40SkColorFilter* SkLumaColorFilter::Create() {
41 return SkNEW(SkLumaColorFilter);
42}
43
44SkLumaColorFilter::SkLumaColorFilter()
45 : INHERITED() {
46}
47
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000048SkLumaColorFilter::SkLumaColorFilter(SkReadBuffer& buffer)
commit-bot@chromium.org6c1ee2d2013-10-07 18:00:17 +000049 : INHERITED(buffer) {
50}
51
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000052void SkLumaColorFilter::flatten(SkWriteBuffer&) const {
commit-bot@chromium.org6c1ee2d2013-10-07 18:00:17 +000053}
54
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +000055#ifndef SK_IGNORE_TO_STRING
commit-bot@chromium.org6c1ee2d2013-10-07 18:00:17 +000056void SkLumaColorFilter::toString(SkString* str) const {
57 str->append("SkLumaColorFilter ");
58}
59#endif
60
61#if SK_SUPPORT_GPU
62class LumaColorFilterEffect : public GrEffect {
63public:
bsalomon83d081a2014-07-08 09:56:10 -070064 static GrEffect* Create() {
bsalomon55fad7a2014-07-08 07:34:20 -070065 GR_CREATE_STATIC_EFFECT(gLumaEffect, LumaColorFilterEffect, ());
66 return SkRef(gLumaEffect);
commit-bot@chromium.org6c1ee2d2013-10-07 18:00:17 +000067 }
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.orgd494b092013-10-10 20:13:51 +000077 // The output is always black.
78 *color = GrColorPackRGBA(0, 0, 0, GrColorUnpackA(*color));
79 *validFlags = kRGB_GrColorComponentFlags;
commit-bot@chromium.org6c1ee2d2013-10-07 18:00:17 +000080 }
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.org824c3462013-10-10 06:30:18 +0000102 inputColor = "vec4(1)";
commit-bot@chromium.org6c1ee2d2013-10-07 18:00:17 +0000103 }
104
commit-bot@chromium.org6c1ee2d2013-10-07 18:00:17 +0000105 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.orgd494b092013-10-10 20:13:51 +0000110 builder->fsCodeAppendf("\t%s = vec4(0, 0, 0, luma);\n",
111 outputColor);
commit-bot@chromium.org6c1ee2d2013-10-07 18:00:17 +0000112
113 }
114
115 private:
116 typedef GrGLEffect INHERITED;
117 };
118
119private:
120 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE {
121 return true;
122 }
123};
124
bsalomon97b9ab72014-07-08 06:52:35 -0700125GrEffect* SkLumaColorFilter::asNewEffect(GrContext*) const {
commit-bot@chromium.org6c1ee2d2013-10-07 18:00:17 +0000126 return LumaColorFilterEffect::Create();
127}
128#endif