xref: /aosp_15_r20/external/skia/src/gpu/ganesh/GrPaint.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 
2 /*
3  * Copyright 2011 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 #ifndef GrPaint_DEFINED
9 #define GrPaint_DEFINED
10 
11 #include "include/core/SkRegion.h"
12 #include "include/private/SkColorData.h"
13 #include "include/private/base/SkAssert.h"
14 #include "include/private/base/SkDebug.h"
15 #include "include/private/base/SkTo.h"
16 #include "src/gpu/ganesh/GrFragmentProcessor.h"
17 
18 #include <memory>
19 #include <utility>
20 
21 class GrXPFactory;
22 enum class SkBlendMode;
23 
24 /**
25  * The paint describes how color and coverage are computed at each pixel by GrContext draw
26  * functions and the how color is blended with the destination pixel.
27  *
28  * The paint allows installation of custom color and coverage stages. New types of stages are
29  * created by subclassing GrProcessor.
30  *
31  * The primitive color computation starts with the color specified by setColor(). This color is the
32  * input to the first color stage. Each color stage feeds its output to the next color stage.
33  *
34  * Fractional pixel coverage follows a similar flow. The GrGeometryProcessor (specified elsewhere)
35  * provides the initial coverage which is passed to the first coverage fragment processor, which
36  * feeds its output to next coverage fragment processor.
37  *
38  * setXPFactory is used to control blending between the output color and dest. It also implements
39  * the application of fractional coverage from the coverage pipeline.
40  */
41 class GrPaint {
42 public:
43     GrPaint() = default;
44     ~GrPaint() = default;
45 
Clone(const GrPaint & src)46     static GrPaint Clone(const GrPaint& src) { return GrPaint(src); }
47 
48     /**
49      * The initial color of the drawn primitive. Defaults to solid white.
50      */
setColor4f(const SkPMColor4f & color)51     void setColor4f(const SkPMColor4f& color) { fColor = color; }
getColor4f()52     const SkPMColor4f& getColor4f() const { return fColor; }
53 
setXPFactory(const GrXPFactory * xpFactory)54     void setXPFactory(const GrXPFactory* xpFactory) {
55         fXPFactory = xpFactory;
56         fTrivial &= !SkToBool(xpFactory);
57     }
58 
59     void setPorterDuffXPFactory(SkBlendMode mode);
60 
61     void setCoverageSetOpXPFactory(SkRegion::Op, bool invertCoverage = false);
62 
63     /**
64      * Sets a processor for color computation.
65      */
setColorFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp)66     void setColorFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp) {
67         SkASSERT(fp);
68         SkASSERT(fColorFragmentProcessor == nullptr);
69         fColorFragmentProcessor = std::move(fp);
70         fTrivial = false;
71     }
72 
73     /**
74      * Appends an additional coverage processor to the coverage computation.
75      */
setCoverageFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp)76     void setCoverageFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp) {
77         SkASSERT(fp);
78         SkASSERT(fCoverageFragmentProcessor == nullptr);
79         fCoverageFragmentProcessor = std::move(fp);
80         fTrivial = false;
81     }
82 
hasColorFragmentProcessor()83     bool hasColorFragmentProcessor() const { return fColorFragmentProcessor ? true : false; }
hasCoverageFragmentProcessor()84     int hasCoverageFragmentProcessor() const { return fCoverageFragmentProcessor ? true : false; }
numTotalFragmentProcessors()85     int numTotalFragmentProcessors() const {
86         return (this->hasColorFragmentProcessor() ? 1 : 0) +
87                (this->hasCoverageFragmentProcessor() ? 1 : 0);
88     }
89 
getXPFactory()90     const GrXPFactory* getXPFactory() const { return fXPFactory; }
91 
getColorFragmentProcessor()92     GrFragmentProcessor* getColorFragmentProcessor() const {
93         return fColorFragmentProcessor.get();
94     }
getCoverageFragmentProcessor()95     GrFragmentProcessor* getCoverageFragmentProcessor() const {
96         return fCoverageFragmentProcessor.get();
97     }
usesLocalCoords()98     bool usesLocalCoords() const {
99         // The sample coords for the top level FPs are implicitly the GP's local coords.
100         return (fColorFragmentProcessor && fColorFragmentProcessor->usesSampleCoords()) ||
101                (fCoverageFragmentProcessor && fCoverageFragmentProcessor->usesSampleCoords());
102     }
103 
104     /**
105      * Returns true if the paint's output color will be constant after blending. If the result is
106      * true, constantColor will be updated to contain the constant color. Note that we can conflate
107      * coverage and color, so the actual values written to pixels with partial coverage may still
108      * not seem constant, even if this function returns true.
109      */
110     bool isConstantBlendedColor(SkPMColor4f* constantColor) const;
111 
112     /**
113      * A trivial paint is one that uses src-over and has no fragment processors.
114      * It may have variable sRGB settings.
115      **/
isTrivial()116     bool isTrivial() const { return fTrivial; }
117 
assert_alive(GrPaint & p)118     friend void assert_alive(GrPaint& p) {
119         SkASSERT(p.fAlive);
120     }
121 
122 private:
123     // Since paint copying is expensive if there are fragment processors, we require going through
124     // the Clone() method.
125     GrPaint(const GrPaint&);
126     GrPaint& operator=(const GrPaint&) = delete;
127 
128     friend class GrProcessorSet;
129 
130     const GrXPFactory* fXPFactory = nullptr;
131     std::unique_ptr<GrFragmentProcessor> fColorFragmentProcessor;
132     std::unique_ptr<GrFragmentProcessor> fCoverageFragmentProcessor;
133     bool fTrivial = true;
134     SkPMColor4f fColor = SK_PMColor4fWHITE;
135     SkDEBUGCODE(bool fAlive = true;)  // Set false after moved from.
136 };
137 
138 #endif
139