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 | |
joshualitt | c2893c5 | 2015-01-28 06:54:30 -0800 | [diff] [blame] | 8 | #include "GrDrawTarget.h" |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 9 | |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 10 | #include "GrAuditTrail.h" |
bsalomon | eb1cb5c | 2015-05-22 08:01:09 -0700 | [diff] [blame] | 11 | #include "GrCaps.h" |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 12 | #include "GrDrawContext.h" |
bsalomon | 4061b12 | 2015-05-29 10:26:19 -0700 | [diff] [blame] | 13 | #include "GrGpu.h" |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 14 | #include "GrGpuCommandBuffer.h" |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 15 | #include "GrPath.h" |
egdaniel | e36914c | 2015-02-13 09:00:33 -0800 | [diff] [blame] | 16 | #include "GrPipeline.h" |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 17 | #include "GrMemoryPool.h" |
robertphillips | 5fa7f30 | 2016-07-21 09:21:04 -0700 | [diff] [blame] | 18 | #include "GrPipelineBuilder.h" |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 19 | #include "GrRenderTarget.h" |
bsalomon | 4061b12 | 2015-05-29 10:26:19 -0700 | [diff] [blame] | 20 | #include "GrResourceProvider.h" |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 21 | #include "GrRenderTargetPriv.h" |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 22 | #include "GrStencilAttachment.h" |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 23 | #include "GrSurfacePriv.h" |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 24 | #include "GrTexture.h" |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 25 | #include "gl/GrGLRenderTarget.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 26 | |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 27 | #include "SkStrokeRec.h" |
| 28 | |
bsalomon | 9f129de | 2016-08-10 16:31:05 -0700 | [diff] [blame] | 29 | #include "batches/GrClearBatch.h" |
robertphillips | 9199a9f | 2016-07-13 07:48:43 -0700 | [diff] [blame] | 30 | #include "batches/GrClearStencilClipBatch.h" |
bsalomon | 872062c | 2015-08-18 12:12:35 -0700 | [diff] [blame] | 31 | #include "batches/GrCopySurfaceBatch.h" |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 32 | #include "batches/GrDiscardBatch.h" |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 33 | #include "batches/GrDrawBatch.h" |
bsalomon | add79ef | 2015-08-19 13:26:49 -0700 | [diff] [blame] | 34 | #include "batches/GrDrawPathBatch.h" |
joshualitt | ecd1a69 | 2015-08-10 10:08:26 -0700 | [diff] [blame] | 35 | #include "batches/GrRectBatchFactory.h" |
bsalomon | a44919e | 2015-08-18 13:28:19 -0700 | [diff] [blame] | 36 | #include "batches/GrStencilPathBatch.h" |
joshualitt | 7441782 | 2015-08-07 11:42:16 -0700 | [diff] [blame] | 37 | |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 38 | #include "instanced/InstancedRendering.h" |
| 39 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 40 | //////////////////////////////////////////////////////////////////////////////// |
| 41 | |
bsalomon | 489147c | 2015-12-14 12:13:09 -0800 | [diff] [blame] | 42 | // Experimentally we have found that most batching occurs within the first 10 comparisons. |
bsalomon | aecc018 | 2016-03-07 11:50:44 -0800 | [diff] [blame] | 43 | static const int kDefaultMaxBatchLookback = 10; |
| 44 | static const int kDefaultMaxBatchLookahead = 10; |
bsalomon | 489147c | 2015-12-14 12:13:09 -0800 | [diff] [blame] | 45 | |
bsalomon | 69cfe95 | 2015-11-30 13:27:47 -0800 | [diff] [blame] | 46 | GrDrawTarget::GrDrawTarget(GrRenderTarget* rt, GrGpu* gpu, GrResourceProvider* resourceProvider, |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 47 | GrAuditTrail* auditTrail, const Options& options) |
bsalomon | fd8d013 | 2016-08-11 11:25:33 -0700 | [diff] [blame^] | 48 | : fLastFullClearBatch(nullptr) |
| 49 | , fGpu(SkRef(gpu)) |
bsalomon | 4061b12 | 2015-05-29 10:26:19 -0700 | [diff] [blame] | 50 | , fResourceProvider(resourceProvider) |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 51 | , fAuditTrail(auditTrail) |
bsalomon | 648c696 | 2015-10-23 09:06:59 -0700 | [diff] [blame] | 52 | , fFlags(0) |
csmartdalton | e0d3629 | 2016-07-29 08:14:20 -0700 | [diff] [blame] | 53 | , fRenderTarget(rt) { |
csmartdalton | c6f411e | 2016-08-05 22:32:12 -0700 | [diff] [blame] | 54 | // TODO: Stop extracting the context (currently needed by GrClip) |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 55 | fContext = fGpu->getContext(); |
robertphillips | 4beb5c1 | 2015-10-20 07:50:00 -0700 | [diff] [blame] | 56 | |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 57 | fClipBatchToBounds = options.fClipBatchToBounds; |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 58 | fDrawBatchBounds = options.fDrawBatchBounds; |
bsalomon | 489147c | 2015-12-14 12:13:09 -0800 | [diff] [blame] | 59 | fMaxBatchLookback = (options.fMaxBatchLookback < 0) ? kDefaultMaxBatchLookback : |
| 60 | options.fMaxBatchLookback; |
bsalomon | aecc018 | 2016-03-07 11:50:44 -0800 | [diff] [blame] | 61 | fMaxBatchLookahead = (options.fMaxBatchLookahead < 0) ? kDefaultMaxBatchLookahead : |
| 62 | options.fMaxBatchLookahead; |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 63 | |
csmartdalton | e0d3629 | 2016-07-29 08:14:20 -0700 | [diff] [blame] | 64 | if (GrCaps::InstancedSupport::kNone != this->caps()->instancedSupport()) { |
| 65 | fInstancedRendering.reset(fGpu->createInstancedRendering()); |
| 66 | } |
| 67 | |
robertphillips | 0dfa62c | 2015-11-16 06:23:31 -0800 | [diff] [blame] | 68 | rt->setLastDrawTarget(this); |
| 69 | |
robertphillips | 4beb5c1 | 2015-10-20 07:50:00 -0700 | [diff] [blame] | 70 | #ifdef SK_DEBUG |
| 71 | static int debugID = 0; |
| 72 | fDebugID = debugID++; |
| 73 | #endif |
bsalomon | 4061b12 | 2015-05-29 10:26:19 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | GrDrawTarget::~GrDrawTarget() { |
robertphillips | 498d7ac | 2015-10-30 10:11:30 -0700 | [diff] [blame] | 77 | if (fRenderTarget && this == fRenderTarget->getLastDrawTarget()) { |
| 78 | fRenderTarget->setLastDrawTarget(nullptr); |
| 79 | } |
| 80 | |
bsalomon | 4061b12 | 2015-05-29 10:26:19 -0700 | [diff] [blame] | 81 | fGpu->unref(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | //////////////////////////////////////////////////////////////////////////////// |
| 85 | |
robertphillips | 6a18665 | 2015-10-20 07:37:58 -0700 | [diff] [blame] | 86 | // Add a GrDrawTarget-based dependency |
| 87 | void GrDrawTarget::addDependency(GrDrawTarget* dependedOn) { |
| 88 | SkASSERT(!dependedOn->dependsOn(this)); // loops are bad |
| 89 | |
| 90 | if (this->dependsOn(dependedOn)) { |
| 91 | return; // don't add duplicate dependencies |
| 92 | } |
| 93 | |
| 94 | *fDependencies.push() = dependedOn; |
| 95 | } |
| 96 | |
| 97 | // Convert from a GrSurface-based dependency to a GrDrawTarget one |
| 98 | void GrDrawTarget::addDependency(GrSurface* dependedOn) { |
| 99 | if (dependedOn->asRenderTarget() && dependedOn->asRenderTarget()->getLastDrawTarget()) { |
| 100 | // If it is still receiving dependencies, this DT shouldn't be closed |
| 101 | SkASSERT(!this->isClosed()); |
| 102 | |
| 103 | GrDrawTarget* dt = dependedOn->asRenderTarget()->getLastDrawTarget(); |
| 104 | if (dt == this) { |
| 105 | // self-read - presumably for dst reads |
| 106 | } else { |
| 107 | this->addDependency(dt); |
| 108 | |
| 109 | // Can't make it closed in the self-read case |
| 110 | dt->makeClosed(); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
robertphillips | 4beb5c1 | 2015-10-20 07:50:00 -0700 | [diff] [blame] | 115 | #ifdef SK_DEBUG |
| 116 | void GrDrawTarget::dump() const { |
| 117 | SkDebugf("--------------------------------------------------------------\n"); |
robertphillips | e004bfc | 2015-11-16 09:06:59 -0800 | [diff] [blame] | 118 | SkDebugf("node: %d -> RT: %d\n", fDebugID, fRenderTarget ? fRenderTarget->getUniqueID() : -1); |
robertphillips | 4beb5c1 | 2015-10-20 07:50:00 -0700 | [diff] [blame] | 119 | SkDebugf("relies On (%d): ", fDependencies.count()); |
| 120 | for (int i = 0; i < fDependencies.count(); ++i) { |
| 121 | SkDebugf("%d, ", fDependencies[i]->fDebugID); |
| 122 | } |
| 123 | SkDebugf("\n"); |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 124 | SkDebugf("batches (%d):\n", fRecordedBatches.count()); |
| 125 | for (int i = 0; i < fRecordedBatches.count(); ++i) { |
robertphillips | 4beb5c1 | 2015-10-20 07:50:00 -0700 | [diff] [blame] | 126 | SkDebugf("*******************************\n"); |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 127 | if (!fRecordedBatches[i].fBatch) { |
bsalomon | aecc018 | 2016-03-07 11:50:44 -0800 | [diff] [blame] | 128 | SkDebugf("%d: <combined forward>\n", i); |
| 129 | } else { |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 130 | SkDebugf("%d: %s\n", i, fRecordedBatches[i].fBatch->name()); |
| 131 | SkString str = fRecordedBatches[i].fBatch->dumpInfo(); |
bsalomon | aecc018 | 2016-03-07 11:50:44 -0800 | [diff] [blame] | 132 | SkDebugf("%s\n", str.c_str()); |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 133 | const SkRect& clippedBounds = fRecordedBatches[i].fClippedBounds; |
| 134 | SkDebugf("ClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", |
| 135 | clippedBounds.fLeft, clippedBounds.fTop, clippedBounds.fRight, |
| 136 | clippedBounds.fBottom); |
bsalomon | aecc018 | 2016-03-07 11:50:44 -0800 | [diff] [blame] | 137 | } |
robertphillips | 4beb5c1 | 2015-10-20 07:50:00 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | #endif |
| 141 | |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 142 | bool GrDrawTarget::setupDstReadIfNecessary(const GrPipelineBuilder& pipelineBuilder, |
robertphillips | 55fdccc | 2016-06-06 06:16:20 -0700 | [diff] [blame] | 143 | GrRenderTarget* rt, |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 144 | const GrClip& clip, |
ethannicholas | de4166a | 2015-11-30 08:57:38 -0800 | [diff] [blame] | 145 | const GrPipelineOptimizations& optimizations, |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 146 | GrXferProcessor::DstTexture* dstTexture, |
bsalomon | ad792c1 | 2015-09-10 11:10:50 -0700 | [diff] [blame] | 147 | const SkRect& batchBounds) { |
| 148 | SkRect bounds = batchBounds; |
| 149 | bounds.outset(0.5f, 0.5f); |
| 150 | |
ethannicholas | de4166a | 2015-11-30 08:57:38 -0800 | [diff] [blame] | 151 | if (!pipelineBuilder.willXPNeedDstTexture(*this->caps(), optimizations)) { |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 152 | return true; |
| 153 | } |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 154 | |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 155 | if (this->caps()->textureBarrierSupport()) { |
| 156 | if (GrTexture* rtTex = rt->asTexture()) { |
bsalomon | dc47ff7 | 2015-05-26 12:16:59 -0700 | [diff] [blame] | 157 | // The render target is a texture, so we can read from it directly in the shader. The XP |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 158 | // will be responsible to detect this situation and request a texture barrier. |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 159 | dstTexture->setTexture(rtTex); |
| 160 | dstTexture->setOffset(0, 0); |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 161 | return true; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | SkIRect copyRect; |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 166 | clip.getConservativeBounds(rt->width(), rt->height(), ©Rect); |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 167 | |
bsalomon | ad792c1 | 2015-09-10 11:10:50 -0700 | [diff] [blame] | 168 | SkIRect drawIBounds; |
| 169 | bounds.roundOut(&drawIBounds); |
| 170 | if (!copyRect.intersect(drawIBounds)) { |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 171 | #ifdef SK_DEBUG |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 172 | GrCapsDebugf(this->caps(), "Missed an early reject. " |
| 173 | "Bailing on draw from setupDstReadIfNecessary.\n"); |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 174 | #endif |
bsalomon | ad792c1 | 2015-09-10 11:10:50 -0700 | [diff] [blame] | 175 | return false; |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 176 | } |
skia.committer@gmail.com | 05a2ee0 | 2013-04-02 07:01:34 +0000 | [diff] [blame] | 177 | |
commit-bot@chromium.org | 63150af | 2013-04-11 22:00:22 +0000 | [diff] [blame] | 178 | // MSAA consideration: When there is support for reading MSAA samples in the shader we could |
| 179 | // have per-sample dst values by making the copy multisampled. |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 180 | GrSurfaceDesc desc; |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 181 | if (!fGpu->initCopySurfaceDstDesc(rt, &desc)) { |
bsalomon | a73239a | 2015-04-28 13:35:17 -0700 | [diff] [blame] | 182 | desc.fOrigin = kDefault_GrSurfaceOrigin; |
| 183 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 184 | desc.fConfig = rt->config(); |
| 185 | } |
| 186 | |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 187 | desc.fWidth = copyRect.width(); |
| 188 | desc.fHeight = copyRect.height(); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 189 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 190 | static const uint32_t kFlags = 0; |
| 191 | SkAutoTUnref<GrTexture> copy(fResourceProvider->createApproxTexture(desc, kFlags)); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 192 | |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 193 | if (!copy) { |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 194 | SkDebugf("Failed to create temporary copy of destination texture.\n"); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 195 | return false; |
| 196 | } |
bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 197 | SkIPoint dstPoint = {0, 0}; |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 198 | this->copySurface(copy, rt, copyRect, dstPoint); |
| 199 | dstTexture->setTexture(copy); |
| 200 | dstTexture->setOffset(copyRect.fLeft, copyRect.fTop); |
| 201 | return true; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 202 | } |
| 203 | |
robertphillips | a13e202 | 2015-11-11 12:01:09 -0800 | [diff] [blame] | 204 | void GrDrawTarget::prepareBatches(GrBatchFlushState* flushState) { |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 205 | // Semi-usually the drawTargets are already closed at this point, but sometimes Ganesh |
| 206 | // needs to flush mid-draw. In that case, the SkGpuDevice's drawTargets won't be closed |
| 207 | // but need to be flushed anyway. Closing such drawTargets here will mean new |
| 208 | // drawTargets will be created to replace them if the SkGpuDevice(s) write to them again. |
| 209 | this->makeClosed(); |
| 210 | |
robertphillips | 498d7ac | 2015-10-30 10:11:30 -0700 | [diff] [blame] | 211 | // Loop over the batches that haven't yet generated their geometry |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 212 | for (int i = 0; i < fRecordedBatches.count(); ++i) { |
| 213 | if (fRecordedBatches[i].fBatch) { |
| 214 | fRecordedBatches[i].fBatch->prepare(flushState); |
bsalomon | aecc018 | 2016-03-07 11:50:44 -0800 | [diff] [blame] | 215 | } |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 216 | } |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 217 | |
| 218 | if (fInstancedRendering) { |
| 219 | fInstancedRendering->beginFlush(flushState->resourceProvider()); |
| 220 | } |
robertphillips | a13e202 | 2015-11-11 12:01:09 -0800 | [diff] [blame] | 221 | } |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 222 | |
robertphillips | a13e202 | 2015-11-11 12:01:09 -0800 | [diff] [blame] | 223 | void GrDrawTarget::drawBatches(GrBatchFlushState* flushState) { |
egdaniel | b4021cf | 2016-07-28 08:53:07 -0700 | [diff] [blame] | 224 | if (0 == fRecordedBatches.count()) { |
| 225 | return; |
| 226 | } |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 227 | // Draw all the generated geometry. |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 228 | SkRandom random; |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 229 | GrRenderTarget* currentRT = nullptr; |
| 230 | SkAutoTDelete<GrGpuCommandBuffer> commandBuffer; |
egdaniel | e7d1b24 | 2016-07-01 08:06:45 -0700 | [diff] [blame] | 231 | SkRect bounds = SkRect::MakeEmpty(); |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 232 | for (int i = 0; i < fRecordedBatches.count(); ++i) { |
| 233 | if (!fRecordedBatches[i].fBatch) { |
bsalomon | aecc018 | 2016-03-07 11:50:44 -0800 | [diff] [blame] | 234 | continue; |
| 235 | } |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 236 | if (fRecordedBatches[i].fBatch->renderTarget() != currentRT) { |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 237 | if (commandBuffer) { |
| 238 | commandBuffer->end(); |
egdaniel | e7d1b24 | 2016-07-01 08:06:45 -0700 | [diff] [blame] | 239 | if (bounds.intersect(0, 0, |
| 240 | SkIntToScalar(currentRT->width()), |
| 241 | SkIntToScalar(currentRT->height()))) { |
| 242 | SkIRect iBounds; |
| 243 | bounds.roundOut(&iBounds); |
| 244 | commandBuffer->submit(iBounds); |
| 245 | } |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 246 | commandBuffer.reset(); |
| 247 | } |
egdaniel | e7d1b24 | 2016-07-01 08:06:45 -0700 | [diff] [blame] | 248 | bounds.setEmpty(); |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 249 | currentRT = fRecordedBatches[i].fBatch->renderTarget(); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 250 | if (currentRT) { |
| 251 | static const GrGpuCommandBuffer::LoadAndStoreInfo kBasicLoadStoreInfo |
| 252 | { GrGpuCommandBuffer::LoadOp::kLoad,GrGpuCommandBuffer::StoreOp::kStore, |
| 253 | GrColor_ILLEGAL }; |
| 254 | commandBuffer.reset(fGpu->createCommandBuffer(currentRT, |
| 255 | kBasicLoadStoreInfo, // Color |
| 256 | kBasicLoadStoreInfo)); // Stencil |
| 257 | } |
| 258 | flushState->setCommandBuffer(commandBuffer); |
| 259 | } |
egdaniel | e7d1b24 | 2016-07-01 08:06:45 -0700 | [diff] [blame] | 260 | if (commandBuffer) { |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 261 | bounds.join(fRecordedBatches[i].fClippedBounds); |
egdaniel | e7d1b24 | 2016-07-01 08:06:45 -0700 | [diff] [blame] | 262 | } |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 263 | if (fDrawBatchBounds) { |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 264 | const SkRect& bounds = fRecordedBatches[i].fClippedBounds; |
| 265 | SkIRect ibounds; |
| 266 | bounds.roundOut(&ibounds); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 267 | // In multi-draw buffer all the batches use the same render target and we won't need to |
| 268 | // get the batchs bounds. |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 269 | if (GrRenderTarget* rt = fRecordedBatches[i].fBatch->renderTarget()) { |
| 270 | fGpu->drawDebugWireRect(rt, ibounds, 0xFF000000 | random.nextU()); |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 271 | } |
| 272 | } |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 273 | fRecordedBatches[i].fBatch->draw(flushState); |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 274 | } |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 275 | if (commandBuffer) { |
| 276 | commandBuffer->end(); |
egdaniel | e7d1b24 | 2016-07-01 08:06:45 -0700 | [diff] [blame] | 277 | if (bounds.intersect(0, 0, |
| 278 | SkIntToScalar(currentRT->width()), |
| 279 | SkIntToScalar(currentRT->height()))) { |
| 280 | SkIRect iBounds; |
| 281 | bounds.roundOut(&iBounds); |
| 282 | commandBuffer->submit(iBounds); |
| 283 | } |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 284 | flushState->setCommandBuffer(nullptr); |
| 285 | } |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 286 | |
jvanverth | d2d2eb9 | 2016-02-17 14:04:46 -0800 | [diff] [blame] | 287 | fGpu->finishDrawTarget(); |
bsalomon | a73239a | 2015-04-28 13:35:17 -0700 | [diff] [blame] | 288 | } |
| 289 | |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 290 | void GrDrawTarget::reset() { |
bsalomon | fd8d013 | 2016-08-11 11:25:33 -0700 | [diff] [blame^] | 291 | fLastFullClearBatch = nullptr; |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 292 | fRecordedBatches.reset(); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 293 | if (fInstancedRendering) { |
| 294 | fInstancedRendering->endFlush(); |
| 295 | } |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 296 | } |
| 297 | |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 298 | static void batch_bounds(SkRect* bounds, const GrBatch* batch) { |
| 299 | *bounds = batch->bounds(); |
| 300 | if (batch->hasZeroArea()) { |
| 301 | if (batch->hasAABloat()) { |
| 302 | bounds->outset(0.5f, 0.5f); |
| 303 | } else { |
| 304 | // We don't know which way the particular GPU will snap lines or points at integer |
| 305 | // coords. So we ensure that the bounds is large enough for either snap. |
| 306 | SkRect before = *bounds; |
| 307 | bounds->roundOut(bounds); |
| 308 | if (bounds->fLeft == before.fLeft) { |
| 309 | bounds->fLeft -= 1; |
| 310 | } |
| 311 | if (bounds->fTop == before.fTop) { |
| 312 | bounds->fTop -= 1; |
| 313 | } |
| 314 | if (bounds->fRight == before.fRight) { |
| 315 | bounds->fRight += 1; |
| 316 | } |
| 317 | if (bounds->fBottom == before.fBottom) { |
| 318 | bounds->fBottom += 1; |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 324 | static inline bool intersect(SkRect* out, const SkRect& a, const SkRect& b) { |
| 325 | SkASSERT(a.fLeft <= a.fRight && a.fTop <= a.fBottom); |
| 326 | SkASSERT(b.fLeft <= b.fRight && b.fTop <= b.fBottom); |
| 327 | out->fLeft = SkTMax(a.fLeft, b.fLeft); |
| 328 | out->fTop = SkTMax(a.fTop, b.fTop); |
| 329 | out->fRight = SkTMin(a.fRight, b.fRight); |
| 330 | out->fBottom = SkTMin(a.fBottom, b.fBottom); |
| 331 | return (out->fLeft <= out->fRight && out->fTop <= out->fBottom); |
| 332 | } |
| 333 | |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 334 | void GrDrawTarget::drawBatch(const GrPipelineBuilder& pipelineBuilder, |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 335 | GrDrawContext* drawContext, |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 336 | const GrClip& clip, |
| 337 | GrDrawBatch* batch) { |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 338 | // Setup clip |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 339 | GrAppliedClip appliedClip; |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 340 | SkRect bounds; |
| 341 | batch_bounds(&bounds, batch); |
robertphillips | 59cf61a | 2016-07-13 09:18:21 -0700 | [diff] [blame] | 342 | if (!clip.apply(fContext, drawContext, &bounds, |
| 343 | pipelineBuilder.isHWAntialias(), pipelineBuilder.hasUserStencilSettings(), |
| 344 | &appliedClip)) { |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 345 | return; |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 346 | } |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 347 | |
robertphillips | 55fdccc | 2016-06-06 06:16:20 -0700 | [diff] [blame] | 348 | // TODO: this is the only remaining usage of the AutoRestoreFragmentProcessorState - remove it |
cdalton | d472792 | 2015-11-10 12:49:06 -0800 | [diff] [blame] | 349 | GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 350 | if (appliedClip.getClipCoverageFragmentProcessor()) { |
cdalton | d472792 | 2015-11-10 12:49:06 -0800 | [diff] [blame] | 351 | arfps.set(&pipelineBuilder); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 352 | arfps.addCoverageFragmentProcessor(sk_ref_sp(appliedClip.getClipCoverageFragmentProcessor())); |
cdalton | d472792 | 2015-11-10 12:49:06 -0800 | [diff] [blame] | 353 | } |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 354 | |
bsalomon | ad792c1 | 2015-09-10 11:10:50 -0700 | [diff] [blame] | 355 | GrPipeline::CreateArgs args; |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 356 | args.fPipelineBuilder = &pipelineBuilder; |
robertphillips | 55fdccc | 2016-06-06 06:16:20 -0700 | [diff] [blame] | 357 | args.fDrawContext = drawContext; |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 358 | args.fCaps = this->caps(); |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 359 | args.fScissor = &appliedClip.scissorState(); |
| 360 | args.fHasStencilClip = appliedClip.hasStencilClip(); |
| 361 | if (pipelineBuilder.hasUserStencilSettings() || appliedClip.hasStencilClip()) { |
robertphillips | 55fdccc | 2016-06-06 06:16:20 -0700 | [diff] [blame] | 362 | if (!fResourceProvider->attachStencilAttachment(drawContext->accessRenderTarget())) { |
cdalton | 17bf820 | 2016-05-13 11:27:15 -0700 | [diff] [blame] | 363 | SkDebugf("ERROR creating stencil attachment. Draw skipped.\n"); |
| 364 | return; |
| 365 | } |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 366 | } |
| 367 | batch->getPipelineOptimizations(&args.fOpts); |
| 368 | GrScissorState finalScissor; |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 369 | if (args.fOpts.fOverrides.fUsePLSDstRead || fClipBatchToBounds) { |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 370 | GrGLIRect viewport; |
| 371 | viewport.fLeft = 0; |
| 372 | viewport.fBottom = 0; |
robertphillips | 55fdccc | 2016-06-06 06:16:20 -0700 | [diff] [blame] | 373 | viewport.fWidth = drawContext->width(); |
| 374 | viewport.fHeight = drawContext->height(); |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 375 | SkIRect ibounds; |
| 376 | ibounds.fLeft = SkTPin(SkScalarFloorToInt(batch->bounds().fLeft), viewport.fLeft, |
| 377 | viewport.fWidth); |
| 378 | ibounds.fTop = SkTPin(SkScalarFloorToInt(batch->bounds().fTop), viewport.fBottom, |
| 379 | viewport.fHeight); |
| 380 | ibounds.fRight = SkTPin(SkScalarCeilToInt(batch->bounds().fRight), viewport.fLeft, |
| 381 | viewport.fWidth); |
| 382 | ibounds.fBottom = SkTPin(SkScalarCeilToInt(batch->bounds().fBottom), viewport.fBottom, |
| 383 | viewport.fHeight); |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 384 | if (appliedClip.scissorState().enabled()) { |
| 385 | const SkIRect& scissorRect = appliedClip.scissorState().rect(); |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 386 | if (!ibounds.intersect(scissorRect)) { |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 387 | return; |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | finalScissor.set(ibounds); |
| 391 | args.fScissor = &finalScissor; |
| 392 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 393 | args.fOpts.fColorPOI.completeCalculations( |
| 394 | sk_sp_address_as_pointer_address(pipelineBuilder.fColorFragmentProcessors.begin()), |
| 395 | pipelineBuilder.numColorFragmentProcessors()); |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 396 | args.fOpts.fCoveragePOI.completeCalculations( |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 397 | sk_sp_address_as_pointer_address(pipelineBuilder.fCoverageFragmentProcessors.begin()), |
| 398 | pipelineBuilder.numCoverageFragmentProcessors()); |
robertphillips | 55fdccc | 2016-06-06 06:16:20 -0700 | [diff] [blame] | 399 | if (!this->setupDstReadIfNecessary(pipelineBuilder, drawContext->accessRenderTarget(), |
| 400 | clip, args.fOpts, |
| 401 | &args.fDstTexture, batch->bounds())) { |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 402 | return; |
| 403 | } |
| 404 | |
| 405 | if (!batch->installPipeline(args)) { |
egdaniel | e36914c | 2015-02-13 09:00:33 -0800 | [diff] [blame] | 406 | return; |
| 407 | } |
bsalomon | ad792c1 | 2015-09-10 11:10:50 -0700 | [diff] [blame] | 408 | |
robertphillips | 498d7ac | 2015-10-30 10:11:30 -0700 | [diff] [blame] | 409 | #ifdef ENABLE_MDB |
| 410 | SkASSERT(fRenderTarget); |
| 411 | batch->pipeline()->addDependenciesTo(fRenderTarget); |
| 412 | #endif |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 413 | SkRect clippedBounds; |
| 414 | SkAssertResult(intersect(&clippedBounds, bounds, appliedClip.deviceBounds())); |
| 415 | this->recordBatch(batch, clippedBounds); |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 416 | } |
| 417 | |
robertphillips | 59cf61a | 2016-07-13 09:18:21 -0700 | [diff] [blame] | 418 | void GrDrawTarget::stencilPath(GrDrawContext* drawContext, |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 419 | const GrClip& clip, |
robertphillips | 59cf61a | 2016-07-13 09:18:21 -0700 | [diff] [blame] | 420 | const GrUserStencilSettings* ss, |
| 421 | bool useHWAA, |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame] | 422 | const SkMatrix& viewMatrix, |
robertphillips | 59cf61a | 2016-07-13 09:18:21 -0700 | [diff] [blame] | 423 | const GrPath* path) { |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 424 | // TODO: extract portions of checkDraw that are relevant to path stenciling. |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 425 | SkASSERT(path); |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 426 | SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport()); |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 427 | |
| 428 | // Setup clip |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 429 | GrAppliedClip appliedClip; |
robertphillips | 59cf61a | 2016-07-13 09:18:21 -0700 | [diff] [blame] | 430 | if (!clip.apply(fContext, drawContext, nullptr, useHWAA, SkToBool(ss), &appliedClip)) { |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 431 | return; |
| 432 | } |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 433 | // TODO: respect fClipBatchToBounds if we ever start computing bounds here. |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 434 | |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 435 | // Coverage AA does not make sense when rendering to the stencil buffer. The caller should never |
| 436 | // attempt this in a situation that would require coverage AA. |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 437 | SkASSERT(!appliedClip.getClipCoverageFragmentProcessor()); |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 438 | |
robertphillips | 55fdccc | 2016-06-06 06:16:20 -0700 | [diff] [blame] | 439 | GrStencilAttachment* stencilAttachment = fResourceProvider->attachStencilAttachment( |
| 440 | drawContext->accessRenderTarget()); |
cdalton | 17bf820 | 2016-05-13 11:27:15 -0700 | [diff] [blame] | 441 | if (!stencilAttachment) { |
| 442 | SkDebugf("ERROR creating stencil attachment. Draw skipped.\n"); |
| 443 | return; |
| 444 | } |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 445 | |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame] | 446 | GrBatch* batch = GrStencilPathBatch::Create(viewMatrix, |
robertphillips | 59cf61a | 2016-07-13 09:18:21 -0700 | [diff] [blame] | 447 | useHWAA, |
| 448 | path->getFillType(), |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 449 | appliedClip.hasStencilClip(), |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 450 | stencilAttachment->bits(), |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 451 | appliedClip.scissorState(), |
robertphillips | 55fdccc | 2016-06-06 06:16:20 -0700 | [diff] [blame] | 452 | drawContext->accessRenderTarget(), |
bsalomon | a44919e | 2015-08-18 13:28:19 -0700 | [diff] [blame] | 453 | path); |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 454 | this->recordBatch(batch, appliedClip.deviceBounds()); |
bsalomon | a44919e | 2015-08-18 13:28:19 -0700 | [diff] [blame] | 455 | batch->unref(); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 456 | } |
| 457 | |
robertphillips | 9199a9f | 2016-07-13 07:48:43 -0700 | [diff] [blame] | 458 | void GrDrawTarget::addBatch(sk_sp<GrBatch> batch) { |
| 459 | this->recordBatch(batch.get(), batch->bounds()); |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 460 | } |
| 461 | |
bsalomon | 9f129de | 2016-08-10 16:31:05 -0700 | [diff] [blame] | 462 | void GrDrawTarget::fullClear(GrRenderTarget* renderTarget, GrColor color) { |
bsalomon | fd8d013 | 2016-08-11 11:25:33 -0700 | [diff] [blame^] | 463 | // Currently this just inserts or updates the last clear batch. However, once in MDB this can |
| 464 | // remove all the previously recorded batches and change the load op to clear with supplied |
| 465 | // color. |
| 466 | if (fLastFullClearBatch && |
| 467 | fLastFullClearBatch->renderTargetUniqueID() == renderTarget->getUniqueID()) { |
| 468 | // As currently implemented, fLastFullClearBatch should be the last batch because we would |
| 469 | // have cleared it when another batch was recorded. |
| 470 | SkASSERT(fRecordedBatches.back().fBatch.get() == fLastFullClearBatch); |
| 471 | fLastFullClearBatch->setColor(color); |
| 472 | return; |
| 473 | } |
| 474 | sk_sp<GrClearBatch> batch(GrClearBatch::Make(SkIRect::MakeWH(renderTarget->width(), |
| 475 | renderTarget->height()), |
| 476 | color, renderTarget)); |
| 477 | if (batch.get() == this->recordBatch(batch.get(), batch->bounds())) { |
| 478 | fLastFullClearBatch = batch.get(); |
| 479 | } |
bsalomon | 9f129de | 2016-08-10 16:31:05 -0700 | [diff] [blame] | 480 | } |
| 481 | |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 482 | void GrDrawTarget::discard(GrRenderTarget* renderTarget) { |
bsalomon | 9f129de | 2016-08-10 16:31:05 -0700 | [diff] [blame] | 483 | // Currently this just inserts a discard batch. However, once in MDB this can remove all the |
| 484 | // previously recorded batches and change the load op to discard. |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 485 | if (this->caps()->discardRenderTargetSupport()) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 486 | GrBatch* batch = new GrDiscardBatch(renderTarget); |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 487 | this->recordBatch(batch, batch->bounds()); |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 488 | batch->unref(); |
bsalomon | 63b2196 | 2014-11-05 07:05:34 -0800 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 492 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 493 | |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 494 | bool GrDrawTarget::copySurface(GrSurface* dst, |
bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 495 | GrSurface* src, |
| 496 | const SkIRect& srcRect, |
| 497 | const SkIPoint& dstPoint) { |
bsalomon | 872062c | 2015-08-18 12:12:35 -0700 | [diff] [blame] | 498 | GrBatch* batch = GrCopySurfaceBatch::Create(dst, src, srcRect, dstPoint); |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 499 | if (!batch) { |
| 500 | return false; |
| 501 | } |
robertphillips | 498d7ac | 2015-10-30 10:11:30 -0700 | [diff] [blame] | 502 | #ifdef ENABLE_MDB |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 503 | this->addDependency(src); |
robertphillips | 498d7ac | 2015-10-30 10:11:30 -0700 | [diff] [blame] | 504 | #endif |
| 505 | |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 506 | this->recordBatch(batch, batch->bounds()); |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 507 | batch->unref(); |
| 508 | return true; |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 509 | } |
| 510 | |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 511 | static inline bool can_reorder(const SkRect& a, const SkRect& b) { |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 512 | return a.fRight <= b.fLeft || a.fBottom <= b.fTop || |
| 513 | b.fRight <= a.fLeft || b.fBottom <= a.fTop; |
| 514 | } |
| 515 | |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 516 | static void join(SkRect* out, const SkRect& a, const SkRect& b) { |
| 517 | SkASSERT(a.fLeft <= a.fRight && a.fTop <= a.fBottom); |
| 518 | SkASSERT(b.fLeft <= b.fRight && b.fTop <= b.fBottom); |
| 519 | out->fLeft = SkTMin(a.fLeft, b.fLeft); |
| 520 | out->fTop = SkTMin(a.fTop, b.fTop); |
| 521 | out->fRight = SkTMax(a.fRight, b.fRight); |
| 522 | out->fBottom = SkTMax(a.fBottom, b.fBottom); |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 523 | } |
| 524 | |
bsalomon | fd8d013 | 2016-08-11 11:25:33 -0700 | [diff] [blame^] | 525 | GrBatch* GrDrawTarget::recordBatch(GrBatch* batch, const SkRect& clippedBounds) { |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 526 | // A closed drawTarget should never receive new/more batches |
robertphillips | 6a18665 | 2015-10-20 07:37:58 -0700 | [diff] [blame] | 527 | SkASSERT(!this->isClosed()); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 528 | |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 529 | // Check if there is a Batch Draw we can batch with by linearly searching back until we either |
| 530 | // 1) check every draw |
| 531 | // 2) intersect with something |
| 532 | // 3) find a 'blocker' |
joshualitt | b0666ad | 2016-03-08 10:43:41 -0800 | [diff] [blame] | 533 | GR_AUDIT_TRAIL_ADDBATCH(fAuditTrail, batch); |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 534 | GrBATCH_INFO("Re-Recording (%s, B%u)\n" |
joshualitt | e2bcec3 | 2015-09-30 06:22:22 -0700 | [diff] [blame] | 535 | "\tBounds LRTB (%f, %f, %f, %f)\n", |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 536 | batch->name(), |
| 537 | batch->uniqueID(), |
| 538 | batch->bounds().fLeft, batch->bounds().fRight, |
| 539 | batch->bounds().fTop, batch->bounds().fBottom); |
| 540 | GrBATCH_INFO(SkTabString(batch->dumpInfo(), 1).c_str()); |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 541 | GrBATCH_INFO("\tClipped Bounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", |
| 542 | clippedBounds.fLeft, clippedBounds.fTop, clippedBounds.fRight, |
| 543 | clippedBounds.fBottom); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 544 | GrBATCH_INFO("\tOutcome:\n"); |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 545 | int maxCandidates = SkTMin(fMaxBatchLookback, fRecordedBatches.count()); |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 546 | if (maxCandidates) { |
| 547 | int i = 0; |
| 548 | while (true) { |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 549 | GrBatch* candidate = fRecordedBatches.fromBack(i).fBatch.get(); |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 550 | // We cannot continue to search backwards if the render target changes |
| 551 | if (candidate->renderTargetUniqueID() != batch->renderTargetUniqueID()) { |
| 552 | GrBATCH_INFO("\t\tBreaking because of (%s, B%u) Rendertarget\n", |
| 553 | candidate->name(), candidate->uniqueID()); |
| 554 | break; |
| 555 | } |
| 556 | if (candidate->combineIfPossible(batch, *this->caps())) { |
| 557 | GrBATCH_INFO("\t\tCombining with (%s, B%u)\n", candidate->name(), |
| 558 | candidate->uniqueID()); |
joshualitt | b0666ad | 2016-03-08 10:43:41 -0800 | [diff] [blame] | 559 | GR_AUDIT_TRAIL_BATCHING_RESULT_COMBINED(fAuditTrail, candidate, batch); |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 560 | join(&fRecordedBatches.fromBack(i).fClippedBounds, |
| 561 | fRecordedBatches.fromBack(i).fClippedBounds, clippedBounds); |
bsalomon | fd8d013 | 2016-08-11 11:25:33 -0700 | [diff] [blame^] | 562 | return candidate; |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 563 | } |
| 564 | // Stop going backwards if we would cause a painter's order violation. |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 565 | const SkRect& candidateBounds = fRecordedBatches.fromBack(i).fClippedBounds; |
| 566 | if (!can_reorder(candidateBounds, clippedBounds)) { |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 567 | GrBATCH_INFO("\t\tIntersects with (%s, B%u)\n", candidate->name(), |
| 568 | candidate->uniqueID()); |
| 569 | break; |
| 570 | } |
| 571 | ++i; |
| 572 | if (i == maxCandidates) { |
| 573 | GrBATCH_INFO("\t\tReached max lookback or beginning of batch array %d\n", i); |
| 574 | break; |
| 575 | } |
| 576 | } |
| 577 | } else { |
| 578 | GrBATCH_INFO("\t\tFirstBatch\n"); |
| 579 | } |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 580 | GR_AUDIT_TRAIL_BATCHING_RESULT_NEW(fAuditTrail, batch); |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 581 | fRecordedBatches.emplace_back(RecordedBatch{sk_ref_sp(batch), clippedBounds}); |
bsalomon | fd8d013 | 2016-08-11 11:25:33 -0700 | [diff] [blame^] | 582 | fLastFullClearBatch = nullptr; |
| 583 | return batch; |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 584 | } |
| 585 | |
bsalomon | aecc018 | 2016-03-07 11:50:44 -0800 | [diff] [blame] | 586 | void GrDrawTarget::forwardCombine() { |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 587 | for (int i = 0; i < fRecordedBatches.count() - 2; ++i) { |
| 588 | GrBatch* batch = fRecordedBatches[i].fBatch.get(); |
| 589 | const SkRect& batchBounds = fRecordedBatches[i].fClippedBounds; |
| 590 | int maxCandidateIdx = SkTMin(i + fMaxBatchLookahead, fRecordedBatches.count() - 1); |
bsalomon | aecc018 | 2016-03-07 11:50:44 -0800 | [diff] [blame] | 591 | int j = i + 1; |
| 592 | while (true) { |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 593 | GrBatch* candidate = fRecordedBatches[j].fBatch.get(); |
bsalomon | aecc018 | 2016-03-07 11:50:44 -0800 | [diff] [blame] | 594 | // We cannot continue to search if the render target changes |
| 595 | if (candidate->renderTargetUniqueID() != batch->renderTargetUniqueID()) { |
| 596 | GrBATCH_INFO("\t\tBreaking because of (%s, B%u) Rendertarget\n", |
| 597 | candidate->name(), candidate->uniqueID()); |
| 598 | break; |
| 599 | } |
| 600 | if (j == i +1) { |
| 601 | // We assume batch would have combined with candidate when the candidate was added |
| 602 | // via backwards combining in recordBatch. |
| 603 | SkASSERT(!batch->combineIfPossible(candidate, *this->caps())); |
| 604 | } else if (batch->combineIfPossible(candidate, *this->caps())) { |
| 605 | GrBATCH_INFO("\t\tCombining with (%s, B%u)\n", candidate->name(), |
| 606 | candidate->uniqueID()); |
joshualitt | b0666ad | 2016-03-08 10:43:41 -0800 | [diff] [blame] | 607 | GR_AUDIT_TRAIL_BATCHING_RESULT_COMBINED(fAuditTrail, batch, candidate); |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 608 | fRecordedBatches[j].fBatch = std::move(fRecordedBatches[i].fBatch); |
| 609 | join(&fRecordedBatches[j].fClippedBounds, fRecordedBatches[j].fClippedBounds, |
| 610 | batchBounds); |
bsalomon | aecc018 | 2016-03-07 11:50:44 -0800 | [diff] [blame] | 611 | break; |
| 612 | } |
| 613 | // Stop going traversing if we would cause a painter's order violation. |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 614 | const SkRect& candidateBounds = fRecordedBatches[j].fClippedBounds; |
| 615 | if (!can_reorder(candidateBounds, batchBounds)) { |
bsalomon | aecc018 | 2016-03-07 11:50:44 -0800 | [diff] [blame] | 616 | GrBATCH_INFO("\t\tIntersects with (%s, B%u)\n", candidate->name(), |
| 617 | candidate->uniqueID()); |
| 618 | break; |
| 619 | } |
| 620 | ++j; |
| 621 | if (j > maxCandidateIdx) { |
| 622 | GrBATCH_INFO("\t\tReached max lookahead or end of batch array %d\n", i); |
| 623 | break; |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | |
egdaniel | e36914c | 2015-02-13 09:00:33 -0800 | [diff] [blame] | 629 | /////////////////////////////////////////////////////////////////////////////// |
| 630 | |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 631 | void GrDrawTarget::clearStencilClip(const SkIRect& rect, bool insideClip, GrRenderTarget* rt) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 632 | GrBatch* batch = new GrClearStencilClipBatch(rect, insideClip, rt); |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 633 | this->recordBatch(batch, batch->bounds()); |
bsalomon | 5ea0363 | 2015-08-18 10:33:30 -0700 | [diff] [blame] | 634 | batch->unref(); |
| 635 | } |