blob: 323599dd7035ba9161edc028e847027420107bc8 [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)
41 , fClosed(false) {
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
bsalomon50785a32015-02-06 07:02:37 -080053bool GrDrawTarget::setupDstReadIfNecessary(const GrPipelineBuilder& pipelineBuilder,
egdaniele36914c2015-02-13 09:00:33 -080054 const GrProcOptInfo& colorPOI,
55 const GrProcOptInfo& coveragePOI,
bsalomon6a44c6a2015-05-26 09:49:05 -070056 GrXferProcessor::DstTexture* dstTexture,
bsalomonad792c12015-09-10 11:10:50 -070057 const SkRect& batchBounds) {
58 SkRect bounds = batchBounds;
59 bounds.outset(0.5f, 0.5f);
60
bsalomon6a44c6a2015-05-26 09:49:05 -070061 if (!pipelineBuilder.willXPNeedDstTexture(*this->caps(), colorPOI, coveragePOI)) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +000062 return true;
63 }
cdalton9954bc32015-04-29 14:17:00 -070064
bsalomon50785a32015-02-06 07:02:37 -080065 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
cdalton9954bc32015-04-29 14:17:00 -070066
67 if (this->caps()->textureBarrierSupport()) {
68 if (GrTexture* rtTex = rt->asTexture()) {
bsalomondc47ff72015-05-26 12:16:59 -070069 // The render target is a texture, so we can read from it directly in the shader. The XP
cdalton9954bc32015-04-29 14:17:00 -070070 // will be responsible to detect this situation and request a texture barrier.
bsalomon6a44c6a2015-05-26 09:49:05 -070071 dstTexture->setTexture(rtTex);
72 dstTexture->setOffset(0, 0);
cdalton9954bc32015-04-29 14:17:00 -070073 return true;
74 }
75 }
76
77 SkIRect copyRect;
joshualitt44701df2015-02-23 14:44:57 -080078 pipelineBuilder.clip().getConservativeBounds(rt, &copyRect);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000079
bsalomonad792c12015-09-10 11:10:50 -070080 SkIRect drawIBounds;
81 bounds.roundOut(&drawIBounds);
82 if (!copyRect.intersect(drawIBounds)) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000083#ifdef SK_DEBUG
bsalomonb3b9aec2015-09-10 11:16:35 -070084 GrCapsDebugf(this->caps(), "Missed an early reject. "
85 "Bailing on draw from setupDstReadIfNecessary.\n");
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +000086#endif
bsalomonad792c12015-09-10 11:10:50 -070087 return false;
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +000088 }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +000089
commit-bot@chromium.org63150af2013-04-11 22:00:22 +000090 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
91 // have per-sample dst values by making the copy multisampled.
bsalomonf2703d82014-10-28 14:33:06 -070092 GrSurfaceDesc desc;
bsalomonb3b9aec2015-09-10 11:16:35 -070093 if (!fGpu->initCopySurfaceDstDesc(rt, &desc)) {
bsalomona73239a2015-04-28 13:35:17 -070094 desc.fOrigin = kDefault_GrSurfaceOrigin;
95 desc.fFlags = kRenderTarget_GrSurfaceFlag;
96 desc.fConfig = rt->config();
97 }
98
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +000099 desc.fWidth = copyRect.width();
100 desc.fHeight = copyRect.height();
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000101
bsalomoneae62002015-07-31 13:59:30 -0700102 static const uint32_t kFlags = 0;
103 SkAutoTUnref<GrTexture> copy(fResourceProvider->createApproxTexture(desc, kFlags));
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000104
bsalomone3059732014-10-14 11:47:22 -0700105 if (!copy) {
tfarina38406c82014-10-31 07:11:12 -0700106 SkDebugf("Failed to create temporary copy of destination texture.\n");
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000107 return false;
108 }
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000109 SkIPoint dstPoint = {0, 0};
bsalomon6df86402015-06-01 10:41:49 -0700110 this->copySurface(copy, rt, copyRect, dstPoint);
111 dstTexture->setTexture(copy);
112 dstTexture->setOffset(copyRect.fLeft, copyRect.fTop);
113 return true;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000114}
115
bsalomona73239a2015-04-28 13:35:17 -0700116void GrDrawTarget::flush() {
117 if (fFlushing) {
118 return;
119 }
120 fFlushing = true;
121
robertphillipsa106c622015-10-16 09:07:06 -0700122 // Semi-usually the drawTargets are already closed at this point, but sometimes Ganesh
123 // needs to flush mid-draw. In that case, the SkGpuDevice's drawTargets won't be closed
124 // but need to be flushed anyway. Closing such drawTargets here will mean new
125 // drawTargets will be created to replace them if the SkGpuDevice(s) write to them again.
126 this->makeClosed();
127
bsalomon512be532015-09-10 10:42:55 -0700128 // Loop over all batches and generate geometry
joshualittf6d259b2015-10-02 11:27:14 -0700129 for (; fFirstUnpreparedBatch < fBatches.count(); ++fFirstUnpreparedBatch) {
130 fBatches[fFirstUnpreparedBatch]->prepare(&fFlushState);
bsalomon512be532015-09-10 10:42:55 -0700131 }
132
133 // Upload all data to the GPU
joshualittf6d259b2015-10-02 11:27:14 -0700134 fFlushState.preIssueDraws();
bsalomon512be532015-09-10 10:42:55 -0700135
136 // Draw all the generated geometry.
137 for (int i = 0; i < fBatches.count(); ++i) {
joshualittf6d259b2015-10-02 11:27:14 -0700138 fBatches[i]->draw(&fFlushState);
bsalomon512be532015-09-10 10:42:55 -0700139 }
140
joshualittf6d259b2015-10-02 11:27:14 -0700141 SkASSERT(fFlushState.lastFlushedToken() == fFlushState.currentToken());
142 this->reset();
bsalomona73239a2015-04-28 13:35:17 -0700143
bsalomona73239a2015-04-28 13:35:17 -0700144 fFlushing = false;
bsalomona73239a2015-04-28 13:35:17 -0700145}
146
bsalomon512be532015-09-10 10:42:55 -0700147void GrDrawTarget::reset() {
joshualittf6d259b2015-10-02 11:27:14 -0700148 fFirstUnpreparedBatch = 0;
bsalomon512be532015-09-10 10:42:55 -0700149 fBatches.reset();
joshualittf6d259b2015-10-02 11:27:14 -0700150 fFlushState.reset();
bsalomon512be532015-09-10 10:42:55 -0700151}
152
bsalomonabd30f52015-08-13 13:34:48 -0700153void GrDrawTarget::drawBatch(const GrPipelineBuilder& pipelineBuilder, GrDrawBatch* batch) {
joshualitt4d8da812015-01-28 12:53:54 -0800154 // Setup clip
155 GrScissorState scissorState;
joshualitt4d8da812015-01-28 12:53:54 -0800156 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomon0ba8c242015-10-07 09:20:28 -0700157 GrAppliedClip clip;
158 if (!fClipMaskManager->setupClipping(pipelineBuilder, &ars, &scissorState, &batch->bounds(),
159 &clip)) {
joshualitt4d8da812015-01-28 12:53:54 -0800160 return;
161 }
bsalomon0ba8c242015-10-07 09:20:28 -0700162 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
163 if (clip.clipCoverageFragmentProcessor()) {
164 arfps.set(&pipelineBuilder);
165 arfps.addCoverageFragmentProcessor(clip.clipCoverageFragmentProcessor());
166 }
joshualitt4d8da812015-01-28 12:53:54 -0800167
bsalomonad792c12015-09-10 11:10:50 -0700168 GrPipeline::CreateArgs args;
169 if (!this->installPipelineInDrawBatch(&pipelineBuilder, &scissorState, batch)) {
egdaniele36914c2015-02-13 09:00:33 -0800170 return;
171 }
bsalomonad792c12015-09-10 11:10:50 -0700172
bsalomon512be532015-09-10 10:42:55 -0700173 this->recordBatch(batch);
joshualitt4d8da812015-01-28 12:53:54 -0800174}
175
joshualitt2c93efe2014-11-06 12:57:13 -0800176static const GrStencilSettings& winding_path_stencil_settings() {
177 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
178 kIncClamp_StencilOp,
179 kIncClamp_StencilOp,
180 kAlwaysIfInClip_StencilFunc,
181 0xFFFF, 0xFFFF, 0xFFFF);
182 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
183}
184
185static const GrStencilSettings& even_odd_path_stencil_settings() {
186 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
187 kInvert_StencilOp,
188 kInvert_StencilOp,
189 kAlwaysIfInClip_StencilFunc,
190 0xFFFF, 0xFFFF, 0xFFFF);
191 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
192}
193
194void GrDrawTarget::getPathStencilSettingsForFilltype(GrPathRendering::FillType fill,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700195 const GrStencilAttachment* sb,
joshualitt2c93efe2014-11-06 12:57:13 -0800196 GrStencilSettings* outStencilSettings) {
197
198 switch (fill) {
199 default:
200 SkFAIL("Unexpected path fill.");
201 case GrPathRendering::kWinding_FillType:
202 *outStencilSettings = winding_path_stencil_settings();
203 break;
204 case GrPathRendering::kEvenOdd_FillType:
205 *outStencilSettings = even_odd_path_stencil_settings();
206 break;
207 }
bsalomonb3b9aec2015-09-10 11:16:35 -0700208 fClipMaskManager->adjustPathStencilParams(sb, outStencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800209}
210
joshualitt1c735482015-07-13 08:08:25 -0700211void GrDrawTarget::stencilPath(const GrPipelineBuilder& pipelineBuilder,
joshualittf2384692015-09-10 11:00:51 -0700212 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800213 const GrPath* path,
214 GrPathRendering::FillType fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000215 // TODO: extract portions of checkDraw that are relevant to path stenciling.
bsalomon49f085d2014-09-05 13:34:00 -0700216 SkASSERT(path);
jvanverthe9c0fc62015-04-29 11:18:05 -0700217 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
joshualitt2c93efe2014-11-06 12:57:13 -0800218
219 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800220 GrScissorState scissorState;
egdaniel8dd688b2015-01-22 10:16:09 -0800221 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomon0ba8c242015-10-07 09:20:28 -0700222 GrAppliedClip clip;
223 if (!fClipMaskManager->setupClipping(pipelineBuilder, &ars, &scissorState, nullptr, &clip)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800224 return;
225 }
226
bsalomon0ba8c242015-10-07 09:20:28 -0700227 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
228 if (clip.clipCoverageFragmentProcessor()) {
229 arfps.set(&pipelineBuilder);
230 arfps.addCoverageFragmentProcessor(clip.clipCoverageFragmentProcessor());
231 }
232
joshualitt2c93efe2014-11-06 12:57:13 -0800233 // set stencil settings for path
234 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700235 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdanielec00d942015-09-14 12:56:10 -0700236 GrStencilAttachment* sb = fResourceProvider->attachStencilAttachment(rt);
bsalomon6bc1b5f2015-02-23 09:06:38 -0800237 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800238
joshualittf2384692015-09-10 11:00:51 -0700239 GrBatch* batch = GrStencilPathBatch::Create(viewMatrix,
bsalomona44919e2015-08-18 13:28:19 -0700240 pipelineBuilder.isHWAntialias(),
241 stencilSettings, scissorState,
242 pipelineBuilder.getRenderTarget(),
243 path);
bsalomon512be532015-09-10 10:42:55 -0700244 this->recordBatch(batch);
bsalomona44919e2015-08-18 13:28:19 -0700245 batch->unref();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000246}
247
joshualitt1c735482015-07-13 08:08:25 -0700248void GrDrawTarget::drawPath(const GrPipelineBuilder& pipelineBuilder,
joshualittf2384692015-09-10 11:00:51 -0700249 const SkMatrix& viewMatrix,
250 GrColor color,
joshualitt9853cce2014-11-17 14:22:48 -0800251 const GrPath* path,
252 GrPathRendering::FillType fill) {
bsalomon49f085d2014-09-05 13:34:00 -0700253 SkASSERT(path);
jvanverthe9c0fc62015-04-29 11:18:05 -0700254 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000255
joshualittf2384692015-09-10 11:00:51 -0700256 GrDrawPathBatchBase* batch = GrDrawPathBatch::Create(viewMatrix, color, path);
bsalomon1fcc01c2015-09-09 09:48:06 -0700257 this->drawPathBatch(pipelineBuilder, batch, fill);
258 batch->unref();
259}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000260
bsalomon1fcc01c2015-09-09 09:48:06 -0700261void GrDrawTarget::drawPathsFromRange(const GrPipelineBuilder& pipelineBuilder,
joshualittf2384692015-09-10 11:00:51 -0700262 const SkMatrix& viewMatrix,
263 const SkMatrix& localMatrix,
264 GrColor color,
cdalton8585dd22015-10-08 08:04:09 -0700265 GrPathRange* range,
bsalomon1fcc01c2015-09-09 09:48:06 -0700266 GrPathRangeDraw* draw,
267 GrPathRendering::FillType fill) {
cdalton8585dd22015-10-08 08:04:09 -0700268 GrDrawPathBatchBase* batch = GrDrawPathRangeBatch::Create(viewMatrix, localMatrix, color,
269 range, draw);
bsalomon1fcc01c2015-09-09 09:48:06 -0700270 this->drawPathBatch(pipelineBuilder, batch, fill);
271 batch->unref();
272}
273
274void GrDrawTarget::drawPathBatch(const GrPipelineBuilder& pipelineBuilder,
275 GrDrawPathBatchBase* batch,
276 GrPathRendering::FillType fill) {
bsalomonadd79ef2015-08-19 13:26:49 -0700277 // This looks like drawBatch() but there is an added wrinkle that stencil settings get inserted
bsalomonb3b9aec2015-09-10 11:16:35 -0700278 // after setting up clipping but before onDrawBatch(). TODO: Figure out a better model for
279 // handling stencil settings WRT interactions between pipeline(builder), clipmaskmanager, and
280 // batches.
bsalomonadd79ef2015-08-19 13:26:49 -0700281
bsalomon3e791242014-12-17 13:43:13 -0800282 GrScissorState scissorState;
egdaniel8dd688b2015-01-22 10:16:09 -0800283 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomon0ba8c242015-10-07 09:20:28 -0700284 GrAppliedClip clip;
285 if (!fClipMaskManager->setupClipping(pipelineBuilder, &ars, &scissorState, &batch->bounds(),
286 &clip)) {
bsalomonadd79ef2015-08-19 13:26:49 -0700287 return;
joshualitt2c93efe2014-11-06 12:57:13 -0800288 }
289
bsalomon0ba8c242015-10-07 09:20:28 -0700290 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
291 if (clip.clipCoverageFragmentProcessor()) {
292 arfps.set(&pipelineBuilder);
293 arfps.addCoverageFragmentProcessor(clip.clipCoverageFragmentProcessor());
294 }
295
bsalomonadd79ef2015-08-19 13:26:49 -0700296 // Ensure the render target has a stencil buffer and get the stencil settings.
joshualitt2c93efe2014-11-06 12:57:13 -0800297 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700298 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdanielec00d942015-09-14 12:56:10 -0700299 GrStencilAttachment* sb = fResourceProvider->attachStencilAttachment(rt);
bsalomon6bc1b5f2015-02-23 09:06:38 -0800300 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
bsalomonadd79ef2015-08-19 13:26:49 -0700301 batch->setStencilSettings(stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800302
bsalomonad792c12015-09-10 11:10:50 -0700303 GrPipeline::CreateArgs args;
304 if (!this->installPipelineInDrawBatch(&pipelineBuilder, &scissorState, batch)) {
bsalomonadd79ef2015-08-19 13:26:49 -0700305 return;
306 }
egdaniele36914c2015-02-13 09:00:33 -0800307
bsalomon512be532015-09-10 10:42:55 -0700308 this->recordBatch(batch);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000309}
310
joshualittd2b23e02015-08-21 10:53:34 -0700311void GrDrawTarget::drawNonAARect(const GrPipelineBuilder& pipelineBuilder,
joshualitt1c735482015-07-13 08:08:25 -0700312 GrColor color,
313 const SkMatrix& viewMatrix,
joshualittb6b513b2015-08-21 10:25:18 -0700314 const SkRect& rect) {
joshualittd2b23e02015-08-21 10:53:34 -0700315 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect,
316 nullptr, nullptr));
joshualittad17cfc2015-05-05 10:45:57 -0700317 this->drawBatch(pipelineBuilder, batch);
318}
319
joshualittd2b23e02015-08-21 10:53:34 -0700320void GrDrawTarget::drawNonAARect(const GrPipelineBuilder& pipelineBuilder,
joshualittb6b513b2015-08-21 10:25:18 -0700321 GrColor color,
322 const SkMatrix& viewMatrix,
323 const SkRect& rect,
324 const SkMatrix& localMatrix) {
joshualittd2b23e02015-08-21 10:53:34 -0700325 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect,
326 nullptr, &localMatrix));
joshualittb6b513b2015-08-21 10:25:18 -0700327 this->drawBatch(pipelineBuilder, batch);
328}
329
joshualittd2b23e02015-08-21 10:53:34 -0700330void GrDrawTarget::drawNonAARect(const GrPipelineBuilder& pipelineBuilder,
joshualittb6b513b2015-08-21 10:25:18 -0700331 GrColor color,
332 const SkMatrix& viewMatrix,
333 const SkRect& rect,
334 const SkRect& localRect) {
joshualittd2b23e02015-08-21 10:53:34 -0700335 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect,
336 &localRect, nullptr));
joshualittb6b513b2015-08-21 10:25:18 -0700337 this->drawBatch(pipelineBuilder, batch);
338}
339
340
joshualitt1c735482015-07-13 08:08:25 -0700341void GrDrawTarget::drawAARect(const GrPipelineBuilder& pipelineBuilder,
robertphillipsea461502015-05-26 11:38:03 -0700342 GrColor color,
343 const SkMatrix& viewMatrix,
344 const SkRect& rect,
345 const SkRect& devRect) {
joshualittd2b23e02015-08-21 10:53:34 -0700346 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateAAFill(color, viewMatrix, rect,
bsalomonabd30f52015-08-13 13:34:48 -0700347 devRect));
joshualitt14205b12015-08-10 11:40:56 -0700348 this->drawBatch(pipelineBuilder, batch);
robertphillipsea461502015-05-26 11:38:03 -0700349}
350
joshualitt9853cce2014-11-17 14:22:48 -0800351void GrDrawTarget::clear(const SkIRect* rect,
352 GrColor color,
353 bool canIgnoreRect,
bsalomon63b21962014-11-05 07:05:34 -0800354 GrRenderTarget* renderTarget) {
egdaniel51c8d402015-08-06 10:54:13 -0700355 SkIRect rtRect = SkIRect::MakeWH(renderTarget->width(), renderTarget->height());
356 SkIRect clippedRect;
357 if (!rect ||
358 (canIgnoreRect && this->caps()->fullClearIsFree()) ||
359 rect->contains(rtRect)) {
360 rect = &rtRect;
361 } else {
362 clippedRect = *rect;
363 if (!clippedRect.intersect(rtRect)) {
364 return;
365 }
366 rect = &clippedRect;
367 }
368
bsalomonb3b9aec2015-09-10 11:16:35 -0700369 if (this->caps()->useDrawInsteadOfClear()) {
bsalomon63b21962014-11-05 07:05:34 -0800370 // This works around a driver bug with clear by drawing a rect instead.
371 // The driver will ignore a clear if it is the only thing rendered to a
372 // target before the target is read.
egdaniel51c8d402015-08-06 10:54:13 -0700373 if (rect == &rtRect) {
bsalomon63b21962014-11-05 07:05:34 -0800374 this->discard(renderTarget);
375 }
bsalomon63b21962014-11-05 07:05:34 -0800376
egdaniel8dd688b2015-01-22 10:16:09 -0800377 GrPipelineBuilder pipelineBuilder;
378 pipelineBuilder.setRenderTarget(renderTarget);
joshualitt9853cce2014-11-17 14:22:48 -0800379
joshualittd2b23e02015-08-21 10:53:34 -0700380 this->drawNonAARect(pipelineBuilder, color, SkMatrix::I(), *rect);
bsalomon53469832015-08-18 09:20:09 -0700381 } else {
halcanary385fe4d2015-08-26 13:07:48 -0700382 GrBatch* batch = new GrClearBatch(*rect, color, renderTarget);
bsalomon512be532015-09-10 10:42:55 -0700383 this->recordBatch(batch);
bsalomon53469832015-08-18 09:20:09 -0700384 batch->unref();
385 }
386}
387
388void GrDrawTarget::discard(GrRenderTarget* renderTarget) {
389 if (this->caps()->discardRenderTargetSupport()) {
halcanary385fe4d2015-08-26 13:07:48 -0700390 GrBatch* batch = new GrDiscardBatch(renderTarget);
bsalomon512be532015-09-10 10:42:55 -0700391 this->recordBatch(batch);
bsalomon53469832015-08-18 09:20:09 -0700392 batch->unref();
bsalomon63b21962014-11-05 07:05:34 -0800393 }
394}
395
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000396////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000397
bsalomon6df86402015-06-01 10:41:49 -0700398void GrDrawTarget::copySurface(GrSurface* dst,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000399 GrSurface* src,
400 const SkIRect& srcRect,
401 const SkIPoint& dstPoint) {
bsalomon872062c2015-08-18 12:12:35 -0700402 GrBatch* batch = GrCopySurfaceBatch::Create(dst, src, srcRect, dstPoint);
403 if (batch) {
bsalomon512be532015-09-10 10:42:55 -0700404 this->recordBatch(batch);
bsalomon872062c2015-08-18 12:12:35 -0700405 batch->unref();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000406 }
bsalomon@google.comeb851172013-04-15 13:51:00 +0000407}
408
bsalomon512be532015-09-10 10:42:55 -0700409template <class Left, class Right> static bool intersect(const Left& a, const Right& b) {
410 SkASSERT(a.fLeft <= a.fRight && a.fTop <= a.fBottom &&
411 b.fLeft <= b.fRight && b.fTop <= b.fBottom);
412 return a.fLeft < b.fRight && b.fLeft < a.fRight && a.fTop < b.fBottom && b.fTop < a.fBottom;
413}
414
415void GrDrawTarget::recordBatch(GrBatch* batch) {
robertphillipsa106c622015-10-16 09:07:06 -0700416 // A closed drawTarget should never receive new/more batches
417 SkASSERT(!fClosed);
418
bsalomon512be532015-09-10 10:42:55 -0700419 // Check if there is a Batch Draw we can batch with by linearly searching back until we either
420 // 1) check every draw
421 // 2) intersect with something
422 // 3) find a 'blocker'
423 // Experimentally we have found that most batching occurs within the first 10 comparisons.
424 static const int kMaxLookback = 10;
425
426 GrBATCH_INFO("Re-Recording (%s, B%u)\n"
joshualitte2bcec32015-09-30 06:22:22 -0700427 "\tBounds LRTB (%f, %f, %f, %f)\n",
bsalomon512be532015-09-10 10:42:55 -0700428 batch->name(),
429 batch->uniqueID(),
430 batch->bounds().fLeft, batch->bounds().fRight,
431 batch->bounds().fTop, batch->bounds().fBottom);
432 GrBATCH_INFO(SkTabString(batch->dumpInfo(), 1).c_str());
433 GrBATCH_INFO("\tOutcome:\n");
434 int maxCandidates = SkTMin(kMaxLookback, fBatches.count());
435 if (maxCandidates) {
436 int i = 0;
437 while (true) {
438 GrBatch* candidate = fBatches.fromBack(i);
439 // We cannot continue to search backwards if the render target changes
440 if (candidate->renderTargetUniqueID() != batch->renderTargetUniqueID()) {
441 GrBATCH_INFO("\t\tBreaking because of (%s, B%u) Rendertarget\n",
442 candidate->name(), candidate->uniqueID());
443 break;
444 }
445 if (candidate->combineIfPossible(batch, *this->caps())) {
446 GrBATCH_INFO("\t\tCombining with (%s, B%u)\n", candidate->name(),
447 candidate->uniqueID());
448 return;
449 }
450 // Stop going backwards if we would cause a painter's order violation.
451 if (intersect(candidate->bounds(), batch->bounds())) {
452 GrBATCH_INFO("\t\tIntersects with (%s, B%u)\n", candidate->name(),
453 candidate->uniqueID());
454 break;
455 }
456 ++i;
457 if (i == maxCandidates) {
458 GrBATCH_INFO("\t\tReached max lookback or beginning of batch array %d\n", i);
459 break;
460 }
461 }
462 } else {
463 GrBATCH_INFO("\t\tFirstBatch\n");
464 }
465 fBatches.push_back().reset(SkRef(batch));
joshualittf6d259b2015-10-02 11:27:14 -0700466 if (fBatches.count() > kMaxLookback) {
467 SkASSERT(fBatches.count() - kMaxLookback - fFirstUnpreparedBatch == 1);
468 fBatches[fFirstUnpreparedBatch++]->prepare(&fFlushState);
469 }
bsalomon512be532015-09-10 10:42:55 -0700470}
471
egdaniele36914c2015-02-13 09:00:33 -0800472///////////////////////////////////////////////////////////////////////////////
473
bsalomonad792c12015-09-10 11:10:50 -0700474bool GrDrawTarget::installPipelineInDrawBatch(const GrPipelineBuilder* pipelineBuilder,
475 const GrScissorState* scissor,
476 GrDrawBatch* batch) {
477 GrPipeline::CreateArgs args;
478 args.fPipelineBuilder = pipelineBuilder;
479 args.fCaps = this->caps();
480 args.fScissor = scissor;
481 args.fColorPOI = pipelineBuilder->colorProcInfo(batch);
482 args.fCoveragePOI = pipelineBuilder->coverageProcInfo(batch);
483 if (!this->setupDstReadIfNecessary(*pipelineBuilder, args.fColorPOI,
484 args.fCoveragePOI, &args.fDstTexture,
485 batch->bounds())) {
486 return false;
egdaniele36914c2015-02-13 09:00:33 -0800487 }
bsalomonad792c12015-09-10 11:10:50 -0700488
489 if (!batch->installPipeline(args)) {
490 return false;
491 }
492
493 return true;
egdaniele36914c2015-02-13 09:00:33 -0800494}
495
bsalomonb3b9aec2015-09-10 11:16:35 -0700496void GrDrawTarget::clearStencilClip(const SkIRect& rect, bool insideClip, GrRenderTarget* rt) {
halcanary385fe4d2015-08-26 13:07:48 -0700497 GrBatch* batch = new GrClearStencilClipBatch(rect, insideClip, rt);
bsalomon512be532015-09-10 10:42:55 -0700498 this->recordBatch(batch);
bsalomon5ea03632015-08-18 10:33:30 -0700499 batch->unref();
500}