blob: 41c0d4f9fd672e7bf35bf3af2e4adaa78b3b72ec [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.comac10a2d2010-12-22 21:39:39 +00006 */
7
Robert Phillipsf2361d22016-10-25 14:20:06 -04008#include "GrRenderTargetOpList.h"
joshualitt4d8da812015-01-28 12:53:54 -08009
csmartdalton28341fa2016-08-17 10:00:21 -070010#include "GrAppliedClip.h"
joshualitt086cee12016-01-12 06:45:24 -080011#include "GrAuditTrail.h"
bsalomoneb1cb5c2015-05-22 08:01:09 -070012#include "GrCaps.h"
Brian Osman11052242016-10-27 14:47:55 -040013#include "GrRenderTargetContext.h"
bsalomon4061b122015-05-29 10:26:19 -070014#include "GrGpu.h"
egdaniel9cb63402016-06-23 08:37:05 -070015#include "GrGpuCommandBuffer.h"
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000016#include "GrPath.h"
egdaniele36914c2015-02-13 09:00:33 -080017#include "GrPipeline.h"
joshualittb7133be2015-04-08 09:08:31 -070018#include "GrMemoryPool.h"
robertphillips5fa7f302016-07-21 09:21:04 -070019#include "GrPipelineBuilder.h"
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000020#include "GrRenderTarget.h"
bsalomon4061b122015-05-29 10:26:19 -070021#include "GrResourceProvider.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080022#include "GrRenderTargetPriv.h"
cdalton93a379b2016-05-11 13:58:08 -070023#include "GrStencilAttachment.h"
bsalomonafbf2d62014-09-30 12:18:44 -070024#include "GrSurfacePriv.h"
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000025#include "GrTexture.h"
ethannicholas22793252016-01-30 09:59:10 -080026#include "gl/GrGLRenderTarget.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000027
joshualitt086cee12016-01-12 06:45:24 -080028#include "SkStrokeRec.h"
29
bsalomon9f129de2016-08-10 16:31:05 -070030#include "batches/GrClearBatch.h"
robertphillips9199a9f2016-07-13 07:48:43 -070031#include "batches/GrClearStencilClipBatch.h"
bsalomon872062c2015-08-18 12:12:35 -070032#include "batches/GrCopySurfaceBatch.h"
bsalomon53469832015-08-18 09:20:09 -070033#include "batches/GrDiscardBatch.h"
Brian Salomon9afd3712016-12-01 10:59:09 -050034#include "batches/GrDrawOp.h"
bsalomonadd79ef2015-08-19 13:26:49 -070035#include "batches/GrDrawPathBatch.h"
joshualittecd1a692015-08-10 10:08:26 -070036#include "batches/GrRectBatchFactory.h"
bsalomona44919e2015-08-18 13:28:19 -070037#include "batches/GrStencilPathBatch.h"
joshualitt74417822015-08-07 11:42:16 -070038
csmartdaltona7f29642016-07-07 08:49:11 -070039#include "instanced/InstancedRendering.h"
40
Robert Phillipsf2361d22016-10-25 14:20:06 -040041using gr_instanced::InstancedRendering;
42
reed@google.comac10a2d2010-12-22 21:39:39 +000043////////////////////////////////////////////////////////////////////////////////
44
bsalomon489147c2015-12-14 12:13:09 -080045// Experimentally we have found that most batching occurs within the first 10 comparisons.
bsalomonaecc0182016-03-07 11:50:44 -080046static const int kDefaultMaxBatchLookback = 10;
47static const int kDefaultMaxBatchLookahead = 10;
bsalomon489147c2015-12-14 12:13:09 -080048
Robert Phillipsc7635fa2016-10-28 13:25:24 -040049GrRenderTargetOpList::GrRenderTargetOpList(GrRenderTargetProxy* rtp, GrGpu* gpu,
Robert Phillipsf2361d22016-10-25 14:20:06 -040050 GrResourceProvider* resourceProvider,
51 GrAuditTrail* auditTrail, const Options& options)
Robert Phillipsc7635fa2016-10-28 13:25:24 -040052 : INHERITED(rtp, auditTrail)
Brian Salomon1e41f4a2016-12-07 15:05:04 -050053 , fLastFullClearOp(nullptr)
bsalomonfd8d0132016-08-11 11:25:33 -070054 , fGpu(SkRef(gpu))
csmartdalton7cdda992016-11-01 07:03:03 -070055 , fResourceProvider(resourceProvider)
56 , fLastClipStackGenID(SK_InvalidUniqueID) {
csmartdaltonc6f411e2016-08-05 22:32:12 -070057 // TODO: Stop extracting the context (currently needed by GrClip)
bsalomonb3b9aec2015-09-10 11:16:35 -070058 fContext = fGpu->getContext();
robertphillips4beb5c12015-10-20 07:50:00 -070059
Brian Salomon1e41f4a2016-12-07 15:05:04 -050060 fClipOpToBounds = options.fClipBatchToBounds;
61 fMaxOpLookback = (options.fMaxBatchLookback < 0) ? kDefaultMaxBatchLookback :
bsalomon489147c2015-12-14 12:13:09 -080062 options.fMaxBatchLookback;
Brian Salomon1e41f4a2016-12-07 15:05:04 -050063 fMaxOpLookahead = (options.fMaxBatchLookahead < 0) ? kDefaultMaxBatchLookahead :
bsalomonaecc0182016-03-07 11:50:44 -080064 options.fMaxBatchLookahead;
bsalomon6dea83f2015-12-03 12:58:06 -080065
csmartdaltone0d36292016-07-29 08:14:20 -070066 if (GrCaps::InstancedSupport::kNone != this->caps()->instancedSupport()) {
67 fInstancedRendering.reset(fGpu->createInstancedRendering());
68 }
bsalomon4061b122015-05-29 10:26:19 -070069}
70
Robert Phillipsf2361d22016-10-25 14:20:06 -040071GrRenderTargetOpList::~GrRenderTargetOpList() {
bsalomon4061b122015-05-29 10:26:19 -070072 fGpu->unref();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000073}
74
75////////////////////////////////////////////////////////////////////////////////
76
robertphillips4beb5c12015-10-20 07:50:00 -070077#ifdef SK_DEBUG
Robert Phillipsf2361d22016-10-25 14:20:06 -040078void GrRenderTargetOpList::dump() const {
79 INHERITED::dump();
80
Brian Salomon1e41f4a2016-12-07 15:05:04 -050081 SkDebugf("ops (%d):\n", fRecordedOps.count());
82 for (int i = 0; i < fRecordedOps.count(); ++i) {
robertphillips4beb5c12015-10-20 07:50:00 -070083 SkDebugf("*******************************\n");
Brian Salomon1e41f4a2016-12-07 15:05:04 -050084 if (!fRecordedOps[i].fOp) {
bsalomonaecc0182016-03-07 11:50:44 -080085 SkDebugf("%d: <combined forward>\n", i);
86 } else {
Brian Salomon1e41f4a2016-12-07 15:05:04 -050087 SkDebugf("%d: %s\n", i, fRecordedOps[i].fOp->name());
88 SkString str = fRecordedOps[i].fOp->dumpInfo();
bsalomonaecc0182016-03-07 11:50:44 -080089 SkDebugf("%s\n", str.c_str());
Brian Salomon1e41f4a2016-12-07 15:05:04 -050090 const SkRect& clippedBounds = fRecordedOps[i].fClippedBounds;
bsalomon6cc90062016-07-08 11:31:22 -070091 SkDebugf("ClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n",
92 clippedBounds.fLeft, clippedBounds.fTop, clippedBounds.fRight,
93 clippedBounds.fBottom);
bsalomonaecc0182016-03-07 11:50:44 -080094 }
robertphillips4beb5c12015-10-20 07:50:00 -070095 }
96}
97#endif
98
Brian Salomon5b7b49f2016-12-07 14:31:00 -050099void GrRenderTargetOpList::setupDstTexture(GrRenderTarget* rt,
100 const GrClip& clip,
101 const SkRect& batchBounds,
102 GrXferProcessor::DstTexture* dstTexture) {
bsalomonad792c12015-09-10 11:10:50 -0700103 SkRect bounds = batchBounds;
104 bounds.outset(0.5f, 0.5f);
105
cdalton9954bc32015-04-29 14:17:00 -0700106 if (this->caps()->textureBarrierSupport()) {
107 if (GrTexture* rtTex = rt->asTexture()) {
bsalomondc47ff72015-05-26 12:16:59 -0700108 // The render target is a texture, so we can read from it directly in the shader. The XP
cdalton9954bc32015-04-29 14:17:00 -0700109 // will be responsible to detect this situation and request a texture barrier.
bungeman6bd52842016-10-27 09:30:08 -0700110 dstTexture->setTexture(sk_ref_sp(rtTex));
bsalomon6a44c6a2015-05-26 09:49:05 -0700111 dstTexture->setOffset(0, 0);
Brian Salomon5b7b49f2016-12-07 14:31:00 -0500112 return;
cdalton9954bc32015-04-29 14:17:00 -0700113 }
114 }
115
116 SkIRect copyRect;
cdalton862cff32016-05-12 15:09:48 -0700117 clip.getConservativeBounds(rt->width(), rt->height(), &copyRect);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000118
bsalomonad792c12015-09-10 11:10:50 -0700119 SkIRect drawIBounds;
120 bounds.roundOut(&drawIBounds);
121 if (!copyRect.intersect(drawIBounds)) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000122#ifdef SK_DEBUG
bsalomonb3b9aec2015-09-10 11:16:35 -0700123 GrCapsDebugf(this->caps(), "Missed an early reject. "
Brian Salomon5b7b49f2016-12-07 14:31:00 -0500124 "Bailing on draw from setupDstTexture.\n");
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000125#endif
Brian Salomon5b7b49f2016-12-07 14:31:00 -0500126 return;
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000127 }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000128
commit-bot@chromium.org63150af2013-04-11 22:00:22 +0000129 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
130 // have per-sample dst values by making the copy multisampled.
bsalomonf2703d82014-10-28 14:33:06 -0700131 GrSurfaceDesc desc;
egdaniel4bcd62e2016-08-31 07:37:31 -0700132 if (!fGpu->initDescForDstCopy(rt, &desc)) {
bsalomona73239a2015-04-28 13:35:17 -0700133 desc.fOrigin = kDefault_GrSurfaceOrigin;
134 desc.fFlags = kRenderTarget_GrSurfaceFlag;
135 desc.fConfig = rt->config();
136 }
137
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000138 desc.fWidth = copyRect.width();
139 desc.fHeight = copyRect.height();
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000140
bsalomoneae62002015-07-31 13:59:30 -0700141 static const uint32_t kFlags = 0;
bungeman6bd52842016-10-27 09:30:08 -0700142 sk_sp<GrTexture> copy(fResourceProvider->createApproxTexture(desc, kFlags));
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000143
bsalomone3059732014-10-14 11:47:22 -0700144 if (!copy) {
tfarina38406c82014-10-31 07:11:12 -0700145 SkDebugf("Failed to create temporary copy of destination texture.\n");
Brian Salomon5b7b49f2016-12-07 14:31:00 -0500146 return;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000147 }
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000148 SkIPoint dstPoint = {0, 0};
bungeman6bd52842016-10-27 09:30:08 -0700149 this->copySurface(copy.get(), rt, copyRect, dstPoint);
150 dstTexture->setTexture(std::move(copy));
bsalomon6df86402015-06-01 10:41:49 -0700151 dstTexture->setOffset(copyRect.fLeft, copyRect.fTop);
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000152}
153
Brian Salomon742e31d2016-12-07 17:06:19 -0500154void GrRenderTargetOpList::prepareOps(GrOpFlushState* flushState) {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400155 // Semi-usually the GrOpLists are already closed at this point, but sometimes Ganesh
156 // needs to flush mid-draw. In that case, the SkGpuDevice's GrOpLists won't be closed
157 // but need to be flushed anyway. Closing such GrOpLists here will mean new
158 // GrOpLists will be created to replace them if the SkGpuDevice(s) write to them again.
robertphillipsa106c622015-10-16 09:07:06 -0700159 this->makeClosed();
160
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500161 // Loop over the ops that haven't yet been prepared.
162 for (int i = 0; i < fRecordedOps.count(); ++i) {
163 if (fRecordedOps[i].fOp) {
164 fRecordedOps[i].fOp->prepare(flushState);
bsalomonaecc0182016-03-07 11:50:44 -0800165 }
bsalomon512be532015-09-10 10:42:55 -0700166 }
csmartdaltona7f29642016-07-07 08:49:11 -0700167
168 if (fInstancedRendering) {
169 fInstancedRendering->beginFlush(flushState->resourceProvider());
170 }
robertphillipsa13e2022015-11-11 12:01:09 -0800171}
bsalomon512be532015-09-10 10:42:55 -0700172
Brian Salomon25a88092016-12-01 09:36:50 -0500173// TODO: this is where GrOp::renderTarget is used (which is fine since it
Robert Phillips294870f2016-11-11 12:38:40 -0500174// is at flush time). However, we need to store the RenderTargetProxy in the
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500175// Ops and instantiate them here.
Brian Salomon742e31d2016-12-07 17:06:19 -0500176bool GrRenderTargetOpList::executeOps(GrOpFlushState* flushState) {
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500177 if (0 == fRecordedOps.count()) {
bsalomondc438982016-08-31 11:53:49 -0700178 return false;
egdanielb4021cf2016-07-28 08:53:07 -0700179 }
bsalomon512be532015-09-10 10:42:55 -0700180 // Draw all the generated geometry.
bsalomon6dea83f2015-12-03 12:58:06 -0800181 SkRandom random;
Brian Salomon3a7492f2016-11-30 10:52:10 -0500182 GrGpuResource::UniqueID currentRTID = GrGpuResource::UniqueID::InvalidID();
Ben Wagner145dbcd2016-11-03 14:40:50 -0400183 std::unique_ptr<GrGpuCommandBuffer> commandBuffer;
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500184 for (int i = 0; i < fRecordedOps.count(); ++i) {
185 if (!fRecordedOps[i].fOp) {
bsalomonaecc0182016-03-07 11:50:44 -0800186 continue;
187 }
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500188 if (fRecordedOps[i].fOp->renderTargetUniqueID() != currentRTID) {
egdaniel9cb63402016-06-23 08:37:05 -0700189 if (commandBuffer) {
190 commandBuffer->end();
Greg Daniel36a77ee2016-10-18 10:33:25 -0400191 commandBuffer->submit();
egdaniel9cb63402016-06-23 08:37:05 -0700192 commandBuffer.reset();
193 }
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500194 currentRTID = fRecordedOps[i].fOp->renderTargetUniqueID();
Brian Salomon3a7492f2016-11-30 10:52:10 -0500195 if (!currentRTID.isInvalid()) {
egdaniel9cb63402016-06-23 08:37:05 -0700196 static const GrGpuCommandBuffer::LoadAndStoreInfo kBasicLoadStoreInfo
197 { GrGpuCommandBuffer::LoadOp::kLoad,GrGpuCommandBuffer::StoreOp::kStore,
198 GrColor_ILLEGAL };
Brian Salomonc293a292016-11-30 13:38:32 -0500199 commandBuffer.reset(fGpu->createCommandBuffer(kBasicLoadStoreInfo, // Color
egdaniel9cb63402016-06-23 08:37:05 -0700200 kBasicLoadStoreInfo)); // Stencil
201 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400202 flushState->setCommandBuffer(commandBuffer.get());
egdaniel9cb63402016-06-23 08:37:05 -0700203 }
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500204 fRecordedOps[i].fOp->draw(flushState, fRecordedOps[i].fClippedBounds);
bsalomon512be532015-09-10 10:42:55 -0700205 }
egdaniel9cb63402016-06-23 08:37:05 -0700206 if (commandBuffer) {
207 commandBuffer->end();
Greg Daniel36a77ee2016-10-18 10:33:25 -0400208 commandBuffer->submit();
egdaniel9cb63402016-06-23 08:37:05 -0700209 flushState->setCommandBuffer(nullptr);
210 }
ethannicholas22793252016-01-30 09:59:10 -0800211
Robert Phillipsf2361d22016-10-25 14:20:06 -0400212 fGpu->finishOpList();
bsalomondc438982016-08-31 11:53:49 -0700213 return true;
bsalomona73239a2015-04-28 13:35:17 -0700214}
215
Robert Phillipsf2361d22016-10-25 14:20:06 -0400216void GrRenderTargetOpList::reset() {
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500217 fLastFullClearOp = nullptr;
218 fRecordedOps.reset();
csmartdaltona7f29642016-07-07 08:49:11 -0700219 if (fInstancedRendering) {
220 fInstancedRendering->endFlush();
221 }
bsalomon512be532015-09-10 10:42:55 -0700222}
223
Robert Phillipsf2361d22016-10-25 14:20:06 -0400224void GrRenderTargetOpList::abandonGpuResources() {
225 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
226 InstancedRendering* ir = this->instancedRendering();
227 ir->resetGpuResources(InstancedRendering::ResetType::kAbandon);
228 }
229}
230
231void GrRenderTargetOpList::freeGpuResources() {
232 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
233 InstancedRendering* ir = this->instancedRendering();
234 ir->resetGpuResources(InstancedRendering::ResetType::kDestroy);
235 }
236}
237
Brian Salomon25a88092016-12-01 09:36:50 -0500238static void batch_bounds(SkRect* bounds, const GrOp* batch) {
bsalomon88cf17d2016-07-08 06:40:56 -0700239 *bounds = batch->bounds();
240 if (batch->hasZeroArea()) {
241 if (batch->hasAABloat()) {
242 bounds->outset(0.5f, 0.5f);
243 } else {
244 // We don't know which way the particular GPU will snap lines or points at integer
245 // coords. So we ensure that the bounds is large enough for either snap.
246 SkRect before = *bounds;
247 bounds->roundOut(bounds);
248 if (bounds->fLeft == before.fLeft) {
249 bounds->fLeft -= 1;
250 }
251 if (bounds->fTop == before.fTop) {
252 bounds->fTop -= 1;
253 }
254 if (bounds->fRight == before.fRight) {
255 bounds->fRight += 1;
256 }
257 if (bounds->fBottom == before.fBottom) {
258 bounds->fBottom += 1;
259 }
260 }
261 }
262}
263
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500264void GrRenderTargetOpList::addDrawOp(const GrPipelineBuilder& pipelineBuilder,
Brian Osman11052242016-10-27 14:47:55 -0400265 GrRenderTargetContext* renderTargetContext,
Robert Phillipsf2361d22016-10-25 14:20:06 -0400266 const GrClip& clip,
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500267 GrDrawOp* op) {
joshualitt4d8da812015-01-28 12:53:54 -0800268 // Setup clip
bsalomon88cf17d2016-07-08 06:40:56 -0700269 SkRect bounds;
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500270 batch_bounds(&bounds, op);
csmartdaltond211e782016-08-15 11:17:19 -0700271 GrAppliedClip appliedClip(bounds);
Brian Osman11052242016-10-27 14:47:55 -0400272 if (!clip.apply(fContext, renderTargetContext, pipelineBuilder.isHWAntialias(),
csmartdaltond211e782016-08-15 11:17:19 -0700273 pipelineBuilder.hasUserStencilSettings(), &appliedClip)) {
cdalton862cff32016-05-12 15:09:48 -0700274 return;
joshualitt4d8da812015-01-28 12:53:54 -0800275 }
robertphillips391395d2016-03-02 09:26:36 -0800276
robertphillips55fdccc2016-06-06 06:16:20 -0700277 // TODO: this is the only remaining usage of the AutoRestoreFragmentProcessorState - remove it
cdaltond4727922015-11-10 12:49:06 -0800278 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
csmartdaltond211e782016-08-15 11:17:19 -0700279 if (appliedClip.clipCoverageFragmentProcessor()) {
cdaltond4727922015-11-10 12:49:06 -0800280 arfps.set(&pipelineBuilder);
csmartdaltond211e782016-08-15 11:17:19 -0700281 arfps.addCoverageFragmentProcessor(sk_ref_sp(appliedClip.clipCoverageFragmentProcessor()));
cdaltond4727922015-11-10 12:49:06 -0800282 }
joshualitt4d8da812015-01-28 12:53:54 -0800283
cdalton862cff32016-05-12 15:09:48 -0700284 if (pipelineBuilder.hasUserStencilSettings() || appliedClip.hasStencilClip()) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500285 if (!renderTargetContext->accessRenderTarget()) {
286 return;
287 }
288
Brian Osman11052242016-10-27 14:47:55 -0400289 if (!fResourceProvider->attachStencilAttachment(
290 renderTargetContext->accessRenderTarget())) {
cdalton17bf8202016-05-13 11:27:15 -0700291 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
292 return;
293 }
cdalton193d9cf2016-05-12 11:52:02 -0700294 }
csmartdaltond211e782016-08-15 11:17:19 -0700295
296 GrPipeline::CreateArgs args;
297 args.fPipelineBuilder = &pipelineBuilder;
Brian Osman11052242016-10-27 14:47:55 -0400298 args.fRenderTargetContext = renderTargetContext;
csmartdaltond211e782016-08-15 11:17:19 -0700299 args.fCaps = this->caps();
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500300 op->getPipelineOptimizations(&args.fOpts);
301 if (args.fOpts.fOverrides.fUsePLSDstRead || fClipOpToBounds) {
cdalton193d9cf2016-05-12 11:52:02 -0700302 GrGLIRect viewport;
303 viewport.fLeft = 0;
304 viewport.fBottom = 0;
Brian Osman11052242016-10-27 14:47:55 -0400305 viewport.fWidth = renderTargetContext->width();
306 viewport.fHeight = renderTargetContext->height();
cdalton193d9cf2016-05-12 11:52:02 -0700307 SkIRect ibounds;
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500308 ibounds.fLeft = SkTPin(SkScalarFloorToInt(op->bounds().fLeft), viewport.fLeft,
cdalton193d9cf2016-05-12 11:52:02 -0700309 viewport.fWidth);
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500310 ibounds.fTop = SkTPin(SkScalarFloorToInt(op->bounds().fTop), viewport.fBottom,
cdalton193d9cf2016-05-12 11:52:02 -0700311 viewport.fHeight);
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500312 ibounds.fRight = SkTPin(SkScalarCeilToInt(op->bounds().fRight), viewport.fLeft,
cdalton193d9cf2016-05-12 11:52:02 -0700313 viewport.fWidth);
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500314 ibounds.fBottom = SkTPin(SkScalarCeilToInt(op->bounds().fBottom), viewport.fBottom,
cdalton193d9cf2016-05-12 11:52:02 -0700315 viewport.fHeight);
csmartdaltond211e782016-08-15 11:17:19 -0700316 if (!appliedClip.addScissor(ibounds)) {
317 return;
cdalton193d9cf2016-05-12 11:52:02 -0700318 }
cdalton193d9cf2016-05-12 11:52:02 -0700319 }
bungeman06ca8ec2016-06-09 08:01:03 -0700320 args.fOpts.fColorPOI.completeCalculations(
321 sk_sp_address_as_pointer_address(pipelineBuilder.fColorFragmentProcessors.begin()),
322 pipelineBuilder.numColorFragmentProcessors());
cdalton193d9cf2016-05-12 11:52:02 -0700323 args.fOpts.fCoveragePOI.completeCalculations(
bungeman06ca8ec2016-06-09 08:01:03 -0700324 sk_sp_address_as_pointer_address(pipelineBuilder.fCoverageFragmentProcessors.begin()),
325 pipelineBuilder.numCoverageFragmentProcessors());
csmartdaltond211e782016-08-15 11:17:19 -0700326 args.fScissor = &appliedClip.scissorState();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700327 args.fWindowRectsState = &appliedClip.windowRectsState();
csmartdaltond211e782016-08-15 11:17:19 -0700328 args.fHasStencilClip = appliedClip.hasStencilClip();
Robert Phillipse60ad622016-11-17 10:22:48 -0500329 if (!renderTargetContext->accessRenderTarget()) {
330 return;
331 }
332
Brian Salomon5b7b49f2016-12-07 14:31:00 -0500333 if (pipelineBuilder.willXPNeedDstTexture(*this->caps(), args.fOpts)) {
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500334 this->setupDstTexture(renderTargetContext->accessRenderTarget(), clip, op->bounds(),
Brian Salomon5b7b49f2016-12-07 14:31:00 -0500335 &args.fDstTexture);
336 if (!args.fDstTexture.texture()) {
337 return;
338 }
cdalton193d9cf2016-05-12 11:52:02 -0700339 }
340
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500341 if (!op->installPipeline(args)) {
egdaniele36914c2015-02-13 09:00:33 -0800342 return;
343 }
bsalomonad792c12015-09-10 11:10:50 -0700344
robertphillips498d7ac2015-10-30 10:11:30 -0700345#ifdef ENABLE_MDB
Robert Phillipsf2361d22016-10-25 14:20:06 -0400346 SkASSERT(fSurface);
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500347 op->pipeline()->addDependenciesTo(fSurface);
robertphillips498d7ac2015-10-30 10:11:30 -0700348#endif
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500349 this->recordOp(op, appliedClip.clippedDrawBounds());
joshualitt4d8da812015-01-28 12:53:54 -0800350}
351
Brian Osman11052242016-10-27 14:47:55 -0400352void GrRenderTargetOpList::stencilPath(GrRenderTargetContext* renderTargetContext,
Robert Phillipsf2361d22016-10-25 14:20:06 -0400353 const GrClip& clip,
354 bool useHWAA,
355 const SkMatrix& viewMatrix,
356 const GrPath* path) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000357 // TODO: extract portions of checkDraw that are relevant to path stenciling.
bsalomon49f085d2014-09-05 13:34:00 -0700358 SkASSERT(path);
jvanverthe9c0fc62015-04-29 11:18:05 -0700359 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
joshualitt2c93efe2014-11-06 12:57:13 -0800360
csmartdaltond211e782016-08-15 11:17:19 -0700361 // FIXME: Use path bounds instead of this WAR once
362 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
Brian Osman11052242016-10-27 14:47:55 -0400363 SkRect bounds = SkRect::MakeIWH(renderTargetContext->width(), renderTargetContext->height());
csmartdaltond211e782016-08-15 11:17:19 -0700364
joshualitt2c93efe2014-11-06 12:57:13 -0800365 // Setup clip
csmartdaltond211e782016-08-15 11:17:19 -0700366 GrAppliedClip appliedClip(bounds);
Brian Osman11052242016-10-27 14:47:55 -0400367 if (!clip.apply(fContext, renderTargetContext, useHWAA, true, &appliedClip)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800368 return;
369 }
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500370 // TODO: respect fClipOpToBounds if we ever start computing bounds here.
joshualitt2c93efe2014-11-06 12:57:13 -0800371
cdalton846c0512016-05-13 10:25:00 -0700372 // Coverage AA does not make sense when rendering to the stencil buffer. The caller should never
373 // attempt this in a situation that would require coverage AA.
csmartdaltond211e782016-08-15 11:17:19 -0700374 SkASSERT(!appliedClip.clipCoverageFragmentProcessor());
bsalomon0ba8c242015-10-07 09:20:28 -0700375
Robert Phillipse60ad622016-11-17 10:22:48 -0500376 if (!renderTargetContext->accessRenderTarget()) {
377 return;
378 }
robertphillips55fdccc2016-06-06 06:16:20 -0700379 GrStencilAttachment* stencilAttachment = fResourceProvider->attachStencilAttachment(
Brian Osman11052242016-10-27 14:47:55 -0400380 renderTargetContext->accessRenderTarget());
cdalton17bf8202016-05-13 11:27:15 -0700381 if (!stencilAttachment) {
382 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
383 return;
384 }
joshualitt2c93efe2014-11-06 12:57:13 -0800385
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500386 GrOp* op = GrStencilPathBatch::Create(viewMatrix,
387 useHWAA,
388 path->getFillType(),
389 appliedClip.hasStencilClip(),
390 stencilAttachment->bits(),
391 appliedClip.scissorState(),
392 renderTargetContext->accessRenderTarget(),
393 path);
394 this->recordOp(op, appliedClip.clippedDrawBounds());
395 op->unref();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000396}
397
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500398void GrRenderTargetOpList::addOp(sk_sp<GrOp> op) { this->recordOp(op.get(), op->bounds()); }
bsalomon53469832015-08-18 09:20:09 -0700399
Robert Phillipsf2361d22016-10-25 14:20:06 -0400400void GrRenderTargetOpList::fullClear(GrRenderTarget* renderTarget, GrColor color) {
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500401 // Currently this just inserts or updates the last clear op. However, once in MDB this can
402 // remove all the previously recorded ops and change the load op to clear with supplied
bsalomonfd8d0132016-08-11 11:25:33 -0700403 // color.
Robert Phillips294870f2016-11-11 12:38:40 -0500404 // TODO: this needs to be updated to use GrSurfaceProxy::UniqueID
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500405 if (fLastFullClearOp &&
406 fLastFullClearOp->renderTargetUniqueID() == renderTarget->uniqueID()) {
407 // As currently implemented, fLastFullClearOp should be the last op because we would
408 // have cleared it when another op was recorded.
409 SkASSERT(fRecordedOps.back().fOp.get() == fLastFullClearOp);
410 fLastFullClearOp->setColor(color);
bsalomonfd8d0132016-08-11 11:25:33 -0700411 return;
412 }
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500413 sk_sp<GrClearBatch> op(GrClearBatch::Make(GrFixedClip::Disabled(), color, renderTarget));
414 if (op.get() == this->recordOp(op.get(), op->bounds())) {
415 fLastFullClearOp = op.get();
bsalomonfd8d0132016-08-11 11:25:33 -0700416 }
bsalomon9f129de2016-08-10 16:31:05 -0700417}
418
Robert Phillipsf2361d22016-10-25 14:20:06 -0400419void GrRenderTargetOpList::discard(GrRenderTarget* renderTarget) {
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500420 // Currently this just inserts a discard op. However, once in MDB this can remove all the
421 // previously recorded ops and change the load op to discard.
bsalomon53469832015-08-18 09:20:09 -0700422 if (this->caps()->discardRenderTargetSupport()) {
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500423 GrOp* op = new GrDiscardBatch(renderTarget);
424 this->recordOp(op, op->bounds());
425 op->unref();
bsalomon63b21962014-11-05 07:05:34 -0800426 }
427}
428
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000429////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000430
Robert Phillipsf2361d22016-10-25 14:20:06 -0400431bool GrRenderTargetOpList::copySurface(GrSurface* dst,
432 GrSurface* src,
433 const SkIRect& srcRect,
434 const SkIPoint& dstPoint) {
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500435 GrOp* op = GrCopySurfaceBatch::Create(dst, src, srcRect, dstPoint);
436 if (!op) {
bsalomonb8fea972016-02-16 07:34:17 -0800437 return false;
438 }
robertphillips498d7ac2015-10-30 10:11:30 -0700439#ifdef ENABLE_MDB
bsalomonb8fea972016-02-16 07:34:17 -0800440 this->addDependency(src);
robertphillips498d7ac2015-10-30 10:11:30 -0700441#endif
442
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500443 this->recordOp(op, op->bounds());
444 op->unref();
bsalomonb8fea972016-02-16 07:34:17 -0800445 return true;
bsalomon@google.comeb851172013-04-15 13:51:00 +0000446}
447
bsalomon6cc90062016-07-08 11:31:22 -0700448static inline bool can_reorder(const SkRect& a, const SkRect& b) {
bsalomon88cf17d2016-07-08 06:40:56 -0700449 return a.fRight <= b.fLeft || a.fBottom <= b.fTop ||
450 b.fRight <= a.fLeft || b.fBottom <= a.fTop;
451}
452
bsalomon6cc90062016-07-08 11:31:22 -0700453static void join(SkRect* out, const SkRect& a, const SkRect& b) {
454 SkASSERT(a.fLeft <= a.fRight && a.fTop <= a.fBottom);
455 SkASSERT(b.fLeft <= b.fRight && b.fTop <= b.fBottom);
456 out->fLeft = SkTMin(a.fLeft, b.fLeft);
457 out->fTop = SkTMin(a.fTop, b.fTop);
458 out->fRight = SkTMax(a.fRight, b.fRight);
459 out->fBottom = SkTMax(a.fBottom, b.fBottom);
bsalomon512be532015-09-10 10:42:55 -0700460}
461
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500462GrOp* GrRenderTargetOpList::recordOp(GrOp* op, const SkRect& clippedBounds) {
463 // A closed GrOpList should never receive new/more ops
robertphillips6a186652015-10-20 07:37:58 -0700464 SkASSERT(!this->isClosed());
robertphillipsa106c622015-10-16 09:07:06 -0700465
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500466 // Check if there is an op we can combine with by linearly searching back until we either
467 // 1) check every op
bsalomon512be532015-09-10 10:42:55 -0700468 // 2) intersect with something
469 // 3) find a 'blocker'
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500470 GR_AUDIT_TRAIL_ADDBATCH(fAuditTrail, op);
Brian Salomon25a88092016-12-01 09:36:50 -0500471 GrOP_INFO("Re-Recording (%s, B%u)\n"
472 "\tBounds LRTB (%f, %f, %f, %f)\n",
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500473 op->name(),
474 op->uniqueID(),
475 op->bounds().fLeft, op->bounds().fRight,
476 op->bounds().fTop, op->bounds().fBottom);
477 GrOP_INFO(SkTabString(op->dumpInfo(), 1).c_str());
Brian Salomon25a88092016-12-01 09:36:50 -0500478 GrOP_INFO("\tClipped Bounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n",
479 clippedBounds.fLeft, clippedBounds.fTop, clippedBounds.fRight,
480 clippedBounds.fBottom);
481 GrOP_INFO("\tOutcome:\n");
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500482 int maxCandidates = SkTMin(fMaxOpLookback, fRecordedOps.count());
bsalomon512be532015-09-10 10:42:55 -0700483 if (maxCandidates) {
484 int i = 0;
485 while (true) {
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500486 GrOp* candidate = fRecordedOps.fromBack(i).fOp.get();
bsalomon512be532015-09-10 10:42:55 -0700487 // We cannot continue to search backwards if the render target changes
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500488 if (candidate->renderTargetUniqueID() != op->renderTargetUniqueID()) {
Brian Salomon25a88092016-12-01 09:36:50 -0500489 GrOP_INFO("\t\tBreaking because of (%s, B%u) Rendertarget\n",
490 candidate->name(), candidate->uniqueID());
bsalomon512be532015-09-10 10:42:55 -0700491 break;
492 }
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500493 if (candidate->combineIfPossible(op, *this->caps())) {
Brian Salomon25a88092016-12-01 09:36:50 -0500494 GrOP_INFO("\t\tCombining with (%s, B%u)\n", candidate->name(),
495 candidate->uniqueID());
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500496 GR_AUDIT_TRAIL_BATCHING_RESULT_COMBINED(fAuditTrail, candidate, op);
497 join(&fRecordedOps.fromBack(i).fClippedBounds,
498 fRecordedOps.fromBack(i).fClippedBounds, clippedBounds);
bsalomonfd8d0132016-08-11 11:25:33 -0700499 return candidate;
bsalomon512be532015-09-10 10:42:55 -0700500 }
501 // Stop going backwards if we would cause a painter's order violation.
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500502 const SkRect& candidateBounds = fRecordedOps.fromBack(i).fClippedBounds;
bsalomon6cc90062016-07-08 11:31:22 -0700503 if (!can_reorder(candidateBounds, clippedBounds)) {
Brian Salomon25a88092016-12-01 09:36:50 -0500504 GrOP_INFO("\t\tIntersects with (%s, B%u)\n", candidate->name(),
505 candidate->uniqueID());
bsalomon512be532015-09-10 10:42:55 -0700506 break;
507 }
508 ++i;
509 if (i == maxCandidates) {
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500510 GrOP_INFO("\t\tReached max lookback or beginning of op array %d\n", i);
bsalomon512be532015-09-10 10:42:55 -0700511 break;
512 }
513 }
514 } else {
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500515 GrOP_INFO("\t\tFirstOp\n");
bsalomon512be532015-09-10 10:42:55 -0700516 }
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500517 GR_AUDIT_TRAIL_BATCHING_RESULT_NEW(fAuditTrail, op);
518 fRecordedOps.emplace_back(RecordedOp{sk_ref_sp(op), clippedBounds});
519 fLastFullClearOp = nullptr;
520 return op;
bsalomon512be532015-09-10 10:42:55 -0700521}
522
Robert Phillipsf2361d22016-10-25 14:20:06 -0400523void GrRenderTargetOpList::forwardCombine() {
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500524 if (fMaxOpLookahead <= 0) {
bsalomondb27fc52016-08-29 12:43:27 -0700525 return;
526 }
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500527 for (int i = 0; i < fRecordedOps.count() - 2; ++i) {
528 GrOp* op = fRecordedOps[i].fOp.get();
529 const SkRect& opBounds = fRecordedOps[i].fClippedBounds;
530 int maxCandidateIdx = SkTMin(i + fMaxOpLookahead, fRecordedOps.count() - 1);
bsalomonaecc0182016-03-07 11:50:44 -0800531 int j = i + 1;
532 while (true) {
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500533 GrOp* candidate = fRecordedOps[j].fOp.get();
bsalomonaecc0182016-03-07 11:50:44 -0800534 // We cannot continue to search if the render target changes
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500535 if (candidate->renderTargetUniqueID() != op->renderTargetUniqueID()) {
Brian Salomon25a88092016-12-01 09:36:50 -0500536 GrOP_INFO("\t\tBreaking because of (%s, B%u) Rendertarget\n",
537 candidate->name(), candidate->uniqueID());
bsalomonaecc0182016-03-07 11:50:44 -0800538 break;
539 }
540 if (j == i +1) {
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500541 // We assume op would have combined with candidate when the candidate was added
542 // via backwards combining in recordOp.
543 SkASSERT(!op->combineIfPossible(candidate, *this->caps()));
544 } else if (op->combineIfPossible(candidate, *this->caps())) {
Brian Salomon25a88092016-12-01 09:36:50 -0500545 GrOP_INFO("\t\tCombining with (%s, B%u)\n", candidate->name(),
546 candidate->uniqueID());
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500547 GR_AUDIT_TRAIL_BATCHING_RESULT_COMBINED(fAuditTrail, op, candidate);
548 fRecordedOps[j].fOp = std::move(fRecordedOps[i].fOp);
549 join(&fRecordedOps[j].fClippedBounds, fRecordedOps[j].fClippedBounds, opBounds);
bsalomonaecc0182016-03-07 11:50:44 -0800550 break;
551 }
552 // Stop going traversing if we would cause a painter's order violation.
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500553 const SkRect& candidateBounds = fRecordedOps[j].fClippedBounds;
554 if (!can_reorder(candidateBounds, opBounds)) {
Brian Salomon25a88092016-12-01 09:36:50 -0500555 GrOP_INFO("\t\tIntersects with (%s, B%u)\n", candidate->name(),
556 candidate->uniqueID());
bsalomonaecc0182016-03-07 11:50:44 -0800557 break;
558 }
559 ++j;
560 if (j > maxCandidateIdx) {
Brian Salomon25a88092016-12-01 09:36:50 -0500561 GrOP_INFO("\t\tReached max lookahead or end of batch array %d\n", i);
bsalomonaecc0182016-03-07 11:50:44 -0800562 break;
563 }
564 }
565 }
566}
567
egdaniele36914c2015-02-13 09:00:33 -0800568///////////////////////////////////////////////////////////////////////////////
569
Robert Phillipsf2361d22016-10-25 14:20:06 -0400570void GrRenderTargetOpList::clearStencilClip(const GrFixedClip& clip,
571 bool insideStencilMask,
572 GrRenderTarget* rt) {
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500573 GrOp* op = new GrClearStencilClipBatch(clip, insideStencilMask, rt);
574 this->recordOp(op, op->bounds());
575 op->unref();
bsalomon5ea03632015-08-18 10:33:30 -0700576}