blob: ba0142edca02fba1f2c5d6ae51c9c53de36ac08e [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)
joshualittf6d259b2015-10-02 11:27:14 -070040 , fFirstUnpreparedBatch(0) {
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));
bsalomon4061b122015-05-29 10:26:19 -070044}
45
46GrDrawTarget::~GrDrawTarget() {
47 fGpu->unref();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000048}
49
50////////////////////////////////////////////////////////////////////////////////
51
bsalomon50785a32015-02-06 07:02:37 -080052bool GrDrawTarget::setupDstReadIfNecessary(const GrPipelineBuilder& pipelineBuilder,
egdaniele36914c2015-02-13 09:00:33 -080053 const GrProcOptInfo& colorPOI,
54 const GrProcOptInfo& coveragePOI,
bsalomon6a44c6a2015-05-26 09:49:05 -070055 GrXferProcessor::DstTexture* dstTexture,
bsalomonad792c12015-09-10 11:10:50 -070056 const SkRect& batchBounds) {
57 SkRect bounds = batchBounds;
58 bounds.outset(0.5f, 0.5f);
59
bsalomon6a44c6a2015-05-26 09:49:05 -070060 if (!pipelineBuilder.willXPNeedDstTexture(*this->caps(), colorPOI, coveragePOI)) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +000061 return true;
62 }
cdalton9954bc32015-04-29 14:17:00 -070063
bsalomon50785a32015-02-06 07:02:37 -080064 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
cdalton9954bc32015-04-29 14:17:00 -070065
66 if (this->caps()->textureBarrierSupport()) {
67 if (GrTexture* rtTex = rt->asTexture()) {
bsalomondc47ff72015-05-26 12:16:59 -070068 // The render target is a texture, so we can read from it directly in the shader. The XP
cdalton9954bc32015-04-29 14:17:00 -070069 // will be responsible to detect this situation and request a texture barrier.
bsalomon6a44c6a2015-05-26 09:49:05 -070070 dstTexture->setTexture(rtTex);
71 dstTexture->setOffset(0, 0);
cdalton9954bc32015-04-29 14:17:00 -070072 return true;
73 }
74 }
75
76 SkIRect copyRect;
joshualitt44701df2015-02-23 14:44:57 -080077 pipelineBuilder.clip().getConservativeBounds(rt, &copyRect);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000078
bsalomonad792c12015-09-10 11:10:50 -070079 SkIRect drawIBounds;
80 bounds.roundOut(&drawIBounds);
81 if (!copyRect.intersect(drawIBounds)) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000082#ifdef SK_DEBUG
bsalomonb3b9aec2015-09-10 11:16:35 -070083 GrCapsDebugf(this->caps(), "Missed an early reject. "
84 "Bailing on draw from setupDstReadIfNecessary.\n");
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +000085#endif
bsalomonad792c12015-09-10 11:10:50 -070086 return false;
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +000087 }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +000088
commit-bot@chromium.org63150af2013-04-11 22:00:22 +000089 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
90 // have per-sample dst values by making the copy multisampled.
bsalomonf2703d82014-10-28 14:33:06 -070091 GrSurfaceDesc desc;
bsalomonb3b9aec2015-09-10 11:16:35 -070092 if (!fGpu->initCopySurfaceDstDesc(rt, &desc)) {
bsalomona73239a2015-04-28 13:35:17 -070093 desc.fOrigin = kDefault_GrSurfaceOrigin;
94 desc.fFlags = kRenderTarget_GrSurfaceFlag;
95 desc.fConfig = rt->config();
96 }
97
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +000098 desc.fWidth = copyRect.width();
99 desc.fHeight = copyRect.height();
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000100
bsalomoneae62002015-07-31 13:59:30 -0700101 static const uint32_t kFlags = 0;
102 SkAutoTUnref<GrTexture> copy(fResourceProvider->createApproxTexture(desc, kFlags));
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000103
bsalomone3059732014-10-14 11:47:22 -0700104 if (!copy) {
tfarina38406c82014-10-31 07:11:12 -0700105 SkDebugf("Failed to create temporary copy of destination texture.\n");
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000106 return false;
107 }
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000108 SkIPoint dstPoint = {0, 0};
bsalomon6df86402015-06-01 10:41:49 -0700109 this->copySurface(copy, rt, copyRect, dstPoint);
110 dstTexture->setTexture(copy);
111 dstTexture->setOffset(copyRect.fLeft, copyRect.fTop);
112 return true;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000113}
114
bsalomona73239a2015-04-28 13:35:17 -0700115void GrDrawTarget::flush() {
116 if (fFlushing) {
117 return;
118 }
119 fFlushing = true;
120
bsalomon512be532015-09-10 10:42:55 -0700121 // Loop over all batches and generate geometry
joshualittf6d259b2015-10-02 11:27:14 -0700122 for (; fFirstUnpreparedBatch < fBatches.count(); ++fFirstUnpreparedBatch) {
123 fBatches[fFirstUnpreparedBatch]->prepare(&fFlushState);
bsalomon512be532015-09-10 10:42:55 -0700124 }
125
126 // Upload all data to the GPU
joshualittf6d259b2015-10-02 11:27:14 -0700127 fFlushState.preIssueDraws();
bsalomon512be532015-09-10 10:42:55 -0700128
129 // Draw all the generated geometry.
130 for (int i = 0; i < fBatches.count(); ++i) {
joshualittf6d259b2015-10-02 11:27:14 -0700131 fBatches[i]->draw(&fFlushState);
bsalomon512be532015-09-10 10:42:55 -0700132 }
133
joshualittf6d259b2015-10-02 11:27:14 -0700134 SkASSERT(fFlushState.lastFlushedToken() == fFlushState.currentToken());
135 this->reset();
bsalomona73239a2015-04-28 13:35:17 -0700136
bsalomona73239a2015-04-28 13:35:17 -0700137 fFlushing = false;
bsalomona73239a2015-04-28 13:35:17 -0700138}
139
bsalomon512be532015-09-10 10:42:55 -0700140void GrDrawTarget::reset() {
joshualittf6d259b2015-10-02 11:27:14 -0700141 fFirstUnpreparedBatch = 0;
bsalomon512be532015-09-10 10:42:55 -0700142 fBatches.reset();
joshualittf6d259b2015-10-02 11:27:14 -0700143 fFlushState.reset();
bsalomon512be532015-09-10 10:42:55 -0700144}
145
bsalomonabd30f52015-08-13 13:34:48 -0700146void GrDrawTarget::drawBatch(const GrPipelineBuilder& pipelineBuilder, GrDrawBatch* batch) {
joshualitt4d8da812015-01-28 12:53:54 -0800147 // Setup clip
148 GrScissorState scissorState;
joshualitt4d8da812015-01-28 12:53:54 -0800149 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomon0ba8c242015-10-07 09:20:28 -0700150 GrAppliedClip clip;
151 if (!fClipMaskManager->setupClipping(pipelineBuilder, &ars, &scissorState, &batch->bounds(),
152 &clip)) {
joshualitt4d8da812015-01-28 12:53:54 -0800153 return;
154 }
bsalomon0ba8c242015-10-07 09:20:28 -0700155 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
156 if (clip.clipCoverageFragmentProcessor()) {
157 arfps.set(&pipelineBuilder);
158 arfps.addCoverageFragmentProcessor(clip.clipCoverageFragmentProcessor());
159 }
joshualitt4d8da812015-01-28 12:53:54 -0800160
bsalomonad792c12015-09-10 11:10:50 -0700161 GrPipeline::CreateArgs args;
162 if (!this->installPipelineInDrawBatch(&pipelineBuilder, &scissorState, batch)) {
egdaniele36914c2015-02-13 09:00:33 -0800163 return;
164 }
bsalomonad792c12015-09-10 11:10:50 -0700165
bsalomon512be532015-09-10 10:42:55 -0700166 this->recordBatch(batch);
joshualitt4d8da812015-01-28 12:53:54 -0800167}
168
joshualitt2c93efe2014-11-06 12:57:13 -0800169static const GrStencilSettings& winding_path_stencil_settings() {
170 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
171 kIncClamp_StencilOp,
172 kIncClamp_StencilOp,
173 kAlwaysIfInClip_StencilFunc,
174 0xFFFF, 0xFFFF, 0xFFFF);
175 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
176}
177
178static const GrStencilSettings& even_odd_path_stencil_settings() {
179 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
180 kInvert_StencilOp,
181 kInvert_StencilOp,
182 kAlwaysIfInClip_StencilFunc,
183 0xFFFF, 0xFFFF, 0xFFFF);
184 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
185}
186
187void GrDrawTarget::getPathStencilSettingsForFilltype(GrPathRendering::FillType fill,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700188 const GrStencilAttachment* sb,
joshualitt2c93efe2014-11-06 12:57:13 -0800189 GrStencilSettings* outStencilSettings) {
190
191 switch (fill) {
192 default:
193 SkFAIL("Unexpected path fill.");
194 case GrPathRendering::kWinding_FillType:
195 *outStencilSettings = winding_path_stencil_settings();
196 break;
197 case GrPathRendering::kEvenOdd_FillType:
198 *outStencilSettings = even_odd_path_stencil_settings();
199 break;
200 }
bsalomonb3b9aec2015-09-10 11:16:35 -0700201 fClipMaskManager->adjustPathStencilParams(sb, outStencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800202}
203
joshualitt1c735482015-07-13 08:08:25 -0700204void GrDrawTarget::stencilPath(const GrPipelineBuilder& pipelineBuilder,
joshualittf2384692015-09-10 11:00:51 -0700205 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800206 const GrPath* path,
207 GrPathRendering::FillType fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000208 // TODO: extract portions of checkDraw that are relevant to path stenciling.
bsalomon49f085d2014-09-05 13:34:00 -0700209 SkASSERT(path);
jvanverthe9c0fc62015-04-29 11:18:05 -0700210 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
joshualitt2c93efe2014-11-06 12:57:13 -0800211
212 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800213 GrScissorState scissorState;
egdaniel8dd688b2015-01-22 10:16:09 -0800214 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomon0ba8c242015-10-07 09:20:28 -0700215 GrAppliedClip clip;
216 if (!fClipMaskManager->setupClipping(pipelineBuilder, &ars, &scissorState, nullptr, &clip)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800217 return;
218 }
219
bsalomon0ba8c242015-10-07 09:20:28 -0700220 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
221 if (clip.clipCoverageFragmentProcessor()) {
222 arfps.set(&pipelineBuilder);
223 arfps.addCoverageFragmentProcessor(clip.clipCoverageFragmentProcessor());
224 }
225
joshualitt2c93efe2014-11-06 12:57:13 -0800226 // set stencil settings for path
227 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700228 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdanielec00d942015-09-14 12:56:10 -0700229 GrStencilAttachment* sb = fResourceProvider->attachStencilAttachment(rt);
bsalomon6bc1b5f2015-02-23 09:06:38 -0800230 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800231
joshualittf2384692015-09-10 11:00:51 -0700232 GrBatch* batch = GrStencilPathBatch::Create(viewMatrix,
bsalomona44919e2015-08-18 13:28:19 -0700233 pipelineBuilder.isHWAntialias(),
234 stencilSettings, scissorState,
235 pipelineBuilder.getRenderTarget(),
236 path);
bsalomon512be532015-09-10 10:42:55 -0700237 this->recordBatch(batch);
bsalomona44919e2015-08-18 13:28:19 -0700238 batch->unref();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000239}
240
joshualitt1c735482015-07-13 08:08:25 -0700241void GrDrawTarget::drawPath(const GrPipelineBuilder& pipelineBuilder,
joshualittf2384692015-09-10 11:00:51 -0700242 const SkMatrix& viewMatrix,
243 GrColor color,
joshualitt9853cce2014-11-17 14:22:48 -0800244 const GrPath* path,
245 GrPathRendering::FillType fill) {
bsalomon49f085d2014-09-05 13:34:00 -0700246 SkASSERT(path);
jvanverthe9c0fc62015-04-29 11:18:05 -0700247 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000248
joshualittf2384692015-09-10 11:00:51 -0700249 GrDrawPathBatchBase* batch = GrDrawPathBatch::Create(viewMatrix, color, path);
bsalomon1fcc01c2015-09-09 09:48:06 -0700250 this->drawPathBatch(pipelineBuilder, batch, fill);
251 batch->unref();
252}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000253
bsalomon1fcc01c2015-09-09 09:48:06 -0700254void GrDrawTarget::drawPathsFromRange(const GrPipelineBuilder& pipelineBuilder,
joshualittf2384692015-09-10 11:00:51 -0700255 const SkMatrix& viewMatrix,
256 const SkMatrix& localMatrix,
257 GrColor color,
bsalomon1fcc01c2015-09-09 09:48:06 -0700258 GrPathRangeDraw* draw,
259 GrPathRendering::FillType fill) {
joshualittf2384692015-09-10 11:00:51 -0700260 GrDrawPathBatchBase* batch = GrDrawPathRangeBatch::Create(viewMatrix, localMatrix, color, draw);
bsalomon1fcc01c2015-09-09 09:48:06 -0700261 this->drawPathBatch(pipelineBuilder, batch, fill);
262 batch->unref();
263}
264
265void GrDrawTarget::drawPathBatch(const GrPipelineBuilder& pipelineBuilder,
266 GrDrawPathBatchBase* batch,
267 GrPathRendering::FillType fill) {
bsalomonadd79ef2015-08-19 13:26:49 -0700268 // This looks like drawBatch() but there is an added wrinkle that stencil settings get inserted
bsalomonb3b9aec2015-09-10 11:16:35 -0700269 // after setting up clipping but before onDrawBatch(). TODO: Figure out a better model for
270 // handling stencil settings WRT interactions between pipeline(builder), clipmaskmanager, and
271 // batches.
bsalomonadd79ef2015-08-19 13:26:49 -0700272
bsalomon3e791242014-12-17 13:43:13 -0800273 GrScissorState scissorState;
egdaniel8dd688b2015-01-22 10:16:09 -0800274 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomon0ba8c242015-10-07 09:20:28 -0700275 GrAppliedClip clip;
276 if (!fClipMaskManager->setupClipping(pipelineBuilder, &ars, &scissorState, &batch->bounds(),
277 &clip)) {
bsalomonadd79ef2015-08-19 13:26:49 -0700278 return;
joshualitt2c93efe2014-11-06 12:57:13 -0800279 }
280
bsalomon0ba8c242015-10-07 09:20:28 -0700281 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
282 if (clip.clipCoverageFragmentProcessor()) {
283 arfps.set(&pipelineBuilder);
284 arfps.addCoverageFragmentProcessor(clip.clipCoverageFragmentProcessor());
285 }
286
bsalomonadd79ef2015-08-19 13:26:49 -0700287 // Ensure the render target has a stencil buffer and get the stencil settings.
joshualitt2c93efe2014-11-06 12:57:13 -0800288 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700289 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdanielec00d942015-09-14 12:56:10 -0700290 GrStencilAttachment* sb = fResourceProvider->attachStencilAttachment(rt);
bsalomon6bc1b5f2015-02-23 09:06:38 -0800291 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
bsalomonadd79ef2015-08-19 13:26:49 -0700292 batch->setStencilSettings(stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800293
bsalomonad792c12015-09-10 11:10:50 -0700294 GrPipeline::CreateArgs args;
295 if (!this->installPipelineInDrawBatch(&pipelineBuilder, &scissorState, batch)) {
bsalomonadd79ef2015-08-19 13:26:49 -0700296 return;
297 }
egdaniele36914c2015-02-13 09:00:33 -0800298
bsalomon512be532015-09-10 10:42:55 -0700299 this->recordBatch(batch);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000300}
301
joshualittd2b23e02015-08-21 10:53:34 -0700302void GrDrawTarget::drawNonAARect(const GrPipelineBuilder& pipelineBuilder,
joshualitt1c735482015-07-13 08:08:25 -0700303 GrColor color,
304 const SkMatrix& viewMatrix,
joshualittb6b513b2015-08-21 10:25:18 -0700305 const SkRect& rect) {
joshualittd2b23e02015-08-21 10:53:34 -0700306 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect,
307 nullptr, nullptr));
joshualittad17cfc2015-05-05 10:45:57 -0700308 this->drawBatch(pipelineBuilder, batch);
309}
310
joshualittd2b23e02015-08-21 10:53:34 -0700311void GrDrawTarget::drawNonAARect(const GrPipelineBuilder& pipelineBuilder,
joshualittb6b513b2015-08-21 10:25:18 -0700312 GrColor color,
313 const SkMatrix& viewMatrix,
314 const SkRect& rect,
315 const SkMatrix& localMatrix) {
joshualittd2b23e02015-08-21 10:53:34 -0700316 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect,
317 nullptr, &localMatrix));
joshualittb6b513b2015-08-21 10:25:18 -0700318 this->drawBatch(pipelineBuilder, batch);
319}
320
joshualittd2b23e02015-08-21 10:53:34 -0700321void GrDrawTarget::drawNonAARect(const GrPipelineBuilder& pipelineBuilder,
joshualittb6b513b2015-08-21 10:25:18 -0700322 GrColor color,
323 const SkMatrix& viewMatrix,
324 const SkRect& rect,
325 const SkRect& localRect) {
joshualittd2b23e02015-08-21 10:53:34 -0700326 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect,
327 &localRect, nullptr));
joshualittb6b513b2015-08-21 10:25:18 -0700328 this->drawBatch(pipelineBuilder, batch);
329}
330
331
joshualitt1c735482015-07-13 08:08:25 -0700332void GrDrawTarget::drawAARect(const GrPipelineBuilder& pipelineBuilder,
robertphillipsea461502015-05-26 11:38:03 -0700333 GrColor color,
334 const SkMatrix& viewMatrix,
335 const SkRect& rect,
336 const SkRect& devRect) {
joshualittd2b23e02015-08-21 10:53:34 -0700337 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateAAFill(color, viewMatrix, rect,
bsalomonabd30f52015-08-13 13:34:48 -0700338 devRect));
joshualitt14205b12015-08-10 11:40:56 -0700339 this->drawBatch(pipelineBuilder, batch);
robertphillipsea461502015-05-26 11:38:03 -0700340}
341
joshualitt9853cce2014-11-17 14:22:48 -0800342void GrDrawTarget::clear(const SkIRect* rect,
343 GrColor color,
344 bool canIgnoreRect,
bsalomon63b21962014-11-05 07:05:34 -0800345 GrRenderTarget* renderTarget) {
egdaniel51c8d402015-08-06 10:54:13 -0700346 SkIRect rtRect = SkIRect::MakeWH(renderTarget->width(), renderTarget->height());
347 SkIRect clippedRect;
348 if (!rect ||
349 (canIgnoreRect && this->caps()->fullClearIsFree()) ||
350 rect->contains(rtRect)) {
351 rect = &rtRect;
352 } else {
353 clippedRect = *rect;
354 if (!clippedRect.intersect(rtRect)) {
355 return;
356 }
357 rect = &clippedRect;
358 }
359
bsalomonb3b9aec2015-09-10 11:16:35 -0700360 if (this->caps()->useDrawInsteadOfClear()) {
bsalomon63b21962014-11-05 07:05:34 -0800361 // This works around a driver bug with clear by drawing a rect instead.
362 // The driver will ignore a clear if it is the only thing rendered to a
363 // target before the target is read.
egdaniel51c8d402015-08-06 10:54:13 -0700364 if (rect == &rtRect) {
bsalomon63b21962014-11-05 07:05:34 -0800365 this->discard(renderTarget);
366 }
bsalomon63b21962014-11-05 07:05:34 -0800367
egdaniel8dd688b2015-01-22 10:16:09 -0800368 GrPipelineBuilder pipelineBuilder;
369 pipelineBuilder.setRenderTarget(renderTarget);
joshualitt9853cce2014-11-17 14:22:48 -0800370
joshualittd2b23e02015-08-21 10:53:34 -0700371 this->drawNonAARect(pipelineBuilder, color, SkMatrix::I(), *rect);
bsalomon53469832015-08-18 09:20:09 -0700372 } else {
halcanary385fe4d2015-08-26 13:07:48 -0700373 GrBatch* batch = new GrClearBatch(*rect, color, renderTarget);
bsalomon512be532015-09-10 10:42:55 -0700374 this->recordBatch(batch);
bsalomon53469832015-08-18 09:20:09 -0700375 batch->unref();
376 }
377}
378
379void GrDrawTarget::discard(GrRenderTarget* renderTarget) {
380 if (this->caps()->discardRenderTargetSupport()) {
halcanary385fe4d2015-08-26 13:07:48 -0700381 GrBatch* batch = new GrDiscardBatch(renderTarget);
bsalomon512be532015-09-10 10:42:55 -0700382 this->recordBatch(batch);
bsalomon53469832015-08-18 09:20:09 -0700383 batch->unref();
bsalomon63b21962014-11-05 07:05:34 -0800384 }
385}
386
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000387////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000388
bsalomon6df86402015-06-01 10:41:49 -0700389void GrDrawTarget::copySurface(GrSurface* dst,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000390 GrSurface* src,
391 const SkIRect& srcRect,
392 const SkIPoint& dstPoint) {
bsalomon872062c2015-08-18 12:12:35 -0700393 GrBatch* batch = GrCopySurfaceBatch::Create(dst, src, srcRect, dstPoint);
394 if (batch) {
bsalomon512be532015-09-10 10:42:55 -0700395 this->recordBatch(batch);
bsalomon872062c2015-08-18 12:12:35 -0700396 batch->unref();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000397 }
bsalomon@google.comeb851172013-04-15 13:51:00 +0000398}
399
bsalomon512be532015-09-10 10:42:55 -0700400template <class Left, class Right> static bool intersect(const Left& a, const Right& b) {
401 SkASSERT(a.fLeft <= a.fRight && a.fTop <= a.fBottom &&
402 b.fLeft <= b.fRight && b.fTop <= b.fBottom);
403 return a.fLeft < b.fRight && b.fLeft < a.fRight && a.fTop < b.fBottom && b.fTop < a.fBottom;
404}
405
406void GrDrawTarget::recordBatch(GrBatch* batch) {
407 // Check if there is a Batch Draw we can batch with by linearly searching back until we either
408 // 1) check every draw
409 // 2) intersect with something
410 // 3) find a 'blocker'
411 // Experimentally we have found that most batching occurs within the first 10 comparisons.
412 static const int kMaxLookback = 10;
413
414 GrBATCH_INFO("Re-Recording (%s, B%u)\n"
joshualitte2bcec32015-09-30 06:22:22 -0700415 "\tBounds LRTB (%f, %f, %f, %f)\n",
bsalomon512be532015-09-10 10:42:55 -0700416 batch->name(),
417 batch->uniqueID(),
418 batch->bounds().fLeft, batch->bounds().fRight,
419 batch->bounds().fTop, batch->bounds().fBottom);
420 GrBATCH_INFO(SkTabString(batch->dumpInfo(), 1).c_str());
421 GrBATCH_INFO("\tOutcome:\n");
422 int maxCandidates = SkTMin(kMaxLookback, fBatches.count());
423 if (maxCandidates) {
424 int i = 0;
425 while (true) {
426 GrBatch* candidate = fBatches.fromBack(i);
427 // We cannot continue to search backwards if the render target changes
428 if (candidate->renderTargetUniqueID() != batch->renderTargetUniqueID()) {
429 GrBATCH_INFO("\t\tBreaking because of (%s, B%u) Rendertarget\n",
430 candidate->name(), candidate->uniqueID());
431 break;
432 }
433 if (candidate->combineIfPossible(batch, *this->caps())) {
434 GrBATCH_INFO("\t\tCombining with (%s, B%u)\n", candidate->name(),
435 candidate->uniqueID());
436 return;
437 }
438 // Stop going backwards if we would cause a painter's order violation.
439 if (intersect(candidate->bounds(), batch->bounds())) {
440 GrBATCH_INFO("\t\tIntersects with (%s, B%u)\n", candidate->name(),
441 candidate->uniqueID());
442 break;
443 }
444 ++i;
445 if (i == maxCandidates) {
446 GrBATCH_INFO("\t\tReached max lookback or beginning of batch array %d\n", i);
447 break;
448 }
449 }
450 } else {
451 GrBATCH_INFO("\t\tFirstBatch\n");
452 }
453 fBatches.push_back().reset(SkRef(batch));
joshualittf6d259b2015-10-02 11:27:14 -0700454 if (fBatches.count() > kMaxLookback) {
455 SkASSERT(fBatches.count() - kMaxLookback - fFirstUnpreparedBatch == 1);
456 fBatches[fFirstUnpreparedBatch++]->prepare(&fFlushState);
457 }
bsalomon512be532015-09-10 10:42:55 -0700458}
459
egdaniele36914c2015-02-13 09:00:33 -0800460///////////////////////////////////////////////////////////////////////////////
461
bsalomonad792c12015-09-10 11:10:50 -0700462bool GrDrawTarget::installPipelineInDrawBatch(const GrPipelineBuilder* pipelineBuilder,
463 const GrScissorState* scissor,
464 GrDrawBatch* batch) {
465 GrPipeline::CreateArgs args;
466 args.fPipelineBuilder = pipelineBuilder;
467 args.fCaps = this->caps();
468 args.fScissor = scissor;
469 args.fColorPOI = pipelineBuilder->colorProcInfo(batch);
470 args.fCoveragePOI = pipelineBuilder->coverageProcInfo(batch);
471 if (!this->setupDstReadIfNecessary(*pipelineBuilder, args.fColorPOI,
472 args.fCoveragePOI, &args.fDstTexture,
473 batch->bounds())) {
474 return false;
egdaniele36914c2015-02-13 09:00:33 -0800475 }
bsalomonad792c12015-09-10 11:10:50 -0700476
477 if (!batch->installPipeline(args)) {
478 return false;
479 }
480
481 return true;
egdaniele36914c2015-02-13 09:00:33 -0800482}
483
bsalomonb3b9aec2015-09-10 11:16:35 -0700484void GrDrawTarget::clearStencilClip(const SkIRect& rect, bool insideClip, GrRenderTarget* rt) {
halcanary385fe4d2015-08-26 13:07:48 -0700485 GrBatch* batch = new GrClearStencilClipBatch(rect, insideClip, rt);
bsalomon512be532015-09-10 10:42:55 -0700486 this->recordBatch(batch);
bsalomon5ea03632015-08-18 10:33:30 -0700487 batch->unref();
488}