blob: 5e75c655383dd5913ff2a4cfa39bb4027f21d11f [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"
Robert Phillips2890fbf2017-07-26 15:48:41 -040018#include "GrRenderTargetProxy.h"
csmartdaltonbf4a8f92016-09-06 10:01:06 -070019#include "GrScissorState.h"
csmartdaltonc633abb2016-11-01 08:55:55 -070020#include "GrUserStencilSettings.h"
csmartdaltonbf4a8f92016-09-06 10:01:06 -070021#include "GrWindowRectsState.h"
egdanielb109ac22014-10-07 06:45:44 -070022#include "SkMatrix.h"
23#include "SkRefCnt.h"
robertphillips5fa7f302016-07-21 09:21:04 -070024#include "effects/GrCoverageSetOpXP.h"
25#include "effects/GrDisableColorXP.h"
26#include "effects/GrPorterDuffXferProcessor.h"
27#include "effects/GrSimpleTextureEffect.h"
28
Brian Salomon652ecb52017-01-17 12:39:53 -050029class GrAppliedClip;
Brian Salomon25a88092016-12-01 09:36:50 -050030class GrOp;
Brian Salomon25a88092016-12-01 09:36:50 -050031class GrRenderTargetContext;
egdaniel3658f382014-09-15 07:01:59 -070032
Brian Salomon92aee3d2016-12-21 09:20:25 -050033/**
Brian Salomone5b399e2017-07-19 13:50:54 -040034 * This immutable object contains information needed to set build a shader program and set API
35 * state for a draw. It is used along with a GrPrimitiveProcessor and a source of geometric
36 * data (GrMesh or GrPath) to draw.
egdaniel3658f382014-09-15 07:01:59 -070037 */
Brian Salomon16351462017-07-19 16:35:31 -040038class GrPipeline {
egdaniel3658f382014-09-15 07:01:59 -070039public:
bsalomoncb02b382015-08-12 11:14:50 -070040 ///////////////////////////////////////////////////////////////////////////
41 /// @name Creation
42
Brian Salomon189098e72017-01-19 09:55:19 -050043 enum Flags {
44 /**
45 * Perform HW anti-aliasing. This means either HW FSAA, if supported by the render target,
46 * or smooth-line rendering if a line primitive is drawn and line smoothing is supported by
47 * the 3D API.
48 */
49 kHWAntialias_Flag = 0x1,
Brian Salomon189098e72017-01-19 09:55:19 -050050 /**
51 * Modifies the vertex shader so that vertices will be positioned at pixel centers.
52 */
53 kSnapVerticesToPixelCenters_Flag = 0x2,
Brian Salomon189098e72017-01-19 09:55:19 -050054 };
55
Brian Salomonb5cb6832017-02-24 11:01:15 -050056 struct InitArgs {
Brian Salomon189098e72017-01-19 09:55:19 -050057 uint32_t fFlags = 0;
Brian Salomon189098e72017-01-19 09:55:19 -050058 const GrUserStencilSettings* fUserStencil = &GrUserStencilSettings::kUnused;
Robert Phillips2890fbf2017-07-26 15:48:41 -040059 GrRenderTargetProxy* fProxy = nullptr;
Brian Salomon189098e72017-01-19 09:55:19 -050060 const GrCaps* fCaps = nullptr;
Robert Phillips9bee2e52017-05-29 12:37:20 -040061 GrResourceProvider* fResourceProvider = nullptr;
Robert Phillipsbb581ce2017-05-29 15:05:15 -040062 GrXferProcessor::DstProxy fDstProxy;
bsalomona387a112015-08-11 14:47:42 -070063 };
64
Brian Salomonb5cb6832017-02-24 11:01:15 -050065 /**
Brian Salomon49348902018-06-26 09:12:38 -040066 * Some state can be changed between GrMeshes without changing GrPipelines. This is generally
67 * less expensive then using multiple pipelines. Such state is called "dynamic state". It can
68 * be specified in two ways:
69 * 1) FixedDynamicState - use this to specify state that does not vary between GrMeshes.
70 * 2) DynamicStateArrays - use this to specify per mesh values for dynamic state.
Chris Dalton46983b72017-06-06 12:27:16 -060071 **/
Brian Salomon49348902018-06-26 09:12:38 -040072 struct FixedDynamicState {
Brian Salomon7eae3e02018-08-07 14:02:38 +000073 explicit FixedDynamicState(const SkIRect& scissorRect) : fScissorRect(scissorRect) {}
74 FixedDynamicState() = default;
75 SkIRect fScissorRect = SkIRect::EmptyIRect();
Brian Salomonf7232642018-09-19 08:58:08 -040076 // Must have GrPrimitiveProcessor::numTextureSamplers() entries. Can be null if no samplers
77 // or textures are passed using DynamicStateArrays.
Brian Salomon7eae3e02018-08-07 14:02:38 +000078 GrTextureProxy** fPrimitiveProcessorTextures = nullptr;
Chris Dalton46983b72017-06-06 12:27:16 -060079 };
80
81 /**
Brian Salomon49348902018-06-26 09:12:38 -040082 * Any non-null array overrides the FixedDynamicState on a mesh-by-mesh basis. Arrays must
83 * have one entry for each GrMesh.
84 */
85 struct DynamicStateArrays {
86 const SkIRect* fScissorRects = nullptr;
Brian Salomonf7232642018-09-19 08:58:08 -040087 // Must have GrPrimitiveProcessor::numTextureSamplers() * num_meshes entries.
88 // Can be null if no samplers or to use the same textures for all meshes via'
89 // FixedDynamicState.
90 GrTextureProxy** fPrimitiveProcessorTextures = nullptr;
Brian Salomon49348902018-06-26 09:12:38 -040091 };
92
93 /**
csmartdalton119fb2b2017-02-08 14:41:05 -050094 * Creates a simple pipeline with default settings and no processors. The provided blend mode
Chris Dalton916c4982018-08-15 00:53:25 -060095 * must be "Porter Duff" (<= kLastCoeffMode). If using GrScissorTest::kEnabled, the caller must
Chris Dalton46983b72017-06-06 12:27:16 -060096 * specify a scissor rectangle through the DynamicState struct.
csmartdalton119fb2b2017-02-08 14:41:05 -050097 **/
Chris Dalton916c4982018-08-15 00:53:25 -060098 GrPipeline(GrRenderTargetProxy*, GrScissorTest, SkBlendMode);
csmartdalton119fb2b2017-02-08 14:41:05 -050099
Brian Salomonbfd18cd2017-08-09 16:27:09 -0400100 GrPipeline(const InitArgs&, GrProcessorSet&&, GrAppliedClip&&);
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400101
Brian Salomon16351462017-07-19 16:35:31 -0400102 GrPipeline(const GrPipeline&) = delete;
103 GrPipeline& operator=(const GrPipeline&) = delete;
104
egdaniel89af44a2014-09-26 06:15:04 -0700105 /// @}
106
107 ///////////////////////////////////////////////////////////////////////////
bsalomon6be6f7c2015-02-26 13:05:21 -0800108 /// @name GrFragmentProcessors
egdaniel89af44a2014-09-26 06:15:04 -0700109
Robert Phillipsd261e102017-06-23 12:37:20 -0400110 // Make the renderTargetContext's GrOpList be dependent on any GrOpLists in this pipeline
111 void addDependenciesTo(GrOpList* recipient, const GrCaps&) const;
bsalomon6be6f7c2015-02-26 13:05:21 -0800112
bsalomonac856c92015-08-27 06:30:17 -0700113 int numColorFragmentProcessors() const { return fNumColorProcessors; }
114 int numCoverageFragmentProcessors() const {
115 return fFragmentProcessors.count() - fNumColorProcessors;
116 }
117 int numFragmentProcessors() const { return fFragmentProcessors.count(); }
egdaniel89af44a2014-09-26 06:15:04 -0700118
bsalomon2047b782015-12-21 13:12:54 -0800119 const GrXferProcessor& getXferProcessor() const {
Brian Salomond61c9d92017-04-10 10:54:25 -0400120 if (fXferProcessor) {
bsalomon2047b782015-12-21 13:12:54 -0800121 return *fXferProcessor.get();
122 } else {
123 // A null xp member means the common src-over case. GrXferProcessor's ref'ing
124 // mechanism is not thread safe so we do not hold a ref on this global.
125 return GrPorterDuffXPFactory::SimpleSrcOverXP();
126 }
127 }
egdaniel378092f2014-12-03 10:40:13 -0800128
Brian Salomon18dfa982017-04-03 16:57:43 -0400129 /**
130 * If the GrXferProcessor uses a texture to access the dst color, then this returns that
131 * texture and the offset to the dst contents within that texture.
132 */
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400133 GrTextureProxy* dstTextureProxy(SkIPoint* offset = nullptr) const {
Brian Salomon18dfa982017-04-03 16:57:43 -0400134 if (offset) {
135 *offset = fDstTextureOffset;
136 }
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400137 return fDstTextureProxy.get();
138 }
139
140 GrTexture* peekDstTexture(SkIPoint* offset = nullptr) const {
141 if (GrTextureProxy* dstProxy = this->dstTextureProxy(offset)) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400142 return dstProxy->peekTexture();
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400143 }
144
145 return nullptr;
Brian Salomon18dfa982017-04-03 16:57:43 -0400146 }
147
bsalomonac856c92015-08-27 06:30:17 -0700148 const GrFragmentProcessor& getColorFragmentProcessor(int idx) const {
149 SkASSERT(idx < this->numColorFragmentProcessors());
150 return *fFragmentProcessors[idx].get();
egdanield9aa2182014-10-09 13:47:05 -0700151 }
bsalomonac856c92015-08-27 06:30:17 -0700152
153 const GrFragmentProcessor& getCoverageFragmentProcessor(int idx) const {
154 SkASSERT(idx < this->numCoverageFragmentProcessors());
155 return *fFragmentProcessors[fNumColorProcessors + idx].get();
egdanield9aa2182014-10-09 13:47:05 -0700156 }
bsalomonac856c92015-08-27 06:30:17 -0700157
158 const GrFragmentProcessor& getFragmentProcessor(int idx) const {
159 return *fFragmentProcessors[idx].get();
bsalomonae59b772014-11-19 08:23:49 -0800160 }
egdaniel89af44a2014-09-26 06:15:04 -0700161
162 /// @}
163
egdaniel89af44a2014-09-26 06:15:04 -0700164 /**
165 * Retrieves the currently set render-target.
166 *
167 * @return The currently set render target.
168 */
Robert Phillips2890fbf2017-07-26 15:48:41 -0400169 GrRenderTargetProxy* proxy() const { return fProxy.get(); }
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400170 GrRenderTarget* renderTarget() const { return fProxy.get()->peekRenderTarget(); }
egdaniel89af44a2014-09-26 06:15:04 -0700171
csmartdaltonc633abb2016-11-01 08:55:55 -0700172 const GrUserStencilSettings* getUserStencil() const { return fUserStencilSettings; }
egdaniel89af44a2014-09-26 06:15:04 -0700173
Chris Dalton916c4982018-08-15 00:53:25 -0600174 bool isScissorEnabled() const {
175 return SkToBool(fFlags & kScissorEnabled_Flag);
Brian Salomon49348902018-06-26 09:12:38 -0400176 }
joshualitt54e0c122014-11-19 09:38:51 -0800177
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700178 const GrWindowRectsState& getWindowRectsState() const { return fWindowRectsState; }
csmartdalton28341fa2016-08-17 10:00:21 -0700179
Brian Salomon189098e72017-01-19 09:55:19 -0500180 bool isHWAntialiasState() const { return SkToBool(fFlags & kHWAntialias_Flag); }
181 bool snapVerticesToPixelCenters() const {
182 return SkToBool(fFlags & kSnapVerticesToPixelCenters_Flag);
183 }
cdalton193d9cf2016-05-12 11:52:02 -0700184 bool hasStencilClip() const {
185 return SkToBool(fFlags & kHasStencilClip_Flag);
186 }
csmartdaltonc633abb2016-11-01 08:55:55 -0700187 bool isStencilEnabled() const {
188 return SkToBool(fFlags & kStencilEnabled_Flag);
189 }
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400190 bool isBad() const { return SkToBool(fFlags & kIsBad_Flag); }
bsalomonae59b772014-11-19 08:23:49 -0800191
Robert Phillipsc9c06d42017-06-12 10:58:31 -0400192 GrXferBarrierType xferBarrierType(const GrCaps& caps) const;
bsalomoncb02b382015-08-12 11:14:50 -0700193
Brian Salomon82dfd3d2017-06-14 12:30:35 -0400194 static SkString DumpFlags(uint32_t flags) {
195 if (flags) {
196 SkString result;
197 if (flags & GrPipeline::kSnapVerticesToPixelCenters_Flag) {
198 result.append("Snap vertices to pixel center.\n");
199 }
200 if (flags & GrPipeline::kHWAntialias_Flag) {
201 result.append("HW Antialiasing enabled.\n");
202 }
Brian Salomon82dfd3d2017-06-14 12:30:35 -0400203 return result;
204 }
205 return SkString("No pipeline flags\n");
206 }
207
bsalomonae59b772014-11-19 08:23:49 -0800208private:
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400209 void markAsBad() { fFlags |= kIsBad_Flag; }
210
Brian Salomonf87e2b92017-01-19 11:31:50 -0500211 /** This is a continuation of the public "Flags" enum. */
Brian Salomon189098e72017-01-19 09:55:19 -0500212 enum PrivateFlags {
Brian Salomone23bffd2017-06-02 11:01:10 -0400213 kHasStencilClip_Flag = 0x10,
214 kStencilEnabled_Flag = 0x20,
Brian Salomon49348902018-06-26 09:12:38 -0400215 kScissorEnabled_Flag = 0x40,
216 kIsBad_Flag = 0x80,
bsalomon04ddf892014-11-19 12:36:22 -0800217 };
218
Robert Phillips2890fbf2017-07-26 15:48:41 -0400219 using RenderTargetProxy = GrPendingIOResource<GrRenderTargetProxy, kWrite_GrIOType>;
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400220 using DstTextureProxy = GrPendingIOResource<GrTextureProxy, kRead_GrIOType>;
Brian Salomonaff329b2017-08-11 09:40:37 -0400221 using FragmentProcessorArray = SkAutoSTArray<8, std::unique_ptr<const GrFragmentProcessor>>;
Brian Salomon18dfa982017-04-03 16:57:43 -0400222
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400223 DstTextureProxy fDstTextureProxy;
Brian Salomon18dfa982017-04-03 16:57:43 -0400224 SkIPoint fDstTextureOffset;
Robert Phillips19e51dc2017-08-09 09:30:51 -0400225 // MDB TODO: do we still need the destination proxy here?
Robert Phillips2890fbf2017-07-26 15:48:41 -0400226 RenderTargetProxy fProxy;
Brian Salomon18dfa982017-04-03 16:57:43 -0400227 GrWindowRectsState fWindowRectsState;
228 const GrUserStencilSettings* fUserStencilSettings;
Brian Salomon18dfa982017-04-03 16:57:43 -0400229 uint16_t fFlags;
Brian Salomond61c9d92017-04-10 10:54:25 -0400230 sk_sp<const GrXferProcessor> fXferProcessor;
Brian Salomon18dfa982017-04-03 16:57:43 -0400231 FragmentProcessorArray fFragmentProcessors;
egdanield9aa2182014-10-09 13:47:05 -0700232
bsalomonac856c92015-08-27 06:30:17 -0700233 // This value is also the index in fFragmentProcessors where coverage processors begin.
Brian Salomon18dfa982017-04-03 16:57:43 -0400234 int fNumColorProcessors;
egdaniel3658f382014-09-15 07:01:59 -0700235};
236
237#endif