blob: 3f898ab26a2061c509b0e5502fa25fe511246081 [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
robertphillipsa13e2022015-11-11 12:01:09 -080035GrDrawTarget::GrDrawTarget(GrRenderTarget* rt, GrGpu* gpu, GrResourceProvider* resourceProvider)
bsalomon4061b122015-05-29 10:26:19 -070036 : fGpu(SkRef(gpu))
bsalomon4061b122015-05-29 10:26:19 -070037 , fResourceProvider(resourceProvider)
bsalomon512be532015-09-10 10:42:55 -070038 , fFlushing(false)
bsalomon648c6962015-10-23 09:06:59 -070039 , fFlags(0)
robertphillips498d7ac2015-10-30 10:11:30 -070040 , fRenderTarget(rt) {
bsalomonb3b9aec2015-09-10 11:16:35 -070041 // TODO: Stop extracting the context (currently needed by GrClipMaskManager)
42 fContext = fGpu->getContext();
Brian Salomone66fec22015-09-10 14:40:20 -040043 fClipMaskManager.reset(new GrClipMaskManager(this));
robertphillips4beb5c12015-10-20 07:50:00 -070044
45#ifdef SK_DEBUG
46 static int debugID = 0;
47 fDebugID = debugID++;
48#endif
bsalomon4061b122015-05-29 10:26:19 -070049}
50
51GrDrawTarget::~GrDrawTarget() {
robertphillips498d7ac2015-10-30 10:11:30 -070052 if (fRenderTarget && this == fRenderTarget->getLastDrawTarget()) {
53 fRenderTarget->setLastDrawTarget(nullptr);
54 }
55
bsalomon4061b122015-05-29 10:26:19 -070056 fGpu->unref();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000057}
58
59////////////////////////////////////////////////////////////////////////////////
60
robertphillips6a186652015-10-20 07:37:58 -070061// Add a GrDrawTarget-based dependency
62void GrDrawTarget::addDependency(GrDrawTarget* dependedOn) {
63 SkASSERT(!dependedOn->dependsOn(this)); // loops are bad
64
65 if (this->dependsOn(dependedOn)) {
66 return; // don't add duplicate dependencies
67 }
68
69 *fDependencies.push() = dependedOn;
70}
71
72// Convert from a GrSurface-based dependency to a GrDrawTarget one
73void GrDrawTarget::addDependency(GrSurface* dependedOn) {
74 if (dependedOn->asRenderTarget() && dependedOn->asRenderTarget()->getLastDrawTarget()) {
75 // If it is still receiving dependencies, this DT shouldn't be closed
76 SkASSERT(!this->isClosed());
77
78 GrDrawTarget* dt = dependedOn->asRenderTarget()->getLastDrawTarget();
79 if (dt == this) {
80 // self-read - presumably for dst reads
81 } else {
82 this->addDependency(dt);
83
84 // Can't make it closed in the self-read case
85 dt->makeClosed();
86 }
87 }
88}
89
robertphillips4beb5c12015-10-20 07:50:00 -070090#ifdef SK_DEBUG
91void GrDrawTarget::dump() const {
92 SkDebugf("--------------------------------------------------------------\n");
93 SkDebugf("node: %d\n");
94 SkDebugf("relies On (%d): ", fDependencies.count());
95 for (int i = 0; i < fDependencies.count(); ++i) {
96 SkDebugf("%d, ", fDependencies[i]->fDebugID);
97 }
98 SkDebugf("\n");
99 SkDebugf("batches (%d):\n", fBatches.count());
100 for (int i = 0; i < fBatches.count(); ++i) {
101#if 0
102 SkDebugf("*******************************\n");
103#endif
104 SkDebugf("%d: %s\n", i, fBatches[i]->name());
105#if 0
106 SkString str = fBatches[i]->dumpInfo();
107 SkDebugf("%s\n", str.c_str());
108#endif
109 }
110}
111#endif
112
bsalomon50785a32015-02-06 07:02:37 -0800113bool GrDrawTarget::setupDstReadIfNecessary(const GrPipelineBuilder& pipelineBuilder,
egdaniele36914c2015-02-13 09:00:33 -0800114 const GrProcOptInfo& colorPOI,
115 const GrProcOptInfo& coveragePOI,
bsalomon6a44c6a2015-05-26 09:49:05 -0700116 GrXferProcessor::DstTexture* dstTexture,
bsalomonad792c12015-09-10 11:10:50 -0700117 const SkRect& batchBounds) {
118 SkRect bounds = batchBounds;
119 bounds.outset(0.5f, 0.5f);
120
bsalomon6a44c6a2015-05-26 09:49:05 -0700121 if (!pipelineBuilder.willXPNeedDstTexture(*this->caps(), colorPOI, coveragePOI)) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000122 return true;
123 }
cdalton9954bc32015-04-29 14:17:00 -0700124
bsalomon50785a32015-02-06 07:02:37 -0800125 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
cdalton9954bc32015-04-29 14:17:00 -0700126
127 if (this->caps()->textureBarrierSupport()) {
128 if (GrTexture* rtTex = rt->asTexture()) {
bsalomondc47ff72015-05-26 12:16:59 -0700129 // The render target is a texture, so we can read from it directly in the shader. The XP
cdalton9954bc32015-04-29 14:17:00 -0700130 // will be responsible to detect this situation and request a texture barrier.
bsalomon6a44c6a2015-05-26 09:49:05 -0700131 dstTexture->setTexture(rtTex);
132 dstTexture->setOffset(0, 0);
cdalton9954bc32015-04-29 14:17:00 -0700133 return true;
134 }
135 }
136
137 SkIRect copyRect;
joshualitt44701df2015-02-23 14:44:57 -0800138 pipelineBuilder.clip().getConservativeBounds(rt, &copyRect);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000139
bsalomonad792c12015-09-10 11:10:50 -0700140 SkIRect drawIBounds;
141 bounds.roundOut(&drawIBounds);
142 if (!copyRect.intersect(drawIBounds)) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000143#ifdef SK_DEBUG
bsalomonb3b9aec2015-09-10 11:16:35 -0700144 GrCapsDebugf(this->caps(), "Missed an early reject. "
145 "Bailing on draw from setupDstReadIfNecessary.\n");
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000146#endif
bsalomonad792c12015-09-10 11:10:50 -0700147 return false;
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000148 }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000149
commit-bot@chromium.org63150af2013-04-11 22:00:22 +0000150 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
151 // have per-sample dst values by making the copy multisampled.
bsalomonf2703d82014-10-28 14:33:06 -0700152 GrSurfaceDesc desc;
bsalomonb3b9aec2015-09-10 11:16:35 -0700153 if (!fGpu->initCopySurfaceDstDesc(rt, &desc)) {
bsalomona73239a2015-04-28 13:35:17 -0700154 desc.fOrigin = kDefault_GrSurfaceOrigin;
155 desc.fFlags = kRenderTarget_GrSurfaceFlag;
156 desc.fConfig = rt->config();
157 }
158
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000159 desc.fWidth = copyRect.width();
160 desc.fHeight = copyRect.height();
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000161
bsalomoneae62002015-07-31 13:59:30 -0700162 static const uint32_t kFlags = 0;
163 SkAutoTUnref<GrTexture> copy(fResourceProvider->createApproxTexture(desc, kFlags));
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000164
bsalomone3059732014-10-14 11:47:22 -0700165 if (!copy) {
tfarina38406c82014-10-31 07:11:12 -0700166 SkDebugf("Failed to create temporary copy of destination texture.\n");
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000167 return false;
168 }
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000169 SkIPoint dstPoint = {0, 0};
bsalomon6df86402015-06-01 10:41:49 -0700170 this->copySurface(copy, rt, copyRect, dstPoint);
171 dstTexture->setTexture(copy);
172 dstTexture->setOffset(copyRect.fLeft, copyRect.fTop);
173 return true;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000174}
175
robertphillipsa13e2022015-11-11 12:01:09 -0800176void GrDrawTarget::prepareBatches(GrBatchFlushState* flushState) {
bsalomona73239a2015-04-28 13:35:17 -0700177 if (fFlushing) {
178 return;
179 }
180 fFlushing = true;
181
robertphillipsa106c622015-10-16 09:07:06 -0700182 // Semi-usually the drawTargets are already closed at this point, but sometimes Ganesh
183 // needs to flush mid-draw. In that case, the SkGpuDevice's drawTargets won't be closed
184 // but need to be flushed anyway. Closing such drawTargets here will mean new
185 // drawTargets will be created to replace them if the SkGpuDevice(s) write to them again.
186 this->makeClosed();
187
robertphillips498d7ac2015-10-30 10:11:30 -0700188 // Loop over the batches that haven't yet generated their geometry
robertphillips1f0e3502015-11-10 10:19:50 -0800189 for (int i = 0; i < fBatches.count(); ++i) {
robertphillipsa13e2022015-11-11 12:01:09 -0800190 fBatches[i]->prepare(flushState);
bsalomon512be532015-09-10 10:42:55 -0700191 }
robertphillipsa13e2022015-11-11 12:01:09 -0800192}
bsalomon512be532015-09-10 10:42:55 -0700193
robertphillipsa13e2022015-11-11 12:01:09 -0800194void GrDrawTarget::drawBatches(GrBatchFlushState* flushState) {
bsalomon512be532015-09-10 10:42:55 -0700195 // Draw all the generated geometry.
196 for (int i = 0; i < fBatches.count(); ++i) {
robertphillipsa13e2022015-11-11 12:01:09 -0800197 fBatches[i]->draw(flushState);
bsalomon512be532015-09-10 10:42:55 -0700198 }
199
bsalomona73239a2015-04-28 13:35:17 -0700200 fFlushing = false;
bsalomona73239a2015-04-28 13:35:17 -0700201}
202
bsalomon512be532015-09-10 10:42:55 -0700203void GrDrawTarget::reset() {
204 fBatches.reset();
205}
206
bsalomonabd30f52015-08-13 13:34:48 -0700207void GrDrawTarget::drawBatch(const GrPipelineBuilder& pipelineBuilder, GrDrawBatch* batch) {
joshualitt4d8da812015-01-28 12:53:54 -0800208 // Setup clip
joshualitt4d8da812015-01-28 12:53:54 -0800209 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomon0ba8c242015-10-07 09:20:28 -0700210 GrAppliedClip clip;
bsalomone91f7b52015-10-27 06:42:50 -0700211 if (!fClipMaskManager->setupClipping(pipelineBuilder, &ars, &batch->bounds(), &clip)) {
joshualitt4d8da812015-01-28 12:53:54 -0800212 return;
213 }
cdaltond4727922015-11-10 12:49:06 -0800214 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
215 if (clip.clipCoverageFragmentProcessor()) {
216 arfps.set(&pipelineBuilder);
217 arfps.addCoverageFragmentProcessor(clip.clipCoverageFragmentProcessor());
218 }
joshualitt4d8da812015-01-28 12:53:54 -0800219
bsalomonad792c12015-09-10 11:10:50 -0700220 GrPipeline::CreateArgs args;
cdaltond4727922015-11-10 12:49:06 -0800221 if (!this->installPipelineInDrawBatch(&pipelineBuilder, &clip.scissorState(), batch)) {
egdaniele36914c2015-02-13 09:00:33 -0800222 return;
223 }
bsalomonad792c12015-09-10 11:10:50 -0700224
robertphillips498d7ac2015-10-30 10:11:30 -0700225#ifdef ENABLE_MDB
226 SkASSERT(fRenderTarget);
227 batch->pipeline()->addDependenciesTo(fRenderTarget);
228#endif
229
bsalomon512be532015-09-10 10:42:55 -0700230 this->recordBatch(batch);
joshualitt4d8da812015-01-28 12:53:54 -0800231}
232
joshualitt2c93efe2014-11-06 12:57:13 -0800233static const GrStencilSettings& winding_path_stencil_settings() {
234 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
235 kIncClamp_StencilOp,
236 kIncClamp_StencilOp,
237 kAlwaysIfInClip_StencilFunc,
238 0xFFFF, 0xFFFF, 0xFFFF);
239 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
240}
241
242static const GrStencilSettings& even_odd_path_stencil_settings() {
243 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
244 kInvert_StencilOp,
245 kInvert_StencilOp,
246 kAlwaysIfInClip_StencilFunc,
247 0xFFFF, 0xFFFF, 0xFFFF);
248 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
249}
250
251void GrDrawTarget::getPathStencilSettingsForFilltype(GrPathRendering::FillType fill,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700252 const GrStencilAttachment* sb,
joshualitt2c93efe2014-11-06 12:57:13 -0800253 GrStencilSettings* outStencilSettings) {
254
255 switch (fill) {
256 default:
257 SkFAIL("Unexpected path fill.");
258 case GrPathRendering::kWinding_FillType:
259 *outStencilSettings = winding_path_stencil_settings();
260 break;
261 case GrPathRendering::kEvenOdd_FillType:
262 *outStencilSettings = even_odd_path_stencil_settings();
263 break;
264 }
bsalomonb3b9aec2015-09-10 11:16:35 -0700265 fClipMaskManager->adjustPathStencilParams(sb, outStencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800266}
267
joshualitt1c735482015-07-13 08:08:25 -0700268void GrDrawTarget::stencilPath(const GrPipelineBuilder& pipelineBuilder,
joshualittf2384692015-09-10 11:00:51 -0700269 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800270 const GrPath* path,
271 GrPathRendering::FillType fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000272 // TODO: extract portions of checkDraw that are relevant to path stenciling.
bsalomon49f085d2014-09-05 13:34:00 -0700273 SkASSERT(path);
jvanverthe9c0fc62015-04-29 11:18:05 -0700274 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
joshualitt2c93efe2014-11-06 12:57:13 -0800275
276 // Setup clip
egdaniel8dd688b2015-01-22 10:16:09 -0800277 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomon0ba8c242015-10-07 09:20:28 -0700278 GrAppliedClip clip;
bsalomone91f7b52015-10-27 06:42:50 -0700279 if (!fClipMaskManager->setupClipping(pipelineBuilder, &ars, nullptr, &clip)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800280 return;
281 }
282
bsalomon0ba8c242015-10-07 09:20:28 -0700283 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
284 if (clip.clipCoverageFragmentProcessor()) {
285 arfps.set(&pipelineBuilder);
286 arfps.addCoverageFragmentProcessor(clip.clipCoverageFragmentProcessor());
287 }
288
joshualitt2c93efe2014-11-06 12:57:13 -0800289 // set stencil settings for path
290 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700291 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdanielec00d942015-09-14 12:56:10 -0700292 GrStencilAttachment* sb = fResourceProvider->attachStencilAttachment(rt);
bsalomon6bc1b5f2015-02-23 09:06:38 -0800293 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800294
joshualittf2384692015-09-10 11:00:51 -0700295 GrBatch* batch = GrStencilPathBatch::Create(viewMatrix,
bsalomona44919e2015-08-18 13:28:19 -0700296 pipelineBuilder.isHWAntialias(),
bsalomone91f7b52015-10-27 06:42:50 -0700297 stencilSettings, clip.scissorState(),
bsalomona44919e2015-08-18 13:28:19 -0700298 pipelineBuilder.getRenderTarget(),
299 path);
bsalomon512be532015-09-10 10:42:55 -0700300 this->recordBatch(batch);
bsalomona44919e2015-08-18 13:28:19 -0700301 batch->unref();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000302}
303
joshualitt1c735482015-07-13 08:08:25 -0700304void GrDrawTarget::drawPath(const GrPipelineBuilder& pipelineBuilder,
joshualittf2384692015-09-10 11:00:51 -0700305 const SkMatrix& viewMatrix,
306 GrColor color,
joshualitt9853cce2014-11-17 14:22:48 -0800307 const GrPath* path,
308 GrPathRendering::FillType fill) {
bsalomon49f085d2014-09-05 13:34:00 -0700309 SkASSERT(path);
jvanverthe9c0fc62015-04-29 11:18:05 -0700310 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000311
joshualittf2384692015-09-10 11:00:51 -0700312 GrDrawPathBatchBase* batch = GrDrawPathBatch::Create(viewMatrix, color, path);
bsalomon1fcc01c2015-09-09 09:48:06 -0700313 this->drawPathBatch(pipelineBuilder, batch, fill);
314 batch->unref();
315}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000316
bsalomon1fcc01c2015-09-09 09:48:06 -0700317void GrDrawTarget::drawPathsFromRange(const GrPipelineBuilder& pipelineBuilder,
joshualittf2384692015-09-10 11:00:51 -0700318 const SkMatrix& viewMatrix,
319 const SkMatrix& localMatrix,
320 GrColor color,
cdalton8585dd22015-10-08 08:04:09 -0700321 GrPathRange* range,
bsalomon1fcc01c2015-09-09 09:48:06 -0700322 GrPathRangeDraw* draw,
323 GrPathRendering::FillType fill) {
cdalton8585dd22015-10-08 08:04:09 -0700324 GrDrawPathBatchBase* batch = GrDrawPathRangeBatch::Create(viewMatrix, localMatrix, color,
325 range, draw);
bsalomon1fcc01c2015-09-09 09:48:06 -0700326 this->drawPathBatch(pipelineBuilder, batch, fill);
327 batch->unref();
328}
329
330void GrDrawTarget::drawPathBatch(const GrPipelineBuilder& pipelineBuilder,
331 GrDrawPathBatchBase* batch,
332 GrPathRendering::FillType fill) {
bsalomonadd79ef2015-08-19 13:26:49 -0700333 // This looks like drawBatch() but there is an added wrinkle that stencil settings get inserted
bsalomonb3b9aec2015-09-10 11:16:35 -0700334 // after setting up clipping but before onDrawBatch(). TODO: Figure out a better model for
335 // handling stencil settings WRT interactions between pipeline(builder), clipmaskmanager, and
336 // batches.
bsalomonadd79ef2015-08-19 13:26:49 -0700337
egdaniel8dd688b2015-01-22 10:16:09 -0800338 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomon0ba8c242015-10-07 09:20:28 -0700339 GrAppliedClip clip;
bsalomone91f7b52015-10-27 06:42:50 -0700340 if (!fClipMaskManager->setupClipping(pipelineBuilder, &ars, &batch->bounds(), &clip)) {
bsalomonadd79ef2015-08-19 13:26:49 -0700341 return;
joshualitt2c93efe2014-11-06 12:57:13 -0800342 }
343
cdaltond4727922015-11-10 12:49:06 -0800344 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
345 if (clip.clipCoverageFragmentProcessor()) {
346 arfps.set(&pipelineBuilder);
347 arfps.addCoverageFragmentProcessor(clip.clipCoverageFragmentProcessor());
348 }
349
bsalomonadd79ef2015-08-19 13:26:49 -0700350 // Ensure the render target has a stencil buffer and get the stencil settings.
joshualitt2c93efe2014-11-06 12:57:13 -0800351 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700352 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdanielec00d942015-09-14 12:56:10 -0700353 GrStencilAttachment* sb = fResourceProvider->attachStencilAttachment(rt);
bsalomon6bc1b5f2015-02-23 09:06:38 -0800354 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
bsalomonadd79ef2015-08-19 13:26:49 -0700355 batch->setStencilSettings(stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800356
bsalomonad792c12015-09-10 11:10:50 -0700357 GrPipeline::CreateArgs args;
cdaltond4727922015-11-10 12:49:06 -0800358 if (!this->installPipelineInDrawBatch(&pipelineBuilder, &clip.scissorState(), batch)) {
bsalomonadd79ef2015-08-19 13:26:49 -0700359 return;
360 }
egdaniele36914c2015-02-13 09:00:33 -0800361
bsalomon512be532015-09-10 10:42:55 -0700362 this->recordBatch(batch);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000363}
364
joshualittd2b23e02015-08-21 10:53:34 -0700365void GrDrawTarget::drawNonAARect(const GrPipelineBuilder& pipelineBuilder,
joshualitt1c735482015-07-13 08:08:25 -0700366 GrColor color,
367 const SkMatrix& viewMatrix,
joshualittb6b513b2015-08-21 10:25:18 -0700368 const SkRect& rect) {
joshualittd2b23e02015-08-21 10:53:34 -0700369 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect,
370 nullptr, nullptr));
joshualittad17cfc2015-05-05 10:45:57 -0700371 this->drawBatch(pipelineBuilder, batch);
372}
373
joshualittd2b23e02015-08-21 10:53:34 -0700374void GrDrawTarget::drawNonAARect(const GrPipelineBuilder& pipelineBuilder,
joshualittb6b513b2015-08-21 10:25:18 -0700375 GrColor color,
376 const SkMatrix& viewMatrix,
377 const SkRect& rect,
378 const SkMatrix& localMatrix) {
joshualittd2b23e02015-08-21 10:53:34 -0700379 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect,
380 nullptr, &localMatrix));
joshualittb6b513b2015-08-21 10:25:18 -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 SkRect& localRect) {
joshualittd2b23e02015-08-21 10:53:34 -0700389 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect,
390 &localRect, nullptr));
joshualittb6b513b2015-08-21 10:25:18 -0700391 this->drawBatch(pipelineBuilder, batch);
392}
393
394
joshualitt1c735482015-07-13 08:08:25 -0700395void GrDrawTarget::drawAARect(const GrPipelineBuilder& pipelineBuilder,
robertphillipsea461502015-05-26 11:38:03 -0700396 GrColor color,
397 const SkMatrix& viewMatrix,
398 const SkRect& rect,
399 const SkRect& devRect) {
joshualittd2b23e02015-08-21 10:53:34 -0700400 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateAAFill(color, viewMatrix, rect,
bsalomonabd30f52015-08-13 13:34:48 -0700401 devRect));
joshualitt14205b12015-08-10 11:40:56 -0700402 this->drawBatch(pipelineBuilder, batch);
robertphillipsea461502015-05-26 11:38:03 -0700403}
404
joshualitt9853cce2014-11-17 14:22:48 -0800405void GrDrawTarget::clear(const SkIRect* rect,
406 GrColor color,
407 bool canIgnoreRect,
bsalomon63b21962014-11-05 07:05:34 -0800408 GrRenderTarget* renderTarget) {
egdaniel51c8d402015-08-06 10:54:13 -0700409 SkIRect rtRect = SkIRect::MakeWH(renderTarget->width(), renderTarget->height());
410 SkIRect clippedRect;
411 if (!rect ||
412 (canIgnoreRect && this->caps()->fullClearIsFree()) ||
413 rect->contains(rtRect)) {
414 rect = &rtRect;
415 } else {
416 clippedRect = *rect;
417 if (!clippedRect.intersect(rtRect)) {
418 return;
419 }
420 rect = &clippedRect;
421 }
422
bsalomonb3b9aec2015-09-10 11:16:35 -0700423 if (this->caps()->useDrawInsteadOfClear()) {
bsalomon63b21962014-11-05 07:05:34 -0800424 // This works around a driver bug with clear by drawing a rect instead.
425 // The driver will ignore a clear if it is the only thing rendered to a
426 // target before the target is read.
egdaniel51c8d402015-08-06 10:54:13 -0700427 if (rect == &rtRect) {
bsalomon63b21962014-11-05 07:05:34 -0800428 this->discard(renderTarget);
429 }
bsalomon63b21962014-11-05 07:05:34 -0800430
egdaniel8dd688b2015-01-22 10:16:09 -0800431 GrPipelineBuilder pipelineBuilder;
432 pipelineBuilder.setRenderTarget(renderTarget);
joshualitt9853cce2014-11-17 14:22:48 -0800433
joshualittd2b23e02015-08-21 10:53:34 -0700434 this->drawNonAARect(pipelineBuilder, color, SkMatrix::I(), *rect);
bsalomon53469832015-08-18 09:20:09 -0700435 } else {
halcanary385fe4d2015-08-26 13:07:48 -0700436 GrBatch* batch = new GrClearBatch(*rect, color, renderTarget);
bsalomon512be532015-09-10 10:42:55 -0700437 this->recordBatch(batch);
bsalomon53469832015-08-18 09:20:09 -0700438 batch->unref();
439 }
440}
441
442void GrDrawTarget::discard(GrRenderTarget* renderTarget) {
443 if (this->caps()->discardRenderTargetSupport()) {
halcanary385fe4d2015-08-26 13:07:48 -0700444 GrBatch* batch = new GrDiscardBatch(renderTarget);
bsalomon512be532015-09-10 10:42:55 -0700445 this->recordBatch(batch);
bsalomon53469832015-08-18 09:20:09 -0700446 batch->unref();
bsalomon63b21962014-11-05 07:05:34 -0800447 }
448}
449
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000450////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000451
bsalomon6df86402015-06-01 10:41:49 -0700452void GrDrawTarget::copySurface(GrSurface* dst,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000453 GrSurface* src,
454 const SkIRect& srcRect,
455 const SkIPoint& dstPoint) {
bsalomon872062c2015-08-18 12:12:35 -0700456 GrBatch* batch = GrCopySurfaceBatch::Create(dst, src, srcRect, dstPoint);
457 if (batch) {
robertphillips498d7ac2015-10-30 10:11:30 -0700458#ifdef ENABLE_MDB
459 this->addDependency(src);
460#endif
461
bsalomon512be532015-09-10 10:42:55 -0700462 this->recordBatch(batch);
bsalomon872062c2015-08-18 12:12:35 -0700463 batch->unref();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000464 }
bsalomon@google.comeb851172013-04-15 13:51:00 +0000465}
466
bsalomon512be532015-09-10 10:42:55 -0700467template <class Left, class Right> static bool intersect(const Left& a, const Right& b) {
468 SkASSERT(a.fLeft <= a.fRight && a.fTop <= a.fBottom &&
469 b.fLeft <= b.fRight && b.fTop <= b.fBottom);
470 return a.fLeft < b.fRight && b.fLeft < a.fRight && a.fTop < b.fBottom && b.fTop < a.fBottom;
471}
472
473void GrDrawTarget::recordBatch(GrBatch* batch) {
robertphillipsa106c622015-10-16 09:07:06 -0700474 // A closed drawTarget should never receive new/more batches
robertphillips6a186652015-10-20 07:37:58 -0700475 SkASSERT(!this->isClosed());
robertphillipsa106c622015-10-16 09:07:06 -0700476
bsalomon512be532015-09-10 10:42:55 -0700477 // Check if there is a Batch Draw we can batch with by linearly searching back until we either
478 // 1) check every draw
479 // 2) intersect with something
480 // 3) find a 'blocker'
481 // Experimentally we have found that most batching occurs within the first 10 comparisons.
482 static const int kMaxLookback = 10;
483
484 GrBATCH_INFO("Re-Recording (%s, B%u)\n"
joshualitte2bcec32015-09-30 06:22:22 -0700485 "\tBounds LRTB (%f, %f, %f, %f)\n",
bsalomon512be532015-09-10 10:42:55 -0700486 batch->name(),
487 batch->uniqueID(),
488 batch->bounds().fLeft, batch->bounds().fRight,
489 batch->bounds().fTop, batch->bounds().fBottom);
490 GrBATCH_INFO(SkTabString(batch->dumpInfo(), 1).c_str());
491 GrBATCH_INFO("\tOutcome:\n");
492 int maxCandidates = SkTMin(kMaxLookback, fBatches.count());
493 if (maxCandidates) {
494 int i = 0;
495 while (true) {
496 GrBatch* candidate = fBatches.fromBack(i);
497 // We cannot continue to search backwards if the render target changes
498 if (candidate->renderTargetUniqueID() != batch->renderTargetUniqueID()) {
499 GrBATCH_INFO("\t\tBreaking because of (%s, B%u) Rendertarget\n",
500 candidate->name(), candidate->uniqueID());
501 break;
502 }
503 if (candidate->combineIfPossible(batch, *this->caps())) {
504 GrBATCH_INFO("\t\tCombining with (%s, B%u)\n", candidate->name(),
505 candidate->uniqueID());
506 return;
507 }
508 // Stop going backwards if we would cause a painter's order violation.
509 if (intersect(candidate->bounds(), batch->bounds())) {
510 GrBATCH_INFO("\t\tIntersects with (%s, B%u)\n", candidate->name(),
511 candidate->uniqueID());
512 break;
513 }
514 ++i;
515 if (i == maxCandidates) {
516 GrBATCH_INFO("\t\tReached max lookback or beginning of batch array %d\n", i);
517 break;
518 }
519 }
520 } else {
521 GrBATCH_INFO("\t\tFirstBatch\n");
522 }
523 fBatches.push_back().reset(SkRef(batch));
524}
525
egdaniele36914c2015-02-13 09:00:33 -0800526///////////////////////////////////////////////////////////////////////////////
527
bsalomonad792c12015-09-10 11:10:50 -0700528bool GrDrawTarget::installPipelineInDrawBatch(const GrPipelineBuilder* pipelineBuilder,
cdaltond4727922015-11-10 12:49:06 -0800529 const GrScissorState* scissor,
530 GrDrawBatch* batch) {
bsalomonad792c12015-09-10 11:10:50 -0700531 GrPipeline::CreateArgs args;
532 args.fPipelineBuilder = pipelineBuilder;
533 args.fCaps = this->caps();
cdaltond4727922015-11-10 12:49:06 -0800534 args.fScissor = scissor;
bsalomonad792c12015-09-10 11:10:50 -0700535 args.fColorPOI = pipelineBuilder->colorProcInfo(batch);
536 args.fCoveragePOI = pipelineBuilder->coverageProcInfo(batch);
537 if (!this->setupDstReadIfNecessary(*pipelineBuilder, args.fColorPOI,
538 args.fCoveragePOI, &args.fDstTexture,
539 batch->bounds())) {
540 return false;
egdaniele36914c2015-02-13 09:00:33 -0800541 }
bsalomonad792c12015-09-10 11:10:50 -0700542
543 if (!batch->installPipeline(args)) {
544 return false;
545 }
546
547 return true;
egdaniele36914c2015-02-13 09:00:33 -0800548}
549
bsalomonb3b9aec2015-09-10 11:16:35 -0700550void GrDrawTarget::clearStencilClip(const SkIRect& rect, bool insideClip, GrRenderTarget* rt) {
halcanary385fe4d2015-08-26 13:07:48 -0700551 GrBatch* batch = new GrClearStencilClipBatch(rect, insideClip, rt);
bsalomon512be532015-09-10 10:42:55 -0700552 this->recordBatch(batch);
bsalomon5ea03632015-08-18 10:33:30 -0700553 batch->unref();
554}