blob: dfc1489d355099d96b317ecfc32e5aeabe3cf0ad [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"
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +000012#include "GrContext.h"
joshualitt8072caa2015-02-12 14:20:52 -080013#include "GrPathProcessor.h"
14#include "GrPrimitiveProcessor.h"
kkinnunenccdaa042014-08-20 01:36:23 -070015#include "GrPathRendering.h"
bsalomon6a44c6a2015-05-26 09:49:05 -070016#include "GrXferProcessor.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000017
bsalomon473addf2015-10-02 07:49:05 -070018#include "batches/GrDrawBatch.h"
19
bsalomon@google.com8d67c072012-12-13 20:38:14 +000020#include "SkClipStack.h"
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000021#include "SkMatrix.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000022#include "SkPath.h"
reed1b55a962015-09-17 20:16:13 -070023#include "SkStringUtils.h"
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +000024#include "SkStrokeRec.h"
robertphillips@google.coma2d71482012-08-01 20:08:47 +000025#include "SkTArray.h"
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000026#include "SkTLazy.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000027#include "SkTypes.h"
bsalomon@google.com8d67c072012-12-13 20:38:14 +000028#include "SkXfermode.h"
Scroggo97c88c22011-05-11 14:05:25 +000029
robertphillips498d7ac2015-10-30 10:11:30 -070030//#define ENABLE_MDB 1
31
joshualitt086cee12016-01-12 06:45:24 -080032class GrAuditTrail;
bsalomon53469832015-08-18 09:20:09 -070033class GrBatch;
bsalomonfd8d0132016-08-11 11:25:33 -070034class GrClearBatch;
joshualitt44701df2015-02-23 14:44:57 -080035class GrClip;
bsalomon4b91f762015-05-19 09:29:46 -070036class GrCaps;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000037class GrPath;
bsalomon1fcc01c2015-09-09 09:48:06 -070038class GrDrawPathBatchBase;
robertphillips28a838e2016-06-23 14:07:00 -070039class GrPipelineBuilder;
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() {
bsalomonfd8d0132016-08-11 11:25:33 -070061 fLastFullClearBatch = nullptr;
robertphillipsa106c622015-10-16 09:07:06 -070062 // We only close drawTargets When MDB is enabled. When MDB is disabled there is only
63 // ever one drawTarget and all calls will be funnelled into it.
64#ifdef ENABLE_MDB
robertphillips6a186652015-10-20 07:37:58 -070065 this->setFlag(kClosed_Flag);
robertphillipsa106c622015-10-16 09:07:06 -070066#endif
bsalomonaecc0182016-03-07 11:50:44 -080067 this->forwardCombine();
robertphillipsa106c622015-10-16 09:07:06 -070068 }
bsalomonaecc0182016-03-07 11:50:44 -080069
robertphillips6a186652015-10-20 07:37:58 -070070 bool isClosed() const { return this->isSetFlag(kClosed_Flag); }
71
robertphillips498d7ac2015-10-30 10:11:30 -070072 // TODO: this entry point is only needed in the non-MDB world. Remove when
73 // we make the switch to MDB
74 void clearRT() { fRenderTarget = nullptr; }
75
robertphillips6a186652015-10-20 07:37:58 -070076 /*
77 * Notify this drawTarget that it relies on the contents of 'dependedOn'
78 */
79 void addDependency(GrSurface* dependedOn);
80
81 /*
82 * Does this drawTarget depend on 'dependedOn'?
83 */
84 bool dependsOn(GrDrawTarget* dependedOn) const {
85 return fDependencies.find(dependedOn) >= 0;
86 }
robertphillipsa106c622015-10-16 09:07:06 -070087
robertphillips4beb5c12015-10-20 07:50:00 -070088 /*
89 * Dump out the drawTarget dependency DAG
90 */
91 SkDEBUGCODE(void dump() const;)
92
reed@google.comac10a2d2010-12-22 21:39:39 +000093 /**
bsalomona73239a2015-04-28 13:35:17 -070094 * Empties the draw buffer of any queued up draws.
95 */
bsalomon512be532015-09-10 10:42:55 -070096 void reset();
bsalomona73239a2015-04-28 13:35:17 -070097
98 /**
bsalomondc438982016-08-31 11:53:49 -070099 * Together these two functions flush all queued up draws to GrCommandBuffer. The return value
100 * of drawBatches() indicates whether any commands were actually issued to the GPU.
bsalomona73239a2015-04-28 13:35:17 -0700101 */
robertphillipsa13e2022015-11-11 12:01:09 -0800102 void prepareBatches(GrBatchFlushState* flushState);
bsalomondc438982016-08-31 11:53:49 -0700103 bool drawBatches(GrBatchFlushState* flushState);
bsalomona73239a2015-04-28 13:35:17 -0700104
105 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000106 * Gets the capabilities of the draw target.
107 */
bsalomonb3b9aec2015-09-10 11:16:35 -0700108 const GrCaps* caps() const { return fGpu->caps(); }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000109
robertphillips976f5f02016-06-03 10:59:20 -0700110 void drawBatch(const GrPipelineBuilder&, GrDrawContext*, const GrClip&, GrDrawBatch*);
joshualitt4d8da812015-01-28 12:53:54 -0800111
robertphillips9199a9f2016-07-13 07:48:43 -0700112 void addBatch(sk_sp<GrBatch>);
113
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000114 /**
csmartdalton5c6fc4f2016-08-12 15:11:51 -0700115 * Draws the path into user stencil bits. Upon return, all user stencil values
116 * inside the path will be nonzero. The path's fill must be either even/odd or
117 * winding (notnverse or hairline).It will respect the HW antialias boolean (if
118 * possible in the 3D API). Note, we will never have an inverse fill with
119 * stencil path.
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000120 */
robertphillips59cf61a2016-07-13 09:18:21 -0700121 void stencilPath(GrDrawContext*,
122 const GrClip&,
robertphillips59cf61a2016-07-13 09:18:21 -0700123 bool useHWAA,
124 const SkMatrix& viewMatrix,
125 const GrPath*);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000126
bsalomon9f129de2016-08-10 16:31:05 -0700127 /** Clears the entire render target */
128 void fullClear(GrRenderTarget*, GrColor color);
129
bsalomon53469832015-08-18 09:20:09 -0700130 /** Discards the contents render target. */
131 void discard(GrRenderTarget*);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000132
133 /**
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000134 * Copies a pixel rectangle from one surface to another. This call may finalize
135 * reserved vertex/index data (as though a draw call was made). The src pixels
136 * copied are specified by srcRect. They are copied to a rect of the same
137 * size in dst with top left at dstPoint. If the src rect is clipped by the
138 * src bounds then pixel values in the dst rect corresponding to area clipped
bsalomon6df86402015-06-01 10:41:49 -0700139 * by the src rect are not overwritten. This method is not guaranteed to succeed
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000140 * depending on the type of surface, configs, etc, and the backend-specific
bsalomon6df86402015-06-01 10:41:49 -0700141 * limitations.
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000142 */
bsalomonb8fea972016-02-16 07:34:17 -0800143 bool copySurface(GrSurface* dst,
bsalomonf90a02b2014-11-26 12:28:00 -0800144 GrSurface* src,
145 const SkIRect& srcRect,
146 const SkIPoint& dstPoint);
robertphillips@google.comff175842012-05-14 19:31:39 +0000147
csmartdaltone0d36292016-07-29 08:14:20 -0700148 gr_instanced::InstancedRendering* instancedRendering() const {
149 SkASSERT(fInstancedRendering);
150 return fInstancedRendering;
151 }
csmartdaltona7f29642016-07-07 08:49:11 -0700152
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000153private:
robertphillips6a186652015-10-20 07:37:58 -0700154 friend class GrDrawingManager; // for resetFlag & TopoSortTraits
robertphillips976f5f02016-06-03 10:59:20 -0700155 friend class GrDrawContextPriv; // for clearStencilClip
robertphillips6a186652015-10-20 07:37:58 -0700156
157 enum Flags {
158 kClosed_Flag = 0x01, //!< This drawTarget can't accept any more batches
159
160 kWasOutput_Flag = 0x02, //!< Flag for topological sorting
161 kTempMark_Flag = 0x04, //!< Flag for topological sorting
162 };
163
164 void setFlag(uint32_t flag) {
165 fFlags |= flag;
166 }
167
168 void resetFlag(uint32_t flag) {
169 fFlags &= ~flag;
170 }
171
172 bool isSetFlag(uint32_t flag) const {
173 return SkToBool(fFlags & flag);
174 }
175
176 struct TopoSortTraits {
177 static void Output(GrDrawTarget* dt, int /* index */) {
178 dt->setFlag(GrDrawTarget::kWasOutput_Flag);
179 }
180 static bool WasOutput(const GrDrawTarget* dt) {
181 return dt->isSetFlag(GrDrawTarget::kWasOutput_Flag);
182 }
183 static void SetTempMark(GrDrawTarget* dt) {
184 dt->setFlag(GrDrawTarget::kTempMark_Flag);
185 }
186 static void ResetTempMark(GrDrawTarget* dt) {
187 dt->resetFlag(GrDrawTarget::kTempMark_Flag);
188 }
189 static bool IsTempMarked(const GrDrawTarget* dt) {
190 return dt->isSetFlag(GrDrawTarget::kTempMark_Flag);
191 }
192 static int NumDependencies(const GrDrawTarget* dt) {
193 return dt->fDependencies.count();
194 }
195 static GrDrawTarget* Dependency(GrDrawTarget* dt, int index) {
196 return dt->fDependencies[index];
197 }
198 };
199
bsalomonfd8d0132016-08-11 11:25:33 -0700200 // Returns the batch that the input batch was combined with or the input batch if it wasn't
201 // combined.
202 GrBatch* recordBatch(GrBatch*, const SkRect& clippedBounds);
bsalomonaecc0182016-03-07 11:50:44 -0800203 void forwardCombine();
bsalomonad792c12015-09-10 11:10:50 -0700204
205 // Makes a copy of the dst if it is necessary for the draw. Returns false if a copy is required
206 // but couldn't be made. Otherwise, returns true. This method needs to be protected because it
207 // needs to be accessed by GLPrograms to setup a correct drawstate
208 bool setupDstReadIfNecessary(const GrPipelineBuilder&,
robertphillips55fdccc2016-06-06 06:16:20 -0700209 GrRenderTarget*,
210 const GrClip&,
211 const GrPipelineOptimizations& optimizations,
212 GrXferProcessor::DstTexture*,
213 const SkRect& batchBounds);
bsalomonad792c12015-09-10 11:10:50 -0700214
robertphillips6a186652015-10-20 07:37:58 -0700215 void addDependency(GrDrawTarget* dependedOn);
216
robertphillips55fdccc2016-06-06 06:16:20 -0700217 // Used only by drawContextPriv.
bsalomon5ea03632015-08-18 10:33:30 -0700218 void clearStencilClip(const SkIRect&, bool insideClip, GrRenderTarget*);
joshualitt6db519c2014-10-29 08:48:18 -0700219
bsalomon6cc90062016-07-08 11:31:22 -0700220 struct RecordedBatch {
221 sk_sp<GrBatch> fBatch;
222 SkRect fClippedBounds;
223 };
224 SkSTArray<256, RecordedBatch, true> fRecordedBatches;
bsalomonfd8d0132016-08-11 11:25:33 -0700225 GrClearBatch* fLastFullClearBatch;
csmartdaltonc6f411e2016-08-05 22:32:12 -0700226 // The context is only in service of the GrClip, remove once it doesn't need this.
csmartdaltona7f29642016-07-07 08:49:11 -0700227 GrContext* fContext;
228 GrGpu* fGpu;
229 GrResourceProvider* fResourceProvider;
230 GrAuditTrail* fAuditTrail;
joshualitt3bdd7dc2014-10-31 08:27:39 -0700231
csmartdaltona7f29642016-07-07 08:49:11 -0700232 SkDEBUGCODE(int fDebugID;)
233 uint32_t fFlags;
robertphillips6a186652015-10-20 07:37:58 -0700234
235 // 'this' drawTarget relies on the output of the drawTargets in 'fDependencies'
csmartdaltona7f29642016-07-07 08:49:11 -0700236 SkTDArray<GrDrawTarget*> fDependencies;
237 GrRenderTarget* fRenderTarget;
robertphillipsa106c622015-10-16 09:07:06 -0700238
csmartdaltona7f29642016-07-07 08:49:11 -0700239 bool fClipBatchToBounds;
240 bool fDrawBatchBounds;
241 int fMaxBatchLookback;
242 int fMaxBatchLookahead;
243
244 SkAutoTDelete<gr_instanced::InstancedRendering> fInstancedRendering;
bsalomon6dea83f2015-12-03 12:58:06 -0800245
bsalomonb3b9aec2015-09-10 11:16:35 -0700246 typedef SkRefCnt INHERITED;
joshualitt6db519c2014-10-29 08:48:18 -0700247};
248
reed@google.comac10a2d2010-12-22 21:39:39 +0000249#endif