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 "GrCaps.h"
16 #include "GrCoordTransform.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,const GrFragmentProcessor * dst)34     static const GrFragmentProcessor* Create(float k1, float k2, float k3, float k4,
35                                              bool enforcePMColor, const GrFragmentProcessor* dst) {
36         return new GrArithmeticFP(k1, k2, k3, k4, enforcePMColor, dst);
37     }
38 
~GrArithmeticFP()39     ~GrArithmeticFP() override {};
40 
name()41     const char* name() const override { return "Arithmetic"; }
42 
dumpInfo()43     SkString dumpInfo() const override {
44         SkString str;
45         str.appendf("K1: %.2f K2: %.2f K3: %.2f K4: %.2f", fK1, fK2, fK3, fK4);
46         return str;
47     }
48 
k1()49     float k1() const { return fK1; }
k2()50     float k2() const { return fK2; }
k3()51     float k3() const { return fK3; }
k4()52     float k4() const { return fK4; }
enforcePMColor()53     bool enforcePMColor() const { return fEnforcePMColor; }
54 
55 private:
56     GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
57 
58     void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override;
59 
60     bool onIsEqual(const GrFragmentProcessor&) const override;
61 
62     void onComputeInvariantOutput(GrInvariantOutput* inout) const override;
63 
64     GrArithmeticFP(float k1, float k2, float k3, float k4, bool enforcePMColor,
65                    const GrFragmentProcessor* dst);
66 
67     float                       fK1, fK2, fK3, fK4;
68     bool                        fEnforcePMColor;
69 
70     GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
71     typedef GrFragmentProcessor INHERITED;
72 };
73 
74 ///////////////////////////////////////////////////////////////////////////////
75 // Xfer Processor
76 ///////////////////////////////////////////////////////////////////////////////
77 
78 class GrArithmeticXPFactory : public GrXPFactory {
79 public:
Create(float k1,float k2,float k3,float k4,bool enforcePMColor)80     static GrXPFactory* Create(float k1, float k2, float k3, float k4, bool enforcePMColor) {
81         return new GrArithmeticXPFactory(k1, k2, k3, k4, enforcePMColor);
82     }
83 
84     void getInvariantBlendedColor(const GrProcOptInfo& colorPOI,
85                                   GrXPFactory::InvariantBlendedColor*) const override;
86 
87 private:
88     GrArithmeticXPFactory(float k1, float k2, float k3, float k4, bool enforcePMColor);
89 
90     GrXferProcessor* onCreateXferProcessor(const GrCaps& caps,
91                                            const GrPipelineOptimizations& optimizations,
92                                            bool hasMixedSamples,
93                                            const DstTexture*) const override;
94 
onWillReadDstColor(const GrCaps & caps,const GrPipelineOptimizations & optimizations,bool hasMixedSamples)95     bool onWillReadDstColor(const GrCaps& caps,
96                             const GrPipelineOptimizations& optimizations,
97                             bool hasMixedSamples) 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