blob: d2c258d1d0760bd2244a3bc20f4438b4c5a49b68 [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"
24#include "batches/GrDiscardBatch.h"
bsalomon16b99132015-08-13 14:55:50 -070025#include "batches/GrDrawBatch.h"
joshualittecd1a692015-08-10 10:08:26 -070026#include "batches/GrRectBatchFactory.h"
joshualitt74417822015-08-07 11:42:16 -070027
sugoi@google.com5f74cf82012-12-17 21:16:45 +000028#include "SkStrokeRec.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000029
reed@google.comac10a2d2010-12-22 21:39:39 +000030////////////////////////////////////////////////////////////////////////////////
31
bsalomon4061b122015-05-29 10:26:19 -070032GrDrawTarget::GrDrawTarget(GrGpu* gpu, GrResourceProvider* resourceProvider)
33 : fGpu(SkRef(gpu))
34 , fCaps(SkRef(gpu->caps()))
35 , fResourceProvider(resourceProvider)
bsalomona73239a2015-04-28 13:35:17 -070036 , fGpuTraceMarkerCount(0)
bsalomona73239a2015-04-28 13:35:17 -070037 , fFlushing(false) {
bsalomon4061b122015-05-29 10:26:19 -070038}
39
40GrDrawTarget::~GrDrawTarget() {
41 fGpu->unref();
42 fCaps->unref();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000043}
44
45////////////////////////////////////////////////////////////////////////////////
46
bsalomon50785a32015-02-06 07:02:37 -080047bool GrDrawTarget::setupDstReadIfNecessary(const GrPipelineBuilder& pipelineBuilder,
egdaniele36914c2015-02-13 09:00:33 -080048 const GrProcOptInfo& colorPOI,
49 const GrProcOptInfo& coveragePOI,
bsalomon6a44c6a2015-05-26 09:49:05 -070050 GrXferProcessor::DstTexture* dstTexture,
joshualitt9853cce2014-11-17 14:22:48 -080051 const SkRect* drawBounds) {
bsalomon6a44c6a2015-05-26 09:49:05 -070052 if (!pipelineBuilder.willXPNeedDstTexture(*this->caps(), colorPOI, coveragePOI)) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +000053 return true;
54 }
cdalton9954bc32015-04-29 14:17:00 -070055
bsalomon50785a32015-02-06 07:02:37 -080056 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
cdalton9954bc32015-04-29 14:17:00 -070057
58 if (this->caps()->textureBarrierSupport()) {
59 if (GrTexture* rtTex = rt->asTexture()) {
bsalomondc47ff72015-05-26 12:16:59 -070060 // The render target is a texture, so we can read from it directly in the shader. The XP
cdalton9954bc32015-04-29 14:17:00 -070061 // will be responsible to detect this situation and request a texture barrier.
bsalomon6a44c6a2015-05-26 09:49:05 -070062 dstTexture->setTexture(rtTex);
63 dstTexture->setOffset(0, 0);
cdalton9954bc32015-04-29 14:17:00 -070064 return true;
65 }
66 }
67
68 SkIRect copyRect;
joshualitt44701df2015-02-23 14:44:57 -080069 pipelineBuilder.clip().getConservativeBounds(rt, &copyRect);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000070
bsalomon49f085d2014-09-05 13:34:00 -070071 if (drawBounds) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000072 SkIRect drawIBounds;
73 drawBounds->roundOut(&drawIBounds);
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +000074 if (!copyRect.intersect(drawIBounds)) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000075#ifdef SK_DEBUG
bsalomon682c2692015-05-22 14:01:46 -070076 GrCapsDebugf(fCaps, "Missed an early reject. "
77 "Bailing on draw from setupDstReadIfNecessary.\n");
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +000078#endif
79 return false;
80 }
81 } else {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000082#ifdef SK_DEBUG
tfarina38406c82014-10-31 07:11:12 -070083 //SkDebugf("No dev bounds when dst copy is made.\n");
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +000084#endif
85 }
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
96
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +000097 desc.fWidth = copyRect.width();
98 desc.fHeight = copyRect.height();
bsalomon@google.com26e18b52013-03-29 19:22:36 +000099
bsalomoneae62002015-07-31 13:59:30 -0700100 static const uint32_t kFlags = 0;
101 SkAutoTUnref<GrTexture> copy(fResourceProvider->createApproxTexture(desc, kFlags));
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000102
bsalomone3059732014-10-14 11:47:22 -0700103 if (!copy) {
tfarina38406c82014-10-31 07:11:12 -0700104 SkDebugf("Failed to create temporary copy of destination texture.\n");
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000105 return false;
106 }
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000107 SkIPoint dstPoint = {0, 0};
bsalomon6df86402015-06-01 10:41:49 -0700108 this->copySurface(copy, rt, copyRect, dstPoint);
109 dstTexture->setTexture(copy);
110 dstTexture->setOffset(copyRect.fLeft, copyRect.fTop);
111 return true;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000112}
113
bsalomona73239a2015-04-28 13:35:17 -0700114void GrDrawTarget::flush() {
115 if (fFlushing) {
116 return;
117 }
118 fFlushing = true;
119
120 this->getGpu()->saveActiveTraceMarkers();
121
122 this->onFlush();
123
124 this->getGpu()->restoreActiveTraceMarkers();
125
126 fFlushing = false;
127 this->reset();
128}
129
bsalomonabd30f52015-08-13 13:34:48 -0700130void GrDrawTarget::drawBatch(const GrPipelineBuilder& pipelineBuilder, GrDrawBatch* batch) {
joshualitt4d8da812015-01-28 12:53:54 -0800131 // TODO some kind of checkdraw, but not at this level
132
133 // Setup clip
134 GrScissorState scissorState;
joshualitt4421a4c2015-07-13 09:36:41 -0700135 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
joshualitt4d8da812015-01-28 12:53:54 -0800136 GrPipelineBuilder::AutoRestoreStencil ars;
joshualitt4421a4c2015-07-13 09:36:41 -0700137 if (!this->setupClip(pipelineBuilder, &arfps, &ars, &scissorState, &batch->bounds())) {
joshualitt4d8da812015-01-28 12:53:54 -0800138 return;
139 }
140
joshualitt99c7c072015-05-01 13:43:30 -0700141 // Batch bounds are tight, so for dev copies
142 // TODO move this into setupDstReadIfNecessary when paths are in batch
143 SkRect bounds = batch->bounds();
144 bounds.outset(0.5f, 0.5f);
145
bsalomona387a112015-08-11 14:47:42 -0700146 GrDrawTarget::PipelineInfo pipelineInfo(&pipelineBuilder, &scissorState, batch, &bounds,
joshualitt99c7c072015-05-01 13:43:30 -0700147 this);
bsalomona387a112015-08-11 14:47:42 -0700148 if (!pipelineInfo.valid()) {
egdaniele36914c2015-02-13 09:00:33 -0800149 return;
150 }
bsalomona387a112015-08-11 14:47:42 -0700151 if (!batch->installPipeline(pipelineInfo.pipelineCreateArgs())) {
152 return;
153 }
154 this->onDrawBatch(batch);
joshualitt4d8da812015-01-28 12:53:54 -0800155}
156
joshualitt2c93efe2014-11-06 12:57:13 -0800157static const GrStencilSettings& winding_path_stencil_settings() {
158 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
159 kIncClamp_StencilOp,
160 kIncClamp_StencilOp,
161 kAlwaysIfInClip_StencilFunc,
162 0xFFFF, 0xFFFF, 0xFFFF);
163 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
164}
165
166static const GrStencilSettings& even_odd_path_stencil_settings() {
167 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
168 kInvert_StencilOp,
169 kInvert_StencilOp,
170 kAlwaysIfInClip_StencilFunc,
171 0xFFFF, 0xFFFF, 0xFFFF);
172 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
173}
174
175void GrDrawTarget::getPathStencilSettingsForFilltype(GrPathRendering::FillType fill,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700176 const GrStencilAttachment* sb,
joshualitt2c93efe2014-11-06 12:57:13 -0800177 GrStencilSettings* outStencilSettings) {
178
179 switch (fill) {
180 default:
181 SkFAIL("Unexpected path fill.");
182 case GrPathRendering::kWinding_FillType:
183 *outStencilSettings = winding_path_stencil_settings();
184 break;
185 case GrPathRendering::kEvenOdd_FillType:
186 *outStencilSettings = even_odd_path_stencil_settings();
187 break;
188 }
joshualitt9853cce2014-11-17 14:22:48 -0800189 this->clipMaskManager()->adjustPathStencilParams(sb, outStencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800190}
191
joshualitt1c735482015-07-13 08:08:25 -0700192void GrDrawTarget::stencilPath(const GrPipelineBuilder& pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800193 const GrPathProcessor* pathProc,
joshualitt9853cce2014-11-17 14:22:48 -0800194 const GrPath* path,
195 GrPathRendering::FillType fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000196 // TODO: extract portions of checkDraw that are relevant to path stenciling.
bsalomon49f085d2014-09-05 13:34:00 -0700197 SkASSERT(path);
jvanverthe9c0fc62015-04-29 11:18:05 -0700198 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
joshualitt2c93efe2014-11-06 12:57:13 -0800199
200 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800201 GrScissorState scissorState;
joshualitt4421a4c2015-07-13 09:36:41 -0700202 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
egdaniel8dd688b2015-01-22 10:16:09 -0800203 GrPipelineBuilder::AutoRestoreStencil ars;
joshualitt4421a4c2015-07-13 09:36:41 -0700204 if (!this->setupClip(pipelineBuilder, &arfps, &ars, &scissorState, NULL)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800205 return;
206 }
207
208 // set stencil settings for path
209 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700210 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdaniel8dc7c3a2015-04-16 11:22:42 -0700211 GrStencilAttachment* sb = rt->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800212 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800213
joshualitt1c735482015-07-13 08:08:25 -0700214 this->onStencilPath(pipelineBuilder, pathProc, path, scissorState, stencilSettings);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000215}
216
joshualitt1c735482015-07-13 08:08:25 -0700217void GrDrawTarget::drawPath(const GrPipelineBuilder& pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800218 const GrPathProcessor* pathProc,
joshualitt9853cce2014-11-17 14:22:48 -0800219 const GrPath* path,
220 GrPathRendering::FillType fill) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000221 // TODO: extract portions of checkDraw that are relevant to path rendering.
bsalomon49f085d2014-09-05 13:34:00 -0700222 SkASSERT(path);
jvanverthe9c0fc62015-04-29 11:18:05 -0700223 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000224
joshualitt92e496f2014-10-31 13:56:50 -0700225 SkRect devBounds = path->getBounds();
joshualitt8059eb92014-12-29 15:10:07 -0800226 pathProc->viewMatrix().mapRect(&devBounds);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000227
joshualitt2c93efe2014-11-06 12:57:13 -0800228 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800229 GrScissorState scissorState;
joshualitt4421a4c2015-07-13 09:36:41 -0700230 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
egdaniel8dd688b2015-01-22 10:16:09 -0800231 GrPipelineBuilder::AutoRestoreStencil ars;
joshualitt4421a4c2015-07-13 09:36:41 -0700232 if (!this->setupClip(pipelineBuilder, &arfps, &ars, &scissorState, &devBounds)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800233 return;
234 }
235
236 // set stencil settings for path
237 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700238 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdaniel8dc7c3a2015-04-16 11:22:42 -0700239 GrStencilAttachment* sb = rt->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800240 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800241
bsalomona387a112015-08-11 14:47:42 -0700242 GrDrawTarget::PipelineInfo pipelineInfo(&pipelineBuilder, &scissorState, pathProc, &devBounds,
egdaniele36914c2015-02-13 09:00:33 -0800243 this);
bsalomona387a112015-08-11 14:47:42 -0700244 if (!pipelineInfo.valid()) {
egdaniele36914c2015-02-13 09:00:33 -0800245 return;
246 }
247
248 this->onDrawPath(pathProc, path, stencilSettings, pipelineInfo);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000249}
250
joshualitt1c735482015-07-13 08:08:25 -0700251void GrDrawTarget::drawPaths(const GrPipelineBuilder& pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800252 const GrPathProcessor* pathProc,
joshualitt9853cce2014-11-17 14:22:48 -0800253 const GrPathRange* pathRange,
cdalton55b24af2014-11-25 11:00:56 -0800254 const void* indices,
255 PathIndexType indexType,
256 const float transformValues[],
257 PathTransformType transformType,
joshualitt9853cce2014-11-17 14:22:48 -0800258 int count,
joshualitt92e496f2014-10-31 13:56:50 -0700259 GrPathRendering::FillType fill) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700260 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
bsalomon49f085d2014-09-05 13:34:00 -0700261 SkASSERT(pathRange);
262 SkASSERT(indices);
bsalomonebc1c102015-08-06 17:33:16 -0700263 SkASSERT(0 == reinterpret_cast<intptr_t>(indices) %
264 GrPathRange::PathIndexSizeInBytes(indexType));
cdalton55b24af2014-11-25 11:00:56 -0800265 SkASSERT(transformValues);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000266
joshualitt2c93efe2014-11-06 12:57:13 -0800267 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800268 GrScissorState scissorState;
joshualitt4421a4c2015-07-13 09:36:41 -0700269 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
egdaniel8dd688b2015-01-22 10:16:09 -0800270 GrPipelineBuilder::AutoRestoreStencil ars;
joshualitt4421a4c2015-07-13 09:36:41 -0700271 if (!this->setupClip(pipelineBuilder, &arfps, &ars, &scissorState, NULL)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800272 return;
273 }
274
275 // set stencil settings for path
276 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700277 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdaniel8dc7c3a2015-04-16 11:22:42 -0700278 GrStencilAttachment* sb = rt->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800279 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800280
bsalomon50785a32015-02-06 07:02:37 -0800281 // Don't compute a bounding box for dst copy texture, we'll opt
cdaltonb85a0aa2014-07-21 15:32:44 -0700282 // instead for it to just copy the entire dst. Realistically this is a moot
283 // point, because any context that supports NV_path_rendering will also
284 // support NV_blend_equation_advanced.
bsalomona387a112015-08-11 14:47:42 -0700285 GrDrawTarget::PipelineInfo pipelineInfo(&pipelineBuilder, &scissorState, pathProc, NULL, this);
286 if (!pipelineInfo.valid()) {
egdaniele36914c2015-02-13 09:00:33 -0800287 return;
288 }
289
290 this->onDrawPaths(pathProc, pathRange, indices, indexType, transformValues,
291 transformType, count, stencilSettings, pipelineInfo);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000292}
293
joshualitt1c735482015-07-13 08:08:25 -0700294void GrDrawTarget::drawBWRect(const GrPipelineBuilder& pipelineBuilder,
295 GrColor color,
296 const SkMatrix& viewMatrix,
297 const SkRect& rect,
298 const SkRect* localRect,
299 const SkMatrix* localMatrix) {
bsalomonabd30f52015-08-13 13:34:48 -0700300 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateFillBW(color, viewMatrix, rect,
301 localRect, localMatrix));
joshualittad17cfc2015-05-05 10:45:57 -0700302 this->drawBatch(pipelineBuilder, batch);
303}
304
joshualitt1c735482015-07-13 08:08:25 -0700305void GrDrawTarget::drawAARect(const GrPipelineBuilder& pipelineBuilder,
robertphillipsea461502015-05-26 11:38:03 -0700306 GrColor color,
307 const SkMatrix& viewMatrix,
308 const SkRect& rect,
309 const SkRect& devRect) {
bsalomonabd30f52015-08-13 13:34:48 -0700310 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateFillAA(color, viewMatrix, rect,
311 devRect));
joshualitt14205b12015-08-10 11:40:56 -0700312 this->drawBatch(pipelineBuilder, batch);
robertphillipsea461502015-05-26 11:38:03 -0700313}
314
joshualitt9853cce2014-11-17 14:22:48 -0800315void GrDrawTarget::clear(const SkIRect* rect,
316 GrColor color,
317 bool canIgnoreRect,
bsalomon63b21962014-11-05 07:05:34 -0800318 GrRenderTarget* renderTarget) {
egdaniel51c8d402015-08-06 10:54:13 -0700319 SkIRect rtRect = SkIRect::MakeWH(renderTarget->width(), renderTarget->height());
320 SkIRect clippedRect;
321 if (!rect ||
322 (canIgnoreRect && this->caps()->fullClearIsFree()) ||
323 rect->contains(rtRect)) {
324 rect = &rtRect;
325 } else {
326 clippedRect = *rect;
327 if (!clippedRect.intersect(rtRect)) {
328 return;
329 }
330 rect = &clippedRect;
331 }
332
bsalomon63b21962014-11-05 07:05:34 -0800333 if (fCaps->useDrawInsteadOfClear()) {
334 // This works around a driver bug with clear by drawing a rect instead.
335 // The driver will ignore a clear if it is the only thing rendered to a
336 // target before the target is read.
egdaniel51c8d402015-08-06 10:54:13 -0700337 if (rect == &rtRect) {
bsalomon63b21962014-11-05 07:05:34 -0800338 this->discard(renderTarget);
339 }
bsalomon63b21962014-11-05 07:05:34 -0800340
egdaniel8dd688b2015-01-22 10:16:09 -0800341 GrPipelineBuilder pipelineBuilder;
342 pipelineBuilder.setRenderTarget(renderTarget);
joshualitt9853cce2014-11-17 14:22:48 -0800343
joshualitt1c735482015-07-13 08:08:25 -0700344 this->drawSimpleRect(pipelineBuilder, color, SkMatrix::I(), *rect);
bsalomon53469832015-08-18 09:20:09 -0700345 } else {
346 GrBatch* batch = SkNEW_ARGS(GrClearBatch, (*rect, color, renderTarget));
347 this->onDrawBatch(batch);
348 batch->unref();
349 }
350}
351
352void GrDrawTarget::discard(GrRenderTarget* renderTarget) {
353 if (this->caps()->discardRenderTargetSupport()) {
354 GrBatch* batch = SkNEW_ARGS(GrDiscardBatch, (renderTarget));
355 this->onDrawBatch(batch);
356 batch->unref();
bsalomon63b21962014-11-05 07:05:34 -0800357 }
358}
359
egdaniel3eee3832014-06-18 13:09:11 -0700360typedef GrTraceMarkerSet::Iter TMIter;
361void GrDrawTarget::saveActiveTraceMarkers() {
362 if (this->caps()->gpuTracingSupport()) {
363 SkASSERT(0 == fStoredTraceMarkers.count());
364 fStoredTraceMarkers.addSet(fActiveTraceMarkers);
365 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
366 this->removeGpuTraceMarker(&(*iter));
367 }
368 }
369}
370
371void GrDrawTarget::restoreActiveTraceMarkers() {
372 if (this->caps()->gpuTracingSupport()) {
373 SkASSERT(0 == fActiveTraceMarkers.count());
374 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
375 this->addGpuTraceMarker(&(*iter));
376 }
377 for (TMIter iter = fActiveTraceMarkers.begin(); iter != fActiveTraceMarkers.end(); ++iter) {
378 this->fStoredTraceMarkers.remove(*iter);
379 }
380 }
381}
382
383void GrDrawTarget::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000384 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000385 SkASSERT(fGpuTraceMarkerCount >= 0);
386 this->fActiveTraceMarkers.add(*marker);
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000387 ++fGpuTraceMarkerCount;
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000388 }
389}
390
egdaniel3eee3832014-06-18 13:09:11 -0700391void GrDrawTarget::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000392 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000393 SkASSERT(fGpuTraceMarkerCount >= 1);
394 this->fActiveTraceMarkers.remove(*marker);
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000395 --fGpuTraceMarkerCount;
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000396 }
397}
398
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000399////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000400
bsalomon@google.com116ad842013-04-09 15:38:19 +0000401namespace {
402// returns true if the read/written rect intersects the src/dst and false if not.
403bool clip_srcrect_and_dstpoint(const GrSurface* dst,
404 const GrSurface* src,
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000405 const SkIRect& srcRect,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000406 const SkIPoint& dstPoint,
407 SkIRect* clippedSrcRect,
408 SkIPoint* clippedDstPoint) {
409 *clippedSrcRect = srcRect;
410 *clippedDstPoint = dstPoint;
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000411
bsalomon@google.com116ad842013-04-09 15:38:19 +0000412 // clip the left edge to src and dst bounds, adjusting dstPoint if necessary
413 if (clippedSrcRect->fLeft < 0) {
414 clippedDstPoint->fX -= clippedSrcRect->fLeft;
415 clippedSrcRect->fLeft = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000416 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000417 if (clippedDstPoint->fX < 0) {
418 clippedSrcRect->fLeft -= clippedDstPoint->fX;
419 clippedDstPoint->fX = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000420 }
421
bsalomon@google.com116ad842013-04-09 15:38:19 +0000422 // clip the top edge to src and dst bounds, adjusting dstPoint if necessary
423 if (clippedSrcRect->fTop < 0) {
424 clippedDstPoint->fY -= clippedSrcRect->fTop;
425 clippedSrcRect->fTop = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000426 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000427 if (clippedDstPoint->fY < 0) {
428 clippedSrcRect->fTop -= clippedDstPoint->fY;
429 clippedDstPoint->fY = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000430 }
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000431
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000432 // clip the right edge to the src and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000433 if (clippedSrcRect->fRight > src->width()) {
434 clippedSrcRect->fRight = src->width();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000435 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000436 if (clippedDstPoint->fX + clippedSrcRect->width() > dst->width()) {
437 clippedSrcRect->fRight = clippedSrcRect->fLeft + dst->width() - clippedDstPoint->fX;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000438 }
439
440 // clip the bottom edge to the src and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000441 if (clippedSrcRect->fBottom > src->height()) {
442 clippedSrcRect->fBottom = src->height();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000443 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000444 if (clippedDstPoint->fY + clippedSrcRect->height() > dst->height()) {
445 clippedSrcRect->fBottom = clippedSrcRect->fTop + dst->height() - clippedDstPoint->fY;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000446 }
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000447
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000448 // The above clipping steps may have inverted the rect if it didn't intersect either the src or
449 // dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000450 return !clippedSrcRect->isEmpty();
451}
452}
453
bsalomon6df86402015-06-01 10:41:49 -0700454void GrDrawTarget::copySurface(GrSurface* dst,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000455 GrSurface* src,
456 const SkIRect& srcRect,
457 const SkIPoint& dstPoint) {
bsalomon49f085d2014-09-05 13:34:00 -0700458 SkASSERT(dst);
459 SkASSERT(src);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000460
461 SkIRect clippedSrcRect;
462 SkIPoint clippedDstPoint;
463 // If the rect is outside the src or dst then we've already succeeded.
464 if (!clip_srcrect_and_dstpoint(dst,
465 src,
466 srcRect,
467 dstPoint,
468 &clippedSrcRect,
469 &clippedDstPoint)) {
bsalomon6df86402015-06-01 10:41:49 -0700470 return;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000471 }
472
bsalomon6df86402015-06-01 10:41:49 -0700473 this->onCopySurface(dst, src, clippedSrcRect, clippedDstPoint);
bsalomon@google.comeb851172013-04-15 13:51:00 +0000474}
475
egdaniele36914c2015-02-13 09:00:33 -0800476///////////////////////////////////////////////////////////////////////////////
477
bsalomona387a112015-08-11 14:47:42 -0700478GrDrawTarget::PipelineInfo::PipelineInfo(const GrPipelineBuilder* pipelineBuilder,
479 const GrScissorState* scissor,
egdaniele36914c2015-02-13 09:00:33 -0800480 const GrPrimitiveProcessor* primProc,
481 const SkRect* devBounds,
bsalomona387a112015-08-11 14:47:42 -0700482 GrDrawTarget* target) {
483 fArgs.fPipelineBuilder = pipelineBuilder;
484 fArgs.fCaps = target->caps();
485 fArgs.fScissor = scissor;
486 fArgs.fColorPOI = fArgs.fPipelineBuilder->colorProcInfo(primProc);
487 fArgs.fCoveragePOI = fArgs.fPipelineBuilder->coverageProcInfo(primProc);
488 if (!target->setupDstReadIfNecessary(*fArgs.fPipelineBuilder, fArgs.fColorPOI,
489 fArgs.fCoveragePOI, &fArgs.fDstTexture, devBounds)) {
490 fArgs.fPipelineBuilder = NULL;
egdaniele36914c2015-02-13 09:00:33 -0800491 }
492}
493
bsalomona387a112015-08-11 14:47:42 -0700494GrDrawTarget::PipelineInfo::PipelineInfo(const GrPipelineBuilder* pipelineBuilder,
495 const GrScissorState* scissor,
bsalomonabd30f52015-08-13 13:34:48 -0700496 const GrDrawBatch* batch,
egdaniele36914c2015-02-13 09:00:33 -0800497 const SkRect* devBounds,
bsalomona387a112015-08-11 14:47:42 -0700498 GrDrawTarget* target) {
499 fArgs.fPipelineBuilder = pipelineBuilder;
500 fArgs.fCaps = target->caps();
501 fArgs.fScissor = scissor;
502 fArgs.fColorPOI = fArgs.fPipelineBuilder->colorProcInfo(batch);
503 fArgs.fCoveragePOI = fArgs.fPipelineBuilder->coverageProcInfo(batch);
504 if (!target->setupDstReadIfNecessary(*fArgs.fPipelineBuilder, fArgs.fColorPOI,
505 fArgs.fCoveragePOI, &fArgs.fDstTexture, devBounds)) {
506 fArgs.fPipelineBuilder = NULL;
egdaniele36914c2015-02-13 09:00:33 -0800507 }
508}
509
bsalomon@google.combcce8922013-03-25 15:38:39 +0000510///////////////////////////////////////////////////////////////////////////////
bsalomon4061b122015-05-29 10:26:19 -0700511GrClipTarget::GrClipTarget(GrContext* context)
512 : INHERITED(context->getGpu(), context->resourceProvider())
513 , fContext(context) {
bsalomonedd77a12015-05-29 09:45:57 -0700514 fClipMaskManager.reset(SkNEW_ARGS(GrClipMaskManager, (this)));
515}
516
bsalomon@google.combcce8922013-03-25 15:38:39 +0000517
joshualitt1c735482015-07-13 08:08:25 -0700518bool GrClipTarget::setupClip(const GrPipelineBuilder& pipelineBuilder,
joshualitt4421a4c2015-07-13 09:36:41 -0700519 GrPipelineBuilder::AutoRestoreFragmentProcessorState* arfps,
egdaniel8dd688b2015-01-22 10:16:09 -0800520 GrPipelineBuilder::AutoRestoreStencil* ars,
joshualitt8059eb92014-12-29 15:10:07 -0800521 GrScissorState* scissorState,
522 const SkRect* devBounds) {
joshualitt1c735482015-07-13 08:08:25 -0700523 return fClipMaskManager->setupClipping(pipelineBuilder,
joshualitt4421a4c2015-07-13 09:36:41 -0700524 arfps,
joshualitt5e6ba212015-07-13 07:35:05 -0700525 ars,
joshualitt5e6ba212015-07-13 07:35:05 -0700526 scissorState,
527 devBounds);
joshualitt2c93efe2014-11-06 12:57:13 -0800528}
bsalomonedd77a12015-05-29 09:45:57 -0700529
530void GrClipTarget::purgeResources() {
531 // The clip mask manager can rebuild all its clip masks so just
532 // get rid of them all.
533 fClipMaskManager->purgeResources();
534};