16c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org/*
26c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org * Copyright 2013 Google Inc.
36c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org *
46c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org * Use of this source code is governed by a BSD-style license that can be
56c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org * found in the LICENSE file.
66c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org */
76c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org
86c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org#include "SkLumaColorFilter.h"
96c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org
106c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org#include "SkColorPriv.h"
116c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org#include "SkString.h"
126c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org
136c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org#if SK_SUPPORT_GPU
14b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt#include "gl/GrGLProcessor.h"
1530ba436f04e61d4505fb854d5fc56079636e0788joshualitt#include "gl/builders/GrGLProgramBuilder.h"
166c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org#include "GrContext.h"
17b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt#include "GrTBackendProcessorFactory.h"
186c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org#endif
196c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org
206c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.orgvoid SkLumaColorFilter::filterSpan(const SkPMColor src[], int count,
216c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org                                   SkPMColor dst[]) const {
226c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org    for (int i = 0; i < count; ++i) {
236c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org        SkPMColor c = src[i];
246c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org
25d494b09f554d470fc6411d0924879bbfb0cb0e95commit-bot@chromium.org        /*
26d494b09f554d470fc6411d0924879bbfb0cb0e95commit-bot@chromium.org         * While LuminanceToAlpha is defined to operate on un-premultiplied
27d494b09f554d470fc6411d0924879bbfb0cb0e95commit-bot@chromium.org         * inputs, due to the final alpha scaling it can be computed based on
28d494b09f554d470fc6411d0924879bbfb0cb0e95commit-bot@chromium.org         * premultipled components:
29d494b09f554d470fc6411d0924879bbfb0cb0e95commit-bot@chromium.org         *
30d494b09f554d470fc6411d0924879bbfb0cb0e95commit-bot@chromium.org         *   LumA = (k1 * r / a + k2 * g / a + k3 * b / a) * a
31d494b09f554d470fc6411d0924879bbfb0cb0e95commit-bot@chromium.org         *   LumA = (k1 * r + k2 * g + k3 * b)
32d494b09f554d470fc6411d0924879bbfb0cb0e95commit-bot@chromium.org         */
33d494b09f554d470fc6411d0924879bbfb0cb0e95commit-bot@chromium.org        unsigned luma = SkComputeLuminance(SkGetPackedR32(c),
34d494b09f554d470fc6411d0924879bbfb0cb0e95commit-bot@chromium.org                                           SkGetPackedG32(c),
35d494b09f554d470fc6411d0924879bbfb0cb0e95commit-bot@chromium.org                                           SkGetPackedB32(c));
36d494b09f554d470fc6411d0924879bbfb0cb0e95commit-bot@chromium.org        dst[i] = SkPackARGB32(luma, 0, 0, 0);
376c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org    }
386c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org}
396c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org
406c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.orgSkColorFilter* SkLumaColorFilter::Create() {
416c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org    return SkNEW(SkLumaColorFilter);
426c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org}
436c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org
449fa60daad4d5f54c0dbe3dbcc7608a8f6d721187reedSkLumaColorFilter::SkLumaColorFilter() : INHERITED() {}
456c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org
469fa60daad4d5f54c0dbe3dbcc7608a8f6d721187reed#ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING
479fa60daad4d5f54c0dbe3dbcc7608a8f6d721187reedSkLumaColorFilter::SkLumaColorFilter(SkReadBuffer& buffer) : INHERITED(buffer) {}
489fa60daad4d5f54c0dbe3dbcc7608a8f6d721187reed#endif
496c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org
509fa60daad4d5f54c0dbe3dbcc7608a8f6d721187reedSkFlattenable* SkLumaColorFilter::CreateProc(SkReadBuffer&) {
519fa60daad4d5f54c0dbe3dbcc7608a8f6d721187reed    return SkNEW(SkLumaColorFilter);
526c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org}
536c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org
549fa60daad4d5f54c0dbe3dbcc7608a8f6d721187reedvoid SkLumaColorFilter::flatten(SkWriteBuffer&) const {}
559fa60daad4d5f54c0dbe3dbcc7608a8f6d721187reed
560f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org#ifndef SK_IGNORE_TO_STRING
576c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.orgvoid SkLumaColorFilter::toString(SkString* str) const {
586c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org    str->append("SkLumaColorFilter ");
596c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org}
606c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org#endif
616c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org
626c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org#if SK_SUPPORT_GPU
63b0a8a377f832c59cee939ad721e1f87d378b7142joshualittclass LumaColorFilterEffect : public GrFragmentProcessor {
646c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.orgpublic:
65b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt    static GrFragmentProcessor* Create() {
66b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt        GR_CREATE_STATIC_FRAGMENT_PROCESSOR(gLumaEffect, LumaColorFilterEffect, ());
6755fad7af61c21d502acb9891d631e8aa29e3628cbsalomon        return SkRef(gLumaEffect);
686c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org    }
696c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org
706c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org    static const char* Name() { return "Luminance-to-Alpha"; }
716c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org
72b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt    virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERRIDE {
73b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt        return GrTBackendFragmentProcessorFactory<LumaColorFilterEffect>::getInstance();
746c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org    }
756c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org
766c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org    virtual void getConstantColorComponents(GrColor* color,
776c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org                                            uint32_t* validFlags) const SK_OVERRIDE {
78d494b09f554d470fc6411d0924879bbfb0cb0e95commit-bot@chromium.org        // The output is always black.
79d494b09f554d470fc6411d0924879bbfb0cb0e95commit-bot@chromium.org        *color = GrColorPackRGBA(0, 0, 0, GrColorUnpackA(*color));
80d494b09f554d470fc6411d0924879bbfb0cb0e95commit-bot@chromium.org        *validFlags = kRGB_GrColorComponentFlags;
816c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org    }
826c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org
83b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt    class GLProcessor : public GrGLFragmentProcessor {
846c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org    public:
85b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt        GLProcessor(const GrBackendProcessorFactory& factory,
86b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt                    const GrProcessor&)
876c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org        : INHERITED(factory) {
886c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org        }
896c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org
90b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt        static void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder* b) {}
916c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org
9230ba436f04e61d4505fb854d5fc56079636e0788joshualitt        virtual void emitCode(GrGLProgramBuilder* builder,
93b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt                              const GrFragmentProcessor&,
94b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt                              const GrProcessorKey&,
956c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org                              const char* outputColor,
966c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org                              const char* inputColor,
976c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org                              const TransformedCoordsArray&,
986c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org                              const TextureSamplerArray&) SK_OVERRIDE {
996c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org            if (NULL == inputColor) {
100824c346b6e0e114063c1a8ad4ba7c3a669ee2cffcommit-bot@chromium.org                inputColor = "vec4(1)";
1016c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org            }
1026c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org
10330ba436f04e61d4505fb854d5fc56079636e0788joshualitt            GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder();
10430ba436f04e61d4505fb854d5fc56079636e0788joshualitt            fsBuilder->codeAppendf("\tfloat luma = dot(vec3(%f, %f, %f), %s.rgb);\n",
1056c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org                                   SK_ITU_BT709_LUM_COEFF_R,
1066c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org                                   SK_ITU_BT709_LUM_COEFF_G,
1076c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org                                   SK_ITU_BT709_LUM_COEFF_B,
1086c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org                                   inputColor);
10930ba436f04e61d4505fb854d5fc56079636e0788joshualitt            fsBuilder->codeAppendf("\t%s = vec4(0, 0, 0, luma);\n",
110d494b09f554d470fc6411d0924879bbfb0cb0e95commit-bot@chromium.org                                   outputColor);
1116c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org
1126c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org        }
1136c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org
1146c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org    private:
115b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt        typedef GrGLFragmentProcessor INHERITED;
1166c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org    };
1176c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org
1186c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.orgprivate:
119b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt    virtual bool onIsEqual(const GrProcessor&) const SK_OVERRIDE {
1206c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org        return true;
1216c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org    }
1226c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org};
1236c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org
124b0a8a377f832c59cee939ad721e1f87d378b7142joshualittGrFragmentProcessor* SkLumaColorFilter::asFragmentProcessor(GrContext*) const {
1256c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org    return LumaColorFilterEffect::Create();
1266c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org}
1276c1ee2d4e727357451c8a6fcf4a08e75890b5d6dcommit-bot@chromium.org#endif
128