blob: b69d915f06ba508e8e9c151e4df3966d66a88d7b [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"
bsalomon16b99132015-08-13 14:55:50 -070034#include "batches/GrDrawBatch.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)
Robert Phillipsf2361d22016-10-25 14:20:06 -040053 , fLastFullClearBatch(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
cdalton862cff32016-05-12 15:09:48 -070060 fClipBatchToBounds = options.fClipBatchToBounds;
bsalomon6dea83f2015-12-03 12:58:06 -080061 fDrawBatchBounds = options.fDrawBatchBounds;
bsalomon489147c2015-12-14 12:13:09 -080062 fMaxBatchLookback = (options.fMaxBatchLookback < 0) ? kDefaultMaxBatchLookback :
63 options.fMaxBatchLookback;
bsalomonaecc0182016-03-07 11:50:44 -080064 fMaxBatchLookahead = (options.fMaxBatchLookahead < 0) ? kDefaultMaxBatchLookahead :
65 options.fMaxBatchLookahead;
bsalomon6dea83f2015-12-03 12:58:06 -080066
csmartdaltone0d36292016-07-29 08:14:20 -070067 if (GrCaps::InstancedSupport::kNone != this->caps()->instancedSupport()) {
68 fInstancedRendering.reset(fGpu->createInstancedRendering());
69 }
bsalomon4061b122015-05-29 10:26:19 -070070}
71
Robert Phillipsf2361d22016-10-25 14:20:06 -040072GrRenderTargetOpList::~GrRenderTargetOpList() {
bsalomon4061b122015-05-29 10:26:19 -070073 fGpu->unref();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000074}
75
76////////////////////////////////////////////////////////////////////////////////
77
robertphillips4beb5c12015-10-20 07:50:00 -070078#ifdef SK_DEBUG
Robert Phillipsf2361d22016-10-25 14:20:06 -040079void GrRenderTargetOpList::dump() const {
80 INHERITED::dump();
81
bsalomon6cc90062016-07-08 11:31:22 -070082 SkDebugf("batches (%d):\n", fRecordedBatches.count());
83 for (int i = 0; i < fRecordedBatches.count(); ++i) {
robertphillips4beb5c12015-10-20 07:50:00 -070084 SkDebugf("*******************************\n");
bsalomon6cc90062016-07-08 11:31:22 -070085 if (!fRecordedBatches[i].fBatch) {
bsalomonaecc0182016-03-07 11:50:44 -080086 SkDebugf("%d: <combined forward>\n", i);
87 } else {
bsalomon6cc90062016-07-08 11:31:22 -070088 SkDebugf("%d: %s\n", i, fRecordedBatches[i].fBatch->name());
89 SkString str = fRecordedBatches[i].fBatch->dumpInfo();
bsalomonaecc0182016-03-07 11:50:44 -080090 SkDebugf("%s\n", str.c_str());
bsalomon6cc90062016-07-08 11:31:22 -070091 const SkRect& clippedBounds = fRecordedBatches[i].fClippedBounds;
92 SkDebugf("ClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n",
93 clippedBounds.fLeft, clippedBounds.fTop, clippedBounds.fRight,
94 clippedBounds.fBottom);
bsalomonaecc0182016-03-07 11:50:44 -080095 }
robertphillips4beb5c12015-10-20 07:50:00 -070096 }
97}
98#endif
99
Robert Phillipsf2361d22016-10-25 14:20:06 -0400100bool GrRenderTargetOpList::setupDstReadIfNecessary(const GrPipelineBuilder& pipelineBuilder,
101 GrRenderTarget* rt,
102 const GrClip& clip,
103 const GrPipelineOptimizations& optimizations,
104 GrXferProcessor::DstTexture* dstTexture,
105 const SkRect& batchBounds) {
bsalomonad792c12015-09-10 11:10:50 -0700106 SkRect bounds = batchBounds;
107 bounds.outset(0.5f, 0.5f);
108
ethannicholasde4166a2015-11-30 08:57:38 -0800109 if (!pipelineBuilder.willXPNeedDstTexture(*this->caps(), optimizations)) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000110 return true;
111 }
cdalton9954bc32015-04-29 14:17:00 -0700112
cdalton9954bc32015-04-29 14:17:00 -0700113 if (this->caps()->textureBarrierSupport()) {
114 if (GrTexture* rtTex = rt->asTexture()) {
bsalomondc47ff72015-05-26 12:16:59 -0700115 // The render target is a texture, so we can read from it directly in the shader. The XP
cdalton9954bc32015-04-29 14:17:00 -0700116 // will be responsible to detect this situation and request a texture barrier.
bungeman6bd52842016-10-27 09:30:08 -0700117 dstTexture->setTexture(sk_ref_sp(rtTex));
bsalomon6a44c6a2015-05-26 09:49:05 -0700118 dstTexture->setOffset(0, 0);
cdalton9954bc32015-04-29 14:17:00 -0700119 return true;
120 }
121 }
122
123 SkIRect copyRect;
cdalton862cff32016-05-12 15:09:48 -0700124 clip.getConservativeBounds(rt->width(), rt->height(), &copyRect);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000125
bsalomonad792c12015-09-10 11:10:50 -0700126 SkIRect drawIBounds;
127 bounds.roundOut(&drawIBounds);
128 if (!copyRect.intersect(drawIBounds)) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000129#ifdef SK_DEBUG
bsalomonb3b9aec2015-09-10 11:16:35 -0700130 GrCapsDebugf(this->caps(), "Missed an early reject. "
131 "Bailing on draw from setupDstReadIfNecessary.\n");
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000132#endif
bsalomonad792c12015-09-10 11:10:50 -0700133 return false;
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000134 }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000135
commit-bot@chromium.org63150af2013-04-11 22:00:22 +0000136 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
137 // have per-sample dst values by making the copy multisampled.
bsalomonf2703d82014-10-28 14:33:06 -0700138 GrSurfaceDesc desc;
egdaniel4bcd62e2016-08-31 07:37:31 -0700139 if (!fGpu->initDescForDstCopy(rt, &desc)) {
bsalomona73239a2015-04-28 13:35:17 -0700140 desc.fOrigin = kDefault_GrSurfaceOrigin;
141 desc.fFlags = kRenderTarget_GrSurfaceFlag;
142 desc.fConfig = rt->config();
143 }
144
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000145 desc.fWidth = copyRect.width();
146 desc.fHeight = copyRect.height();
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000147
bsalomoneae62002015-07-31 13:59:30 -0700148 static const uint32_t kFlags = 0;
bungeman6bd52842016-10-27 09:30:08 -0700149 sk_sp<GrTexture> copy(fResourceProvider->createApproxTexture(desc, kFlags));
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000150
bsalomone3059732014-10-14 11:47:22 -0700151 if (!copy) {
tfarina38406c82014-10-31 07:11:12 -0700152 SkDebugf("Failed to create temporary copy of destination texture.\n");
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000153 return false;
154 }
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000155 SkIPoint dstPoint = {0, 0};
bungeman6bd52842016-10-27 09:30:08 -0700156 this->copySurface(copy.get(), rt, copyRect, dstPoint);
157 dstTexture->setTexture(std::move(copy));
bsalomon6df86402015-06-01 10:41:49 -0700158 dstTexture->setOffset(copyRect.fLeft, copyRect.fTop);
159 return true;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000160}
161
Robert Phillipsf2361d22016-10-25 14:20:06 -0400162void GrRenderTargetOpList::prepareBatches(GrBatchFlushState* flushState) {
163 // Semi-usually the GrOpLists are already closed at this point, but sometimes Ganesh
164 // needs to flush mid-draw. In that case, the SkGpuDevice's GrOpLists won't be closed
165 // but need to be flushed anyway. Closing such GrOpLists here will mean new
166 // GrOpLists will be created to replace them if the SkGpuDevice(s) write to them again.
robertphillipsa106c622015-10-16 09:07:06 -0700167 this->makeClosed();
168
robertphillips498d7ac2015-10-30 10:11:30 -0700169 // Loop over the batches that haven't yet generated their geometry
bsalomon6cc90062016-07-08 11:31:22 -0700170 for (int i = 0; i < fRecordedBatches.count(); ++i) {
171 if (fRecordedBatches[i].fBatch) {
172 fRecordedBatches[i].fBatch->prepare(flushState);
bsalomonaecc0182016-03-07 11:50:44 -0800173 }
bsalomon512be532015-09-10 10:42:55 -0700174 }
csmartdaltona7f29642016-07-07 08:49:11 -0700175
176 if (fInstancedRendering) {
177 fInstancedRendering->beginFlush(flushState->resourceProvider());
178 }
robertphillipsa13e2022015-11-11 12:01:09 -0800179}
bsalomon512be532015-09-10 10:42:55 -0700180
Robert Phillips294870f2016-11-11 12:38:40 -0500181// TODO: this is where GrBatch::renderTarget is used (which is fine since it
182// is at flush time). However, we need to store the RenderTargetProxy in the
183// Batches and instantiate them here.
Robert Phillipsf2361d22016-10-25 14:20:06 -0400184bool GrRenderTargetOpList::drawBatches(GrBatchFlushState* flushState) {
egdanielb4021cf2016-07-28 08:53:07 -0700185 if (0 == fRecordedBatches.count()) {
bsalomondc438982016-08-31 11:53:49 -0700186 return false;
egdanielb4021cf2016-07-28 08:53:07 -0700187 }
bsalomon512be532015-09-10 10:42:55 -0700188 // Draw all the generated geometry.
bsalomon6dea83f2015-12-03 12:58:06 -0800189 SkRandom random;
egdaniel9cb63402016-06-23 08:37:05 -0700190 GrRenderTarget* currentRT = nullptr;
Ben Wagner145dbcd2016-11-03 14:40:50 -0400191 std::unique_ptr<GrGpuCommandBuffer> commandBuffer;
bsalomon6cc90062016-07-08 11:31:22 -0700192 for (int i = 0; i < fRecordedBatches.count(); ++i) {
193 if (!fRecordedBatches[i].fBatch) {
bsalomonaecc0182016-03-07 11:50:44 -0800194 continue;
195 }
bsalomon6cc90062016-07-08 11:31:22 -0700196 if (fRecordedBatches[i].fBatch->renderTarget() != currentRT) {
egdaniel9cb63402016-06-23 08:37:05 -0700197 if (commandBuffer) {
198 commandBuffer->end();
Greg Daniel36a77ee2016-10-18 10:33:25 -0400199 commandBuffer->submit();
egdaniel9cb63402016-06-23 08:37:05 -0700200 commandBuffer.reset();
201 }
bsalomon6cc90062016-07-08 11:31:22 -0700202 currentRT = fRecordedBatches[i].fBatch->renderTarget();
egdaniel9cb63402016-06-23 08:37:05 -0700203 if (currentRT) {
204 static const GrGpuCommandBuffer::LoadAndStoreInfo kBasicLoadStoreInfo
205 { GrGpuCommandBuffer::LoadOp::kLoad,GrGpuCommandBuffer::StoreOp::kStore,
206 GrColor_ILLEGAL };
Brian Salomonc293a292016-11-30 13:38:32 -0500207 commandBuffer.reset(fGpu->createCommandBuffer(kBasicLoadStoreInfo, // Color
egdaniel9cb63402016-06-23 08:37:05 -0700208 kBasicLoadStoreInfo)); // Stencil
209 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400210 flushState->setCommandBuffer(commandBuffer.get());
egdaniel9cb63402016-06-23 08:37:05 -0700211 }
bsalomon6dea83f2015-12-03 12:58:06 -0800212 if (fDrawBatchBounds) {
bsalomon6cc90062016-07-08 11:31:22 -0700213 const SkRect& bounds = fRecordedBatches[i].fClippedBounds;
214 SkIRect ibounds;
215 bounds.roundOut(&ibounds);
bsalomon6dea83f2015-12-03 12:58:06 -0800216 // In multi-draw buffer all the batches use the same render target and we won't need to
217 // get the batchs bounds.
bsalomon6cc90062016-07-08 11:31:22 -0700218 if (GrRenderTarget* rt = fRecordedBatches[i].fBatch->renderTarget()) {
219 fGpu->drawDebugWireRect(rt, ibounds, 0xFF000000 | random.nextU());
bsalomon6dea83f2015-12-03 12:58:06 -0800220 }
221 }
Greg Daniel36a77ee2016-10-18 10:33:25 -0400222 fRecordedBatches[i].fBatch->draw(flushState, fRecordedBatches[i].fClippedBounds);
bsalomon512be532015-09-10 10:42:55 -0700223 }
egdaniel9cb63402016-06-23 08:37:05 -0700224 if (commandBuffer) {
225 commandBuffer->end();
Greg Daniel36a77ee2016-10-18 10:33:25 -0400226 commandBuffer->submit();
egdaniel9cb63402016-06-23 08:37:05 -0700227 flushState->setCommandBuffer(nullptr);
228 }
ethannicholas22793252016-01-30 09:59:10 -0800229
Robert Phillipsf2361d22016-10-25 14:20:06 -0400230 fGpu->finishOpList();
bsalomondc438982016-08-31 11:53:49 -0700231 return true;
bsalomona73239a2015-04-28 13:35:17 -0700232}
233
Robert Phillipsf2361d22016-10-25 14:20:06 -0400234void GrRenderTargetOpList::reset() {
bsalomonfd8d0132016-08-11 11:25:33 -0700235 fLastFullClearBatch = nullptr;
bsalomon6cc90062016-07-08 11:31:22 -0700236 fRecordedBatches.reset();
csmartdaltona7f29642016-07-07 08:49:11 -0700237 if (fInstancedRendering) {
238 fInstancedRendering->endFlush();
239 }
bsalomon512be532015-09-10 10:42:55 -0700240}
241
Robert Phillipsf2361d22016-10-25 14:20:06 -0400242void GrRenderTargetOpList::abandonGpuResources() {
243 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
244 InstancedRendering* ir = this->instancedRendering();
245 ir->resetGpuResources(InstancedRendering::ResetType::kAbandon);
246 }
247}
248
249void GrRenderTargetOpList::freeGpuResources() {
250 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
251 InstancedRendering* ir = this->instancedRendering();
252 ir->resetGpuResources(InstancedRendering::ResetType::kDestroy);
253 }
254}
255
bsalomon88cf17d2016-07-08 06:40:56 -0700256static void batch_bounds(SkRect* bounds, const GrBatch* batch) {
257 *bounds = batch->bounds();
258 if (batch->hasZeroArea()) {
259 if (batch->hasAABloat()) {
260 bounds->outset(0.5f, 0.5f);
261 } else {
262 // We don't know which way the particular GPU will snap lines or points at integer
263 // coords. So we ensure that the bounds is large enough for either snap.
264 SkRect before = *bounds;
265 bounds->roundOut(bounds);
266 if (bounds->fLeft == before.fLeft) {
267 bounds->fLeft -= 1;
268 }
269 if (bounds->fTop == before.fTop) {
270 bounds->fTop -= 1;
271 }
272 if (bounds->fRight == before.fRight) {
273 bounds->fRight += 1;
274 }
275 if (bounds->fBottom == before.fBottom) {
276 bounds->fBottom += 1;
277 }
278 }
279 }
280}
281
Robert Phillipsf2361d22016-10-25 14:20:06 -0400282void GrRenderTargetOpList::drawBatch(const GrPipelineBuilder& pipelineBuilder,
Brian Osman11052242016-10-27 14:47:55 -0400283 GrRenderTargetContext* renderTargetContext,
Robert Phillipsf2361d22016-10-25 14:20:06 -0400284 const GrClip& clip,
285 GrDrawBatch* batch) {
joshualitt4d8da812015-01-28 12:53:54 -0800286 // Setup clip
bsalomon88cf17d2016-07-08 06:40:56 -0700287 SkRect bounds;
288 batch_bounds(&bounds, batch);
csmartdaltond211e782016-08-15 11:17:19 -0700289 GrAppliedClip appliedClip(bounds);
Brian Osman11052242016-10-27 14:47:55 -0400290 if (!clip.apply(fContext, renderTargetContext, pipelineBuilder.isHWAntialias(),
csmartdaltond211e782016-08-15 11:17:19 -0700291 pipelineBuilder.hasUserStencilSettings(), &appliedClip)) {
cdalton862cff32016-05-12 15:09:48 -0700292 return;
joshualitt4d8da812015-01-28 12:53:54 -0800293 }
robertphillips391395d2016-03-02 09:26:36 -0800294
robertphillips55fdccc2016-06-06 06:16:20 -0700295 // TODO: this is the only remaining usage of the AutoRestoreFragmentProcessorState - remove it
cdaltond4727922015-11-10 12:49:06 -0800296 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
csmartdaltond211e782016-08-15 11:17:19 -0700297 if (appliedClip.clipCoverageFragmentProcessor()) {
cdaltond4727922015-11-10 12:49:06 -0800298 arfps.set(&pipelineBuilder);
csmartdaltond211e782016-08-15 11:17:19 -0700299 arfps.addCoverageFragmentProcessor(sk_ref_sp(appliedClip.clipCoverageFragmentProcessor()));
cdaltond4727922015-11-10 12:49:06 -0800300 }
joshualitt4d8da812015-01-28 12:53:54 -0800301
cdalton862cff32016-05-12 15:09:48 -0700302 if (pipelineBuilder.hasUserStencilSettings() || appliedClip.hasStencilClip()) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500303 if (!renderTargetContext->accessRenderTarget()) {
304 return;
305 }
306
Brian Osman11052242016-10-27 14:47:55 -0400307 if (!fResourceProvider->attachStencilAttachment(
308 renderTargetContext->accessRenderTarget())) {
cdalton17bf8202016-05-13 11:27:15 -0700309 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
310 return;
311 }
cdalton193d9cf2016-05-12 11:52:02 -0700312 }
csmartdaltond211e782016-08-15 11:17:19 -0700313
314 GrPipeline::CreateArgs args;
315 args.fPipelineBuilder = &pipelineBuilder;
Brian Osman11052242016-10-27 14:47:55 -0400316 args.fRenderTargetContext = renderTargetContext;
csmartdaltond211e782016-08-15 11:17:19 -0700317 args.fCaps = this->caps();
cdalton193d9cf2016-05-12 11:52:02 -0700318 batch->getPipelineOptimizations(&args.fOpts);
cdalton862cff32016-05-12 15:09:48 -0700319 if (args.fOpts.fOverrides.fUsePLSDstRead || fClipBatchToBounds) {
cdalton193d9cf2016-05-12 11:52:02 -0700320 GrGLIRect viewport;
321 viewport.fLeft = 0;
322 viewport.fBottom = 0;
Brian Osman11052242016-10-27 14:47:55 -0400323 viewport.fWidth = renderTargetContext->width();
324 viewport.fHeight = renderTargetContext->height();
cdalton193d9cf2016-05-12 11:52:02 -0700325 SkIRect ibounds;
326 ibounds.fLeft = SkTPin(SkScalarFloorToInt(batch->bounds().fLeft), viewport.fLeft,
327 viewport.fWidth);
328 ibounds.fTop = SkTPin(SkScalarFloorToInt(batch->bounds().fTop), viewport.fBottom,
329 viewport.fHeight);
330 ibounds.fRight = SkTPin(SkScalarCeilToInt(batch->bounds().fRight), viewport.fLeft,
331 viewport.fWidth);
332 ibounds.fBottom = SkTPin(SkScalarCeilToInt(batch->bounds().fBottom), viewport.fBottom,
333 viewport.fHeight);
csmartdaltond211e782016-08-15 11:17:19 -0700334 if (!appliedClip.addScissor(ibounds)) {
335 return;
cdalton193d9cf2016-05-12 11:52:02 -0700336 }
cdalton193d9cf2016-05-12 11:52:02 -0700337 }
bungeman06ca8ec2016-06-09 08:01:03 -0700338 args.fOpts.fColorPOI.completeCalculations(
339 sk_sp_address_as_pointer_address(pipelineBuilder.fColorFragmentProcessors.begin()),
340 pipelineBuilder.numColorFragmentProcessors());
cdalton193d9cf2016-05-12 11:52:02 -0700341 args.fOpts.fCoveragePOI.completeCalculations(
bungeman06ca8ec2016-06-09 08:01:03 -0700342 sk_sp_address_as_pointer_address(pipelineBuilder.fCoverageFragmentProcessors.begin()),
343 pipelineBuilder.numCoverageFragmentProcessors());
csmartdaltond211e782016-08-15 11:17:19 -0700344 args.fScissor = &appliedClip.scissorState();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700345 args.fWindowRectsState = &appliedClip.windowRectsState();
csmartdaltond211e782016-08-15 11:17:19 -0700346 args.fHasStencilClip = appliedClip.hasStencilClip();
Robert Phillipse60ad622016-11-17 10:22:48 -0500347 if (!renderTargetContext->accessRenderTarget()) {
348 return;
349 }
350
Brian Osman11052242016-10-27 14:47:55 -0400351 if (!this->setupDstReadIfNecessary(pipelineBuilder, renderTargetContext->accessRenderTarget(),
robertphillips55fdccc2016-06-06 06:16:20 -0700352 clip, args.fOpts,
353 &args.fDstTexture, batch->bounds())) {
cdalton193d9cf2016-05-12 11:52:02 -0700354 return;
355 }
356
357 if (!batch->installPipeline(args)) {
egdaniele36914c2015-02-13 09:00:33 -0800358 return;
359 }
bsalomonad792c12015-09-10 11:10:50 -0700360
robertphillips498d7ac2015-10-30 10:11:30 -0700361#ifdef ENABLE_MDB
Robert Phillipsf2361d22016-10-25 14:20:06 -0400362 SkASSERT(fSurface);
363 batch->pipeline()->addDependenciesTo(fSurface);
robertphillips498d7ac2015-10-30 10:11:30 -0700364#endif
csmartdaltond211e782016-08-15 11:17:19 -0700365 this->recordBatch(batch, appliedClip.clippedDrawBounds());
joshualitt4d8da812015-01-28 12:53:54 -0800366}
367
Brian Osman11052242016-10-27 14:47:55 -0400368void GrRenderTargetOpList::stencilPath(GrRenderTargetContext* renderTargetContext,
Robert Phillipsf2361d22016-10-25 14:20:06 -0400369 const GrClip& clip,
370 bool useHWAA,
371 const SkMatrix& viewMatrix,
372 const GrPath* path) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000373 // TODO: extract portions of checkDraw that are relevant to path stenciling.
bsalomon49f085d2014-09-05 13:34:00 -0700374 SkASSERT(path);
jvanverthe9c0fc62015-04-29 11:18:05 -0700375 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
joshualitt2c93efe2014-11-06 12:57:13 -0800376
csmartdaltond211e782016-08-15 11:17:19 -0700377 // FIXME: Use path bounds instead of this WAR once
378 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
Brian Osman11052242016-10-27 14:47:55 -0400379 SkRect bounds = SkRect::MakeIWH(renderTargetContext->width(), renderTargetContext->height());
csmartdaltond211e782016-08-15 11:17:19 -0700380
joshualitt2c93efe2014-11-06 12:57:13 -0800381 // Setup clip
csmartdaltond211e782016-08-15 11:17:19 -0700382 GrAppliedClip appliedClip(bounds);
Brian Osman11052242016-10-27 14:47:55 -0400383 if (!clip.apply(fContext, renderTargetContext, useHWAA, true, &appliedClip)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800384 return;
385 }
cdalton862cff32016-05-12 15:09:48 -0700386 // TODO: respect fClipBatchToBounds if we ever start computing bounds here.
joshualitt2c93efe2014-11-06 12:57:13 -0800387
cdalton846c0512016-05-13 10:25:00 -0700388 // Coverage AA does not make sense when rendering to the stencil buffer. The caller should never
389 // attempt this in a situation that would require coverage AA.
csmartdaltond211e782016-08-15 11:17:19 -0700390 SkASSERT(!appliedClip.clipCoverageFragmentProcessor());
bsalomon0ba8c242015-10-07 09:20:28 -0700391
Robert Phillipse60ad622016-11-17 10:22:48 -0500392 if (!renderTargetContext->accessRenderTarget()) {
393 return;
394 }
robertphillips55fdccc2016-06-06 06:16:20 -0700395 GrStencilAttachment* stencilAttachment = fResourceProvider->attachStencilAttachment(
Brian Osman11052242016-10-27 14:47:55 -0400396 renderTargetContext->accessRenderTarget());
cdalton17bf8202016-05-13 11:27:15 -0700397 if (!stencilAttachment) {
398 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
399 return;
400 }
joshualitt2c93efe2014-11-06 12:57:13 -0800401
joshualittf2384692015-09-10 11:00:51 -0700402 GrBatch* batch = GrStencilPathBatch::Create(viewMatrix,
robertphillips59cf61a2016-07-13 09:18:21 -0700403 useHWAA,
404 path->getFillType(),
cdalton862cff32016-05-12 15:09:48 -0700405 appliedClip.hasStencilClip(),
cdalton93a379b2016-05-11 13:58:08 -0700406 stencilAttachment->bits(),
cdalton862cff32016-05-12 15:09:48 -0700407 appliedClip.scissorState(),
Brian Osman11052242016-10-27 14:47:55 -0400408 renderTargetContext->accessRenderTarget(),
bsalomona44919e2015-08-18 13:28:19 -0700409 path);
csmartdaltond211e782016-08-15 11:17:19 -0700410 this->recordBatch(batch, appliedClip.clippedDrawBounds());
bsalomona44919e2015-08-18 13:28:19 -0700411 batch->unref();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000412}
413
Robert Phillipsf2361d22016-10-25 14:20:06 -0400414void GrRenderTargetOpList::addBatch(sk_sp<GrBatch> batch) {
robertphillips9199a9f2016-07-13 07:48:43 -0700415 this->recordBatch(batch.get(), batch->bounds());
bsalomon53469832015-08-18 09:20:09 -0700416}
417
Robert Phillipsf2361d22016-10-25 14:20:06 -0400418void GrRenderTargetOpList::fullClear(GrRenderTarget* renderTarget, GrColor color) {
bsalomonfd8d0132016-08-11 11:25:33 -0700419 // Currently this just inserts or updates the last clear batch. However, once in MDB this can
420 // remove all the previously recorded batches and change the load op to clear with supplied
421 // color.
Robert Phillips294870f2016-11-11 12:38:40 -0500422 // TODO: this needs to be updated to use GrSurfaceProxy::UniqueID
bsalomonfd8d0132016-08-11 11:25:33 -0700423 if (fLastFullClearBatch &&
robertphillips8abb3702016-08-31 14:04:06 -0700424 fLastFullClearBatch->renderTargetUniqueID() == renderTarget->uniqueID()) {
bsalomonfd8d0132016-08-11 11:25:33 -0700425 // As currently implemented, fLastFullClearBatch should be the last batch because we would
426 // have cleared it when another batch was recorded.
427 SkASSERT(fRecordedBatches.back().fBatch.get() == fLastFullClearBatch);
428 fLastFullClearBatch->setColor(color);
429 return;
430 }
csmartdalton29df7602016-08-31 11:55:52 -0700431 sk_sp<GrClearBatch> batch(GrClearBatch::Make(GrFixedClip::Disabled(), color, renderTarget));
bsalomonfd8d0132016-08-11 11:25:33 -0700432 if (batch.get() == this->recordBatch(batch.get(), batch->bounds())) {
433 fLastFullClearBatch = batch.get();
434 }
bsalomon9f129de2016-08-10 16:31:05 -0700435}
436
Robert Phillipsf2361d22016-10-25 14:20:06 -0400437void GrRenderTargetOpList::discard(GrRenderTarget* renderTarget) {
bsalomon9f129de2016-08-10 16:31:05 -0700438 // Currently this just inserts a discard batch. However, once in MDB this can remove all the
439 // previously recorded batches and change the load op to discard.
bsalomon53469832015-08-18 09:20:09 -0700440 if (this->caps()->discardRenderTargetSupport()) {
halcanary385fe4d2015-08-26 13:07:48 -0700441 GrBatch* batch = new GrDiscardBatch(renderTarget);
bsalomon6cc90062016-07-08 11:31:22 -0700442 this->recordBatch(batch, batch->bounds());
bsalomon53469832015-08-18 09:20:09 -0700443 batch->unref();
bsalomon63b21962014-11-05 07:05:34 -0800444 }
445}
446
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000447////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000448
Robert Phillipsf2361d22016-10-25 14:20:06 -0400449bool GrRenderTargetOpList::copySurface(GrSurface* dst,
450 GrSurface* src,
451 const SkIRect& srcRect,
452 const SkIPoint& dstPoint) {
bsalomon872062c2015-08-18 12:12:35 -0700453 GrBatch* batch = GrCopySurfaceBatch::Create(dst, src, srcRect, dstPoint);
bsalomonb8fea972016-02-16 07:34:17 -0800454 if (!batch) {
455 return false;
456 }
robertphillips498d7ac2015-10-30 10:11:30 -0700457#ifdef ENABLE_MDB
bsalomonb8fea972016-02-16 07:34:17 -0800458 this->addDependency(src);
robertphillips498d7ac2015-10-30 10:11:30 -0700459#endif
460
bsalomon6cc90062016-07-08 11:31:22 -0700461 this->recordBatch(batch, batch->bounds());
bsalomonb8fea972016-02-16 07:34:17 -0800462 batch->unref();
463 return true;
bsalomon@google.comeb851172013-04-15 13:51:00 +0000464}
465
bsalomon6cc90062016-07-08 11:31:22 -0700466static inline bool can_reorder(const SkRect& a, const SkRect& b) {
bsalomon88cf17d2016-07-08 06:40:56 -0700467 return a.fRight <= b.fLeft || a.fBottom <= b.fTop ||
468 b.fRight <= a.fLeft || b.fBottom <= a.fTop;
469}
470
bsalomon6cc90062016-07-08 11:31:22 -0700471static void join(SkRect* out, const SkRect& a, const SkRect& b) {
472 SkASSERT(a.fLeft <= a.fRight && a.fTop <= a.fBottom);
473 SkASSERT(b.fLeft <= b.fRight && b.fTop <= b.fBottom);
474 out->fLeft = SkTMin(a.fLeft, b.fLeft);
475 out->fTop = SkTMin(a.fTop, b.fTop);
476 out->fRight = SkTMax(a.fRight, b.fRight);
477 out->fBottom = SkTMax(a.fBottom, b.fBottom);
bsalomon512be532015-09-10 10:42:55 -0700478}
479
Robert Phillipsf2361d22016-10-25 14:20:06 -0400480GrBatch* GrRenderTargetOpList::recordBatch(GrBatch* batch, const SkRect& clippedBounds) {
481 // A closed GrOpList should never receive new/more batches
robertphillips6a186652015-10-20 07:37:58 -0700482 SkASSERT(!this->isClosed());
robertphillipsa106c622015-10-16 09:07:06 -0700483
bsalomon512be532015-09-10 10:42:55 -0700484 // Check if there is a Batch Draw we can batch with by linearly searching back until we either
485 // 1) check every draw
486 // 2) intersect with something
487 // 3) find a 'blocker'
joshualittb0666ad2016-03-08 10:43:41 -0800488 GR_AUDIT_TRAIL_ADDBATCH(fAuditTrail, batch);
bsalomon512be532015-09-10 10:42:55 -0700489 GrBATCH_INFO("Re-Recording (%s, B%u)\n"
joshualitte2bcec32015-09-30 06:22:22 -0700490 "\tBounds LRTB (%f, %f, %f, %f)\n",
bsalomon512be532015-09-10 10:42:55 -0700491 batch->name(),
492 batch->uniqueID(),
493 batch->bounds().fLeft, batch->bounds().fRight,
494 batch->bounds().fTop, batch->bounds().fBottom);
495 GrBATCH_INFO(SkTabString(batch->dumpInfo(), 1).c_str());
bsalomon6cc90062016-07-08 11:31:22 -0700496 GrBATCH_INFO("\tClipped Bounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n",
497 clippedBounds.fLeft, clippedBounds.fTop, clippedBounds.fRight,
498 clippedBounds.fBottom);
halcanary9d524f22016-03-29 09:03:52 -0700499 GrBATCH_INFO("\tOutcome:\n");
bsalomon6cc90062016-07-08 11:31:22 -0700500 int maxCandidates = SkTMin(fMaxBatchLookback, fRecordedBatches.count());
bsalomon512be532015-09-10 10:42:55 -0700501 if (maxCandidates) {
502 int i = 0;
503 while (true) {
bsalomon6cc90062016-07-08 11:31:22 -0700504 GrBatch* candidate = fRecordedBatches.fromBack(i).fBatch.get();
bsalomon512be532015-09-10 10:42:55 -0700505 // We cannot continue to search backwards if the render target changes
506 if (candidate->renderTargetUniqueID() != batch->renderTargetUniqueID()) {
507 GrBATCH_INFO("\t\tBreaking because of (%s, B%u) Rendertarget\n",
508 candidate->name(), candidate->uniqueID());
509 break;
510 }
511 if (candidate->combineIfPossible(batch, *this->caps())) {
512 GrBATCH_INFO("\t\tCombining with (%s, B%u)\n", candidate->name(),
513 candidate->uniqueID());
joshualittb0666ad2016-03-08 10:43:41 -0800514 GR_AUDIT_TRAIL_BATCHING_RESULT_COMBINED(fAuditTrail, candidate, batch);
bsalomon6cc90062016-07-08 11:31:22 -0700515 join(&fRecordedBatches.fromBack(i).fClippedBounds,
516 fRecordedBatches.fromBack(i).fClippedBounds, clippedBounds);
bsalomonfd8d0132016-08-11 11:25:33 -0700517 return candidate;
bsalomon512be532015-09-10 10:42:55 -0700518 }
519 // Stop going backwards if we would cause a painter's order violation.
bsalomon6cc90062016-07-08 11:31:22 -0700520 const SkRect& candidateBounds = fRecordedBatches.fromBack(i).fClippedBounds;
521 if (!can_reorder(candidateBounds, clippedBounds)) {
bsalomon512be532015-09-10 10:42:55 -0700522 GrBATCH_INFO("\t\tIntersects with (%s, B%u)\n", candidate->name(),
523 candidate->uniqueID());
524 break;
525 }
526 ++i;
527 if (i == maxCandidates) {
528 GrBATCH_INFO("\t\tReached max lookback or beginning of batch array %d\n", i);
529 break;
530 }
531 }
532 } else {
533 GrBATCH_INFO("\t\tFirstBatch\n");
534 }
joshualitt18d6b752016-02-26 08:07:50 -0800535 GR_AUDIT_TRAIL_BATCHING_RESULT_NEW(fAuditTrail, batch);
bsalomon6cc90062016-07-08 11:31:22 -0700536 fRecordedBatches.emplace_back(RecordedBatch{sk_ref_sp(batch), clippedBounds});
bsalomonfd8d0132016-08-11 11:25:33 -0700537 fLastFullClearBatch = nullptr;
538 return batch;
bsalomon512be532015-09-10 10:42:55 -0700539}
540
Robert Phillipsf2361d22016-10-25 14:20:06 -0400541void GrRenderTargetOpList::forwardCombine() {
bsalomondb27fc52016-08-29 12:43:27 -0700542 if (fMaxBatchLookahead <= 0) {
543 return;
544 }
bsalomon6cc90062016-07-08 11:31:22 -0700545 for (int i = 0; i < fRecordedBatches.count() - 2; ++i) {
546 GrBatch* batch = fRecordedBatches[i].fBatch.get();
547 const SkRect& batchBounds = fRecordedBatches[i].fClippedBounds;
548 int maxCandidateIdx = SkTMin(i + fMaxBatchLookahead, fRecordedBatches.count() - 1);
bsalomonaecc0182016-03-07 11:50:44 -0800549 int j = i + 1;
550 while (true) {
bsalomon6cc90062016-07-08 11:31:22 -0700551 GrBatch* candidate = fRecordedBatches[j].fBatch.get();
bsalomonaecc0182016-03-07 11:50:44 -0800552 // We cannot continue to search if the render target changes
553 if (candidate->renderTargetUniqueID() != batch->renderTargetUniqueID()) {
554 GrBATCH_INFO("\t\tBreaking because of (%s, B%u) Rendertarget\n",
555 candidate->name(), candidate->uniqueID());
556 break;
557 }
558 if (j == i +1) {
559 // We assume batch would have combined with candidate when the candidate was added
560 // via backwards combining in recordBatch.
561 SkASSERT(!batch->combineIfPossible(candidate, *this->caps()));
562 } else if (batch->combineIfPossible(candidate, *this->caps())) {
563 GrBATCH_INFO("\t\tCombining with (%s, B%u)\n", candidate->name(),
564 candidate->uniqueID());
joshualittb0666ad2016-03-08 10:43:41 -0800565 GR_AUDIT_TRAIL_BATCHING_RESULT_COMBINED(fAuditTrail, batch, candidate);
bsalomon6cc90062016-07-08 11:31:22 -0700566 fRecordedBatches[j].fBatch = std::move(fRecordedBatches[i].fBatch);
567 join(&fRecordedBatches[j].fClippedBounds, fRecordedBatches[j].fClippedBounds,
568 batchBounds);
bsalomonaecc0182016-03-07 11:50:44 -0800569 break;
570 }
571 // Stop going traversing if we would cause a painter's order violation.
bsalomon6cc90062016-07-08 11:31:22 -0700572 const SkRect& candidateBounds = fRecordedBatches[j].fClippedBounds;
573 if (!can_reorder(candidateBounds, batchBounds)) {
bsalomonaecc0182016-03-07 11:50:44 -0800574 GrBATCH_INFO("\t\tIntersects with (%s, B%u)\n", candidate->name(),
575 candidate->uniqueID());
576 break;
577 }
578 ++j;
579 if (j > maxCandidateIdx) {
580 GrBATCH_INFO("\t\tReached max lookahead or end of batch array %d\n", i);
581 break;
582 }
583 }
584 }
585}
586
egdaniele36914c2015-02-13 09:00:33 -0800587///////////////////////////////////////////////////////////////////////////////
588
Robert Phillipsf2361d22016-10-25 14:20:06 -0400589void GrRenderTargetOpList::clearStencilClip(const GrFixedClip& clip,
590 bool insideStencilMask,
591 GrRenderTarget* rt) {
csmartdalton29df7602016-08-31 11:55:52 -0700592 GrBatch* batch = new GrClearStencilClipBatch(clip, insideStencilMask, rt);
bsalomon6cc90062016-07-08 11:31:22 -0700593 this->recordBatch(batch, batch->bounds());
bsalomon5ea03632015-08-18 10:33:30 -0700594 batch->unref();
595}