blob: a7556eb59d5177e9948b52e4c3c1b0063dcc300e [file] [log] [blame]
tomhudson@google.com93813632011-10-27 20:21:16 +00001/*
egdaniel8dd688b2015-01-22 10:16:09 -08002 * Copyright 2015 Google Inc.
tomhudson@google.com93813632011-10-27 20:21:16 +00003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
egdaniel8dd688b2015-01-22 10:16:09 -08008#ifndef GrPipelineBuilder_DEFINED
9#define GrPipelineBuilder_DEFINED
tomhudson@google.com93813632011-10-27 20:21:16 +000010
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000011#include "GrBlend.h"
bsalomoneb1cb5c2015-05-22 08:01:09 -070012#include "GrCaps.h"
bsalomonf96ba022014-09-17 08:05:40 -070013#include "GrGpuResourceRef.h"
egdanielb6cbc382014-11-13 11:00:34 -080014#include "GrProcOptInfo.h"
egdaniel89af44a2014-09-26 06:15:04 -070015#include "GrRenderTarget.h"
cdalton93a379b2016-05-11 13:58:08 -070016#include "GrUserStencilSettings.h"
egdaniel95131432014-12-09 11:15:43 -080017#include "GrXferProcessor.h"
egdaniel89af44a2014-09-26 06:15:04 -070018#include "SkMatrix.h"
bungeman06ca8ec2016-06-09 08:01:03 -070019#include "SkRefCnt.h"
egdaniel87509242014-12-17 13:37:13 -080020#include "effects/GrCoverageSetOpXP.h"
egdaniel080e6732014-12-22 07:35:52 -080021#include "effects/GrDisableColorXP.h"
egdaniel95131432014-12-09 11:15:43 -080022#include "effects/GrPorterDuffXferProcessor.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000023#include "effects/GrSimpleTextureEffect.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000024
bsalomonabd30f52015-08-13 13:34:48 -070025class GrDrawBatch;
bsalomon4b91f762015-05-19 09:29:46 -070026class GrCaps;
egdaniel89af44a2014-09-26 06:15:04 -070027class GrPaint;
28class GrTexture;
egdaniel170f90b2014-09-16 12:54:40 -070029
bsalomonac856c92015-08-27 06:30:17 -070030class GrPipelineBuilder : public SkNoncopyable {
bsalomon@google.com2e3d1442012-03-26 20:33:54 +000031public:
egdaniel8dd688b2015-01-22 10:16:09 -080032 GrPipelineBuilder();
33
bsalomon@google.com52a5dcb2012-01-17 16:01:37 +000034 /**
robertphillips55fdccc2016-06-06 06:16:20 -070035 * Initializes the GrPipelineBuilder based on a GrPaint and MSAA availability. Note
egdaniel8dd688b2015-01-22 10:16:09 -080036 * that GrPipelineBuilder encompasses more than GrPaint. Aspects of GrPipelineBuilder that have
37 * no GrPaint equivalents are set to default values with the exception of vertex attribute state
38 * which is unmodified by this function and clipping which will be enabled.
bsalomon@google.comaf84e742012-10-05 13:23:24 +000039 */
csmartdaltonecbc12b2016-06-08 10:08:43 -070040 GrPipelineBuilder(const GrPaint&, bool useHWAA = false);
joshualitt7b670db2015-07-09 13:25:02 -070041
42 virtual ~GrPipelineBuilder();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000043
bsalomon@google.com2401ae82012-01-17 21:03:05 +000044 ///////////////////////////////////////////////////////////////////////////
bsalomon6be6f7c2015-02-26 13:05:21 -080045 /// @name Fragment Processors
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000046 ///
bsalomon6be6f7c2015-02-26 13:05:21 -080047 /// GrFragmentProcessors are used to compute per-pixel color and per-pixel fractional coverage.
48 /// There are two chains of FPs, one for color and one for coverage. The first FP in each
49 /// chain gets the initial color/coverage from the GrPrimitiveProcessor. It computes an output
50 /// color/coverage which is fed to the next FP in the chain. The last color and coverage FPs
51 /// feed their output to the GrXferProcessor which controls blending.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000052 ////
53
bsalomonac856c92015-08-27 06:30:17 -070054 int numColorFragmentProcessors() const { return fColorFragmentProcessors.count(); }
55 int numCoverageFragmentProcessors() const { return fCoverageFragmentProcessors.count(); }
56 int numFragmentProcessors() const { return this->numColorFragmentProcessors() +
57 this->numCoverageFragmentProcessors(); }
egdaniel378092f2014-12-03 10:40:13 -080058
bsalomonac856c92015-08-27 06:30:17 -070059 const GrFragmentProcessor* getColorFragmentProcessor(int idx) const {
bungeman06ca8ec2016-06-09 08:01:03 -070060 return fColorFragmentProcessors[idx].get();
bsalomonac856c92015-08-27 06:30:17 -070061 }
62 const GrFragmentProcessor* getCoverageFragmentProcessor(int idx) const {
bungeman06ca8ec2016-06-09 08:01:03 -070063 return fCoverageFragmentProcessors[idx].get();
jvanverth@google.com65eb4d52013-03-19 18:51:02 +000064 }
skia.committer@gmail.com01c34ee2013-03-20 07:01:02 +000065
bungeman06ca8ec2016-06-09 08:01:03 -070066 void addColorFragmentProcessor(sk_sp<GrFragmentProcessor> processor) {
bsalomonac856c92015-08-27 06:30:17 -070067 SkASSERT(processor);
bungeman06ca8ec2016-06-09 08:01:03 -070068 fColorFragmentProcessors.push_back(std::move(processor));
bsalomonac856c92015-08-27 06:30:17 -070069 }
70
bungeman06ca8ec2016-06-09 08:01:03 -070071 void addCoverageFragmentProcessor(sk_sp<GrFragmentProcessor> processor) {
bsalomonac856c92015-08-27 06:30:17 -070072 SkASSERT(processor);
bungeman06ca8ec2016-06-09 08:01:03 -070073 fCoverageFragmentProcessors.push_back(std::move(processor));
bsalomon@google.comadc65362013-01-28 14:26:09 +000074 }
75
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000076 /**
bsalomon@google.comc7818882013-03-20 19:19:53 +000077 * Creates a GrSimpleTextureEffect that uses local coords as texture coordinates.
tomhudson@google.com1e8f0162012-07-20 16:25:18 +000078 */
joshualittb0a8a372014-09-23 09:50:21 -070079 void addColorTextureProcessor(GrTexture* texture, const SkMatrix& matrix) {
bungeman06ca8ec2016-06-09 08:01:03 -070080 this->addColorFragmentProcessor(GrSimpleTextureEffect::Make(texture, matrix));
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +000081 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000082
joshualittb0a8a372014-09-23 09:50:21 -070083 void addCoverageTextureProcessor(GrTexture* texture, const SkMatrix& matrix) {
bungeman06ca8ec2016-06-09 08:01:03 -070084 this->addCoverageFragmentProcessor(GrSimpleTextureEffect::Make(texture, matrix));
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000085 }
86
joshualittb0a8a372014-09-23 09:50:21 -070087 void addColorTextureProcessor(GrTexture* texture,
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000088 const SkMatrix& matrix,
89 const GrTextureParams& params) {
bungeman06ca8ec2016-06-09 08:01:03 -070090 this->addColorFragmentProcessor(GrSimpleTextureEffect::Make(texture, matrix, params));
joshualittb0a8a372014-09-23 09:50:21 -070091 }
92
93 void addCoverageTextureProcessor(GrTexture* texture,
94 const SkMatrix& matrix,
95 const GrTextureParams& params) {
bungeman06ca8ec2016-06-09 08:01:03 -070096 this->addCoverageFragmentProcessor(GrSimpleTextureEffect::Make(texture, matrix, params));
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +000097 }
tomhudson@google.com676e6602012-07-10 17:21:48 +000098
robertphillips@google.com972265d2012-06-13 18:49:30 +000099 /**
bsalomon6be6f7c2015-02-26 13:05:21 -0800100 * When this object is destroyed it will remove any color/coverage FPs from the pipeline builder
bsalomon4a339522015-10-06 08:40:50 -0700101 * that were added after its constructor.
joshualitt5e6ba212015-07-13 07:35:05 -0700102 * This class can transiently modify its "const" GrPipelineBuilder object but will restore it
103 * when done - so it is notionally "const" correct.
robertphillips@google.com972265d2012-06-13 18:49:30 +0000104 */
joshualitt4421a4c2015-07-13 09:36:41 -0700105 class AutoRestoreFragmentProcessorState : public ::SkNoncopyable {
robertphillips@google.com972265d2012-06-13 18:49:30 +0000106 public:
halcanary9d524f22016-03-29 09:03:52 -0700107 AutoRestoreFragmentProcessorState()
halcanary96fcdcc2015-08-27 07:41:13 -0700108 : fPipelineBuilder(nullptr)
bsalomon9b536522014-09-05 09:18:51 -0700109 , fColorEffectCnt(0)
joshualittaf2533a2015-09-09 10:00:12 -0700110 , fCoverageEffectCnt(0) {}
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000111
joshualitt4421a4c2015-07-13 09:36:41 -0700112 AutoRestoreFragmentProcessorState(const GrPipelineBuilder& ds)
halcanary96fcdcc2015-08-27 07:41:13 -0700113 : fPipelineBuilder(nullptr)
bsalomon9b536522014-09-05 09:18:51 -0700114 , fColorEffectCnt(0)
joshualittaf2533a2015-09-09 10:00:12 -0700115 , fCoverageEffectCnt(0) {
joshualitt4421a4c2015-07-13 09:36:41 -0700116 this->set(&ds);
robertphillips@google.comf09b87d2013-06-13 20:06:44 +0000117 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000118
halcanary96fcdcc2015-08-27 07:41:13 -0700119 ~AutoRestoreFragmentProcessorState() { this->set(nullptr); }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000120
joshualitt5e6ba212015-07-13 07:35:05 -0700121 void set(const GrPipelineBuilder* ds);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000122
egdaniel8dd688b2015-01-22 10:16:09 -0800123 bool isSet() const { return SkToBool(fPipelineBuilder); }
bsalomon8af05232014-06-03 06:34:58 -0700124
bungeman06ca8ec2016-06-09 08:01:03 -0700125 void addCoverageFragmentProcessor(sk_sp<GrFragmentProcessor> processor) {
joshualitt5e6ba212015-07-13 07:35:05 -0700126 SkASSERT(this->isSet());
bungeman06ca8ec2016-06-09 08:01:03 -0700127 return fPipelineBuilder->addCoverageFragmentProcessor(std::move(processor));
joshualitt5e6ba212015-07-13 07:35:05 -0700128 }
129
robertphillips@google.com972265d2012-06-13 18:49:30 +0000130 private:
joshualitt5e6ba212015-07-13 07:35:05 -0700131 // notionally const (as marginalia)
egdaniel8dd688b2015-01-22 10:16:09 -0800132 GrPipelineBuilder* fPipelineBuilder;
joshualitt5e6ba212015-07-13 07:35:05 -0700133 int fColorEffectCnt;
134 int fCoverageEffectCnt;
robertphillips@google.com972265d2012-06-13 18:49:30 +0000135 };
136
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000137 /// @}
138
139 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000140 /// @name Blending
141 ////
142
egdaniel89af44a2014-09-26 06:15:04 -0700143 /**
bsalomon6be6f7c2015-02-26 13:05:21 -0800144 * Installs a GrXPFactory. This object controls how src color, fractional pixel coverage,
145 * and the dst color are blended.
146 */
bungeman06ca8ec2016-06-09 08:01:03 -0700147 void setXPFactory(sk_sp<GrXPFactory> xpFactory) {
148 fXPFactory = std::move(xpFactory);
bsalomon6be6f7c2015-02-26 13:05:21 -0800149 }
150
151 /**
bsalomon6be6f7c2015-02-26 13:05:21 -0800152 * Sets a GrXPFactory that disables color writes to the destination. This is useful when
153 * rendering to the stencil buffer.
154 */
155 void setDisableColorXPFactory() {
bungeman06ca8ec2016-06-09 08:01:03 -0700156 fXPFactory = GrDisableColorXPFactory::Make();
bsalomon6be6f7c2015-02-26 13:05:21 -0800157 }
158
159 const GrXPFactory* getXPFactory() const {
bungeman06ca8ec2016-06-09 08:01:03 -0700160 return fXPFactory.get();
bsalomon6be6f7c2015-02-26 13:05:21 -0800161 }
162
163 /**
bsalomon6a44c6a2015-05-26 09:49:05 -0700164 * Checks whether the xp will need destination in a texture to correctly blend.
bsalomon6be6f7c2015-02-26 13:05:21 -0800165 */
halcanary9d524f22016-03-29 09:03:52 -0700166 bool willXPNeedDstTexture(const GrCaps& caps,
ethannicholasde4166a2015-11-30 08:57:38 -0800167 const GrPipelineOptimizations& optimizations) const;
joshualittd27f73e2014-12-29 07:43:36 -0800168
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000169 /// @}
170
bsalomon6be6f7c2015-02-26 13:05:21 -0800171
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000172 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000173 /// @name Stencil
174 ////
175
robertphillips976f5f02016-06-03 10:59:20 -0700176 bool hasUserStencilSettings() const { return !fUserStencilSettings->isUnused(); }
cdalton93a379b2016-05-11 13:58:08 -0700177 const GrUserStencilSettings* getUserStencil() const { return fUserStencilSettings; }
egdaniel89af44a2014-09-26 06:15:04 -0700178
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000179 /**
cdalton93a379b2016-05-11 13:58:08 -0700180 * Sets the user stencil settings for the next draw.
181 * This class only stores pointers to stencil settings objects.
182 * The caller guarantees the pointer will remain valid until it
183 * changes or goes out of scope.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000184 * @param settings the stencil settings to use.
185 */
cdalton93a379b2016-05-11 13:58:08 -0700186 void setUserStencil(const GrUserStencilSettings* settings) { fUserStencilSettings = settings; }
187 void disableUserStencil() { fUserStencilSettings = &GrUserStencilSettings::kUnused; }
bsalomon6be6f7c2015-02-26 13:05:21 -0800188
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000189 /// @}
190
191 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000192 /// @name State Flags
193 ////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000194
egdaniel89af44a2014-09-26 06:15:04 -0700195 /**
196 * Flags that affect rendering. Controlled using enable/disableState(). All
197 * default to disabled.
198 */
bsalomond79c5492015-04-27 10:07:04 -0700199 enum Flags {
egdaniel89af44a2014-09-26 06:15:04 -0700200 /**
egdaniel89af44a2014-09-26 06:15:04 -0700201 * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target,
202 * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by
203 * the 3D API.
204 */
bsalomonaca31fe2015-09-22 11:38:46 -0700205 kHWAntialias_Flag = 0x01,
egdaniel89af44a2014-09-26 06:15:04 -0700206
bsalomond79c5492015-04-27 10:07:04 -0700207 /**
208 * Modifies the vertex shader so that vertices will be positioned at pixel centers.
209 */
bsalomonaca31fe2015-09-22 11:38:46 -0700210 kSnapVerticesToPixelCenters_Flag = 0x02,
bsalomond79c5492015-04-27 10:07:04 -0700211
brianosman64d094d2016-03-25 06:01:59 -0700212 /**
213 * Suppress linear -> sRGB conversion when rendering to sRGB render targets.
214 */
215 kDisableOutputConversionToSRGB_Flag = 0x04,
216
brianosman898235c2016-04-06 07:38:23 -0700217 /**
218 * Allow sRGB -> linear conversion when reading from sRGB inputs.
219 */
220 kAllowSRGBInputs_Flag = 0x08,
221
222 kLast_Flag = kAllowSRGBInputs_Flag,
egdaniel89af44a2014-09-26 06:15:04 -0700223 };
224
bsalomond79c5492015-04-27 10:07:04 -0700225 bool isHWAntialias() const { return SkToBool(fFlags & kHWAntialias_Flag); }
226 bool snapVerticesToPixelCenters() const {
227 return SkToBool(fFlags & kSnapVerticesToPixelCenters_Flag); }
brianosman64d094d2016-03-25 06:01:59 -0700228 bool getDisableOutputConversionToSRGB() const {
229 return SkToBool(fFlags & kDisableOutputConversionToSRGB_Flag); }
brianosman898235c2016-04-06 07:38:23 -0700230 bool getAllowSRGBInputs() const {
231 return SkToBool(fFlags & kAllowSRGBInputs_Flag); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000232
233 /**
234 * Enable render state settings.
235 *
bsalomond79c5492015-04-27 10:07:04 -0700236 * @param flags bitfield of Flags specifying the states to enable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000237 */
bsalomond79c5492015-04-27 10:07:04 -0700238 void enableState(uint32_t flags) { fFlags |= flags; }
halcanary9d524f22016-03-29 09:03:52 -0700239
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000240 /**
241 * Disable render state settings.
242 *
bsalomond79c5492015-04-27 10:07:04 -0700243 * @param flags bitfield of Flags specifying the states to disable
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000244 */
bsalomond79c5492015-04-27 10:07:04 -0700245 void disableState(uint32_t flags) { fFlags &= ~(flags); }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000246
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000247 /**
bsalomond79c5492015-04-27 10:07:04 -0700248 * Enable or disable flags based on a boolean.
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000249 *
bsalomond79c5492015-04-27 10:07:04 -0700250 * @param flags bitfield of Flags to enable or disable
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000251 * @param enable if true enable stateBits, otherwise disable
252 */
bsalomond79c5492015-04-27 10:07:04 -0700253 void setState(uint32_t flags, bool enable) {
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000254 if (enable) {
bsalomond79c5492015-04-27 10:07:04 -0700255 this->enableState(flags);
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000256 } else {
bsalomond79c5492015-04-27 10:07:04 -0700257 this->disableState(flags);
bsalomon@google.comd5d69ff2012-10-04 19:42:00 +0000258 }
259 }
260
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000261 /// @}
262
263 ///////////////////////////////////////////////////////////////////////////
264 /// @name Face Culling
265 ////
266
egdaniel89af44a2014-09-26 06:15:04 -0700267 enum DrawFace {
268 kInvalid_DrawFace = -1,
269
270 kBoth_DrawFace,
271 kCCW_DrawFace,
272 kCW_DrawFace,
273 };
274
275 /**
276 * Gets whether the target is drawing clockwise, counterclockwise,
277 * or both faces.
278 * @return the current draw face(s).
279 */
280 DrawFace getDrawFace() const { return fDrawFace; }
281
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000282 /**
283 * Controls whether clockwise, counterclockwise, or both faces are drawn.
284 * @param face the face(s) to draw.
285 */
286 void setDrawFace(DrawFace face) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000287 SkASSERT(kInvalid_DrawFace != face);
bsalomon2ed5ef82014-07-07 08:44:05 -0700288 fDrawFace = face;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000289 }
290
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000291 /// @}
292
293 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.com62b09682011-11-09 16:39:17 +0000294
ethannicholasff210322015-11-24 12:10:10 -0800295 bool usePLSDstRead(const GrDrawBatch* batch) const;
joshualitt44701df2015-02-23 14:44:57 -0800296
egdaniele36914c2015-02-13 09:00:33 -0800297private:
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000298 // Some of the auto restore objects assume that no effects are removed during their lifetime.
299 // This is used to assert that this condition holds.
joshualitt5e6ba212015-07-13 07:35:05 -0700300 SkDEBUGCODE(mutable int fBlockEffectRemovalCnt;)
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000301
bungeman06ca8ec2016-06-09 08:01:03 -0700302 typedef SkSTArray<4, sk_sp<GrFragmentProcessor>> FragmentProcessorArray;
egdaniel89af44a2014-09-26 06:15:04 -0700303
bsalomond79c5492015-04-27 10:07:04 -0700304 uint32_t fFlags;
cdalton93a379b2016-05-11 13:58:08 -0700305 const GrUserStencilSettings* fUserStencilSettings;
bsalomonae59b772014-11-19 08:23:49 -0800306 DrawFace fDrawFace;
bungeman06ca8ec2016-06-09 08:01:03 -0700307 mutable sk_sp<GrXPFactory> fXPFactory;
bsalomonac856c92015-08-27 06:30:17 -0700308 FragmentProcessorArray fColorFragmentProcessors;
309 FragmentProcessorArray fCoverageFragmentProcessors;
egdaniel89af44a2014-09-26 06:15:04 -0700310
egdaniel8dd688b2015-01-22 10:16:09 -0800311 friend class GrPipeline;
ethannicholasff210322015-11-24 12:10:10 -0800312 friend class GrDrawTarget;
tomhudson@google.com93813632011-10-27 20:21:16 +0000313};
314
315#endif