blob: 7b1464ca7fb94d3151d785dc1c57d03e4eafd68a [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
robertphillips498d7ac2015-10-30 10:11:30 -070035GrDrawTarget::GrDrawTarget(GrRenderTarget* rt, GrGpu* gpu, GrResourceProvider* resourceProvider,
bsalomon648c6962015-10-23 09:06:59 -070036 const Options& options)
bsalomon4061b122015-05-29 10:26:19 -070037 : fGpu(SkRef(gpu))
bsalomon4061b122015-05-29 10:26:19 -070038 , fResourceProvider(resourceProvider)
joshualittf6d259b2015-10-02 11:27:14 -070039 , fFlushState(fGpu, fResourceProvider, 0)
bsalomon512be532015-09-10 10:42:55 -070040 , fFlushing(false)
robertphillipsa106c622015-10-16 09:07:06 -070041 , fFirstUnpreparedBatch(0)
bsalomon648c6962015-10-23 09:06:59 -070042 , fFlags(0)
robertphillips498d7ac2015-10-30 10:11:30 -070043 , fOptions(options)
44 , fRenderTarget(rt) {
bsalomonb3b9aec2015-09-10 11:16:35 -070045 // TODO: Stop extracting the context (currently needed by GrClipMaskManager)
46 fContext = fGpu->getContext();
Brian Salomone66fec22015-09-10 14:40:20 -040047 fClipMaskManager.reset(new GrClipMaskManager(this));
robertphillips4beb5c12015-10-20 07:50:00 -070048
49#ifdef SK_DEBUG
50 static int debugID = 0;
51 fDebugID = debugID++;
52#endif
bsalomon4061b122015-05-29 10:26:19 -070053}
54
55GrDrawTarget::~GrDrawTarget() {
robertphillips498d7ac2015-10-30 10:11:30 -070056 if (fRenderTarget && this == fRenderTarget->getLastDrawTarget()) {
57 fRenderTarget->setLastDrawTarget(nullptr);
58 }
59
bsalomon4061b122015-05-29 10:26:19 -070060 fGpu->unref();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000061}
62
63////////////////////////////////////////////////////////////////////////////////
64
robertphillips6a186652015-10-20 07:37:58 -070065// Add a GrDrawTarget-based dependency
66void GrDrawTarget::addDependency(GrDrawTarget* dependedOn) {
67 SkASSERT(!dependedOn->dependsOn(this)); // loops are bad
68
69 if (this->dependsOn(dependedOn)) {
70 return; // don't add duplicate dependencies
71 }
72
73 *fDependencies.push() = dependedOn;
74}
75
76// Convert from a GrSurface-based dependency to a GrDrawTarget one
77void GrDrawTarget::addDependency(GrSurface* dependedOn) {
78 if (dependedOn->asRenderTarget() && dependedOn->asRenderTarget()->getLastDrawTarget()) {
79 // If it is still receiving dependencies, this DT shouldn't be closed
80 SkASSERT(!this->isClosed());
81
82 GrDrawTarget* dt = dependedOn->asRenderTarget()->getLastDrawTarget();
83 if (dt == this) {
84 // self-read - presumably for dst reads
85 } else {
86 this->addDependency(dt);
87
88 // Can't make it closed in the self-read case
89 dt->makeClosed();
90 }
91 }
92}
93
robertphillips4beb5c12015-10-20 07:50:00 -070094#ifdef SK_DEBUG
95void GrDrawTarget::dump() const {
96 SkDebugf("--------------------------------------------------------------\n");
97 SkDebugf("node: %d\n");
98 SkDebugf("relies On (%d): ", fDependencies.count());
99 for (int i = 0; i < fDependencies.count(); ++i) {
100 SkDebugf("%d, ", fDependencies[i]->fDebugID);
101 }
102 SkDebugf("\n");
103 SkDebugf("batches (%d):\n", fBatches.count());
104 for (int i = 0; i < fBatches.count(); ++i) {
105#if 0
106 SkDebugf("*******************************\n");
107#endif
108 SkDebugf("%d: %s\n", i, fBatches[i]->name());
109#if 0
110 SkString str = fBatches[i]->dumpInfo();
111 SkDebugf("%s\n", str.c_str());
112#endif
113 }
114}
115#endif
116
bsalomon50785a32015-02-06 07:02:37 -0800117bool GrDrawTarget::setupDstReadIfNecessary(const GrPipelineBuilder& pipelineBuilder,
egdaniele36914c2015-02-13 09:00:33 -0800118 const GrProcOptInfo& colorPOI,
119 const GrProcOptInfo& coveragePOI,
bsalomon6a44c6a2015-05-26 09:49:05 -0700120 GrXferProcessor::DstTexture* dstTexture,
bsalomonad792c12015-09-10 11:10:50 -0700121 const SkRect& batchBounds) {
122 SkRect bounds = batchBounds;
123 bounds.outset(0.5f, 0.5f);
124
bsalomon6a44c6a2015-05-26 09:49:05 -0700125 if (!pipelineBuilder.willXPNeedDstTexture(*this->caps(), colorPOI, coveragePOI)) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000126 return true;
127 }
cdalton9954bc32015-04-29 14:17:00 -0700128
bsalomon50785a32015-02-06 07:02:37 -0800129 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
cdalton9954bc32015-04-29 14:17:00 -0700130
131 if (this->caps()->textureBarrierSupport()) {
132 if (GrTexture* rtTex = rt->asTexture()) {
bsalomondc47ff72015-05-26 12:16:59 -0700133 // The render target is a texture, so we can read from it directly in the shader. The XP
cdalton9954bc32015-04-29 14:17:00 -0700134 // will be responsible to detect this situation and request a texture barrier.
bsalomon6a44c6a2015-05-26 09:49:05 -0700135 dstTexture->setTexture(rtTex);
136 dstTexture->setOffset(0, 0);
cdalton9954bc32015-04-29 14:17:00 -0700137 return true;
138 }
139 }
140
141 SkIRect copyRect;
joshualitt44701df2015-02-23 14:44:57 -0800142 pipelineBuilder.clip().getConservativeBounds(rt, &copyRect);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000143
bsalomonad792c12015-09-10 11:10:50 -0700144 SkIRect drawIBounds;
145 bounds.roundOut(&drawIBounds);
146 if (!copyRect.intersect(drawIBounds)) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000147#ifdef SK_DEBUG
bsalomonb3b9aec2015-09-10 11:16:35 -0700148 GrCapsDebugf(this->caps(), "Missed an early reject. "
149 "Bailing on draw from setupDstReadIfNecessary.\n");
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000150#endif
bsalomonad792c12015-09-10 11:10:50 -0700151 return false;
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000152 }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000153
commit-bot@chromium.org63150af2013-04-11 22:00:22 +0000154 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
155 // have per-sample dst values by making the copy multisampled.
bsalomonf2703d82014-10-28 14:33:06 -0700156 GrSurfaceDesc desc;
bsalomonb3b9aec2015-09-10 11:16:35 -0700157 if (!fGpu->initCopySurfaceDstDesc(rt, &desc)) {
bsalomona73239a2015-04-28 13:35:17 -0700158 desc.fOrigin = kDefault_GrSurfaceOrigin;
159 desc.fFlags = kRenderTarget_GrSurfaceFlag;
160 desc.fConfig = rt->config();
161 }
162
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000163 desc.fWidth = copyRect.width();
164 desc.fHeight = copyRect.height();
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000165
bsalomoneae62002015-07-31 13:59:30 -0700166 static const uint32_t kFlags = 0;
167 SkAutoTUnref<GrTexture> copy(fResourceProvider->createApproxTexture(desc, kFlags));
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000168
bsalomone3059732014-10-14 11:47:22 -0700169 if (!copy) {
tfarina38406c82014-10-31 07:11:12 -0700170 SkDebugf("Failed to create temporary copy of destination texture.\n");
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000171 return false;
172 }
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000173 SkIPoint dstPoint = {0, 0};
bsalomon6df86402015-06-01 10:41:49 -0700174 this->copySurface(copy, rt, copyRect, dstPoint);
175 dstTexture->setTexture(copy);
176 dstTexture->setOffset(copyRect.fLeft, copyRect.fTop);
177 return true;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000178}
179
bsalomona73239a2015-04-28 13:35:17 -0700180void GrDrawTarget::flush() {
181 if (fFlushing) {
182 return;
183 }
184 fFlushing = true;
185
robertphillipsa106c622015-10-16 09:07:06 -0700186 // Semi-usually the drawTargets are already closed at this point, but sometimes Ganesh
187 // needs to flush mid-draw. In that case, the SkGpuDevice's drawTargets won't be closed
188 // but need to be flushed anyway. Closing such drawTargets here will mean new
189 // drawTargets will be created to replace them if the SkGpuDevice(s) write to them again.
190 this->makeClosed();
191
robertphillips498d7ac2015-10-30 10:11:30 -0700192 // Loop over the batches that haven't yet generated their geometry
joshualittf6d259b2015-10-02 11:27:14 -0700193 for (; fFirstUnpreparedBatch < fBatches.count(); ++fFirstUnpreparedBatch) {
194 fBatches[fFirstUnpreparedBatch]->prepare(&fFlushState);
bsalomon512be532015-09-10 10:42:55 -0700195 }
196
197 // Upload all data to the GPU
joshualittf6d259b2015-10-02 11:27:14 -0700198 fFlushState.preIssueDraws();
bsalomon512be532015-09-10 10:42:55 -0700199
200 // Draw all the generated geometry.
201 for (int i = 0; i < fBatches.count(); ++i) {
joshualittf6d259b2015-10-02 11:27:14 -0700202 fBatches[i]->draw(&fFlushState);
bsalomon512be532015-09-10 10:42:55 -0700203 }
204
joshualittf6d259b2015-10-02 11:27:14 -0700205 SkASSERT(fFlushState.lastFlushedToken() == fFlushState.currentToken());
206 this->reset();
bsalomona73239a2015-04-28 13:35:17 -0700207
bsalomona73239a2015-04-28 13:35:17 -0700208 fFlushing = false;
bsalomona73239a2015-04-28 13:35:17 -0700209}
210
bsalomon512be532015-09-10 10:42:55 -0700211void GrDrawTarget::reset() {
joshualittf6d259b2015-10-02 11:27:14 -0700212 fFirstUnpreparedBatch = 0;
bsalomon512be532015-09-10 10:42:55 -0700213 fBatches.reset();
joshualittf6d259b2015-10-02 11:27:14 -0700214 fFlushState.reset();
bsalomon512be532015-09-10 10:42:55 -0700215}
216
bsalomonabd30f52015-08-13 13:34:48 -0700217void GrDrawTarget::drawBatch(const GrPipelineBuilder& pipelineBuilder, GrDrawBatch* batch) {
joshualitt4d8da812015-01-28 12:53:54 -0800218 // Setup clip
joshualitt4d8da812015-01-28 12:53:54 -0800219 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomon0ba8c242015-10-07 09:20:28 -0700220 GrAppliedClip clip;
bsalomone91f7b52015-10-27 06:42:50 -0700221 if (!fClipMaskManager->setupClipping(pipelineBuilder, &ars, &batch->bounds(), &clip)) {
joshualitt4d8da812015-01-28 12:53:54 -0800222 return;
223 }
bsalomon0ba8c242015-10-07 09:20:28 -0700224 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
225 if (clip.clipCoverageFragmentProcessor()) {
226 arfps.set(&pipelineBuilder);
227 arfps.addCoverageFragmentProcessor(clip.clipCoverageFragmentProcessor());
228 }
joshualitt4d8da812015-01-28 12:53:54 -0800229
bsalomonad792c12015-09-10 11:10:50 -0700230 GrPipeline::CreateArgs args;
bsalomone91f7b52015-10-27 06:42:50 -0700231 if (!this->installPipelineInDrawBatch(&pipelineBuilder, &clip.scissorState(), batch)) {
egdaniele36914c2015-02-13 09:00:33 -0800232 return;
233 }
bsalomonad792c12015-09-10 11:10:50 -0700234
robertphillips498d7ac2015-10-30 10:11:30 -0700235#ifdef ENABLE_MDB
236 SkASSERT(fRenderTarget);
237 batch->pipeline()->addDependenciesTo(fRenderTarget);
238#endif
239
bsalomon512be532015-09-10 10:42:55 -0700240 this->recordBatch(batch);
joshualitt4d8da812015-01-28 12:53:54 -0800241}
242
joshualitt2c93efe2014-11-06 12:57:13 -0800243static const GrStencilSettings& winding_path_stencil_settings() {
244 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
245 kIncClamp_StencilOp,
246 kIncClamp_StencilOp,
247 kAlwaysIfInClip_StencilFunc,
248 0xFFFF, 0xFFFF, 0xFFFF);
249 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
250}
251
252static const GrStencilSettings& even_odd_path_stencil_settings() {
253 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
254 kInvert_StencilOp,
255 kInvert_StencilOp,
256 kAlwaysIfInClip_StencilFunc,
257 0xFFFF, 0xFFFF, 0xFFFF);
258 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
259}
260
261void GrDrawTarget::getPathStencilSettingsForFilltype(GrPathRendering::FillType fill,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700262 const GrStencilAttachment* sb,
joshualitt2c93efe2014-11-06 12:57:13 -0800263 GrStencilSettings* outStencilSettings) {
264
265 switch (fill) {
266 default:
267 SkFAIL("Unexpected path fill.");
268 case GrPathRendering::kWinding_FillType:
269 *outStencilSettings = winding_path_stencil_settings();
270 break;
271 case GrPathRendering::kEvenOdd_FillType:
272 *outStencilSettings = even_odd_path_stencil_settings();
273 break;
274 }
bsalomonb3b9aec2015-09-10 11:16:35 -0700275 fClipMaskManager->adjustPathStencilParams(sb, outStencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800276}
277
joshualitt1c735482015-07-13 08:08:25 -0700278void GrDrawTarget::stencilPath(const GrPipelineBuilder& pipelineBuilder,
joshualittf2384692015-09-10 11:00:51 -0700279 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800280 const GrPath* path,
281 GrPathRendering::FillType fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000282 // TODO: extract portions of checkDraw that are relevant to path stenciling.
bsalomon49f085d2014-09-05 13:34:00 -0700283 SkASSERT(path);
jvanverthe9c0fc62015-04-29 11:18:05 -0700284 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
joshualitt2c93efe2014-11-06 12:57:13 -0800285
286 // Setup clip
egdaniel8dd688b2015-01-22 10:16:09 -0800287 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomon0ba8c242015-10-07 09:20:28 -0700288 GrAppliedClip clip;
bsalomone91f7b52015-10-27 06:42:50 -0700289 if (!fClipMaskManager->setupClipping(pipelineBuilder, &ars, nullptr, &clip)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800290 return;
291 }
292
bsalomon0ba8c242015-10-07 09:20:28 -0700293 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
294 if (clip.clipCoverageFragmentProcessor()) {
295 arfps.set(&pipelineBuilder);
296 arfps.addCoverageFragmentProcessor(clip.clipCoverageFragmentProcessor());
297 }
298
joshualitt2c93efe2014-11-06 12:57:13 -0800299 // set stencil settings for path
300 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700301 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdanielec00d942015-09-14 12:56:10 -0700302 GrStencilAttachment* sb = fResourceProvider->attachStencilAttachment(rt);
bsalomon6bc1b5f2015-02-23 09:06:38 -0800303 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800304
joshualittf2384692015-09-10 11:00:51 -0700305 GrBatch* batch = GrStencilPathBatch::Create(viewMatrix,
bsalomona44919e2015-08-18 13:28:19 -0700306 pipelineBuilder.isHWAntialias(),
bsalomone91f7b52015-10-27 06:42:50 -0700307 stencilSettings, clip.scissorState(),
bsalomona44919e2015-08-18 13:28:19 -0700308 pipelineBuilder.getRenderTarget(),
309 path);
bsalomon512be532015-09-10 10:42:55 -0700310 this->recordBatch(batch);
bsalomona44919e2015-08-18 13:28:19 -0700311 batch->unref();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000312}
313
joshualitt1c735482015-07-13 08:08:25 -0700314void GrDrawTarget::drawPath(const GrPipelineBuilder& pipelineBuilder,
joshualittf2384692015-09-10 11:00:51 -0700315 const SkMatrix& viewMatrix,
316 GrColor color,
joshualitt9853cce2014-11-17 14:22:48 -0800317 const GrPath* path,
318 GrPathRendering::FillType fill) {
bsalomon49f085d2014-09-05 13:34:00 -0700319 SkASSERT(path);
jvanverthe9c0fc62015-04-29 11:18:05 -0700320 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000321
joshualittf2384692015-09-10 11:00:51 -0700322 GrDrawPathBatchBase* batch = GrDrawPathBatch::Create(viewMatrix, color, path);
bsalomon1fcc01c2015-09-09 09:48:06 -0700323 this->drawPathBatch(pipelineBuilder, batch, fill);
324 batch->unref();
325}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000326
bsalomon1fcc01c2015-09-09 09:48:06 -0700327void GrDrawTarget::drawPathsFromRange(const GrPipelineBuilder& pipelineBuilder,
joshualittf2384692015-09-10 11:00:51 -0700328 const SkMatrix& viewMatrix,
329 const SkMatrix& localMatrix,
330 GrColor color,
cdalton8585dd22015-10-08 08:04:09 -0700331 GrPathRange* range,
bsalomon1fcc01c2015-09-09 09:48:06 -0700332 GrPathRangeDraw* draw,
333 GrPathRendering::FillType fill) {
cdalton8585dd22015-10-08 08:04:09 -0700334 GrDrawPathBatchBase* batch = GrDrawPathRangeBatch::Create(viewMatrix, localMatrix, color,
335 range, draw);
bsalomon1fcc01c2015-09-09 09:48:06 -0700336 this->drawPathBatch(pipelineBuilder, batch, fill);
337 batch->unref();
338}
339
340void GrDrawTarget::drawPathBatch(const GrPipelineBuilder& pipelineBuilder,
341 GrDrawPathBatchBase* batch,
342 GrPathRendering::FillType fill) {
bsalomonadd79ef2015-08-19 13:26:49 -0700343 // This looks like drawBatch() but there is an added wrinkle that stencil settings get inserted
bsalomonb3b9aec2015-09-10 11:16:35 -0700344 // after setting up clipping but before onDrawBatch(). TODO: Figure out a better model for
345 // handling stencil settings WRT interactions between pipeline(builder), clipmaskmanager, and
346 // batches.
bsalomonadd79ef2015-08-19 13:26:49 -0700347
egdaniel8dd688b2015-01-22 10:16:09 -0800348 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomon0ba8c242015-10-07 09:20:28 -0700349 GrAppliedClip clip;
bsalomone91f7b52015-10-27 06:42:50 -0700350 if (!fClipMaskManager->setupClipping(pipelineBuilder, &ars, &batch->bounds(), &clip)) {
bsalomonadd79ef2015-08-19 13:26:49 -0700351 return;
joshualitt2c93efe2014-11-06 12:57:13 -0800352 }
353
bsalomon0ba8c242015-10-07 09:20:28 -0700354 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
355 if (clip.clipCoverageFragmentProcessor()) {
356 arfps.set(&pipelineBuilder);
357 arfps.addCoverageFragmentProcessor(clip.clipCoverageFragmentProcessor());
358 }
359
bsalomonadd79ef2015-08-19 13:26:49 -0700360 // Ensure the render target has a stencil buffer and get the stencil settings.
joshualitt2c93efe2014-11-06 12:57:13 -0800361 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700362 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdanielec00d942015-09-14 12:56:10 -0700363 GrStencilAttachment* sb = fResourceProvider->attachStencilAttachment(rt);
bsalomon6bc1b5f2015-02-23 09:06:38 -0800364 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
bsalomonadd79ef2015-08-19 13:26:49 -0700365 batch->setStencilSettings(stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800366
bsalomonad792c12015-09-10 11:10:50 -0700367 GrPipeline::CreateArgs args;
bsalomone91f7b52015-10-27 06:42:50 -0700368 if (!this->installPipelineInDrawBatch(&pipelineBuilder, &clip.scissorState(), batch)) {
bsalomonadd79ef2015-08-19 13:26:49 -0700369 return;
370 }
egdaniele36914c2015-02-13 09:00:33 -0800371
bsalomon512be532015-09-10 10:42:55 -0700372 this->recordBatch(batch);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000373}
374
joshualittd2b23e02015-08-21 10:53:34 -0700375void GrDrawTarget::drawNonAARect(const GrPipelineBuilder& pipelineBuilder,
joshualitt1c735482015-07-13 08:08:25 -0700376 GrColor color,
377 const SkMatrix& viewMatrix,
joshualittb6b513b2015-08-21 10:25:18 -0700378 const SkRect& rect) {
joshualittd2b23e02015-08-21 10:53:34 -0700379 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect,
380 nullptr, nullptr));
joshualittad17cfc2015-05-05 10:45:57 -0700381 this->drawBatch(pipelineBuilder, batch);
382}
383
joshualittd2b23e02015-08-21 10:53:34 -0700384void GrDrawTarget::drawNonAARect(const GrPipelineBuilder& pipelineBuilder,
joshualittb6b513b2015-08-21 10:25:18 -0700385 GrColor color,
386 const SkMatrix& viewMatrix,
387 const SkRect& rect,
388 const SkMatrix& localMatrix) {
joshualittd2b23e02015-08-21 10:53:34 -0700389 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect,
390 nullptr, &localMatrix));
joshualittb6b513b2015-08-21 10:25:18 -0700391 this->drawBatch(pipelineBuilder, batch);
392}
393
joshualittd2b23e02015-08-21 10:53:34 -0700394void GrDrawTarget::drawNonAARect(const GrPipelineBuilder& pipelineBuilder,
joshualittb6b513b2015-08-21 10:25:18 -0700395 GrColor color,
396 const SkMatrix& viewMatrix,
397 const SkRect& rect,
398 const SkRect& localRect) {
joshualittd2b23e02015-08-21 10:53:34 -0700399 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect,
400 &localRect, nullptr));
joshualittb6b513b2015-08-21 10:25:18 -0700401 this->drawBatch(pipelineBuilder, batch);
402}
403
404
joshualitt1c735482015-07-13 08:08:25 -0700405void GrDrawTarget::drawAARect(const GrPipelineBuilder& pipelineBuilder,
robertphillipsea461502015-05-26 11:38:03 -0700406 GrColor color,
407 const SkMatrix& viewMatrix,
408 const SkRect& rect,
409 const SkRect& devRect) {
joshualittd2b23e02015-08-21 10:53:34 -0700410 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateAAFill(color, viewMatrix, rect,
bsalomonabd30f52015-08-13 13:34:48 -0700411 devRect));
joshualitt14205b12015-08-10 11:40:56 -0700412 this->drawBatch(pipelineBuilder, batch);
robertphillipsea461502015-05-26 11:38:03 -0700413}
414
joshualitt9853cce2014-11-17 14:22:48 -0800415void GrDrawTarget::clear(const SkIRect* rect,
416 GrColor color,
417 bool canIgnoreRect,
bsalomon63b21962014-11-05 07:05:34 -0800418 GrRenderTarget* renderTarget) {
egdaniel51c8d402015-08-06 10:54:13 -0700419 SkIRect rtRect = SkIRect::MakeWH(renderTarget->width(), renderTarget->height());
420 SkIRect clippedRect;
421 if (!rect ||
422 (canIgnoreRect && this->caps()->fullClearIsFree()) ||
423 rect->contains(rtRect)) {
424 rect = &rtRect;
425 } else {
426 clippedRect = *rect;
427 if (!clippedRect.intersect(rtRect)) {
428 return;
429 }
430 rect = &clippedRect;
431 }
432
bsalomonb3b9aec2015-09-10 11:16:35 -0700433 if (this->caps()->useDrawInsteadOfClear()) {
bsalomon63b21962014-11-05 07:05:34 -0800434 // This works around a driver bug with clear by drawing a rect instead.
435 // The driver will ignore a clear if it is the only thing rendered to a
436 // target before the target is read.
egdaniel51c8d402015-08-06 10:54:13 -0700437 if (rect == &rtRect) {
bsalomon63b21962014-11-05 07:05:34 -0800438 this->discard(renderTarget);
439 }
bsalomon63b21962014-11-05 07:05:34 -0800440
egdaniel8dd688b2015-01-22 10:16:09 -0800441 GrPipelineBuilder pipelineBuilder;
442 pipelineBuilder.setRenderTarget(renderTarget);
joshualitt9853cce2014-11-17 14:22:48 -0800443
joshualittd2b23e02015-08-21 10:53:34 -0700444 this->drawNonAARect(pipelineBuilder, color, SkMatrix::I(), *rect);
bsalomon53469832015-08-18 09:20:09 -0700445 } else {
halcanary385fe4d2015-08-26 13:07:48 -0700446 GrBatch* batch = new GrClearBatch(*rect, color, renderTarget);
bsalomon512be532015-09-10 10:42:55 -0700447 this->recordBatch(batch);
bsalomon53469832015-08-18 09:20:09 -0700448 batch->unref();
449 }
450}
451
452void GrDrawTarget::discard(GrRenderTarget* renderTarget) {
453 if (this->caps()->discardRenderTargetSupport()) {
halcanary385fe4d2015-08-26 13:07:48 -0700454 GrBatch* batch = new GrDiscardBatch(renderTarget);
bsalomon512be532015-09-10 10:42:55 -0700455 this->recordBatch(batch);
bsalomon53469832015-08-18 09:20:09 -0700456 batch->unref();
bsalomon63b21962014-11-05 07:05:34 -0800457 }
458}
459
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000460////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000461
bsalomon6df86402015-06-01 10:41:49 -0700462void GrDrawTarget::copySurface(GrSurface* dst,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000463 GrSurface* src,
464 const SkIRect& srcRect,
465 const SkIPoint& dstPoint) {
bsalomon872062c2015-08-18 12:12:35 -0700466 GrBatch* batch = GrCopySurfaceBatch::Create(dst, src, srcRect, dstPoint);
467 if (batch) {
robertphillips498d7ac2015-10-30 10:11:30 -0700468#ifdef ENABLE_MDB
469 this->addDependency(src);
470#endif
471
bsalomon512be532015-09-10 10:42:55 -0700472 this->recordBatch(batch);
bsalomon872062c2015-08-18 12:12:35 -0700473 batch->unref();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000474 }
bsalomon@google.comeb851172013-04-15 13:51:00 +0000475}
476
bsalomon512be532015-09-10 10:42:55 -0700477template <class Left, class Right> static bool intersect(const Left& a, const Right& b) {
478 SkASSERT(a.fLeft <= a.fRight && a.fTop <= a.fBottom &&
479 b.fLeft <= b.fRight && b.fTop <= b.fBottom);
480 return a.fLeft < b.fRight && b.fLeft < a.fRight && a.fTop < b.fBottom && b.fTop < a.fBottom;
481}
482
483void GrDrawTarget::recordBatch(GrBatch* batch) {
robertphillipsa106c622015-10-16 09:07:06 -0700484 // A closed drawTarget should never receive new/more batches
robertphillips6a186652015-10-20 07:37:58 -0700485 SkASSERT(!this->isClosed());
bsalomon648c6962015-10-23 09:06:59 -0700486 // Should never have batches queued up when in immediate mode.
487 SkASSERT(!fOptions.fImmediateMode || !fBatches.count());
robertphillipsa106c622015-10-16 09:07:06 -0700488
bsalomon512be532015-09-10 10:42:55 -0700489 // Check if there is a Batch Draw we can batch with by linearly searching back until we either
490 // 1) check every draw
491 // 2) intersect with something
492 // 3) find a 'blocker'
493 // Experimentally we have found that most batching occurs within the first 10 comparisons.
494 static const int kMaxLookback = 10;
495
496 GrBATCH_INFO("Re-Recording (%s, B%u)\n"
joshualitte2bcec32015-09-30 06:22:22 -0700497 "\tBounds LRTB (%f, %f, %f, %f)\n",
bsalomon512be532015-09-10 10:42:55 -0700498 batch->name(),
499 batch->uniqueID(),
500 batch->bounds().fLeft, batch->bounds().fRight,
501 batch->bounds().fTop, batch->bounds().fBottom);
502 GrBATCH_INFO(SkTabString(batch->dumpInfo(), 1).c_str());
503 GrBATCH_INFO("\tOutcome:\n");
504 int maxCandidates = SkTMin(kMaxLookback, fBatches.count());
505 if (maxCandidates) {
506 int i = 0;
507 while (true) {
508 GrBatch* candidate = fBatches.fromBack(i);
509 // We cannot continue to search backwards if the render target changes
510 if (candidate->renderTargetUniqueID() != batch->renderTargetUniqueID()) {
511 GrBATCH_INFO("\t\tBreaking because of (%s, B%u) Rendertarget\n",
512 candidate->name(), candidate->uniqueID());
513 break;
514 }
515 if (candidate->combineIfPossible(batch, *this->caps())) {
516 GrBATCH_INFO("\t\tCombining with (%s, B%u)\n", candidate->name(),
517 candidate->uniqueID());
518 return;
519 }
520 // Stop going backwards if we would cause a painter's order violation.
521 if (intersect(candidate->bounds(), batch->bounds())) {
522 GrBATCH_INFO("\t\tIntersects with (%s, B%u)\n", candidate->name(),
523 candidate->uniqueID());
524 break;
525 }
526 ++i;
527 if (i == maxCandidates) {
528 GrBATCH_INFO("\t\tReached max lookback or beginning of batch array %d\n", i);
529 break;
530 }
531 }
532 } else {
533 GrBATCH_INFO("\t\tFirstBatch\n");
534 }
535 fBatches.push_back().reset(SkRef(batch));
joshualittf6d259b2015-10-02 11:27:14 -0700536 if (fBatches.count() > kMaxLookback) {
537 SkASSERT(fBatches.count() - kMaxLookback - fFirstUnpreparedBatch == 1);
538 fBatches[fFirstUnpreparedBatch++]->prepare(&fFlushState);
539 }
bsalomon648c6962015-10-23 09:06:59 -0700540 if (fOptions.fImmediateMode) {
541 this->flush();
542 }
bsalomon512be532015-09-10 10:42:55 -0700543}
544
egdaniele36914c2015-02-13 09:00:33 -0800545///////////////////////////////////////////////////////////////////////////////
546
bsalomonad792c12015-09-10 11:10:50 -0700547bool GrDrawTarget::installPipelineInDrawBatch(const GrPipelineBuilder* pipelineBuilder,
548 const GrScissorState* scissor,
549 GrDrawBatch* batch) {
550 GrPipeline::CreateArgs args;
551 args.fPipelineBuilder = pipelineBuilder;
552 args.fCaps = this->caps();
553 args.fScissor = scissor;
554 args.fColorPOI = pipelineBuilder->colorProcInfo(batch);
555 args.fCoveragePOI = pipelineBuilder->coverageProcInfo(batch);
556 if (!this->setupDstReadIfNecessary(*pipelineBuilder, args.fColorPOI,
557 args.fCoveragePOI, &args.fDstTexture,
558 batch->bounds())) {
559 return false;
egdaniele36914c2015-02-13 09:00:33 -0800560 }
bsalomonad792c12015-09-10 11:10:50 -0700561
562 if (!batch->installPipeline(args)) {
563 return false;
564 }
565
566 return true;
egdaniele36914c2015-02-13 09:00:33 -0800567}
568
bsalomonb3b9aec2015-09-10 11:16:35 -0700569void GrDrawTarget::clearStencilClip(const SkIRect& rect, bool insideClip, GrRenderTarget* rt) {
halcanary385fe4d2015-08-26 13:07:48 -0700570 GrBatch* batch = new GrClearStencilClipBatch(rect, insideClip, rt);
bsalomon512be532015-09-10 10:42:55 -0700571 this->recordBatch(batch);
bsalomon5ea03632015-08-18 10:33:30 -0700572 batch->unref();
573}