blob: e2e188eb12cf131d0781ddbc019b7f9d1372a784 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
joshualittc2893c52015-01-28 06:54:30 -08009#include "GrDrawTarget.h"
joshualitt4d8da812015-01-28 12:53:54 -080010
joshualitt086cee12016-01-12 06:45:24 -080011#include "GrAuditTrail.h"
bsalomoneb1cb5c2015-05-22 08:01:09 -070012#include "GrCaps.h"
bsalomon4061b122015-05-29 10:26:19 -070013#include "GrGpu.h"
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000014#include "GrPath.h"
egdaniele36914c2015-02-13 09:00:33 -080015#include "GrPipeline.h"
joshualittb7133be2015-04-08 09:08:31 -070016#include "GrMemoryPool.h"
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000017#include "GrRenderTarget.h"
bsalomon4061b122015-05-29 10:26:19 -070018#include "GrResourceProvider.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080019#include "GrRenderTargetPriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070020#include "GrSurfacePriv.h"
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000021#include "GrTexture.h"
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000022#include "GrVertexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000023
joshualitt086cee12016-01-12 06:45:24 -080024#include "SkStrokeRec.h"
25
bsalomon53469832015-08-18 09:20:09 -070026#include "batches/GrClearBatch.h"
bsalomon872062c2015-08-18 12:12:35 -070027#include "batches/GrCopySurfaceBatch.h"
bsalomon53469832015-08-18 09:20:09 -070028#include "batches/GrDiscardBatch.h"
bsalomon16b99132015-08-13 14:55:50 -070029#include "batches/GrDrawBatch.h"
bsalomonadd79ef2015-08-19 13:26:49 -070030#include "batches/GrDrawPathBatch.h"
joshualittecd1a692015-08-10 10:08:26 -070031#include "batches/GrRectBatchFactory.h"
bsalomona44919e2015-08-18 13:28:19 -070032#include "batches/GrStencilPathBatch.h"
joshualitt74417822015-08-07 11:42:16 -070033
reed@google.comac10a2d2010-12-22 21:39:39 +000034////////////////////////////////////////////////////////////////////////////////
35
bsalomon489147c2015-12-14 12:13:09 -080036// Experimentally we have found that most batching occurs within the first 10 comparisons.
37static const int kDefaultMaxBatchLookback = 10;
38
bsalomon69cfe952015-11-30 13:27:47 -080039GrDrawTarget::GrDrawTarget(GrRenderTarget* rt, GrGpu* gpu, GrResourceProvider* resourceProvider,
joshualitt086cee12016-01-12 06:45:24 -080040 GrAuditTrail* auditTrail, const Options& options)
bsalomon4061b122015-05-29 10:26:19 -070041 : fGpu(SkRef(gpu))
bsalomon4061b122015-05-29 10:26:19 -070042 , fResourceProvider(resourceProvider)
joshualitt086cee12016-01-12 06:45:24 -080043 , fAuditTrail(auditTrail)
bsalomon648c6962015-10-23 09:06:59 -070044 , fFlags(0)
robertphillips498d7ac2015-10-30 10:11:30 -070045 , fRenderTarget(rt) {
bsalomonb3b9aec2015-09-10 11:16:35 -070046 // TODO: Stop extracting the context (currently needed by GrClipMaskManager)
47 fContext = fGpu->getContext();
bsalomon69cfe952015-11-30 13:27:47 -080048 fClipMaskManager.reset(new GrClipMaskManager(this, options.fClipBatchToBounds));
robertphillips4beb5c12015-10-20 07:50:00 -070049
bsalomon6dea83f2015-12-03 12:58:06 -080050 fDrawBatchBounds = options.fDrawBatchBounds;
bsalomon489147c2015-12-14 12:13:09 -080051 fMaxBatchLookback = (options.fMaxBatchLookback < 0) ? kDefaultMaxBatchLookback :
52 options.fMaxBatchLookback;
bsalomon6dea83f2015-12-03 12:58:06 -080053
robertphillips0dfa62c2015-11-16 06:23:31 -080054 rt->setLastDrawTarget(this);
55
robertphillips4beb5c12015-10-20 07:50:00 -070056#ifdef SK_DEBUG
57 static int debugID = 0;
58 fDebugID = debugID++;
59#endif
bsalomon4061b122015-05-29 10:26:19 -070060}
61
62GrDrawTarget::~GrDrawTarget() {
robertphillips498d7ac2015-10-30 10:11:30 -070063 if (fRenderTarget && this == fRenderTarget->getLastDrawTarget()) {
64 fRenderTarget->setLastDrawTarget(nullptr);
65 }
66
bsalomon4061b122015-05-29 10:26:19 -070067 fGpu->unref();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000068}
69
70////////////////////////////////////////////////////////////////////////////////
71
robertphillips6a186652015-10-20 07:37:58 -070072// Add a GrDrawTarget-based dependency
73void GrDrawTarget::addDependency(GrDrawTarget* dependedOn) {
74 SkASSERT(!dependedOn->dependsOn(this)); // loops are bad
75
76 if (this->dependsOn(dependedOn)) {
77 return; // don't add duplicate dependencies
78 }
79
80 *fDependencies.push() = dependedOn;
81}
82
83// Convert from a GrSurface-based dependency to a GrDrawTarget one
84void GrDrawTarget::addDependency(GrSurface* dependedOn) {
85 if (dependedOn->asRenderTarget() && dependedOn->asRenderTarget()->getLastDrawTarget()) {
86 // If it is still receiving dependencies, this DT shouldn't be closed
87 SkASSERT(!this->isClosed());
88
89 GrDrawTarget* dt = dependedOn->asRenderTarget()->getLastDrawTarget();
90 if (dt == this) {
91 // self-read - presumably for dst reads
92 } else {
93 this->addDependency(dt);
94
95 // Can't make it closed in the self-read case
96 dt->makeClosed();
97 }
98 }
99}
100
robertphillips4beb5c12015-10-20 07:50:00 -0700101#ifdef SK_DEBUG
102void GrDrawTarget::dump() const {
103 SkDebugf("--------------------------------------------------------------\n");
robertphillipse004bfc2015-11-16 09:06:59 -0800104 SkDebugf("node: %d -> RT: %d\n", fDebugID, fRenderTarget ? fRenderTarget->getUniqueID() : -1);
robertphillips4beb5c12015-10-20 07:50:00 -0700105 SkDebugf("relies On (%d): ", fDependencies.count());
106 for (int i = 0; i < fDependencies.count(); ++i) {
107 SkDebugf("%d, ", fDependencies[i]->fDebugID);
108 }
109 SkDebugf("\n");
110 SkDebugf("batches (%d):\n", fBatches.count());
111 for (int i = 0; i < fBatches.count(); ++i) {
112#if 0
113 SkDebugf("*******************************\n");
114#endif
115 SkDebugf("%d: %s\n", i, fBatches[i]->name());
116#if 0
117 SkString str = fBatches[i]->dumpInfo();
118 SkDebugf("%s\n", str.c_str());
119#endif
120 }
121}
122#endif
123
bsalomon50785a32015-02-06 07:02:37 -0800124bool GrDrawTarget::setupDstReadIfNecessary(const GrPipelineBuilder& pipelineBuilder,
ethannicholasde4166a2015-11-30 08:57:38 -0800125 const GrPipelineOptimizations& optimizations,
bsalomon6a44c6a2015-05-26 09:49:05 -0700126 GrXferProcessor::DstTexture* dstTexture,
bsalomonad792c12015-09-10 11:10:50 -0700127 const SkRect& batchBounds) {
128 SkRect bounds = batchBounds;
129 bounds.outset(0.5f, 0.5f);
130
ethannicholasde4166a2015-11-30 08:57:38 -0800131 if (!pipelineBuilder.willXPNeedDstTexture(*this->caps(), optimizations)) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000132 return true;
133 }
cdalton9954bc32015-04-29 14:17:00 -0700134
bsalomon50785a32015-02-06 07:02:37 -0800135 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
cdalton9954bc32015-04-29 14:17:00 -0700136
137 if (this->caps()->textureBarrierSupport()) {
138 if (GrTexture* rtTex = rt->asTexture()) {
bsalomondc47ff72015-05-26 12:16:59 -0700139 // The render target is a texture, so we can read from it directly in the shader. The XP
cdalton9954bc32015-04-29 14:17:00 -0700140 // will be responsible to detect this situation and request a texture barrier.
bsalomon6a44c6a2015-05-26 09:49:05 -0700141 dstTexture->setTexture(rtTex);
142 dstTexture->setOffset(0, 0);
cdalton9954bc32015-04-29 14:17:00 -0700143 return true;
144 }
145 }
146
147 SkIRect copyRect;
robertphillips7bceedc2015-12-01 12:51:26 -0800148 pipelineBuilder.clip().getConservativeBounds(rt->width(), rt->height(), &copyRect);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000149
bsalomonad792c12015-09-10 11:10:50 -0700150 SkIRect drawIBounds;
151 bounds.roundOut(&drawIBounds);
152 if (!copyRect.intersect(drawIBounds)) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000153#ifdef SK_DEBUG
bsalomonb3b9aec2015-09-10 11:16:35 -0700154 GrCapsDebugf(this->caps(), "Missed an early reject. "
155 "Bailing on draw from setupDstReadIfNecessary.\n");
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000156#endif
bsalomonad792c12015-09-10 11:10:50 -0700157 return false;
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000158 }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000159
commit-bot@chromium.org63150af2013-04-11 22:00:22 +0000160 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
161 // have per-sample dst values by making the copy multisampled.
bsalomonf2703d82014-10-28 14:33:06 -0700162 GrSurfaceDesc desc;
bsalomonb3b9aec2015-09-10 11:16:35 -0700163 if (!fGpu->initCopySurfaceDstDesc(rt, &desc)) {
bsalomona73239a2015-04-28 13:35:17 -0700164 desc.fOrigin = kDefault_GrSurfaceOrigin;
165 desc.fFlags = kRenderTarget_GrSurfaceFlag;
166 desc.fConfig = rt->config();
167 }
168
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000169 desc.fWidth = copyRect.width();
170 desc.fHeight = copyRect.height();
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000171
bsalomoneae62002015-07-31 13:59:30 -0700172 static const uint32_t kFlags = 0;
173 SkAutoTUnref<GrTexture> copy(fResourceProvider->createApproxTexture(desc, kFlags));
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000174
bsalomone3059732014-10-14 11:47:22 -0700175 if (!copy) {
tfarina38406c82014-10-31 07:11:12 -0700176 SkDebugf("Failed to create temporary copy of destination texture.\n");
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000177 return false;
178 }
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000179 SkIPoint dstPoint = {0, 0};
bsalomon6df86402015-06-01 10:41:49 -0700180 this->copySurface(copy, rt, copyRect, dstPoint);
181 dstTexture->setTexture(copy);
182 dstTexture->setOffset(copyRect.fLeft, copyRect.fTop);
183 return true;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000184}
185
robertphillipsa13e2022015-11-11 12:01:09 -0800186void GrDrawTarget::prepareBatches(GrBatchFlushState* flushState) {
robertphillipsa106c622015-10-16 09:07:06 -0700187 // Semi-usually the drawTargets are already closed at this point, but sometimes Ganesh
188 // needs to flush mid-draw. In that case, the SkGpuDevice's drawTargets won't be closed
189 // but need to be flushed anyway. Closing such drawTargets here will mean new
190 // drawTargets will be created to replace them if the SkGpuDevice(s) write to them again.
191 this->makeClosed();
192
robertphillips498d7ac2015-10-30 10:11:30 -0700193 // Loop over the batches that haven't yet generated their geometry
robertphillips1f0e3502015-11-10 10:19:50 -0800194 for (int i = 0; i < fBatches.count(); ++i) {
robertphillipsa13e2022015-11-11 12:01:09 -0800195 fBatches[i]->prepare(flushState);
bsalomon512be532015-09-10 10:42:55 -0700196 }
robertphillipsa13e2022015-11-11 12:01:09 -0800197}
bsalomon512be532015-09-10 10:42:55 -0700198
robertphillipsa13e2022015-11-11 12:01:09 -0800199void GrDrawTarget::drawBatches(GrBatchFlushState* flushState) {
bsalomon512be532015-09-10 10:42:55 -0700200 // Draw all the generated geometry.
bsalomon6dea83f2015-12-03 12:58:06 -0800201 SkRandom random;
bsalomon512be532015-09-10 10:42:55 -0700202 for (int i = 0; i < fBatches.count(); ++i) {
bsalomon6dea83f2015-12-03 12:58:06 -0800203 if (fDrawBatchBounds) {
204 const SkRect& bounds = fBatches[i]->bounds();
205 SkIRect ibounds;
206 bounds.roundOut(&ibounds);
207 // In multi-draw buffer all the batches use the same render target and we won't need to
208 // get the batchs bounds.
209 if (GrRenderTarget* rt = fBatches[i]->renderTarget()) {
210 fGpu->drawDebugWireRect(rt, ibounds, 0xFF000000 | random.nextU());
211 }
212 }
robertphillipsa13e2022015-11-11 12:01:09 -0800213 fBatches[i]->draw(flushState);
bsalomon512be532015-09-10 10:42:55 -0700214 }
bsalomona73239a2015-04-28 13:35:17 -0700215}
216
bsalomon512be532015-09-10 10:42:55 -0700217void GrDrawTarget::reset() {
218 fBatches.reset();
219}
220
bsalomonabd30f52015-08-13 13:34:48 -0700221void GrDrawTarget::drawBatch(const GrPipelineBuilder& pipelineBuilder, GrDrawBatch* batch) {
joshualitt4d8da812015-01-28 12:53:54 -0800222 // Setup clip
joshualitt4d8da812015-01-28 12:53:54 -0800223 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomon0ba8c242015-10-07 09:20:28 -0700224 GrAppliedClip clip;
bsalomone91f7b52015-10-27 06:42:50 -0700225 if (!fClipMaskManager->setupClipping(pipelineBuilder, &ars, &batch->bounds(), &clip)) {
joshualitt4d8da812015-01-28 12:53:54 -0800226 return;
227 }
cdaltond4727922015-11-10 12:49:06 -0800228 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
229 if (clip.clipCoverageFragmentProcessor()) {
230 arfps.set(&pipelineBuilder);
231 arfps.addCoverageFragmentProcessor(clip.clipCoverageFragmentProcessor());
232 }
joshualitt4d8da812015-01-28 12:53:54 -0800233
bsalomonad792c12015-09-10 11:10:50 -0700234 GrPipeline::CreateArgs args;
cdaltond4727922015-11-10 12:49:06 -0800235 if (!this->installPipelineInDrawBatch(&pipelineBuilder, &clip.scissorState(), batch)) {
egdaniele36914c2015-02-13 09:00:33 -0800236 return;
237 }
bsalomonad792c12015-09-10 11:10:50 -0700238
robertphillips498d7ac2015-10-30 10:11:30 -0700239#ifdef ENABLE_MDB
240 SkASSERT(fRenderTarget);
241 batch->pipeline()->addDependenciesTo(fRenderTarget);
242#endif
243
bsalomon512be532015-09-10 10:42:55 -0700244 this->recordBatch(batch);
joshualitt4d8da812015-01-28 12:53:54 -0800245}
246
joshualitt2c93efe2014-11-06 12:57:13 -0800247static const GrStencilSettings& winding_path_stencil_settings() {
248 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
249 kIncClamp_StencilOp,
250 kIncClamp_StencilOp,
251 kAlwaysIfInClip_StencilFunc,
252 0xFFFF, 0xFFFF, 0xFFFF);
253 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
254}
255
256static const GrStencilSettings& even_odd_path_stencil_settings() {
257 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
258 kInvert_StencilOp,
259 kInvert_StencilOp,
260 kAlwaysIfInClip_StencilFunc,
261 0xFFFF, 0xFFFF, 0xFFFF);
262 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
263}
264
265void GrDrawTarget::getPathStencilSettingsForFilltype(GrPathRendering::FillType fill,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700266 const GrStencilAttachment* sb,
joshualitt2c93efe2014-11-06 12:57:13 -0800267 GrStencilSettings* outStencilSettings) {
268
269 switch (fill) {
270 default:
271 SkFAIL("Unexpected path fill.");
272 case GrPathRendering::kWinding_FillType:
273 *outStencilSettings = winding_path_stencil_settings();
274 break;
275 case GrPathRendering::kEvenOdd_FillType:
276 *outStencilSettings = even_odd_path_stencil_settings();
277 break;
278 }
bsalomonb3b9aec2015-09-10 11:16:35 -0700279 fClipMaskManager->adjustPathStencilParams(sb, outStencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800280}
281
joshualitt1c735482015-07-13 08:08:25 -0700282void GrDrawTarget::stencilPath(const GrPipelineBuilder& pipelineBuilder,
joshualittf2384692015-09-10 11:00:51 -0700283 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800284 const GrPath* path,
285 GrPathRendering::FillType fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000286 // TODO: extract portions of checkDraw that are relevant to path stenciling.
bsalomon49f085d2014-09-05 13:34:00 -0700287 SkASSERT(path);
jvanverthe9c0fc62015-04-29 11:18:05 -0700288 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
joshualitt2c93efe2014-11-06 12:57:13 -0800289
290 // Setup clip
egdaniel8dd688b2015-01-22 10:16:09 -0800291 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomon0ba8c242015-10-07 09:20:28 -0700292 GrAppliedClip clip;
bsalomone91f7b52015-10-27 06:42:50 -0700293 if (!fClipMaskManager->setupClipping(pipelineBuilder, &ars, nullptr, &clip)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800294 return;
295 }
296
bsalomon0ba8c242015-10-07 09:20:28 -0700297 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
298 if (clip.clipCoverageFragmentProcessor()) {
299 arfps.set(&pipelineBuilder);
300 arfps.addCoverageFragmentProcessor(clip.clipCoverageFragmentProcessor());
301 }
302
joshualitt2c93efe2014-11-06 12:57:13 -0800303 // set stencil settings for path
304 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700305 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdanielec00d942015-09-14 12:56:10 -0700306 GrStencilAttachment* sb = fResourceProvider->attachStencilAttachment(rt);
bsalomon6bc1b5f2015-02-23 09:06:38 -0800307 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800308
joshualittf2384692015-09-10 11:00:51 -0700309 GrBatch* batch = GrStencilPathBatch::Create(viewMatrix,
bsalomona44919e2015-08-18 13:28:19 -0700310 pipelineBuilder.isHWAntialias(),
bsalomone91f7b52015-10-27 06:42:50 -0700311 stencilSettings, clip.scissorState(),
bsalomona44919e2015-08-18 13:28:19 -0700312 pipelineBuilder.getRenderTarget(),
313 path);
bsalomon512be532015-09-10 10:42:55 -0700314 this->recordBatch(batch);
bsalomona44919e2015-08-18 13:28:19 -0700315 batch->unref();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000316}
317
bsalomon1fcc01c2015-09-09 09:48:06 -0700318void GrDrawTarget::drawPathBatch(const GrPipelineBuilder& pipelineBuilder,
cdalton8ff8d242015-12-08 10:20:32 -0800319 GrDrawPathBatchBase* batch) {
bsalomonadd79ef2015-08-19 13:26:49 -0700320 // This looks like drawBatch() but there is an added wrinkle that stencil settings get inserted
bsalomonb3b9aec2015-09-10 11:16:35 -0700321 // after setting up clipping but before onDrawBatch(). TODO: Figure out a better model for
322 // handling stencil settings WRT interactions between pipeline(builder), clipmaskmanager, and
323 // batches.
cdalton8ff8d242015-12-08 10:20:32 -0800324 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
bsalomonadd79ef2015-08-19 13:26:49 -0700325
egdaniel8dd688b2015-01-22 10:16:09 -0800326 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomon0ba8c242015-10-07 09:20:28 -0700327 GrAppliedClip clip;
bsalomone91f7b52015-10-27 06:42:50 -0700328 if (!fClipMaskManager->setupClipping(pipelineBuilder, &ars, &batch->bounds(), &clip)) {
bsalomonadd79ef2015-08-19 13:26:49 -0700329 return;
joshualitt2c93efe2014-11-06 12:57:13 -0800330 }
331
cdaltond4727922015-11-10 12:49:06 -0800332 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
333 if (clip.clipCoverageFragmentProcessor()) {
334 arfps.set(&pipelineBuilder);
335 arfps.addCoverageFragmentProcessor(clip.clipCoverageFragmentProcessor());
336 }
337
bsalomonadd79ef2015-08-19 13:26:49 -0700338 // Ensure the render target has a stencil buffer and get the stencil settings.
joshualitt2c93efe2014-11-06 12:57:13 -0800339 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700340 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdanielec00d942015-09-14 12:56:10 -0700341 GrStencilAttachment* sb = fResourceProvider->attachStencilAttachment(rt);
cdalton8ff8d242015-12-08 10:20:32 -0800342 this->getPathStencilSettingsForFilltype(batch->fillType(), sb, &stencilSettings);
bsalomonadd79ef2015-08-19 13:26:49 -0700343 batch->setStencilSettings(stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800344
bsalomonad792c12015-09-10 11:10:50 -0700345 GrPipeline::CreateArgs args;
cdaltond4727922015-11-10 12:49:06 -0800346 if (!this->installPipelineInDrawBatch(&pipelineBuilder, &clip.scissorState(), batch)) {
bsalomonadd79ef2015-08-19 13:26:49 -0700347 return;
348 }
egdaniele36914c2015-02-13 09:00:33 -0800349
bsalomon512be532015-09-10 10:42:55 -0700350 this->recordBatch(batch);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000351}
352
joshualitt9853cce2014-11-17 14:22:48 -0800353void GrDrawTarget::clear(const SkIRect* rect,
354 GrColor color,
355 bool canIgnoreRect,
bsalomon63b21962014-11-05 07:05:34 -0800356 GrRenderTarget* renderTarget) {
egdaniel51c8d402015-08-06 10:54:13 -0700357 SkIRect rtRect = SkIRect::MakeWH(renderTarget->width(), renderTarget->height());
358 SkIRect clippedRect;
359 if (!rect ||
360 (canIgnoreRect && this->caps()->fullClearIsFree()) ||
361 rect->contains(rtRect)) {
362 rect = &rtRect;
363 } else {
364 clippedRect = *rect;
365 if (!clippedRect.intersect(rtRect)) {
366 return;
367 }
368 rect = &clippedRect;
369 }
370
bsalomonb3b9aec2015-09-10 11:16:35 -0700371 if (this->caps()->useDrawInsteadOfClear()) {
bsalomon63b21962014-11-05 07:05:34 -0800372 // This works around a driver bug with clear by drawing a rect instead.
373 // The driver will ignore a clear if it is the only thing rendered to a
374 // target before the target is read.
egdaniel51c8d402015-08-06 10:54:13 -0700375 if (rect == &rtRect) {
bsalomon63b21962014-11-05 07:05:34 -0800376 this->discard(renderTarget);
377 }
bsalomon63b21962014-11-05 07:05:34 -0800378
egdaniel8dd688b2015-01-22 10:16:09 -0800379 GrPipelineBuilder pipelineBuilder;
egdanielc4b72722015-11-23 13:20:41 -0800380 pipelineBuilder.setXPFactory(
381 GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode))->unref();
egdaniel8dd688b2015-01-22 10:16:09 -0800382 pipelineBuilder.setRenderTarget(renderTarget);
joshualitt9853cce2014-11-17 14:22:48 -0800383
joshualitta8b84992016-01-13 13:35:35 -0800384 SkRect scalarRect = SkRect::Make(*rect);
385 SkAutoTUnref<GrDrawBatch> batch(
386 GrRectBatchFactory::CreateNonAAFill(color, SkMatrix::I(), scalarRect,
387 nullptr, nullptr));
388 this->drawBatch(pipelineBuilder, batch);
bsalomon53469832015-08-18 09:20:09 -0700389 } else {
halcanary385fe4d2015-08-26 13:07:48 -0700390 GrBatch* batch = new GrClearBatch(*rect, color, renderTarget);
bsalomon512be532015-09-10 10:42:55 -0700391 this->recordBatch(batch);
bsalomon53469832015-08-18 09:20:09 -0700392 batch->unref();
393 }
394}
395
396void GrDrawTarget::discard(GrRenderTarget* renderTarget) {
397 if (this->caps()->discardRenderTargetSupport()) {
halcanary385fe4d2015-08-26 13:07:48 -0700398 GrBatch* batch = new GrDiscardBatch(renderTarget);
bsalomon512be532015-09-10 10:42:55 -0700399 this->recordBatch(batch);
bsalomon53469832015-08-18 09:20:09 -0700400 batch->unref();
bsalomon63b21962014-11-05 07:05:34 -0800401 }
402}
403
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000404////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000405
bsalomon6df86402015-06-01 10:41:49 -0700406void GrDrawTarget::copySurface(GrSurface* dst,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000407 GrSurface* src,
408 const SkIRect& srcRect,
409 const SkIPoint& dstPoint) {
bsalomon872062c2015-08-18 12:12:35 -0700410 GrBatch* batch = GrCopySurfaceBatch::Create(dst, src, srcRect, dstPoint);
411 if (batch) {
robertphillips498d7ac2015-10-30 10:11:30 -0700412#ifdef ENABLE_MDB
413 this->addDependency(src);
414#endif
415
bsalomon512be532015-09-10 10:42:55 -0700416 this->recordBatch(batch);
bsalomon872062c2015-08-18 12:12:35 -0700417 batch->unref();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000418 }
bsalomon@google.comeb851172013-04-15 13:51:00 +0000419}
420
bsalomon512be532015-09-10 10:42:55 -0700421template <class Left, class Right> static bool intersect(const Left& a, const Right& b) {
422 SkASSERT(a.fLeft <= a.fRight && a.fTop <= a.fBottom &&
423 b.fLeft <= b.fRight && b.fTop <= b.fBottom);
424 return a.fLeft < b.fRight && b.fLeft < a.fRight && a.fTop < b.fBottom && b.fTop < a.fBottom;
425}
426
427void GrDrawTarget::recordBatch(GrBatch* batch) {
robertphillipsa106c622015-10-16 09:07:06 -0700428 // A closed drawTarget should never receive new/more batches
robertphillips6a186652015-10-20 07:37:58 -0700429 SkASSERT(!this->isClosed());
robertphillipsa106c622015-10-16 09:07:06 -0700430
bsalomon512be532015-09-10 10:42:55 -0700431 // Check if there is a Batch Draw we can batch with by linearly searching back until we either
432 // 1) check every draw
433 // 2) intersect with something
434 // 3) find a 'blocker'
joshualitt086cee12016-01-12 06:45:24 -0800435 GR_AUDIT_TRAIL_ADDBATCH(fAuditTrail, batch->name(), batch->bounds());
bsalomon512be532015-09-10 10:42:55 -0700436 GrBATCH_INFO("Re-Recording (%s, B%u)\n"
joshualitte2bcec32015-09-30 06:22:22 -0700437 "\tBounds LRTB (%f, %f, %f, %f)\n",
bsalomon512be532015-09-10 10:42:55 -0700438 batch->name(),
439 batch->uniqueID(),
440 batch->bounds().fLeft, batch->bounds().fRight,
441 batch->bounds().fTop, batch->bounds().fBottom);
442 GrBATCH_INFO(SkTabString(batch->dumpInfo(), 1).c_str());
443 GrBATCH_INFO("\tOutcome:\n");
bsalomon489147c2015-12-14 12:13:09 -0800444 int maxCandidates = SkTMin(fMaxBatchLookback, fBatches.count());
bsalomon512be532015-09-10 10:42:55 -0700445 if (maxCandidates) {
446 int i = 0;
447 while (true) {
448 GrBatch* candidate = fBatches.fromBack(i);
449 // We cannot continue to search backwards if the render target changes
450 if (candidate->renderTargetUniqueID() != batch->renderTargetUniqueID()) {
451 GrBATCH_INFO("\t\tBreaking because of (%s, B%u) Rendertarget\n",
452 candidate->name(), candidate->uniqueID());
453 break;
454 }
455 if (candidate->combineIfPossible(batch, *this->caps())) {
456 GrBATCH_INFO("\t\tCombining with (%s, B%u)\n", candidate->name(),
457 candidate->uniqueID());
458 return;
459 }
460 // Stop going backwards if we would cause a painter's order violation.
bsalomondb4758c2015-11-23 11:14:20 -0800461 // TODO: The bounds used here do not fully consider the clip. It may be advantageous
462 // to clip each batch's bounds to the clip.
bsalomon512be532015-09-10 10:42:55 -0700463 if (intersect(candidate->bounds(), batch->bounds())) {
464 GrBATCH_INFO("\t\tIntersects with (%s, B%u)\n", candidate->name(),
465 candidate->uniqueID());
466 break;
467 }
468 ++i;
469 if (i == maxCandidates) {
470 GrBATCH_INFO("\t\tReached max lookback or beginning of batch array %d\n", i);
471 break;
472 }
473 }
474 } else {
475 GrBATCH_INFO("\t\tFirstBatch\n");
476 }
477 fBatches.push_back().reset(SkRef(batch));
478}
479
egdaniele36914c2015-02-13 09:00:33 -0800480///////////////////////////////////////////////////////////////////////////////
481
bsalomonad792c12015-09-10 11:10:50 -0700482bool GrDrawTarget::installPipelineInDrawBatch(const GrPipelineBuilder* pipelineBuilder,
cdaltond4727922015-11-10 12:49:06 -0800483 const GrScissorState* scissor,
484 GrDrawBatch* batch) {
bsalomonad792c12015-09-10 11:10:50 -0700485 GrPipeline::CreateArgs args;
486 args.fPipelineBuilder = pipelineBuilder;
487 args.fCaps = this->caps();
cdaltond4727922015-11-10 12:49:06 -0800488 args.fScissor = scissor;
ethannicholasff210322015-11-24 12:10:10 -0800489 batch->getPipelineOptimizations(&args.fOpts);
490 args.fOpts.fColorPOI.completeCalculations(pipelineBuilder->fColorFragmentProcessors.begin(),
491 pipelineBuilder->numColorFragmentProcessors());
492 args.fOpts.fCoveragePOI.completeCalculations(
493 pipelineBuilder->fCoverageFragmentProcessors.begin(),
494 pipelineBuilder->numCoverageFragmentProcessors());
ethannicholasde4166a2015-11-30 08:57:38 -0800495 if (!this->setupDstReadIfNecessary(*pipelineBuilder, args.fOpts, &args.fDstTexture,
bsalomonad792c12015-09-10 11:10:50 -0700496 batch->bounds())) {
497 return false;
egdaniele36914c2015-02-13 09:00:33 -0800498 }
bsalomonad792c12015-09-10 11:10:50 -0700499
500 if (!batch->installPipeline(args)) {
501 return false;
502 }
503
504 return true;
egdaniele36914c2015-02-13 09:00:33 -0800505}
506
bsalomonb3b9aec2015-09-10 11:16:35 -0700507void GrDrawTarget::clearStencilClip(const SkIRect& rect, bool insideClip, GrRenderTarget* rt) {
halcanary385fe4d2015-08-26 13:07:48 -0700508 GrBatch* batch = new GrClearStencilClipBatch(rect, insideClip, rt);
bsalomon512be532015-09-10 10:42:55 -0700509 this->recordBatch(batch);
bsalomon5ea03632015-08-18 10:33:30 -0700510 batch->unref();
511}