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