blob: 91f619e885bbf0f703c6794a913428c66b8afbe7 [file] [log] [blame]
egdaniel3658f382014-09-15 07:01:59 -07001/*
egdaniel8dd688b2015-01-22 10:16:09 -08002 * Copyright 2015 Google Inc.
egdaniel3658f382014-09-15 07:01:59 -07003 *
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 GrPipeline_DEFINED
9#define GrPipeline_DEFINED
egdaniel3658f382014-09-15 07:01:59 -070010
egdanielb109ac22014-10-07 06:45:44 -070011#include "GrColor.h"
bsalomonac856c92015-08-27 06:30:17 -070012#include "GrFragmentProcessor.h"
joshualittdbe1e6f2015-07-16 08:12:45 -070013#include "GrNonAtomicRef.h"
Brian Salomonae5f9532018-07-31 11:03:40 -040014#include "GrPendingIOResource.h"
Brian Salomon5298dc82017-02-22 11:52:03 -050015#include "GrProcessorSet.h"
joshualitt79f8fae2014-10-28 17:59:26 -070016#include "GrProgramDesc.h"
Brian Salomona4677b52017-05-04 12:39:56 -040017#include "GrRect.h"
csmartdaltonbf4a8f92016-09-06 10:01:06 -070018#include "GrScissorState.h"
csmartdaltonc633abb2016-11-01 08:55:55 -070019#include "GrUserStencilSettings.h"
csmartdaltonbf4a8f92016-09-06 10:01:06 -070020#include "GrWindowRectsState.h"
egdanielb109ac22014-10-07 06:45:44 -070021#include "SkMatrix.h"
22#include "SkRefCnt.h"
robertphillips5fa7f302016-07-21 09:21:04 -070023#include "effects/GrCoverageSetOpXP.h"
24#include "effects/GrDisableColorXP.h"
25#include "effects/GrPorterDuffXferProcessor.h"
26#include "effects/GrSimpleTextureEffect.h"
27
Brian Salomon652ecb52017-01-17 12:39:53 -050028class GrAppliedClip;
Brian Salomon25a88092016-12-01 09:36:50 -050029class GrOp;
Brian Salomon25a88092016-12-01 09:36:50 -050030class GrRenderTargetContext;
egdaniel3658f382014-09-15 07:01:59 -070031
Brian Salomon92aee3d2016-12-21 09:20:25 -050032/**
Brian Salomone5b399e2017-07-19 13:50:54 -040033 * This immutable object contains information needed to set build a shader program and set API
34 * state for a draw. It is used along with a GrPrimitiveProcessor and a source of geometric
35 * data (GrMesh or GrPath) to draw.
egdaniel3658f382014-09-15 07:01:59 -070036 */
Brian Salomon16351462017-07-19 16:35:31 -040037class GrPipeline {
egdaniel3658f382014-09-15 07:01:59 -070038public:
bsalomoncb02b382015-08-12 11:14:50 -070039 ///////////////////////////////////////////////////////////////////////////
40 /// @name Creation
41
Brian Salomon189098e72017-01-19 09:55:19 -050042 enum Flags {
43 /**
44 * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target,
45 * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by
46 * the 3D API.
47 */
48 kHWAntialias_Flag = 0x1,
Brian Salomon189098e72017-01-19 09:55:19 -050049 /**
50 * Modifies the vertex shader so that vertices will be positioned at pixel centers.
51 */
52 kSnapVerticesToPixelCenters_Flag = 0x2,
Brian Salomon189098e72017-01-19 09:55:19 -050053 };
54
Brian Salomonb5cb6832017-02-24 11:01:15 -050055 struct InitArgs {
Brian Salomon189098e72017-01-19 09:55:19 -050056 uint32_t fFlags = 0;
Brian Salomon189098e72017-01-19 09:55:19 -050057 const GrUserStencilSettings* fUserStencil = &GrUserStencilSettings::kUnused;
Brian Salomon189098e72017-01-19 09:55:19 -050058 const GrCaps* fCaps = nullptr;
Robert Phillips9bee2e52017-05-29 12:37:20 -040059 GrResourceProvider* fResourceProvider = nullptr;
Robert Phillipsbb581ce2017-05-29 15:05:15 -040060 GrXferProcessor::DstProxy fDstProxy;
bsalomona387a112015-08-11 14:47:42 -070061 };
62
Brian Salomonb5cb6832017-02-24 11:01:15 -050063 /**
Brian Salomon49348902018-06-26 09:12:38 -040064 * Some state can be changed between GrMeshes without changing GrPipelines. This is generally
65 * less expensive then using multiple pipelines. Such state is called "dynamic state". It can
66 * be specified in two ways:
67 * 1) FixedDynamicState - use this to specify state that does not vary between GrMeshes.
68 * 2) DynamicStateArrays - use this to specify per mesh values for dynamic state.
Chris Dalton46983b72017-06-06 12:27:16 -060069 **/
Brian Salomon49348902018-06-26 09:12:38 -040070 struct FixedDynamicState {
Brian Salomon7eae3e02018-08-07 14:02:38 +000071 explicit FixedDynamicState(const SkIRect& scissorRect) : fScissorRect(scissorRect) {}
72 FixedDynamicState() = default;
73 SkIRect fScissorRect = SkIRect::EmptyIRect();
Brian Salomonf7232642018-09-19 08:58:08 -040074 // Must have GrPrimitiveProcessor::numTextureSamplers() entries. Can be null if no samplers
75 // or textures are passed using DynamicStateArrays.
Brian Salomon7eae3e02018-08-07 14:02:38 +000076 GrTextureProxy** fPrimitiveProcessorTextures = nullptr;
Chris Dalton46983b72017-06-06 12:27:16 -060077 };
78
79 /**
Brian Salomon49348902018-06-26 09:12:38 -040080 * Any non-null array overrides the FixedDynamicState on a mesh-by-mesh basis. Arrays must
81 * have one entry for each GrMesh.
82 */
83 struct DynamicStateArrays {
84 const SkIRect* fScissorRects = nullptr;
Brian Salomonf7232642018-09-19 08:58:08 -040085 // Must have GrPrimitiveProcessor::numTextureSamplers() * num_meshes entries.
86 // Can be null if no samplers or to use the same textures for all meshes via'
87 // FixedDynamicState.
88 GrTextureProxy** fPrimitiveProcessorTextures = nullptr;
Brian Salomon49348902018-06-26 09:12:38 -040089 };
90
91 /**
csmartdalton119fb2b2017-02-08 14:41:05 -050092 * Creates a simple pipeline with default settings and no processors. The provided blend mode
Chris Dalton916c4982018-08-15 00:53:25 -060093 * must be "Porter Duff" (<= kLastCoeffMode). If using GrScissorTest::kEnabled, the caller must
Chris Dalton46983b72017-06-06 12:27:16 -060094 * specify a scissor rectangle through the DynamicState struct.
csmartdalton119fb2b2017-02-08 14:41:05 -050095 **/
Robert Phillipsd0fe8752019-01-31 14:13:59 -050096 GrPipeline(GrScissorTest, SkBlendMode);
csmartdalton119fb2b2017-02-08 14:41:05 -050097
Brian Salomonbfd18cd2017-08-09 16:27:09 -040098 GrPipeline(const InitArgs&, GrProcessorSet&&, GrAppliedClip&&);
Brian Salomon6d4b65e2017-05-03 17:06:09 -040099
Brian Salomon16351462017-07-19 16:35:31 -0400100 GrPipeline(const GrPipeline&) = delete;
101 GrPipeline& operator=(const GrPipeline&) = delete;
102
egdaniel89af44a2014-09-26 06:15:04 -0700103 /// @}
104
105 ///////////////////////////////////////////////////////////////////////////
bsalomon6be6f7c2015-02-26 13:05:21 -0800106 /// @name GrFragmentProcessors
egdaniel89af44a2014-09-26 06:15:04 -0700107
Robert Phillipsd261e102017-06-23 12:37:20 -0400108 // Make the renderTargetContext's GrOpList be dependent on any GrOpLists in this pipeline
109 void addDependenciesTo(GrOpList* recipient, const GrCaps&) const;
bsalomon6be6f7c2015-02-26 13:05:21 -0800110
bsalomonac856c92015-08-27 06:30:17 -0700111 int numColorFragmentProcessors() const { return fNumColorProcessors; }
112 int numCoverageFragmentProcessors() const {
113 return fFragmentProcessors.count() - fNumColorProcessors;
114 }
115 int numFragmentProcessors() const { return fFragmentProcessors.count(); }
egdaniel89af44a2014-09-26 06:15:04 -0700116
bsalomon2047b782015-12-21 13:12:54 -0800117 const GrXferProcessor& getXferProcessor() const {
Brian Salomond61c9d92017-04-10 10:54:25 -0400118 if (fXferProcessor) {
bsalomon2047b782015-12-21 13:12:54 -0800119 return *fXferProcessor.get();
120 } else {
121 // A null xp member means the common src-over case. GrXferProcessor's ref'ing
122 // mechanism is not thread safe so we do not hold a ref on this global.
123 return GrPorterDuffXPFactory::SimpleSrcOverXP();
124 }
125 }
egdaniel378092f2014-12-03 10:40:13 -0800126
Brian Salomon18dfa982017-04-03 16:57:43 -0400127 /**
128 * If the GrXferProcessor uses a texture to access the dst color, then this returns that
129 * texture and the offset to the dst contents within that texture.
130 */
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400131 GrTextureProxy* dstTextureProxy(SkIPoint* offset = nullptr) const {
Brian Salomon18dfa982017-04-03 16:57:43 -0400132 if (offset) {
133 *offset = fDstTextureOffset;
134 }
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400135 return fDstTextureProxy.get();
136 }
137
138 GrTexture* peekDstTexture(SkIPoint* offset = nullptr) const {
139 if (GrTextureProxy* dstProxy = this->dstTextureProxy(offset)) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400140 return dstProxy->peekTexture();
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400141 }
142
143 return nullptr;
Brian Salomon18dfa982017-04-03 16:57:43 -0400144 }
145
bsalomonac856c92015-08-27 06:30:17 -0700146 const GrFragmentProcessor& getColorFragmentProcessor(int idx) const {
147 SkASSERT(idx < this->numColorFragmentProcessors());
148 return *fFragmentProcessors[idx].get();
egdanield9aa2182014-10-09 13:47:05 -0700149 }
bsalomonac856c92015-08-27 06:30:17 -0700150
151 const GrFragmentProcessor& getCoverageFragmentProcessor(int idx) const {
152 SkASSERT(idx < this->numCoverageFragmentProcessors());
153 return *fFragmentProcessors[fNumColorProcessors + idx].get();
egdanield9aa2182014-10-09 13:47:05 -0700154 }
bsalomonac856c92015-08-27 06:30:17 -0700155
156 const GrFragmentProcessor& getFragmentProcessor(int idx) const {
157 return *fFragmentProcessors[idx].get();
bsalomonae59b772014-11-19 08:23:49 -0800158 }
egdaniel89af44a2014-09-26 06:15:04 -0700159
160 /// @}
161
csmartdaltonc633abb2016-11-01 08:55:55 -0700162 const GrUserStencilSettings* getUserStencil() const { return fUserStencilSettings; }
egdaniel89af44a2014-09-26 06:15:04 -0700163
Chris Dalton916c4982018-08-15 00:53:25 -0600164 bool isScissorEnabled() const {
165 return SkToBool(fFlags & kScissorEnabled_Flag);
Brian Salomon49348902018-06-26 09:12:38 -0400166 }
joshualitt54e0c122014-11-19 09:38:51 -0800167
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700168 const GrWindowRectsState& getWindowRectsState() const { return fWindowRectsState; }
csmartdalton28341fa2016-08-17 10:00:21 -0700169
Brian Salomon189098e72017-01-19 09:55:19 -0500170 bool isHWAntialiasState() const { return SkToBool(fFlags & kHWAntialias_Flag); }
171 bool snapVerticesToPixelCenters() const {
172 return SkToBool(fFlags & kSnapVerticesToPixelCenters_Flag);
173 }
cdalton193d9cf2016-05-12 11:52:02 -0700174 bool hasStencilClip() const {
175 return SkToBool(fFlags & kHasStencilClip_Flag);
176 }
csmartdaltonc633abb2016-11-01 08:55:55 -0700177 bool isStencilEnabled() const {
178 return SkToBool(fFlags & kStencilEnabled_Flag);
179 }
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400180 bool isBad() const { return SkToBool(fFlags & kIsBad_Flag); }
bsalomonae59b772014-11-19 08:23:49 -0800181
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500182 GrXferBarrierType xferBarrierType(GrTexture*, const GrCaps&) const;
bsalomoncb02b382015-08-12 11:14:50 -0700183
Brian Salomon82dfd3d2017-06-14 12:30:35 -0400184 static SkString DumpFlags(uint32_t flags) {
185 if (flags) {
186 SkString result;
187 if (flags & GrPipeline::kSnapVerticesToPixelCenters_Flag) {
188 result.append("Snap vertices to pixel center.\n");
189 }
190 if (flags & GrPipeline::kHWAntialias_Flag) {
191 result.append("HW Antialiasing enabled.\n");
192 }
Brian Salomon82dfd3d2017-06-14 12:30:35 -0400193 return result;
194 }
195 return SkString("No pipeline flags\n");
196 }
197
bsalomonae59b772014-11-19 08:23:49 -0800198private:
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400199 void markAsBad() { fFlags |= kIsBad_Flag; }
200
Brian Salomonf87e2b92017-01-19 11:31:50 -0500201 /** This is a continuation of the public "Flags" enum. */
Brian Salomon189098e72017-01-19 09:55:19 -0500202 enum PrivateFlags {
Brian Salomone23bffd2017-06-02 11:01:10 -0400203 kHasStencilClip_Flag = 0x10,
204 kStencilEnabled_Flag = 0x20,
Brian Salomon49348902018-06-26 09:12:38 -0400205 kScissorEnabled_Flag = 0x40,
206 kIsBad_Flag = 0x80,
bsalomon04ddf892014-11-19 12:36:22 -0800207 };
208
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400209 using DstTextureProxy = GrPendingIOResource<GrTextureProxy, kRead_GrIOType>;
Brian Salomonaff329b2017-08-11 09:40:37 -0400210 using FragmentProcessorArray = SkAutoSTArray<8, std::unique_ptr<const GrFragmentProcessor>>;
Brian Salomon18dfa982017-04-03 16:57:43 -0400211
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400212 DstTextureProxy fDstTextureProxy;
Brian Salomon18dfa982017-04-03 16:57:43 -0400213 SkIPoint fDstTextureOffset;
Brian Salomon18dfa982017-04-03 16:57:43 -0400214 GrWindowRectsState fWindowRectsState;
215 const GrUserStencilSettings* fUserStencilSettings;
Brian Salomon18dfa982017-04-03 16:57:43 -0400216 uint16_t fFlags;
Brian Salomond61c9d92017-04-10 10:54:25 -0400217 sk_sp<const GrXferProcessor> fXferProcessor;
Brian Salomon18dfa982017-04-03 16:57:43 -0400218 FragmentProcessorArray fFragmentProcessors;
egdanield9aa2182014-10-09 13:47:05 -0700219
bsalomonac856c92015-08-27 06:30:17 -0700220 // This value is also the index in fFragmentProcessors where coverage processors begin.
Brian Salomon18dfa982017-04-03 16:57:43 -0400221 int fNumColorProcessors;
egdaniel3658f382014-09-15 07:01:59 -0700222};
223
224#endif