blob: 717cd5d1bb9f109425730cc4e4df6e109347f10c [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
robertphillips9199a9f2016-07-13 07:48:43 -070029#include "batches/GrClearStencilClipBatch.h"
bsalomon872062c2015-08-18 12:12:35 -070030#include "batches/GrCopySurfaceBatch.h"
bsalomon53469832015-08-18 09:20:09 -070031#include "batches/GrDiscardBatch.h"
bsalomon16b99132015-08-13 14:55:50 -070032#include "batches/GrDrawBatch.h"
bsalomonadd79ef2015-08-19 13:26:49 -070033#include "batches/GrDrawPathBatch.h"
joshualittecd1a692015-08-10 10:08:26 -070034#include "batches/GrRectBatchFactory.h"
bsalomona44919e2015-08-18 13:28:19 -070035#include "batches/GrStencilPathBatch.h"
joshualitt74417822015-08-07 11:42:16 -070036
csmartdaltona7f29642016-07-07 08:49:11 -070037#include "instanced/InstancedRendering.h"
38
reed@google.comac10a2d2010-12-22 21:39:39 +000039////////////////////////////////////////////////////////////////////////////////
40
bsalomon489147c2015-12-14 12:13:09 -080041// Experimentally we have found that most batching occurs within the first 10 comparisons.
bsalomonaecc0182016-03-07 11:50:44 -080042static const int kDefaultMaxBatchLookback = 10;
43static const int kDefaultMaxBatchLookahead = 10;
bsalomon489147c2015-12-14 12:13:09 -080044
bsalomon69cfe952015-11-30 13:27:47 -080045GrDrawTarget::GrDrawTarget(GrRenderTarget* rt, GrGpu* gpu, GrResourceProvider* resourceProvider,
joshualitt086cee12016-01-12 06:45:24 -080046 GrAuditTrail* auditTrail, const Options& options)
bsalomon4061b122015-05-29 10:26:19 -070047 : fGpu(SkRef(gpu))
bsalomon4061b122015-05-29 10:26:19 -070048 , fResourceProvider(resourceProvider)
joshualitt086cee12016-01-12 06:45:24 -080049 , fAuditTrail(auditTrail)
bsalomon648c6962015-10-23 09:06:59 -070050 , fFlags(0)
csmartdaltona7f29642016-07-07 08:49:11 -070051 , fRenderTarget(rt)
52 , fInstancedRendering(fGpu->createInstancedRenderingIfSupported()) {
bsalomonb3b9aec2015-09-10 11:16:35 -070053 // TODO: Stop extracting the context (currently needed by GrClipMaskManager)
54 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
robertphillips0dfa62c2015-11-16 06:23:31 -080063 rt->setLastDrawTarget(this);
64
robertphillips4beb5c12015-10-20 07:50:00 -070065#ifdef SK_DEBUG
66 static int debugID = 0;
67 fDebugID = debugID++;
68#endif
bsalomon4061b122015-05-29 10:26:19 -070069}
70
71GrDrawTarget::~GrDrawTarget() {
robertphillips498d7ac2015-10-30 10:11:30 -070072 if (fRenderTarget && this == fRenderTarget->getLastDrawTarget()) {
73 fRenderTarget->setLastDrawTarget(nullptr);
74 }
75
bsalomon4061b122015-05-29 10:26:19 -070076 fGpu->unref();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000077}
78
79////////////////////////////////////////////////////////////////////////////////
80
robertphillips6a186652015-10-20 07:37:58 -070081// Add a GrDrawTarget-based dependency
82void GrDrawTarget::addDependency(GrDrawTarget* dependedOn) {
83 SkASSERT(!dependedOn->dependsOn(this)); // loops are bad
84
85 if (this->dependsOn(dependedOn)) {
86 return; // don't add duplicate dependencies
87 }
88
89 *fDependencies.push() = dependedOn;
90}
91
92// Convert from a GrSurface-based dependency to a GrDrawTarget one
93void GrDrawTarget::addDependency(GrSurface* dependedOn) {
94 if (dependedOn->asRenderTarget() && dependedOn->asRenderTarget()->getLastDrawTarget()) {
95 // If it is still receiving dependencies, this DT shouldn't be closed
96 SkASSERT(!this->isClosed());
97
98 GrDrawTarget* dt = dependedOn->asRenderTarget()->getLastDrawTarget();
99 if (dt == this) {
100 // self-read - presumably for dst reads
101 } else {
102 this->addDependency(dt);
103
104 // Can't make it closed in the self-read case
105 dt->makeClosed();
106 }
107 }
108}
109
robertphillips4beb5c12015-10-20 07:50:00 -0700110#ifdef SK_DEBUG
111void GrDrawTarget::dump() const {
112 SkDebugf("--------------------------------------------------------------\n");
robertphillipse004bfc2015-11-16 09:06:59 -0800113 SkDebugf("node: %d -> RT: %d\n", fDebugID, fRenderTarget ? fRenderTarget->getUniqueID() : -1);
robertphillips4beb5c12015-10-20 07:50:00 -0700114 SkDebugf("relies On (%d): ", fDependencies.count());
115 for (int i = 0; i < fDependencies.count(); ++i) {
116 SkDebugf("%d, ", fDependencies[i]->fDebugID);
117 }
118 SkDebugf("\n");
bsalomon6cc90062016-07-08 11:31:22 -0700119 SkDebugf("batches (%d):\n", fRecordedBatches.count());
120 for (int i = 0; i < fRecordedBatches.count(); ++i) {
robertphillips4beb5c12015-10-20 07:50:00 -0700121 SkDebugf("*******************************\n");
bsalomon6cc90062016-07-08 11:31:22 -0700122 if (!fRecordedBatches[i].fBatch) {
bsalomonaecc0182016-03-07 11:50:44 -0800123 SkDebugf("%d: <combined forward>\n", i);
124 } else {
bsalomon6cc90062016-07-08 11:31:22 -0700125 SkDebugf("%d: %s\n", i, fRecordedBatches[i].fBatch->name());
126 SkString str = fRecordedBatches[i].fBatch->dumpInfo();
bsalomonaecc0182016-03-07 11:50:44 -0800127 SkDebugf("%s\n", str.c_str());
bsalomon6cc90062016-07-08 11:31:22 -0700128 const SkRect& clippedBounds = fRecordedBatches[i].fClippedBounds;
129 SkDebugf("ClippedBounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n",
130 clippedBounds.fLeft, clippedBounds.fTop, clippedBounds.fRight,
131 clippedBounds.fBottom);
bsalomonaecc0182016-03-07 11:50:44 -0800132 }
robertphillips4beb5c12015-10-20 07:50:00 -0700133 }
134}
135#endif
136
bsalomon50785a32015-02-06 07:02:37 -0800137bool GrDrawTarget::setupDstReadIfNecessary(const GrPipelineBuilder& pipelineBuilder,
robertphillips55fdccc2016-06-06 06:16:20 -0700138 GrRenderTarget* rt,
cdalton862cff32016-05-12 15:09:48 -0700139 const GrClip& clip,
ethannicholasde4166a2015-11-30 08:57:38 -0800140 const GrPipelineOptimizations& optimizations,
bsalomon6a44c6a2015-05-26 09:49:05 -0700141 GrXferProcessor::DstTexture* dstTexture,
bsalomonad792c12015-09-10 11:10:50 -0700142 const SkRect& batchBounds) {
143 SkRect bounds = batchBounds;
144 bounds.outset(0.5f, 0.5f);
145
ethannicholasde4166a2015-11-30 08:57:38 -0800146 if (!pipelineBuilder.willXPNeedDstTexture(*this->caps(), optimizations)) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000147 return true;
148 }
cdalton9954bc32015-04-29 14:17:00 -0700149
cdalton9954bc32015-04-29 14:17:00 -0700150 if (this->caps()->textureBarrierSupport()) {
151 if (GrTexture* rtTex = rt->asTexture()) {
bsalomondc47ff72015-05-26 12:16:59 -0700152 // The render target is a texture, so we can read from it directly in the shader. The XP
cdalton9954bc32015-04-29 14:17:00 -0700153 // will be responsible to detect this situation and request a texture barrier.
bsalomon6a44c6a2015-05-26 09:49:05 -0700154 dstTexture->setTexture(rtTex);
155 dstTexture->setOffset(0, 0);
cdalton9954bc32015-04-29 14:17:00 -0700156 return true;
157 }
158 }
159
160 SkIRect copyRect;
cdalton862cff32016-05-12 15:09:48 -0700161 clip.getConservativeBounds(rt->width(), rt->height(), &copyRect);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000162
bsalomonad792c12015-09-10 11:10:50 -0700163 SkIRect drawIBounds;
164 bounds.roundOut(&drawIBounds);
165 if (!copyRect.intersect(drawIBounds)) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000166#ifdef SK_DEBUG
bsalomonb3b9aec2015-09-10 11:16:35 -0700167 GrCapsDebugf(this->caps(), "Missed an early reject. "
168 "Bailing on draw from setupDstReadIfNecessary.\n");
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000169#endif
bsalomonad792c12015-09-10 11:10:50 -0700170 return false;
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000171 }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000172
commit-bot@chromium.org63150af2013-04-11 22:00:22 +0000173 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
174 // have per-sample dst values by making the copy multisampled.
bsalomonf2703d82014-10-28 14:33:06 -0700175 GrSurfaceDesc desc;
bsalomonb3b9aec2015-09-10 11:16:35 -0700176 if (!fGpu->initCopySurfaceDstDesc(rt, &desc)) {
bsalomona73239a2015-04-28 13:35:17 -0700177 desc.fOrigin = kDefault_GrSurfaceOrigin;
178 desc.fFlags = kRenderTarget_GrSurfaceFlag;
179 desc.fConfig = rt->config();
180 }
181
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000182 desc.fWidth = copyRect.width();
183 desc.fHeight = copyRect.height();
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000184
bsalomoneae62002015-07-31 13:59:30 -0700185 static const uint32_t kFlags = 0;
186 SkAutoTUnref<GrTexture> copy(fResourceProvider->createApproxTexture(desc, kFlags));
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000187
bsalomone3059732014-10-14 11:47:22 -0700188 if (!copy) {
tfarina38406c82014-10-31 07:11:12 -0700189 SkDebugf("Failed to create temporary copy of destination texture.\n");
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000190 return false;
191 }
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000192 SkIPoint dstPoint = {0, 0};
bsalomon6df86402015-06-01 10:41:49 -0700193 this->copySurface(copy, rt, copyRect, dstPoint);
194 dstTexture->setTexture(copy);
195 dstTexture->setOffset(copyRect.fLeft, copyRect.fTop);
196 return true;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000197}
198
robertphillipsa13e2022015-11-11 12:01:09 -0800199void GrDrawTarget::prepareBatches(GrBatchFlushState* flushState) {
robertphillipsa106c622015-10-16 09:07:06 -0700200 // Semi-usually the drawTargets are already closed at this point, but sometimes Ganesh
201 // needs to flush mid-draw. In that case, the SkGpuDevice's drawTargets won't be closed
202 // but need to be flushed anyway. Closing such drawTargets here will mean new
203 // drawTargets will be created to replace them if the SkGpuDevice(s) write to them again.
204 this->makeClosed();
205
robertphillips498d7ac2015-10-30 10:11:30 -0700206 // Loop over the batches that haven't yet generated their geometry
bsalomon6cc90062016-07-08 11:31:22 -0700207 for (int i = 0; i < fRecordedBatches.count(); ++i) {
208 if (fRecordedBatches[i].fBatch) {
209 fRecordedBatches[i].fBatch->prepare(flushState);
bsalomonaecc0182016-03-07 11:50:44 -0800210 }
bsalomon512be532015-09-10 10:42:55 -0700211 }
csmartdaltona7f29642016-07-07 08:49:11 -0700212
213 if (fInstancedRendering) {
214 fInstancedRendering->beginFlush(flushState->resourceProvider());
215 }
robertphillipsa13e2022015-11-11 12:01:09 -0800216}
bsalomon512be532015-09-10 10:42:55 -0700217
robertphillipsa13e2022015-11-11 12:01:09 -0800218void GrDrawTarget::drawBatches(GrBatchFlushState* flushState) {
egdanielb4021cf2016-07-28 08:53:07 -0700219 if (0 == fRecordedBatches.count()) {
220 return;
221 }
bsalomon512be532015-09-10 10:42:55 -0700222 // Draw all the generated geometry.
bsalomon6dea83f2015-12-03 12:58:06 -0800223 SkRandom random;
egdaniel9cb63402016-06-23 08:37:05 -0700224 GrRenderTarget* currentRT = nullptr;
225 SkAutoTDelete<GrGpuCommandBuffer> commandBuffer;
egdaniele7d1b242016-07-01 08:06:45 -0700226 SkRect bounds = SkRect::MakeEmpty();
bsalomon6cc90062016-07-08 11:31:22 -0700227 for (int i = 0; i < fRecordedBatches.count(); ++i) {
228 if (!fRecordedBatches[i].fBatch) {
bsalomonaecc0182016-03-07 11:50:44 -0800229 continue;
230 }
bsalomon6cc90062016-07-08 11:31:22 -0700231 if (fRecordedBatches[i].fBatch->renderTarget() != currentRT) {
egdaniel9cb63402016-06-23 08:37:05 -0700232 if (commandBuffer) {
233 commandBuffer->end();
egdaniele7d1b242016-07-01 08:06:45 -0700234 if (bounds.intersect(0, 0,
235 SkIntToScalar(currentRT->width()),
236 SkIntToScalar(currentRT->height()))) {
237 SkIRect iBounds;
238 bounds.roundOut(&iBounds);
239 commandBuffer->submit(iBounds);
240 }
egdaniel9cb63402016-06-23 08:37:05 -0700241 commandBuffer.reset();
242 }
egdaniele7d1b242016-07-01 08:06:45 -0700243 bounds.setEmpty();
bsalomon6cc90062016-07-08 11:31:22 -0700244 currentRT = fRecordedBatches[i].fBatch->renderTarget();
egdaniel9cb63402016-06-23 08:37:05 -0700245 if (currentRT) {
246 static const GrGpuCommandBuffer::LoadAndStoreInfo kBasicLoadStoreInfo
247 { GrGpuCommandBuffer::LoadOp::kLoad,GrGpuCommandBuffer::StoreOp::kStore,
248 GrColor_ILLEGAL };
249 commandBuffer.reset(fGpu->createCommandBuffer(currentRT,
250 kBasicLoadStoreInfo, // Color
251 kBasicLoadStoreInfo)); // Stencil
252 }
253 flushState->setCommandBuffer(commandBuffer);
254 }
egdaniele7d1b242016-07-01 08:06:45 -0700255 if (commandBuffer) {
bsalomon6cc90062016-07-08 11:31:22 -0700256 bounds.join(fRecordedBatches[i].fClippedBounds);
egdaniele7d1b242016-07-01 08:06:45 -0700257 }
bsalomon6dea83f2015-12-03 12:58:06 -0800258 if (fDrawBatchBounds) {
bsalomon6cc90062016-07-08 11:31:22 -0700259 const SkRect& bounds = fRecordedBatches[i].fClippedBounds;
260 SkIRect ibounds;
261 bounds.roundOut(&ibounds);
bsalomon6dea83f2015-12-03 12:58:06 -0800262 // In multi-draw buffer all the batches use the same render target and we won't need to
263 // get the batchs bounds.
bsalomon6cc90062016-07-08 11:31:22 -0700264 if (GrRenderTarget* rt = fRecordedBatches[i].fBatch->renderTarget()) {
265 fGpu->drawDebugWireRect(rt, ibounds, 0xFF000000 | random.nextU());
bsalomon6dea83f2015-12-03 12:58:06 -0800266 }
267 }
bsalomon6cc90062016-07-08 11:31:22 -0700268 fRecordedBatches[i].fBatch->draw(flushState);
bsalomon512be532015-09-10 10:42:55 -0700269 }
egdaniel9cb63402016-06-23 08:37:05 -0700270 if (commandBuffer) {
271 commandBuffer->end();
egdaniele7d1b242016-07-01 08:06:45 -0700272 if (bounds.intersect(0, 0,
273 SkIntToScalar(currentRT->width()),
274 SkIntToScalar(currentRT->height()))) {
275 SkIRect iBounds;
276 bounds.roundOut(&iBounds);
277 commandBuffer->submit(iBounds);
278 }
egdaniel9cb63402016-06-23 08:37:05 -0700279 flushState->setCommandBuffer(nullptr);
280 }
ethannicholas22793252016-01-30 09:59:10 -0800281
jvanverthd2d2eb92016-02-17 14:04:46 -0800282 fGpu->finishDrawTarget();
bsalomona73239a2015-04-28 13:35:17 -0700283}
284
bsalomon512be532015-09-10 10:42:55 -0700285void GrDrawTarget::reset() {
bsalomon6cc90062016-07-08 11:31:22 -0700286 fRecordedBatches.reset();
csmartdaltona7f29642016-07-07 08:49:11 -0700287 if (fInstancedRendering) {
288 fInstancedRendering->endFlush();
289 }
bsalomon512be532015-09-10 10:42:55 -0700290}
291
bsalomon88cf17d2016-07-08 06:40:56 -0700292static void batch_bounds(SkRect* bounds, const GrBatch* batch) {
293 *bounds = batch->bounds();
294 if (batch->hasZeroArea()) {
295 if (batch->hasAABloat()) {
296 bounds->outset(0.5f, 0.5f);
297 } else {
298 // We don't know which way the particular GPU will snap lines or points at integer
299 // coords. So we ensure that the bounds is large enough for either snap.
300 SkRect before = *bounds;
301 bounds->roundOut(bounds);
302 if (bounds->fLeft == before.fLeft) {
303 bounds->fLeft -= 1;
304 }
305 if (bounds->fTop == before.fTop) {
306 bounds->fTop -= 1;
307 }
308 if (bounds->fRight == before.fRight) {
309 bounds->fRight += 1;
310 }
311 if (bounds->fBottom == before.fBottom) {
312 bounds->fBottom += 1;
313 }
314 }
315 }
316}
317
bsalomon6cc90062016-07-08 11:31:22 -0700318static inline bool intersect(SkRect* out, const SkRect& a, const SkRect& b) {
319 SkASSERT(a.fLeft <= a.fRight && a.fTop <= a.fBottom);
320 SkASSERT(b.fLeft <= b.fRight && b.fTop <= b.fBottom);
321 out->fLeft = SkTMax(a.fLeft, b.fLeft);
322 out->fTop = SkTMax(a.fTop, b.fTop);
323 out->fRight = SkTMin(a.fRight, b.fRight);
324 out->fBottom = SkTMin(a.fBottom, b.fBottom);
325 return (out->fLeft <= out->fRight && out->fTop <= out->fBottom);
326}
327
robertphillips391395d2016-03-02 09:26:36 -0800328void GrDrawTarget::drawBatch(const GrPipelineBuilder& pipelineBuilder,
robertphillips976f5f02016-06-03 10:59:20 -0700329 GrDrawContext* drawContext,
cdalton862cff32016-05-12 15:09:48 -0700330 const GrClip& clip,
331 GrDrawBatch* batch) {
joshualitt4d8da812015-01-28 12:53:54 -0800332 // Setup clip
cdalton862cff32016-05-12 15:09:48 -0700333 GrAppliedClip appliedClip;
bsalomon88cf17d2016-07-08 06:40:56 -0700334 SkRect bounds;
335 batch_bounds(&bounds, batch);
robertphillips59cf61a2016-07-13 09:18:21 -0700336 if (!clip.apply(fContext, drawContext, &bounds,
337 pipelineBuilder.isHWAntialias(), pipelineBuilder.hasUserStencilSettings(),
338 &appliedClip)) {
cdalton862cff32016-05-12 15:09:48 -0700339 return;
joshualitt4d8da812015-01-28 12:53:54 -0800340 }
robertphillips391395d2016-03-02 09:26:36 -0800341
robertphillips55fdccc2016-06-06 06:16:20 -0700342 // TODO: this is the only remaining usage of the AutoRestoreFragmentProcessorState - remove it
cdaltond4727922015-11-10 12:49:06 -0800343 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
bungeman06ca8ec2016-06-09 08:01:03 -0700344 if (appliedClip.getClipCoverageFragmentProcessor()) {
cdaltond4727922015-11-10 12:49:06 -0800345 arfps.set(&pipelineBuilder);
bungeman06ca8ec2016-06-09 08:01:03 -0700346 arfps.addCoverageFragmentProcessor(sk_ref_sp(appliedClip.getClipCoverageFragmentProcessor()));
cdaltond4727922015-11-10 12:49:06 -0800347 }
joshualitt4d8da812015-01-28 12:53:54 -0800348
bsalomonad792c12015-09-10 11:10:50 -0700349 GrPipeline::CreateArgs args;
cdalton193d9cf2016-05-12 11:52:02 -0700350 args.fPipelineBuilder = &pipelineBuilder;
robertphillips55fdccc2016-06-06 06:16:20 -0700351 args.fDrawContext = drawContext;
cdalton193d9cf2016-05-12 11:52:02 -0700352 args.fCaps = this->caps();
cdalton862cff32016-05-12 15:09:48 -0700353 args.fScissor = &appliedClip.scissorState();
354 args.fHasStencilClip = appliedClip.hasStencilClip();
355 if (pipelineBuilder.hasUserStencilSettings() || appliedClip.hasStencilClip()) {
robertphillips55fdccc2016-06-06 06:16:20 -0700356 if (!fResourceProvider->attachStencilAttachment(drawContext->accessRenderTarget())) {
cdalton17bf8202016-05-13 11:27:15 -0700357 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
358 return;
359 }
cdalton193d9cf2016-05-12 11:52:02 -0700360 }
361 batch->getPipelineOptimizations(&args.fOpts);
362 GrScissorState finalScissor;
cdalton862cff32016-05-12 15:09:48 -0700363 if (args.fOpts.fOverrides.fUsePLSDstRead || fClipBatchToBounds) {
cdalton193d9cf2016-05-12 11:52:02 -0700364 GrGLIRect viewport;
365 viewport.fLeft = 0;
366 viewport.fBottom = 0;
robertphillips55fdccc2016-06-06 06:16:20 -0700367 viewport.fWidth = drawContext->width();
368 viewport.fHeight = drawContext->height();
cdalton193d9cf2016-05-12 11:52:02 -0700369 SkIRect ibounds;
370 ibounds.fLeft = SkTPin(SkScalarFloorToInt(batch->bounds().fLeft), viewport.fLeft,
371 viewport.fWidth);
372 ibounds.fTop = SkTPin(SkScalarFloorToInt(batch->bounds().fTop), viewport.fBottom,
373 viewport.fHeight);
374 ibounds.fRight = SkTPin(SkScalarCeilToInt(batch->bounds().fRight), viewport.fLeft,
375 viewport.fWidth);
376 ibounds.fBottom = SkTPin(SkScalarCeilToInt(batch->bounds().fBottom), viewport.fBottom,
377 viewport.fHeight);
cdalton862cff32016-05-12 15:09:48 -0700378 if (appliedClip.scissorState().enabled()) {
379 const SkIRect& scissorRect = appliedClip.scissorState().rect();
cdalton193d9cf2016-05-12 11:52:02 -0700380 if (!ibounds.intersect(scissorRect)) {
cdalton862cff32016-05-12 15:09:48 -0700381 return;
cdalton193d9cf2016-05-12 11:52:02 -0700382 }
383 }
384 finalScissor.set(ibounds);
385 args.fScissor = &finalScissor;
386 }
bungeman06ca8ec2016-06-09 08:01:03 -0700387 args.fOpts.fColorPOI.completeCalculations(
388 sk_sp_address_as_pointer_address(pipelineBuilder.fColorFragmentProcessors.begin()),
389 pipelineBuilder.numColorFragmentProcessors());
cdalton193d9cf2016-05-12 11:52:02 -0700390 args.fOpts.fCoveragePOI.completeCalculations(
bungeman06ca8ec2016-06-09 08:01:03 -0700391 sk_sp_address_as_pointer_address(pipelineBuilder.fCoverageFragmentProcessors.begin()),
392 pipelineBuilder.numCoverageFragmentProcessors());
robertphillips55fdccc2016-06-06 06:16:20 -0700393 if (!this->setupDstReadIfNecessary(pipelineBuilder, drawContext->accessRenderTarget(),
394 clip, args.fOpts,
395 &args.fDstTexture, batch->bounds())) {
cdalton193d9cf2016-05-12 11:52:02 -0700396 return;
397 }
398
399 if (!batch->installPipeline(args)) {
egdaniele36914c2015-02-13 09:00:33 -0800400 return;
401 }
bsalomonad792c12015-09-10 11:10:50 -0700402
robertphillips498d7ac2015-10-30 10:11:30 -0700403#ifdef ENABLE_MDB
404 SkASSERT(fRenderTarget);
405 batch->pipeline()->addDependenciesTo(fRenderTarget);
406#endif
bsalomon6cc90062016-07-08 11:31:22 -0700407 SkRect clippedBounds;
408 SkAssertResult(intersect(&clippedBounds, bounds, appliedClip.deviceBounds()));
409 this->recordBatch(batch, clippedBounds);
joshualitt4d8da812015-01-28 12:53:54 -0800410}
411
robertphillips59cf61a2016-07-13 09:18:21 -0700412void GrDrawTarget::stencilPath(GrDrawContext* drawContext,
cdalton862cff32016-05-12 15:09:48 -0700413 const GrClip& clip,
robertphillips59cf61a2016-07-13 09:18:21 -0700414 const GrUserStencilSettings* ss,
415 bool useHWAA,
joshualittf2384692015-09-10 11:00:51 -0700416 const SkMatrix& viewMatrix,
robertphillips59cf61a2016-07-13 09:18:21 -0700417 const GrPath* path) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000418 // TODO: extract portions of checkDraw that are relevant to path stenciling.
bsalomon49f085d2014-09-05 13:34:00 -0700419 SkASSERT(path);
jvanverthe9c0fc62015-04-29 11:18:05 -0700420 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
joshualitt2c93efe2014-11-06 12:57:13 -0800421
422 // Setup clip
cdalton862cff32016-05-12 15:09:48 -0700423 GrAppliedClip appliedClip;
robertphillips59cf61a2016-07-13 09:18:21 -0700424 if (!clip.apply(fContext, drawContext, nullptr, useHWAA, SkToBool(ss), &appliedClip)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800425 return;
426 }
cdalton862cff32016-05-12 15:09:48 -0700427 // TODO: respect fClipBatchToBounds if we ever start computing bounds here.
joshualitt2c93efe2014-11-06 12:57:13 -0800428
cdalton846c0512016-05-13 10:25:00 -0700429 // Coverage AA does not make sense when rendering to the stencil buffer. The caller should never
430 // attempt this in a situation that would require coverage AA.
bungeman06ca8ec2016-06-09 08:01:03 -0700431 SkASSERT(!appliedClip.getClipCoverageFragmentProcessor());
bsalomon0ba8c242015-10-07 09:20:28 -0700432
robertphillips55fdccc2016-06-06 06:16:20 -0700433 GrStencilAttachment* stencilAttachment = fResourceProvider->attachStencilAttachment(
434 drawContext->accessRenderTarget());
cdalton17bf8202016-05-13 11:27:15 -0700435 if (!stencilAttachment) {
436 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
437 return;
438 }
joshualitt2c93efe2014-11-06 12:57:13 -0800439
joshualittf2384692015-09-10 11:00:51 -0700440 GrBatch* batch = GrStencilPathBatch::Create(viewMatrix,
robertphillips59cf61a2016-07-13 09:18:21 -0700441 useHWAA,
442 path->getFillType(),
cdalton862cff32016-05-12 15:09:48 -0700443 appliedClip.hasStencilClip(),
cdalton93a379b2016-05-11 13:58:08 -0700444 stencilAttachment->bits(),
cdalton862cff32016-05-12 15:09:48 -0700445 appliedClip.scissorState(),
robertphillips55fdccc2016-06-06 06:16:20 -0700446 drawContext->accessRenderTarget(),
bsalomona44919e2015-08-18 13:28:19 -0700447 path);
bsalomon6cc90062016-07-08 11:31:22 -0700448 this->recordBatch(batch, appliedClip.deviceBounds());
bsalomona44919e2015-08-18 13:28:19 -0700449 batch->unref();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000450}
451
robertphillips9199a9f2016-07-13 07:48:43 -0700452void GrDrawTarget::addBatch(sk_sp<GrBatch> batch) {
453 this->recordBatch(batch.get(), batch->bounds());
bsalomon53469832015-08-18 09:20:09 -0700454}
455
456void GrDrawTarget::discard(GrRenderTarget* renderTarget) {
457 if (this->caps()->discardRenderTargetSupport()) {
halcanary385fe4d2015-08-26 13:07:48 -0700458 GrBatch* batch = new GrDiscardBatch(renderTarget);
bsalomon6cc90062016-07-08 11:31:22 -0700459 this->recordBatch(batch, batch->bounds());
bsalomon53469832015-08-18 09:20:09 -0700460 batch->unref();
bsalomon63b21962014-11-05 07:05:34 -0800461 }
462}
463
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000464////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000465
bsalomonb8fea972016-02-16 07:34:17 -0800466bool GrDrawTarget::copySurface(GrSurface* dst,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000467 GrSurface* src,
468 const SkIRect& srcRect,
469 const SkIPoint& dstPoint) {
bsalomon872062c2015-08-18 12:12:35 -0700470 GrBatch* batch = GrCopySurfaceBatch::Create(dst, src, srcRect, dstPoint);
bsalomonb8fea972016-02-16 07:34:17 -0800471 if (!batch) {
472 return false;
473 }
robertphillips498d7ac2015-10-30 10:11:30 -0700474#ifdef ENABLE_MDB
bsalomonb8fea972016-02-16 07:34:17 -0800475 this->addDependency(src);
robertphillips498d7ac2015-10-30 10:11:30 -0700476#endif
477
bsalomon6cc90062016-07-08 11:31:22 -0700478 this->recordBatch(batch, batch->bounds());
bsalomonb8fea972016-02-16 07:34:17 -0800479 batch->unref();
480 return true;
bsalomon@google.comeb851172013-04-15 13:51:00 +0000481}
482
bsalomon6cc90062016-07-08 11:31:22 -0700483static inline bool can_reorder(const SkRect& a, const SkRect& b) {
bsalomon88cf17d2016-07-08 06:40:56 -0700484 return a.fRight <= b.fLeft || a.fBottom <= b.fTop ||
485 b.fRight <= a.fLeft || b.fBottom <= a.fTop;
486}
487
bsalomon6cc90062016-07-08 11:31:22 -0700488static void join(SkRect* out, const SkRect& a, const SkRect& b) {
489 SkASSERT(a.fLeft <= a.fRight && a.fTop <= a.fBottom);
490 SkASSERT(b.fLeft <= b.fRight && b.fTop <= b.fBottom);
491 out->fLeft = SkTMin(a.fLeft, b.fLeft);
492 out->fTop = SkTMin(a.fTop, b.fTop);
493 out->fRight = SkTMax(a.fRight, b.fRight);
494 out->fBottom = SkTMax(a.fBottom, b.fBottom);
bsalomon512be532015-09-10 10:42:55 -0700495}
496
bsalomon6cc90062016-07-08 11:31:22 -0700497void GrDrawTarget::recordBatch(GrBatch* batch, const SkRect& clippedBounds) {
robertphillipsa106c622015-10-16 09:07:06 -0700498 // A closed drawTarget should never receive new/more batches
robertphillips6a186652015-10-20 07:37:58 -0700499 SkASSERT(!this->isClosed());
robertphillipsa106c622015-10-16 09:07:06 -0700500
bsalomon512be532015-09-10 10:42:55 -0700501 // Check if there is a Batch Draw we can batch with by linearly searching back until we either
502 // 1) check every draw
503 // 2) intersect with something
504 // 3) find a 'blocker'
joshualittb0666ad2016-03-08 10:43:41 -0800505 GR_AUDIT_TRAIL_ADDBATCH(fAuditTrail, batch);
bsalomon512be532015-09-10 10:42:55 -0700506 GrBATCH_INFO("Re-Recording (%s, B%u)\n"
joshualitte2bcec32015-09-30 06:22:22 -0700507 "\tBounds LRTB (%f, %f, %f, %f)\n",
bsalomon512be532015-09-10 10:42:55 -0700508 batch->name(),
509 batch->uniqueID(),
510 batch->bounds().fLeft, batch->bounds().fRight,
511 batch->bounds().fTop, batch->bounds().fBottom);
512 GrBATCH_INFO(SkTabString(batch->dumpInfo(), 1).c_str());
bsalomon6cc90062016-07-08 11:31:22 -0700513 GrBATCH_INFO("\tClipped Bounds: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n",
514 clippedBounds.fLeft, clippedBounds.fTop, clippedBounds.fRight,
515 clippedBounds.fBottom);
halcanary9d524f22016-03-29 09:03:52 -0700516 GrBATCH_INFO("\tOutcome:\n");
bsalomon6cc90062016-07-08 11:31:22 -0700517 int maxCandidates = SkTMin(fMaxBatchLookback, fRecordedBatches.count());
bsalomon512be532015-09-10 10:42:55 -0700518 if (maxCandidates) {
519 int i = 0;
520 while (true) {
bsalomon6cc90062016-07-08 11:31:22 -0700521 GrBatch* candidate = fRecordedBatches.fromBack(i).fBatch.get();
bsalomon512be532015-09-10 10:42:55 -0700522 // We cannot continue to search backwards if the render target changes
523 if (candidate->renderTargetUniqueID() != batch->renderTargetUniqueID()) {
524 GrBATCH_INFO("\t\tBreaking because of (%s, B%u) Rendertarget\n",
525 candidate->name(), candidate->uniqueID());
526 break;
527 }
528 if (candidate->combineIfPossible(batch, *this->caps())) {
529 GrBATCH_INFO("\t\tCombining with (%s, B%u)\n", candidate->name(),
530 candidate->uniqueID());
joshualittb0666ad2016-03-08 10:43:41 -0800531 GR_AUDIT_TRAIL_BATCHING_RESULT_COMBINED(fAuditTrail, candidate, batch);
bsalomon6cc90062016-07-08 11:31:22 -0700532 join(&fRecordedBatches.fromBack(i).fClippedBounds,
533 fRecordedBatches.fromBack(i).fClippedBounds, clippedBounds);
bsalomon512be532015-09-10 10:42:55 -0700534 return;
535 }
536 // Stop going backwards if we would cause a painter's order violation.
bsalomon6cc90062016-07-08 11:31:22 -0700537 const SkRect& candidateBounds = fRecordedBatches.fromBack(i).fClippedBounds;
538 if (!can_reorder(candidateBounds, clippedBounds)) {
bsalomon512be532015-09-10 10:42:55 -0700539 GrBATCH_INFO("\t\tIntersects with (%s, B%u)\n", candidate->name(),
540 candidate->uniqueID());
541 break;
542 }
543 ++i;
544 if (i == maxCandidates) {
545 GrBATCH_INFO("\t\tReached max lookback or beginning of batch array %d\n", i);
546 break;
547 }
548 }
549 } else {
550 GrBATCH_INFO("\t\tFirstBatch\n");
551 }
joshualitt18d6b752016-02-26 08:07:50 -0800552 GR_AUDIT_TRAIL_BATCHING_RESULT_NEW(fAuditTrail, batch);
bsalomon6cc90062016-07-08 11:31:22 -0700553 fRecordedBatches.emplace_back(RecordedBatch{sk_ref_sp(batch), clippedBounds});
bsalomon512be532015-09-10 10:42:55 -0700554}
555
bsalomonaecc0182016-03-07 11:50:44 -0800556void GrDrawTarget::forwardCombine() {
bsalomon6cc90062016-07-08 11:31:22 -0700557 for (int i = 0; i < fRecordedBatches.count() - 2; ++i) {
558 GrBatch* batch = fRecordedBatches[i].fBatch.get();
559 const SkRect& batchBounds = fRecordedBatches[i].fClippedBounds;
560 int maxCandidateIdx = SkTMin(i + fMaxBatchLookahead, fRecordedBatches.count() - 1);
bsalomonaecc0182016-03-07 11:50:44 -0800561 int j = i + 1;
562 while (true) {
bsalomon6cc90062016-07-08 11:31:22 -0700563 GrBatch* candidate = fRecordedBatches[j].fBatch.get();
bsalomonaecc0182016-03-07 11:50:44 -0800564 // We cannot continue to search if the render target changes
565 if (candidate->renderTargetUniqueID() != batch->renderTargetUniqueID()) {
566 GrBATCH_INFO("\t\tBreaking because of (%s, B%u) Rendertarget\n",
567 candidate->name(), candidate->uniqueID());
568 break;
569 }
570 if (j == i +1) {
571 // We assume batch would have combined with candidate when the candidate was added
572 // via backwards combining in recordBatch.
573 SkASSERT(!batch->combineIfPossible(candidate, *this->caps()));
574 } else if (batch->combineIfPossible(candidate, *this->caps())) {
575 GrBATCH_INFO("\t\tCombining with (%s, B%u)\n", candidate->name(),
576 candidate->uniqueID());
joshualittb0666ad2016-03-08 10:43:41 -0800577 GR_AUDIT_TRAIL_BATCHING_RESULT_COMBINED(fAuditTrail, batch, candidate);
bsalomon6cc90062016-07-08 11:31:22 -0700578 fRecordedBatches[j].fBatch = std::move(fRecordedBatches[i].fBatch);
579 join(&fRecordedBatches[j].fClippedBounds, fRecordedBatches[j].fClippedBounds,
580 batchBounds);
bsalomonaecc0182016-03-07 11:50:44 -0800581 break;
582 }
583 // Stop going traversing if we would cause a painter's order violation.
bsalomon6cc90062016-07-08 11:31:22 -0700584 const SkRect& candidateBounds = fRecordedBatches[j].fClippedBounds;
585 if (!can_reorder(candidateBounds, batchBounds)) {
bsalomonaecc0182016-03-07 11:50:44 -0800586 GrBATCH_INFO("\t\tIntersects with (%s, B%u)\n", candidate->name(),
587 candidate->uniqueID());
588 break;
589 }
590 ++j;
591 if (j > maxCandidateIdx) {
592 GrBATCH_INFO("\t\tReached max lookahead or end of batch array %d\n", i);
593 break;
594 }
595 }
596 }
597}
598
egdaniele36914c2015-02-13 09:00:33 -0800599///////////////////////////////////////////////////////////////////////////////
600
bsalomonb3b9aec2015-09-10 11:16:35 -0700601void GrDrawTarget::clearStencilClip(const SkIRect& rect, bool insideClip, GrRenderTarget* rt) {
halcanary385fe4d2015-08-26 13:07:48 -0700602 GrBatch* batch = new GrClearStencilClipBatch(rect, insideClip, rt);
bsalomon6cc90062016-07-08 11:31:22 -0700603 this->recordBatch(batch, batch->bounds());
bsalomon5ea03632015-08-18 10:33:30 -0700604 batch->unref();
605}