blob: c424f5a593929b6ba7db0d15685f99b4a9d3cfb3 [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
joshualittc2893c52015-01-28 06:54:30 -08008#include "GrDrawTarget.h"
joshualitt4d8da812015-01-28 12:53:54 -08009
joshualitt086cee12016-01-12 06:45:24 -080010#include "GrAuditTrail.h"
bsalomoneb1cb5c2015-05-22 08:01:09 -070011#include "GrCaps.h"
robertphillips976f5f02016-06-03 10:59:20 -070012#include "GrDrawContext.h"
bsalomon4061b122015-05-29 10:26:19 -070013#include "GrGpu.h"
egdaniel9cb63402016-06-23 08:37:05 -070014#include "GrGpuCommandBuffer.h"
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000015#include "GrPath.h"
egdaniele36914c2015-02-13 09:00:33 -080016#include "GrPipeline.h"
joshualittb7133be2015-04-08 09:08:31 -070017#include "GrMemoryPool.h"
robertphillips5fa7f302016-07-21 09:21:04 -070018#include "GrPipelineBuilder.h"
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000019#include "GrRenderTarget.h"
bsalomon4061b122015-05-29 10:26:19 -070020#include "GrResourceProvider.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080021#include "GrRenderTargetPriv.h"
cdalton93a379b2016-05-11 13:58:08 -070022#include "GrStencilAttachment.h"
bsalomonafbf2d62014-09-30 12:18:44 -070023#include "GrSurfacePriv.h"
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000024#include "GrTexture.h"
ethannicholas22793252016-01-30 09:59:10 -080025#include "gl/GrGLRenderTarget.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000026
joshualitt086cee12016-01-12 06:45:24 -080027#include "SkStrokeRec.h"
28
bsalomon9f129de2016-08-10 16:31:05 -070029#include "batches/GrClearBatch.h"
robertphillips9199a9f2016-07-13 07:48:43 -070030#include "batches/GrClearStencilClipBatch.h"
bsalomon872062c2015-08-18 12:12:35 -070031#include "batches/GrCopySurfaceBatch.h"
bsalomon53469832015-08-18 09:20:09 -070032#include "batches/GrDiscardBatch.h"
bsalomon16b99132015-08-13 14:55:50 -070033#include "batches/GrDrawBatch.h"
bsalomonadd79ef2015-08-19 13:26:49 -070034#include "batches/GrDrawPathBatch.h"
joshualittecd1a692015-08-10 10:08:26 -070035#include "batches/GrRectBatchFactory.h"
bsalomona44919e2015-08-18 13:28:19 -070036#include "batches/GrStencilPathBatch.h"
joshualitt74417822015-08-07 11:42:16 -070037
csmartdaltona7f29642016-07-07 08:49:11 -070038#include "instanced/InstancedRendering.h"
39
reed@google.comac10a2d2010-12-22 21:39:39 +000040////////////////////////////////////////////////////////////////////////////////
41
bsalomon489147c2015-12-14 12:13:09 -080042// Experimentally we have found that most batching occurs within the first 10 comparisons.
bsalomonaecc0182016-03-07 11:50:44 -080043static const int kDefaultMaxBatchLookback = 10;
44static const int kDefaultMaxBatchLookahead = 10;
bsalomon489147c2015-12-14 12:13:09 -080045
bsalomon69cfe952015-11-30 13:27:47 -080046GrDrawTarget::GrDrawTarget(GrRenderTarget* rt, GrGpu* gpu, GrResourceProvider* resourceProvider,
joshualitt086cee12016-01-12 06:45:24 -080047 GrAuditTrail* auditTrail, const Options& options)
bsalomon4061b122015-05-29 10:26:19 -070048 : fGpu(SkRef(gpu))
bsalomon4061b122015-05-29 10:26:19 -070049 , fResourceProvider(resourceProvider)
joshualitt086cee12016-01-12 06:45:24 -080050 , fAuditTrail(auditTrail)
bsalomon648c6962015-10-23 09:06:59 -070051 , fFlags(0)
csmartdaltone0d36292016-07-29 08:14:20 -070052 , fRenderTarget(rt) {
csmartdaltonc6f411e2016-08-05 22:32:12 -070053 // TODO: Stop extracting the context (currently needed by GrClip)
bsalomonb3b9aec2015-09-10 11:16:35 -070054 fContext = fGpu->getContext();
robertphillips4beb5c12015-10-20 07:50:00 -070055
cdalton862cff32016-05-12 15:09:48 -070056 fClipBatchToBounds = options.fClipBatchToBounds;
bsalomon6dea83f2015-12-03 12:58:06 -080057 fDrawBatchBounds = options.fDrawBatchBounds;
bsalomon489147c2015-12-14 12:13:09 -080058 fMaxBatchLookback = (options.fMaxBatchLookback < 0) ? kDefaultMaxBatchLookback :
59 options.fMaxBatchLookback;
bsalomonaecc0182016-03-07 11:50:44 -080060 fMaxBatchLookahead = (options.fMaxBatchLookahead < 0) ? kDefaultMaxBatchLookahead :
61 options.fMaxBatchLookahead;
bsalomon6dea83f2015-12-03 12:58:06 -080062
csmartdaltone0d36292016-07-29 08:14:20 -070063 if (GrCaps::InstancedSupport::kNone != this->caps()->instancedSupport()) {
64 fInstancedRendering.reset(fGpu->createInstancedRendering());
65 }
66
robertphillips0dfa62c2015-11-16 06:23:31 -080067 rt->setLastDrawTarget(this);
68
robertphillips4beb5c12015-10-20 07:50:00 -070069#ifdef SK_DEBUG
70 static int debugID = 0;
71 fDebugID = debugID++;
72#endif
bsalomon4061b122015-05-29 10:26:19 -070073}
74
75GrDrawTarget::~GrDrawTarget() {
robertphillips498d7ac2015-10-30 10:11:30 -070076 if (fRenderTarget && this == fRenderTarget->getLastDrawTarget()) {
77 fRenderTarget->setLastDrawTarget(nullptr);
78 }
79
bsalomon4061b122015-05-29 10:26:19 -070080 fGpu->unref();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000081}
82
83////////////////////////////////////////////////////////////////////////////////
84
robertphillips6a186652015-10-20 07:37:58 -070085// Add a GrDrawTarget-based dependency
86void GrDrawTarget::addDependency(GrDrawTarget* dependedOn) {
87 SkASSERT(!dependedOn->dependsOn(this)); // loops are bad
88
89 if (this->dependsOn(dependedOn)) {
90 return; // don't add duplicate dependencies
91 }
92
93 *fDependencies.push() = dependedOn;
94}
95
96// Convert from a GrSurface-based dependency to a GrDrawTarget one
97void GrDrawTarget::addDependency(GrSurface* dependedOn) {
98 if (dependedOn->asRenderTarget() && dependedOn->asRenderTarget()->getLastDrawTarget()) {
99 // If it is still receiving dependencies, this DT shouldn't be closed
100 SkASSERT(!this->isClosed());
101
102 GrDrawTarget* dt = dependedOn->asRenderTarget()->getLastDrawTarget();
103 if (dt == this) {
104 // self-read - presumably for dst reads
105 } else {
106 this->addDependency(dt);
107
108 // Can't make it closed in the self-read case
109 dt->makeClosed();
110 }
111 }
112}
113
robertphillips4beb5c12015-10-20 07:50:00 -0700114#ifdef SK_DEBUG
115void GrDrawTarget::dump() const {
116 SkDebugf("--------------------------------------------------------------\n");
robertphillipse004bfc2015-11-16 09:06:59 -0800117 SkDebugf("node: %d -> RT: %d\n", fDebugID, fRenderTarget ? fRenderTarget->getUniqueID() : -1);
robertphillips4beb5c12015-10-20 07:50:00 -0700118 SkDebugf("relies On (%d): ", fDependencies.count());
119 for (int i = 0; i < fDependencies.count(); ++i) {
120 SkDebugf("%d, ", fDependencies[i]->fDebugID);
121 }
122 SkDebugf("\n");
bsalomon6cc90062016-07-08 11:31:22 -0700123 SkDebugf("batches (%d):\n", fRecordedBatches.count());
124 for (int i = 0; i < fRecordedBatches.count(); ++i) {
robertphillips4beb5c12015-10-20 07:50:00 -0700125 SkDebugf("*******************************\n");
bsalomon6cc90062016-07-08 11:31:22 -0700126 if (!fRecordedBatches[i].fBatch) {
bsalomonaecc0182016-03-07 11:50:44 -0800127 SkDebugf("%d: <combined forward>\n", i);
128 } else {
bsalomon6cc90062016-07-08 11:31:22 -0700129 SkDebugf("%d: %s\n", i, fRecordedBatches[i].fBatch->name());
130 SkString str = fRecordedBatches[i].fBatch->dumpInfo();
bsalomonaecc0182016-03-07 11:50:44 -0800131 SkDebugf("%s\n", str.c_str());
bsalomon6cc90062016-07-08 11:31:22 -0700132 const SkRect& clippedBounds = fRecordedBatches[i].fClippedBounds;
133 SkDebugf("ClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n",
134 clippedBounds.fLeft, clippedBounds.fTop, clippedBounds.fRight,
135 clippedBounds.fBottom);
bsalomonaecc0182016-03-07 11:50:44 -0800136 }
robertphillips4beb5c12015-10-20 07:50:00 -0700137 }
138}
139#endif
140
bsalomon50785a32015-02-06 07:02:37 -0800141bool GrDrawTarget::setupDstReadIfNecessary(const GrPipelineBuilder& pipelineBuilder,
robertphillips55fdccc2016-06-06 06:16:20 -0700142 GrRenderTarget* rt,
cdalton862cff32016-05-12 15:09:48 -0700143 const GrClip& clip,
ethannicholasde4166a2015-11-30 08:57:38 -0800144 const GrPipelineOptimizations& optimizations,
bsalomon6a44c6a2015-05-26 09:49:05 -0700145 GrXferProcessor::DstTexture* dstTexture,
bsalomonad792c12015-09-10 11:10:50 -0700146 const SkRect& batchBounds) {
147 SkRect bounds = batchBounds;
148 bounds.outset(0.5f, 0.5f);
149
ethannicholasde4166a2015-11-30 08:57:38 -0800150 if (!pipelineBuilder.willXPNeedDstTexture(*this->caps(), optimizations)) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000151 return true;
152 }
cdalton9954bc32015-04-29 14:17:00 -0700153
cdalton9954bc32015-04-29 14:17:00 -0700154 if (this->caps()->textureBarrierSupport()) {
155 if (GrTexture* rtTex = rt->asTexture()) {
bsalomondc47ff72015-05-26 12:16:59 -0700156 // The render target is a texture, so we can read from it directly in the shader. The XP
cdalton9954bc32015-04-29 14:17:00 -0700157 // will be responsible to detect this situation and request a texture barrier.
bsalomon6a44c6a2015-05-26 09:49:05 -0700158 dstTexture->setTexture(rtTex);
159 dstTexture->setOffset(0, 0);
cdalton9954bc32015-04-29 14:17:00 -0700160 return true;
161 }
162 }
163
164 SkIRect copyRect;
cdalton862cff32016-05-12 15:09:48 -0700165 clip.getConservativeBounds(rt->width(), rt->height(), &copyRect);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000166
bsalomonad792c12015-09-10 11:10:50 -0700167 SkIRect drawIBounds;
168 bounds.roundOut(&drawIBounds);
169 if (!copyRect.intersect(drawIBounds)) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000170#ifdef SK_DEBUG
bsalomonb3b9aec2015-09-10 11:16:35 -0700171 GrCapsDebugf(this->caps(), "Missed an early reject. "
172 "Bailing on draw from setupDstReadIfNecessary.\n");
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000173#endif
bsalomonad792c12015-09-10 11:10:50 -0700174 return false;
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000175 }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000176
commit-bot@chromium.org63150af2013-04-11 22:00:22 +0000177 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
178 // have per-sample dst values by making the copy multisampled.
bsalomonf2703d82014-10-28 14:33:06 -0700179 GrSurfaceDesc desc;
bsalomonb3b9aec2015-09-10 11:16:35 -0700180 if (!fGpu->initCopySurfaceDstDesc(rt, &desc)) {
bsalomona73239a2015-04-28 13:35:17 -0700181 desc.fOrigin = kDefault_GrSurfaceOrigin;
182 desc.fFlags = kRenderTarget_GrSurfaceFlag;
183 desc.fConfig = rt->config();
184 }
185
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000186 desc.fWidth = copyRect.width();
187 desc.fHeight = copyRect.height();
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000188
bsalomoneae62002015-07-31 13:59:30 -0700189 static const uint32_t kFlags = 0;
190 SkAutoTUnref<GrTexture> copy(fResourceProvider->createApproxTexture(desc, kFlags));
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000191
bsalomone3059732014-10-14 11:47:22 -0700192 if (!copy) {
tfarina38406c82014-10-31 07:11:12 -0700193 SkDebugf("Failed to create temporary copy of destination texture.\n");
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000194 return false;
195 }
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000196 SkIPoint dstPoint = {0, 0};
bsalomon6df86402015-06-01 10:41:49 -0700197 this->copySurface(copy, rt, copyRect, dstPoint);
198 dstTexture->setTexture(copy);
199 dstTexture->setOffset(copyRect.fLeft, copyRect.fTop);
200 return true;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000201}
202
robertphillipsa13e2022015-11-11 12:01:09 -0800203void GrDrawTarget::prepareBatches(GrBatchFlushState* flushState) {
robertphillipsa106c622015-10-16 09:07:06 -0700204 // Semi-usually the drawTargets are already closed at this point, but sometimes Ganesh
205 // needs to flush mid-draw. In that case, the SkGpuDevice's drawTargets won't be closed
206 // but need to be flushed anyway. Closing such drawTargets here will mean new
207 // drawTargets will be created to replace them if the SkGpuDevice(s) write to them again.
208 this->makeClosed();
209
robertphillips498d7ac2015-10-30 10:11:30 -0700210 // Loop over the batches that haven't yet generated their geometry
bsalomon6cc90062016-07-08 11:31:22 -0700211 for (int i = 0; i < fRecordedBatches.count(); ++i) {
212 if (fRecordedBatches[i].fBatch) {
213 fRecordedBatches[i].fBatch->prepare(flushState);
bsalomonaecc0182016-03-07 11:50:44 -0800214 }
bsalomon512be532015-09-10 10:42:55 -0700215 }
csmartdaltona7f29642016-07-07 08:49:11 -0700216
217 if (fInstancedRendering) {
218 fInstancedRendering->beginFlush(flushState->resourceProvider());
219 }
robertphillipsa13e2022015-11-11 12:01:09 -0800220}
bsalomon512be532015-09-10 10:42:55 -0700221
robertphillipsa13e2022015-11-11 12:01:09 -0800222void GrDrawTarget::drawBatches(GrBatchFlushState* flushState) {
egdanielb4021cf2016-07-28 08:53:07 -0700223 if (0 == fRecordedBatches.count()) {
224 return;
225 }
bsalomon512be532015-09-10 10:42:55 -0700226 // Draw all the generated geometry.
bsalomon6dea83f2015-12-03 12:58:06 -0800227 SkRandom random;
egdaniel9cb63402016-06-23 08:37:05 -0700228 GrRenderTarget* currentRT = nullptr;
229 SkAutoTDelete<GrGpuCommandBuffer> commandBuffer;
egdaniele7d1b242016-07-01 08:06:45 -0700230 SkRect bounds = SkRect::MakeEmpty();
bsalomon6cc90062016-07-08 11:31:22 -0700231 for (int i = 0; i < fRecordedBatches.count(); ++i) {
232 if (!fRecordedBatches[i].fBatch) {
bsalomonaecc0182016-03-07 11:50:44 -0800233 continue;
234 }
bsalomon6cc90062016-07-08 11:31:22 -0700235 if (fRecordedBatches[i].fBatch->renderTarget() != currentRT) {
egdaniel9cb63402016-06-23 08:37:05 -0700236 if (commandBuffer) {
237 commandBuffer->end();
egdaniele7d1b242016-07-01 08:06:45 -0700238 if (bounds.intersect(0, 0,
239 SkIntToScalar(currentRT->width()),
240 SkIntToScalar(currentRT->height()))) {
241 SkIRect iBounds;
242 bounds.roundOut(&iBounds);
243 commandBuffer->submit(iBounds);
244 }
egdaniel9cb63402016-06-23 08:37:05 -0700245 commandBuffer.reset();
246 }
egdaniele7d1b242016-07-01 08:06:45 -0700247 bounds.setEmpty();
bsalomon6cc90062016-07-08 11:31:22 -0700248 currentRT = fRecordedBatches[i].fBatch->renderTarget();
egdaniel9cb63402016-06-23 08:37:05 -0700249 if (currentRT) {
250 static const GrGpuCommandBuffer::LoadAndStoreInfo kBasicLoadStoreInfo
251 { GrGpuCommandBuffer::LoadOp::kLoad,GrGpuCommandBuffer::StoreOp::kStore,
252 GrColor_ILLEGAL };
253 commandBuffer.reset(fGpu->createCommandBuffer(currentRT,
254 kBasicLoadStoreInfo, // Color
255 kBasicLoadStoreInfo)); // Stencil
256 }
257 flushState->setCommandBuffer(commandBuffer);
258 }
egdaniele7d1b242016-07-01 08:06:45 -0700259 if (commandBuffer) {
bsalomon6cc90062016-07-08 11:31:22 -0700260 bounds.join(fRecordedBatches[i].fClippedBounds);
egdaniele7d1b242016-07-01 08:06:45 -0700261 }
bsalomon6dea83f2015-12-03 12:58:06 -0800262 if (fDrawBatchBounds) {
bsalomon6cc90062016-07-08 11:31:22 -0700263 const SkRect& bounds = fRecordedBatches[i].fClippedBounds;
264 SkIRect ibounds;
265 bounds.roundOut(&ibounds);
bsalomon6dea83f2015-12-03 12:58:06 -0800266 // In multi-draw buffer all the batches use the same render target and we won't need to
267 // get the batchs bounds.
bsalomon6cc90062016-07-08 11:31:22 -0700268 if (GrRenderTarget* rt = fRecordedBatches[i].fBatch->renderTarget()) {
269 fGpu->drawDebugWireRect(rt, ibounds, 0xFF000000 | random.nextU());
bsalomon6dea83f2015-12-03 12:58:06 -0800270 }
271 }
bsalomon6cc90062016-07-08 11:31:22 -0700272 fRecordedBatches[i].fBatch->draw(flushState);
bsalomon512be532015-09-10 10:42:55 -0700273 }
egdaniel9cb63402016-06-23 08:37:05 -0700274 if (commandBuffer) {
275 commandBuffer->end();
egdaniele7d1b242016-07-01 08:06:45 -0700276 if (bounds.intersect(0, 0,
277 SkIntToScalar(currentRT->width()),
278 SkIntToScalar(currentRT->height()))) {
279 SkIRect iBounds;
280 bounds.roundOut(&iBounds);
281 commandBuffer->submit(iBounds);
282 }
egdaniel9cb63402016-06-23 08:37:05 -0700283 flushState->setCommandBuffer(nullptr);
284 }
ethannicholas22793252016-01-30 09:59:10 -0800285
jvanverthd2d2eb92016-02-17 14:04:46 -0800286 fGpu->finishDrawTarget();
bsalomona73239a2015-04-28 13:35:17 -0700287}
288
bsalomon512be532015-09-10 10:42:55 -0700289void GrDrawTarget::reset() {
bsalomon6cc90062016-07-08 11:31:22 -0700290 fRecordedBatches.reset();
csmartdaltona7f29642016-07-07 08:49:11 -0700291 if (fInstancedRendering) {
292 fInstancedRendering->endFlush();
293 }
bsalomon512be532015-09-10 10:42:55 -0700294}
295
bsalomon88cf17d2016-07-08 06:40:56 -0700296static void batch_bounds(SkRect* bounds, const GrBatch* batch) {
297 *bounds = batch->bounds();
298 if (batch->hasZeroArea()) {
299 if (batch->hasAABloat()) {
300 bounds->outset(0.5f, 0.5f);
301 } else {
302 // We don't know which way the particular GPU will snap lines or points at integer
303 // coords. So we ensure that the bounds is large enough for either snap.
304 SkRect before = *bounds;
305 bounds->roundOut(bounds);
306 if (bounds->fLeft == before.fLeft) {
307 bounds->fLeft -= 1;
308 }
309 if (bounds->fTop == before.fTop) {
310 bounds->fTop -= 1;
311 }
312 if (bounds->fRight == before.fRight) {
313 bounds->fRight += 1;
314 }
315 if (bounds->fBottom == before.fBottom) {
316 bounds->fBottom += 1;
317 }
318 }
319 }
320}
321
bsalomon6cc90062016-07-08 11:31:22 -0700322static inline bool intersect(SkRect* out, const SkRect& a, const SkRect& b) {
323 SkASSERT(a.fLeft <= a.fRight && a.fTop <= a.fBottom);
324 SkASSERT(b.fLeft <= b.fRight && b.fTop <= b.fBottom);
325 out->fLeft = SkTMax(a.fLeft, b.fLeft);
326 out->fTop = SkTMax(a.fTop, b.fTop);
327 out->fRight = SkTMin(a.fRight, b.fRight);
328 out->fBottom = SkTMin(a.fBottom, b.fBottom);
329 return (out->fLeft <= out->fRight && out->fTop <= out->fBottom);
330}
331
robertphillips391395d2016-03-02 09:26:36 -0800332void GrDrawTarget::drawBatch(const GrPipelineBuilder& pipelineBuilder,
robertphillips976f5f02016-06-03 10:59:20 -0700333 GrDrawContext* drawContext,
cdalton862cff32016-05-12 15:09:48 -0700334 const GrClip& clip,
335 GrDrawBatch* batch) {
joshualitt4d8da812015-01-28 12:53:54 -0800336 // Setup clip
cdalton862cff32016-05-12 15:09:48 -0700337 GrAppliedClip appliedClip;
bsalomon88cf17d2016-07-08 06:40:56 -0700338 SkRect bounds;
339 batch_bounds(&bounds, batch);
robertphillips59cf61a2016-07-13 09:18:21 -0700340 if (!clip.apply(fContext, drawContext, &bounds,
341 pipelineBuilder.isHWAntialias(), pipelineBuilder.hasUserStencilSettings(),
342 &appliedClip)) {
cdalton862cff32016-05-12 15:09:48 -0700343 return;
joshualitt4d8da812015-01-28 12:53:54 -0800344 }
robertphillips391395d2016-03-02 09:26:36 -0800345
robertphillips55fdccc2016-06-06 06:16:20 -0700346 // TODO: this is the only remaining usage of the AutoRestoreFragmentProcessorState - remove it
cdaltond4727922015-11-10 12:49:06 -0800347 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
bungeman06ca8ec2016-06-09 08:01:03 -0700348 if (appliedClip.getClipCoverageFragmentProcessor()) {
cdaltond4727922015-11-10 12:49:06 -0800349 arfps.set(&pipelineBuilder);
bungeman06ca8ec2016-06-09 08:01:03 -0700350 arfps.addCoverageFragmentProcessor(sk_ref_sp(appliedClip.getClipCoverageFragmentProcessor()));
cdaltond4727922015-11-10 12:49:06 -0800351 }
joshualitt4d8da812015-01-28 12:53:54 -0800352
bsalomonad792c12015-09-10 11:10:50 -0700353 GrPipeline::CreateArgs args;
cdalton193d9cf2016-05-12 11:52:02 -0700354 args.fPipelineBuilder = &pipelineBuilder;
robertphillips55fdccc2016-06-06 06:16:20 -0700355 args.fDrawContext = drawContext;
cdalton193d9cf2016-05-12 11:52:02 -0700356 args.fCaps = this->caps();
cdalton862cff32016-05-12 15:09:48 -0700357 args.fScissor = &appliedClip.scissorState();
358 args.fHasStencilClip = appliedClip.hasStencilClip();
359 if (pipelineBuilder.hasUserStencilSettings() || appliedClip.hasStencilClip()) {
robertphillips55fdccc2016-06-06 06:16:20 -0700360 if (!fResourceProvider->attachStencilAttachment(drawContext->accessRenderTarget())) {
cdalton17bf8202016-05-13 11:27:15 -0700361 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
362 return;
363 }
cdalton193d9cf2016-05-12 11:52:02 -0700364 }
365 batch->getPipelineOptimizations(&args.fOpts);
366 GrScissorState finalScissor;
cdalton862cff32016-05-12 15:09:48 -0700367 if (args.fOpts.fOverrides.fUsePLSDstRead || fClipBatchToBounds) {
cdalton193d9cf2016-05-12 11:52:02 -0700368 GrGLIRect viewport;
369 viewport.fLeft = 0;
370 viewport.fBottom = 0;
robertphillips55fdccc2016-06-06 06:16:20 -0700371 viewport.fWidth = drawContext->width();
372 viewport.fHeight = drawContext->height();
cdalton193d9cf2016-05-12 11:52:02 -0700373 SkIRect ibounds;
374 ibounds.fLeft = SkTPin(SkScalarFloorToInt(batch->bounds().fLeft), viewport.fLeft,
375 viewport.fWidth);
376 ibounds.fTop = SkTPin(SkScalarFloorToInt(batch->bounds().fTop), viewport.fBottom,
377 viewport.fHeight);
378 ibounds.fRight = SkTPin(SkScalarCeilToInt(batch->bounds().fRight), viewport.fLeft,
379 viewport.fWidth);
380 ibounds.fBottom = SkTPin(SkScalarCeilToInt(batch->bounds().fBottom), viewport.fBottom,
381 viewport.fHeight);
cdalton862cff32016-05-12 15:09:48 -0700382 if (appliedClip.scissorState().enabled()) {
383 const SkIRect& scissorRect = appliedClip.scissorState().rect();
cdalton193d9cf2016-05-12 11:52:02 -0700384 if (!ibounds.intersect(scissorRect)) {
cdalton862cff32016-05-12 15:09:48 -0700385 return;
cdalton193d9cf2016-05-12 11:52:02 -0700386 }
387 }
388 finalScissor.set(ibounds);
389 args.fScissor = &finalScissor;
390 }
bungeman06ca8ec2016-06-09 08:01:03 -0700391 args.fOpts.fColorPOI.completeCalculations(
392 sk_sp_address_as_pointer_address(pipelineBuilder.fColorFragmentProcessors.begin()),
393 pipelineBuilder.numColorFragmentProcessors());
cdalton193d9cf2016-05-12 11:52:02 -0700394 args.fOpts.fCoveragePOI.completeCalculations(
bungeman06ca8ec2016-06-09 08:01:03 -0700395 sk_sp_address_as_pointer_address(pipelineBuilder.fCoverageFragmentProcessors.begin()),
396 pipelineBuilder.numCoverageFragmentProcessors());
robertphillips55fdccc2016-06-06 06:16:20 -0700397 if (!this->setupDstReadIfNecessary(pipelineBuilder, drawContext->accessRenderTarget(),
398 clip, args.fOpts,
399 &args.fDstTexture, batch->bounds())) {
cdalton193d9cf2016-05-12 11:52:02 -0700400 return;
401 }
402
403 if (!batch->installPipeline(args)) {
egdaniele36914c2015-02-13 09:00:33 -0800404 return;
405 }
bsalomonad792c12015-09-10 11:10:50 -0700406
robertphillips498d7ac2015-10-30 10:11:30 -0700407#ifdef ENABLE_MDB
408 SkASSERT(fRenderTarget);
409 batch->pipeline()->addDependenciesTo(fRenderTarget);
410#endif
bsalomon6cc90062016-07-08 11:31:22 -0700411 SkRect clippedBounds;
412 SkAssertResult(intersect(&clippedBounds, bounds, appliedClip.deviceBounds()));
413 this->recordBatch(batch, clippedBounds);
joshualitt4d8da812015-01-28 12:53:54 -0800414}
415
robertphillips59cf61a2016-07-13 09:18:21 -0700416void GrDrawTarget::stencilPath(GrDrawContext* drawContext,
cdalton862cff32016-05-12 15:09:48 -0700417 const GrClip& clip,
robertphillips59cf61a2016-07-13 09:18:21 -0700418 const GrUserStencilSettings* ss,
419 bool useHWAA,
joshualittf2384692015-09-10 11:00:51 -0700420 const SkMatrix& viewMatrix,
robertphillips59cf61a2016-07-13 09:18:21 -0700421 const GrPath* path) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000422 // TODO: extract portions of checkDraw that are relevant to path stenciling.
bsalomon49f085d2014-09-05 13:34:00 -0700423 SkASSERT(path);
jvanverthe9c0fc62015-04-29 11:18:05 -0700424 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
joshualitt2c93efe2014-11-06 12:57:13 -0800425
426 // Setup clip
cdalton862cff32016-05-12 15:09:48 -0700427 GrAppliedClip appliedClip;
robertphillips59cf61a2016-07-13 09:18:21 -0700428 if (!clip.apply(fContext, drawContext, nullptr, useHWAA, SkToBool(ss), &appliedClip)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800429 return;
430 }
cdalton862cff32016-05-12 15:09:48 -0700431 // TODO: respect fClipBatchToBounds if we ever start computing bounds here.
joshualitt2c93efe2014-11-06 12:57:13 -0800432
cdalton846c0512016-05-13 10:25:00 -0700433 // Coverage AA does not make sense when rendering to the stencil buffer. The caller should never
434 // attempt this in a situation that would require coverage AA.
bungeman06ca8ec2016-06-09 08:01:03 -0700435 SkASSERT(!appliedClip.getClipCoverageFragmentProcessor());
bsalomon0ba8c242015-10-07 09:20:28 -0700436
robertphillips55fdccc2016-06-06 06:16:20 -0700437 GrStencilAttachment* stencilAttachment = fResourceProvider->attachStencilAttachment(
438 drawContext->accessRenderTarget());
cdalton17bf8202016-05-13 11:27:15 -0700439 if (!stencilAttachment) {
440 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
441 return;
442 }
joshualitt2c93efe2014-11-06 12:57:13 -0800443
joshualittf2384692015-09-10 11:00:51 -0700444 GrBatch* batch = GrStencilPathBatch::Create(viewMatrix,
robertphillips59cf61a2016-07-13 09:18:21 -0700445 useHWAA,
446 path->getFillType(),
cdalton862cff32016-05-12 15:09:48 -0700447 appliedClip.hasStencilClip(),
cdalton93a379b2016-05-11 13:58:08 -0700448 stencilAttachment->bits(),
cdalton862cff32016-05-12 15:09:48 -0700449 appliedClip.scissorState(),
robertphillips55fdccc2016-06-06 06:16:20 -0700450 drawContext->accessRenderTarget(),
bsalomona44919e2015-08-18 13:28:19 -0700451 path);
bsalomon6cc90062016-07-08 11:31:22 -0700452 this->recordBatch(batch, appliedClip.deviceBounds());
bsalomona44919e2015-08-18 13:28:19 -0700453 batch->unref();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000454}
455
robertphillips9199a9f2016-07-13 07:48:43 -0700456void GrDrawTarget::addBatch(sk_sp<GrBatch> batch) {
457 this->recordBatch(batch.get(), batch->bounds());
bsalomon53469832015-08-18 09:20:09 -0700458}
459
bsalomon9f129de2016-08-10 16:31:05 -0700460void GrDrawTarget::fullClear(GrRenderTarget* renderTarget, GrColor color) {
461 // Currently this just inserts a clear batch. However, once in MDB this can remove all the
462 // previously recorded batches and change the load op to clear with supplied color.
463 sk_sp<GrBatch> batch = GrClearBatch::Make(SkIRect::MakeWH(renderTarget->width(),
464 renderTarget->height()),
465 color, renderTarget);
466 this->recordBatch(batch.get(), batch->bounds());
467}
468
bsalomon53469832015-08-18 09:20:09 -0700469void GrDrawTarget::discard(GrRenderTarget* renderTarget) {
bsalomon9f129de2016-08-10 16:31:05 -0700470 // Currently this just inserts a discard batch. However, once in MDB this can remove all the
471 // previously recorded batches and change the load op to discard.
bsalomon53469832015-08-18 09:20:09 -0700472 if (this->caps()->discardRenderTargetSupport()) {
halcanary385fe4d2015-08-26 13:07:48 -0700473 GrBatch* batch = new GrDiscardBatch(renderTarget);
bsalomon6cc90062016-07-08 11:31:22 -0700474 this->recordBatch(batch, batch->bounds());
bsalomon53469832015-08-18 09:20:09 -0700475 batch->unref();
bsalomon63b21962014-11-05 07:05:34 -0800476 }
477}
478
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000479////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000480
bsalomonb8fea972016-02-16 07:34:17 -0800481bool GrDrawTarget::copySurface(GrSurface* dst,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000482 GrSurface* src,
483 const SkIRect& srcRect,
484 const SkIPoint& dstPoint) {
bsalomon872062c2015-08-18 12:12:35 -0700485 GrBatch* batch = GrCopySurfaceBatch::Create(dst, src, srcRect, dstPoint);
bsalomonb8fea972016-02-16 07:34:17 -0800486 if (!batch) {
487 return false;
488 }
robertphillips498d7ac2015-10-30 10:11:30 -0700489#ifdef ENABLE_MDB
bsalomonb8fea972016-02-16 07:34:17 -0800490 this->addDependency(src);
robertphillips498d7ac2015-10-30 10:11:30 -0700491#endif
492
bsalomon6cc90062016-07-08 11:31:22 -0700493 this->recordBatch(batch, batch->bounds());
bsalomonb8fea972016-02-16 07:34:17 -0800494 batch->unref();
495 return true;
bsalomon@google.comeb851172013-04-15 13:51:00 +0000496}
497
bsalomon6cc90062016-07-08 11:31:22 -0700498static inline bool can_reorder(const SkRect& a, const SkRect& b) {
bsalomon88cf17d2016-07-08 06:40:56 -0700499 return a.fRight <= b.fLeft || a.fBottom <= b.fTop ||
500 b.fRight <= a.fLeft || b.fBottom <= a.fTop;
501}
502
bsalomon6cc90062016-07-08 11:31:22 -0700503static void join(SkRect* out, const SkRect& a, const SkRect& b) {
504 SkASSERT(a.fLeft <= a.fRight && a.fTop <= a.fBottom);
505 SkASSERT(b.fLeft <= b.fRight && b.fTop <= b.fBottom);
506 out->fLeft = SkTMin(a.fLeft, b.fLeft);
507 out->fTop = SkTMin(a.fTop, b.fTop);
508 out->fRight = SkTMax(a.fRight, b.fRight);
509 out->fBottom = SkTMax(a.fBottom, b.fBottom);
bsalomon512be532015-09-10 10:42:55 -0700510}
511
bsalomon6cc90062016-07-08 11:31:22 -0700512void GrDrawTarget::recordBatch(GrBatch* batch, const SkRect& clippedBounds) {
robertphillipsa106c622015-10-16 09:07:06 -0700513 // A closed drawTarget should never receive new/more batches
robertphillips6a186652015-10-20 07:37:58 -0700514 SkASSERT(!this->isClosed());
robertphillipsa106c622015-10-16 09:07:06 -0700515
bsalomon512be532015-09-10 10:42:55 -0700516 // Check if there is a Batch Draw we can batch with by linearly searching back until we either
517 // 1) check every draw
518 // 2) intersect with something
519 // 3) find a 'blocker'
joshualittb0666ad2016-03-08 10:43:41 -0800520 GR_AUDIT_TRAIL_ADDBATCH(fAuditTrail, batch);
bsalomon512be532015-09-10 10:42:55 -0700521 GrBATCH_INFO("Re-Recording (%s, B%u)\n"
joshualitte2bcec32015-09-30 06:22:22 -0700522 "\tBounds LRTB (%f, %f, %f, %f)\n",
bsalomon512be532015-09-10 10:42:55 -0700523 batch->name(),
524 batch->uniqueID(),
525 batch->bounds().fLeft, batch->bounds().fRight,
526 batch->bounds().fTop, batch->bounds().fBottom);
527 GrBATCH_INFO(SkTabString(batch->dumpInfo(), 1).c_str());
bsalomon6cc90062016-07-08 11:31:22 -0700528 GrBATCH_INFO("\tClipped Bounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n",
529 clippedBounds.fLeft, clippedBounds.fTop, clippedBounds.fRight,
530 clippedBounds.fBottom);
halcanary9d524f22016-03-29 09:03:52 -0700531 GrBATCH_INFO("\tOutcome:\n");
bsalomon6cc90062016-07-08 11:31:22 -0700532 int maxCandidates = SkTMin(fMaxBatchLookback, fRecordedBatches.count());
bsalomon512be532015-09-10 10:42:55 -0700533 if (maxCandidates) {
534 int i = 0;
535 while (true) {
bsalomon6cc90062016-07-08 11:31:22 -0700536 GrBatch* candidate = fRecordedBatches.fromBack(i).fBatch.get();
bsalomon512be532015-09-10 10:42:55 -0700537 // We cannot continue to search backwards if the render target changes
538 if (candidate->renderTargetUniqueID() != batch->renderTargetUniqueID()) {
539 GrBATCH_INFO("\t\tBreaking because of (%s, B%u) Rendertarget\n",
540 candidate->name(), candidate->uniqueID());
541 break;
542 }
543 if (candidate->combineIfPossible(batch, *this->caps())) {
544 GrBATCH_INFO("\t\tCombining with (%s, B%u)\n", candidate->name(),
545 candidate->uniqueID());
joshualittb0666ad2016-03-08 10:43:41 -0800546 GR_AUDIT_TRAIL_BATCHING_RESULT_COMBINED(fAuditTrail, candidate, batch);
bsalomon6cc90062016-07-08 11:31:22 -0700547 join(&fRecordedBatches.fromBack(i).fClippedBounds,
548 fRecordedBatches.fromBack(i).fClippedBounds, clippedBounds);
bsalomon512be532015-09-10 10:42:55 -0700549 return;
550 }
551 // Stop going backwards if we would cause a painter's order violation.
bsalomon6cc90062016-07-08 11:31:22 -0700552 const SkRect& candidateBounds = fRecordedBatches.fromBack(i).fClippedBounds;
553 if (!can_reorder(candidateBounds, clippedBounds)) {
bsalomon512be532015-09-10 10:42:55 -0700554 GrBATCH_INFO("\t\tIntersects with (%s, B%u)\n", candidate->name(),
555 candidate->uniqueID());
556 break;
557 }
558 ++i;
559 if (i == maxCandidates) {
560 GrBATCH_INFO("\t\tReached max lookback or beginning of batch array %d\n", i);
561 break;
562 }
563 }
564 } else {
565 GrBATCH_INFO("\t\tFirstBatch\n");
566 }
joshualitt18d6b752016-02-26 08:07:50 -0800567 GR_AUDIT_TRAIL_BATCHING_RESULT_NEW(fAuditTrail, batch);
bsalomon6cc90062016-07-08 11:31:22 -0700568 fRecordedBatches.emplace_back(RecordedBatch{sk_ref_sp(batch), clippedBounds});
bsalomon512be532015-09-10 10:42:55 -0700569}
570
bsalomonaecc0182016-03-07 11:50:44 -0800571void GrDrawTarget::forwardCombine() {
bsalomon6cc90062016-07-08 11:31:22 -0700572 for (int i = 0; i < fRecordedBatches.count() - 2; ++i) {
573 GrBatch* batch = fRecordedBatches[i].fBatch.get();
574 const SkRect& batchBounds = fRecordedBatches[i].fClippedBounds;
575 int maxCandidateIdx = SkTMin(i + fMaxBatchLookahead, fRecordedBatches.count() - 1);
bsalomonaecc0182016-03-07 11:50:44 -0800576 int j = i + 1;
577 while (true) {
bsalomon6cc90062016-07-08 11:31:22 -0700578 GrBatch* candidate = fRecordedBatches[j].fBatch.get();
bsalomonaecc0182016-03-07 11:50:44 -0800579 // We cannot continue to search if the render target changes
580 if (candidate->renderTargetUniqueID() != batch->renderTargetUniqueID()) {
581 GrBATCH_INFO("\t\tBreaking because of (%s, B%u) Rendertarget\n",
582 candidate->name(), candidate->uniqueID());
583 break;
584 }
585 if (j == i +1) {
586 // We assume batch would have combined with candidate when the candidate was added
587 // via backwards combining in recordBatch.
588 SkASSERT(!batch->combineIfPossible(candidate, *this->caps()));
589 } else if (batch->combineIfPossible(candidate, *this->caps())) {
590 GrBATCH_INFO("\t\tCombining with (%s, B%u)\n", candidate->name(),
591 candidate->uniqueID());
joshualittb0666ad2016-03-08 10:43:41 -0800592 GR_AUDIT_TRAIL_BATCHING_RESULT_COMBINED(fAuditTrail, batch, candidate);
bsalomon6cc90062016-07-08 11:31:22 -0700593 fRecordedBatches[j].fBatch = std::move(fRecordedBatches[i].fBatch);
594 join(&fRecordedBatches[j].fClippedBounds, fRecordedBatches[j].fClippedBounds,
595 batchBounds);
bsalomonaecc0182016-03-07 11:50:44 -0800596 break;
597 }
598 // Stop going traversing if we would cause a painter's order violation.
bsalomon6cc90062016-07-08 11:31:22 -0700599 const SkRect& candidateBounds = fRecordedBatches[j].fClippedBounds;
600 if (!can_reorder(candidateBounds, batchBounds)) {
bsalomonaecc0182016-03-07 11:50:44 -0800601 GrBATCH_INFO("\t\tIntersects with (%s, B%u)\n", candidate->name(),
602 candidate->uniqueID());
603 break;
604 }
605 ++j;
606 if (j > maxCandidateIdx) {
607 GrBATCH_INFO("\t\tReached max lookahead or end of batch array %d\n", i);
608 break;
609 }
610 }
611 }
612}
613
egdaniele36914c2015-02-13 09:00:33 -0800614///////////////////////////////////////////////////////////////////////////////
615
bsalomonb3b9aec2015-09-10 11:16:35 -0700616void GrDrawTarget::clearStencilClip(const SkIRect& rect, bool insideClip, GrRenderTarget* rt) {
halcanary385fe4d2015-08-26 13:07:48 -0700617 GrBatch* batch = new GrClearStencilClipBatch(rect, insideClip, rt);
bsalomon6cc90062016-07-08 11:31:22 -0700618 this->recordBatch(batch, batch->bounds());
bsalomon5ea03632015-08-18 10:33:30 -0700619 batch->unref();
620}