blob: 72ab46e934d1f3419e74bef59a0adb0a3e8ce9d0 [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
bsalomonabd30f52015-08-13 13:34:48 -0700104 void drawBatch(const GrPipelineBuilder&, GrDrawBatch*);
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 /**
bsalomon72e3ae42015-04-28 08:08:46 -0700125 * Helper function for drawing rects.
skia.committer@gmail.com044679e2013-02-15 07:16:57 +0000126 *
bsalomon@google.comc7818882013-03-20 19:19:53 +0000127 * @param rect the rect to draw
bsalomon@google.comc7818882013-03-20 19:19:53 +0000128 * @param localRect optional rect that specifies local coords to map onto
halcanary96fcdcc2015-08-27 07:41:13 -0700129 * rect. If nullptr then rect serves as the local coords.
joshualitt8fc6c2d2014-12-22 15:27:05 -0800130 * @param localMatrix Optional local matrix. The local coordinates are specified by localRect,
halcanary96fcdcc2015-08-27 07:41:13 -0700131 * or if it is nullptr by rect. This matrix applies to the coordinate implied by
joshualitt8fc6c2d2014-12-22 15:27:05 -0800132 * that rectangle before it is input to GrCoordTransforms that read local
133 * coordinates
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000134 */
joshualittd2b23e02015-08-21 10:53:34 -0700135 void drawNonAARect(const GrPipelineBuilder& pipelineBuilder,
136 GrColor color,
137 const SkMatrix& viewMatrix,
138 const SkRect& rect);
joshualittb6b513b2015-08-21 10:25:18 -0700139
joshualittd2b23e02015-08-21 10:53:34 -0700140 void drawNonAARect(const GrPipelineBuilder& pipelineBuilder,
141 GrColor color,
142 const SkMatrix& viewMatrix,
143 const SkRect& rect,
144 const SkMatrix& localMatrix);
joshualittb6b513b2015-08-21 10:25:18 -0700145
joshualittd2b23e02015-08-21 10:53:34 -0700146 void drawNonAARect(const GrPipelineBuilder& pipelineBuilder,
147 GrColor color,
148 const SkMatrix& viewMatrix,
149 const SkRect& rect,
150 const SkRect& localRect);
jvanverth@google.com39768252013-02-14 15:25:44 +0000151
joshualittd2b23e02015-08-21 10:53:34 -0700152 void drawNonAARect(const GrPipelineBuilder& ds,
153 GrColor color,
154 const SkMatrix& viewM,
155 const SkIRect& irect) {
reed@google.com44699382013-10-31 17:28:30 +0000156 SkRect rect = SkRect::Make(irect);
joshualittd2b23e02015-08-21 10:53:34 -0700157 this->drawNonAARect(ds, color, viewM, rect);
bsalomon@google.comcf939ae2012-12-13 19:59:23 +0000158 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000159
joshualitt1c735482015-07-13 08:08:25 -0700160 void drawAARect(const GrPipelineBuilder& pipelineBuilder,
robertphillipsea461502015-05-26 11:38:03 -0700161 GrColor color,
162 const SkMatrix& viewMatrix,
163 const SkRect& rect,
164 const SkRect& devRect);
bsalomon@google.com934c5702012-03-20 21:17:58 +0000165
166 /**
egdaniel8dd688b2015-01-22 10:16:09 -0800167 * Clear the passed in render target. Ignores the GrPipelineBuilder and clip. Clears the whole
halcanary96fcdcc2015-08-27 07:41:13 -0700168 * thing if rect is nullptr, otherwise just the rect. If canIgnoreRect is set then the entire
egdaniel8dd688b2015-01-22 10:16:09 -0800169 * render target can be optionally cleared.
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000170 */
joshualitt9853cce2014-11-17 14:22:48 -0800171 void clear(const SkIRect* rect,
172 GrColor color,
173 bool canIgnoreRect,
bsalomon63b21962014-11-05 07:05:34 -0800174 GrRenderTarget* renderTarget);
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000175
bsalomon53469832015-08-18 09:20:09 -0700176 /** Discards the contents render target. */
177 void discard(GrRenderTarget*);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000178
179 /**
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000180 * Copies a pixel rectangle from one surface to another. This call may finalize
181 * reserved vertex/index data (as though a draw call was made). The src pixels
182 * copied are specified by srcRect. They are copied to a rect of the same
183 * size in dst with top left at dstPoint. If the src rect is clipped by the
184 * src bounds then pixel values in the dst rect corresponding to area clipped
bsalomon6df86402015-06-01 10:41:49 -0700185 * by the src rect are not overwritten. This method is not guaranteed to succeed
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000186 * depending on the type of surface, configs, etc, and the backend-specific
bsalomon6df86402015-06-01 10:41:49 -0700187 * limitations.
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000188 */
bsalomon6df86402015-06-01 10:41:49 -0700189 void copySurface(GrSurface* dst,
bsalomonf90a02b2014-11-26 12:28:00 -0800190 GrSurface* src,
191 const SkIRect& srcRect,
192 const SkIPoint& dstPoint);
robertphillips@google.comff175842012-05-14 19:31:39 +0000193
bsalomonb3b9aec2015-09-10 11:16:35 -0700194 /** Provides access to internal functions to GrClipMaskManager without friending all of
195 GrDrawTarget to CMM. */
196 class CMMAccess {
197 public:
198 CMMAccess(GrDrawTarget* drawTarget) : fDrawTarget(drawTarget) {}
199 private:
200 void clearStencilClip(const SkIRect& rect, bool insideClip, GrRenderTarget* rt) const {
201 fDrawTarget->clearStencilClip(rect, insideClip, rt);
202 }
bsalomona73239a2015-04-28 13:35:17 -0700203
bsalomonb3b9aec2015-09-10 11:16:35 -0700204 GrContext* context() const { return fDrawTarget->fContext; }
205 GrResourceProvider* resourceProvider() const { return fDrawTarget->fResourceProvider; }
206 GrDrawTarget* fDrawTarget;
207 friend class GrClipMaskManager;
208 };
209
210 const CMMAccess cmmAccess() { return CMMAccess(this); }
bsalomon5ea03632015-08-18 10:33:30 -0700211
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000212private:
robertphillips6a186652015-10-20 07:37:58 -0700213 friend class GrDrawingManager; // for resetFlag & TopoSortTraits
214
215 enum Flags {
216 kClosed_Flag = 0x01, //!< This drawTarget can't accept any more batches
217
218 kWasOutput_Flag = 0x02, //!< Flag for topological sorting
219 kTempMark_Flag = 0x04, //!< Flag for topological sorting
220 };
221
222 void setFlag(uint32_t flag) {
223 fFlags |= flag;
224 }
225
226 void resetFlag(uint32_t flag) {
227 fFlags &= ~flag;
228 }
229
230 bool isSetFlag(uint32_t flag) const {
231 return SkToBool(fFlags & flag);
232 }
233
234 struct TopoSortTraits {
235 static void Output(GrDrawTarget* dt, int /* index */) {
236 dt->setFlag(GrDrawTarget::kWasOutput_Flag);
237 }
238 static bool WasOutput(const GrDrawTarget* dt) {
239 return dt->isSetFlag(GrDrawTarget::kWasOutput_Flag);
240 }
241 static void SetTempMark(GrDrawTarget* dt) {
242 dt->setFlag(GrDrawTarget::kTempMark_Flag);
243 }
244 static void ResetTempMark(GrDrawTarget* dt) {
245 dt->resetFlag(GrDrawTarget::kTempMark_Flag);
246 }
247 static bool IsTempMarked(const GrDrawTarget* dt) {
248 return dt->isSetFlag(GrDrawTarget::kTempMark_Flag);
249 }
250 static int NumDependencies(const GrDrawTarget* dt) {
251 return dt->fDependencies.count();
252 }
253 static GrDrawTarget* Dependency(GrDrawTarget* dt, int index) {
254 return dt->fDependencies[index];
255 }
256 };
257
bsalomonb3b9aec2015-09-10 11:16:35 -0700258 void recordBatch(GrBatch*);
cdaltond4727922015-11-10 12:49:06 -0800259 bool installPipelineInDrawBatch(const GrPipelineBuilder* pipelineBuilder,
260 const GrScissorState* scissor,
261 GrDrawBatch* batch);
bsalomonad792c12015-09-10 11:10:50 -0700262
263 // Makes a copy of the dst if it is necessary for the draw. Returns false if a copy is required
264 // but couldn't be made. Otherwise, returns true. This method needs to be protected because it
265 // needs to be accessed by GLPrograms to setup a correct drawstate
266 bool setupDstReadIfNecessary(const GrPipelineBuilder&,
ethannicholasde4166a2015-11-30 08:57:38 -0800267 const GrPipelineOptimizations& optimizations,
bsalomonad792c12015-09-10 11:10:50 -0700268 GrXferProcessor::DstTexture*,
269 const SkRect& batchBounds);
270
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000271 // Check to see if this set of draw commands has been sent out
joshualitt9853cce2014-11-17 14:22:48 -0800272 void getPathStencilSettingsForFilltype(GrPathRendering::FillType,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700273 const GrStencilAttachment*,
joshualitt9853cce2014-11-17 14:22:48 -0800274 GrStencilSettings*);
bsalomonb3b9aec2015-09-10 11:16:35 -0700275 bool setupClip(const GrPipelineBuilder&,
joshualitt4421a4c2015-07-13 09:36:41 -0700276 GrPipelineBuilder::AutoRestoreFragmentProcessorState*,
bsalomon6be6f7c2015-02-26 13:05:21 -0800277 GrPipelineBuilder::AutoRestoreStencil*,
278 GrScissorState*,
bsalomonb3b9aec2015-09-10 11:16:35 -0700279 const SkRect* devBounds);
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000280
robertphillips6a186652015-10-20 07:37:58 -0700281 void addDependency(GrDrawTarget* dependedOn);
282
bsalomonb3b9aec2015-09-10 11:16:35 -0700283 // Used only by CMM.
bsalomon5ea03632015-08-18 10:33:30 -0700284 void clearStencilClip(const SkIRect&, bool insideClip, GrRenderTarget*);
joshualitt6db519c2014-10-29 08:48:18 -0700285
bsalomonb3b9aec2015-09-10 11:16:35 -0700286 SkSTArray<256, SkAutoTUnref<GrBatch>, true> fBatches;
287 SkAutoTDelete<GrClipMaskManager> fClipMaskManager;
288 // The context is only in service of the clip mask manager, remove once CMM doesn't need this.
289 GrContext* fContext;
290 GrGpu* fGpu;
291 GrResourceProvider* fResourceProvider;
joshualitt086cee12016-01-12 06:45:24 -0800292 GrAuditTrail* fAuditTrail;
joshualitt3bdd7dc2014-10-31 08:27:39 -0700293
robertphillips4beb5c12015-10-20 07:50:00 -0700294 SkDEBUGCODE(int fDebugID;)
robertphillips6a186652015-10-20 07:37:58 -0700295 uint32_t fFlags;
296
297 // 'this' drawTarget relies on the output of the drawTargets in 'fDependencies'
298 SkTDArray<GrDrawTarget*> fDependencies;
robertphillips498d7ac2015-10-30 10:11:30 -0700299 GrRenderTarget* fRenderTarget;
robertphillipsa106c622015-10-16 09:07:06 -0700300
bsalomon6dea83f2015-12-03 12:58:06 -0800301 bool fDrawBatchBounds;
bsalomon489147c2015-12-14 12:13:09 -0800302 int fMaxBatchLookback;
bsalomon6dea83f2015-12-03 12:58:06 -0800303
bsalomonb3b9aec2015-09-10 11:16:35 -0700304 typedef SkRefCnt INHERITED;
joshualitt6db519c2014-10-29 08:48:18 -0700305};
306
reed@google.comac10a2d2010-12-22 21:39:39 +0000307#endif