epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2010 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
joshualitt | c2893c5 | 2015-01-28 06:54:30 -0800 | [diff] [blame] | 9 | #include "GrDrawTarget.h" |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 10 | |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 11 | #include "GrAuditTrail.h" |
bsalomon | eb1cb5c | 2015-05-22 08:01:09 -0700 | [diff] [blame] | 12 | #include "GrCaps.h" |
bsalomon | 4061b12 | 2015-05-29 10:26:19 -0700 | [diff] [blame] | 13 | #include "GrGpu.h" |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 14 | #include "GrPath.h" |
egdaniel | e36914c | 2015-02-13 09:00:33 -0800 | [diff] [blame] | 15 | #include "GrPipeline.h" |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 16 | #include "GrMemoryPool.h" |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 17 | #include "GrRenderTarget.h" |
bsalomon | 4061b12 | 2015-05-29 10:26:19 -0700 | [diff] [blame] | 18 | #include "GrResourceProvider.h" |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 19 | #include "GrRenderTargetPriv.h" |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 20 | #include "GrSurfacePriv.h" |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 21 | #include "GrTexture.h" |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 22 | #include "GrVertexBuffer.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 23 | |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 24 | #include "SkStrokeRec.h" |
| 25 | |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 26 | #include "batches/GrClearBatch.h" |
bsalomon | 872062c | 2015-08-18 12:12:35 -0700 | [diff] [blame] | 27 | #include "batches/GrCopySurfaceBatch.h" |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 28 | #include "batches/GrDiscardBatch.h" |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 29 | #include "batches/GrDrawBatch.h" |
bsalomon | add79ef | 2015-08-19 13:26:49 -0700 | [diff] [blame] | 30 | #include "batches/GrDrawPathBatch.h" |
joshualitt | ecd1a69 | 2015-08-10 10:08:26 -0700 | [diff] [blame] | 31 | #include "batches/GrRectBatchFactory.h" |
bsalomon | a44919e | 2015-08-18 13:28:19 -0700 | [diff] [blame] | 32 | #include "batches/GrStencilPathBatch.h" |
joshualitt | 7441782 | 2015-08-07 11:42:16 -0700 | [diff] [blame] | 33 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 34 | //////////////////////////////////////////////////////////////////////////////// |
| 35 | |
bsalomon | 489147c | 2015-12-14 12:13:09 -0800 | [diff] [blame] | 36 | // Experimentally we have found that most batching occurs within the first 10 comparisons. |
| 37 | static const int kDefaultMaxBatchLookback = 10; |
| 38 | |
bsalomon | 69cfe95 | 2015-11-30 13:27:47 -0800 | [diff] [blame] | 39 | GrDrawTarget::GrDrawTarget(GrRenderTarget* rt, GrGpu* gpu, GrResourceProvider* resourceProvider, |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 40 | GrAuditTrail* auditTrail, const Options& options) |
bsalomon | 4061b12 | 2015-05-29 10:26:19 -0700 | [diff] [blame] | 41 | : fGpu(SkRef(gpu)) |
bsalomon | 4061b12 | 2015-05-29 10:26:19 -0700 | [diff] [blame] | 42 | , fResourceProvider(resourceProvider) |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 43 | , fAuditTrail(auditTrail) |
bsalomon | 648c696 | 2015-10-23 09:06:59 -0700 | [diff] [blame] | 44 | , fFlags(0) |
robertphillips | 498d7ac | 2015-10-30 10:11:30 -0700 | [diff] [blame] | 45 | , fRenderTarget(rt) { |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 46 | // TODO: Stop extracting the context (currently needed by GrClipMaskManager) |
| 47 | fContext = fGpu->getContext(); |
bsalomon | 69cfe95 | 2015-11-30 13:27:47 -0800 | [diff] [blame] | 48 | fClipMaskManager.reset(new GrClipMaskManager(this, options.fClipBatchToBounds)); |
robertphillips | 4beb5c1 | 2015-10-20 07:50:00 -0700 | [diff] [blame] | 49 | |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 50 | fDrawBatchBounds = options.fDrawBatchBounds; |
bsalomon | 489147c | 2015-12-14 12:13:09 -0800 | [diff] [blame] | 51 | fMaxBatchLookback = (options.fMaxBatchLookback < 0) ? kDefaultMaxBatchLookback : |
| 52 | options.fMaxBatchLookback; |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 53 | |
robertphillips | 0dfa62c | 2015-11-16 06:23:31 -0800 | [diff] [blame] | 54 | rt->setLastDrawTarget(this); |
| 55 | |
robertphillips | 4beb5c1 | 2015-10-20 07:50:00 -0700 | [diff] [blame] | 56 | #ifdef SK_DEBUG |
| 57 | static int debugID = 0; |
| 58 | fDebugID = debugID++; |
| 59 | #endif |
bsalomon | 4061b12 | 2015-05-29 10:26:19 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | GrDrawTarget::~GrDrawTarget() { |
robertphillips | 498d7ac | 2015-10-30 10:11:30 -0700 | [diff] [blame] | 63 | if (fRenderTarget && this == fRenderTarget->getLastDrawTarget()) { |
| 64 | fRenderTarget->setLastDrawTarget(nullptr); |
| 65 | } |
| 66 | |
bsalomon | 4061b12 | 2015-05-29 10:26:19 -0700 | [diff] [blame] | 67 | fGpu->unref(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | //////////////////////////////////////////////////////////////////////////////// |
| 71 | |
robertphillips | 6a18665 | 2015-10-20 07:37:58 -0700 | [diff] [blame] | 72 | // Add a GrDrawTarget-based dependency |
| 73 | void GrDrawTarget::addDependency(GrDrawTarget* dependedOn) { |
| 74 | SkASSERT(!dependedOn->dependsOn(this)); // loops are bad |
| 75 | |
| 76 | if (this->dependsOn(dependedOn)) { |
| 77 | return; // don't add duplicate dependencies |
| 78 | } |
| 79 | |
| 80 | *fDependencies.push() = dependedOn; |
| 81 | } |
| 82 | |
| 83 | // Convert from a GrSurface-based dependency to a GrDrawTarget one |
| 84 | void GrDrawTarget::addDependency(GrSurface* dependedOn) { |
| 85 | if (dependedOn->asRenderTarget() && dependedOn->asRenderTarget()->getLastDrawTarget()) { |
| 86 | // If it is still receiving dependencies, this DT shouldn't be closed |
| 87 | SkASSERT(!this->isClosed()); |
| 88 | |
| 89 | GrDrawTarget* dt = dependedOn->asRenderTarget()->getLastDrawTarget(); |
| 90 | if (dt == this) { |
| 91 | // self-read - presumably for dst reads |
| 92 | } else { |
| 93 | this->addDependency(dt); |
| 94 | |
| 95 | // Can't make it closed in the self-read case |
| 96 | dt->makeClosed(); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
robertphillips | 4beb5c1 | 2015-10-20 07:50:00 -0700 | [diff] [blame] | 101 | #ifdef SK_DEBUG |
| 102 | void GrDrawTarget::dump() const { |
| 103 | SkDebugf("--------------------------------------------------------------\n"); |
robertphillips | e004bfc | 2015-11-16 09:06:59 -0800 | [diff] [blame] | 104 | SkDebugf("node: %d -> RT: %d\n", fDebugID, fRenderTarget ? fRenderTarget->getUniqueID() : -1); |
robertphillips | 4beb5c1 | 2015-10-20 07:50:00 -0700 | [diff] [blame] | 105 | SkDebugf("relies On (%d): ", fDependencies.count()); |
| 106 | for (int i = 0; i < fDependencies.count(); ++i) { |
| 107 | SkDebugf("%d, ", fDependencies[i]->fDebugID); |
| 108 | } |
| 109 | SkDebugf("\n"); |
| 110 | SkDebugf("batches (%d):\n", fBatches.count()); |
| 111 | for (int i = 0; i < fBatches.count(); ++i) { |
| 112 | #if 0 |
| 113 | SkDebugf("*******************************\n"); |
| 114 | #endif |
| 115 | SkDebugf("%d: %s\n", i, fBatches[i]->name()); |
| 116 | #if 0 |
| 117 | SkString str = fBatches[i]->dumpInfo(); |
| 118 | SkDebugf("%s\n", str.c_str()); |
| 119 | #endif |
| 120 | } |
| 121 | } |
| 122 | #endif |
| 123 | |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 124 | bool GrDrawTarget::setupDstReadIfNecessary(const GrPipelineBuilder& pipelineBuilder, |
ethannicholas | de4166a | 2015-11-30 08:57:38 -0800 | [diff] [blame] | 125 | const GrPipelineOptimizations& optimizations, |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 126 | GrXferProcessor::DstTexture* dstTexture, |
bsalomon | ad792c1 | 2015-09-10 11:10:50 -0700 | [diff] [blame] | 127 | const SkRect& batchBounds) { |
| 128 | SkRect bounds = batchBounds; |
| 129 | bounds.outset(0.5f, 0.5f); |
| 130 | |
ethannicholas | de4166a | 2015-11-30 08:57:38 -0800 | [diff] [blame] | 131 | if (!pipelineBuilder.willXPNeedDstTexture(*this->caps(), optimizations)) { |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 132 | return true; |
| 133 | } |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 134 | |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 135 | GrRenderTarget* rt = pipelineBuilder.getRenderTarget(); |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 136 | |
| 137 | if (this->caps()->textureBarrierSupport()) { |
| 138 | if (GrTexture* rtTex = rt->asTexture()) { |
bsalomon | dc47ff7 | 2015-05-26 12:16:59 -0700 | [diff] [blame] | 139 | // 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] | 140 | // will be responsible to detect this situation and request a texture barrier. |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 141 | dstTexture->setTexture(rtTex); |
| 142 | dstTexture->setOffset(0, 0); |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 143 | return true; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | SkIRect copyRect; |
robertphillips | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 148 | pipelineBuilder.clip().getConservativeBounds(rt->width(), rt->height(), ©Rect); |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 149 | |
bsalomon | ad792c1 | 2015-09-10 11:10:50 -0700 | [diff] [blame] | 150 | SkIRect drawIBounds; |
| 151 | bounds.roundOut(&drawIBounds); |
| 152 | if (!copyRect.intersect(drawIBounds)) { |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 153 | #ifdef SK_DEBUG |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 154 | GrCapsDebugf(this->caps(), "Missed an early reject. " |
| 155 | "Bailing on draw from setupDstReadIfNecessary.\n"); |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 156 | #endif |
bsalomon | ad792c1 | 2015-09-10 11:10:50 -0700 | [diff] [blame] | 157 | return false; |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 158 | } |
skia.committer@gmail.com | 05a2ee0 | 2013-04-02 07:01:34 +0000 | [diff] [blame] | 159 | |
commit-bot@chromium.org | 63150af | 2013-04-11 22:00:22 +0000 | [diff] [blame] | 160 | // MSAA consideration: When there is support for reading MSAA samples in the shader we could |
| 161 | // have per-sample dst values by making the copy multisampled. |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 162 | GrSurfaceDesc desc; |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 163 | if (!fGpu->initCopySurfaceDstDesc(rt, &desc)) { |
bsalomon | a73239a | 2015-04-28 13:35:17 -0700 | [diff] [blame] | 164 | desc.fOrigin = kDefault_GrSurfaceOrigin; |
| 165 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 166 | desc.fConfig = rt->config(); |
| 167 | } |
| 168 | |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 169 | desc.fWidth = copyRect.width(); |
| 170 | desc.fHeight = copyRect.height(); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 171 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 172 | static const uint32_t kFlags = 0; |
| 173 | SkAutoTUnref<GrTexture> copy(fResourceProvider->createApproxTexture(desc, kFlags)); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 174 | |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 175 | if (!copy) { |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 176 | SkDebugf("Failed to create temporary copy of destination texture.\n"); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 177 | return false; |
| 178 | } |
bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 179 | SkIPoint dstPoint = {0, 0}; |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 180 | this->copySurface(copy, rt, copyRect, dstPoint); |
| 181 | dstTexture->setTexture(copy); |
| 182 | dstTexture->setOffset(copyRect.fLeft, copyRect.fTop); |
| 183 | return true; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 184 | } |
| 185 | |
robertphillips | a13e202 | 2015-11-11 12:01:09 -0800 | [diff] [blame] | 186 | void GrDrawTarget::prepareBatches(GrBatchFlushState* flushState) { |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 187 | // Semi-usually the drawTargets are already closed at this point, but sometimes Ganesh |
| 188 | // needs to flush mid-draw. In that case, the SkGpuDevice's drawTargets won't be closed |
| 189 | // but need to be flushed anyway. Closing such drawTargets here will mean new |
| 190 | // drawTargets will be created to replace them if the SkGpuDevice(s) write to them again. |
| 191 | this->makeClosed(); |
| 192 | |
robertphillips | 498d7ac | 2015-10-30 10:11:30 -0700 | [diff] [blame] | 193 | // Loop over the batches that haven't yet generated their geometry |
robertphillips | 1f0e350 | 2015-11-10 10:19:50 -0800 | [diff] [blame] | 194 | for (int i = 0; i < fBatches.count(); ++i) { |
robertphillips | a13e202 | 2015-11-11 12:01:09 -0800 | [diff] [blame] | 195 | fBatches[i]->prepare(flushState); |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 196 | } |
robertphillips | a13e202 | 2015-11-11 12:01:09 -0800 | [diff] [blame] | 197 | } |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 198 | |
robertphillips | a13e202 | 2015-11-11 12:01:09 -0800 | [diff] [blame] | 199 | void GrDrawTarget::drawBatches(GrBatchFlushState* flushState) { |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 200 | // Draw all the generated geometry. |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 201 | SkRandom random; |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 202 | for (int i = 0; i < fBatches.count(); ++i) { |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 203 | if (fDrawBatchBounds) { |
| 204 | const SkRect& bounds = fBatches[i]->bounds(); |
| 205 | SkIRect ibounds; |
| 206 | bounds.roundOut(&ibounds); |
| 207 | // In multi-draw buffer all the batches use the same render target and we won't need to |
| 208 | // get the batchs bounds. |
| 209 | if (GrRenderTarget* rt = fBatches[i]->renderTarget()) { |
| 210 | fGpu->drawDebugWireRect(rt, ibounds, 0xFF000000 | random.nextU()); |
| 211 | } |
| 212 | } |
robertphillips | a13e202 | 2015-11-11 12:01:09 -0800 | [diff] [blame] | 213 | fBatches[i]->draw(flushState); |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 214 | } |
bsalomon | a73239a | 2015-04-28 13:35:17 -0700 | [diff] [blame] | 215 | } |
| 216 | |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 217 | void GrDrawTarget::reset() { |
| 218 | fBatches.reset(); |
| 219 | } |
| 220 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 221 | void GrDrawTarget::drawBatch(const GrPipelineBuilder& pipelineBuilder, GrDrawBatch* batch) { |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 222 | // Setup clip |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 223 | GrPipelineBuilder::AutoRestoreStencil ars; |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 224 | GrAppliedClip clip; |
bsalomon | e91f7b5 | 2015-10-27 06:42:50 -0700 | [diff] [blame] | 225 | if (!fClipMaskManager->setupClipping(pipelineBuilder, &ars, &batch->bounds(), &clip)) { |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 226 | return; |
| 227 | } |
cdalton | d472792 | 2015-11-10 12:49:06 -0800 | [diff] [blame] | 228 | GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps; |
| 229 | if (clip.clipCoverageFragmentProcessor()) { |
| 230 | arfps.set(&pipelineBuilder); |
| 231 | arfps.addCoverageFragmentProcessor(clip.clipCoverageFragmentProcessor()); |
| 232 | } |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 233 | |
bsalomon | ad792c1 | 2015-09-10 11:10:50 -0700 | [diff] [blame] | 234 | GrPipeline::CreateArgs args; |
cdalton | d472792 | 2015-11-10 12:49:06 -0800 | [diff] [blame] | 235 | if (!this->installPipelineInDrawBatch(&pipelineBuilder, &clip.scissorState(), batch)) { |
egdaniel | e36914c | 2015-02-13 09:00:33 -0800 | [diff] [blame] | 236 | return; |
| 237 | } |
bsalomon | ad792c1 | 2015-09-10 11:10:50 -0700 | [diff] [blame] | 238 | |
robertphillips | 498d7ac | 2015-10-30 10:11:30 -0700 | [diff] [blame] | 239 | #ifdef ENABLE_MDB |
| 240 | SkASSERT(fRenderTarget); |
| 241 | batch->pipeline()->addDependenciesTo(fRenderTarget); |
| 242 | #endif |
| 243 | |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 244 | this->recordBatch(batch); |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 245 | } |
| 246 | |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 247 | static const GrStencilSettings& winding_path_stencil_settings() { |
| 248 | GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings, |
| 249 | kIncClamp_StencilOp, |
| 250 | kIncClamp_StencilOp, |
| 251 | kAlwaysIfInClip_StencilFunc, |
| 252 | 0xFFFF, 0xFFFF, 0xFFFF); |
| 253 | return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings); |
| 254 | } |
| 255 | |
| 256 | static const GrStencilSettings& even_odd_path_stencil_settings() { |
| 257 | GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings, |
| 258 | kInvert_StencilOp, |
| 259 | kInvert_StencilOp, |
| 260 | kAlwaysIfInClip_StencilFunc, |
| 261 | 0xFFFF, 0xFFFF, 0xFFFF); |
| 262 | return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings); |
| 263 | } |
| 264 | |
| 265 | void GrDrawTarget::getPathStencilSettingsForFilltype(GrPathRendering::FillType fill, |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 266 | const GrStencilAttachment* sb, |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 267 | GrStencilSettings* outStencilSettings) { |
| 268 | |
| 269 | switch (fill) { |
| 270 | default: |
| 271 | SkFAIL("Unexpected path fill."); |
| 272 | case GrPathRendering::kWinding_FillType: |
| 273 | *outStencilSettings = winding_path_stencil_settings(); |
| 274 | break; |
| 275 | case GrPathRendering::kEvenOdd_FillType: |
| 276 | *outStencilSettings = even_odd_path_stencil_settings(); |
| 277 | break; |
| 278 | } |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 279 | fClipMaskManager->adjustPathStencilParams(sb, outStencilSettings); |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 280 | } |
| 281 | |
joshualitt | 1c73548 | 2015-07-13 08:08:25 -0700 | [diff] [blame] | 282 | void GrDrawTarget::stencilPath(const GrPipelineBuilder& pipelineBuilder, |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame] | 283 | const SkMatrix& viewMatrix, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 284 | const GrPath* path, |
| 285 | GrPathRendering::FillType fill) { |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 286 | // TODO: extract portions of checkDraw that are relevant to path stenciling. |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 287 | SkASSERT(path); |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 288 | SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport()); |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 289 | |
| 290 | // Setup clip |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 291 | GrPipelineBuilder::AutoRestoreStencil ars; |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 292 | GrAppliedClip clip; |
bsalomon | e91f7b5 | 2015-10-27 06:42:50 -0700 | [diff] [blame] | 293 | if (!fClipMaskManager->setupClipping(pipelineBuilder, &ars, nullptr, &clip)) { |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 294 | return; |
| 295 | } |
| 296 | |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 297 | GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps; |
| 298 | if (clip.clipCoverageFragmentProcessor()) { |
| 299 | arfps.set(&pipelineBuilder); |
| 300 | arfps.addCoverageFragmentProcessor(clip.clipCoverageFragmentProcessor()); |
| 301 | } |
| 302 | |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 303 | // set stencil settings for path |
| 304 | GrStencilSettings stencilSettings; |
joshualitt | 1c73548 | 2015-07-13 08:08:25 -0700 | [diff] [blame] | 305 | GrRenderTarget* rt = pipelineBuilder.getRenderTarget(); |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 306 | GrStencilAttachment* sb = fResourceProvider->attachStencilAttachment(rt); |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 307 | this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings); |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 308 | |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame] | 309 | GrBatch* batch = GrStencilPathBatch::Create(viewMatrix, |
bsalomon | a44919e | 2015-08-18 13:28:19 -0700 | [diff] [blame] | 310 | pipelineBuilder.isHWAntialias(), |
bsalomon | e91f7b5 | 2015-10-27 06:42:50 -0700 | [diff] [blame] | 311 | stencilSettings, clip.scissorState(), |
bsalomon | a44919e | 2015-08-18 13:28:19 -0700 | [diff] [blame] | 312 | pipelineBuilder.getRenderTarget(), |
| 313 | path); |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 314 | this->recordBatch(batch); |
bsalomon | a44919e | 2015-08-18 13:28:19 -0700 | [diff] [blame] | 315 | batch->unref(); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 316 | } |
| 317 | |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 318 | void GrDrawTarget::drawPathBatch(const GrPipelineBuilder& pipelineBuilder, |
cdalton | 8ff8d24 | 2015-12-08 10:20:32 -0800 | [diff] [blame] | 319 | GrDrawPathBatchBase* batch) { |
bsalomon | add79ef | 2015-08-19 13:26:49 -0700 | [diff] [blame] | 320 | // This looks like drawBatch() but there is an added wrinkle that stencil settings get inserted |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 321 | // after setting up clipping but before onDrawBatch(). TODO: Figure out a better model for |
| 322 | // handling stencil settings WRT interactions between pipeline(builder), clipmaskmanager, and |
| 323 | // batches. |
cdalton | 8ff8d24 | 2015-12-08 10:20:32 -0800 | [diff] [blame] | 324 | SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport()); |
bsalomon | add79ef | 2015-08-19 13:26:49 -0700 | [diff] [blame] | 325 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 326 | GrPipelineBuilder::AutoRestoreStencil ars; |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 327 | GrAppliedClip clip; |
bsalomon | e91f7b5 | 2015-10-27 06:42:50 -0700 | [diff] [blame] | 328 | if (!fClipMaskManager->setupClipping(pipelineBuilder, &ars, &batch->bounds(), &clip)) { |
bsalomon | add79ef | 2015-08-19 13:26:49 -0700 | [diff] [blame] | 329 | return; |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 330 | } |
| 331 | |
cdalton | d472792 | 2015-11-10 12:49:06 -0800 | [diff] [blame] | 332 | GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps; |
| 333 | if (clip.clipCoverageFragmentProcessor()) { |
| 334 | arfps.set(&pipelineBuilder); |
| 335 | arfps.addCoverageFragmentProcessor(clip.clipCoverageFragmentProcessor()); |
| 336 | } |
| 337 | |
bsalomon | add79ef | 2015-08-19 13:26:49 -0700 | [diff] [blame] | 338 | // Ensure the render target has a stencil buffer and get the stencil settings. |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 339 | GrStencilSettings stencilSettings; |
joshualitt | 1c73548 | 2015-07-13 08:08:25 -0700 | [diff] [blame] | 340 | GrRenderTarget* rt = pipelineBuilder.getRenderTarget(); |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 341 | GrStencilAttachment* sb = fResourceProvider->attachStencilAttachment(rt); |
cdalton | 8ff8d24 | 2015-12-08 10:20:32 -0800 | [diff] [blame] | 342 | this->getPathStencilSettingsForFilltype(batch->fillType(), sb, &stencilSettings); |
bsalomon | add79ef | 2015-08-19 13:26:49 -0700 | [diff] [blame] | 343 | batch->setStencilSettings(stencilSettings); |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 344 | |
bsalomon | ad792c1 | 2015-09-10 11:10:50 -0700 | [diff] [blame] | 345 | GrPipeline::CreateArgs args; |
cdalton | d472792 | 2015-11-10 12:49:06 -0800 | [diff] [blame] | 346 | if (!this->installPipelineInDrawBatch(&pipelineBuilder, &clip.scissorState(), batch)) { |
bsalomon | add79ef | 2015-08-19 13:26:49 -0700 | [diff] [blame] | 347 | return; |
| 348 | } |
egdaniel | e36914c | 2015-02-13 09:00:33 -0800 | [diff] [blame] | 349 | |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 350 | this->recordBatch(batch); |
commit-bot@chromium.org | 9b62aa1 | 2014-03-25 11:59:40 +0000 | [diff] [blame] | 351 | } |
| 352 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 353 | void GrDrawTarget::clear(const SkIRect* rect, |
| 354 | GrColor color, |
| 355 | bool canIgnoreRect, |
bsalomon | 63b2196 | 2014-11-05 07:05:34 -0800 | [diff] [blame] | 356 | GrRenderTarget* renderTarget) { |
egdaniel | 51c8d40 | 2015-08-06 10:54:13 -0700 | [diff] [blame] | 357 | SkIRect rtRect = SkIRect::MakeWH(renderTarget->width(), renderTarget->height()); |
| 358 | SkIRect clippedRect; |
| 359 | if (!rect || |
| 360 | (canIgnoreRect && this->caps()->fullClearIsFree()) || |
| 361 | rect->contains(rtRect)) { |
| 362 | rect = &rtRect; |
| 363 | } else { |
| 364 | clippedRect = *rect; |
| 365 | if (!clippedRect.intersect(rtRect)) { |
| 366 | return; |
| 367 | } |
| 368 | rect = &clippedRect; |
| 369 | } |
| 370 | |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 371 | if (this->caps()->useDrawInsteadOfClear()) { |
bsalomon | 63b2196 | 2014-11-05 07:05:34 -0800 | [diff] [blame] | 372 | // This works around a driver bug with clear by drawing a rect instead. |
| 373 | // The driver will ignore a clear if it is the only thing rendered to a |
| 374 | // target before the target is read. |
egdaniel | 51c8d40 | 2015-08-06 10:54:13 -0700 | [diff] [blame] | 375 | if (rect == &rtRect) { |
bsalomon | 63b2196 | 2014-11-05 07:05:34 -0800 | [diff] [blame] | 376 | this->discard(renderTarget); |
| 377 | } |
bsalomon | 63b2196 | 2014-11-05 07:05:34 -0800 | [diff] [blame] | 378 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 379 | GrPipelineBuilder pipelineBuilder; |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 380 | pipelineBuilder.setXPFactory( |
| 381 | GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode))->unref(); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 382 | pipelineBuilder.setRenderTarget(renderTarget); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 383 | |
joshualitt | a8b8499 | 2016-01-13 13:35:35 -0800 | [diff] [blame] | 384 | SkRect scalarRect = SkRect::Make(*rect); |
| 385 | SkAutoTUnref<GrDrawBatch> batch( |
| 386 | GrRectBatchFactory::CreateNonAAFill(color, SkMatrix::I(), scalarRect, |
| 387 | nullptr, nullptr)); |
| 388 | this->drawBatch(pipelineBuilder, batch); |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 389 | } else { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 390 | GrBatch* batch = new GrClearBatch(*rect, color, renderTarget); |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 391 | this->recordBatch(batch); |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 392 | batch->unref(); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | void GrDrawTarget::discard(GrRenderTarget* renderTarget) { |
| 397 | if (this->caps()->discardRenderTargetSupport()) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 398 | GrBatch* batch = new GrDiscardBatch(renderTarget); |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 399 | this->recordBatch(batch); |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 400 | batch->unref(); |
bsalomon | 63b2196 | 2014-11-05 07:05:34 -0800 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 404 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 405 | |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 406 | void GrDrawTarget::copySurface(GrSurface* dst, |
bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 407 | GrSurface* src, |
| 408 | const SkIRect& srcRect, |
| 409 | const SkIPoint& dstPoint) { |
bsalomon | 872062c | 2015-08-18 12:12:35 -0700 | [diff] [blame] | 410 | GrBatch* batch = GrCopySurfaceBatch::Create(dst, src, srcRect, dstPoint); |
| 411 | if (batch) { |
robertphillips | 498d7ac | 2015-10-30 10:11:30 -0700 | [diff] [blame] | 412 | #ifdef ENABLE_MDB |
| 413 | this->addDependency(src); |
| 414 | #endif |
| 415 | |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 416 | this->recordBatch(batch); |
bsalomon | 872062c | 2015-08-18 12:12:35 -0700 | [diff] [blame] | 417 | batch->unref(); |
bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 418 | } |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 419 | } |
| 420 | |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 421 | template <class Left, class Right> static bool intersect(const Left& a, const Right& b) { |
| 422 | SkASSERT(a.fLeft <= a.fRight && a.fTop <= a.fBottom && |
| 423 | b.fLeft <= b.fRight && b.fTop <= b.fBottom); |
| 424 | return a.fLeft < b.fRight && b.fLeft < a.fRight && a.fTop < b.fBottom && b.fTop < a.fBottom; |
| 425 | } |
| 426 | |
| 427 | void GrDrawTarget::recordBatch(GrBatch* batch) { |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 428 | // A closed drawTarget should never receive new/more batches |
robertphillips | 6a18665 | 2015-10-20 07:37:58 -0700 | [diff] [blame] | 429 | SkASSERT(!this->isClosed()); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 430 | |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 431 | // Check if there is a Batch Draw we can batch with by linearly searching back until we either |
| 432 | // 1) check every draw |
| 433 | // 2) intersect with something |
| 434 | // 3) find a 'blocker' |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 435 | GR_AUDIT_TRAIL_ADDBATCH(fAuditTrail, batch->name(), batch->bounds()); |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 436 | GrBATCH_INFO("Re-Recording (%s, B%u)\n" |
joshualitt | e2bcec3 | 2015-09-30 06:22:22 -0700 | [diff] [blame] | 437 | "\tBounds LRTB (%f, %f, %f, %f)\n", |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 438 | batch->name(), |
| 439 | batch->uniqueID(), |
| 440 | batch->bounds().fLeft, batch->bounds().fRight, |
| 441 | batch->bounds().fTop, batch->bounds().fBottom); |
| 442 | GrBATCH_INFO(SkTabString(batch->dumpInfo(), 1).c_str()); |
| 443 | GrBATCH_INFO("\tOutcome:\n"); |
bsalomon | 489147c | 2015-12-14 12:13:09 -0800 | [diff] [blame] | 444 | int maxCandidates = SkTMin(fMaxBatchLookback, fBatches.count()); |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 445 | if (maxCandidates) { |
| 446 | int i = 0; |
| 447 | while (true) { |
| 448 | GrBatch* candidate = fBatches.fromBack(i); |
| 449 | // We cannot continue to search backwards if the render target changes |
| 450 | if (candidate->renderTargetUniqueID() != batch->renderTargetUniqueID()) { |
| 451 | GrBATCH_INFO("\t\tBreaking because of (%s, B%u) Rendertarget\n", |
| 452 | candidate->name(), candidate->uniqueID()); |
| 453 | break; |
| 454 | } |
| 455 | if (candidate->combineIfPossible(batch, *this->caps())) { |
| 456 | GrBATCH_INFO("\t\tCombining with (%s, B%u)\n", candidate->name(), |
| 457 | candidate->uniqueID()); |
| 458 | return; |
| 459 | } |
| 460 | // Stop going backwards if we would cause a painter's order violation. |
bsalomon | db4758c | 2015-11-23 11:14:20 -0800 | [diff] [blame] | 461 | // TODO: The bounds used here do not fully consider the clip. It may be advantageous |
| 462 | // to clip each batch's bounds to the clip. |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 463 | if (intersect(candidate->bounds(), batch->bounds())) { |
| 464 | GrBATCH_INFO("\t\tIntersects with (%s, B%u)\n", candidate->name(), |
| 465 | candidate->uniqueID()); |
| 466 | break; |
| 467 | } |
| 468 | ++i; |
| 469 | if (i == maxCandidates) { |
| 470 | GrBATCH_INFO("\t\tReached max lookback or beginning of batch array %d\n", i); |
| 471 | break; |
| 472 | } |
| 473 | } |
| 474 | } else { |
| 475 | GrBATCH_INFO("\t\tFirstBatch\n"); |
| 476 | } |
| 477 | fBatches.push_back().reset(SkRef(batch)); |
| 478 | } |
| 479 | |
egdaniel | e36914c | 2015-02-13 09:00:33 -0800 | [diff] [blame] | 480 | /////////////////////////////////////////////////////////////////////////////// |
| 481 | |
bsalomon | ad792c1 | 2015-09-10 11:10:50 -0700 | [diff] [blame] | 482 | bool GrDrawTarget::installPipelineInDrawBatch(const GrPipelineBuilder* pipelineBuilder, |
cdalton | d472792 | 2015-11-10 12:49:06 -0800 | [diff] [blame] | 483 | const GrScissorState* scissor, |
| 484 | GrDrawBatch* batch) { |
bsalomon | ad792c1 | 2015-09-10 11:10:50 -0700 | [diff] [blame] | 485 | GrPipeline::CreateArgs args; |
| 486 | args.fPipelineBuilder = pipelineBuilder; |
| 487 | args.fCaps = this->caps(); |
cdalton | d472792 | 2015-11-10 12:49:06 -0800 | [diff] [blame] | 488 | args.fScissor = scissor; |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 489 | batch->getPipelineOptimizations(&args.fOpts); |
| 490 | args.fOpts.fColorPOI.completeCalculations(pipelineBuilder->fColorFragmentProcessors.begin(), |
| 491 | pipelineBuilder->numColorFragmentProcessors()); |
| 492 | args.fOpts.fCoveragePOI.completeCalculations( |
| 493 | pipelineBuilder->fCoverageFragmentProcessors.begin(), |
| 494 | pipelineBuilder->numCoverageFragmentProcessors()); |
ethannicholas | de4166a | 2015-11-30 08:57:38 -0800 | [diff] [blame] | 495 | if (!this->setupDstReadIfNecessary(*pipelineBuilder, args.fOpts, &args.fDstTexture, |
bsalomon | ad792c1 | 2015-09-10 11:10:50 -0700 | [diff] [blame] | 496 | batch->bounds())) { |
| 497 | return false; |
egdaniel | e36914c | 2015-02-13 09:00:33 -0800 | [diff] [blame] | 498 | } |
bsalomon | ad792c1 | 2015-09-10 11:10:50 -0700 | [diff] [blame] | 499 | |
| 500 | if (!batch->installPipeline(args)) { |
| 501 | return false; |
| 502 | } |
| 503 | |
| 504 | return true; |
egdaniel | e36914c | 2015-02-13 09:00:33 -0800 | [diff] [blame] | 505 | } |
| 506 | |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 507 | void GrDrawTarget::clearStencilClip(const SkIRect& rect, bool insideClip, GrRenderTarget* rt) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 508 | GrBatch* batch = new GrClearStencilClipBatch(rect, insideClip, rt); |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 509 | this->recordBatch(batch); |
bsalomon | 5ea0363 | 2015-08-18 10:33:30 -0700 | [diff] [blame] | 510 | batch->unref(); |
| 511 | } |