SkArithmeticMode_gpu.h revision e36914cb205699526988127a827d1a76c9a98d39
1/*
2 * Copyright 2015 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 SkArithmeticMode_gpu_DEFINED
9#define SkArithmeticMode_gpu_DEFINED
10
11#include "SkTypes.h"
12
13#if SK_SUPPORT_GPU
14
15#include "GrCoordTransform.h"
16#include "GrDrawTargetCaps.h"
17#include "GrFragmentProcessor.h"
18#include "GrTextureAccess.h"
19#include "GrTypes.h"
20#include "GrXferProcessor.h"
21
22class GrInvariantOutput;
23class GrProcOptInfo;
24class GrTexture;
25
26///////////////////////////////////////////////////////////////////////////////
27// Fragment Processor
28///////////////////////////////////////////////////////////////////////////////
29
30class GrGLArtithmeticFP;
31
32class GrArithmeticFP : public GrFragmentProcessor {
33public:
34    static GrFragmentProcessor* Create(float k1, float k2, float k3, float k4, bool enforcePMColor,
35                               GrTexture* background) {
36        return SkNEW_ARGS(GrArithmeticFP, (k1, k2, k3, k4, enforcePMColor, background));
37    }
38
39    ~GrArithmeticFP() SK_OVERRIDE {};
40
41    const char* name() const SK_OVERRIDE { return "Arithmetic"; }
42
43    void getGLProcessorKey(const GrGLCaps& caps, GrProcessorKeyBuilder* b) const SK_OVERRIDE;
44
45    GrGLFragmentProcessor* createGLInstance() const SK_OVERRIDE;
46
47    float k1() const { return fK1; }
48    float k2() const { return fK2; }
49    float k3() const { return fK3; }
50    float k4() const { return fK4; }
51    bool enforcePMColor() const { return fEnforcePMColor; }
52
53private:
54    bool onIsEqual(const GrFragmentProcessor&) const SK_OVERRIDE;
55
56    void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE;
57
58    GrArithmeticFP(float k1, float k2, float k3, float k4, bool enforcePMColor,
59                   GrTexture* background);
60
61    float                       fK1, fK2, fK3, fK4;
62    bool                        fEnforcePMColor;
63    GrCoordTransform            fBackgroundTransform;
64    GrTextureAccess             fBackgroundAccess;
65
66    GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
67    typedef GrFragmentProcessor INHERITED;
68};
69
70///////////////////////////////////////////////////////////////////////////////
71// Xfer Processor
72///////////////////////////////////////////////////////////////////////////////
73
74class GrArithmeticXPFactory : public GrXPFactory {
75public:
76    static GrXPFactory* Create(float k1, float k2, float k3, float k4, bool enforcePMColor) {
77        return SkNEW_ARGS(GrArithmeticXPFactory, (k1, k2, k3, k4, enforcePMColor));
78    }
79
80    bool supportsRGBCoverage(GrColor knownColor, uint32_t knownColorFlags) const SK_OVERRIDE {
81        return true;
82    }
83
84    bool canApplyCoverage(const GrProcOptInfo& colorPOI,
85                          const GrProcOptInfo& coveragePOI) const SK_OVERRIDE {
86        return true;
87    }
88
89    bool canTweakAlphaForCoverage() const SK_OVERRIDE {
90        return false;
91    }
92
93    void getInvariantOutput(const GrProcOptInfo& colorPOI, const GrProcOptInfo& coveragePOI,
94                            GrXPFactory::InvariantOutput*) const SK_OVERRIDE;
95
96private:
97    GrArithmeticXPFactory(float k1, float k2, float k3, float k4, bool enforcePMColor);
98
99    GrXferProcessor* onCreateXferProcessor(const GrProcOptInfo& colorPOI,
100                                           const GrProcOptInfo& coveragePOI,
101                                           const GrDeviceCoordTexture* dstCopy) const SK_OVERRIDE;
102
103    bool willReadDstColor(const GrProcOptInfo& colorPOI,
104                          const GrProcOptInfo& coveragePOI) const SK_OVERRIDE {
105        return true;
106    }
107
108    bool onIsEqual(const GrXPFactory& xpfBase) const SK_OVERRIDE {
109        const GrArithmeticXPFactory& xpf = xpfBase.cast<GrArithmeticXPFactory>();
110        if (fK1 != xpf.fK1 ||
111            fK2 != xpf.fK2 ||
112            fK3 != xpf.fK3 ||
113            fK4 != xpf.fK4 ||
114            fEnforcePMColor != xpf.fEnforcePMColor) {
115            return false;
116        }
117        return true;
118    }
119
120    GR_DECLARE_XP_FACTORY_TEST;
121
122    float                       fK1, fK2, fK3, fK4;
123    bool                        fEnforcePMColor;
124
125    typedef GrXPFactory INHERITED;
126};
127
128#endif
129#endif
130