reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 8 | #ifndef GrDrawTarget_DEFINED |
| 9 | #define GrDrawTarget_DEFINED |
| 10 | |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 11 | #include "GrClip.h" |
commit-bot@chromium.org | 2a05de0 | 2014-03-25 15:17:32 +0000 | [diff] [blame] | 12 | #include "GrContext.h" |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 13 | #include "GrPathProcessor.h" |
| 14 | #include "GrPrimitiveProcessor.h" |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 15 | #include "GrPathRendering.h" |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 16 | #include "GrXferProcessor.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 17 | |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 18 | #include "batches/GrDrawBatch.h" |
| 19 | |
bsalomon@google.com | 8d67c07 | 2012-12-13 20:38:14 +0000 | [diff] [blame] | 20 | #include "SkClipStack.h" |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 21 | #include "SkMatrix.h" |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 22 | #include "SkPath.h" |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 23 | #include "SkStringUtils.h" |
commit-bot@chromium.org | 9b62aa1 | 2014-03-25 11:59:40 +0000 | [diff] [blame] | 24 | #include "SkStrokeRec.h" |
robertphillips@google.com | a2d7148 | 2012-08-01 20:08:47 +0000 | [diff] [blame] | 25 | #include "SkTArray.h" |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 26 | #include "SkTLazy.h" |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 27 | #include "SkTypes.h" |
bsalomon@google.com | 8d67c07 | 2012-12-13 20:38:14 +0000 | [diff] [blame] | 28 | #include "SkXfermode.h" |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 29 | |
robertphillips | 498d7ac | 2015-10-30 10:11:30 -0700 | [diff] [blame] | 30 | //#define ENABLE_MDB 1 |
| 31 | |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 32 | class GrAuditTrail; |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 33 | class GrBatch; |
bsalomon | fd8d013 | 2016-08-11 11:25:33 -0700 | [diff] [blame] | 34 | class GrClearBatch; |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 35 | class GrClip; |
bsalomon | 4b91f76 | 2015-05-19 09:29:46 -0700 | [diff] [blame] | 36 | class GrCaps; |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 37 | class GrPath; |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 38 | class GrDrawPathBatchBase; |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 39 | class GrPipelineBuilder; |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 40 | |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 41 | class GrDrawTarget final : public SkRefCnt { |
bsalomon@google.com | f660187 | 2012-08-28 21:11:35 +0000 | [diff] [blame] | 42 | public: |
bsalomon | 69cfe95 | 2015-11-30 13:27:47 -0800 | [diff] [blame] | 43 | /** Options for GrDrawTarget behavior. */ |
| 44 | struct Options { |
bsalomon | aecc018 | 2016-03-07 11:50:44 -0800 | [diff] [blame] | 45 | Options () |
| 46 | : fClipBatchToBounds(false) |
| 47 | , fDrawBatchBounds(false) |
| 48 | , fMaxBatchLookback(-1) |
| 49 | , fMaxBatchLookahead(-1) {} |
bsalomon | 69cfe95 | 2015-11-30 13:27:47 -0800 | [diff] [blame] | 50 | bool fClipBatchToBounds; |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 51 | bool fDrawBatchBounds; |
bsalomon | 489147c | 2015-12-14 12:13:09 -0800 | [diff] [blame] | 52 | int fMaxBatchLookback; |
bsalomon | aecc018 | 2016-03-07 11:50:44 -0800 | [diff] [blame] | 53 | int fMaxBatchLookahead; |
bsalomon | 69cfe95 | 2015-11-30 13:27:47 -0800 | [diff] [blame] | 54 | }; |
| 55 | |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 56 | GrDrawTarget(GrRenderTarget*, GrGpu*, GrResourceProvider*, GrAuditTrail*, const Options&); |
bsalomon | a73239a | 2015-04-28 13:35:17 -0700 | [diff] [blame] | 57 | |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 58 | ~GrDrawTarget() override; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 59 | |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 60 | void makeClosed() { |
bsalomon | fd8d013 | 2016-08-11 11:25:33 -0700 | [diff] [blame] | 61 | fLastFullClearBatch = nullptr; |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 62 | // 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 |
robertphillips | 6a18665 | 2015-10-20 07:37:58 -0700 | [diff] [blame] | 65 | this->setFlag(kClosed_Flag); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 66 | #endif |
bsalomon | aecc018 | 2016-03-07 11:50:44 -0800 | [diff] [blame] | 67 | this->forwardCombine(); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 68 | } |
bsalomon | aecc018 | 2016-03-07 11:50:44 -0800 | [diff] [blame] | 69 | |
robertphillips | 6a18665 | 2015-10-20 07:37:58 -0700 | [diff] [blame] | 70 | bool isClosed() const { return this->isSetFlag(kClosed_Flag); } |
| 71 | |
robertphillips | 498d7ac | 2015-10-30 10:11:30 -0700 | [diff] [blame] | 72 | // 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 | |
robertphillips | 6a18665 | 2015-10-20 07:37:58 -0700 | [diff] [blame] | 76 | /* |
| 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 | } |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 87 | |
robertphillips | 4beb5c1 | 2015-10-20 07:50:00 -0700 | [diff] [blame] | 88 | /* |
| 89 | * Dump out the drawTarget dependency DAG |
| 90 | */ |
| 91 | SkDEBUGCODE(void dump() const;) |
| 92 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 93 | /** |
bsalomon | a73239a | 2015-04-28 13:35:17 -0700 | [diff] [blame] | 94 | * Empties the draw buffer of any queued up draws. |
| 95 | */ |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 96 | void reset(); |
bsalomon | a73239a | 2015-04-28 13:35:17 -0700 | [diff] [blame] | 97 | |
| 98 | /** |
bsalomon | dc43898 | 2016-08-31 11:53:49 -0700 | [diff] [blame^] | 99 | * 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. |
bsalomon | a73239a | 2015-04-28 13:35:17 -0700 | [diff] [blame] | 101 | */ |
robertphillips | a13e202 | 2015-11-11 12:01:09 -0800 | [diff] [blame] | 102 | void prepareBatches(GrBatchFlushState* flushState); |
bsalomon | dc43898 | 2016-08-31 11:53:49 -0700 | [diff] [blame^] | 103 | bool drawBatches(GrBatchFlushState* flushState); |
bsalomon | a73239a | 2015-04-28 13:35:17 -0700 | [diff] [blame] | 104 | |
| 105 | /** |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 106 | * Gets the capabilities of the draw target. |
| 107 | */ |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 108 | const GrCaps* caps() const { return fGpu->caps(); } |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 109 | |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 110 | void drawBatch(const GrPipelineBuilder&, GrDrawContext*, const GrClip&, GrDrawBatch*); |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 111 | |
robertphillips | 9199a9f | 2016-07-13 07:48:43 -0700 | [diff] [blame] | 112 | void addBatch(sk_sp<GrBatch>); |
| 113 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 114 | /** |
csmartdalton | 5c6fc4f | 2016-08-12 15:11:51 -0700 | [diff] [blame] | 115 | * 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.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 120 | */ |
robertphillips | 59cf61a | 2016-07-13 09:18:21 -0700 | [diff] [blame] | 121 | void stencilPath(GrDrawContext*, |
| 122 | const GrClip&, |
robertphillips | 59cf61a | 2016-07-13 09:18:21 -0700 | [diff] [blame] | 123 | bool useHWAA, |
| 124 | const SkMatrix& viewMatrix, |
| 125 | const GrPath*); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 126 | |
bsalomon | 9f129de | 2016-08-10 16:31:05 -0700 | [diff] [blame] | 127 | /** Clears the entire render target */ |
| 128 | void fullClear(GrRenderTarget*, GrColor color); |
| 129 | |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 130 | /** Discards the contents render target. */ |
| 131 | void discard(GrRenderTarget*); |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 132 | |
| 133 | /** |
bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 134 | * 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 |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 139 | * by the src rect are not overwritten. This method is not guaranteed to succeed |
bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 140 | * depending on the type of surface, configs, etc, and the backend-specific |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 141 | * limitations. |
bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 142 | */ |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 143 | bool copySurface(GrSurface* dst, |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 144 | GrSurface* src, |
| 145 | const SkIRect& srcRect, |
| 146 | const SkIPoint& dstPoint); |
robertphillips@google.com | ff17584 | 2012-05-14 19:31:39 +0000 | [diff] [blame] | 147 | |
csmartdalton | e0d3629 | 2016-07-29 08:14:20 -0700 | [diff] [blame] | 148 | gr_instanced::InstancedRendering* instancedRendering() const { |
| 149 | SkASSERT(fInstancedRendering); |
| 150 | return fInstancedRendering; |
| 151 | } |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 152 | |
bsalomon@google.com | 02ddc8b | 2013-01-28 15:35:28 +0000 | [diff] [blame] | 153 | private: |
robertphillips | 6a18665 | 2015-10-20 07:37:58 -0700 | [diff] [blame] | 154 | friend class GrDrawingManager; // for resetFlag & TopoSortTraits |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 155 | friend class GrDrawContextPriv; // for clearStencilClip |
robertphillips | 6a18665 | 2015-10-20 07:37:58 -0700 | [diff] [blame] | 156 | |
| 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 | |
bsalomon | fd8d013 | 2016-08-11 11:25:33 -0700 | [diff] [blame] | 200 | // 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); |
bsalomon | aecc018 | 2016-03-07 11:50:44 -0800 | [diff] [blame] | 203 | void forwardCombine(); |
bsalomon | ad792c1 | 2015-09-10 11:10:50 -0700 | [diff] [blame] | 204 | |
| 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&, |
robertphillips | 55fdccc | 2016-06-06 06:16:20 -0700 | [diff] [blame] | 209 | GrRenderTarget*, |
| 210 | const GrClip&, |
| 211 | const GrPipelineOptimizations& optimizations, |
| 212 | GrXferProcessor::DstTexture*, |
| 213 | const SkRect& batchBounds); |
bsalomon | ad792c1 | 2015-09-10 11:10:50 -0700 | [diff] [blame] | 214 | |
robertphillips | 6a18665 | 2015-10-20 07:37:58 -0700 | [diff] [blame] | 215 | void addDependency(GrDrawTarget* dependedOn); |
| 216 | |
robertphillips | 55fdccc | 2016-06-06 06:16:20 -0700 | [diff] [blame] | 217 | // Used only by drawContextPriv. |
bsalomon | 5ea0363 | 2015-08-18 10:33:30 -0700 | [diff] [blame] | 218 | void clearStencilClip(const SkIRect&, bool insideClip, GrRenderTarget*); |
joshualitt | 6db519c | 2014-10-29 08:48:18 -0700 | [diff] [blame] | 219 | |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 220 | struct RecordedBatch { |
| 221 | sk_sp<GrBatch> fBatch; |
| 222 | SkRect fClippedBounds; |
| 223 | }; |
| 224 | SkSTArray<256, RecordedBatch, true> fRecordedBatches; |
bsalomon | fd8d013 | 2016-08-11 11:25:33 -0700 | [diff] [blame] | 225 | GrClearBatch* fLastFullClearBatch; |
csmartdalton | c6f411e | 2016-08-05 22:32:12 -0700 | [diff] [blame] | 226 | // The context is only in service of the GrClip, remove once it doesn't need this. |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 227 | GrContext* fContext; |
| 228 | GrGpu* fGpu; |
| 229 | GrResourceProvider* fResourceProvider; |
| 230 | GrAuditTrail* fAuditTrail; |
joshualitt | 3bdd7dc | 2014-10-31 08:27:39 -0700 | [diff] [blame] | 231 | |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 232 | SkDEBUGCODE(int fDebugID;) |
| 233 | uint32_t fFlags; |
robertphillips | 6a18665 | 2015-10-20 07:37:58 -0700 | [diff] [blame] | 234 | |
| 235 | // 'this' drawTarget relies on the output of the drawTargets in 'fDependencies' |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 236 | SkTDArray<GrDrawTarget*> fDependencies; |
| 237 | GrRenderTarget* fRenderTarget; |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 238 | |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 239 | bool fClipBatchToBounds; |
| 240 | bool fDrawBatchBounds; |
| 241 | int fMaxBatchLookback; |
| 242 | int fMaxBatchLookahead; |
| 243 | |
| 244 | SkAutoTDelete<gr_instanced::InstancedRendering> fInstancedRendering; |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 245 | |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 246 | typedef SkRefCnt INHERITED; |
joshualitt | 6db519c | 2014-10-29 08:48:18 -0700 | [diff] [blame] | 247 | }; |
| 248 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 249 | #endif |