blob: c2520b0eaf09f1504b798a0c272f4f546283d69d [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkBlendMode.h"
14#include "include/core/SkRefCnt.h"
15#include "include/core/SkRegion.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/core/SkTLazy.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040017#include "src/gpu/GrColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrFragmentProcessor.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 Salomon078e8fa2019-11-22 04:10:18 +000084 void addColorTextureProcessor(sk_sp<GrTextureProxy>, GrColorType srcColorType, const SkMatrix&);
85 void addColorTextureProcessor(sk_sp<GrTextureProxy>, GrColorType srcColorType, const SkMatrix&,
86 const GrSamplerState&);
Robert Phillips901f29a2017-01-24 16:24:41 -050087
bsalomonac856c92015-08-27 06:30:17 -070088 int numColorFragmentProcessors() const { return fColorFragmentProcessors.count(); }
89 int numCoverageFragmentProcessors() const { return fCoverageFragmentProcessors.count(); }
90 int numTotalFragmentProcessors() const { return this->numColorFragmentProcessors() +
91 this->numCoverageFragmentProcessors(); }
bsalomon@google.come3d32162012-07-20 13:37:06 +000092
Brian Salomona1633922017-01-09 11:46:10 -050093 const GrXPFactory* getXPFactory() const { return fXPFactory; }
egdaniel378092f2014-12-03 10:40:13 -080094
bungeman06ca8ec2016-06-09 08:01:03 -070095 GrFragmentProcessor* getColorFragmentProcessor(int i) const {
96 return fColorFragmentProcessors[i].get();
bsalomonac856c92015-08-27 06:30:17 -070097 }
bungeman06ca8ec2016-06-09 08:01:03 -070098 GrFragmentProcessor* getCoverageFragmentProcessor(int i) const {
99 return fCoverageFragmentProcessors[i].get();
bsalomonac856c92015-08-27 06:30:17 -0700100 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000101
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000102 /**
cdalton1fa45722015-06-02 10:43:39 -0700103 * Returns true if the paint's output color will be constant after blending. If the result is
104 * true, constantColor will be updated to contain the constant color. Note that we can conflate
105 * coverage and color, so the actual values written to pixels with partial coverage may still
106 * not seem constant, even if this function returns true.
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000107 */
Brian Osman9a9baae2018-11-05 15:06:26 -0500108 bool isConstantBlendedColor(SkPMColor4f* constantColor) const;
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000109
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400110 /**
111 * A trivial paint is one that uses src-over and has no fragment processors.
112 * It may have variable sRGB settings.
113 **/
114 bool isTrivial() const { return fTrivial; }
115
Mike Klein16885072018-12-11 09:54:31 -0500116 friend void assert_alive(GrPaint& p) {
117 SkASSERT(p.fAlive);
118 }
119
joshualitt5531d512014-12-17 15:50:11 -0800120private:
Brian Salomonb74ef032017-08-10 12:46:01 -0400121 // Since paint copying is expensive if there are fragment processors, we require going through
122 // the Clone() method.
123 GrPaint(const GrPaint&);
Brian Salomon82f44312017-01-11 13:42:54 -0500124 GrPaint& operator=(const GrPaint&) = delete;
125
Brian Salomon92ce5942017-01-18 11:01:10 -0500126 friend class GrProcessorSet;
Brian Salomon82f44312017-01-11 13:42:54 -0500127
Brian Salomon82f44312017-01-11 13:42:54 -0500128 const GrXPFactory* fXPFactory = nullptr;
Brian Salomonaff329b2017-08-11 09:40:37 -0400129 SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> fColorFragmentProcessors;
130 SkSTArray<2, std::unique_ptr<GrFragmentProcessor>> fCoverageFragmentProcessors;
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400131 bool fTrivial = true;
Brian Osmancb3d0872018-10-16 15:19:28 -0400132 SkPMColor4f fColor = SK_PMColor4fWHITE;
Mike Klein16885072018-12-11 09:54:31 -0500133 SkDEBUGCODE(bool fAlive = true;) // Set false after moved from.
Brian Salomon82f44312017-01-11 13:42:54 -0500134};
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000135
bsalomon@google.com27847de2011-02-22 20:59:41 +0000136#endif