blob: 36f38edd3f3a9d14787c04748750ec83bbad09a1 [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 */
Brian Osmancb3d0872018-10-16 15:19:28 -040050 void setColor4f(const SkPMColor4f& color) { fColor = color; }
51 const SkPMColor4f& getColor4f() const { return fColor; }
brianosmana4535a32016-06-24 12:50:19 -070052
Brian Salomon6d4b65e2017-05-03 17:06:09 -040053 void setXPFactory(const GrXPFactory* xpFactory) {
54 fXPFactory = xpFactory;
55 fTrivial &= !SkToBool(xpFactory);
56 }
egdaniel378092f2014-12-03 10:40:13 -080057
Brian Salomon94cce4c2017-02-21 16:32:20 -050058 void setPorterDuffXPFactory(SkBlendMode mode);
egdaniel95131432014-12-09 11:15:43 -080059
reed73603f32016-09-20 08:42:38 -070060 void setCoverageSetOpXPFactory(SkRegion::Op, bool invertCoverage = false);
egdaniel95131432014-12-09 11:15:43 -080061
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000062 /**
joshualittb0a8a372014-09-23 09:50:21 -070063 * Appends an additional color processor to the color computation.
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000064 */
Brian Salomonaff329b2017-08-11 09:40:37 -040065 void addColorFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp) {
joshualittb0a8a372014-09-23 09:50:21 -070066 SkASSERT(fp);
bungeman06ca8ec2016-06-09 08:01:03 -070067 fColorFragmentProcessors.push_back(std::move(fp));
Brian Salomon6d4b65e2017-05-03 17:06:09 -040068 fTrivial = false;
tomhudson@google.comf13f5882012-06-25 17:27:28 +000069 }
70
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000071 /**
joshualittb0a8a372014-09-23 09:50:21 -070072 * Appends an additional coverage processor to the coverage computation.
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000073 */
Brian Salomonaff329b2017-08-11 09:40:37 -040074 void addCoverageFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp) {
joshualittb0a8a372014-09-23 09:50:21 -070075 SkASSERT(fp);
bungeman06ca8ec2016-06-09 08:01:03 -070076 fCoverageFragmentProcessors.push_back(std::move(fp));
Brian Salomon6d4b65e2017-05-03 17:06:09 -040077 fTrivial = false;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000078 }
79
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +000080 /**
81 * Helpers for adding color or coverage effects that sample a texture. The matrix is applied
82 * to the src space position to compute texture coordinates.
83 */
Brian Osman2240be92017-10-18 13:15:13 -040084 void addColorTextureProcessor(sk_sp<GrTextureProxy>, const SkMatrix&);
85 void addColorTextureProcessor(sk_sp<GrTextureProxy>, const SkMatrix&, const GrSamplerState&);
Robert Phillips901f29a2017-01-24 16:24:41 -050086
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040087 void addCoverageTextureProcessor(sk_sp<GrTextureProxy>, const SkMatrix&);
Brian Salomon2bbdcc42017-09-07 12:36:34 -040088 void addCoverageTextureProcessor(sk_sp<GrTextureProxy>, const SkMatrix&, const GrSamplerState&);
Robert Phillips901f29a2017-01-24 16:24:41 -050089
bsalomonac856c92015-08-27 06:30:17 -070090 int numColorFragmentProcessors() const { return fColorFragmentProcessors.count(); }
91 int numCoverageFragmentProcessors() const { return fCoverageFragmentProcessors.count(); }
92 int numTotalFragmentProcessors() const { return this->numColorFragmentProcessors() +
93 this->numCoverageFragmentProcessors(); }
bsalomon@google.come3d32162012-07-20 13:37:06 +000094
Brian Salomona1633922017-01-09 11:46:10 -050095 const GrXPFactory* getXPFactory() const { return fXPFactory; }
egdaniel378092f2014-12-03 10:40:13 -080096
bungeman06ca8ec2016-06-09 08:01:03 -070097 GrFragmentProcessor* getColorFragmentProcessor(int i) const {
98 return fColorFragmentProcessors[i].get();
bsalomonac856c92015-08-27 06:30:17 -070099 }
bungeman06ca8ec2016-06-09 08:01:03 -0700100 GrFragmentProcessor* getCoverageFragmentProcessor(int i) const {
101 return fCoverageFragmentProcessors[i].get();
bsalomonac856c92015-08-27 06:30:17 -0700102 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000103
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000104 /**
cdalton1fa45722015-06-02 10:43:39 -0700105 * 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.
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000109 */
Brian Osman9a9baae2018-11-05 15:06:26 -0500110 bool isConstantBlendedColor(SkPMColor4f* constantColor) const;
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000111
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400112 /**
113 * A trivial paint is one that uses src-over and has no fragment processors.
114 * It may have variable sRGB settings.
115 **/
116 bool isTrivial() const { return fTrivial; }
117
Mike Klein16885072018-12-11 09:54:31 -0500118 friend void assert_alive(GrPaint& p) {
119 SkASSERT(p.fAlive);
120 }
121
joshualitt5531d512014-12-17 15:50:11 -0800122private:
Brian Salomonb74ef032017-08-10 12:46:01 -0400123 // Since paint copying is expensive if there are fragment processors, we require going through
124 // the Clone() method.
125 GrPaint(const GrPaint&);
Brian Salomon82f44312017-01-11 13:42:54 -0500126 GrPaint& operator=(const GrPaint&) = delete;
127
Brian Salomon92ce5942017-01-18 11:01:10 -0500128 friend class GrProcessorSet;
Brian Salomon82f44312017-01-11 13:42:54 -0500129
Brian Salomon82f44312017-01-11 13:42:54 -0500130 const GrXPFactory* fXPFactory = nullptr;
Brian Salomonaff329b2017-08-11 09:40:37 -0400131 SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> fColorFragmentProcessors;
132 SkSTArray<2, std::unique_ptr<GrFragmentProcessor>> fCoverageFragmentProcessors;
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400133 bool fTrivial = true;
Brian Osmancb3d0872018-10-16 15:19:28 -0400134 SkPMColor4f fColor = SK_PMColor4fWHITE;
Mike Klein16885072018-12-11 09:54:31 -0500135 SkDEBUGCODE(bool fAlive = true;) // Set false after moved from.
Brian Salomon82f44312017-01-11 13:42:54 -0500136};
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000137
bsalomon@google.com27847de2011-02-22 20:59:41 +0000138#endif