blob: 5d87eb0b2581d4197301bd9b00e80855cb80aae7 [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
Brian Salomon6d4b65e2017-05-03 17:06:09 -040058 void setXPFactory(const GrXPFactory* xpFactory) {
59 fXPFactory = xpFactory;
60 fTrivial &= !SkToBool(xpFactory);
61 }
egdaniel378092f2014-12-03 10:40:13 -080062
Brian Salomon94cce4c2017-02-21 16:32:20 -050063 void setPorterDuffXPFactory(SkBlendMode mode);
egdaniel95131432014-12-09 11:15:43 -080064
reed73603f32016-09-20 08:42:38 -070065 void setCoverageSetOpXPFactory(SkRegion::Op, bool invertCoverage = false);
egdaniel95131432014-12-09 11:15:43 -080066
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000067 /**
joshualittb0a8a372014-09-23 09:50:21 -070068 * Appends an additional color processor to the color computation.
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000069 */
Brian Salomonaff329b2017-08-11 09:40:37 -040070 void addColorFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp) {
joshualittb0a8a372014-09-23 09:50:21 -070071 SkASSERT(fp);
bungeman06ca8ec2016-06-09 08:01:03 -070072 fColorFragmentProcessors.push_back(std::move(fp));
Brian Salomon6d4b65e2017-05-03 17:06:09 -040073 fTrivial = false;
tomhudson@google.comf13f5882012-06-25 17:27:28 +000074 }
75
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000076 /**
joshualittb0a8a372014-09-23 09:50:21 -070077 * Appends an additional coverage processor to the coverage computation.
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000078 */
Brian Salomonaff329b2017-08-11 09:40:37 -040079 void addCoverageFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp) {
joshualittb0a8a372014-09-23 09:50:21 -070080 SkASSERT(fp);
bungeman06ca8ec2016-06-09 08:01:03 -070081 fCoverageFragmentProcessors.push_back(std::move(fp));
Brian Salomon6d4b65e2017-05-03 17:06:09 -040082 fTrivial = false;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000083 }
84
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +000085 /**
86 * Helpers for adding color or coverage effects that sample a texture. The matrix is applied
87 * to the src space position to compute texture coordinates.
88 */
Brian Osman2240be92017-10-18 13:15:13 -040089 void addColorTextureProcessor(sk_sp<GrTextureProxy>, const SkMatrix&);
90 void addColorTextureProcessor(sk_sp<GrTextureProxy>, const SkMatrix&, const GrSamplerState&);
Robert Phillips901f29a2017-01-24 16:24:41 -050091
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040092 void addCoverageTextureProcessor(sk_sp<GrTextureProxy>, const SkMatrix&);
Brian Salomon2bbdcc42017-09-07 12:36:34 -040093 void addCoverageTextureProcessor(sk_sp<GrTextureProxy>, const SkMatrix&, const GrSamplerState&);
Robert Phillips901f29a2017-01-24 16:24:41 -050094
bsalomonac856c92015-08-27 06:30:17 -070095 int numColorFragmentProcessors() const { return fColorFragmentProcessors.count(); }
96 int numCoverageFragmentProcessors() const { return fCoverageFragmentProcessors.count(); }
97 int numTotalFragmentProcessors() const { return this->numColorFragmentProcessors() +
98 this->numCoverageFragmentProcessors(); }
bsalomon@google.come3d32162012-07-20 13:37:06 +000099
Brian Salomona1633922017-01-09 11:46:10 -0500100 const GrXPFactory* getXPFactory() const { return fXPFactory; }
egdaniel378092f2014-12-03 10:40:13 -0800101
bungeman06ca8ec2016-06-09 08:01:03 -0700102 GrFragmentProcessor* getColorFragmentProcessor(int i) const {
103 return fColorFragmentProcessors[i].get();
bsalomonac856c92015-08-27 06:30:17 -0700104 }
bungeman06ca8ec2016-06-09 08:01:03 -0700105 GrFragmentProcessor* getCoverageFragmentProcessor(int i) const {
106 return fCoverageFragmentProcessors[i].get();
bsalomonac856c92015-08-27 06:30:17 -0700107 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000108
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000109 /**
cdalton1fa45722015-06-02 10:43:39 -0700110 * Returns true if the paint's output color will be constant after blending. If the result is
111 * true, constantColor will be updated to contain the constant color. Note that we can conflate
112 * coverage and color, so the actual values written to pixels with partial coverage may still
113 * not seem constant, even if this function returns true.
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000114 */
Brian Salomon94cce4c2017-02-21 16:32:20 -0500115 bool isConstantBlendedColor(GrColor* constantColor) const;
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000116
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400117 /**
118 * A trivial paint is one that uses src-over and has no fragment processors.
119 * It may have variable sRGB settings.
120 **/
121 bool isTrivial() const { return fTrivial; }
122
joshualitt5531d512014-12-17 15:50:11 -0800123private:
Brian Salomonb74ef032017-08-10 12:46:01 -0400124 // Since paint copying is expensive if there are fragment processors, we require going through
125 // the Clone() method.
126 GrPaint(const GrPaint&);
Brian Salomon82f44312017-01-11 13:42:54 -0500127 GrPaint& operator=(const GrPaint&) = delete;
128
Brian Salomon92ce5942017-01-18 11:01:10 -0500129 friend class GrProcessorSet;
Brian Salomon82f44312017-01-11 13:42:54 -0500130
Brian Salomon82f44312017-01-11 13:42:54 -0500131 const GrXPFactory* fXPFactory = nullptr;
Brian Salomonaff329b2017-08-11 09:40:37 -0400132 SkSTArray<4, std::unique_ptr<GrFragmentProcessor>> fColorFragmentProcessors;
133 SkSTArray<2, std::unique_ptr<GrFragmentProcessor>> fCoverageFragmentProcessors;
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400134 bool fTrivial = true;
Brian Salomon82f44312017-01-11 13:42:54 -0500135 GrColor4f fColor = GrColor4f::OpaqueWhite();
136};
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000137
bsalomon@google.com27847de2011-02-22 20:59:41 +0000138#endif