blob: c5b45633a77a6277776e7928fcb0201123f18b9c [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bsalomon@google.com27847de2011-02-22 20:59:41 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.
bsalomon@google.com27847de2011-02-22 20:59:41 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
bsalomon@google.com27847de2011-02-22 20:59:41 +000010#ifndef GrPaint_DEFINED
11#define GrPaint_DEFINED
12
bsalomon@google.com27847de2011-02-22 20:59:41 +000013#include "GrColor.h"
wangyix58d890b2015-08-12 09:40:47 -070014#include "GrFragmentProcessor.h"
reed374772b2016-10-05 17:33:02 -070015#include "SkBlendMode.h"
bungeman06ca8ec2016-06-09 08:01:03 -070016#include "SkRefCnt.h"
egdanielb197b8f2015-02-17 07:34:43 -080017#include "SkRegion.h"
Brian Salomon82f44312017-01-11 13:42:54 -050018#include "SkTLazy.h"
Scroggo97c88c22011-05-11 14:05:25 +000019
Robert Phillips901f29a2017-01-24 16:24:41 -050020class GrTextureProxy;
Brian Salomon94cce4c2017-02-21 16:32:20 -050021class GrXPFactory;
Robert Phillips901f29a2017-01-24 16:24:41 -050022
bsalomon@google.com27847de2011-02-22 20:59:41 +000023/**
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000024 * The paint describes how color and coverage are computed at each pixel by GrContext draw
25 * functions and the how color is blended with the destination pixel.
26 *
27 * The paint allows installation of custom color and coverage stages. New types of stages are
joshualittb0a8a372014-09-23 09:50:21 -070028 * created by subclassing GrProcessor.
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000029 *
30 * The primitive color computation starts with the color specified by setColor(). This color is the
bsalomon37f9a262015-02-02 13:00:10 -080031 * input to the first color stage. Each color stage feeds its output to the next color stage.
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000032 *
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050033 * Fractional pixel coverage follows a similar flow. The GrGeometryProcessor (specified elsewhere)
34 * provides the initial coverage which is passed to the first coverage fragment processor, which
35 * feeds its output to next coverage fragment processor.
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000036 *
bsalomon37f9a262015-02-02 13:00:10 -080037 * setXPFactory is used to control blending between the output color and dest. It also implements
38 * the application of fractional coverage from the coverage pipeline.
bsalomon@google.com27847de2011-02-22 20:59:41 +000039 */
40class GrPaint {
41public:
Brian Salomon82f44312017-01-11 13:42:54 -050042 GrPaint() = default;
Brian Salomon82f44312017-01-11 13:42:54 -050043 ~GrPaint() = default;
Scroggo97c88c22011-05-11 14:05:25 +000044
Brian Salomonb74ef032017-08-10 12:46:01 -040045 static GrPaint Clone(const GrPaint& src) { return GrPaint(src); }
46
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000047 /**
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000048 * The initial color of the drawn primitive. Defaults to solid white.
49 */
brianosmana4535a32016-06-24 12:50:19 -070050 void setColor4f(const GrColor4f& color) { fColor = color; }
51 const GrColor4f& getColor4f() const { return fColor; }
52
53 /**
54 * Legacy getter, until all code handles 4f directly.
55 */
56 GrColor getColor() const { return fColor.toGrColor(); }
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000057
58 /**
brianosman64d094d2016-03-25 06:01:59 -070059 * Should shader output conversion from linear to sRGB be disabled.
60 * Only relevant if the destination is sRGB. Defaults to false.
61 */
62 void setDisableOutputConversionToSRGB(bool srgb) { fDisableOutputConversionToSRGB = srgb; }
63 bool getDisableOutputConversionToSRGB() const { return fDisableOutputConversionToSRGB; }
64
brianosman898235c2016-04-06 07:38:23 -070065 /**
66 * Should sRGB inputs be allowed to perform sRGB to linear conversion. With this flag
67 * set to false, sRGB textures will be treated as linear (including filtering).
68 */
69 void setAllowSRGBInputs(bool allowSRGBInputs) { fAllowSRGBInputs = allowSRGBInputs; }
70 bool getAllowSRGBInputs() const { return fAllowSRGBInputs; }
71
brianosmanb461d342016-04-13 13:10:14 -070072 /**
brianosman0e3c5542016-04-13 13:56:21 -070073 * Should rendering be gamma-correct, end-to-end. Causes sRGB render targets to behave
74 * as such (with linear blending), and sRGB inputs to be filtered and decoded correctly.
brianosmanb461d342016-04-13 13:10:14 -070075 */
76 void setGammaCorrect(bool gammaCorrect) {
Brian Salomon6d4b65e2017-05-03 17:06:09 -040077 this->setDisableOutputConversionToSRGB(!gammaCorrect);
78 this->setAllowSRGBInputs(gammaCorrect);
brianosmanb461d342016-04-13 13:10:14 -070079 }
80
Brian Salomon6d4b65e2017-05-03 17:06:09 -040081 void setXPFactory(const GrXPFactory* xpFactory) {
82 fXPFactory = xpFactory;
83 fTrivial &= !SkToBool(xpFactory);
84 }
egdaniel378092f2014-12-03 10:40:13 -080085
Brian Salomon94cce4c2017-02-21 16:32:20 -050086 void setPorterDuffXPFactory(SkBlendMode mode);
egdaniel95131432014-12-09 11:15:43 -080087
reed73603f32016-09-20 08:42:38 -070088 void setCoverageSetOpXPFactory(SkRegion::Op, bool invertCoverage = false);
egdaniel95131432014-12-09 11:15:43 -080089
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000090 /**
joshualittb0a8a372014-09-23 09:50:21 -070091 * Appends an additional color processor to the color computation.
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000092 */
Brian Salomonaff329b2017-08-11 09:40:37 -040093 void addColorFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp) {
joshualittb0a8a372014-09-23 09:50:21 -070094 SkASSERT(fp);
bungeman06ca8ec2016-06-09 08:01:03 -070095 fColorFragmentProcessors.push_back(std::move(fp));
Brian Salomon6d4b65e2017-05-03 17:06:09 -040096 fTrivial = false;
tomhudson@google.comf13f5882012-06-25 17:27:28 +000097 }
98
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000099 /**
joshualittb0a8a372014-09-23 09:50:21 -0700100 * Appends an additional coverage processor to the coverage computation.
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000101 */
Brian Salomonaff329b2017-08-11 09:40:37 -0400102 void addCoverageFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp) {
joshualittb0a8a372014-09-23 09:50:21 -0700103 SkASSERT(fp);
bungeman06ca8ec2016-06-09 08:01:03 -0700104 fCoverageFragmentProcessors.push_back(std::move(fp));
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400105 fTrivial = false;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000106 }
107
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000108 /**
109 * Helpers for adding color or coverage effects that sample a texture. The matrix is applied
110 * to the src space position to compute texture coordinates.
111 */
Brian Osman2240be92017-10-18 13:15:13 -0400112 void addColorTextureProcessor(sk_sp<GrTextureProxy>, const SkMatrix&);
113 void addColorTextureProcessor(sk_sp<GrTextureProxy>, const SkMatrix&, const GrSamplerState&);
Robert Phillips901f29a2017-01-24 16:24:41 -0500114
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400115 void addCoverageTextureProcessor(sk_sp<GrTextureProxy>, const SkMatrix&);
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400116 void addCoverageTextureProcessor(sk_sp<GrTextureProxy>, const SkMatrix&, const GrSamplerState&);
Robert Phillips901f29a2017-01-24 16:24:41 -0500117
bsalomonac856c92015-08-27 06:30:17 -0700118 int numColorFragmentProcessors() const { return fColorFragmentProcessors.count(); }
119 int numCoverageFragmentProcessors() const { return fCoverageFragmentProcessors.count(); }
120 int numTotalFragmentProcessors() const { return this->numColorFragmentProcessors() +
121 this->numCoverageFragmentProcessors(); }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000122
Brian Salomona1633922017-01-09 11:46:10 -0500123 const GrXPFactory* getXPFactory() const { return fXPFactory; }
egdaniel378092f2014-12-03 10:40:13 -0800124
bungeman06ca8ec2016-06-09 08:01:03 -0700125 GrFragmentProcessor* getColorFragmentProcessor(int i) const {
126 return fColorFragmentProcessors[i].get();
bsalomonac856c92015-08-27 06:30:17 -0700127 }
bungeman06ca8ec2016-06-09 08:01:03 -0700128 GrFragmentProcessor* getCoverageFragmentProcessor(int i) const {
129 return fCoverageFragmentProcessors[i].get();
bsalomonac856c92015-08-27 06:30:17 -0700130 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000131
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000132 /**
cdalton1fa45722015-06-02 10:43:39 -0700133 * Returns true if the paint's output color will be constant after blending. If the result is
134 * true, constantColor will be updated to contain the constant color. Note that we can conflate
135 * coverage and color, so the actual values written to pixels with partial coverage may still
136 * not seem constant, even if this function returns true.
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000137 */
Brian Salomon94cce4c2017-02-21 16:32:20 -0500138 bool isConstantBlendedColor(GrColor* constantColor) const;
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000139
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400140 /**
141 * A trivial paint is one that uses src-over and has no fragment processors.
142 * It may have variable sRGB settings.
143 **/
144 bool isTrivial() const { return fTrivial; }
145
joshualitt5531d512014-12-17 15:50:11 -0800146private:
Brian Salomonb74ef032017-08-10 12:46:01 -0400147 // Since paint copying is expensive if there are fragment processors, we require going through
148 // the Clone() method.
149 GrPaint(const GrPaint&);
Brian Salomon82f44312017-01-11 13:42:54 -0500150 GrPaint& operator=(const GrPaint&) = delete;
151
Brian Salomon92ce5942017-01-18 11:01:10 -0500152 friend class GrProcessorSet;
Brian Salomon82f44312017-01-11 13:42:54 -0500153
Brian Salomon82f44312017-01-11 13:42:54 -0500154 const GrXPFactory* fXPFactory = nullptr;
Brian Salomonaff329b2017-08-11 09:40:37 -0400155 SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> fColorFragmentProcessors;
156 SkSTArray<2, std::unique_ptr<GrFragmentProcessor>> fCoverageFragmentProcessors;
Brian Salomon82f44312017-01-11 13:42:54 -0500157 bool fDisableOutputConversionToSRGB = false;
158 bool fAllowSRGBInputs = false;
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400159 bool fTrivial = true;
Brian Salomon82f44312017-01-11 13:42:54 -0500160 GrColor4f fColor = GrColor4f::OpaqueWhite();
161};
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000162
bsalomon@google.com27847de2011-02-22 20:59:41 +0000163#endif