blob: 8b30ab9b6001dc5d291163f09d1559a6f02dea55 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2010 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
reed@google.comac10a2d2010-12-22 21:39:39 +00008#ifndef GrDrawTarget_DEFINED
9#define GrDrawTarget_DEFINED
10
joshualitt44701df2015-02-23 14:44:57 -080011#include "GrClip.h"
joshualitt6db519c2014-10-29 08:48:18 -070012#include "GrClipMaskManager.h"
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +000013#include "GrContext.h"
joshualitt8072caa2015-02-12 14:20:52 -080014#include "GrPathProcessor.h"
15#include "GrPrimitiveProcessor.h"
bsalomon@google.com934c5702012-03-20 21:17:58 +000016#include "GrIndexBuffer.h"
kkinnunenccdaa042014-08-20 01:36:23 -070017#include "GrPathRendering.h"
egdaniel8dd688b2015-01-22 10:16:09 -080018#include "GrPipelineBuilder.h"
bsalomona387a112015-08-11 14:47:42 -070019#include "GrPipeline.h"
joshualitt7eb8c7b2014-11-18 14:24:27 -080020#include "GrVertexBuffer.h"
bsalomon6a44c6a2015-05-26 09:49:05 -070021#include "GrXferProcessor.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000022
bsalomon473addf2015-10-02 07:49:05 -070023#include "batches/GrDrawBatch.h"
24
bsalomon@google.com8d67c072012-12-13 20:38:14 +000025#include "SkClipStack.h"
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000026#include "SkMatrix.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000027#include "SkPath.h"
reed1b55a962015-09-17 20:16:13 -070028#include "SkStringUtils.h"
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +000029#include "SkStrokeRec.h"
robertphillips@google.coma2d71482012-08-01 20:08:47 +000030#include "SkTArray.h"
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000031#include "SkTLazy.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000032#include "SkTypes.h"
bsalomon@google.com8d67c072012-12-13 20:38:14 +000033#include "SkXfermode.h"
Scroggo97c88c22011-05-11 14:05:25 +000034
robertphillips498d7ac2015-10-30 10:11:30 -070035//#define ENABLE_MDB 1
36
joshualitt086cee12016-01-12 06:45:24 -080037class GrAuditTrail;
bsalomon53469832015-08-18 09:20:09 -070038class GrBatch;
joshualitt44701df2015-02-23 14:44:57 -080039class GrClip;
bsalomon4b91f762015-05-19 09:29:46 -070040class GrCaps;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000041class GrPath;
bsalomon1fcc01c2015-09-09 09:48:06 -070042class GrDrawPathBatchBase;
sugoi@google.com12b4e272012-12-06 20:13:11 +000043
bsalomonb3b9aec2015-09-10 11:16:35 -070044class GrDrawTarget final : public SkRefCnt {
bsalomon@google.comf6601872012-08-28 21:11:35 +000045public:
bsalomon69cfe952015-11-30 13:27:47 -080046 /** Options for GrDrawTarget behavior. */
47 struct Options {
bsalomon489147c2015-12-14 12:13:09 -080048 Options () : fClipBatchToBounds(false), fDrawBatchBounds(false), fMaxBatchLookback(-1) {}
bsalomon69cfe952015-11-30 13:27:47 -080049 bool fClipBatchToBounds;
bsalomon6dea83f2015-12-03 12:58:06 -080050 bool fDrawBatchBounds;
bsalomon489147c2015-12-14 12:13:09 -080051 int fMaxBatchLookback;
bsalomon69cfe952015-11-30 13:27:47 -080052 };
53
joshualitt086cee12016-01-12 06:45:24 -080054 GrDrawTarget(GrRenderTarget*, GrGpu*, GrResourceProvider*, GrAuditTrail*, const Options&);
bsalomona73239a2015-04-28 13:35:17 -070055
bsalomon512be532015-09-10 10:42:55 -070056 ~GrDrawTarget() override;
reed@google.comac10a2d2010-12-22 21:39:39 +000057
robertphillipsa106c622015-10-16 09:07:06 -070058 void makeClosed() {
59 // We only close drawTargets When MDB is enabled. When MDB is disabled there is only
60 // ever one drawTarget and all calls will be funnelled into it.
61#ifdef ENABLE_MDB
robertphillips6a186652015-10-20 07:37:58 -070062 this->setFlag(kClosed_Flag);
robertphillipsa106c622015-10-16 09:07:06 -070063#endif
64 }
robertphillips6a186652015-10-20 07:37:58 -070065 bool isClosed() const { return this->isSetFlag(kClosed_Flag); }
66
robertphillips498d7ac2015-10-30 10:11:30 -070067 // TODO: this entry point is only needed in the non-MDB world. Remove when
68 // we make the switch to MDB
69 void clearRT() { fRenderTarget = nullptr; }
70
robertphillips6a186652015-10-20 07:37:58 -070071 /*
72 * Notify this drawTarget that it relies on the contents of 'dependedOn'
73 */
74 void addDependency(GrSurface* dependedOn);
75
76 /*
77 * Does this drawTarget depend on 'dependedOn'?
78 */
79 bool dependsOn(GrDrawTarget* dependedOn) const {
80 return fDependencies.find(dependedOn) >= 0;
81 }
robertphillipsa106c622015-10-16 09:07:06 -070082
robertphillips4beb5c12015-10-20 07:50:00 -070083 /*
84 * Dump out the drawTarget dependency DAG
85 */
86 SkDEBUGCODE(void dump() const;)
87
reed@google.comac10a2d2010-12-22 21:39:39 +000088 /**
bsalomona73239a2015-04-28 13:35:17 -070089 * Empties the draw buffer of any queued up draws.
90 */
bsalomon512be532015-09-10 10:42:55 -070091 void reset();
bsalomona73239a2015-04-28 13:35:17 -070092
93 /**
robertphillipsa13e2022015-11-11 12:01:09 -080094 * Together these two functions flush all queued up draws to the Gpu.
bsalomona73239a2015-04-28 13:35:17 -070095 */
robertphillipsa13e2022015-11-11 12:01:09 -080096 void prepareBatches(GrBatchFlushState* flushState);
97 void drawBatches(GrBatchFlushState* flushState);
bsalomona73239a2015-04-28 13:35:17 -070098
99 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000100 * Gets the capabilities of the draw target.
101 */
bsalomonb3b9aec2015-09-10 11:16:35 -0700102 const GrCaps* caps() const { return fGpu->caps(); }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000103
robertphillips86c60752016-03-02 08:43:13 -0800104 void drawBatch(const GrPipelineBuilder&, GrDrawBatch*, const SkIRect* scissorRect = nullptr);
joshualitt4d8da812015-01-28 12:53:54 -0800105
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000106 /**
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000107 * Draws path into the stencil buffer. The fill must be either even/odd or
108 * winding (not inverse or hairline). It will respect the HW antialias flag
egdaniel8dd688b2015-01-22 10:16:09 -0800109 * on the GrPipelineBuilder (if possible in the 3D API). Note, we will never have an inverse
110 * fill with stencil path
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000111 */
joshualittf2384692015-09-10 11:00:51 -0700112 void stencilPath(const GrPipelineBuilder&, const SkMatrix& viewMatrix, const GrPath*,
egdaniel8dd688b2015-01-22 10:16:09 -0800113 GrPathRendering::FillType);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000114
115 /**
cdalton8ff8d242015-12-08 10:20:32 -0800116 * Draws a path batch. Fill must not be a hairline. It will respect the HW antialias flag on
117 * the GrPipelineBuilder (if possible in the 3D API). This needs to be separate from drawBatch
118 * because we install path stencil settings late.
bsalomon1fcc01c2015-09-09 09:48:06 -0700119 *
cdalton8ff8d242015-12-08 10:20:32 -0800120 * TODO: Figure out a better model that allows us to roll this method into drawBatch.
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000121 */
cdalton8ff8d242015-12-08 10:20:32 -0800122 void drawPathBatch(const GrPipelineBuilder& pipelineBuilder, GrDrawPathBatchBase* batch);
cdaltonb85a0aa2014-07-21 15:32:44 -0700123
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000124 /**
egdaniel8dd688b2015-01-22 10:16:09 -0800125 * Clear the passed in render target. Ignores the GrPipelineBuilder and clip. Clears the whole
halcanary96fcdcc2015-08-27 07:41:13 -0700126 * thing if rect is nullptr, otherwise just the rect. If canIgnoreRect is set then the entire
egdaniel8dd688b2015-01-22 10:16:09 -0800127 * render target can be optionally cleared.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000128 */
joshualitt9853cce2014-11-17 14:22:48 -0800129 void clear(const SkIRect* rect,
130 GrColor color,
131 bool canIgnoreRect,
bsalomon63b21962014-11-05 07:05:34 -0800132 GrRenderTarget* renderTarget);
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000133
bsalomon53469832015-08-18 09:20:09 -0700134 /** Discards the contents render target. */
135 void discard(GrRenderTarget*);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000136
137 /**
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000138 * Copies a pixel rectangle from one surface to another. This call may finalize
139 * reserved vertex/index data (as though a draw call was made). The src pixels
140 * copied are specified by srcRect. They are copied to a rect of the same
141 * size in dst with top left at dstPoint. If the src rect is clipped by the
142 * src bounds then pixel values in the dst rect corresponding to area clipped
bsalomon6df86402015-06-01 10:41:49 -0700143 * by the src rect are not overwritten. This method is not guaranteed to succeed
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000144 * depending on the type of surface, configs, etc, and the backend-specific
bsalomon6df86402015-06-01 10:41:49 -0700145 * limitations.
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000146 */
bsalomonb8fea972016-02-16 07:34:17 -0800147 bool copySurface(GrSurface* dst,
bsalomonf90a02b2014-11-26 12:28:00 -0800148 GrSurface* src,
149 const SkIRect& srcRect,
150 const SkIPoint& dstPoint);
robertphillips@google.comff175842012-05-14 19:31:39 +0000151
bsalomonb3b9aec2015-09-10 11:16:35 -0700152 /** Provides access to internal functions to GrClipMaskManager without friending all of
153 GrDrawTarget to CMM. */
154 class CMMAccess {
155 public:
156 CMMAccess(GrDrawTarget* drawTarget) : fDrawTarget(drawTarget) {}
157 private:
158 void clearStencilClip(const SkIRect& rect, bool insideClip, GrRenderTarget* rt) const {
159 fDrawTarget->clearStencilClip(rect, insideClip, rt);
160 }
bsalomona73239a2015-04-28 13:35:17 -0700161
bsalomonb3b9aec2015-09-10 11:16:35 -0700162 GrContext* context() const { return fDrawTarget->fContext; }
163 GrResourceProvider* resourceProvider() const { return fDrawTarget->fResourceProvider; }
164 GrDrawTarget* fDrawTarget;
165 friend class GrClipMaskManager;
166 };
167
168 const CMMAccess cmmAccess() { return CMMAccess(this); }
bsalomon5ea03632015-08-18 10:33:30 -0700169
joshualittde83b412016-01-14 09:58:36 -0800170 GrAuditTrail* getAuditTrail() const { return fAuditTrail; }
171
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000172private:
robertphillips6a186652015-10-20 07:37:58 -0700173 friend class GrDrawingManager; // for resetFlag & TopoSortTraits
174
175 enum Flags {
176 kClosed_Flag = 0x01, //!< This drawTarget can't accept any more batches
177
178 kWasOutput_Flag = 0x02, //!< Flag for topological sorting
179 kTempMark_Flag = 0x04, //!< Flag for topological sorting
180 };
181
182 void setFlag(uint32_t flag) {
183 fFlags |= flag;
184 }
185
186 void resetFlag(uint32_t flag) {
187 fFlags &= ~flag;
188 }
189
190 bool isSetFlag(uint32_t flag) const {
191 return SkToBool(fFlags & flag);
192 }
193
194 struct TopoSortTraits {
195 static void Output(GrDrawTarget* dt, int /* index */) {
196 dt->setFlag(GrDrawTarget::kWasOutput_Flag);
197 }
198 static bool WasOutput(const GrDrawTarget* dt) {
199 return dt->isSetFlag(GrDrawTarget::kWasOutput_Flag);
200 }
201 static void SetTempMark(GrDrawTarget* dt) {
202 dt->setFlag(GrDrawTarget::kTempMark_Flag);
203 }
204 static void ResetTempMark(GrDrawTarget* dt) {
205 dt->resetFlag(GrDrawTarget::kTempMark_Flag);
206 }
207 static bool IsTempMarked(const GrDrawTarget* dt) {
208 return dt->isSetFlag(GrDrawTarget::kTempMark_Flag);
209 }
210 static int NumDependencies(const GrDrawTarget* dt) {
211 return dt->fDependencies.count();
212 }
213 static GrDrawTarget* Dependency(GrDrawTarget* dt, int index) {
214 return dt->fDependencies[index];
215 }
216 };
217
bsalomonb3b9aec2015-09-10 11:16:35 -0700218 void recordBatch(GrBatch*);
cdaltond4727922015-11-10 12:49:06 -0800219 bool installPipelineInDrawBatch(const GrPipelineBuilder* pipelineBuilder,
220 const GrScissorState* scissor,
221 GrDrawBatch* batch);
bsalomonad792c12015-09-10 11:10:50 -0700222
223 // Makes a copy of the dst if it is necessary for the draw. Returns false if a copy is required
224 // but couldn't be made. Otherwise, returns true. This method needs to be protected because it
225 // needs to be accessed by GLPrograms to setup a correct drawstate
226 bool setupDstReadIfNecessary(const GrPipelineBuilder&,
ethannicholasde4166a2015-11-30 08:57:38 -0800227 const GrPipelineOptimizations& optimizations,
bsalomonad792c12015-09-10 11:10:50 -0700228 GrXferProcessor::DstTexture*,
229 const SkRect& batchBounds);
230
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000231 // Check to see if this set of draw commands has been sent out
joshualitt9853cce2014-11-17 14:22:48 -0800232 void getPathStencilSettingsForFilltype(GrPathRendering::FillType,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700233 const GrStencilAttachment*,
joshualitt9853cce2014-11-17 14:22:48 -0800234 GrStencilSettings*);
bsalomonb3b9aec2015-09-10 11:16:35 -0700235 bool setupClip(const GrPipelineBuilder&,
joshualitt4421a4c2015-07-13 09:36:41 -0700236 GrPipelineBuilder::AutoRestoreFragmentProcessorState*,
bsalomon6be6f7c2015-02-26 13:05:21 -0800237 GrPipelineBuilder::AutoRestoreStencil*,
238 GrScissorState*,
bsalomonb3b9aec2015-09-10 11:16:35 -0700239 const SkRect* devBounds);
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000240
robertphillips6a186652015-10-20 07:37:58 -0700241 void addDependency(GrDrawTarget* dependedOn);
242
bsalomonb3b9aec2015-09-10 11:16:35 -0700243 // Used only by CMM.
bsalomon5ea03632015-08-18 10:33:30 -0700244 void clearStencilClip(const SkIRect&, bool insideClip, GrRenderTarget*);
joshualitt6db519c2014-10-29 08:48:18 -0700245
bsalomonb3b9aec2015-09-10 11:16:35 -0700246 SkSTArray<256, SkAutoTUnref<GrBatch>, true> fBatches;
247 SkAutoTDelete<GrClipMaskManager> fClipMaskManager;
248 // The context is only in service of the clip mask manager, remove once CMM doesn't need this.
249 GrContext* fContext;
250 GrGpu* fGpu;
251 GrResourceProvider* fResourceProvider;
joshualitt086cee12016-01-12 06:45:24 -0800252 GrAuditTrail* fAuditTrail;
joshualitt3bdd7dc2014-10-31 08:27:39 -0700253
robertphillips4beb5c12015-10-20 07:50:00 -0700254 SkDEBUGCODE(int fDebugID;)
robertphillips6a186652015-10-20 07:37:58 -0700255 uint32_t fFlags;
256
257 // 'this' drawTarget relies on the output of the drawTargets in 'fDependencies'
258 SkTDArray<GrDrawTarget*> fDependencies;
robertphillips498d7ac2015-10-30 10:11:30 -0700259 GrRenderTarget* fRenderTarget;
robertphillipsa106c622015-10-16 09:07:06 -0700260
bsalomon6dea83f2015-12-03 12:58:06 -0800261 bool fDrawBatchBounds;
bsalomon489147c2015-12-14 12:13:09 -0800262 int fMaxBatchLookback;
bsalomon6dea83f2015-12-03 12:58:06 -0800263
bsalomonb3b9aec2015-09-10 11:16:35 -0700264 typedef SkRefCnt INHERITED;
joshualitt6db519c2014-10-29 08:48:18 -0700265};
266
reed@google.comac10a2d2010-12-22 21:39:39 +0000267#endif