blob: c863b455b36ed8ae630424daf503c179cdb8d6b1 [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"
kkinnunenccdaa042014-08-20 01:36:23 -070016#include "GrPathRendering.h"
egdaniel8dd688b2015-01-22 10:16:09 -080017#include "GrPipelineBuilder.h"
bsalomon6a44c6a2015-05-26 09:49:05 -070018#include "GrXferProcessor.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000019
bsalomon473addf2015-10-02 07:49:05 -070020#include "batches/GrDrawBatch.h"
21
bsalomon@google.com8d67c072012-12-13 20:38:14 +000022#include "SkClipStack.h"
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000023#include "SkMatrix.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000024#include "SkPath.h"
reed1b55a962015-09-17 20:16:13 -070025#include "SkStringUtils.h"
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +000026#include "SkStrokeRec.h"
robertphillips@google.coma2d71482012-08-01 20:08:47 +000027#include "SkTArray.h"
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000028#include "SkTLazy.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000029#include "SkTypes.h"
bsalomon@google.com8d67c072012-12-13 20:38:14 +000030#include "SkXfermode.h"
Scroggo97c88c22011-05-11 14:05:25 +000031
robertphillips498d7ac2015-10-30 10:11:30 -070032//#define ENABLE_MDB 1
33
joshualitt086cee12016-01-12 06:45:24 -080034class GrAuditTrail;
bsalomon53469832015-08-18 09:20:09 -070035class GrBatch;
joshualitt44701df2015-02-23 14:44:57 -080036class GrClip;
bsalomon4b91f762015-05-19 09:29:46 -070037class GrCaps;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000038class GrPath;
bsalomon1fcc01c2015-09-09 09:48:06 -070039class GrDrawPathBatchBase;
sugoi@google.com12b4e272012-12-06 20:13:11 +000040
bsalomonb3b9aec2015-09-10 11:16:35 -070041class GrDrawTarget final : public SkRefCnt {
bsalomon@google.comf6601872012-08-28 21:11:35 +000042public:
bsalomon69cfe952015-11-30 13:27:47 -080043 /** Options for GrDrawTarget behavior. */
44 struct Options {
bsalomonaecc0182016-03-07 11:50:44 -080045 Options ()
46 : fClipBatchToBounds(false)
47 , fDrawBatchBounds(false)
48 , fMaxBatchLookback(-1)
49 , fMaxBatchLookahead(-1) {}
bsalomon69cfe952015-11-30 13:27:47 -080050 bool fClipBatchToBounds;
bsalomon6dea83f2015-12-03 12:58:06 -080051 bool fDrawBatchBounds;
bsalomon489147c2015-12-14 12:13:09 -080052 int fMaxBatchLookback;
bsalomonaecc0182016-03-07 11:50:44 -080053 int fMaxBatchLookahead;
bsalomon69cfe952015-11-30 13:27:47 -080054 };
55
joshualitt086cee12016-01-12 06:45:24 -080056 GrDrawTarget(GrRenderTarget*, GrGpu*, GrResourceProvider*, GrAuditTrail*, const Options&);
bsalomona73239a2015-04-28 13:35:17 -070057
bsalomon512be532015-09-10 10:42:55 -070058 ~GrDrawTarget() override;
reed@google.comac10a2d2010-12-22 21:39:39 +000059
robertphillipsa106c622015-10-16 09:07:06 -070060 void makeClosed() {
61 // We only close drawTargets When MDB is enabled. When MDB is disabled there is only
62 // ever one drawTarget and all calls will be funnelled into it.
63#ifdef ENABLE_MDB
robertphillips6a186652015-10-20 07:37:58 -070064 this->setFlag(kClosed_Flag);
robertphillipsa106c622015-10-16 09:07:06 -070065#endif
bsalomonaecc0182016-03-07 11:50:44 -080066 this->forwardCombine();
robertphillipsa106c622015-10-16 09:07:06 -070067 }
bsalomonaecc0182016-03-07 11:50:44 -080068
robertphillips6a186652015-10-20 07:37:58 -070069 bool isClosed() const { return this->isSetFlag(kClosed_Flag); }
70
robertphillips498d7ac2015-10-30 10:11:30 -070071 // TODO: this entry point is only needed in the non-MDB world. Remove when
72 // we make the switch to MDB
73 void clearRT() { fRenderTarget = nullptr; }
74
robertphillips6a186652015-10-20 07:37:58 -070075 /*
76 * Notify this drawTarget that it relies on the contents of 'dependedOn'
77 */
78 void addDependency(GrSurface* dependedOn);
79
80 /*
81 * Does this drawTarget depend on 'dependedOn'?
82 */
83 bool dependsOn(GrDrawTarget* dependedOn) const {
84 return fDependencies.find(dependedOn) >= 0;
85 }
robertphillipsa106c622015-10-16 09:07:06 -070086
robertphillips4beb5c12015-10-20 07:50:00 -070087 /*
88 * Dump out the drawTarget dependency DAG
89 */
90 SkDEBUGCODE(void dump() const;)
91
reed@google.comac10a2d2010-12-22 21:39:39 +000092 /**
bsalomona73239a2015-04-28 13:35:17 -070093 * Empties the draw buffer of any queued up draws.
94 */
bsalomon512be532015-09-10 10:42:55 -070095 void reset();
bsalomona73239a2015-04-28 13:35:17 -070096
97 /**
robertphillipsa13e2022015-11-11 12:01:09 -080098 * Together these two functions flush all queued up draws to the Gpu.
bsalomona73239a2015-04-28 13:35:17 -070099 */
robertphillipsa13e2022015-11-11 12:01:09 -0800100 void prepareBatches(GrBatchFlushState* flushState);
101 void drawBatches(GrBatchFlushState* flushState);
bsalomona73239a2015-04-28 13:35:17 -0700102
103 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000104 * Gets the capabilities of the draw target.
105 */
bsalomonb3b9aec2015-09-10 11:16:35 -0700106 const GrCaps* caps() const { return fGpu->caps(); }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000107
robertphillips391395d2016-03-02 09:26:36 -0800108 void drawBatch(const GrPipelineBuilder&, GrDrawBatch*, const SkIRect* scissorRect = nullptr);
joshualitt4d8da812015-01-28 12:53:54 -0800109
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000110 /**
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000111 * Draws path into the stencil buffer. The fill must be either even/odd or
112 * winding (not inverse or hairline). It will respect the HW antialias flag
egdaniel8dd688b2015-01-22 10:16:09 -0800113 * on the GrPipelineBuilder (if possible in the 3D API). Note, we will never have an inverse
114 * fill with stencil path
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000115 */
joshualittf2384692015-09-10 11:00:51 -0700116 void stencilPath(const GrPipelineBuilder&, const SkMatrix& viewMatrix, const GrPath*,
egdaniel8dd688b2015-01-22 10:16:09 -0800117 GrPathRendering::FillType);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000118
119 /**
cdalton8ff8d242015-12-08 10:20:32 -0800120 * Draws a path batch. Fill must not be a hairline. It will respect the HW antialias flag on
121 * the GrPipelineBuilder (if possible in the 3D API). This needs to be separate from drawBatch
122 * because we install path stencil settings late.
bsalomon1fcc01c2015-09-09 09:48:06 -0700123 *
cdalton8ff8d242015-12-08 10:20:32 -0800124 * TODO: Figure out a better model that allows us to roll this method into drawBatch.
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000125 */
cdalton8ff8d242015-12-08 10:20:32 -0800126 void drawPathBatch(const GrPipelineBuilder& pipelineBuilder, GrDrawPathBatchBase* batch);
cdaltonb85a0aa2014-07-21 15:32:44 -0700127
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000128 /**
egdaniel8dd688b2015-01-22 10:16:09 -0800129 * Clear the passed in render target. Ignores the GrPipelineBuilder and clip. Clears the whole
halcanary96fcdcc2015-08-27 07:41:13 -0700130 * thing if rect is nullptr, otherwise just the rect. If canIgnoreRect is set then the entire
egdaniel8dd688b2015-01-22 10:16:09 -0800131 * render target can be optionally cleared.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000132 */
joshualitt9853cce2014-11-17 14:22:48 -0800133 void clear(const SkIRect* rect,
134 GrColor color,
135 bool canIgnoreRect,
bsalomon63b21962014-11-05 07:05:34 -0800136 GrRenderTarget* renderTarget);
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000137
bsalomon53469832015-08-18 09:20:09 -0700138 /** Discards the contents render target. */
139 void discard(GrRenderTarget*);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000140
141 /**
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000142 * Copies a pixel rectangle from one surface to another. This call may finalize
143 * reserved vertex/index data (as though a draw call was made). The src pixels
144 * copied are specified by srcRect. They are copied to a rect of the same
145 * size in dst with top left at dstPoint. If the src rect is clipped by the
146 * src bounds then pixel values in the dst rect corresponding to area clipped
bsalomon6df86402015-06-01 10:41:49 -0700147 * by the src rect are not overwritten. This method is not guaranteed to succeed
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000148 * depending on the type of surface, configs, etc, and the backend-specific
bsalomon6df86402015-06-01 10:41:49 -0700149 * limitations.
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000150 */
bsalomonb8fea972016-02-16 07:34:17 -0800151 bool copySurface(GrSurface* dst,
bsalomonf90a02b2014-11-26 12:28:00 -0800152 GrSurface* src,
153 const SkIRect& srcRect,
154 const SkIPoint& dstPoint);
robertphillips@google.comff175842012-05-14 19:31:39 +0000155
bsalomonb3b9aec2015-09-10 11:16:35 -0700156 /** Provides access to internal functions to GrClipMaskManager without friending all of
157 GrDrawTarget to CMM. */
158 class CMMAccess {
159 public:
160 CMMAccess(GrDrawTarget* drawTarget) : fDrawTarget(drawTarget) {}
161 private:
162 void clearStencilClip(const SkIRect& rect, bool insideClip, GrRenderTarget* rt) const {
163 fDrawTarget->clearStencilClip(rect, insideClip, rt);
164 }
bsalomona73239a2015-04-28 13:35:17 -0700165
bsalomonb3b9aec2015-09-10 11:16:35 -0700166 GrContext* context() const { return fDrawTarget->fContext; }
167 GrResourceProvider* resourceProvider() const { return fDrawTarget->fResourceProvider; }
168 GrDrawTarget* fDrawTarget;
169 friend class GrClipMaskManager;
170 };
171
172 const CMMAccess cmmAccess() { return CMMAccess(this); }
bsalomon5ea03632015-08-18 10:33:30 -0700173
joshualittde83b412016-01-14 09:58:36 -0800174 GrAuditTrail* getAuditTrail() const { return fAuditTrail; }
175
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000176private:
robertphillips6a186652015-10-20 07:37:58 -0700177 friend class GrDrawingManager; // for resetFlag & TopoSortTraits
178
179 enum Flags {
180 kClosed_Flag = 0x01, //!< This drawTarget can't accept any more batches
181
182 kWasOutput_Flag = 0x02, //!< Flag for topological sorting
183 kTempMark_Flag = 0x04, //!< Flag for topological sorting
184 };
185
186 void setFlag(uint32_t flag) {
187 fFlags |= flag;
188 }
189
190 void resetFlag(uint32_t flag) {
191 fFlags &= ~flag;
192 }
193
194 bool isSetFlag(uint32_t flag) const {
195 return SkToBool(fFlags & flag);
196 }
197
198 struct TopoSortTraits {
199 static void Output(GrDrawTarget* dt, int /* index */) {
200 dt->setFlag(GrDrawTarget::kWasOutput_Flag);
201 }
202 static bool WasOutput(const GrDrawTarget* dt) {
203 return dt->isSetFlag(GrDrawTarget::kWasOutput_Flag);
204 }
205 static void SetTempMark(GrDrawTarget* dt) {
206 dt->setFlag(GrDrawTarget::kTempMark_Flag);
207 }
208 static void ResetTempMark(GrDrawTarget* dt) {
209 dt->resetFlag(GrDrawTarget::kTempMark_Flag);
210 }
211 static bool IsTempMarked(const GrDrawTarget* dt) {
212 return dt->isSetFlag(GrDrawTarget::kTempMark_Flag);
213 }
214 static int NumDependencies(const GrDrawTarget* dt) {
215 return dt->fDependencies.count();
216 }
217 static GrDrawTarget* Dependency(GrDrawTarget* dt, int index) {
218 return dt->fDependencies[index];
219 }
220 };
221
bsalomonb3b9aec2015-09-10 11:16:35 -0700222 void recordBatch(GrBatch*);
bsalomonaecc0182016-03-07 11:50:44 -0800223 void forwardCombine();
cdaltond4727922015-11-10 12:49:06 -0800224 bool installPipelineInDrawBatch(const GrPipelineBuilder* pipelineBuilder,
225 const GrScissorState* scissor,
226 GrDrawBatch* batch);
bsalomonad792c12015-09-10 11:10:50 -0700227
228 // Makes a copy of the dst if it is necessary for the draw. Returns false if a copy is required
229 // but couldn't be made. Otherwise, returns true. This method needs to be protected because it
230 // needs to be accessed by GLPrograms to setup a correct drawstate
231 bool setupDstReadIfNecessary(const GrPipelineBuilder&,
ethannicholasde4166a2015-11-30 08:57:38 -0800232 const GrPipelineOptimizations& optimizations,
bsalomonad792c12015-09-10 11:10:50 -0700233 GrXferProcessor::DstTexture*,
234 const SkRect& batchBounds);
235
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000236 // Check to see if this set of draw commands has been sent out
joshualitt9853cce2014-11-17 14:22:48 -0800237 void getPathStencilSettingsForFilltype(GrPathRendering::FillType,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700238 const GrStencilAttachment*,
joshualitt9853cce2014-11-17 14:22:48 -0800239 GrStencilSettings*);
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;
bsalomonaecc0182016-03-07 11:50:44 -0800263 int fMaxBatchLookahead;
bsalomon6dea83f2015-12-03 12:58:06 -0800264
bsalomonb3b9aec2015-09-10 11:16:35 -0700265 typedef SkRefCnt INHERITED;
joshualitt6db519c2014-10-29 08:48:18 -0700266};
267
reed@google.comac10a2d2010-12-22 21:39:39 +0000268#endif