aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/GrColorSpaceXform.h
blob: f63b1660f36119929bd70d9b58fb63cd9eb576c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
 * Copyright 2016 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef GrColorSpaceXform_DEFINED
#define GrColorSpaceXform_DEFINED

#include "GrColor.h"
#include "GrFragmentProcessor.h"
#include "SkColorSpace.h"
#include "SkMatrix44.h"
#include "SkRefCnt.h"

 /**
  * Represents a color space transformation
  */
class GrColorSpaceXform : public SkRefCnt {
public:
    GrColorSpaceXform(const SkColorSpaceTransferFn&, const SkMatrix44&, uint32_t);

    static sk_sp<GrColorSpaceXform> Make(const SkColorSpace* src,
                                         GrPixelConfig srcConfig,
                                         const SkColorSpace* dst);
    static sk_sp<GrColorSpaceXform> MakeGamutXform(const SkColorSpace* src,
                                                   const SkColorSpace* dst) {
        auto result = Make(src, kUnknown_GrPixelConfig, dst);
        SkASSERT(!result || 0 == (result->fFlags & ~kApplyGamutXform_Flag));
        return result;
    }

    const SkColorSpaceTransferFn& transferFn() const { return fSrcTransferFn; }
    const float* transferFnCoeffs() const {
        static_assert(0 == offsetof(SkColorSpaceTransferFn, fG), "TransferFn layout");
        return &fSrcTransferFn.fG;
    }

    const SkMatrix44& gamutXform() const { return fGamutXform; }

    /**
     * GrGLSLFragmentProcessor::GenKey() must call this and include the returned value in its
     * computed key.
     */
    static uint32_t XformKey(const GrColorSpaceXform* xform) {
        // Code generation depends on which steps we apply (as encoded by fFlags)
        return SkToBool(xform) ? xform->fFlags : 0;
    }

    static bool Equals(const GrColorSpaceXform* a, const GrColorSpaceXform* b);

    GrColor4f unclampedXform(const GrColor4f& srcColor);
    GrColor4f clampedXform(const GrColor4f& srcColor);

private:
    friend class GrGLSLColorSpaceXformHelper;

    enum Flags {
        kApplyTransferFn_Flag = 0x1,
        kApplyGamutXform_Flag = 0x2,

        // Almost never used. This handles the case where the src data is sRGB pixel config,
        // but the color space has a different transfer function. In that case, we first undo
        // the HW sRGB -> Linear conversion, before applying any other steps.
        kApplyInverseSRGB_Flag = 0x4,
    };

    SkColorSpaceTransferFn fSrcTransferFn;
    SkMatrix44 fGamutXform;
    uint32_t fFlags;
};

class GrColorSpaceXformEffect : public GrFragmentProcessor {
public:
    /**
     *  Returns a fragment processor that calls the passed in fragment processor, and then converts
     *  the color space of the output from src to dst.
     */
    static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> child,
                                                     const SkColorSpace* src,
                                                     GrPixelConfig srcConfig,
                                                     const SkColorSpace* dst);

    const char* name() const override { return "ColorSpaceXform"; }
    std::unique_ptr<GrFragmentProcessor> clone() const override;

    const GrColorSpaceXform* colorXform() const { return fColorXform.get(); }

private:
    GrColorSpaceXformEffect(std::unique_ptr<GrFragmentProcessor> child,
                            sk_sp<GrColorSpaceXform> colorXform);

    static OptimizationFlags OptFlags(const GrFragmentProcessor* child);

    GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
    void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
    bool onIsEqual(const GrFragmentProcessor&) const override;

    sk_sp<GrColorSpaceXform> fColorXform;

    typedef GrFragmentProcessor INHERITED;
};

#endif