1/*
2 * Copyright 2017 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#ifndef GrNonlinearColorSpaceXformEffect_DEFINED
9#define GrNonlinearColorSpaceXformEffect_DEFINED
10
11#include "GrFragmentProcessor.h"
12#include "SkColorSpace.h"
13#include "SkMatrix44.h"
14
15/**
16 * The output of this effect is the input, transformed into a different color space.
17 * This effect is used for nonlinear blending color space support - it does not assume HW sRGB
18 * capabilities, and performs both the source and destination transfer functions numerically in
19 * the shader. Any parametric transfer function is supported. Because of the nonlinear blending,
20 * premultiplication is also nonlinear - source pixels are unpremultiplied before the source
21 * transfer function, and then premultiplied after the destination transfer function.
22 */
23class GrNonlinearColorSpaceXformEffect : public GrFragmentProcessor {
24public:
25    /**
26     * The conversion effect is only well defined with a valid source and destination color space.
27     * This will return nullptr if either space is nullptr, if both spaces are equal, or if either
28     * space has a non-parametric transfer funcion (e.g. lookup table or A2B).
29     */
30    static sk_sp<GrFragmentProcessor> Make(const SkColorSpace* src, const SkColorSpace* dst);
31
32    const char* name() const override { return "NonlinearColorSpaceXform"; }
33
34    static const int kNumTransferFnCoeffs = 7;
35
36    /**
37     * Flags that specify which operations are performed for one particular conversion.
38     * Some color space pairs may not need all operations, if one or both transfer functions
39     * is linear, or if the gamuts are the same.
40     */
41    enum Ops {
42        kSrcTransfer_Op = 0x1,
43        kGamutXform_Op  = 0x2,
44        kDstTransfer_Op = 0x4,
45    };
46
47    uint32_t ops() const { return fOps; }
48    const float* srcTransferFnCoeffs() const { return fSrcTransferFnCoeffs; }
49    const float* dstTransferFnCoeffs() const { return fDstTransferFnCoeffs; }
50    const SkMatrix44& gamutXform() const { return fGamutXform; }
51
52private:
53    GrNonlinearColorSpaceXformEffect(uint32_t ops,
54                                     const SkColorSpaceTransferFn& srcTransferFn,
55                                     const SkColorSpaceTransferFn& dstTransferFn,
56                                     const SkMatrix44& gamutXform);
57
58    GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
59    void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
60    bool onIsEqual(const GrFragmentProcessor&) const override;
61
62    float fSrcTransferFnCoeffs[kNumTransferFnCoeffs];
63    float fDstTransferFnCoeffs[kNumTransferFnCoeffs];
64    SkMatrix44 fGamutXform;
65    uint32_t fOps;
66
67    GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
68
69    typedef GrFragmentProcessor INHERITED;
70};
71
72#endif
73