blob: 55f66249847c1970733415521fe2ba05251e0347 [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
bsalomoneb1cb5c2015-05-22 08:01:09 -070011#include "GrCaps.h"
bsalomon4061b122015-05-29 10:26:19 -070012#include "GrGpu.h"
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000013#include "GrPath.h"
egdaniele36914c2015-02-13 09:00:33 -080014#include "GrPipeline.h"
joshualittb7133be2015-04-08 09:08:31 -070015#include "GrMemoryPool.h"
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000016#include "GrRenderTarget.h"
bsalomon4061b122015-05-29 10:26:19 -070017#include "GrResourceProvider.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080018#include "GrRenderTargetPriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070019#include "GrSurfacePriv.h"
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000020#include "GrTexture.h"
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000021#include "GrVertexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000022
bsalomon53469832015-08-18 09:20:09 -070023#include "batches/GrClearBatch.h"
bsalomon872062c2015-08-18 12:12:35 -070024#include "batches/GrCopySurfaceBatch.h"
bsalomon53469832015-08-18 09:20:09 -070025#include "batches/GrDiscardBatch.h"
bsalomon16b99132015-08-13 14:55:50 -070026#include "batches/GrDrawBatch.h"
bsalomonadd79ef2015-08-19 13:26:49 -070027#include "batches/GrDrawPathBatch.h"
joshualittecd1a692015-08-10 10:08:26 -070028#include "batches/GrRectBatchFactory.h"
bsalomona44919e2015-08-18 13:28:19 -070029#include "batches/GrStencilPathBatch.h"
joshualitt74417822015-08-07 11:42:16 -070030
sugoi@google.com5f74cf82012-12-17 21:16:45 +000031#include "SkStrokeRec.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000032
reed@google.comac10a2d2010-12-22 21:39:39 +000033////////////////////////////////////////////////////////////////////////////////
34
bsalomon489147c2015-12-14 12:13:09 -080035// Experimentally we have found that most batching occurs within the first 10 comparisons.
36static const int kDefaultMaxBatchLookback = 10;
37
bsalomon69cfe952015-11-30 13:27:47 -080038GrDrawTarget::GrDrawTarget(GrRenderTarget* rt, GrGpu* gpu, GrResourceProvider* resourceProvider,
39 const Options& options)
bsalomon4061b122015-05-29 10:26:19 -070040 : fGpu(SkRef(gpu))
bsalomon4061b122015-05-29 10:26:19 -070041 , fResourceProvider(resourceProvider)
bsalomon512be532015-09-10 10:42:55 -070042 , fFlushing(false)
bsalomon648c6962015-10-23 09:06:59 -070043 , fFlags(0)
robertphillips498d7ac2015-10-30 10:11:30 -070044 , fRenderTarget(rt) {
bsalomonb3b9aec2015-09-10 11:16:35 -070045 // TODO: Stop extracting the context (currently needed by GrClipMaskManager)
46 fContext = fGpu->getContext();
bsalomon69cfe952015-11-30 13:27:47 -080047 fClipMaskManager.reset(new GrClipMaskManager(this, options.fClipBatchToBounds));
robertphillips4beb5c12015-10-20 07:50:00 -070048
bsalomon6dea83f2015-12-03 12:58:06 -080049 fDrawBatchBounds = options.fDrawBatchBounds;
bsalomon489147c2015-12-14 12:13:09 -080050 fMaxBatchLookback = (options.fMaxBatchLookback < 0) ? kDefaultMaxBatchLookback :
51 options.fMaxBatchLookback;
bsalomon6dea83f2015-12-03 12:58:06 -080052
robertphillips0dfa62c2015-11-16 06:23:31 -080053 rt->setLastDrawTarget(this);
54
robertphillips4beb5c12015-10-20 07:50:00 -070055#ifdef SK_DEBUG
56 static int debugID = 0;
57 fDebugID = debugID++;
58#endif
bsalomon4061b122015-05-29 10:26:19 -070059}
60
61GrDrawTarget::~GrDrawTarget() {
robertphillips498d7ac2015-10-30 10:11:30 -070062 if (fRenderTarget && this == fRenderTarget->getLastDrawTarget()) {
63 fRenderTarget->setLastDrawTarget(nullptr);
64 }
65
bsalomon4061b122015-05-29 10:26:19 -070066 fGpu->unref();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000067}
68
69////////////////////////////////////////////////////////////////////////////////
70
robertphillips6a186652015-10-20 07:37:58 -070071// Add a GrDrawTarget-based dependency
72void GrDrawTarget::addDependency(GrDrawTarget* dependedOn) {
73 SkASSERT(!dependedOn->dependsOn(this)); // loops are bad
74
75 if (this->dependsOn(dependedOn)) {
76 return; // don't add duplicate dependencies
77 }
78
79 *fDependencies.push() = dependedOn;
80}
81
82// Convert from a GrSurface-based dependency to a GrDrawTarget one
83void GrDrawTarget::addDependency(GrSurface* dependedOn) {
84 if (dependedOn->asRenderTarget() && dependedOn->asRenderTarget()->getLastDrawTarget()) {
85 // If it is still receiving dependencies, this DT shouldn't be closed
86 SkASSERT(!this->isClosed());
87
88 GrDrawTarget* dt = dependedOn->asRenderTarget()->getLastDrawTarget();
89 if (dt == this) {
90 // self-read - presumably for dst reads
91 } else {
92 this->addDependency(dt);
93
94 // Can't make it closed in the self-read case
95 dt->makeClosed();
96 }
97 }
98}
99
robertphillips4beb5c12015-10-20 07:50:00 -0700100#ifdef SK_DEBUG
101void GrDrawTarget::dump() const {
102 SkDebugf("--------------------------------------------------------------\n");
robertphillipse004bfc2015-11-16 09:06:59 -0800103 SkDebugf("node: %d -> RT: %d\n", fDebugID, fRenderTarget ? fRenderTarget->getUniqueID() : -1);
robertphillips4beb5c12015-10-20 07:50:00 -0700104 SkDebugf("relies On (%d): ", fDependencies.count());
105 for (int i = 0; i < fDependencies.count(); ++i) {
106 SkDebugf("%d, ", fDependencies[i]->fDebugID);
107 }
108 SkDebugf("\n");
109 SkDebugf("batches (%d):\n", fBatches.count());
110 for (int i = 0; i < fBatches.count(); ++i) {
111#if 0
112 SkDebugf("*******************************\n");
113#endif
114 SkDebugf("%d: %s\n", i, fBatches[i]->name());
115#if 0
116 SkString str = fBatches[i]->dumpInfo();
117 SkDebugf("%s\n", str.c_str());
118#endif
119 }
120}
121#endif
122
bsalomon50785a32015-02-06 07:02:37 -0800123bool GrDrawTarget::setupDstReadIfNecessary(const GrPipelineBuilder& pipelineBuilder,
ethannicholasde4166a2015-11-30 08:57:38 -0800124 const GrPipelineOptimizations& optimizations,
bsalomon6a44c6a2015-05-26 09:49:05 -0700125 GrXferProcessor::DstTexture* dstTexture,
bsalomonad792c12015-09-10 11:10:50 -0700126 const SkRect& batchBounds) {
127 SkRect bounds = batchBounds;
128 bounds.outset(0.5f, 0.5f);
129
ethannicholasde4166a2015-11-30 08:57:38 -0800130 if (!pipelineBuilder.willXPNeedDstTexture(*this->caps(), optimizations)) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000131 return true;
132 }
cdalton9954bc32015-04-29 14:17:00 -0700133
bsalomon50785a32015-02-06 07:02:37 -0800134 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
cdalton9954bc32015-04-29 14:17:00 -0700135
136 if (this->caps()->textureBarrierSupport()) {
137 if (GrTexture* rtTex = rt->asTexture()) {
bsalomondc47ff72015-05-26 12:16:59 -0700138 // The render target is a texture, so we can read from it directly in the shader. The XP
cdalton9954bc32015-04-29 14:17:00 -0700139 // will be responsible to detect this situation and request a texture barrier.
bsalomon6a44c6a2015-05-26 09:49:05 -0700140 dstTexture->setTexture(rtTex);
141 dstTexture->setOffset(0, 0);
cdalton9954bc32015-04-29 14:17:00 -0700142 return true;
143 }
144 }
145
146 SkIRect copyRect;
robertphillips7bceedc2015-12-01 12:51:26 -0800147 pipelineBuilder.clip().getConservativeBounds(rt->width(), rt->height(), &copyRect);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000148
bsalomonad792c12015-09-10 11:10:50 -0700149 SkIRect drawIBounds;
150 bounds.roundOut(&drawIBounds);
151 if (!copyRect.intersect(drawIBounds)) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000152#ifdef SK_DEBUG
bsalomonb3b9aec2015-09-10 11:16:35 -0700153 GrCapsDebugf(this->caps(), "Missed an early reject. "
154 "Bailing on draw from setupDstReadIfNecessary.\n");
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000155#endif
bsalomonad792c12015-09-10 11:10:50 -0700156 return false;
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000157 }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000158
commit-bot@chromium.org63150af2013-04-11 22:00:22 +0000159 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
160 // have per-sample dst values by making the copy multisampled.
bsalomonf2703d82014-10-28 14:33:06 -0700161 GrSurfaceDesc desc;
bsalomonb3b9aec2015-09-10 11:16:35 -0700162 if (!fGpu->initCopySurfaceDstDesc(rt, &desc)) {
bsalomona73239a2015-04-28 13:35:17 -0700163 desc.fOrigin = kDefault_GrSurfaceOrigin;
164 desc.fFlags = kRenderTarget_GrSurfaceFlag;
165 desc.fConfig = rt->config();
166 }
167
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000168 desc.fWidth = copyRect.width();
169 desc.fHeight = copyRect.height();
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000170
bsalomoneae62002015-07-31 13:59:30 -0700171 static const uint32_t kFlags = 0;
172 SkAutoTUnref<GrTexture> copy(fResourceProvider->createApproxTexture(desc, kFlags));
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000173
bsalomone3059732014-10-14 11:47:22 -0700174 if (!copy) {
tfarina38406c82014-10-31 07:11:12 -0700175 SkDebugf("Failed to create temporary copy of destination texture.\n");
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000176 return false;
177 }
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000178 SkIPoint dstPoint = {0, 0};
bsalomon6df86402015-06-01 10:41:49 -0700179 this->copySurface(copy, rt, copyRect, dstPoint);
180 dstTexture->setTexture(copy);
181 dstTexture->setOffset(copyRect.fLeft, copyRect.fTop);
182 return true;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000183}
184
robertphillipsa13e2022015-11-11 12:01:09 -0800185void GrDrawTarget::prepareBatches(GrBatchFlushState* flushState) {
bsalomona73239a2015-04-28 13:35:17 -0700186 if (fFlushing) {
187 return;
188 }
189 fFlushing = true;
190
robertphillipsa106c622015-10-16 09:07:06 -0700191 // Semi-usually the drawTargets are already closed at this point, but sometimes Ganesh
192 // needs to flush mid-draw. In that case, the SkGpuDevice's drawTargets won't be closed
193 // but need to be flushed anyway. Closing such drawTargets here will mean new
194 // drawTargets will be created to replace them if the SkGpuDevice(s) write to them again.
195 this->makeClosed();
196
robertphillips498d7ac2015-10-30 10:11:30 -0700197 // Loop over the batches that haven't yet generated their geometry
robertphillips1f0e3502015-11-10 10:19:50 -0800198 for (int i = 0; i < fBatches.count(); ++i) {
robertphillipsa13e2022015-11-11 12:01:09 -0800199 fBatches[i]->prepare(flushState);
bsalomon512be532015-09-10 10:42:55 -0700200 }
robertphillipsa13e2022015-11-11 12:01:09 -0800201}
bsalomon512be532015-09-10 10:42:55 -0700202
robertphillipsa13e2022015-11-11 12:01:09 -0800203void GrDrawTarget::drawBatches(GrBatchFlushState* flushState) {
bsalomon512be532015-09-10 10:42:55 -0700204 // Draw all the generated geometry.
bsalomon6dea83f2015-12-03 12:58:06 -0800205 SkRandom random;
bsalomon512be532015-09-10 10:42:55 -0700206 for (int i = 0; i < fBatches.count(); ++i) {
bsalomon6dea83f2015-12-03 12:58:06 -0800207 if (fDrawBatchBounds) {
208 const SkRect& bounds = fBatches[i]->bounds();
209 SkIRect ibounds;
210 bounds.roundOut(&ibounds);
211 // In multi-draw buffer all the batches use the same render target and we won't need to
212 // get the batchs bounds.
213 if (GrRenderTarget* rt = fBatches[i]->renderTarget()) {
214 fGpu->drawDebugWireRect(rt, ibounds, 0xFF000000 | random.nextU());
215 }
216 }
robertphillipsa13e2022015-11-11 12:01:09 -0800217 fBatches[i]->draw(flushState);
bsalomon512be532015-09-10 10:42:55 -0700218 }
219
bsalomona73239a2015-04-28 13:35:17 -0700220 fFlushing = false;
bsalomona73239a2015-04-28 13:35:17 -0700221}
222
bsalomon512be532015-09-10 10:42:55 -0700223void GrDrawTarget::reset() {
224 fBatches.reset();
225}
226
bsalomonabd30f52015-08-13 13:34:48 -0700227void GrDrawTarget::drawBatch(const GrPipelineBuilder& pipelineBuilder, GrDrawBatch* batch) {
joshualitt4d8da812015-01-28 12:53:54 -0800228 // Setup clip
joshualitt4d8da812015-01-28 12:53:54 -0800229 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomon0ba8c242015-10-07 09:20:28 -0700230 GrAppliedClip clip;
bsalomone91f7b52015-10-27 06:42:50 -0700231 if (!fClipMaskManager->setupClipping(pipelineBuilder, &ars, &batch->bounds(), &clip)) {
joshualitt4d8da812015-01-28 12:53:54 -0800232 return;
233 }
cdaltond4727922015-11-10 12:49:06 -0800234 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
235 if (clip.clipCoverageFragmentProcessor()) {
236 arfps.set(&pipelineBuilder);
237 arfps.addCoverageFragmentProcessor(clip.clipCoverageFragmentProcessor());
238 }
joshualitt4d8da812015-01-28 12:53:54 -0800239
bsalomonad792c12015-09-10 11:10:50 -0700240 GrPipeline::CreateArgs args;
cdaltond4727922015-11-10 12:49:06 -0800241 if (!this->installPipelineInDrawBatch(&pipelineBuilder, &clip.scissorState(), batch)) {
egdaniele36914c2015-02-13 09:00:33 -0800242 return;
243 }
bsalomonad792c12015-09-10 11:10:50 -0700244
robertphillips498d7ac2015-10-30 10:11:30 -0700245#ifdef ENABLE_MDB
246 SkASSERT(fRenderTarget);
247 batch->pipeline()->addDependenciesTo(fRenderTarget);
248#endif
249
bsalomon512be532015-09-10 10:42:55 -0700250 this->recordBatch(batch);
joshualitt4d8da812015-01-28 12:53:54 -0800251}
252
joshualitt2c93efe2014-11-06 12:57:13 -0800253static const GrStencilSettings& winding_path_stencil_settings() {
254 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
255 kIncClamp_StencilOp,
256 kIncClamp_StencilOp,
257 kAlwaysIfInClip_StencilFunc,
258 0xFFFF, 0xFFFF, 0xFFFF);
259 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
260}
261
262static const GrStencilSettings& even_odd_path_stencil_settings() {
263 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
264 kInvert_StencilOp,
265 kInvert_StencilOp,
266 kAlwaysIfInClip_StencilFunc,
267 0xFFFF, 0xFFFF, 0xFFFF);
268 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
269}
270
271void GrDrawTarget::getPathStencilSettingsForFilltype(GrPathRendering::FillType fill,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700272 const GrStencilAttachment* sb,
joshualitt2c93efe2014-11-06 12:57:13 -0800273 GrStencilSettings* outStencilSettings) {
274
275 switch (fill) {
276 default:
277 SkFAIL("Unexpected path fill.");
278 case GrPathRendering::kWinding_FillType:
279 *outStencilSettings = winding_path_stencil_settings();
280 break;
281 case GrPathRendering::kEvenOdd_FillType:
282 *outStencilSettings = even_odd_path_stencil_settings();
283 break;
284 }
bsalomonb3b9aec2015-09-10 11:16:35 -0700285 fClipMaskManager->adjustPathStencilParams(sb, outStencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800286}
287
joshualitt1c735482015-07-13 08:08:25 -0700288void GrDrawTarget::stencilPath(const GrPipelineBuilder& pipelineBuilder,
joshualittf2384692015-09-10 11:00:51 -0700289 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800290 const GrPath* path,
291 GrPathRendering::FillType fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000292 // TODO: extract portions of checkDraw that are relevant to path stenciling.
bsalomon49f085d2014-09-05 13:34:00 -0700293 SkASSERT(path);
jvanverthe9c0fc62015-04-29 11:18:05 -0700294 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
joshualitt2c93efe2014-11-06 12:57:13 -0800295
296 // Setup clip
egdaniel8dd688b2015-01-22 10:16:09 -0800297 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomon0ba8c242015-10-07 09:20:28 -0700298 GrAppliedClip clip;
bsalomone91f7b52015-10-27 06:42:50 -0700299 if (!fClipMaskManager->setupClipping(pipelineBuilder, &ars, nullptr, &clip)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800300 return;
301 }
302
bsalomon0ba8c242015-10-07 09:20:28 -0700303 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
304 if (clip.clipCoverageFragmentProcessor()) {
305 arfps.set(&pipelineBuilder);
306 arfps.addCoverageFragmentProcessor(clip.clipCoverageFragmentProcessor());
307 }
308
joshualitt2c93efe2014-11-06 12:57:13 -0800309 // set stencil settings for path
310 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700311 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdanielec00d942015-09-14 12:56:10 -0700312 GrStencilAttachment* sb = fResourceProvider->attachStencilAttachment(rt);
bsalomon6bc1b5f2015-02-23 09:06:38 -0800313 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800314
joshualittf2384692015-09-10 11:00:51 -0700315 GrBatch* batch = GrStencilPathBatch::Create(viewMatrix,
bsalomona44919e2015-08-18 13:28:19 -0700316 pipelineBuilder.isHWAntialias(),
bsalomone91f7b52015-10-27 06:42:50 -0700317 stencilSettings, clip.scissorState(),
bsalomona44919e2015-08-18 13:28:19 -0700318 pipelineBuilder.getRenderTarget(),
319 path);
bsalomon512be532015-09-10 10:42:55 -0700320 this->recordBatch(batch);
bsalomona44919e2015-08-18 13:28:19 -0700321 batch->unref();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000322}
323
bsalomon1fcc01c2015-09-09 09:48:06 -0700324void GrDrawTarget::drawPathBatch(const GrPipelineBuilder& pipelineBuilder,
cdalton8ff8d242015-12-08 10:20:32 -0800325 GrDrawPathBatchBase* batch) {
bsalomonadd79ef2015-08-19 13:26:49 -0700326 // This looks like drawBatch() but there is an added wrinkle that stencil settings get inserted
bsalomonb3b9aec2015-09-10 11:16:35 -0700327 // after setting up clipping but before onDrawBatch(). TODO: Figure out a better model for
328 // handling stencil settings WRT interactions between pipeline(builder), clipmaskmanager, and
329 // batches.
cdalton8ff8d242015-12-08 10:20:32 -0800330 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
bsalomonadd79ef2015-08-19 13:26:49 -0700331
egdaniel8dd688b2015-01-22 10:16:09 -0800332 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomon0ba8c242015-10-07 09:20:28 -0700333 GrAppliedClip clip;
bsalomone91f7b52015-10-27 06:42:50 -0700334 if (!fClipMaskManager->setupClipping(pipelineBuilder, &ars, &batch->bounds(), &clip)) {
bsalomonadd79ef2015-08-19 13:26:49 -0700335 return;
joshualitt2c93efe2014-11-06 12:57:13 -0800336 }
337
cdaltond4727922015-11-10 12:49:06 -0800338 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
339 if (clip.clipCoverageFragmentProcessor()) {
340 arfps.set(&pipelineBuilder);
341 arfps.addCoverageFragmentProcessor(clip.clipCoverageFragmentProcessor());
342 }
343
bsalomonadd79ef2015-08-19 13:26:49 -0700344 // Ensure the render target has a stencil buffer and get the stencil settings.
joshualitt2c93efe2014-11-06 12:57:13 -0800345 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700346 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdanielec00d942015-09-14 12:56:10 -0700347 GrStencilAttachment* sb = fResourceProvider->attachStencilAttachment(rt);
cdalton8ff8d242015-12-08 10:20:32 -0800348 this->getPathStencilSettingsForFilltype(batch->fillType(), sb, &stencilSettings);
bsalomonadd79ef2015-08-19 13:26:49 -0700349 batch->setStencilSettings(stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800350
bsalomonad792c12015-09-10 11:10:50 -0700351 GrPipeline::CreateArgs args;
cdaltond4727922015-11-10 12:49:06 -0800352 if (!this->installPipelineInDrawBatch(&pipelineBuilder, &clip.scissorState(), batch)) {
bsalomonadd79ef2015-08-19 13:26:49 -0700353 return;
354 }
egdaniele36914c2015-02-13 09:00:33 -0800355
bsalomon512be532015-09-10 10:42:55 -0700356 this->recordBatch(batch);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000357}
358
joshualittd2b23e02015-08-21 10:53:34 -0700359void GrDrawTarget::drawNonAARect(const GrPipelineBuilder& pipelineBuilder,
joshualitt1c735482015-07-13 08:08:25 -0700360 GrColor color,
361 const SkMatrix& viewMatrix,
joshualittb6b513b2015-08-21 10:25:18 -0700362 const SkRect& rect) {
joshualittd2b23e02015-08-21 10:53:34 -0700363 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect,
364 nullptr, nullptr));
joshualittad17cfc2015-05-05 10:45:57 -0700365 this->drawBatch(pipelineBuilder, batch);
366}
367
joshualittd2b23e02015-08-21 10:53:34 -0700368void GrDrawTarget::drawNonAARect(const GrPipelineBuilder& pipelineBuilder,
joshualittb6b513b2015-08-21 10:25:18 -0700369 GrColor color,
370 const SkMatrix& viewMatrix,
371 const SkRect& rect,
372 const SkMatrix& localMatrix) {
joshualittd2b23e02015-08-21 10:53:34 -0700373 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect,
374 nullptr, &localMatrix));
joshualittb6b513b2015-08-21 10:25:18 -0700375 this->drawBatch(pipelineBuilder, batch);
376}
377
joshualittd2b23e02015-08-21 10:53:34 -0700378void GrDrawTarget::drawNonAARect(const GrPipelineBuilder& pipelineBuilder,
joshualittb6b513b2015-08-21 10:25:18 -0700379 GrColor color,
380 const SkMatrix& viewMatrix,
381 const SkRect& rect,
382 const SkRect& localRect) {
joshualittd2b23e02015-08-21 10:53:34 -0700383 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect,
384 &localRect, nullptr));
joshualittb6b513b2015-08-21 10:25:18 -0700385 this->drawBatch(pipelineBuilder, batch);
386}
387
388
joshualitt1c735482015-07-13 08:08:25 -0700389void GrDrawTarget::drawAARect(const GrPipelineBuilder& pipelineBuilder,
robertphillipsea461502015-05-26 11:38:03 -0700390 GrColor color,
391 const SkMatrix& viewMatrix,
392 const SkRect& rect,
393 const SkRect& devRect) {
joshualittd2b23e02015-08-21 10:53:34 -0700394 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateAAFill(color, viewMatrix, rect,
bsalomonabd30f52015-08-13 13:34:48 -0700395 devRect));
joshualitt14205b12015-08-10 11:40:56 -0700396 this->drawBatch(pipelineBuilder, batch);
robertphillipsea461502015-05-26 11:38:03 -0700397}
398
joshualitt9853cce2014-11-17 14:22:48 -0800399void GrDrawTarget::clear(const SkIRect* rect,
400 GrColor color,
401 bool canIgnoreRect,
bsalomon63b21962014-11-05 07:05:34 -0800402 GrRenderTarget* renderTarget) {
egdaniel51c8d402015-08-06 10:54:13 -0700403 SkIRect rtRect = SkIRect::MakeWH(renderTarget->width(), renderTarget->height());
404 SkIRect clippedRect;
405 if (!rect ||
406 (canIgnoreRect && this->caps()->fullClearIsFree()) ||
407 rect->contains(rtRect)) {
408 rect = &rtRect;
409 } else {
410 clippedRect = *rect;
411 if (!clippedRect.intersect(rtRect)) {
412 return;
413 }
414 rect = &clippedRect;
415 }
416
bsalomonb3b9aec2015-09-10 11:16:35 -0700417 if (this->caps()->useDrawInsteadOfClear()) {
bsalomon63b21962014-11-05 07:05:34 -0800418 // This works around a driver bug with clear by drawing a rect instead.
419 // The driver will ignore a clear if it is the only thing rendered to a
420 // target before the target is read.
egdaniel51c8d402015-08-06 10:54:13 -0700421 if (rect == &rtRect) {
bsalomon63b21962014-11-05 07:05:34 -0800422 this->discard(renderTarget);
423 }
bsalomon63b21962014-11-05 07:05:34 -0800424
egdaniel8dd688b2015-01-22 10:16:09 -0800425 GrPipelineBuilder pipelineBuilder;
egdanielc4b72722015-11-23 13:20:41 -0800426 pipelineBuilder.setXPFactory(
427 GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode))->unref();
egdaniel8dd688b2015-01-22 10:16:09 -0800428 pipelineBuilder.setRenderTarget(renderTarget);
joshualitt9853cce2014-11-17 14:22:48 -0800429
joshualittd2b23e02015-08-21 10:53:34 -0700430 this->drawNonAARect(pipelineBuilder, color, SkMatrix::I(), *rect);
bsalomon53469832015-08-18 09:20:09 -0700431 } else {
halcanary385fe4d2015-08-26 13:07:48 -0700432 GrBatch* batch = new GrClearBatch(*rect, color, renderTarget);
bsalomon512be532015-09-10 10:42:55 -0700433 this->recordBatch(batch);
bsalomon53469832015-08-18 09:20:09 -0700434 batch->unref();
435 }
436}
437
438void GrDrawTarget::discard(GrRenderTarget* renderTarget) {
439 if (this->caps()->discardRenderTargetSupport()) {
halcanary385fe4d2015-08-26 13:07:48 -0700440 GrBatch* batch = new GrDiscardBatch(renderTarget);
bsalomon512be532015-09-10 10:42:55 -0700441 this->recordBatch(batch);
bsalomon53469832015-08-18 09:20:09 -0700442 batch->unref();
bsalomon63b21962014-11-05 07:05:34 -0800443 }
444}
445
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000446////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000447
bsalomon6df86402015-06-01 10:41:49 -0700448void GrDrawTarget::copySurface(GrSurface* dst,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000449 GrSurface* src,
450 const SkIRect& srcRect,
451 const SkIPoint& dstPoint) {
bsalomon872062c2015-08-18 12:12:35 -0700452 GrBatch* batch = GrCopySurfaceBatch::Create(dst, src, srcRect, dstPoint);
453 if (batch) {
robertphillips498d7ac2015-10-30 10:11:30 -0700454#ifdef ENABLE_MDB
455 this->addDependency(src);
456#endif
457
bsalomon512be532015-09-10 10:42:55 -0700458 this->recordBatch(batch);
bsalomon872062c2015-08-18 12:12:35 -0700459 batch->unref();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000460 }
bsalomon@google.comeb851172013-04-15 13:51:00 +0000461}
462
bsalomon512be532015-09-10 10:42:55 -0700463template <class Left, class Right> static bool intersect(const Left& a, const Right& b) {
464 SkASSERT(a.fLeft <= a.fRight && a.fTop <= a.fBottom &&
465 b.fLeft <= b.fRight && b.fTop <= b.fBottom);
466 return a.fLeft < b.fRight && b.fLeft < a.fRight && a.fTop < b.fBottom && b.fTop < a.fBottom;
467}
468
469void GrDrawTarget::recordBatch(GrBatch* batch) {
robertphillipsa106c622015-10-16 09:07:06 -0700470 // A closed drawTarget should never receive new/more batches
robertphillips6a186652015-10-20 07:37:58 -0700471 SkASSERT(!this->isClosed());
robertphillipsa106c622015-10-16 09:07:06 -0700472
bsalomon512be532015-09-10 10:42:55 -0700473 // Check if there is a Batch Draw we can batch with by linearly searching back until we either
474 // 1) check every draw
475 // 2) intersect with something
476 // 3) find a 'blocker'
bsalomon512be532015-09-10 10:42:55 -0700477
478 GrBATCH_INFO("Re-Recording (%s, B%u)\n"
joshualitte2bcec32015-09-30 06:22:22 -0700479 "\tBounds LRTB (%f, %f, %f, %f)\n",
bsalomon512be532015-09-10 10:42:55 -0700480 batch->name(),
481 batch->uniqueID(),
482 batch->bounds().fLeft, batch->bounds().fRight,
483 batch->bounds().fTop, batch->bounds().fBottom);
484 GrBATCH_INFO(SkTabString(batch->dumpInfo(), 1).c_str());
485 GrBATCH_INFO("\tOutcome:\n");
bsalomon489147c2015-12-14 12:13:09 -0800486 int maxCandidates = SkTMin(fMaxBatchLookback, fBatches.count());
bsalomon512be532015-09-10 10:42:55 -0700487 if (maxCandidates) {
488 int i = 0;
489 while (true) {
490 GrBatch* candidate = fBatches.fromBack(i);
491 // We cannot continue to search backwards if the render target changes
492 if (candidate->renderTargetUniqueID() != batch->renderTargetUniqueID()) {
493 GrBATCH_INFO("\t\tBreaking because of (%s, B%u) Rendertarget\n",
494 candidate->name(), candidate->uniqueID());
495 break;
496 }
497 if (candidate->combineIfPossible(batch, *this->caps())) {
498 GrBATCH_INFO("\t\tCombining with (%s, B%u)\n", candidate->name(),
499 candidate->uniqueID());
500 return;
501 }
502 // Stop going backwards if we would cause a painter's order violation.
bsalomondb4758c2015-11-23 11:14:20 -0800503 // TODO: The bounds used here do not fully consider the clip. It may be advantageous
504 // to clip each batch's bounds to the clip.
bsalomon512be532015-09-10 10:42:55 -0700505 if (intersect(candidate->bounds(), batch->bounds())) {
506 GrBATCH_INFO("\t\tIntersects with (%s, B%u)\n", candidate->name(),
507 candidate->uniqueID());
508 break;
509 }
510 ++i;
511 if (i == maxCandidates) {
512 GrBATCH_INFO("\t\tReached max lookback or beginning of batch array %d\n", i);
513 break;
514 }
515 }
516 } else {
517 GrBATCH_INFO("\t\tFirstBatch\n");
518 }
519 fBatches.push_back().reset(SkRef(batch));
520}
521
egdaniele36914c2015-02-13 09:00:33 -0800522///////////////////////////////////////////////////////////////////////////////
523
bsalomonad792c12015-09-10 11:10:50 -0700524bool GrDrawTarget::installPipelineInDrawBatch(const GrPipelineBuilder* pipelineBuilder,
cdaltond4727922015-11-10 12:49:06 -0800525 const GrScissorState* scissor,
526 GrDrawBatch* batch) {
bsalomonad792c12015-09-10 11:10:50 -0700527 GrPipeline::CreateArgs args;
528 args.fPipelineBuilder = pipelineBuilder;
529 args.fCaps = this->caps();
cdaltond4727922015-11-10 12:49:06 -0800530 args.fScissor = scissor;
ethannicholasff210322015-11-24 12:10:10 -0800531 batch->getPipelineOptimizations(&args.fOpts);
532 args.fOpts.fColorPOI.completeCalculations(pipelineBuilder->fColorFragmentProcessors.begin(),
533 pipelineBuilder->numColorFragmentProcessors());
534 args.fOpts.fCoveragePOI.completeCalculations(
535 pipelineBuilder->fCoverageFragmentProcessors.begin(),
536 pipelineBuilder->numCoverageFragmentProcessors());
ethannicholasde4166a2015-11-30 08:57:38 -0800537 if (!this->setupDstReadIfNecessary(*pipelineBuilder, args.fOpts, &args.fDstTexture,
bsalomonad792c12015-09-10 11:10:50 -0700538 batch->bounds())) {
539 return false;
egdaniele36914c2015-02-13 09:00:33 -0800540 }
bsalomonad792c12015-09-10 11:10:50 -0700541
542 if (!batch->installPipeline(args)) {
543 return false;
544 }
545
546 return true;
egdaniele36914c2015-02-13 09:00:33 -0800547}
548
bsalomonb3b9aec2015-09-10 11:16:35 -0700549void GrDrawTarget::clearStencilClip(const SkIRect& rect, bool insideClip, GrRenderTarget* rt) {
halcanary385fe4d2015-08-26 13:07:48 -0700550 GrBatch* batch = new GrClearStencilClipBatch(rect, insideClip, rt);
bsalomon512be532015-09-10 10:42:55 -0700551 this->recordBatch(batch);
bsalomon5ea03632015-08-18 10:33:30 -0700552 batch->unref();
553}