blob: 22951f94a2d71319a25f8137edbb0b01d16d31ab [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"
bsalomonae59b772014-11-19 08:23:49 -080014#include "GrFragmentStage.h"
joshualitt9cc17752015-07-09 06:28:14 -070015#include "GrProcessorDataManager.h"
egdaniel378092f2014-12-03 10:40:13 -080016#include "GrXferProcessor.h"
egdaniel95131432014-12-09 11:15:43 -080017#include "effects/GrPorterDuffXferProcessor.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000018
egdanielb197b8f2015-02-17 07:34:43 -080019#include "SkRegion.h"
Scroggo97c88c22011-05-11 14:05:25 +000020#include "SkXfermode.h"
21
bsalomon@google.com27847de2011-02-22 20:59:41 +000022/**
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000023 * The paint describes how color and coverage are computed at each pixel by GrContext draw
24 * functions and the how color is blended with the destination pixel.
25 *
26 * The paint allows installation of custom color and coverage stages. New types of stages are
joshualittb0a8a372014-09-23 09:50:21 -070027 * created by subclassing GrProcessor.
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000028 *
29 * The primitive color computation starts with the color specified by setColor(). This color is the
bsalomon37f9a262015-02-02 13:00:10 -080030 * 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 +000031 *
32 * Fractional pixel coverage follows a similar flow. The coverage is initially the value specified
33 * by setCoverage(). This is input to the first coverage stage. Coverage stages are chained
34 * together in the same manner as color stages. The output of the last stage is modulated by any
35 * fractional coverage produced by anti-aliasing. This last step produces the final coverage, C.
36 *
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:
joshualitt2fdeda02015-01-22 07:11:44 -080042 GrPaint();
bsalomon@google.com27847de2011-02-22 20:59:41 +000043
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000044 GrPaint(const GrPaint& paint) { *this = paint; }
bsalomon@google.com27847de2011-02-22 20:59:41 +000045
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000046 ~GrPaint() {}
Scroggo97c88c22011-05-11 14:05:25 +000047
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000048 /**
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000049 * The initial color of the drawn primitive. Defaults to solid white.
50 */
51 void setColor(GrColor color) { fColor = color; }
52 GrColor getColor() const { return fColor; }
53
54 /**
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000055 * Should primitives be anti-aliased or not. Defaults to false.
56 */
57 void setAntiAlias(bool aa) { fAntiAlias = aa; }
58 bool isAntiAlias() const { return fAntiAlias; }
59
60 /**
61 * Should dithering be applied. Defaults to false.
62 */
63 void setDither(bool dither) { fDither = dither; }
64 bool isDither() const { return fDither; }
65
egdaniel378092f2014-12-03 10:40:13 -080066 const GrXPFactory* setXPFactory(const GrXPFactory* xpFactory) {
67 fXPFactory.reset(SkRef(xpFactory));
68 return xpFactory;
69 }
70
egdaniel95131432014-12-09 11:15:43 -080071 void setPorterDuffXPFactory(SkXfermode::Mode mode) {
72 fXPFactory.reset(GrPorterDuffXPFactory::Create(mode));
73 }
74
egdanielb197b8f2015-02-17 07:34:43 -080075 void setCoverageSetOpXPFactory(SkRegion::Op regionOp, bool invertCoverage = false);
egdaniel95131432014-12-09 11:15:43 -080076
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000077 /**
joshualittb0a8a372014-09-23 09:50:21 -070078 * Appends an additional color processor to the color computation.
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000079 */
joshualittb0a8a372014-09-23 09:50:21 -070080 const GrFragmentProcessor* addColorProcessor(const GrFragmentProcessor* fp) {
81 SkASSERT(fp);
joshualitt47bb3822014-10-07 16:43:25 -070082 SkNEW_APPEND_TO_TARRAY(&fColorStages, GrFragmentStage, (fp));
joshualittb0a8a372014-09-23 09:50:21 -070083 return fp;
tomhudson@google.comf13f5882012-06-25 17:27:28 +000084 }
85
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000086 /**
joshualittb0a8a372014-09-23 09:50:21 -070087 * Appends an additional coverage processor to the coverage computation.
bsalomon@google.comc7448ce2012-10-05 19:04:13 +000088 */
joshualittb0a8a372014-09-23 09:50:21 -070089 const GrFragmentProcessor* addCoverageProcessor(const GrFragmentProcessor* fp) {
90 SkASSERT(fp);
joshualitt47bb3822014-10-07 16:43:25 -070091 SkNEW_APPEND_TO_TARRAY(&fCoverageStages, GrFragmentStage, (fp));
joshualittb0a8a372014-09-23 09:50:21 -070092 return fp;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000093 }
94
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +000095 /**
96 * Helpers for adding color or coverage effects that sample a texture. The matrix is applied
97 * to the src space position to compute texture coordinates.
98 */
joshualittb0a8a372014-09-23 09:50:21 -070099 void addColorTextureProcessor(GrTexture*, const SkMatrix&);
100 void addCoverageTextureProcessor(GrTexture*, const SkMatrix&);
101 void addColorTextureProcessor(GrTexture*, const SkMatrix&, const GrTextureParams&);
102 void addCoverageTextureProcessor(GrTexture*, const SkMatrix&, const GrTextureParams&);
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000103
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000104 int numColorStages() const { return fColorStages.count(); }
105 int numCoverageStages() const { return fCoverageStages.count(); }
106 int numTotalStages() const { return this->numColorStages() + this->numCoverageStages(); }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000107
joshualitt2fdeda02015-01-22 07:11:44 -0800108 const GrXPFactory* getXPFactory() const {
109 if (!fXPFactory) {
110 fXPFactory.reset(GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode));
111 }
112 return fXPFactory.get();
113 }
egdaniel378092f2014-12-03 10:40:13 -0800114
joshualittb0a8a372014-09-23 09:50:21 -0700115 const GrFragmentStage& getColorStage(int s) const { return fColorStages[s]; }
116 const GrFragmentStage& getCoverageStage(int s) const { return fCoverageStages[s]; }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000117
bsalomon@google.com27c9b6d2011-09-12 14:30:27 +0000118 GrPaint& operator=(const GrPaint& paint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000119 fAntiAlias = paint.fAntiAlias;
120 fDither = paint.fDither;
121
122 fColor = paint.fColor;
123
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000124 fColorStages = paint.fColorStages;
125 fCoverageStages = paint.fCoverageStages;
126
egdaniel378092f2014-12-03 10:40:13 -0800127 fXPFactory.reset(SkRef(paint.getXPFactory()));
joshualitt5b4f05f2015-07-10 07:26:21 -0700128 fProcDataManager.reset(SkNEW_ARGS(GrProcessorDataManager, (*paint.processorDataManager())));
egdaniel378092f2014-12-03 10:40:13 -0800129
bsalomon@google.com27c9b6d2011-09-12 14:30:27 +0000130 return *this;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000131 }
132
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000133 /**
cdalton1fa45722015-06-02 10:43:39 -0700134 * Returns true if the paint's output color will be constant after blending. If the result is
135 * true, constantColor will be updated to contain the constant color. Note that we can conflate
136 * coverage and color, so the actual values written to pixels with partial coverage may still
137 * not seem constant, even if this function returns true.
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000138 */
cdalton1fa45722015-06-02 10:43:39 -0700139 bool isConstantBlendedColor(GrColor* constantColor) const;
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000140
joshualitt5b4f05f2015-07-10 07:26:21 -0700141 GrProcessorDataManager* getProcessorDataManager() { return fProcDataManager.get(); }
142
143 const GrProcessorDataManager* processorDataManager() const { return fProcDataManager.get(); }
joshualitt8ca93e72015-07-08 06:51:43 -0700144
joshualitt5531d512014-12-17 15:50:11 -0800145private:
joshualitt2fdeda02015-01-22 07:11:44 -0800146 mutable SkAutoTUnref<const GrXPFactory> fXPFactory;
joshualitt5b4f05f2015-07-10 07:26:21 -0700147 SkSTArray<4, GrFragmentStage> fColorStages;
148 SkSTArray<2, GrFragmentStage> fCoverageStages;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000149
joshualitt5b4f05f2015-07-10 07:26:21 -0700150 bool fAntiAlias;
151 bool fDither;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000152
joshualitt5b4f05f2015-07-10 07:26:21 -0700153 GrColor fColor;
154 SkAutoTUnref<GrProcessorDataManager> fProcDataManager;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000155};
156
157#endif