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