blob: 5f91baa33b6e4ffe8711d4a56881c5a254946fa4 [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;
joshualitt44701df2015-02-23 14:44:57 -080034class GrClip;
bsalomon4b91f762015-05-19 09:29:46 -070035class GrCaps;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000036class GrPath;
bsalomon1fcc01c2015-09-09 09:48:06 -070037class GrDrawPathBatchBase;
robertphillips28a838e2016-06-23 14:07:00 -070038class GrPipelineBuilder;
sugoi@google.com12b4e272012-12-06 20:13:11 +000039
bsalomonb3b9aec2015-09-10 11:16:35 -070040class GrDrawTarget final : public SkRefCnt {
bsalomon@google.comf6601872012-08-28 21:11:35 +000041public:
bsalomon69cfe952015-11-30 13:27:47 -080042 /** Options for GrDrawTarget behavior. */
43 struct Options {
bsalomonaecc0182016-03-07 11:50:44 -080044 Options ()
45 : fClipBatchToBounds(false)
46 , fDrawBatchBounds(false)
47 , fMaxBatchLookback(-1)
48 , fMaxBatchLookahead(-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;
bsalomonaecc0182016-03-07 11:50:44 -080052 int fMaxBatchLookahead;
bsalomon69cfe952015-11-30 13:27:47 -080053 };
54
joshualitt086cee12016-01-12 06:45:24 -080055 GrDrawTarget(GrRenderTarget*, GrGpu*, GrResourceProvider*, GrAuditTrail*, const Options&);
bsalomona73239a2015-04-28 13:35:17 -070056
bsalomon512be532015-09-10 10:42:55 -070057 ~GrDrawTarget() override;
reed@google.comac10a2d2010-12-22 21:39:39 +000058
robertphillipsa106c622015-10-16 09:07:06 -070059 void makeClosed() {
60 // We only close drawTargets When MDB is enabled. When MDB is disabled there is only
61 // ever one drawTarget and all calls will be funnelled into it.
62#ifdef ENABLE_MDB
robertphillips6a186652015-10-20 07:37:58 -070063 this->setFlag(kClosed_Flag);
robertphillipsa106c622015-10-16 09:07:06 -070064#endif
bsalomonaecc0182016-03-07 11:50:44 -080065 this->forwardCombine();
robertphillipsa106c622015-10-16 09:07:06 -070066 }
bsalomonaecc0182016-03-07 11:50:44 -080067
robertphillips6a186652015-10-20 07:37:58 -070068 bool isClosed() const { return this->isSetFlag(kClosed_Flag); }
69
robertphillips498d7ac2015-10-30 10:11:30 -070070 // TODO: this entry point is only needed in the non-MDB world. Remove when
71 // we make the switch to MDB
72 void clearRT() { fRenderTarget = nullptr; }
73
robertphillips6a186652015-10-20 07:37:58 -070074 /*
75 * Notify this drawTarget that it relies on the contents of 'dependedOn'
76 */
77 void addDependency(GrSurface* dependedOn);
78
79 /*
80 * Does this drawTarget depend on 'dependedOn'?
81 */
82 bool dependsOn(GrDrawTarget* dependedOn) const {
83 return fDependencies.find(dependedOn) >= 0;
84 }
robertphillipsa106c622015-10-16 09:07:06 -070085
robertphillips4beb5c12015-10-20 07:50:00 -070086 /*
87 * Dump out the drawTarget dependency DAG
88 */
89 SkDEBUGCODE(void dump() const;)
90
reed@google.comac10a2d2010-12-22 21:39:39 +000091 /**
bsalomona73239a2015-04-28 13:35:17 -070092 * Empties the draw buffer of any queued up draws.
93 */
bsalomon512be532015-09-10 10:42:55 -070094 void reset();
bsalomona73239a2015-04-28 13:35:17 -070095
96 /**
robertphillipsa13e2022015-11-11 12:01:09 -080097 * Together these two functions flush all queued up draws to the Gpu.
bsalomona73239a2015-04-28 13:35:17 -070098 */
robertphillipsa13e2022015-11-11 12:01:09 -080099 void prepareBatches(GrBatchFlushState* flushState);
100 void drawBatches(GrBatchFlushState* flushState);
bsalomona73239a2015-04-28 13:35:17 -0700101
102 /**
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000103 * Gets the capabilities of the draw target.
104 */
bsalomonb3b9aec2015-09-10 11:16:35 -0700105 const GrCaps* caps() const { return fGpu->caps(); }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000106
robertphillips976f5f02016-06-03 10:59:20 -0700107 void drawBatch(const GrPipelineBuilder&, GrDrawContext*, const GrClip&, GrDrawBatch*);
joshualitt4d8da812015-01-28 12:53:54 -0800108
robertphillips9199a9f2016-07-13 07:48:43 -0700109 void addBatch(sk_sp<GrBatch>);
110
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000111 /**
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000112 * Draws path into the stencil buffer. The fill must be either even/odd or
113 * winding (not inverse or hairline). It will respect the HW antialias flag
egdaniel8dd688b2015-01-22 10:16:09 -0800114 * on the GrPipelineBuilder (if possible in the 3D API). Note, we will never have an inverse
115 * fill with stencil path
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000116 */
robertphillips976f5f02016-06-03 10:59:20 -0700117 void stencilPath(const GrPipelineBuilder&, GrDrawContext*,
118 const GrClip&, const SkMatrix& viewMatrix,
cdalton862cff32016-05-12 15:09:48 -0700119 const GrPath*, GrPathRendering::FillType);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000120
bsalomon53469832015-08-18 09:20:09 -0700121 /** Discards the contents render target. */
122 void discard(GrRenderTarget*);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000123
124 /**
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000125 * Copies a pixel rectangle from one surface to another. This call may finalize
126 * reserved vertex/index data (as though a draw call was made). The src pixels
127 * copied are specified by srcRect. They are copied to a rect of the same
128 * size in dst with top left at dstPoint. If the src rect is clipped by the
129 * src bounds then pixel values in the dst rect corresponding to area clipped
bsalomon6df86402015-06-01 10:41:49 -0700130 * by the src rect are not overwritten. This method is not guaranteed to succeed
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000131 * depending on the type of surface, configs, etc, and the backend-specific
bsalomon6df86402015-06-01 10:41:49 -0700132 * limitations.
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000133 */
bsalomonb8fea972016-02-16 07:34:17 -0800134 bool copySurface(GrSurface* dst,
bsalomonf90a02b2014-11-26 12:28:00 -0800135 GrSurface* src,
136 const SkIRect& srcRect,
137 const SkIPoint& dstPoint);
robertphillips@google.comff175842012-05-14 19:31:39 +0000138
csmartdaltona7f29642016-07-07 08:49:11 -0700139 /**
140 * Gets the shape rendering object if it is supported on this platform.
141 */
142 gr_instanced::InstancedRendering* instancedRendering() const { return fInstancedRendering; }
143
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000144private:
robertphillips6a186652015-10-20 07:37:58 -0700145 friend class GrDrawingManager; // for resetFlag & TopoSortTraits
robertphillips976f5f02016-06-03 10:59:20 -0700146 friend class GrDrawContextPriv; // for clearStencilClip
robertphillips6a186652015-10-20 07:37:58 -0700147
148 enum Flags {
149 kClosed_Flag = 0x01, //!< This drawTarget can't accept any more batches
150
151 kWasOutput_Flag = 0x02, //!< Flag for topological sorting
152 kTempMark_Flag = 0x04, //!< Flag for topological sorting
153 };
154
155 void setFlag(uint32_t flag) {
156 fFlags |= flag;
157 }
158
159 void resetFlag(uint32_t flag) {
160 fFlags &= ~flag;
161 }
162
163 bool isSetFlag(uint32_t flag) const {
164 return SkToBool(fFlags & flag);
165 }
166
167 struct TopoSortTraits {
168 static void Output(GrDrawTarget* dt, int /* index */) {
169 dt->setFlag(GrDrawTarget::kWasOutput_Flag);
170 }
171 static bool WasOutput(const GrDrawTarget* dt) {
172 return dt->isSetFlag(GrDrawTarget::kWasOutput_Flag);
173 }
174 static void SetTempMark(GrDrawTarget* dt) {
175 dt->setFlag(GrDrawTarget::kTempMark_Flag);
176 }
177 static void ResetTempMark(GrDrawTarget* dt) {
178 dt->resetFlag(GrDrawTarget::kTempMark_Flag);
179 }
180 static bool IsTempMarked(const GrDrawTarget* dt) {
181 return dt->isSetFlag(GrDrawTarget::kTempMark_Flag);
182 }
183 static int NumDependencies(const GrDrawTarget* dt) {
184 return dt->fDependencies.count();
185 }
186 static GrDrawTarget* Dependency(GrDrawTarget* dt, int index) {
187 return dt->fDependencies[index];
188 }
189 };
190
bsalomon6cc90062016-07-08 11:31:22 -0700191 void recordBatch(GrBatch*, const SkRect& clippedBounds);
bsalomonaecc0182016-03-07 11:50:44 -0800192 void forwardCombine();
bsalomonad792c12015-09-10 11:10:50 -0700193
194 // Makes a copy of the dst if it is necessary for the draw. Returns false if a copy is required
195 // but couldn't be made. Otherwise, returns true. This method needs to be protected because it
196 // needs to be accessed by GLPrograms to setup a correct drawstate
197 bool setupDstReadIfNecessary(const GrPipelineBuilder&,
robertphillips55fdccc2016-06-06 06:16:20 -0700198 GrRenderTarget*,
199 const GrClip&,
200 const GrPipelineOptimizations& optimizations,
201 GrXferProcessor::DstTexture*,
202 const SkRect& batchBounds);
bsalomonad792c12015-09-10 11:10:50 -0700203
robertphillips6a186652015-10-20 07:37:58 -0700204 void addDependency(GrDrawTarget* dependedOn);
205
robertphillips55fdccc2016-06-06 06:16:20 -0700206 // Used only by drawContextPriv.
bsalomon5ea03632015-08-18 10:33:30 -0700207 void clearStencilClip(const SkIRect&, bool insideClip, GrRenderTarget*);
joshualitt6db519c2014-10-29 08:48:18 -0700208
bsalomon6cc90062016-07-08 11:31:22 -0700209 struct RecordedBatch {
210 sk_sp<GrBatch> fBatch;
211 SkRect fClippedBounds;
212 };
213 SkSTArray<256, RecordedBatch, true> fRecordedBatches;
bsalomonb3b9aec2015-09-10 11:16:35 -0700214 // The context is only in service of the clip mask manager, remove once CMM doesn't need this.
csmartdaltona7f29642016-07-07 08:49:11 -0700215 GrContext* fContext;
216 GrGpu* fGpu;
217 GrResourceProvider* fResourceProvider;
218 GrAuditTrail* fAuditTrail;
joshualitt3bdd7dc2014-10-31 08:27:39 -0700219
csmartdaltona7f29642016-07-07 08:49:11 -0700220 SkDEBUGCODE(int fDebugID;)
221 uint32_t fFlags;
robertphillips6a186652015-10-20 07:37:58 -0700222
223 // 'this' drawTarget relies on the output of the drawTargets in 'fDependencies'
csmartdaltona7f29642016-07-07 08:49:11 -0700224 SkTDArray<GrDrawTarget*> fDependencies;
225 GrRenderTarget* fRenderTarget;
robertphillipsa106c622015-10-16 09:07:06 -0700226
csmartdaltona7f29642016-07-07 08:49:11 -0700227 bool fClipBatchToBounds;
228 bool fDrawBatchBounds;
229 int fMaxBatchLookback;
230 int fMaxBatchLookahead;
231
232 SkAutoTDelete<gr_instanced::InstancedRendering> fInstancedRendering;
bsalomon6dea83f2015-12-03 12:58:06 -0800233
bsalomonb3b9aec2015-09-10 11:16:35 -0700234 typedef SkRefCnt INHERITED;
joshualitt6db519c2014-10-29 08:48:18 -0700235};
236
reed@google.comac10a2d2010-12-22 21:39:39 +0000237#endif