blob: 7f84f8efd11888334cc9d6578d9a82624cf5dd7c [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
bsalomon4061b122015-05-29 10:26:19 -070035GrDrawTarget::GrDrawTarget(GrGpu* gpu, GrResourceProvider* resourceProvider)
36 : fGpu(SkRef(gpu))
bsalomon4061b122015-05-29 10:26:19 -070037 , fResourceProvider(resourceProvider)
joshualittf6d259b2015-10-02 11:27:14 -070038 , fFlushState(fGpu, fResourceProvider, 0)
bsalomon512be532015-09-10 10:42:55 -070039 , fFlushing(false)
robertphillipsa106c622015-10-16 09:07:06 -070040 , fFirstUnpreparedBatch(0)
robertphillips6a186652015-10-20 07:37:58 -070041 , fFlags(0) {
bsalomonb3b9aec2015-09-10 11:16:35 -070042 // TODO: Stop extracting the context (currently needed by GrClipMaskManager)
43 fContext = fGpu->getContext();
Brian Salomone66fec22015-09-10 14:40:20 -040044 fClipMaskManager.reset(new GrClipMaskManager(this));
bsalomon4061b122015-05-29 10:26:19 -070045}
46
47GrDrawTarget::~GrDrawTarget() {
48 fGpu->unref();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000049}
50
51////////////////////////////////////////////////////////////////////////////////
52
robertphillips6a186652015-10-20 07:37:58 -070053// Add a GrDrawTarget-based dependency
54void GrDrawTarget::addDependency(GrDrawTarget* dependedOn) {
55 SkASSERT(!dependedOn->dependsOn(this)); // loops are bad
56
57 if (this->dependsOn(dependedOn)) {
58 return; // don't add duplicate dependencies
59 }
60
61 *fDependencies.push() = dependedOn;
62}
63
64// Convert from a GrSurface-based dependency to a GrDrawTarget one
65void GrDrawTarget::addDependency(GrSurface* dependedOn) {
66 if (dependedOn->asRenderTarget() && dependedOn->asRenderTarget()->getLastDrawTarget()) {
67 // If it is still receiving dependencies, this DT shouldn't be closed
68 SkASSERT(!this->isClosed());
69
70 GrDrawTarget* dt = dependedOn->asRenderTarget()->getLastDrawTarget();
71 if (dt == this) {
72 // self-read - presumably for dst reads
73 } else {
74 this->addDependency(dt);
75
76 // Can't make it closed in the self-read case
77 dt->makeClosed();
78 }
79 }
80}
81
bsalomon50785a32015-02-06 07:02:37 -080082bool GrDrawTarget::setupDstReadIfNecessary(const GrPipelineBuilder& pipelineBuilder,
egdaniele36914c2015-02-13 09:00:33 -080083 const GrProcOptInfo& colorPOI,
84 const GrProcOptInfo& coveragePOI,
bsalomon6a44c6a2015-05-26 09:49:05 -070085 GrXferProcessor::DstTexture* dstTexture,
bsalomonad792c12015-09-10 11:10:50 -070086 const SkRect& batchBounds) {
87 SkRect bounds = batchBounds;
88 bounds.outset(0.5f, 0.5f);
89
bsalomon6a44c6a2015-05-26 09:49:05 -070090 if (!pipelineBuilder.willXPNeedDstTexture(*this->caps(), colorPOI, coveragePOI)) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +000091 return true;
92 }
cdalton9954bc32015-04-29 14:17:00 -070093
bsalomon50785a32015-02-06 07:02:37 -080094 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
cdalton9954bc32015-04-29 14:17:00 -070095
96 if (this->caps()->textureBarrierSupport()) {
97 if (GrTexture* rtTex = rt->asTexture()) {
bsalomondc47ff72015-05-26 12:16:59 -070098 // The render target is a texture, so we can read from it directly in the shader. The XP
cdalton9954bc32015-04-29 14:17:00 -070099 // will be responsible to detect this situation and request a texture barrier.
bsalomon6a44c6a2015-05-26 09:49:05 -0700100 dstTexture->setTexture(rtTex);
101 dstTexture->setOffset(0, 0);
cdalton9954bc32015-04-29 14:17:00 -0700102 return true;
103 }
104 }
105
106 SkIRect copyRect;
joshualitt44701df2015-02-23 14:44:57 -0800107 pipelineBuilder.clip().getConservativeBounds(rt, &copyRect);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000108
bsalomonad792c12015-09-10 11:10:50 -0700109 SkIRect drawIBounds;
110 bounds.roundOut(&drawIBounds);
111 if (!copyRect.intersect(drawIBounds)) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000112#ifdef SK_DEBUG
bsalomonb3b9aec2015-09-10 11:16:35 -0700113 GrCapsDebugf(this->caps(), "Missed an early reject. "
114 "Bailing on draw from setupDstReadIfNecessary.\n");
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000115#endif
bsalomonad792c12015-09-10 11:10:50 -0700116 return false;
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000117 }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000118
commit-bot@chromium.org63150af2013-04-11 22:00:22 +0000119 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
120 // have per-sample dst values by making the copy multisampled.
bsalomonf2703d82014-10-28 14:33:06 -0700121 GrSurfaceDesc desc;
bsalomonb3b9aec2015-09-10 11:16:35 -0700122 if (!fGpu->initCopySurfaceDstDesc(rt, &desc)) {
bsalomona73239a2015-04-28 13:35:17 -0700123 desc.fOrigin = kDefault_GrSurfaceOrigin;
124 desc.fFlags = kRenderTarget_GrSurfaceFlag;
125 desc.fConfig = rt->config();
126 }
127
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000128 desc.fWidth = copyRect.width();
129 desc.fHeight = copyRect.height();
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000130
bsalomoneae62002015-07-31 13:59:30 -0700131 static const uint32_t kFlags = 0;
132 SkAutoTUnref<GrTexture> copy(fResourceProvider->createApproxTexture(desc, kFlags));
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000133
bsalomone3059732014-10-14 11:47:22 -0700134 if (!copy) {
tfarina38406c82014-10-31 07:11:12 -0700135 SkDebugf("Failed to create temporary copy of destination texture.\n");
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000136 return false;
137 }
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000138 SkIPoint dstPoint = {0, 0};
bsalomon6df86402015-06-01 10:41:49 -0700139 this->copySurface(copy, rt, copyRect, dstPoint);
140 dstTexture->setTexture(copy);
141 dstTexture->setOffset(copyRect.fLeft, copyRect.fTop);
142 return true;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000143}
144
bsalomona73239a2015-04-28 13:35:17 -0700145void GrDrawTarget::flush() {
146 if (fFlushing) {
147 return;
148 }
149 fFlushing = true;
150
robertphillipsa106c622015-10-16 09:07:06 -0700151 // Semi-usually the drawTargets are already closed at this point, but sometimes Ganesh
152 // needs to flush mid-draw. In that case, the SkGpuDevice's drawTargets won't be closed
153 // but need to be flushed anyway. Closing such drawTargets here will mean new
154 // drawTargets will be created to replace them if the SkGpuDevice(s) write to them again.
155 this->makeClosed();
156
bsalomon512be532015-09-10 10:42:55 -0700157 // Loop over all batches and generate geometry
joshualittf6d259b2015-10-02 11:27:14 -0700158 for (; fFirstUnpreparedBatch < fBatches.count(); ++fFirstUnpreparedBatch) {
159 fBatches[fFirstUnpreparedBatch]->prepare(&fFlushState);
bsalomon512be532015-09-10 10:42:55 -0700160 }
161
162 // Upload all data to the GPU
joshualittf6d259b2015-10-02 11:27:14 -0700163 fFlushState.preIssueDraws();
bsalomon512be532015-09-10 10:42:55 -0700164
165 // Draw all the generated geometry.
166 for (int i = 0; i < fBatches.count(); ++i) {
joshualittf6d259b2015-10-02 11:27:14 -0700167 fBatches[i]->draw(&fFlushState);
bsalomon512be532015-09-10 10:42:55 -0700168 }
169
joshualittf6d259b2015-10-02 11:27:14 -0700170 SkASSERT(fFlushState.lastFlushedToken() == fFlushState.currentToken());
171 this->reset();
bsalomona73239a2015-04-28 13:35:17 -0700172
bsalomona73239a2015-04-28 13:35:17 -0700173 fFlushing = false;
bsalomona73239a2015-04-28 13:35:17 -0700174}
175
bsalomon512be532015-09-10 10:42:55 -0700176void GrDrawTarget::reset() {
joshualittf6d259b2015-10-02 11:27:14 -0700177 fFirstUnpreparedBatch = 0;
bsalomon512be532015-09-10 10:42:55 -0700178 fBatches.reset();
joshualittf6d259b2015-10-02 11:27:14 -0700179 fFlushState.reset();
bsalomon512be532015-09-10 10:42:55 -0700180}
181
bsalomonabd30f52015-08-13 13:34:48 -0700182void GrDrawTarget::drawBatch(const GrPipelineBuilder& pipelineBuilder, GrDrawBatch* batch) {
joshualitt4d8da812015-01-28 12:53:54 -0800183 // Setup clip
184 GrScissorState scissorState;
joshualitt4d8da812015-01-28 12:53:54 -0800185 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomon0ba8c242015-10-07 09:20:28 -0700186 GrAppliedClip clip;
187 if (!fClipMaskManager->setupClipping(pipelineBuilder, &ars, &scissorState, &batch->bounds(),
188 &clip)) {
joshualitt4d8da812015-01-28 12:53:54 -0800189 return;
190 }
bsalomon0ba8c242015-10-07 09:20:28 -0700191 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
192 if (clip.clipCoverageFragmentProcessor()) {
193 arfps.set(&pipelineBuilder);
194 arfps.addCoverageFragmentProcessor(clip.clipCoverageFragmentProcessor());
195 }
joshualitt4d8da812015-01-28 12:53:54 -0800196
bsalomonad792c12015-09-10 11:10:50 -0700197 GrPipeline::CreateArgs args;
198 if (!this->installPipelineInDrawBatch(&pipelineBuilder, &scissorState, batch)) {
egdaniele36914c2015-02-13 09:00:33 -0800199 return;
200 }
bsalomonad792c12015-09-10 11:10:50 -0700201
bsalomon512be532015-09-10 10:42:55 -0700202 this->recordBatch(batch);
joshualitt4d8da812015-01-28 12:53:54 -0800203}
204
joshualitt2c93efe2014-11-06 12:57:13 -0800205static const GrStencilSettings& winding_path_stencil_settings() {
206 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
207 kIncClamp_StencilOp,
208 kIncClamp_StencilOp,
209 kAlwaysIfInClip_StencilFunc,
210 0xFFFF, 0xFFFF, 0xFFFF);
211 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
212}
213
214static const GrStencilSettings& even_odd_path_stencil_settings() {
215 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
216 kInvert_StencilOp,
217 kInvert_StencilOp,
218 kAlwaysIfInClip_StencilFunc,
219 0xFFFF, 0xFFFF, 0xFFFF);
220 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
221}
222
223void GrDrawTarget::getPathStencilSettingsForFilltype(GrPathRendering::FillType fill,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700224 const GrStencilAttachment* sb,
joshualitt2c93efe2014-11-06 12:57:13 -0800225 GrStencilSettings* outStencilSettings) {
226
227 switch (fill) {
228 default:
229 SkFAIL("Unexpected path fill.");
230 case GrPathRendering::kWinding_FillType:
231 *outStencilSettings = winding_path_stencil_settings();
232 break;
233 case GrPathRendering::kEvenOdd_FillType:
234 *outStencilSettings = even_odd_path_stencil_settings();
235 break;
236 }
bsalomonb3b9aec2015-09-10 11:16:35 -0700237 fClipMaskManager->adjustPathStencilParams(sb, outStencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800238}
239
joshualitt1c735482015-07-13 08:08:25 -0700240void GrDrawTarget::stencilPath(const GrPipelineBuilder& pipelineBuilder,
joshualittf2384692015-09-10 11:00:51 -0700241 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800242 const GrPath* path,
243 GrPathRendering::FillType fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000244 // TODO: extract portions of checkDraw that are relevant to path stenciling.
bsalomon49f085d2014-09-05 13:34:00 -0700245 SkASSERT(path);
jvanverthe9c0fc62015-04-29 11:18:05 -0700246 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
joshualitt2c93efe2014-11-06 12:57:13 -0800247
248 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800249 GrScissorState scissorState;
egdaniel8dd688b2015-01-22 10:16:09 -0800250 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomon0ba8c242015-10-07 09:20:28 -0700251 GrAppliedClip clip;
252 if (!fClipMaskManager->setupClipping(pipelineBuilder, &ars, &scissorState, nullptr, &clip)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800253 return;
254 }
255
bsalomon0ba8c242015-10-07 09:20:28 -0700256 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
257 if (clip.clipCoverageFragmentProcessor()) {
258 arfps.set(&pipelineBuilder);
259 arfps.addCoverageFragmentProcessor(clip.clipCoverageFragmentProcessor());
260 }
261
joshualitt2c93efe2014-11-06 12:57:13 -0800262 // set stencil settings for path
263 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700264 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdanielec00d942015-09-14 12:56:10 -0700265 GrStencilAttachment* sb = fResourceProvider->attachStencilAttachment(rt);
bsalomon6bc1b5f2015-02-23 09:06:38 -0800266 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800267
joshualittf2384692015-09-10 11:00:51 -0700268 GrBatch* batch = GrStencilPathBatch::Create(viewMatrix,
bsalomona44919e2015-08-18 13:28:19 -0700269 pipelineBuilder.isHWAntialias(),
270 stencilSettings, scissorState,
271 pipelineBuilder.getRenderTarget(),
272 path);
bsalomon512be532015-09-10 10:42:55 -0700273 this->recordBatch(batch);
bsalomona44919e2015-08-18 13:28:19 -0700274 batch->unref();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000275}
276
joshualitt1c735482015-07-13 08:08:25 -0700277void GrDrawTarget::drawPath(const GrPipelineBuilder& pipelineBuilder,
joshualittf2384692015-09-10 11:00:51 -0700278 const SkMatrix& viewMatrix,
279 GrColor color,
joshualitt9853cce2014-11-17 14:22:48 -0800280 const GrPath* path,
281 GrPathRendering::FillType fill) {
bsalomon49f085d2014-09-05 13:34:00 -0700282 SkASSERT(path);
jvanverthe9c0fc62015-04-29 11:18:05 -0700283 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000284
joshualittf2384692015-09-10 11:00:51 -0700285 GrDrawPathBatchBase* batch = GrDrawPathBatch::Create(viewMatrix, color, path);
bsalomon1fcc01c2015-09-09 09:48:06 -0700286 this->drawPathBatch(pipelineBuilder, batch, fill);
287 batch->unref();
288}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000289
bsalomon1fcc01c2015-09-09 09:48:06 -0700290void GrDrawTarget::drawPathsFromRange(const GrPipelineBuilder& pipelineBuilder,
joshualittf2384692015-09-10 11:00:51 -0700291 const SkMatrix& viewMatrix,
292 const SkMatrix& localMatrix,
293 GrColor color,
cdalton8585dd22015-10-08 08:04:09 -0700294 GrPathRange* range,
bsalomon1fcc01c2015-09-09 09:48:06 -0700295 GrPathRangeDraw* draw,
296 GrPathRendering::FillType fill) {
cdalton8585dd22015-10-08 08:04:09 -0700297 GrDrawPathBatchBase* batch = GrDrawPathRangeBatch::Create(viewMatrix, localMatrix, color,
298 range, draw);
bsalomon1fcc01c2015-09-09 09:48:06 -0700299 this->drawPathBatch(pipelineBuilder, batch, fill);
300 batch->unref();
301}
302
303void GrDrawTarget::drawPathBatch(const GrPipelineBuilder& pipelineBuilder,
304 GrDrawPathBatchBase* batch,
305 GrPathRendering::FillType fill) {
bsalomonadd79ef2015-08-19 13:26:49 -0700306 // This looks like drawBatch() but there is an added wrinkle that stencil settings get inserted
bsalomonb3b9aec2015-09-10 11:16:35 -0700307 // after setting up clipping but before onDrawBatch(). TODO: Figure out a better model for
308 // handling stencil settings WRT interactions between pipeline(builder), clipmaskmanager, and
309 // batches.
bsalomonadd79ef2015-08-19 13:26:49 -0700310
bsalomon3e791242014-12-17 13:43:13 -0800311 GrScissorState scissorState;
egdaniel8dd688b2015-01-22 10:16:09 -0800312 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomon0ba8c242015-10-07 09:20:28 -0700313 GrAppliedClip clip;
314 if (!fClipMaskManager->setupClipping(pipelineBuilder, &ars, &scissorState, &batch->bounds(),
315 &clip)) {
bsalomonadd79ef2015-08-19 13:26:49 -0700316 return;
joshualitt2c93efe2014-11-06 12:57:13 -0800317 }
318
bsalomon0ba8c242015-10-07 09:20:28 -0700319 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
320 if (clip.clipCoverageFragmentProcessor()) {
321 arfps.set(&pipelineBuilder);
322 arfps.addCoverageFragmentProcessor(clip.clipCoverageFragmentProcessor());
323 }
324
bsalomonadd79ef2015-08-19 13:26:49 -0700325 // Ensure the render target has a stencil buffer and get the stencil settings.
joshualitt2c93efe2014-11-06 12:57:13 -0800326 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700327 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdanielec00d942015-09-14 12:56:10 -0700328 GrStencilAttachment* sb = fResourceProvider->attachStencilAttachment(rt);
bsalomon6bc1b5f2015-02-23 09:06:38 -0800329 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
bsalomonadd79ef2015-08-19 13:26:49 -0700330 batch->setStencilSettings(stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800331
bsalomonad792c12015-09-10 11:10:50 -0700332 GrPipeline::CreateArgs args;
333 if (!this->installPipelineInDrawBatch(&pipelineBuilder, &scissorState, batch)) {
bsalomonadd79ef2015-08-19 13:26:49 -0700334 return;
335 }
egdaniele36914c2015-02-13 09:00:33 -0800336
bsalomon512be532015-09-10 10:42:55 -0700337 this->recordBatch(batch);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000338}
339
joshualittd2b23e02015-08-21 10:53:34 -0700340void GrDrawTarget::drawNonAARect(const GrPipelineBuilder& pipelineBuilder,
joshualitt1c735482015-07-13 08:08:25 -0700341 GrColor color,
342 const SkMatrix& viewMatrix,
joshualittb6b513b2015-08-21 10:25:18 -0700343 const SkRect& rect) {
joshualittd2b23e02015-08-21 10:53:34 -0700344 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect,
345 nullptr, nullptr));
joshualittad17cfc2015-05-05 10:45:57 -0700346 this->drawBatch(pipelineBuilder, batch);
347}
348
joshualittd2b23e02015-08-21 10:53:34 -0700349void GrDrawTarget::drawNonAARect(const GrPipelineBuilder& pipelineBuilder,
joshualittb6b513b2015-08-21 10:25:18 -0700350 GrColor color,
351 const SkMatrix& viewMatrix,
352 const SkRect& rect,
353 const SkMatrix& localMatrix) {
joshualittd2b23e02015-08-21 10:53:34 -0700354 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect,
355 nullptr, &localMatrix));
joshualittb6b513b2015-08-21 10:25:18 -0700356 this->drawBatch(pipelineBuilder, batch);
357}
358
joshualittd2b23e02015-08-21 10:53:34 -0700359void GrDrawTarget::drawNonAARect(const GrPipelineBuilder& pipelineBuilder,
joshualittb6b513b2015-08-21 10:25:18 -0700360 GrColor color,
361 const SkMatrix& viewMatrix,
362 const SkRect& rect,
363 const SkRect& localRect) {
joshualittd2b23e02015-08-21 10:53:34 -0700364 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect,
365 &localRect, nullptr));
joshualittb6b513b2015-08-21 10:25:18 -0700366 this->drawBatch(pipelineBuilder, batch);
367}
368
369
joshualitt1c735482015-07-13 08:08:25 -0700370void GrDrawTarget::drawAARect(const GrPipelineBuilder& pipelineBuilder,
robertphillipsea461502015-05-26 11:38:03 -0700371 GrColor color,
372 const SkMatrix& viewMatrix,
373 const SkRect& rect,
374 const SkRect& devRect) {
joshualittd2b23e02015-08-21 10:53:34 -0700375 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateAAFill(color, viewMatrix, rect,
bsalomonabd30f52015-08-13 13:34:48 -0700376 devRect));
joshualitt14205b12015-08-10 11:40:56 -0700377 this->drawBatch(pipelineBuilder, batch);
robertphillipsea461502015-05-26 11:38:03 -0700378}
379
joshualitt9853cce2014-11-17 14:22:48 -0800380void GrDrawTarget::clear(const SkIRect* rect,
381 GrColor color,
382 bool canIgnoreRect,
bsalomon63b21962014-11-05 07:05:34 -0800383 GrRenderTarget* renderTarget) {
egdaniel51c8d402015-08-06 10:54:13 -0700384 SkIRect rtRect = SkIRect::MakeWH(renderTarget->width(), renderTarget->height());
385 SkIRect clippedRect;
386 if (!rect ||
387 (canIgnoreRect && this->caps()->fullClearIsFree()) ||
388 rect->contains(rtRect)) {
389 rect = &rtRect;
390 } else {
391 clippedRect = *rect;
392 if (!clippedRect.intersect(rtRect)) {
393 return;
394 }
395 rect = &clippedRect;
396 }
397
bsalomonb3b9aec2015-09-10 11:16:35 -0700398 if (this->caps()->useDrawInsteadOfClear()) {
bsalomon63b21962014-11-05 07:05:34 -0800399 // This works around a driver bug with clear by drawing a rect instead.
400 // The driver will ignore a clear if it is the only thing rendered to a
401 // target before the target is read.
egdaniel51c8d402015-08-06 10:54:13 -0700402 if (rect == &rtRect) {
bsalomon63b21962014-11-05 07:05:34 -0800403 this->discard(renderTarget);
404 }
bsalomon63b21962014-11-05 07:05:34 -0800405
egdaniel8dd688b2015-01-22 10:16:09 -0800406 GrPipelineBuilder pipelineBuilder;
407 pipelineBuilder.setRenderTarget(renderTarget);
joshualitt9853cce2014-11-17 14:22:48 -0800408
joshualittd2b23e02015-08-21 10:53:34 -0700409 this->drawNonAARect(pipelineBuilder, color, SkMatrix::I(), *rect);
bsalomon53469832015-08-18 09:20:09 -0700410 } else {
halcanary385fe4d2015-08-26 13:07:48 -0700411 GrBatch* batch = new GrClearBatch(*rect, color, renderTarget);
bsalomon512be532015-09-10 10:42:55 -0700412 this->recordBatch(batch);
bsalomon53469832015-08-18 09:20:09 -0700413 batch->unref();
414 }
415}
416
417void GrDrawTarget::discard(GrRenderTarget* renderTarget) {
418 if (this->caps()->discardRenderTargetSupport()) {
halcanary385fe4d2015-08-26 13:07:48 -0700419 GrBatch* batch = new GrDiscardBatch(renderTarget);
bsalomon512be532015-09-10 10:42:55 -0700420 this->recordBatch(batch);
bsalomon53469832015-08-18 09:20:09 -0700421 batch->unref();
bsalomon63b21962014-11-05 07:05:34 -0800422 }
423}
424
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000425////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000426
bsalomon6df86402015-06-01 10:41:49 -0700427void GrDrawTarget::copySurface(GrSurface* dst,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000428 GrSurface* src,
429 const SkIRect& srcRect,
430 const SkIPoint& dstPoint) {
bsalomon872062c2015-08-18 12:12:35 -0700431 GrBatch* batch = GrCopySurfaceBatch::Create(dst, src, srcRect, dstPoint);
432 if (batch) {
bsalomon512be532015-09-10 10:42:55 -0700433 this->recordBatch(batch);
bsalomon872062c2015-08-18 12:12:35 -0700434 batch->unref();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000435 }
bsalomon@google.comeb851172013-04-15 13:51:00 +0000436}
437
bsalomon512be532015-09-10 10:42:55 -0700438template <class Left, class Right> static bool intersect(const Left& a, const Right& b) {
439 SkASSERT(a.fLeft <= a.fRight && a.fTop <= a.fBottom &&
440 b.fLeft <= b.fRight && b.fTop <= b.fBottom);
441 return a.fLeft < b.fRight && b.fLeft < a.fRight && a.fTop < b.fBottom && b.fTop < a.fBottom;
442}
443
444void GrDrawTarget::recordBatch(GrBatch* batch) {
robertphillipsa106c622015-10-16 09:07:06 -0700445 // A closed drawTarget should never receive new/more batches
robertphillips6a186652015-10-20 07:37:58 -0700446 SkASSERT(!this->isClosed());
robertphillipsa106c622015-10-16 09:07:06 -0700447
bsalomon512be532015-09-10 10:42:55 -0700448 // Check if there is a Batch Draw we can batch with by linearly searching back until we either
449 // 1) check every draw
450 // 2) intersect with something
451 // 3) find a 'blocker'
452 // Experimentally we have found that most batching occurs within the first 10 comparisons.
453 static const int kMaxLookback = 10;
454
455 GrBATCH_INFO("Re-Recording (%s, B%u)\n"
joshualitte2bcec32015-09-30 06:22:22 -0700456 "\tBounds LRTB (%f, %f, %f, %f)\n",
bsalomon512be532015-09-10 10:42:55 -0700457 batch->name(),
458 batch->uniqueID(),
459 batch->bounds().fLeft, batch->bounds().fRight,
460 batch->bounds().fTop, batch->bounds().fBottom);
461 GrBATCH_INFO(SkTabString(batch->dumpInfo(), 1).c_str());
462 GrBATCH_INFO("\tOutcome:\n");
463 int maxCandidates = SkTMin(kMaxLookback, fBatches.count());
464 if (maxCandidates) {
465 int i = 0;
466 while (true) {
467 GrBatch* candidate = fBatches.fromBack(i);
468 // We cannot continue to search backwards if the render target changes
469 if (candidate->renderTargetUniqueID() != batch->renderTargetUniqueID()) {
470 GrBATCH_INFO("\t\tBreaking because of (%s, B%u) Rendertarget\n",
471 candidate->name(), candidate->uniqueID());
472 break;
473 }
474 if (candidate->combineIfPossible(batch, *this->caps())) {
475 GrBATCH_INFO("\t\tCombining with (%s, B%u)\n", candidate->name(),
476 candidate->uniqueID());
477 return;
478 }
479 // Stop going backwards if we would cause a painter's order violation.
480 if (intersect(candidate->bounds(), batch->bounds())) {
481 GrBATCH_INFO("\t\tIntersects with (%s, B%u)\n", candidate->name(),
482 candidate->uniqueID());
483 break;
484 }
485 ++i;
486 if (i == maxCandidates) {
487 GrBATCH_INFO("\t\tReached max lookback or beginning of batch array %d\n", i);
488 break;
489 }
490 }
491 } else {
492 GrBATCH_INFO("\t\tFirstBatch\n");
493 }
494 fBatches.push_back().reset(SkRef(batch));
joshualittf6d259b2015-10-02 11:27:14 -0700495 if (fBatches.count() > kMaxLookback) {
496 SkASSERT(fBatches.count() - kMaxLookback - fFirstUnpreparedBatch == 1);
497 fBatches[fFirstUnpreparedBatch++]->prepare(&fFlushState);
498 }
bsalomon512be532015-09-10 10:42:55 -0700499}
500
egdaniele36914c2015-02-13 09:00:33 -0800501///////////////////////////////////////////////////////////////////////////////
502
bsalomonad792c12015-09-10 11:10:50 -0700503bool GrDrawTarget::installPipelineInDrawBatch(const GrPipelineBuilder* pipelineBuilder,
504 const GrScissorState* scissor,
505 GrDrawBatch* batch) {
506 GrPipeline::CreateArgs args;
507 args.fPipelineBuilder = pipelineBuilder;
508 args.fCaps = this->caps();
509 args.fScissor = scissor;
510 args.fColorPOI = pipelineBuilder->colorProcInfo(batch);
511 args.fCoveragePOI = pipelineBuilder->coverageProcInfo(batch);
512 if (!this->setupDstReadIfNecessary(*pipelineBuilder, args.fColorPOI,
513 args.fCoveragePOI, &args.fDstTexture,
514 batch->bounds())) {
515 return false;
egdaniele36914c2015-02-13 09:00:33 -0800516 }
bsalomonad792c12015-09-10 11:10:50 -0700517
518 if (!batch->installPipeline(args)) {
519 return false;
520 }
521
522 return true;
egdaniele36914c2015-02-13 09:00:33 -0800523}
524
bsalomonb3b9aec2015-09-10 11:16:35 -0700525void GrDrawTarget::clearStencilClip(const SkIRect& rect, bool insideClip, GrRenderTarget* rt) {
halcanary385fe4d2015-08-26 13:07:48 -0700526 GrBatch* batch = new GrClearStencilClipBatch(rect, insideClip, rt);
bsalomon512be532015-09-10 10:42:55 -0700527 this->recordBatch(batch);
bsalomon5ea03632015-08-18 10:33:30 -0700528 batch->unref();
529}