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 
22 class GrInvariantOutput;
23 class GrProcOptInfo;
24 class GrTexture;
25 
26 ///////////////////////////////////////////////////////////////////////////////
27 // Fragment Processor
28 ///////////////////////////////////////////////////////////////////////////////
29 
30 class GrGLArtithmeticFP;
31 
32 class GrArithmeticFP : public GrFragmentProcessor {
33 public:
Create(float k1,float k2,float k3,float k4,bool enforcePMColor,GrTexture * background)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 
~GrArithmeticFP()39     ~GrArithmeticFP() override {};
40 
name()41     const char* name() const override { return "Arithmetic"; }
42 
43     void getGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override;
44 
45     GrGLFragmentProcessor* createGLInstance() const override;
46 
k1()47     float k1() const { return fK1; }
k2()48     float k2() const { return fK2; }
k3()49     float k3() const { return fK3; }
k4()50     float k4() const { return fK4; }
enforcePMColor()51     bool enforcePMColor() const { return fEnforcePMColor; }
52 
53 private:
54     bool onIsEqual(const GrFragmentProcessor&) const override;
55 
56     void onComputeInvariantOutput(GrInvariantOutput* inout) const 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 
74 class GrArithmeticXPFactory : public GrXPFactory {
75 public:
Create(float k1,float k2,float k3,float k4,bool enforcePMColor)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 
supportsRGBCoverage(GrColor knownColor,uint32_t knownColorFlags)80     bool supportsRGBCoverage(GrColor knownColor, uint32_t knownColorFlags) const override {
81         return true;
82     }
83 
84     void getInvariantOutput(const GrProcOptInfo& colorPOI, const GrProcOptInfo& coveragePOI,
85                             GrXPFactory::InvariantOutput*) const override;
86 
87 private:
88     GrArithmeticXPFactory(float k1, float k2, float k3, float k4, bool enforcePMColor);
89 
90     GrXferProcessor* onCreateXferProcessor(const GrDrawTargetCaps& caps,
91                                            const GrProcOptInfo& colorPOI,
92                                            const GrProcOptInfo& coveragePOI,
93                                            const GrDeviceCoordTexture* dstCopy) const override;
94 
willReadDstColor(const GrDrawTargetCaps & caps,const GrProcOptInfo & colorPOI,const GrProcOptInfo & coveragePOI)95     bool willReadDstColor(const GrDrawTargetCaps& caps,
96                           const GrProcOptInfo& colorPOI,
97                           const GrProcOptInfo& coveragePOI) const override {
98         return true;
99     }
100 
onIsEqual(const GrXPFactory & xpfBase)101     bool onIsEqual(const GrXPFactory& xpfBase) const override {
102         const GrArithmeticXPFactory& xpf = xpfBase.cast<GrArithmeticXPFactory>();
103         if (fK1 != xpf.fK1 ||
104             fK2 != xpf.fK2 ||
105             fK3 != xpf.fK3 ||
106             fK4 != xpf.fK4 ||
107             fEnforcePMColor != xpf.fEnforcePMColor) {
108             return false;
109         }
110         return true;
111     }
112 
113     GR_DECLARE_XP_FACTORY_TEST;
114 
115     float                       fK1, fK2, fK3, fK4;
116     bool                        fEnforcePMColor;
117 
118     typedef GrXPFactory INHERITED;
119 };
120 
121 #endif
122 #endif
123