blob: 0049767e8b420cfde0bc53b8487920ea80584c9a [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
robertphillipsea461502015-05-26 11:38:03 -070011#include "GrAARectRenderer.h"
joshualitt4d8da812015-01-28 12:53:54 -080012#include "GrBatch.h"
bsalomoneb1cb5c2015-05-22 08:01:09 -070013#include "GrCaps.h"
bsalomon4061b122015-05-29 10:26:19 -070014#include "GrGpu.h"
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000015#include "GrPath.h"
egdaniele36914c2015-02-13 09:00:33 -080016#include "GrPipeline.h"
joshualittb7133be2015-04-08 09:08:31 -070017#include "GrMemoryPool.h"
joshualittad17cfc2015-05-05 10:45:57 -070018#include "GrRectBatch.h"
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000019#include "GrRenderTarget.h"
bsalomon4061b122015-05-29 10:26:19 -070020#include "GrResourceProvider.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080021#include "GrRenderTargetPriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070022#include "GrSurfacePriv.h"
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000023#include "GrTexture.h"
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000024#include "GrVertexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000025
sugoi@google.com5f74cf82012-12-17 21:16:45 +000026#include "SkStrokeRec.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000027
reed@google.comac10a2d2010-12-22 21:39:39 +000028////////////////////////////////////////////////////////////////////////////////
29
bsalomon4061b122015-05-29 10:26:19 -070030GrDrawTarget::GrDrawTarget(GrGpu* gpu, GrResourceProvider* resourceProvider)
31 : fGpu(SkRef(gpu))
32 , fCaps(SkRef(gpu->caps()))
33 , fResourceProvider(resourceProvider)
bsalomona73239a2015-04-28 13:35:17 -070034 , fGpuTraceMarkerCount(0)
bsalomona73239a2015-04-28 13:35:17 -070035 , fFlushing(false) {
bsalomon4061b122015-05-29 10:26:19 -070036}
37
38GrDrawTarget::~GrDrawTarget() {
39 fGpu->unref();
40 fCaps->unref();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000041}
42
43////////////////////////////////////////////////////////////////////////////////
44
bsalomon50785a32015-02-06 07:02:37 -080045bool GrDrawTarget::setupDstReadIfNecessary(const GrPipelineBuilder& pipelineBuilder,
egdaniele36914c2015-02-13 09:00:33 -080046 const GrProcOptInfo& colorPOI,
47 const GrProcOptInfo& coveragePOI,
bsalomon6a44c6a2015-05-26 09:49:05 -070048 GrXferProcessor::DstTexture* dstTexture,
joshualitt9853cce2014-11-17 14:22:48 -080049 const SkRect* drawBounds) {
bsalomon6a44c6a2015-05-26 09:49:05 -070050 if (!pipelineBuilder.willXPNeedDstTexture(*this->caps(), colorPOI, coveragePOI)) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +000051 return true;
52 }
cdalton9954bc32015-04-29 14:17:00 -070053
bsalomon50785a32015-02-06 07:02:37 -080054 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
cdalton9954bc32015-04-29 14:17:00 -070055
56 if (this->caps()->textureBarrierSupport()) {
57 if (GrTexture* rtTex = rt->asTexture()) {
bsalomondc47ff72015-05-26 12:16:59 -070058 // The render target is a texture, so we can read from it directly in the shader. The XP
cdalton9954bc32015-04-29 14:17:00 -070059 // will be responsible to detect this situation and request a texture barrier.
bsalomon6a44c6a2015-05-26 09:49:05 -070060 dstTexture->setTexture(rtTex);
61 dstTexture->setOffset(0, 0);
cdalton9954bc32015-04-29 14:17:00 -070062 return true;
63 }
64 }
65
66 SkIRect copyRect;
joshualitt44701df2015-02-23 14:44:57 -080067 pipelineBuilder.clip().getConservativeBounds(rt, &copyRect);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000068
bsalomon49f085d2014-09-05 13:34:00 -070069 if (drawBounds) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000070 SkIRect drawIBounds;
71 drawBounds->roundOut(&drawIBounds);
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +000072 if (!copyRect.intersect(drawIBounds)) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000073#ifdef SK_DEBUG
bsalomon682c2692015-05-22 14:01:46 -070074 GrCapsDebugf(fCaps, "Missed an early reject. "
75 "Bailing on draw from setupDstReadIfNecessary.\n");
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +000076#endif
77 return false;
78 }
79 } else {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000080#ifdef SK_DEBUG
tfarina38406c82014-10-31 07:11:12 -070081 //SkDebugf("No dev bounds when dst copy is made.\n");
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +000082#endif
83 }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +000084
commit-bot@chromium.org63150af2013-04-11 22:00:22 +000085 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
86 // have per-sample dst values by making the copy multisampled.
bsalomonf2703d82014-10-28 14:33:06 -070087 GrSurfaceDesc desc;
bsalomona73239a2015-04-28 13:35:17 -070088 if (!this->getGpu()->initCopySurfaceDstDesc(rt, &desc)) {
89 desc.fOrigin = kDefault_GrSurfaceOrigin;
90 desc.fFlags = kRenderTarget_GrSurfaceFlag;
91 desc.fConfig = rt->config();
92 }
93
94
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +000095 desc.fWidth = copyRect.width();
96 desc.fHeight = copyRect.height();
bsalomon@google.com26e18b52013-03-29 19:22:36 +000097
bsalomoneae62002015-07-31 13:59:30 -070098 static const uint32_t kFlags = 0;
99 SkAutoTUnref<GrTexture> copy(fResourceProvider->createApproxTexture(desc, kFlags));
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000100
bsalomone3059732014-10-14 11:47:22 -0700101 if (!copy) {
tfarina38406c82014-10-31 07:11:12 -0700102 SkDebugf("Failed to create temporary copy of destination texture.\n");
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000103 return false;
104 }
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000105 SkIPoint dstPoint = {0, 0};
bsalomon6df86402015-06-01 10:41:49 -0700106 this->copySurface(copy, rt, copyRect, dstPoint);
107 dstTexture->setTexture(copy);
108 dstTexture->setOffset(copyRect.fLeft, copyRect.fTop);
109 return true;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000110}
111
bsalomona73239a2015-04-28 13:35:17 -0700112void GrDrawTarget::flush() {
113 if (fFlushing) {
114 return;
115 }
116 fFlushing = true;
117
118 this->getGpu()->saveActiveTraceMarkers();
119
120 this->onFlush();
121
122 this->getGpu()->restoreActiveTraceMarkers();
123
124 fFlushing = false;
125 this->reset();
126}
127
joshualitt1c735482015-07-13 08:08:25 -0700128void GrDrawTarget::drawBatch(const GrPipelineBuilder& pipelineBuilder, GrBatch* batch) {
joshualitt4d8da812015-01-28 12:53:54 -0800129 // TODO some kind of checkdraw, but not at this level
130
131 // Setup clip
132 GrScissorState scissorState;
joshualitt4421a4c2015-07-13 09:36:41 -0700133 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
joshualitt4d8da812015-01-28 12:53:54 -0800134 GrPipelineBuilder::AutoRestoreStencil ars;
joshualitt4421a4c2015-07-13 09:36:41 -0700135 if (!this->setupClip(pipelineBuilder, &arfps, &ars, &scissorState, &batch->bounds())) {
joshualitt4d8da812015-01-28 12:53:54 -0800136 return;
137 }
138
joshualitt99c7c072015-05-01 13:43:30 -0700139 // Batch bounds are tight, so for dev copies
140 // TODO move this into setupDstReadIfNecessary when paths are in batch
141 SkRect bounds = batch->bounds();
142 bounds.outset(0.5f, 0.5f);
143
144 GrDrawTarget::PipelineInfo pipelineInfo(pipelineBuilder, &scissorState, batch, &bounds,
145 this);
egdaniele36914c2015-02-13 09:00:33 -0800146 if (pipelineInfo.mustSkipDraw()) {
147 return;
148 }
149
150 this->onDrawBatch(batch, pipelineInfo);
joshualitt4d8da812015-01-28 12:53:54 -0800151}
152
joshualitt2c93efe2014-11-06 12:57:13 -0800153static const GrStencilSettings& winding_path_stencil_settings() {
154 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
155 kIncClamp_StencilOp,
156 kIncClamp_StencilOp,
157 kAlwaysIfInClip_StencilFunc,
158 0xFFFF, 0xFFFF, 0xFFFF);
159 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
160}
161
162static const GrStencilSettings& even_odd_path_stencil_settings() {
163 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
164 kInvert_StencilOp,
165 kInvert_StencilOp,
166 kAlwaysIfInClip_StencilFunc,
167 0xFFFF, 0xFFFF, 0xFFFF);
168 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
169}
170
171void GrDrawTarget::getPathStencilSettingsForFilltype(GrPathRendering::FillType fill,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700172 const GrStencilAttachment* sb,
joshualitt2c93efe2014-11-06 12:57:13 -0800173 GrStencilSettings* outStencilSettings) {
174
175 switch (fill) {
176 default:
177 SkFAIL("Unexpected path fill.");
178 case GrPathRendering::kWinding_FillType:
179 *outStencilSettings = winding_path_stencil_settings();
180 break;
181 case GrPathRendering::kEvenOdd_FillType:
182 *outStencilSettings = even_odd_path_stencil_settings();
183 break;
184 }
joshualitt9853cce2014-11-17 14:22:48 -0800185 this->clipMaskManager()->adjustPathStencilParams(sb, outStencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800186}
187
joshualitt1c735482015-07-13 08:08:25 -0700188void GrDrawTarget::stencilPath(const GrPipelineBuilder& pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800189 const GrPathProcessor* pathProc,
joshualitt9853cce2014-11-17 14:22:48 -0800190 const GrPath* path,
191 GrPathRendering::FillType fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000192 // TODO: extract portions of checkDraw that are relevant to path stenciling.
bsalomon49f085d2014-09-05 13:34:00 -0700193 SkASSERT(path);
jvanverthe9c0fc62015-04-29 11:18:05 -0700194 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
joshualitt2c93efe2014-11-06 12:57:13 -0800195
196 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800197 GrScissorState scissorState;
joshualitt4421a4c2015-07-13 09:36:41 -0700198 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
egdaniel8dd688b2015-01-22 10:16:09 -0800199 GrPipelineBuilder::AutoRestoreStencil ars;
joshualitt4421a4c2015-07-13 09:36:41 -0700200 if (!this->setupClip(pipelineBuilder, &arfps, &ars, &scissorState, NULL)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800201 return;
202 }
203
204 // set stencil settings for path
205 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700206 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdaniel8dc7c3a2015-04-16 11:22:42 -0700207 GrStencilAttachment* sb = rt->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800208 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800209
joshualitt1c735482015-07-13 08:08:25 -0700210 this->onStencilPath(pipelineBuilder, pathProc, path, scissorState, stencilSettings);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000211}
212
joshualitt1c735482015-07-13 08:08:25 -0700213void GrDrawTarget::drawPath(const GrPipelineBuilder& pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800214 const GrPathProcessor* pathProc,
joshualitt9853cce2014-11-17 14:22:48 -0800215 const GrPath* path,
216 GrPathRendering::FillType fill) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000217 // TODO: extract portions of checkDraw that are relevant to path rendering.
bsalomon49f085d2014-09-05 13:34:00 -0700218 SkASSERT(path);
jvanverthe9c0fc62015-04-29 11:18:05 -0700219 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000220
joshualitt92e496f2014-10-31 13:56:50 -0700221 SkRect devBounds = path->getBounds();
joshualitt8059eb92014-12-29 15:10:07 -0800222 pathProc->viewMatrix().mapRect(&devBounds);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000223
joshualitt2c93efe2014-11-06 12:57:13 -0800224 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800225 GrScissorState scissorState;
joshualitt4421a4c2015-07-13 09:36:41 -0700226 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
egdaniel8dd688b2015-01-22 10:16:09 -0800227 GrPipelineBuilder::AutoRestoreStencil ars;
joshualitt4421a4c2015-07-13 09:36:41 -0700228 if (!this->setupClip(pipelineBuilder, &arfps, &ars, &scissorState, &devBounds)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800229 return;
230 }
231
232 // set stencil settings for path
233 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700234 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdaniel8dc7c3a2015-04-16 11:22:42 -0700235 GrStencilAttachment* sb = rt->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800236 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800237
egdaniele36914c2015-02-13 09:00:33 -0800238 GrDrawTarget::PipelineInfo pipelineInfo(pipelineBuilder, &scissorState, pathProc, &devBounds,
239 this);
240 if (pipelineInfo.mustSkipDraw()) {
241 return;
242 }
243
244 this->onDrawPath(pathProc, path, stencilSettings, pipelineInfo);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000245}
246
joshualitt1c735482015-07-13 08:08:25 -0700247void GrDrawTarget::drawPaths(const GrPipelineBuilder& pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800248 const GrPathProcessor* pathProc,
joshualitt9853cce2014-11-17 14:22:48 -0800249 const GrPathRange* pathRange,
cdalton55b24af2014-11-25 11:00:56 -0800250 const void* indices,
251 PathIndexType indexType,
252 const float transformValues[],
253 PathTransformType transformType,
joshualitt9853cce2014-11-17 14:22:48 -0800254 int count,
joshualitt92e496f2014-10-31 13:56:50 -0700255 GrPathRendering::FillType fill) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700256 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
bsalomon49f085d2014-09-05 13:34:00 -0700257 SkASSERT(pathRange);
258 SkASSERT(indices);
cdalton55b24af2014-11-25 11:00:56 -0800259 SkASSERT(0 == reinterpret_cast<long>(indices) % GrPathRange::PathIndexSizeInBytes(indexType));
260 SkASSERT(transformValues);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000261
joshualitt2c93efe2014-11-06 12:57:13 -0800262 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800263 GrScissorState scissorState;
joshualitt4421a4c2015-07-13 09:36:41 -0700264 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
egdaniel8dd688b2015-01-22 10:16:09 -0800265 GrPipelineBuilder::AutoRestoreStencil ars;
joshualitt4421a4c2015-07-13 09:36:41 -0700266 if (!this->setupClip(pipelineBuilder, &arfps, &ars, &scissorState, NULL)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800267 return;
268 }
269
270 // set stencil settings for path
271 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700272 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdaniel8dc7c3a2015-04-16 11:22:42 -0700273 GrStencilAttachment* sb = rt->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800274 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800275
bsalomon50785a32015-02-06 07:02:37 -0800276 // Don't compute a bounding box for dst copy texture, we'll opt
cdaltonb85a0aa2014-07-21 15:32:44 -0700277 // instead for it to just copy the entire dst. Realistically this is a moot
278 // point, because any context that supports NV_path_rendering will also
279 // support NV_blend_equation_advanced.
egdaniele36914c2015-02-13 09:00:33 -0800280 GrDrawTarget::PipelineInfo pipelineInfo(pipelineBuilder, &scissorState, pathProc, NULL, this);
281 if (pipelineInfo.mustSkipDraw()) {
282 return;
283 }
284
285 this->onDrawPaths(pathProc, pathRange, indices, indexType, transformValues,
286 transformType, count, stencilSettings, pipelineInfo);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000287}
288
joshualitt1c735482015-07-13 08:08:25 -0700289void GrDrawTarget::drawBWRect(const GrPipelineBuilder& pipelineBuilder,
290 GrColor color,
291 const SkMatrix& viewMatrix,
292 const SkRect& rect,
293 const SkRect* localRect,
294 const SkMatrix* localMatrix) {
joshualittad17cfc2015-05-05 10:45:57 -0700295 SkAutoTUnref<GrBatch> batch(GrRectBatch::Create(color, viewMatrix, rect, localRect,
296 localMatrix));
297 this->drawBatch(pipelineBuilder, batch);
298}
299
joshualitt1c735482015-07-13 08:08:25 -0700300void GrDrawTarget::drawAARect(const GrPipelineBuilder& pipelineBuilder,
robertphillipsea461502015-05-26 11:38:03 -0700301 GrColor color,
302 const SkMatrix& viewMatrix,
303 const SkRect& rect,
304 const SkRect& devRect) {
305 GrAARectRenderer::FillAARect(this, pipelineBuilder, color, viewMatrix, rect, devRect);
306}
307
joshualitt9853cce2014-11-17 14:22:48 -0800308void GrDrawTarget::clear(const SkIRect* rect,
309 GrColor color,
310 bool canIgnoreRect,
bsalomon63b21962014-11-05 07:05:34 -0800311 GrRenderTarget* renderTarget) {
312 if (fCaps->useDrawInsteadOfClear()) {
313 // This works around a driver bug with clear by drawing a rect instead.
314 // The driver will ignore a clear if it is the only thing rendered to a
315 // target before the target is read.
316 SkIRect rtRect = SkIRect::MakeWH(renderTarget->width(), renderTarget->height());
317 if (NULL == rect || canIgnoreRect || rect->contains(rtRect)) {
318 rect = &rtRect;
319 // We first issue a discard() since that may help tilers.
320 this->discard(renderTarget);
321 }
bsalomon63b21962014-11-05 07:05:34 -0800322
egdaniel8dd688b2015-01-22 10:16:09 -0800323 GrPipelineBuilder pipelineBuilder;
324 pipelineBuilder.setRenderTarget(renderTarget);
joshualitt9853cce2014-11-17 14:22:48 -0800325
joshualitt1c735482015-07-13 08:08:25 -0700326 this->drawSimpleRect(pipelineBuilder, color, SkMatrix::I(), *rect);
bsalomon63b21962014-11-05 07:05:34 -0800327 } else {
328 this->onClear(rect, color, canIgnoreRect, renderTarget);
329 }
330}
331
egdaniel3eee3832014-06-18 13:09:11 -0700332typedef GrTraceMarkerSet::Iter TMIter;
333void GrDrawTarget::saveActiveTraceMarkers() {
334 if (this->caps()->gpuTracingSupport()) {
335 SkASSERT(0 == fStoredTraceMarkers.count());
336 fStoredTraceMarkers.addSet(fActiveTraceMarkers);
337 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
338 this->removeGpuTraceMarker(&(*iter));
339 }
340 }
341}
342
343void GrDrawTarget::restoreActiveTraceMarkers() {
344 if (this->caps()->gpuTracingSupport()) {
345 SkASSERT(0 == fActiveTraceMarkers.count());
346 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
347 this->addGpuTraceMarker(&(*iter));
348 }
349 for (TMIter iter = fActiveTraceMarkers.begin(); iter != fActiveTraceMarkers.end(); ++iter) {
350 this->fStoredTraceMarkers.remove(*iter);
351 }
352 }
353}
354
355void GrDrawTarget::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000356 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000357 SkASSERT(fGpuTraceMarkerCount >= 0);
358 this->fActiveTraceMarkers.add(*marker);
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000359 ++fGpuTraceMarkerCount;
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000360 }
361}
362
egdaniel3eee3832014-06-18 13:09:11 -0700363void GrDrawTarget::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000364 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000365 SkASSERT(fGpuTraceMarkerCount >= 1);
366 this->fActiveTraceMarkers.remove(*marker);
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000367 --fGpuTraceMarkerCount;
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000368 }
369}
370
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000371////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000372
bsalomon@google.com116ad842013-04-09 15:38:19 +0000373namespace {
374// returns true if the read/written rect intersects the src/dst and false if not.
375bool clip_srcrect_and_dstpoint(const GrSurface* dst,
376 const GrSurface* src,
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000377 const SkIRect& srcRect,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000378 const SkIPoint& dstPoint,
379 SkIRect* clippedSrcRect,
380 SkIPoint* clippedDstPoint) {
381 *clippedSrcRect = srcRect;
382 *clippedDstPoint = dstPoint;
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000383
bsalomon@google.com116ad842013-04-09 15:38:19 +0000384 // clip the left edge to src and dst bounds, adjusting dstPoint if necessary
385 if (clippedSrcRect->fLeft < 0) {
386 clippedDstPoint->fX -= clippedSrcRect->fLeft;
387 clippedSrcRect->fLeft = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000388 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000389 if (clippedDstPoint->fX < 0) {
390 clippedSrcRect->fLeft -= clippedDstPoint->fX;
391 clippedDstPoint->fX = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000392 }
393
bsalomon@google.com116ad842013-04-09 15:38:19 +0000394 // clip the top edge to src and dst bounds, adjusting dstPoint if necessary
395 if (clippedSrcRect->fTop < 0) {
396 clippedDstPoint->fY -= clippedSrcRect->fTop;
397 clippedSrcRect->fTop = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000398 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000399 if (clippedDstPoint->fY < 0) {
400 clippedSrcRect->fTop -= clippedDstPoint->fY;
401 clippedDstPoint->fY = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000402 }
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000403
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000404 // clip the right edge to the src and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000405 if (clippedSrcRect->fRight > src->width()) {
406 clippedSrcRect->fRight = src->width();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000407 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000408 if (clippedDstPoint->fX + clippedSrcRect->width() > dst->width()) {
409 clippedSrcRect->fRight = clippedSrcRect->fLeft + dst->width() - clippedDstPoint->fX;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000410 }
411
412 // clip the bottom edge to the src and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000413 if (clippedSrcRect->fBottom > src->height()) {
414 clippedSrcRect->fBottom = src->height();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000415 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000416 if (clippedDstPoint->fY + clippedSrcRect->height() > dst->height()) {
417 clippedSrcRect->fBottom = clippedSrcRect->fTop + dst->height() - clippedDstPoint->fY;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000418 }
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000419
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000420 // The above clipping steps may have inverted the rect if it didn't intersect either the src or
421 // dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000422 return !clippedSrcRect->isEmpty();
423}
424}
425
bsalomon6df86402015-06-01 10:41:49 -0700426void GrDrawTarget::copySurface(GrSurface* dst,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000427 GrSurface* src,
428 const SkIRect& srcRect,
429 const SkIPoint& dstPoint) {
bsalomon49f085d2014-09-05 13:34:00 -0700430 SkASSERT(dst);
431 SkASSERT(src);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000432
433 SkIRect clippedSrcRect;
434 SkIPoint clippedDstPoint;
435 // If the rect is outside the src or dst then we've already succeeded.
436 if (!clip_srcrect_and_dstpoint(dst,
437 src,
438 srcRect,
439 dstPoint,
440 &clippedSrcRect,
441 &clippedDstPoint)) {
bsalomon6df86402015-06-01 10:41:49 -0700442 return;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000443 }
444
bsalomon6df86402015-06-01 10:41:49 -0700445 this->onCopySurface(dst, src, clippedSrcRect, clippedDstPoint);
bsalomon@google.comeb851172013-04-15 13:51:00 +0000446}
447
bsalomon85cd78d2015-07-31 12:15:50 -0700448const GrPipeline* GrDrawTarget::setupPipeline(const PipelineInfo& pipelineInfo,
449 void* pipelineAddr) {
450 return SkNEW_PLACEMENT_ARGS(pipelineAddr, GrPipeline, (*pipelineInfo.fPipelineBuilder,
451 pipelineInfo.fColorPOI,
452 pipelineInfo.fCoveragePOI,
453 *this->caps(),
454 *pipelineInfo.fScissor,
455 &pipelineInfo.fDstTexture));
egdaniele36914c2015-02-13 09:00:33 -0800456}
457///////////////////////////////////////////////////////////////////////////////
458
joshualitt1c735482015-07-13 08:08:25 -0700459GrDrawTarget::PipelineInfo::PipelineInfo(const GrPipelineBuilder& pipelineBuilder,
egdaniele36914c2015-02-13 09:00:33 -0800460 GrScissorState* scissor,
461 const GrPrimitiveProcessor* primProc,
462 const SkRect* devBounds,
463 GrDrawTarget* target)
joshualitt1c735482015-07-13 08:08:25 -0700464 : fPipelineBuilder(&pipelineBuilder)
egdaniele36914c2015-02-13 09:00:33 -0800465 , fScissor(scissor) {
466 fColorPOI = fPipelineBuilder->colorProcInfo(primProc);
467 fCoveragePOI = fPipelineBuilder->coverageProcInfo(primProc);
468 if (!target->setupDstReadIfNecessary(*fPipelineBuilder, fColorPOI, fCoveragePOI,
bsalomon6a44c6a2015-05-26 09:49:05 -0700469 &fDstTexture, devBounds)) {
egdaniele36914c2015-02-13 09:00:33 -0800470 fPipelineBuilder = NULL;
471 }
472}
473
joshualitt1c735482015-07-13 08:08:25 -0700474GrDrawTarget::PipelineInfo::PipelineInfo(const GrPipelineBuilder& pipelineBuilder,
egdaniele36914c2015-02-13 09:00:33 -0800475 GrScissorState* scissor,
476 const GrBatch* batch,
477 const SkRect* devBounds,
478 GrDrawTarget* target)
joshualitt1c735482015-07-13 08:08:25 -0700479 : fPipelineBuilder(&pipelineBuilder)
egdaniele36914c2015-02-13 09:00:33 -0800480 , fScissor(scissor) {
481 fColorPOI = fPipelineBuilder->colorProcInfo(batch);
482 fCoveragePOI = fPipelineBuilder->coverageProcInfo(batch);
483 if (!target->setupDstReadIfNecessary(*fPipelineBuilder, fColorPOI, fCoveragePOI,
bsalomon6a44c6a2015-05-26 09:49:05 -0700484 &fDstTexture, devBounds)) {
egdaniele36914c2015-02-13 09:00:33 -0800485 fPipelineBuilder = NULL;
486 }
487}
488
bsalomon@google.combcce8922013-03-25 15:38:39 +0000489///////////////////////////////////////////////////////////////////////////////
bsalomon4061b122015-05-29 10:26:19 -0700490GrClipTarget::GrClipTarget(GrContext* context)
491 : INHERITED(context->getGpu(), context->resourceProvider())
492 , fContext(context) {
bsalomonedd77a12015-05-29 09:45:57 -0700493 fClipMaskManager.reset(SkNEW_ARGS(GrClipMaskManager, (this)));
494}
495
bsalomon@google.combcce8922013-03-25 15:38:39 +0000496
joshualitt1c735482015-07-13 08:08:25 -0700497bool GrClipTarget::setupClip(const GrPipelineBuilder& pipelineBuilder,
joshualitt4421a4c2015-07-13 09:36:41 -0700498 GrPipelineBuilder::AutoRestoreFragmentProcessorState* arfps,
egdaniel8dd688b2015-01-22 10:16:09 -0800499 GrPipelineBuilder::AutoRestoreStencil* ars,
joshualitt8059eb92014-12-29 15:10:07 -0800500 GrScissorState* scissorState,
501 const SkRect* devBounds) {
joshualitt1c735482015-07-13 08:08:25 -0700502 return fClipMaskManager->setupClipping(pipelineBuilder,
joshualitt4421a4c2015-07-13 09:36:41 -0700503 arfps,
joshualitt5e6ba212015-07-13 07:35:05 -0700504 ars,
joshualitt5e6ba212015-07-13 07:35:05 -0700505 scissorState,
506 devBounds);
joshualitt2c93efe2014-11-06 12:57:13 -0800507}
bsalomonedd77a12015-05-29 09:45:57 -0700508
509void GrClipTarget::purgeResources() {
510 // The clip mask manager can rebuild all its clip masks so just
511 // get rid of them all.
512 fClipMaskManager->purgeResources();
513};