blob: 3aa7a540f8e89cc6dba35bc716d5403fdd10775b [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)
robertphillips1f0e3502015-11-10 10:19:50 -080039 , fFlushState(fGpu, fResourceProvider)
bsalomon512be532015-09-10 10:42:55 -070040 , fFlushing(false)
bsalomon648c6962015-10-23 09:06:59 -070041 , fFlags(0)
robertphillips498d7ac2015-10-30 10:11:30 -070042 , fOptions(options)
43 , fRenderTarget(rt) {
bsalomonb3b9aec2015-09-10 11:16:35 -070044 // TODO: Stop extracting the context (currently needed by GrClipMaskManager)
45 fContext = fGpu->getContext();
Brian Salomone66fec22015-09-10 14:40:20 -040046 fClipMaskManager.reset(new GrClipMaskManager(this));
robertphillips4beb5c12015-10-20 07:50:00 -070047
48#ifdef SK_DEBUG
49 static int debugID = 0;
50 fDebugID = debugID++;
51#endif
bsalomon4061b122015-05-29 10:26:19 -070052}
53
54GrDrawTarget::~GrDrawTarget() {
robertphillips498d7ac2015-10-30 10:11:30 -070055 if (fRenderTarget && this == fRenderTarget->getLastDrawTarget()) {
56 fRenderTarget->setLastDrawTarget(nullptr);
57 }
58
bsalomon4061b122015-05-29 10:26:19 -070059 fGpu->unref();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000060}
61
62////////////////////////////////////////////////////////////////////////////////
63
robertphillips6a186652015-10-20 07:37:58 -070064// Add a GrDrawTarget-based dependency
65void GrDrawTarget::addDependency(GrDrawTarget* dependedOn) {
66 SkASSERT(!dependedOn->dependsOn(this)); // loops are bad
67
68 if (this->dependsOn(dependedOn)) {
69 return; // don't add duplicate dependencies
70 }
71
72 *fDependencies.push() = dependedOn;
73}
74
75// Convert from a GrSurface-based dependency to a GrDrawTarget one
76void GrDrawTarget::addDependency(GrSurface* dependedOn) {
77 if (dependedOn->asRenderTarget() && dependedOn->asRenderTarget()->getLastDrawTarget()) {
78 // If it is still receiving dependencies, this DT shouldn't be closed
79 SkASSERT(!this->isClosed());
80
81 GrDrawTarget* dt = dependedOn->asRenderTarget()->getLastDrawTarget();
82 if (dt == this) {
83 // self-read - presumably for dst reads
84 } else {
85 this->addDependency(dt);
86
87 // Can't make it closed in the self-read case
88 dt->makeClosed();
89 }
90 }
91}
92
robertphillips4beb5c12015-10-20 07:50:00 -070093#ifdef SK_DEBUG
94void GrDrawTarget::dump() const {
95 SkDebugf("--------------------------------------------------------------\n");
96 SkDebugf("node: %d\n");
97 SkDebugf("relies On (%d): ", fDependencies.count());
98 for (int i = 0; i < fDependencies.count(); ++i) {
99 SkDebugf("%d, ", fDependencies[i]->fDebugID);
100 }
101 SkDebugf("\n");
102 SkDebugf("batches (%d):\n", fBatches.count());
103 for (int i = 0; i < fBatches.count(); ++i) {
104#if 0
105 SkDebugf("*******************************\n");
106#endif
107 SkDebugf("%d: %s\n", i, fBatches[i]->name());
108#if 0
109 SkString str = fBatches[i]->dumpInfo();
110 SkDebugf("%s\n", str.c_str());
111#endif
112 }
113}
114#endif
115
bsalomon50785a32015-02-06 07:02:37 -0800116bool GrDrawTarget::setupDstReadIfNecessary(const GrPipelineBuilder& pipelineBuilder,
egdaniele36914c2015-02-13 09:00:33 -0800117 const GrProcOptInfo& colorPOI,
118 const GrProcOptInfo& coveragePOI,
bsalomon6a44c6a2015-05-26 09:49:05 -0700119 GrXferProcessor::DstTexture* dstTexture,
bsalomonad792c12015-09-10 11:10:50 -0700120 const SkRect& batchBounds) {
121 SkRect bounds = batchBounds;
122 bounds.outset(0.5f, 0.5f);
123
bsalomon6a44c6a2015-05-26 09:49:05 -0700124 if (!pipelineBuilder.willXPNeedDstTexture(*this->caps(), colorPOI, coveragePOI)) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000125 return true;
126 }
cdalton9954bc32015-04-29 14:17:00 -0700127
bsalomon50785a32015-02-06 07:02:37 -0800128 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
cdalton9954bc32015-04-29 14:17:00 -0700129
130 if (this->caps()->textureBarrierSupport()) {
131 if (GrTexture* rtTex = rt->asTexture()) {
bsalomondc47ff72015-05-26 12:16:59 -0700132 // The render target is a texture, so we can read from it directly in the shader. The XP
cdalton9954bc32015-04-29 14:17:00 -0700133 // will be responsible to detect this situation and request a texture barrier.
bsalomon6a44c6a2015-05-26 09:49:05 -0700134 dstTexture->setTexture(rtTex);
135 dstTexture->setOffset(0, 0);
cdalton9954bc32015-04-29 14:17:00 -0700136 return true;
137 }
138 }
139
140 SkIRect copyRect;
joshualitt44701df2015-02-23 14:44:57 -0800141 pipelineBuilder.clip().getConservativeBounds(rt, &copyRect);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000142
bsalomonad792c12015-09-10 11:10:50 -0700143 SkIRect drawIBounds;
144 bounds.roundOut(&drawIBounds);
145 if (!copyRect.intersect(drawIBounds)) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000146#ifdef SK_DEBUG
bsalomonb3b9aec2015-09-10 11:16:35 -0700147 GrCapsDebugf(this->caps(), "Missed an early reject. "
148 "Bailing on draw from setupDstReadIfNecessary.\n");
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000149#endif
bsalomonad792c12015-09-10 11:10:50 -0700150 return false;
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000151 }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000152
commit-bot@chromium.org63150af2013-04-11 22:00:22 +0000153 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
154 // have per-sample dst values by making the copy multisampled.
bsalomonf2703d82014-10-28 14:33:06 -0700155 GrSurfaceDesc desc;
bsalomonb3b9aec2015-09-10 11:16:35 -0700156 if (!fGpu->initCopySurfaceDstDesc(rt, &desc)) {
bsalomona73239a2015-04-28 13:35:17 -0700157 desc.fOrigin = kDefault_GrSurfaceOrigin;
158 desc.fFlags = kRenderTarget_GrSurfaceFlag;
159 desc.fConfig = rt->config();
160 }
161
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000162 desc.fWidth = copyRect.width();
163 desc.fHeight = copyRect.height();
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000164
bsalomoneae62002015-07-31 13:59:30 -0700165 static const uint32_t kFlags = 0;
166 SkAutoTUnref<GrTexture> copy(fResourceProvider->createApproxTexture(desc, kFlags));
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000167
bsalomone3059732014-10-14 11:47:22 -0700168 if (!copy) {
tfarina38406c82014-10-31 07:11:12 -0700169 SkDebugf("Failed to create temporary copy of destination texture.\n");
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000170 return false;
171 }
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000172 SkIPoint dstPoint = {0, 0};
bsalomon6df86402015-06-01 10:41:49 -0700173 this->copySurface(copy, rt, copyRect, dstPoint);
174 dstTexture->setTexture(copy);
175 dstTexture->setOffset(copyRect.fLeft, copyRect.fTop);
176 return true;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000177}
178
bsalomona73239a2015-04-28 13:35:17 -0700179void GrDrawTarget::flush() {
180 if (fFlushing) {
181 return;
182 }
183 fFlushing = true;
184
robertphillipsa106c622015-10-16 09:07:06 -0700185 // Semi-usually the drawTargets are already closed at this point, but sometimes Ganesh
186 // needs to flush mid-draw. In that case, the SkGpuDevice's drawTargets won't be closed
187 // but need to be flushed anyway. Closing such drawTargets here will mean new
188 // drawTargets will be created to replace them if the SkGpuDevice(s) write to them again.
189 this->makeClosed();
190
robertphillips498d7ac2015-10-30 10:11:30 -0700191 // Loop over the batches that haven't yet generated their geometry
robertphillips1f0e3502015-11-10 10:19:50 -0800192 for (int i = 0; i < fBatches.count(); ++i) {
193 fBatches[i]->prepare(&fFlushState);
bsalomon512be532015-09-10 10:42:55 -0700194 }
195
196 // Upload all data to the GPU
joshualittf6d259b2015-10-02 11:27:14 -0700197 fFlushState.preIssueDraws();
bsalomon512be532015-09-10 10:42:55 -0700198
199 // Draw all the generated geometry.
200 for (int i = 0; i < fBatches.count(); ++i) {
joshualittf6d259b2015-10-02 11:27:14 -0700201 fBatches[i]->draw(&fFlushState);
bsalomon512be532015-09-10 10:42:55 -0700202 }
203
joshualittf6d259b2015-10-02 11:27:14 -0700204 SkASSERT(fFlushState.lastFlushedToken() == fFlushState.currentToken());
205 this->reset();
bsalomona73239a2015-04-28 13:35:17 -0700206
bsalomona73239a2015-04-28 13:35:17 -0700207 fFlushing = false;
bsalomona73239a2015-04-28 13:35:17 -0700208}
209
bsalomon512be532015-09-10 10:42:55 -0700210void GrDrawTarget::reset() {
211 fBatches.reset();
joshualittf6d259b2015-10-02 11:27:14 -0700212 fFlushState.reset();
bsalomon512be532015-09-10 10:42:55 -0700213}
214
bsalomonabd30f52015-08-13 13:34:48 -0700215void GrDrawTarget::drawBatch(const GrPipelineBuilder& pipelineBuilder, GrDrawBatch* batch) {
joshualitt4d8da812015-01-28 12:53:54 -0800216 // Setup clip
joshualitt4d8da812015-01-28 12:53:54 -0800217 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomon0ba8c242015-10-07 09:20:28 -0700218 GrAppliedClip clip;
bsalomone91f7b52015-10-27 06:42:50 -0700219 if (!fClipMaskManager->setupClipping(pipelineBuilder, &ars, &batch->bounds(), &clip)) {
joshualitt4d8da812015-01-28 12:53:54 -0800220 return;
221 }
222
bsalomonad792c12015-09-10 11:10:50 -0700223 GrPipeline::CreateArgs args;
cdalton14184d52015-11-09 15:12:19 -0800224 if (!this->installPipelineInDrawBatch(&pipelineBuilder, &clip, batch)) {
egdaniele36914c2015-02-13 09:00:33 -0800225 return;
226 }
bsalomonad792c12015-09-10 11:10:50 -0700227
robertphillips498d7ac2015-10-30 10:11:30 -0700228#ifdef ENABLE_MDB
229 SkASSERT(fRenderTarget);
230 batch->pipeline()->addDependenciesTo(fRenderTarget);
231#endif
232
bsalomon512be532015-09-10 10:42:55 -0700233 this->recordBatch(batch);
joshualitt4d8da812015-01-28 12:53:54 -0800234}
235
joshualitt2c93efe2014-11-06 12:57:13 -0800236static const GrStencilSettings& winding_path_stencil_settings() {
237 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
238 kIncClamp_StencilOp,
239 kIncClamp_StencilOp,
240 kAlwaysIfInClip_StencilFunc,
241 0xFFFF, 0xFFFF, 0xFFFF);
242 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
243}
244
245static const GrStencilSettings& even_odd_path_stencil_settings() {
246 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
247 kInvert_StencilOp,
248 kInvert_StencilOp,
249 kAlwaysIfInClip_StencilFunc,
250 0xFFFF, 0xFFFF, 0xFFFF);
251 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
252}
253
254void GrDrawTarget::getPathStencilSettingsForFilltype(GrPathRendering::FillType fill,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700255 const GrStencilAttachment* sb,
joshualitt2c93efe2014-11-06 12:57:13 -0800256 GrStencilSettings* outStencilSettings) {
257
258 switch (fill) {
259 default:
260 SkFAIL("Unexpected path fill.");
261 case GrPathRendering::kWinding_FillType:
262 *outStencilSettings = winding_path_stencil_settings();
263 break;
264 case GrPathRendering::kEvenOdd_FillType:
265 *outStencilSettings = even_odd_path_stencil_settings();
266 break;
267 }
bsalomonb3b9aec2015-09-10 11:16:35 -0700268 fClipMaskManager->adjustPathStencilParams(sb, outStencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800269}
270
joshualitt1c735482015-07-13 08:08:25 -0700271void GrDrawTarget::stencilPath(const GrPipelineBuilder& pipelineBuilder,
joshualittf2384692015-09-10 11:00:51 -0700272 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800273 const GrPath* path,
274 GrPathRendering::FillType fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000275 // TODO: extract portions of checkDraw that are relevant to path stenciling.
bsalomon49f085d2014-09-05 13:34:00 -0700276 SkASSERT(path);
jvanverthe9c0fc62015-04-29 11:18:05 -0700277 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
joshualitt2c93efe2014-11-06 12:57:13 -0800278
279 // Setup clip
egdaniel8dd688b2015-01-22 10:16:09 -0800280 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomon0ba8c242015-10-07 09:20:28 -0700281 GrAppliedClip clip;
bsalomone91f7b52015-10-27 06:42:50 -0700282 if (!fClipMaskManager->setupClipping(pipelineBuilder, &ars, nullptr, &clip)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800283 return;
284 }
285
bsalomon0ba8c242015-10-07 09:20:28 -0700286 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
287 if (clip.clipCoverageFragmentProcessor()) {
288 arfps.set(&pipelineBuilder);
289 arfps.addCoverageFragmentProcessor(clip.clipCoverageFragmentProcessor());
290 }
291
joshualitt2c93efe2014-11-06 12:57:13 -0800292 // set stencil settings for path
293 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700294 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdanielec00d942015-09-14 12:56:10 -0700295 GrStencilAttachment* sb = fResourceProvider->attachStencilAttachment(rt);
bsalomon6bc1b5f2015-02-23 09:06:38 -0800296 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800297
joshualittf2384692015-09-10 11:00:51 -0700298 GrBatch* batch = GrStencilPathBatch::Create(viewMatrix,
bsalomona44919e2015-08-18 13:28:19 -0700299 pipelineBuilder.isHWAntialias(),
bsalomone91f7b52015-10-27 06:42:50 -0700300 stencilSettings, clip.scissorState(),
bsalomona44919e2015-08-18 13:28:19 -0700301 pipelineBuilder.getRenderTarget(),
302 path);
bsalomon512be532015-09-10 10:42:55 -0700303 this->recordBatch(batch);
bsalomona44919e2015-08-18 13:28:19 -0700304 batch->unref();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000305}
306
joshualitt1c735482015-07-13 08:08:25 -0700307void GrDrawTarget::drawPath(const GrPipelineBuilder& pipelineBuilder,
joshualittf2384692015-09-10 11:00:51 -0700308 const SkMatrix& viewMatrix,
309 GrColor color,
joshualitt9853cce2014-11-17 14:22:48 -0800310 const GrPath* path,
311 GrPathRendering::FillType fill) {
bsalomon49f085d2014-09-05 13:34:00 -0700312 SkASSERT(path);
jvanverthe9c0fc62015-04-29 11:18:05 -0700313 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000314
joshualittf2384692015-09-10 11:00:51 -0700315 GrDrawPathBatchBase* batch = GrDrawPathBatch::Create(viewMatrix, color, path);
bsalomon1fcc01c2015-09-09 09:48:06 -0700316 this->drawPathBatch(pipelineBuilder, batch, fill);
317 batch->unref();
318}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000319
bsalomon1fcc01c2015-09-09 09:48:06 -0700320void GrDrawTarget::drawPathsFromRange(const GrPipelineBuilder& pipelineBuilder,
joshualittf2384692015-09-10 11:00:51 -0700321 const SkMatrix& viewMatrix,
322 const SkMatrix& localMatrix,
323 GrColor color,
cdalton8585dd22015-10-08 08:04:09 -0700324 GrPathRange* range,
bsalomon1fcc01c2015-09-09 09:48:06 -0700325 GrPathRangeDraw* draw,
326 GrPathRendering::FillType fill) {
cdalton8585dd22015-10-08 08:04:09 -0700327 GrDrawPathBatchBase* batch = GrDrawPathRangeBatch::Create(viewMatrix, localMatrix, color,
328 range, draw);
bsalomon1fcc01c2015-09-09 09:48:06 -0700329 this->drawPathBatch(pipelineBuilder, batch, fill);
330 batch->unref();
331}
332
333void GrDrawTarget::drawPathBatch(const GrPipelineBuilder& pipelineBuilder,
334 GrDrawPathBatchBase* batch,
335 GrPathRendering::FillType fill) {
bsalomonadd79ef2015-08-19 13:26:49 -0700336 // This looks like drawBatch() but there is an added wrinkle that stencil settings get inserted
bsalomonb3b9aec2015-09-10 11:16:35 -0700337 // after setting up clipping but before onDrawBatch(). TODO: Figure out a better model for
338 // handling stencil settings WRT interactions between pipeline(builder), clipmaskmanager, and
339 // batches.
bsalomonadd79ef2015-08-19 13:26:49 -0700340
egdaniel8dd688b2015-01-22 10:16:09 -0800341 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomon0ba8c242015-10-07 09:20:28 -0700342 GrAppliedClip clip;
bsalomone91f7b52015-10-27 06:42:50 -0700343 if (!fClipMaskManager->setupClipping(pipelineBuilder, &ars, &batch->bounds(), &clip)) {
bsalomonadd79ef2015-08-19 13:26:49 -0700344 return;
joshualitt2c93efe2014-11-06 12:57:13 -0800345 }
346
bsalomonadd79ef2015-08-19 13:26:49 -0700347 // Ensure the render target has a stencil buffer and get the stencil settings.
joshualitt2c93efe2014-11-06 12:57:13 -0800348 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700349 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdanielec00d942015-09-14 12:56:10 -0700350 GrStencilAttachment* sb = fResourceProvider->attachStencilAttachment(rt);
bsalomon6bc1b5f2015-02-23 09:06:38 -0800351 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
bsalomonadd79ef2015-08-19 13:26:49 -0700352 batch->setStencilSettings(stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800353
bsalomonad792c12015-09-10 11:10:50 -0700354 GrPipeline::CreateArgs args;
cdalton14184d52015-11-09 15:12:19 -0800355 if (!this->installPipelineInDrawBatch(&pipelineBuilder, &clip, batch)) {
bsalomonadd79ef2015-08-19 13:26:49 -0700356 return;
357 }
egdaniele36914c2015-02-13 09:00:33 -0800358
bsalomon512be532015-09-10 10:42:55 -0700359 this->recordBatch(batch);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000360}
361
joshualittd2b23e02015-08-21 10:53:34 -0700362void GrDrawTarget::drawNonAARect(const GrPipelineBuilder& pipelineBuilder,
joshualitt1c735482015-07-13 08:08:25 -0700363 GrColor color,
364 const SkMatrix& viewMatrix,
joshualittb6b513b2015-08-21 10:25:18 -0700365 const SkRect& rect) {
joshualittd2b23e02015-08-21 10:53:34 -0700366 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect,
367 nullptr, nullptr));
joshualittad17cfc2015-05-05 10:45:57 -0700368 this->drawBatch(pipelineBuilder, batch);
369}
370
joshualittd2b23e02015-08-21 10:53:34 -0700371void GrDrawTarget::drawNonAARect(const GrPipelineBuilder& pipelineBuilder,
joshualittb6b513b2015-08-21 10:25:18 -0700372 GrColor color,
373 const SkMatrix& viewMatrix,
374 const SkRect& rect,
375 const SkMatrix& localMatrix) {
joshualittd2b23e02015-08-21 10:53:34 -0700376 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect,
377 nullptr, &localMatrix));
joshualittb6b513b2015-08-21 10:25:18 -0700378 this->drawBatch(pipelineBuilder, batch);
379}
380
joshualittd2b23e02015-08-21 10:53:34 -0700381void GrDrawTarget::drawNonAARect(const GrPipelineBuilder& pipelineBuilder,
joshualittb6b513b2015-08-21 10:25:18 -0700382 GrColor color,
383 const SkMatrix& viewMatrix,
384 const SkRect& rect,
385 const SkRect& localRect) {
joshualittd2b23e02015-08-21 10:53:34 -0700386 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect,
387 &localRect, nullptr));
joshualittb6b513b2015-08-21 10:25:18 -0700388 this->drawBatch(pipelineBuilder, batch);
389}
390
391
joshualitt1c735482015-07-13 08:08:25 -0700392void GrDrawTarget::drawAARect(const GrPipelineBuilder& pipelineBuilder,
robertphillipsea461502015-05-26 11:38:03 -0700393 GrColor color,
394 const SkMatrix& viewMatrix,
395 const SkRect& rect,
396 const SkRect& devRect) {
joshualittd2b23e02015-08-21 10:53:34 -0700397 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateAAFill(color, viewMatrix, rect,
bsalomonabd30f52015-08-13 13:34:48 -0700398 devRect));
joshualitt14205b12015-08-10 11:40:56 -0700399 this->drawBatch(pipelineBuilder, batch);
robertphillipsea461502015-05-26 11:38:03 -0700400}
401
joshualitt9853cce2014-11-17 14:22:48 -0800402void GrDrawTarget::clear(const SkIRect* rect,
403 GrColor color,
404 bool canIgnoreRect,
bsalomon63b21962014-11-05 07:05:34 -0800405 GrRenderTarget* renderTarget) {
egdaniel51c8d402015-08-06 10:54:13 -0700406 SkIRect rtRect = SkIRect::MakeWH(renderTarget->width(), renderTarget->height());
407 SkIRect clippedRect;
408 if (!rect ||
409 (canIgnoreRect && this->caps()->fullClearIsFree()) ||
410 rect->contains(rtRect)) {
411 rect = &rtRect;
412 } else {
413 clippedRect = *rect;
414 if (!clippedRect.intersect(rtRect)) {
415 return;
416 }
417 rect = &clippedRect;
418 }
419
bsalomonb3b9aec2015-09-10 11:16:35 -0700420 if (this->caps()->useDrawInsteadOfClear()) {
bsalomon63b21962014-11-05 07:05:34 -0800421 // This works around a driver bug with clear by drawing a rect instead.
422 // The driver will ignore a clear if it is the only thing rendered to a
423 // target before the target is read.
egdaniel51c8d402015-08-06 10:54:13 -0700424 if (rect == &rtRect) {
bsalomon63b21962014-11-05 07:05:34 -0800425 this->discard(renderTarget);
426 }
bsalomon63b21962014-11-05 07:05:34 -0800427
egdaniel8dd688b2015-01-22 10:16:09 -0800428 GrPipelineBuilder pipelineBuilder;
429 pipelineBuilder.setRenderTarget(renderTarget);
joshualitt9853cce2014-11-17 14:22:48 -0800430
joshualittd2b23e02015-08-21 10:53:34 -0700431 this->drawNonAARect(pipelineBuilder, color, SkMatrix::I(), *rect);
bsalomon53469832015-08-18 09:20:09 -0700432 } else {
halcanary385fe4d2015-08-26 13:07:48 -0700433 GrBatch* batch = new GrClearBatch(*rect, color, renderTarget);
bsalomon512be532015-09-10 10:42:55 -0700434 this->recordBatch(batch);
bsalomon53469832015-08-18 09:20:09 -0700435 batch->unref();
436 }
437}
438
439void GrDrawTarget::discard(GrRenderTarget* renderTarget) {
440 if (this->caps()->discardRenderTargetSupport()) {
halcanary385fe4d2015-08-26 13:07:48 -0700441 GrBatch* batch = new GrDiscardBatch(renderTarget);
bsalomon512be532015-09-10 10:42:55 -0700442 this->recordBatch(batch);
bsalomon53469832015-08-18 09:20:09 -0700443 batch->unref();
bsalomon63b21962014-11-05 07:05:34 -0800444 }
445}
446
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000447////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000448
bsalomon6df86402015-06-01 10:41:49 -0700449void GrDrawTarget::copySurface(GrSurface* dst,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000450 GrSurface* src,
451 const SkIRect& srcRect,
452 const SkIPoint& dstPoint) {
bsalomon872062c2015-08-18 12:12:35 -0700453 GrBatch* batch = GrCopySurfaceBatch::Create(dst, src, srcRect, dstPoint);
454 if (batch) {
robertphillips498d7ac2015-10-30 10:11:30 -0700455#ifdef ENABLE_MDB
456 this->addDependency(src);
457#endif
458
bsalomon512be532015-09-10 10:42:55 -0700459 this->recordBatch(batch);
bsalomon872062c2015-08-18 12:12:35 -0700460 batch->unref();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000461 }
bsalomon@google.comeb851172013-04-15 13:51:00 +0000462}
463
bsalomon512be532015-09-10 10:42:55 -0700464template <class Left, class Right> static bool intersect(const Left& a, const Right& b) {
465 SkASSERT(a.fLeft <= a.fRight && a.fTop <= a.fBottom &&
466 b.fLeft <= b.fRight && b.fTop <= b.fBottom);
467 return a.fLeft < b.fRight && b.fLeft < a.fRight && a.fTop < b.fBottom && b.fTop < a.fBottom;
468}
469
470void GrDrawTarget::recordBatch(GrBatch* batch) {
robertphillipsa106c622015-10-16 09:07:06 -0700471 // A closed drawTarget should never receive new/more batches
robertphillips6a186652015-10-20 07:37:58 -0700472 SkASSERT(!this->isClosed());
bsalomon648c6962015-10-23 09:06:59 -0700473 // Should never have batches queued up when in immediate mode.
474 SkASSERT(!fOptions.fImmediateMode || !fBatches.count());
robertphillipsa106c622015-10-16 09:07:06 -0700475
bsalomon512be532015-09-10 10:42:55 -0700476 // Check if there is a Batch Draw we can batch with by linearly searching back until we either
477 // 1) check every draw
478 // 2) intersect with something
479 // 3) find a 'blocker'
480 // Experimentally we have found that most batching occurs within the first 10 comparisons.
481 static const int kMaxLookback = 10;
482
483 GrBATCH_INFO("Re-Recording (%s, B%u)\n"
joshualitte2bcec32015-09-30 06:22:22 -0700484 "\tBounds LRTB (%f, %f, %f, %f)\n",
bsalomon512be532015-09-10 10:42:55 -0700485 batch->name(),
486 batch->uniqueID(),
487 batch->bounds().fLeft, batch->bounds().fRight,
488 batch->bounds().fTop, batch->bounds().fBottom);
489 GrBATCH_INFO(SkTabString(batch->dumpInfo(), 1).c_str());
490 GrBATCH_INFO("\tOutcome:\n");
491 int maxCandidates = SkTMin(kMaxLookback, fBatches.count());
492 if (maxCandidates) {
493 int i = 0;
494 while (true) {
495 GrBatch* candidate = fBatches.fromBack(i);
496 // We cannot continue to search backwards if the render target changes
497 if (candidate->renderTargetUniqueID() != batch->renderTargetUniqueID()) {
498 GrBATCH_INFO("\t\tBreaking because of (%s, B%u) Rendertarget\n",
499 candidate->name(), candidate->uniqueID());
500 break;
501 }
502 if (candidate->combineIfPossible(batch, *this->caps())) {
503 GrBATCH_INFO("\t\tCombining with (%s, B%u)\n", candidate->name(),
504 candidate->uniqueID());
505 return;
506 }
507 // Stop going backwards if we would cause a painter's order violation.
508 if (intersect(candidate->bounds(), batch->bounds())) {
509 GrBATCH_INFO("\t\tIntersects with (%s, B%u)\n", candidate->name(),
510 candidate->uniqueID());
511 break;
512 }
513 ++i;
514 if (i == maxCandidates) {
515 GrBATCH_INFO("\t\tReached max lookback or beginning of batch array %d\n", i);
516 break;
517 }
518 }
519 } else {
520 GrBATCH_INFO("\t\tFirstBatch\n");
521 }
522 fBatches.push_back().reset(SkRef(batch));
bsalomon648c6962015-10-23 09:06:59 -0700523 if (fOptions.fImmediateMode) {
524 this->flush();
525 }
bsalomon512be532015-09-10 10:42:55 -0700526}
527
egdaniele36914c2015-02-13 09:00:33 -0800528///////////////////////////////////////////////////////////////////////////////
529
bsalomonad792c12015-09-10 11:10:50 -0700530bool GrDrawTarget::installPipelineInDrawBatch(const GrPipelineBuilder* pipelineBuilder,
cdalton14184d52015-11-09 15:12:19 -0800531 const GrAppliedClip* clip, GrDrawBatch* batch) {
532 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
533 if (clip->clipCoverageFragmentProcessor()) {
534 arfps.set(pipelineBuilder);
535 arfps.addCoverageFragmentProcessor(clip->clipCoverageFragmentProcessor());
536 }
537
bsalomonad792c12015-09-10 11:10:50 -0700538 GrPipeline::CreateArgs args;
539 args.fPipelineBuilder = pipelineBuilder;
540 args.fCaps = this->caps();
bsalomonad792c12015-09-10 11:10:50 -0700541 args.fColorPOI = pipelineBuilder->colorProcInfo(batch);
542 args.fCoveragePOI = pipelineBuilder->coverageProcInfo(batch);
cdalton14184d52015-11-09 15:12:19 -0800543 args.fClip = clip;
bsalomonad792c12015-09-10 11:10:50 -0700544 if (!this->setupDstReadIfNecessary(*pipelineBuilder, args.fColorPOI,
545 args.fCoveragePOI, &args.fDstTexture,
546 batch->bounds())) {
547 return false;
egdaniele36914c2015-02-13 09:00:33 -0800548 }
bsalomonad792c12015-09-10 11:10:50 -0700549
550 if (!batch->installPipeline(args)) {
551 return false;
552 }
553
554 return true;
egdaniele36914c2015-02-13 09:00:33 -0800555}
556
bsalomonb3b9aec2015-09-10 11:16:35 -0700557void GrDrawTarget::clearStencilClip(const SkIRect& rect, bool insideClip, GrRenderTarget* rt) {
halcanary385fe4d2015-08-26 13:07:48 -0700558 GrBatch* batch = new GrClearStencilClipBatch(rect, insideClip, rt);
bsalomon512be532015-09-10 10:42:55 -0700559 this->recordBatch(batch);
bsalomon5ea03632015-08-18 10:33:30 -0700560 batch->unref();
561}