blob: 0117d0e1a15ae1431f7532465ec2264b91328c05 [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
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"
robertphillips976f5f02016-06-03 10:59:20 -070013#include "GrDrawContext.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
reed@google.comac10a2d2010-12-22 21:39:39 +000041////////////////////////////////////////////////////////////////////////////////
42
bsalomon489147c2015-12-14 12:13:09 -080043// Experimentally we have found that most batching occurs within the first 10 comparisons.
bsalomonaecc0182016-03-07 11:50:44 -080044static const int kDefaultMaxBatchLookback = 10;
45static const int kDefaultMaxBatchLookahead = 10;
bsalomon489147c2015-12-14 12:13:09 -080046
bsalomon69cfe952015-11-30 13:27:47 -080047GrDrawTarget::GrDrawTarget(GrRenderTarget* rt, GrGpu* gpu, GrResourceProvider* resourceProvider,
joshualitt086cee12016-01-12 06:45:24 -080048 GrAuditTrail* auditTrail, const Options& options)
bsalomonfd8d0132016-08-11 11:25:33 -070049 : fLastFullClearBatch(nullptr)
50 , fGpu(SkRef(gpu))
bsalomon4061b122015-05-29 10:26:19 -070051 , fResourceProvider(resourceProvider)
joshualitt086cee12016-01-12 06:45:24 -080052 , fAuditTrail(auditTrail)
bsalomon648c6962015-10-23 09:06:59 -070053 , fFlags(0)
csmartdaltone0d36292016-07-29 08:14:20 -070054 , fRenderTarget(rt) {
csmartdaltonc6f411e2016-08-05 22:32:12 -070055 // TODO: Stop extracting the context (currently needed by GrClip)
bsalomonb3b9aec2015-09-10 11:16:35 -070056 fContext = fGpu->getContext();
robertphillips4beb5c12015-10-20 07:50:00 -070057
cdalton862cff32016-05-12 15:09:48 -070058 fClipBatchToBounds = options.fClipBatchToBounds;
bsalomon6dea83f2015-12-03 12:58:06 -080059 fDrawBatchBounds = options.fDrawBatchBounds;
bsalomon489147c2015-12-14 12:13:09 -080060 fMaxBatchLookback = (options.fMaxBatchLookback < 0) ? kDefaultMaxBatchLookback :
61 options.fMaxBatchLookback;
bsalomonaecc0182016-03-07 11:50:44 -080062 fMaxBatchLookahead = (options.fMaxBatchLookahead < 0) ? kDefaultMaxBatchLookahead :
63 options.fMaxBatchLookahead;
bsalomon6dea83f2015-12-03 12:58:06 -080064
csmartdaltone0d36292016-07-29 08:14:20 -070065 if (GrCaps::InstancedSupport::kNone != this->caps()->instancedSupport()) {
66 fInstancedRendering.reset(fGpu->createInstancedRendering());
67 }
68
robertphillips0dfa62c2015-11-16 06:23:31 -080069 rt->setLastDrawTarget(this);
70
robertphillips4beb5c12015-10-20 07:50:00 -070071#ifdef SK_DEBUG
72 static int debugID = 0;
73 fDebugID = debugID++;
74#endif
bsalomon4061b122015-05-29 10:26:19 -070075}
76
77GrDrawTarget::~GrDrawTarget() {
robertphillips498d7ac2015-10-30 10:11:30 -070078 if (fRenderTarget && this == fRenderTarget->getLastDrawTarget()) {
79 fRenderTarget->setLastDrawTarget(nullptr);
80 }
81
bsalomon4061b122015-05-29 10:26:19 -070082 fGpu->unref();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000083}
84
85////////////////////////////////////////////////////////////////////////////////
86
robertphillips6a186652015-10-20 07:37:58 -070087// Add a GrDrawTarget-based dependency
88void GrDrawTarget::addDependency(GrDrawTarget* dependedOn) {
89 SkASSERT(!dependedOn->dependsOn(this)); // loops are bad
90
91 if (this->dependsOn(dependedOn)) {
92 return; // don't add duplicate dependencies
93 }
94
95 *fDependencies.push() = dependedOn;
96}
97
98// Convert from a GrSurface-based dependency to a GrDrawTarget one
99void GrDrawTarget::addDependency(GrSurface* dependedOn) {
100 if (dependedOn->asRenderTarget() && dependedOn->asRenderTarget()->getLastDrawTarget()) {
101 // If it is still receiving dependencies, this DT shouldn't be closed
102 SkASSERT(!this->isClosed());
103
104 GrDrawTarget* dt = dependedOn->asRenderTarget()->getLastDrawTarget();
105 if (dt == this) {
106 // self-read - presumably for dst reads
107 } else {
108 this->addDependency(dt);
109
110 // Can't make it closed in the self-read case
111 dt->makeClosed();
112 }
113 }
114}
115
robertphillips4beb5c12015-10-20 07:50:00 -0700116#ifdef SK_DEBUG
117void GrDrawTarget::dump() const {
118 SkDebugf("--------------------------------------------------------------\n");
robertphillips8abb3702016-08-31 14:04:06 -0700119 SkDebugf("node: %d -> RT: %d\n", fDebugID, fRenderTarget ? fRenderTarget->uniqueID() : -1);
robertphillips4beb5c12015-10-20 07:50:00 -0700120 SkDebugf("relies On (%d): ", fDependencies.count());
121 for (int i = 0; i < fDependencies.count(); ++i) {
122 SkDebugf("%d, ", fDependencies[i]->fDebugID);
123 }
124 SkDebugf("\n");
bsalomon6cc90062016-07-08 11:31:22 -0700125 SkDebugf("batches (%d):\n", fRecordedBatches.count());
126 for (int i = 0; i < fRecordedBatches.count(); ++i) {
robertphillips4beb5c12015-10-20 07:50:00 -0700127 SkDebugf("*******************************\n");
bsalomon6cc90062016-07-08 11:31:22 -0700128 if (!fRecordedBatches[i].fBatch) {
bsalomonaecc0182016-03-07 11:50:44 -0800129 SkDebugf("%d: <combined forward>\n", i);
130 } else {
bsalomon6cc90062016-07-08 11:31:22 -0700131 SkDebugf("%d: %s\n", i, fRecordedBatches[i].fBatch->name());
132 SkString str = fRecordedBatches[i].fBatch->dumpInfo();
bsalomonaecc0182016-03-07 11:50:44 -0800133 SkDebugf("%s\n", str.c_str());
bsalomon6cc90062016-07-08 11:31:22 -0700134 const SkRect& clippedBounds = fRecordedBatches[i].fClippedBounds;
135 SkDebugf("ClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n",
136 clippedBounds.fLeft, clippedBounds.fTop, clippedBounds.fRight,
137 clippedBounds.fBottom);
bsalomonaecc0182016-03-07 11:50:44 -0800138 }
robertphillips4beb5c12015-10-20 07:50:00 -0700139 }
140}
141#endif
142
bsalomon50785a32015-02-06 07:02:37 -0800143bool GrDrawTarget::setupDstReadIfNecessary(const GrPipelineBuilder& pipelineBuilder,
robertphillips55fdccc2016-06-06 06:16:20 -0700144 GrRenderTarget* rt,
cdalton862cff32016-05-12 15:09:48 -0700145 const GrClip& clip,
ethannicholasde4166a2015-11-30 08:57:38 -0800146 const GrPipelineOptimizations& optimizations,
bsalomon6a44c6a2015-05-26 09:49:05 -0700147 GrXferProcessor::DstTexture* dstTexture,
bsalomonad792c12015-09-10 11:10:50 -0700148 const SkRect& batchBounds) {
149 SkRect bounds = batchBounds;
150 bounds.outset(0.5f, 0.5f);
151
ethannicholasde4166a2015-11-30 08:57:38 -0800152 if (!pipelineBuilder.willXPNeedDstTexture(*this->caps(), optimizations)) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000153 return true;
154 }
cdalton9954bc32015-04-29 14:17:00 -0700155
cdalton9954bc32015-04-29 14:17:00 -0700156 if (this->caps()->textureBarrierSupport()) {
157 if (GrTexture* rtTex = rt->asTexture()) {
bsalomondc47ff72015-05-26 12:16:59 -0700158 // The render target is a texture, so we can read from it directly in the shader. The XP
cdalton9954bc32015-04-29 14:17:00 -0700159 // will be responsible to detect this situation and request a texture barrier.
bsalomon6a44c6a2015-05-26 09:49:05 -0700160 dstTexture->setTexture(rtTex);
161 dstTexture->setOffset(0, 0);
cdalton9954bc32015-04-29 14:17:00 -0700162 return true;
163 }
164 }
165
166 SkIRect copyRect;
cdalton862cff32016-05-12 15:09:48 -0700167 clip.getConservativeBounds(rt->width(), rt->height(), &copyRect);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000168
bsalomonad792c12015-09-10 11:10:50 -0700169 SkIRect drawIBounds;
170 bounds.roundOut(&drawIBounds);
171 if (!copyRect.intersect(drawIBounds)) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000172#ifdef SK_DEBUG
bsalomonb3b9aec2015-09-10 11:16:35 -0700173 GrCapsDebugf(this->caps(), "Missed an early reject. "
174 "Bailing on draw from setupDstReadIfNecessary.\n");
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000175#endif
bsalomonad792c12015-09-10 11:10:50 -0700176 return false;
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000177 }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000178
commit-bot@chromium.org63150af2013-04-11 22:00:22 +0000179 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
180 // have per-sample dst values by making the copy multisampled.
bsalomonf2703d82014-10-28 14:33:06 -0700181 GrSurfaceDesc desc;
egdaniel4bcd62e2016-08-31 07:37:31 -0700182 if (!fGpu->initDescForDstCopy(rt, &desc)) {
bsalomona73239a2015-04-28 13:35:17 -0700183 desc.fOrigin = kDefault_GrSurfaceOrigin;
184 desc.fFlags = kRenderTarget_GrSurfaceFlag;
185 desc.fConfig = rt->config();
186 }
187
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000188 desc.fWidth = copyRect.width();
189 desc.fHeight = copyRect.height();
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000190
bsalomoneae62002015-07-31 13:59:30 -0700191 static const uint32_t kFlags = 0;
192 SkAutoTUnref<GrTexture> copy(fResourceProvider->createApproxTexture(desc, kFlags));
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000193
bsalomone3059732014-10-14 11:47:22 -0700194 if (!copy) {
tfarina38406c82014-10-31 07:11:12 -0700195 SkDebugf("Failed to create temporary copy of destination texture.\n");
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000196 return false;
197 }
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000198 SkIPoint dstPoint = {0, 0};
bsalomon6df86402015-06-01 10:41:49 -0700199 this->copySurface(copy, rt, copyRect, dstPoint);
200 dstTexture->setTexture(copy);
201 dstTexture->setOffset(copyRect.fLeft, copyRect.fTop);
202 return true;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000203}
204
robertphillipsa13e2022015-11-11 12:01:09 -0800205void GrDrawTarget::prepareBatches(GrBatchFlushState* flushState) {
robertphillipsa106c622015-10-16 09:07:06 -0700206 // Semi-usually the drawTargets are already closed at this point, but sometimes Ganesh
207 // needs to flush mid-draw. In that case, the SkGpuDevice's drawTargets won't be closed
208 // but need to be flushed anyway. Closing such drawTargets here will mean new
209 // drawTargets will be created to replace them if the SkGpuDevice(s) write to them again.
210 this->makeClosed();
211
robertphillips498d7ac2015-10-30 10:11:30 -0700212 // Loop over the batches that haven't yet generated their geometry
bsalomon6cc90062016-07-08 11:31:22 -0700213 for (int i = 0; i < fRecordedBatches.count(); ++i) {
214 if (fRecordedBatches[i].fBatch) {
215 fRecordedBatches[i].fBatch->prepare(flushState);
bsalomonaecc0182016-03-07 11:50:44 -0800216 }
bsalomon512be532015-09-10 10:42:55 -0700217 }
csmartdaltona7f29642016-07-07 08:49:11 -0700218
219 if (fInstancedRendering) {
220 fInstancedRendering->beginFlush(flushState->resourceProvider());
221 }
robertphillipsa13e2022015-11-11 12:01:09 -0800222}
bsalomon512be532015-09-10 10:42:55 -0700223
bsalomondc438982016-08-31 11:53:49 -0700224bool GrDrawTarget::drawBatches(GrBatchFlushState* flushState) {
egdanielb4021cf2016-07-28 08:53:07 -0700225 if (0 == fRecordedBatches.count()) {
bsalomondc438982016-08-31 11:53:49 -0700226 return false;
egdanielb4021cf2016-07-28 08:53:07 -0700227 }
bsalomon512be532015-09-10 10:42:55 -0700228 // Draw all the generated geometry.
bsalomon6dea83f2015-12-03 12:58:06 -0800229 SkRandom random;
egdaniel9cb63402016-06-23 08:37:05 -0700230 GrRenderTarget* currentRT = nullptr;
231 SkAutoTDelete<GrGpuCommandBuffer> commandBuffer;
egdaniele7d1b242016-07-01 08:06:45 -0700232 SkRect bounds = SkRect::MakeEmpty();
bsalomon6cc90062016-07-08 11:31:22 -0700233 for (int i = 0; i < fRecordedBatches.count(); ++i) {
234 if (!fRecordedBatches[i].fBatch) {
bsalomonaecc0182016-03-07 11:50:44 -0800235 continue;
236 }
bsalomon6cc90062016-07-08 11:31:22 -0700237 if (fRecordedBatches[i].fBatch->renderTarget() != currentRT) {
egdaniel9cb63402016-06-23 08:37:05 -0700238 if (commandBuffer) {
239 commandBuffer->end();
egdaniele7d1b242016-07-01 08:06:45 -0700240 if (bounds.intersect(0, 0,
241 SkIntToScalar(currentRT->width()),
242 SkIntToScalar(currentRT->height()))) {
243 SkIRect iBounds;
244 bounds.roundOut(&iBounds);
245 commandBuffer->submit(iBounds);
246 }
egdaniel9cb63402016-06-23 08:37:05 -0700247 commandBuffer.reset();
248 }
egdaniele7d1b242016-07-01 08:06:45 -0700249 bounds.setEmpty();
bsalomon6cc90062016-07-08 11:31:22 -0700250 currentRT = fRecordedBatches[i].fBatch->renderTarget();
egdaniel9cb63402016-06-23 08:37:05 -0700251 if (currentRT) {
252 static const GrGpuCommandBuffer::LoadAndStoreInfo kBasicLoadStoreInfo
253 { GrGpuCommandBuffer::LoadOp::kLoad,GrGpuCommandBuffer::StoreOp::kStore,
254 GrColor_ILLEGAL };
255 commandBuffer.reset(fGpu->createCommandBuffer(currentRT,
256 kBasicLoadStoreInfo, // Color
257 kBasicLoadStoreInfo)); // Stencil
258 }
259 flushState->setCommandBuffer(commandBuffer);
260 }
egdaniele7d1b242016-07-01 08:06:45 -0700261 if (commandBuffer) {
bsalomon6cc90062016-07-08 11:31:22 -0700262 bounds.join(fRecordedBatches[i].fClippedBounds);
egdaniele7d1b242016-07-01 08:06:45 -0700263 }
bsalomon6dea83f2015-12-03 12:58:06 -0800264 if (fDrawBatchBounds) {
bsalomon6cc90062016-07-08 11:31:22 -0700265 const SkRect& bounds = fRecordedBatches[i].fClippedBounds;
266 SkIRect ibounds;
267 bounds.roundOut(&ibounds);
bsalomon6dea83f2015-12-03 12:58:06 -0800268 // In multi-draw buffer all the batches use the same render target and we won't need to
269 // get the batchs bounds.
bsalomon6cc90062016-07-08 11:31:22 -0700270 if (GrRenderTarget* rt = fRecordedBatches[i].fBatch->renderTarget()) {
271 fGpu->drawDebugWireRect(rt, ibounds, 0xFF000000 | random.nextU());
bsalomon6dea83f2015-12-03 12:58:06 -0800272 }
273 }
bsalomon6cc90062016-07-08 11:31:22 -0700274 fRecordedBatches[i].fBatch->draw(flushState);
bsalomon512be532015-09-10 10:42:55 -0700275 }
egdaniel9cb63402016-06-23 08:37:05 -0700276 if (commandBuffer) {
277 commandBuffer->end();
egdaniele7d1b242016-07-01 08:06:45 -0700278 if (bounds.intersect(0, 0,
279 SkIntToScalar(currentRT->width()),
280 SkIntToScalar(currentRT->height()))) {
281 SkIRect iBounds;
282 bounds.roundOut(&iBounds);
283 commandBuffer->submit(iBounds);
284 }
egdaniel9cb63402016-06-23 08:37:05 -0700285 flushState->setCommandBuffer(nullptr);
286 }
ethannicholas22793252016-01-30 09:59:10 -0800287
jvanverthd2d2eb92016-02-17 14:04:46 -0800288 fGpu->finishDrawTarget();
bsalomondc438982016-08-31 11:53:49 -0700289 return true;
bsalomona73239a2015-04-28 13:35:17 -0700290}
291
bsalomon512be532015-09-10 10:42:55 -0700292void GrDrawTarget::reset() {
bsalomonfd8d0132016-08-11 11:25:33 -0700293 fLastFullClearBatch = nullptr;
bsalomon6cc90062016-07-08 11:31:22 -0700294 fRecordedBatches.reset();
csmartdaltona7f29642016-07-07 08:49:11 -0700295 if (fInstancedRendering) {
296 fInstancedRendering->endFlush();
297 }
bsalomon512be532015-09-10 10:42:55 -0700298}
299
bsalomon88cf17d2016-07-08 06:40:56 -0700300static void batch_bounds(SkRect* bounds, const GrBatch* batch) {
301 *bounds = batch->bounds();
302 if (batch->hasZeroArea()) {
303 if (batch->hasAABloat()) {
304 bounds->outset(0.5f, 0.5f);
305 } else {
306 // We don't know which way the particular GPU will snap lines or points at integer
307 // coords. So we ensure that the bounds is large enough for either snap.
308 SkRect before = *bounds;
309 bounds->roundOut(bounds);
310 if (bounds->fLeft == before.fLeft) {
311 bounds->fLeft -= 1;
312 }
313 if (bounds->fTop == before.fTop) {
314 bounds->fTop -= 1;
315 }
316 if (bounds->fRight == before.fRight) {
317 bounds->fRight += 1;
318 }
319 if (bounds->fBottom == before.fBottom) {
320 bounds->fBottom += 1;
321 }
322 }
323 }
324}
325
robertphillips391395d2016-03-02 09:26:36 -0800326void GrDrawTarget::drawBatch(const GrPipelineBuilder& pipelineBuilder,
robertphillips976f5f02016-06-03 10:59:20 -0700327 GrDrawContext* drawContext,
cdalton862cff32016-05-12 15:09:48 -0700328 const GrClip& clip,
329 GrDrawBatch* batch) {
joshualitt4d8da812015-01-28 12:53:54 -0800330 // Setup clip
bsalomon88cf17d2016-07-08 06:40:56 -0700331 SkRect bounds;
332 batch_bounds(&bounds, batch);
csmartdaltond211e782016-08-15 11:17:19 -0700333 GrAppliedClip appliedClip(bounds);
334 if (!clip.apply(fContext, drawContext, pipelineBuilder.isHWAntialias(),
335 pipelineBuilder.hasUserStencilSettings(), &appliedClip)) {
cdalton862cff32016-05-12 15:09:48 -0700336 return;
joshualitt4d8da812015-01-28 12:53:54 -0800337 }
robertphillips391395d2016-03-02 09:26:36 -0800338
robertphillips55fdccc2016-06-06 06:16:20 -0700339 // TODO: this is the only remaining usage of the AutoRestoreFragmentProcessorState - remove it
cdaltond4727922015-11-10 12:49:06 -0800340 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
csmartdaltond211e782016-08-15 11:17:19 -0700341 if (appliedClip.clipCoverageFragmentProcessor()) {
cdaltond4727922015-11-10 12:49:06 -0800342 arfps.set(&pipelineBuilder);
csmartdaltond211e782016-08-15 11:17:19 -0700343 arfps.addCoverageFragmentProcessor(sk_ref_sp(appliedClip.clipCoverageFragmentProcessor()));
cdaltond4727922015-11-10 12:49:06 -0800344 }
joshualitt4d8da812015-01-28 12:53:54 -0800345
cdalton862cff32016-05-12 15:09:48 -0700346 if (pipelineBuilder.hasUserStencilSettings() || appliedClip.hasStencilClip()) {
robertphillips55fdccc2016-06-06 06:16:20 -0700347 if (!fResourceProvider->attachStencilAttachment(drawContext->accessRenderTarget())) {
cdalton17bf8202016-05-13 11:27:15 -0700348 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
349 return;
350 }
cdalton193d9cf2016-05-12 11:52:02 -0700351 }
csmartdaltond211e782016-08-15 11:17:19 -0700352
353 GrPipeline::CreateArgs args;
354 args.fPipelineBuilder = &pipelineBuilder;
355 args.fDrawContext = drawContext;
356 args.fCaps = this->caps();
cdalton193d9cf2016-05-12 11:52:02 -0700357 batch->getPipelineOptimizations(&args.fOpts);
cdalton862cff32016-05-12 15:09:48 -0700358 if (args.fOpts.fOverrides.fUsePLSDstRead || fClipBatchToBounds) {
cdalton193d9cf2016-05-12 11:52:02 -0700359 GrGLIRect viewport;
360 viewport.fLeft = 0;
361 viewport.fBottom = 0;
robertphillips55fdccc2016-06-06 06:16:20 -0700362 viewport.fWidth = drawContext->width();
363 viewport.fHeight = drawContext->height();
cdalton193d9cf2016-05-12 11:52:02 -0700364 SkIRect ibounds;
365 ibounds.fLeft = SkTPin(SkScalarFloorToInt(batch->bounds().fLeft), viewport.fLeft,
366 viewport.fWidth);
367 ibounds.fTop = SkTPin(SkScalarFloorToInt(batch->bounds().fTop), viewport.fBottom,
368 viewport.fHeight);
369 ibounds.fRight = SkTPin(SkScalarCeilToInt(batch->bounds().fRight), viewport.fLeft,
370 viewport.fWidth);
371 ibounds.fBottom = SkTPin(SkScalarCeilToInt(batch->bounds().fBottom), viewport.fBottom,
372 viewport.fHeight);
csmartdaltond211e782016-08-15 11:17:19 -0700373 if (!appliedClip.addScissor(ibounds)) {
374 return;
cdalton193d9cf2016-05-12 11:52:02 -0700375 }
cdalton193d9cf2016-05-12 11:52:02 -0700376 }
bungeman06ca8ec2016-06-09 08:01:03 -0700377 args.fOpts.fColorPOI.completeCalculations(
378 sk_sp_address_as_pointer_address(pipelineBuilder.fColorFragmentProcessors.begin()),
379 pipelineBuilder.numColorFragmentProcessors());
cdalton193d9cf2016-05-12 11:52:02 -0700380 args.fOpts.fCoveragePOI.completeCalculations(
bungeman06ca8ec2016-06-09 08:01:03 -0700381 sk_sp_address_as_pointer_address(pipelineBuilder.fCoverageFragmentProcessors.begin()),
382 pipelineBuilder.numCoverageFragmentProcessors());
csmartdaltond211e782016-08-15 11:17:19 -0700383 args.fScissor = &appliedClip.scissorState();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700384 args.fWindowRectsState = &appliedClip.windowRectsState();
csmartdaltond211e782016-08-15 11:17:19 -0700385 args.fHasStencilClip = appliedClip.hasStencilClip();
robertphillips55fdccc2016-06-06 06:16:20 -0700386 if (!this->setupDstReadIfNecessary(pipelineBuilder, drawContext->accessRenderTarget(),
387 clip, args.fOpts,
388 &args.fDstTexture, batch->bounds())) {
cdalton193d9cf2016-05-12 11:52:02 -0700389 return;
390 }
391
392 if (!batch->installPipeline(args)) {
egdaniele36914c2015-02-13 09:00:33 -0800393 return;
394 }
bsalomonad792c12015-09-10 11:10:50 -0700395
robertphillips498d7ac2015-10-30 10:11:30 -0700396#ifdef ENABLE_MDB
397 SkASSERT(fRenderTarget);
398 batch->pipeline()->addDependenciesTo(fRenderTarget);
399#endif
csmartdaltond211e782016-08-15 11:17:19 -0700400 this->recordBatch(batch, appliedClip.clippedDrawBounds());
joshualitt4d8da812015-01-28 12:53:54 -0800401}
402
robertphillips59cf61a2016-07-13 09:18:21 -0700403void GrDrawTarget::stencilPath(GrDrawContext* drawContext,
cdalton862cff32016-05-12 15:09:48 -0700404 const GrClip& clip,
robertphillips59cf61a2016-07-13 09:18:21 -0700405 bool useHWAA,
joshualittf2384692015-09-10 11:00:51 -0700406 const SkMatrix& viewMatrix,
robertphillips59cf61a2016-07-13 09:18:21 -0700407 const GrPath* path) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000408 // TODO: extract portions of checkDraw that are relevant to path stenciling.
bsalomon49f085d2014-09-05 13:34:00 -0700409 SkASSERT(path);
jvanverthe9c0fc62015-04-29 11:18:05 -0700410 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
joshualitt2c93efe2014-11-06 12:57:13 -0800411
csmartdaltond211e782016-08-15 11:17:19 -0700412 // FIXME: Use path bounds instead of this WAR once
413 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
414 SkRect bounds = SkRect::MakeIWH(drawContext->width(), drawContext->height());
415
joshualitt2c93efe2014-11-06 12:57:13 -0800416 // Setup clip
csmartdaltond211e782016-08-15 11:17:19 -0700417 GrAppliedClip appliedClip(bounds);
418 if (!clip.apply(fContext, drawContext, useHWAA, true, &appliedClip)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800419 return;
420 }
cdalton862cff32016-05-12 15:09:48 -0700421 // TODO: respect fClipBatchToBounds if we ever start computing bounds here.
joshualitt2c93efe2014-11-06 12:57:13 -0800422
cdalton846c0512016-05-13 10:25:00 -0700423 // Coverage AA does not make sense when rendering to the stencil buffer. The caller should never
424 // attempt this in a situation that would require coverage AA.
csmartdaltond211e782016-08-15 11:17:19 -0700425 SkASSERT(!appliedClip.clipCoverageFragmentProcessor());
bsalomon0ba8c242015-10-07 09:20:28 -0700426
robertphillips55fdccc2016-06-06 06:16:20 -0700427 GrStencilAttachment* stencilAttachment = fResourceProvider->attachStencilAttachment(
428 drawContext->accessRenderTarget());
cdalton17bf8202016-05-13 11:27:15 -0700429 if (!stencilAttachment) {
430 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
431 return;
432 }
joshualitt2c93efe2014-11-06 12:57:13 -0800433
joshualittf2384692015-09-10 11:00:51 -0700434 GrBatch* batch = GrStencilPathBatch::Create(viewMatrix,
robertphillips59cf61a2016-07-13 09:18:21 -0700435 useHWAA,
436 path->getFillType(),
cdalton862cff32016-05-12 15:09:48 -0700437 appliedClip.hasStencilClip(),
cdalton93a379b2016-05-11 13:58:08 -0700438 stencilAttachment->bits(),
cdalton862cff32016-05-12 15:09:48 -0700439 appliedClip.scissorState(),
robertphillips55fdccc2016-06-06 06:16:20 -0700440 drawContext->accessRenderTarget(),
bsalomona44919e2015-08-18 13:28:19 -0700441 path);
csmartdaltond211e782016-08-15 11:17:19 -0700442 this->recordBatch(batch, appliedClip.clippedDrawBounds());
bsalomona44919e2015-08-18 13:28:19 -0700443 batch->unref();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000444}
445
robertphillips9199a9f2016-07-13 07:48:43 -0700446void GrDrawTarget::addBatch(sk_sp<GrBatch> batch) {
447 this->recordBatch(batch.get(), batch->bounds());
bsalomon53469832015-08-18 09:20:09 -0700448}
449
bsalomon9f129de2016-08-10 16:31:05 -0700450void GrDrawTarget::fullClear(GrRenderTarget* renderTarget, GrColor color) {
bsalomonfd8d0132016-08-11 11:25:33 -0700451 // Currently this just inserts or updates the last clear batch. However, once in MDB this can
452 // remove all the previously recorded batches and change the load op to clear with supplied
453 // color.
454 if (fLastFullClearBatch &&
robertphillips8abb3702016-08-31 14:04:06 -0700455 fLastFullClearBatch->renderTargetUniqueID() == renderTarget->uniqueID()) {
bsalomonfd8d0132016-08-11 11:25:33 -0700456 // As currently implemented, fLastFullClearBatch should be the last batch because we would
457 // have cleared it when another batch was recorded.
458 SkASSERT(fRecordedBatches.back().fBatch.get() == fLastFullClearBatch);
459 fLastFullClearBatch->setColor(color);
460 return;
461 }
csmartdalton29df7602016-08-31 11:55:52 -0700462 sk_sp<GrClearBatch> batch(GrClearBatch::Make(GrFixedClip::Disabled(), color, renderTarget));
bsalomonfd8d0132016-08-11 11:25:33 -0700463 if (batch.get() == this->recordBatch(batch.get(), batch->bounds())) {
464 fLastFullClearBatch = batch.get();
465 }
bsalomon9f129de2016-08-10 16:31:05 -0700466}
467
bsalomon53469832015-08-18 09:20:09 -0700468void GrDrawTarget::discard(GrRenderTarget* renderTarget) {
bsalomon9f129de2016-08-10 16:31:05 -0700469 // Currently this just inserts a discard batch. However, once in MDB this can remove all the
470 // previously recorded batches and change the load op to discard.
bsalomon53469832015-08-18 09:20:09 -0700471 if (this->caps()->discardRenderTargetSupport()) {
halcanary385fe4d2015-08-26 13:07:48 -0700472 GrBatch* batch = new GrDiscardBatch(renderTarget);
bsalomon6cc90062016-07-08 11:31:22 -0700473 this->recordBatch(batch, batch->bounds());
bsalomon53469832015-08-18 09:20:09 -0700474 batch->unref();
bsalomon63b21962014-11-05 07:05:34 -0800475 }
476}
477
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000478////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000479
bsalomonb8fea972016-02-16 07:34:17 -0800480bool GrDrawTarget::copySurface(GrSurface* dst,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000481 GrSurface* src,
482 const SkIRect& srcRect,
483 const SkIPoint& dstPoint) {
bsalomon872062c2015-08-18 12:12:35 -0700484 GrBatch* batch = GrCopySurfaceBatch::Create(dst, src, srcRect, dstPoint);
bsalomonb8fea972016-02-16 07:34:17 -0800485 if (!batch) {
486 return false;
487 }
robertphillips498d7ac2015-10-30 10:11:30 -0700488#ifdef ENABLE_MDB
bsalomonb8fea972016-02-16 07:34:17 -0800489 this->addDependency(src);
robertphillips498d7ac2015-10-30 10:11:30 -0700490#endif
491
bsalomon6cc90062016-07-08 11:31:22 -0700492 this->recordBatch(batch, batch->bounds());
bsalomonb8fea972016-02-16 07:34:17 -0800493 batch->unref();
494 return true;
bsalomon@google.comeb851172013-04-15 13:51:00 +0000495}
496
bsalomon6cc90062016-07-08 11:31:22 -0700497static inline bool can_reorder(const SkRect& a, const SkRect& b) {
bsalomon88cf17d2016-07-08 06:40:56 -0700498 return a.fRight <= b.fLeft || a.fBottom <= b.fTop ||
499 b.fRight <= a.fLeft || b.fBottom <= a.fTop;
500}
501
bsalomon6cc90062016-07-08 11:31:22 -0700502static void join(SkRect* out, const SkRect& a, const SkRect& b) {
503 SkASSERT(a.fLeft <= a.fRight && a.fTop <= a.fBottom);
504 SkASSERT(b.fLeft <= b.fRight && b.fTop <= b.fBottom);
505 out->fLeft = SkTMin(a.fLeft, b.fLeft);
506 out->fTop = SkTMin(a.fTop, b.fTop);
507 out->fRight = SkTMax(a.fRight, b.fRight);
508 out->fBottom = SkTMax(a.fBottom, b.fBottom);
bsalomon512be532015-09-10 10:42:55 -0700509}
510
bsalomonfd8d0132016-08-11 11:25:33 -0700511GrBatch* GrDrawTarget::recordBatch(GrBatch* batch, const SkRect& clippedBounds) {
robertphillipsa106c622015-10-16 09:07:06 -0700512 // A closed drawTarget should never receive new/more batches
robertphillips6a186652015-10-20 07:37:58 -0700513 SkASSERT(!this->isClosed());
robertphillipsa106c622015-10-16 09:07:06 -0700514
bsalomon512be532015-09-10 10:42:55 -0700515 // Check if there is a Batch Draw we can batch with by linearly searching back until we either
516 // 1) check every draw
517 // 2) intersect with something
518 // 3) find a 'blocker'
joshualittb0666ad2016-03-08 10:43:41 -0800519 GR_AUDIT_TRAIL_ADDBATCH(fAuditTrail, batch);
bsalomon512be532015-09-10 10:42:55 -0700520 GrBATCH_INFO("Re-Recording (%s, B%u)\n"
joshualitte2bcec32015-09-30 06:22:22 -0700521 "\tBounds LRTB (%f, %f, %f, %f)\n",
bsalomon512be532015-09-10 10:42:55 -0700522 batch->name(),
523 batch->uniqueID(),
524 batch->bounds().fLeft, batch->bounds().fRight,
525 batch->bounds().fTop, batch->bounds().fBottom);
526 GrBATCH_INFO(SkTabString(batch->dumpInfo(), 1).c_str());
bsalomon6cc90062016-07-08 11:31:22 -0700527 GrBATCH_INFO("\tClipped Bounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n",
528 clippedBounds.fLeft, clippedBounds.fTop, clippedBounds.fRight,
529 clippedBounds.fBottom);
halcanary9d524f22016-03-29 09:03:52 -0700530 GrBATCH_INFO("\tOutcome:\n");
bsalomon6cc90062016-07-08 11:31:22 -0700531 int maxCandidates = SkTMin(fMaxBatchLookback, fRecordedBatches.count());
bsalomon512be532015-09-10 10:42:55 -0700532 if (maxCandidates) {
533 int i = 0;
534 while (true) {
bsalomon6cc90062016-07-08 11:31:22 -0700535 GrBatch* candidate = fRecordedBatches.fromBack(i).fBatch.get();
bsalomon512be532015-09-10 10:42:55 -0700536 // We cannot continue to search backwards if the render target changes
537 if (candidate->renderTargetUniqueID() != batch->renderTargetUniqueID()) {
538 GrBATCH_INFO("\t\tBreaking because of (%s, B%u) Rendertarget\n",
539 candidate->name(), candidate->uniqueID());
540 break;
541 }
542 if (candidate->combineIfPossible(batch, *this->caps())) {
543 GrBATCH_INFO("\t\tCombining with (%s, B%u)\n", candidate->name(),
544 candidate->uniqueID());
joshualittb0666ad2016-03-08 10:43:41 -0800545 GR_AUDIT_TRAIL_BATCHING_RESULT_COMBINED(fAuditTrail, candidate, batch);
bsalomon6cc90062016-07-08 11:31:22 -0700546 join(&fRecordedBatches.fromBack(i).fClippedBounds,
547 fRecordedBatches.fromBack(i).fClippedBounds, clippedBounds);
bsalomonfd8d0132016-08-11 11:25:33 -0700548 return candidate;
bsalomon512be532015-09-10 10:42:55 -0700549 }
550 // Stop going backwards if we would cause a painter's order violation.
bsalomon6cc90062016-07-08 11:31:22 -0700551 const SkRect& candidateBounds = fRecordedBatches.fromBack(i).fClippedBounds;
552 if (!can_reorder(candidateBounds, clippedBounds)) {
bsalomon512be532015-09-10 10:42:55 -0700553 GrBATCH_INFO("\t\tIntersects with (%s, B%u)\n", candidate->name(),
554 candidate->uniqueID());
555 break;
556 }
557 ++i;
558 if (i == maxCandidates) {
559 GrBATCH_INFO("\t\tReached max lookback or beginning of batch array %d\n", i);
560 break;
561 }
562 }
563 } else {
564 GrBATCH_INFO("\t\tFirstBatch\n");
565 }
joshualitt18d6b752016-02-26 08:07:50 -0800566 GR_AUDIT_TRAIL_BATCHING_RESULT_NEW(fAuditTrail, batch);
bsalomon6cc90062016-07-08 11:31:22 -0700567 fRecordedBatches.emplace_back(RecordedBatch{sk_ref_sp(batch), clippedBounds});
bsalomonfd8d0132016-08-11 11:25:33 -0700568 fLastFullClearBatch = nullptr;
569 return batch;
bsalomon512be532015-09-10 10:42:55 -0700570}
571
bsalomonaecc0182016-03-07 11:50:44 -0800572void GrDrawTarget::forwardCombine() {
bsalomondb27fc52016-08-29 12:43:27 -0700573 if (fMaxBatchLookahead <= 0) {
574 return;
575 }
bsalomon6cc90062016-07-08 11:31:22 -0700576 for (int i = 0; i < fRecordedBatches.count() - 2; ++i) {
577 GrBatch* batch = fRecordedBatches[i].fBatch.get();
578 const SkRect& batchBounds = fRecordedBatches[i].fClippedBounds;
579 int maxCandidateIdx = SkTMin(i + fMaxBatchLookahead, fRecordedBatches.count() - 1);
bsalomonaecc0182016-03-07 11:50:44 -0800580 int j = i + 1;
581 while (true) {
bsalomon6cc90062016-07-08 11:31:22 -0700582 GrBatch* candidate = fRecordedBatches[j].fBatch.get();
bsalomonaecc0182016-03-07 11:50:44 -0800583 // We cannot continue to search if the render target changes
584 if (candidate->renderTargetUniqueID() != batch->renderTargetUniqueID()) {
585 GrBATCH_INFO("\t\tBreaking because of (%s, B%u) Rendertarget\n",
586 candidate->name(), candidate->uniqueID());
587 break;
588 }
589 if (j == i +1) {
590 // We assume batch would have combined with candidate when the candidate was added
591 // via backwards combining in recordBatch.
592 SkASSERT(!batch->combineIfPossible(candidate, *this->caps()));
593 } else if (batch->combineIfPossible(candidate, *this->caps())) {
594 GrBATCH_INFO("\t\tCombining with (%s, B%u)\n", candidate->name(),
595 candidate->uniqueID());
joshualittb0666ad2016-03-08 10:43:41 -0800596 GR_AUDIT_TRAIL_BATCHING_RESULT_COMBINED(fAuditTrail, batch, candidate);
bsalomon6cc90062016-07-08 11:31:22 -0700597 fRecordedBatches[j].fBatch = std::move(fRecordedBatches[i].fBatch);
598 join(&fRecordedBatches[j].fClippedBounds, fRecordedBatches[j].fClippedBounds,
599 batchBounds);
bsalomonaecc0182016-03-07 11:50:44 -0800600 break;
601 }
602 // Stop going traversing if we would cause a painter's order violation.
bsalomon6cc90062016-07-08 11:31:22 -0700603 const SkRect& candidateBounds = fRecordedBatches[j].fClippedBounds;
604 if (!can_reorder(candidateBounds, batchBounds)) {
bsalomonaecc0182016-03-07 11:50:44 -0800605 GrBATCH_INFO("\t\tIntersects with (%s, B%u)\n", candidate->name(),
606 candidate->uniqueID());
607 break;
608 }
609 ++j;
610 if (j > maxCandidateIdx) {
611 GrBATCH_INFO("\t\tReached max lookahead or end of batch array %d\n", i);
612 break;
613 }
614 }
615 }
616}
617
egdaniele36914c2015-02-13 09:00:33 -0800618///////////////////////////////////////////////////////////////////////////////
619
csmartdalton29df7602016-08-31 11:55:52 -0700620void GrDrawTarget::clearStencilClip(const GrFixedClip& clip,
621 bool insideStencilMask,
622 GrRenderTarget* rt) {
623 GrBatch* batch = new GrClearStencilClipBatch(clip, insideStencilMask, rt);
bsalomon6cc90062016-07-08 11:31:22 -0700624 this->recordBatch(batch, batch->bounds());
bsalomon5ea03632015-08-18 10:33:30 -0700625 batch->unref();
626}