blob: 85a9af432a128ec32c1c8013c4a101bdf291835f [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))
37 , fCaps(SkRef(gpu->caps()))
38 , fResourceProvider(resourceProvider)
bsalomon512be532015-09-10 10:42:55 -070039 , fFlushing(false)
40 , fLastFlushToken(0) {
bsalomon4061b122015-05-29 10:26:19 -070041}
42
43GrDrawTarget::~GrDrawTarget() {
44 fGpu->unref();
45 fCaps->unref();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000046}
47
48////////////////////////////////////////////////////////////////////////////////
49
bsalomon50785a32015-02-06 07:02:37 -080050bool GrDrawTarget::setupDstReadIfNecessary(const GrPipelineBuilder& pipelineBuilder,
egdaniele36914c2015-02-13 09:00:33 -080051 const GrProcOptInfo& colorPOI,
52 const GrProcOptInfo& coveragePOI,
bsalomon6a44c6a2015-05-26 09:49:05 -070053 GrXferProcessor::DstTexture* dstTexture,
bsalomonad792c12015-09-10 11:10:50 -070054 const SkRect& batchBounds) {
55 SkRect bounds = batchBounds;
56 bounds.outset(0.5f, 0.5f);
57
bsalomon6a44c6a2015-05-26 09:49:05 -070058 if (!pipelineBuilder.willXPNeedDstTexture(*this->caps(), colorPOI, coveragePOI)) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +000059 return true;
60 }
cdalton9954bc32015-04-29 14:17:00 -070061
bsalomon50785a32015-02-06 07:02:37 -080062 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
cdalton9954bc32015-04-29 14:17:00 -070063
64 if (this->caps()->textureBarrierSupport()) {
65 if (GrTexture* rtTex = rt->asTexture()) {
bsalomondc47ff72015-05-26 12:16:59 -070066 // The render target is a texture, so we can read from it directly in the shader. The XP
cdalton9954bc32015-04-29 14:17:00 -070067 // will be responsible to detect this situation and request a texture barrier.
bsalomon6a44c6a2015-05-26 09:49:05 -070068 dstTexture->setTexture(rtTex);
69 dstTexture->setOffset(0, 0);
cdalton9954bc32015-04-29 14:17:00 -070070 return true;
71 }
72 }
73
74 SkIRect copyRect;
joshualitt44701df2015-02-23 14:44:57 -080075 pipelineBuilder.clip().getConservativeBounds(rt, &copyRect);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000076
bsalomonad792c12015-09-10 11:10:50 -070077 SkIRect drawIBounds;
78 bounds.roundOut(&drawIBounds);
79 if (!copyRect.intersect(drawIBounds)) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000080#ifdef SK_DEBUG
bsalomonad792c12015-09-10 11:10:50 -070081 GrCapsDebugf(fCaps, "Missed an early reject. "
82 "Bailing on draw from setupDstReadIfNecessary.\n");
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +000083#endif
bsalomonad792c12015-09-10 11:10:50 -070084 return false;
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +000085 }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +000086
commit-bot@chromium.org63150af2013-04-11 22:00:22 +000087 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
88 // have per-sample dst values by making the copy multisampled.
bsalomonf2703d82014-10-28 14:33:06 -070089 GrSurfaceDesc desc;
bsalomona73239a2015-04-28 13:35:17 -070090 if (!this->getGpu()->initCopySurfaceDstDesc(rt, &desc)) {
91 desc.fOrigin = kDefault_GrSurfaceOrigin;
92 desc.fFlags = kRenderTarget_GrSurfaceFlag;
93 desc.fConfig = rt->config();
94 }
95
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +000096 desc.fWidth = copyRect.width();
97 desc.fHeight = copyRect.height();
bsalomon@google.com26e18b52013-03-29 19:22:36 +000098
bsalomoneae62002015-07-31 13:59:30 -070099 static const uint32_t kFlags = 0;
100 SkAutoTUnref<GrTexture> copy(fResourceProvider->createApproxTexture(desc, kFlags));
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000101
bsalomone3059732014-10-14 11:47:22 -0700102 if (!copy) {
tfarina38406c82014-10-31 07:11:12 -0700103 SkDebugf("Failed to create temporary copy of destination texture.\n");
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000104 return false;
105 }
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000106 SkIPoint dstPoint = {0, 0};
bsalomon6df86402015-06-01 10:41:49 -0700107 this->copySurface(copy, rt, copyRect, dstPoint);
108 dstTexture->setTexture(copy);
109 dstTexture->setOffset(copyRect.fLeft, copyRect.fTop);
110 return true;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000111}
112
bsalomona73239a2015-04-28 13:35:17 -0700113void GrDrawTarget::flush() {
114 if (fFlushing) {
115 return;
116 }
117 fFlushing = true;
118
bsalomon512be532015-09-10 10:42:55 -0700119 GrBatchFlushState flushState(fGpu, fResourceProvider, fLastFlushToken);
120
121 // Loop over all batches and generate geometry
122 for (int i = 0; i < fBatches.count(); ++i) {
123 fBatches[i]->prepare(&flushState);
124 }
125
126 // Upload all data to the GPU
127 flushState.preIssueDraws();
128
129 // Draw all the generated geometry.
130 for (int i = 0; i < fBatches.count(); ++i) {
131 fBatches[i]->draw(&flushState);
132 }
133
134 fLastFlushToken = flushState.lastFlushedToken();
bsalomona73239a2015-04-28 13:35:17 -0700135
bsalomona73239a2015-04-28 13:35:17 -0700136 fFlushing = false;
137 this->reset();
138}
139
bsalomon512be532015-09-10 10:42:55 -0700140void GrDrawTarget::reset() {
141 fBatches.reset();
142}
143
bsalomonabd30f52015-08-13 13:34:48 -0700144void GrDrawTarget::drawBatch(const GrPipelineBuilder& pipelineBuilder, GrDrawBatch* batch) {
joshualitt4d8da812015-01-28 12:53:54 -0800145 // Setup clip
146 GrScissorState scissorState;
joshualitt4421a4c2015-07-13 09:36:41 -0700147 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
joshualitt4d8da812015-01-28 12:53:54 -0800148 GrPipelineBuilder::AutoRestoreStencil ars;
joshualitt4421a4c2015-07-13 09:36:41 -0700149 if (!this->setupClip(pipelineBuilder, &arfps, &ars, &scissorState, &batch->bounds())) {
joshualitt4d8da812015-01-28 12:53:54 -0800150 return;
151 }
152
bsalomonad792c12015-09-10 11:10:50 -0700153 GrPipeline::CreateArgs args;
154 if (!this->installPipelineInDrawBatch(&pipelineBuilder, &scissorState, batch)) {
egdaniele36914c2015-02-13 09:00:33 -0800155 return;
156 }
bsalomonad792c12015-09-10 11:10:50 -0700157
bsalomon512be532015-09-10 10:42:55 -0700158 this->recordBatch(batch);
joshualitt4d8da812015-01-28 12:53:54 -0800159}
160
joshualitt2c93efe2014-11-06 12:57:13 -0800161static const GrStencilSettings& winding_path_stencil_settings() {
162 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
163 kIncClamp_StencilOp,
164 kIncClamp_StencilOp,
165 kAlwaysIfInClip_StencilFunc,
166 0xFFFF, 0xFFFF, 0xFFFF);
167 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
168}
169
170static const GrStencilSettings& even_odd_path_stencil_settings() {
171 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
172 kInvert_StencilOp,
173 kInvert_StencilOp,
174 kAlwaysIfInClip_StencilFunc,
175 0xFFFF, 0xFFFF, 0xFFFF);
176 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
177}
178
179void GrDrawTarget::getPathStencilSettingsForFilltype(GrPathRendering::FillType fill,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700180 const GrStencilAttachment* sb,
joshualitt2c93efe2014-11-06 12:57:13 -0800181 GrStencilSettings* outStencilSettings) {
182
183 switch (fill) {
184 default:
185 SkFAIL("Unexpected path fill.");
186 case GrPathRendering::kWinding_FillType:
187 *outStencilSettings = winding_path_stencil_settings();
188 break;
189 case GrPathRendering::kEvenOdd_FillType:
190 *outStencilSettings = even_odd_path_stencil_settings();
191 break;
192 }
joshualitt9853cce2014-11-17 14:22:48 -0800193 this->clipMaskManager()->adjustPathStencilParams(sb, outStencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800194}
195
joshualitt1c735482015-07-13 08:08:25 -0700196void GrDrawTarget::stencilPath(const GrPipelineBuilder& pipelineBuilder,
joshualittf2384692015-09-10 11:00:51 -0700197 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800198 const GrPath* path,
199 GrPathRendering::FillType fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000200 // TODO: extract portions of checkDraw that are relevant to path stenciling.
bsalomon49f085d2014-09-05 13:34:00 -0700201 SkASSERT(path);
jvanverthe9c0fc62015-04-29 11:18:05 -0700202 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
joshualitt2c93efe2014-11-06 12:57:13 -0800203
204 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800205 GrScissorState scissorState;
joshualitt4421a4c2015-07-13 09:36:41 -0700206 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
egdaniel8dd688b2015-01-22 10:16:09 -0800207 GrPipelineBuilder::AutoRestoreStencil ars;
halcanary96fcdcc2015-08-27 07:41:13 -0700208 if (!this->setupClip(pipelineBuilder, &arfps, &ars, &scissorState, nullptr)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800209 return;
210 }
211
212 // set stencil settings for path
213 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700214 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdaniel8dc7c3a2015-04-16 11:22:42 -0700215 GrStencilAttachment* sb = rt->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800216 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800217
joshualittf2384692015-09-10 11:00:51 -0700218 GrBatch* batch = GrStencilPathBatch::Create(viewMatrix,
bsalomona44919e2015-08-18 13:28:19 -0700219 pipelineBuilder.isHWAntialias(),
220 stencilSettings, scissorState,
221 pipelineBuilder.getRenderTarget(),
222 path);
bsalomon512be532015-09-10 10:42:55 -0700223 this->recordBatch(batch);
bsalomona44919e2015-08-18 13:28:19 -0700224 batch->unref();
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000225}
226
joshualitt1c735482015-07-13 08:08:25 -0700227void GrDrawTarget::drawPath(const GrPipelineBuilder& pipelineBuilder,
joshualittf2384692015-09-10 11:00:51 -0700228 const SkMatrix& viewMatrix,
229 GrColor color,
joshualitt9853cce2014-11-17 14:22:48 -0800230 const GrPath* path,
231 GrPathRendering::FillType fill) {
bsalomon49f085d2014-09-05 13:34:00 -0700232 SkASSERT(path);
jvanverthe9c0fc62015-04-29 11:18:05 -0700233 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000234
joshualittf2384692015-09-10 11:00:51 -0700235 GrDrawPathBatchBase* batch = GrDrawPathBatch::Create(viewMatrix, color, path);
bsalomon1fcc01c2015-09-09 09:48:06 -0700236 this->drawPathBatch(pipelineBuilder, batch, fill);
237 batch->unref();
238}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000239
bsalomon1fcc01c2015-09-09 09:48:06 -0700240void GrDrawTarget::drawPathsFromRange(const GrPipelineBuilder& pipelineBuilder,
joshualittf2384692015-09-10 11:00:51 -0700241 const SkMatrix& viewMatrix,
242 const SkMatrix& localMatrix,
243 GrColor color,
bsalomon1fcc01c2015-09-09 09:48:06 -0700244 GrPathRangeDraw* draw,
245 GrPathRendering::FillType fill) {
joshualittf2384692015-09-10 11:00:51 -0700246 GrDrawPathBatchBase* batch = GrDrawPathRangeBatch::Create(viewMatrix, localMatrix, color, draw);
bsalomon1fcc01c2015-09-09 09:48:06 -0700247 this->drawPathBatch(pipelineBuilder, batch, fill);
248 batch->unref();
249}
250
251void GrDrawTarget::drawPathBatch(const GrPipelineBuilder& pipelineBuilder,
252 GrDrawPathBatchBase* batch,
253 GrPathRendering::FillType fill) {
bsalomonadd79ef2015-08-19 13:26:49 -0700254 // This looks like drawBatch() but there is an added wrinkle that stencil settings get inserted
255 // after setupClip() but before onDrawBatch(). TODO: Figure out a better model for handling
256 // stencil settings WRT interactions between pipeline(builder), clipmaskmanager, and batches.
257
bsalomon3e791242014-12-17 13:43:13 -0800258 GrScissorState scissorState;
joshualitt4421a4c2015-07-13 09:36:41 -0700259 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
egdaniel8dd688b2015-01-22 10:16:09 -0800260 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomonadd79ef2015-08-19 13:26:49 -0700261 if (!this->setupClip(pipelineBuilder, &arfps, &ars, &scissorState, &batch->bounds())) {
262 return;
joshualitt2c93efe2014-11-06 12:57:13 -0800263 }
264
bsalomonadd79ef2015-08-19 13:26:49 -0700265 // Ensure the render target has a stencil buffer and get the stencil settings.
joshualitt2c93efe2014-11-06 12:57:13 -0800266 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700267 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdaniel8dc7c3a2015-04-16 11:22:42 -0700268 GrStencilAttachment* sb = rt->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800269 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
bsalomonadd79ef2015-08-19 13:26:49 -0700270 batch->setStencilSettings(stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800271
bsalomonad792c12015-09-10 11:10:50 -0700272 GrPipeline::CreateArgs args;
273 if (!this->installPipelineInDrawBatch(&pipelineBuilder, &scissorState, batch)) {
bsalomonadd79ef2015-08-19 13:26:49 -0700274 return;
275 }
egdaniele36914c2015-02-13 09:00:33 -0800276
bsalomon512be532015-09-10 10:42:55 -0700277 this->recordBatch(batch);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000278}
279
joshualittd2b23e02015-08-21 10:53:34 -0700280void GrDrawTarget::drawNonAARect(const GrPipelineBuilder& pipelineBuilder,
joshualitt1c735482015-07-13 08:08:25 -0700281 GrColor color,
282 const SkMatrix& viewMatrix,
joshualittb6b513b2015-08-21 10:25:18 -0700283 const SkRect& rect) {
joshualittd2b23e02015-08-21 10:53:34 -0700284 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect,
285 nullptr, nullptr));
joshualittad17cfc2015-05-05 10:45:57 -0700286 this->drawBatch(pipelineBuilder, batch);
287}
288
joshualittd2b23e02015-08-21 10:53:34 -0700289void GrDrawTarget::drawNonAARect(const GrPipelineBuilder& pipelineBuilder,
joshualittb6b513b2015-08-21 10:25:18 -0700290 GrColor color,
291 const SkMatrix& viewMatrix,
292 const SkRect& rect,
293 const SkMatrix& localMatrix) {
joshualittd2b23e02015-08-21 10:53:34 -0700294 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect,
295 nullptr, &localMatrix));
joshualittb6b513b2015-08-21 10:25:18 -0700296 this->drawBatch(pipelineBuilder, batch);
297}
298
joshualittd2b23e02015-08-21 10:53:34 -0700299void GrDrawTarget::drawNonAARect(const GrPipelineBuilder& pipelineBuilder,
joshualittb6b513b2015-08-21 10:25:18 -0700300 GrColor color,
301 const SkMatrix& viewMatrix,
302 const SkRect& rect,
303 const SkRect& localRect) {
joshualittd2b23e02015-08-21 10:53:34 -0700304 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect,
305 &localRect, nullptr));
joshualittb6b513b2015-08-21 10:25:18 -0700306 this->drawBatch(pipelineBuilder, batch);
307}
308
309
joshualitt1c735482015-07-13 08:08:25 -0700310void GrDrawTarget::drawAARect(const GrPipelineBuilder& pipelineBuilder,
robertphillipsea461502015-05-26 11:38:03 -0700311 GrColor color,
312 const SkMatrix& viewMatrix,
313 const SkRect& rect,
314 const SkRect& devRect) {
joshualittd2b23e02015-08-21 10:53:34 -0700315 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateAAFill(color, viewMatrix, rect,
bsalomonabd30f52015-08-13 13:34:48 -0700316 devRect));
joshualitt14205b12015-08-10 11:40:56 -0700317 this->drawBatch(pipelineBuilder, batch);
robertphillipsea461502015-05-26 11:38:03 -0700318}
319
joshualitt9853cce2014-11-17 14:22:48 -0800320void GrDrawTarget::clear(const SkIRect* rect,
321 GrColor color,
322 bool canIgnoreRect,
bsalomon63b21962014-11-05 07:05:34 -0800323 GrRenderTarget* renderTarget) {
egdaniel51c8d402015-08-06 10:54:13 -0700324 SkIRect rtRect = SkIRect::MakeWH(renderTarget->width(), renderTarget->height());
325 SkIRect clippedRect;
326 if (!rect ||
327 (canIgnoreRect && this->caps()->fullClearIsFree()) ||
328 rect->contains(rtRect)) {
329 rect = &rtRect;
330 } else {
331 clippedRect = *rect;
332 if (!clippedRect.intersect(rtRect)) {
333 return;
334 }
335 rect = &clippedRect;
336 }
337
bsalomon63b21962014-11-05 07:05:34 -0800338 if (fCaps->useDrawInsteadOfClear()) {
339 // This works around a driver bug with clear by drawing a rect instead.
340 // The driver will ignore a clear if it is the only thing rendered to a
341 // target before the target is read.
egdaniel51c8d402015-08-06 10:54:13 -0700342 if (rect == &rtRect) {
bsalomon63b21962014-11-05 07:05:34 -0800343 this->discard(renderTarget);
344 }
bsalomon63b21962014-11-05 07:05:34 -0800345
egdaniel8dd688b2015-01-22 10:16:09 -0800346 GrPipelineBuilder pipelineBuilder;
347 pipelineBuilder.setRenderTarget(renderTarget);
joshualitt9853cce2014-11-17 14:22:48 -0800348
joshualittd2b23e02015-08-21 10:53:34 -0700349 this->drawNonAARect(pipelineBuilder, color, SkMatrix::I(), *rect);
bsalomon53469832015-08-18 09:20:09 -0700350 } else {
halcanary385fe4d2015-08-26 13:07:48 -0700351 GrBatch* batch = new GrClearBatch(*rect, color, renderTarget);
bsalomon512be532015-09-10 10:42:55 -0700352 this->recordBatch(batch);
bsalomon53469832015-08-18 09:20:09 -0700353 batch->unref();
354 }
355}
356
357void GrDrawTarget::discard(GrRenderTarget* renderTarget) {
358 if (this->caps()->discardRenderTargetSupport()) {
halcanary385fe4d2015-08-26 13:07:48 -0700359 GrBatch* batch = new GrDiscardBatch(renderTarget);
bsalomon512be532015-09-10 10:42:55 -0700360 this->recordBatch(batch);
bsalomon53469832015-08-18 09:20:09 -0700361 batch->unref();
bsalomon63b21962014-11-05 07:05:34 -0800362 }
363}
364
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000365////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000366
bsalomon6df86402015-06-01 10:41:49 -0700367void GrDrawTarget::copySurface(GrSurface* dst,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000368 GrSurface* src,
369 const SkIRect& srcRect,
370 const SkIPoint& dstPoint) {
bsalomon872062c2015-08-18 12:12:35 -0700371 GrBatch* batch = GrCopySurfaceBatch::Create(dst, src, srcRect, dstPoint);
372 if (batch) {
bsalomon512be532015-09-10 10:42:55 -0700373 this->recordBatch(batch);
bsalomon872062c2015-08-18 12:12:35 -0700374 batch->unref();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000375 }
bsalomon@google.comeb851172013-04-15 13:51:00 +0000376}
377
bsalomon512be532015-09-10 10:42:55 -0700378template <class Left, class Right> static bool intersect(const Left& a, const Right& b) {
379 SkASSERT(a.fLeft <= a.fRight && a.fTop <= a.fBottom &&
380 b.fLeft <= b.fRight && b.fTop <= b.fBottom);
381 return a.fLeft < b.fRight && b.fLeft < a.fRight && a.fTop < b.fBottom && b.fTop < a.fBottom;
382}
383
384void GrDrawTarget::recordBatch(GrBatch* batch) {
385 // Check if there is a Batch Draw we can batch with by linearly searching back until we either
386 // 1) check every draw
387 // 2) intersect with something
388 // 3) find a 'blocker'
389 // Experimentally we have found that most batching occurs within the first 10 comparisons.
390 static const int kMaxLookback = 10;
391
392 GrBATCH_INFO("Re-Recording (%s, B%u)\n"
393 "\tBounds (%f, %f, %f, %f)\n",
394 batch->name(),
395 batch->uniqueID(),
396 batch->bounds().fLeft, batch->bounds().fRight,
397 batch->bounds().fTop, batch->bounds().fBottom);
398 GrBATCH_INFO(SkTabString(batch->dumpInfo(), 1).c_str());
399 GrBATCH_INFO("\tOutcome:\n");
400 int maxCandidates = SkTMin(kMaxLookback, fBatches.count());
401 if (maxCandidates) {
402 int i = 0;
403 while (true) {
404 GrBatch* candidate = fBatches.fromBack(i);
405 // We cannot continue to search backwards if the render target changes
406 if (candidate->renderTargetUniqueID() != batch->renderTargetUniqueID()) {
407 GrBATCH_INFO("\t\tBreaking because of (%s, B%u) Rendertarget\n",
408 candidate->name(), candidate->uniqueID());
409 break;
410 }
411 if (candidate->combineIfPossible(batch, *this->caps())) {
412 GrBATCH_INFO("\t\tCombining with (%s, B%u)\n", candidate->name(),
413 candidate->uniqueID());
414 return;
415 }
416 // Stop going backwards if we would cause a painter's order violation.
417 if (intersect(candidate->bounds(), batch->bounds())) {
418 GrBATCH_INFO("\t\tIntersects with (%s, B%u)\n", candidate->name(),
419 candidate->uniqueID());
420 break;
421 }
422 ++i;
423 if (i == maxCandidates) {
424 GrBATCH_INFO("\t\tReached max lookback or beginning of batch array %d\n", i);
425 break;
426 }
427 }
428 } else {
429 GrBATCH_INFO("\t\tFirstBatch\n");
430 }
431 fBatches.push_back().reset(SkRef(batch));
432}
433
egdaniele36914c2015-02-13 09:00:33 -0800434///////////////////////////////////////////////////////////////////////////////
435
bsalomonad792c12015-09-10 11:10:50 -0700436bool GrDrawTarget::installPipelineInDrawBatch(const GrPipelineBuilder* pipelineBuilder,
437 const GrScissorState* scissor,
438 GrDrawBatch* batch) {
439 GrPipeline::CreateArgs args;
440 args.fPipelineBuilder = pipelineBuilder;
441 args.fCaps = this->caps();
442 args.fScissor = scissor;
443 args.fColorPOI = pipelineBuilder->colorProcInfo(batch);
444 args.fCoveragePOI = pipelineBuilder->coverageProcInfo(batch);
445 if (!this->setupDstReadIfNecessary(*pipelineBuilder, args.fColorPOI,
446 args.fCoveragePOI, &args.fDstTexture,
447 batch->bounds())) {
448 return false;
egdaniele36914c2015-02-13 09:00:33 -0800449 }
bsalomonad792c12015-09-10 11:10:50 -0700450
451 if (!batch->installPipeline(args)) {
452 return false;
453 }
454
455 return true;
egdaniele36914c2015-02-13 09:00:33 -0800456}
457
bsalomon@google.combcce8922013-03-25 15:38:39 +0000458///////////////////////////////////////////////////////////////////////////////
bsalomon4061b122015-05-29 10:26:19 -0700459GrClipTarget::GrClipTarget(GrContext* context)
460 : INHERITED(context->getGpu(), context->resourceProvider())
461 , fContext(context) {
halcanary385fe4d2015-08-26 13:07:48 -0700462 fClipMaskManager.reset(new GrClipMaskManager(this));
bsalomonedd77a12015-05-29 09:45:57 -0700463}
464
bsalomon@google.combcce8922013-03-25 15:38:39 +0000465
joshualitt1c735482015-07-13 08:08:25 -0700466bool GrClipTarget::setupClip(const GrPipelineBuilder& pipelineBuilder,
joshualitt4421a4c2015-07-13 09:36:41 -0700467 GrPipelineBuilder::AutoRestoreFragmentProcessorState* arfps,
egdaniel8dd688b2015-01-22 10:16:09 -0800468 GrPipelineBuilder::AutoRestoreStencil* ars,
joshualitt8059eb92014-12-29 15:10:07 -0800469 GrScissorState* scissorState,
470 const SkRect* devBounds) {
joshualitt1c735482015-07-13 08:08:25 -0700471 return fClipMaskManager->setupClipping(pipelineBuilder,
joshualitt4421a4c2015-07-13 09:36:41 -0700472 arfps,
joshualitt5e6ba212015-07-13 07:35:05 -0700473 ars,
joshualitt5e6ba212015-07-13 07:35:05 -0700474 scissorState,
475 devBounds);
joshualitt2c93efe2014-11-06 12:57:13 -0800476}
bsalomonedd77a12015-05-29 09:45:57 -0700477
478void GrClipTarget::purgeResources() {
479 // The clip mask manager can rebuild all its clip masks so just
480 // get rid of them all.
481 fClipMaskManager->purgeResources();
482};
bsalomon5ea03632015-08-18 10:33:30 -0700483
484void GrClipTarget::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}