blob: ee668f014c8d1c2038f29337a988bc0c39f10b15 [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"
bsalomon62c447d2014-08-08 08:08:50 -070023#include "GrTemplates.h"
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000024#include "GrTexture.h"
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000025#include "GrVertexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000026
sugoi@google.com5f74cf82012-12-17 21:16:45 +000027#include "SkStrokeRec.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000028
reed@google.comac10a2d2010-12-22 21:39:39 +000029////////////////////////////////////////////////////////////////////////////////
30
bsalomon4061b122015-05-29 10:26:19 -070031GrDrawTarget::GrDrawTarget(GrGpu* gpu, GrResourceProvider* resourceProvider)
32 : fGpu(SkRef(gpu))
33 , fCaps(SkRef(gpu->caps()))
34 , fResourceProvider(resourceProvider)
bsalomona73239a2015-04-28 13:35:17 -070035 , fGpuTraceMarkerCount(0)
bsalomona73239a2015-04-28 13:35:17 -070036 , fFlushing(false) {
bsalomon4061b122015-05-29 10:26:19 -070037}
38
39GrDrawTarget::~GrDrawTarget() {
40 fGpu->unref();
41 fCaps->unref();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000042}
43
44////////////////////////////////////////////////////////////////////////////////
45
bsalomon50785a32015-02-06 07:02:37 -080046bool GrDrawTarget::setupDstReadIfNecessary(const GrPipelineBuilder& pipelineBuilder,
egdaniele36914c2015-02-13 09:00:33 -080047 const GrProcOptInfo& colorPOI,
48 const GrProcOptInfo& coveragePOI,
bsalomon6a44c6a2015-05-26 09:49:05 -070049 GrXferProcessor::DstTexture* dstTexture,
joshualitt9853cce2014-11-17 14:22:48 -080050 const SkRect* drawBounds) {
bsalomon6a44c6a2015-05-26 09:49:05 -070051 if (!pipelineBuilder.willXPNeedDstTexture(*this->caps(), colorPOI, coveragePOI)) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +000052 return true;
53 }
cdalton9954bc32015-04-29 14:17:00 -070054
bsalomon50785a32015-02-06 07:02:37 -080055 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
cdalton9954bc32015-04-29 14:17:00 -070056
57 if (this->caps()->textureBarrierSupport()) {
58 if (GrTexture* rtTex = rt->asTexture()) {
bsalomondc47ff72015-05-26 12:16:59 -070059 // The render target is a texture, so we can read from it directly in the shader. The XP
cdalton9954bc32015-04-29 14:17:00 -070060 // will be responsible to detect this situation and request a texture barrier.
bsalomon6a44c6a2015-05-26 09:49:05 -070061 dstTexture->setTexture(rtTex);
62 dstTexture->setOffset(0, 0);
cdalton9954bc32015-04-29 14:17:00 -070063 return true;
64 }
65 }
66
67 SkIRect copyRect;
joshualitt44701df2015-02-23 14:44:57 -080068 pipelineBuilder.clip().getConservativeBounds(rt, &copyRect);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000069
bsalomon49f085d2014-09-05 13:34:00 -070070 if (drawBounds) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000071 SkIRect drawIBounds;
72 drawBounds->roundOut(&drawIBounds);
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +000073 if (!copyRect.intersect(drawIBounds)) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000074#ifdef SK_DEBUG
bsalomon682c2692015-05-22 14:01:46 -070075 GrCapsDebugf(fCaps, "Missed an early reject. "
76 "Bailing on draw from setupDstReadIfNecessary.\n");
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +000077#endif
78 return false;
79 }
80 } else {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000081#ifdef SK_DEBUG
tfarina38406c82014-10-31 07:11:12 -070082 //SkDebugf("No dev bounds when dst copy is made.\n");
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +000083#endif
84 }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +000085
commit-bot@chromium.org63150af2013-04-11 22:00:22 +000086 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
87 // have per-sample dst values by making the copy multisampled.
bsalomonf2703d82014-10-28 14:33:06 -070088 GrSurfaceDesc desc;
bsalomona73239a2015-04-28 13:35:17 -070089 if (!this->getGpu()->initCopySurfaceDstDesc(rt, &desc)) {
90 desc.fOrigin = kDefault_GrSurfaceOrigin;
91 desc.fFlags = kRenderTarget_GrSurfaceFlag;
92 desc.fConfig = rt->config();
93 }
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
bsalomon4061b122015-05-29 10:26:19 -070099 SkAutoTUnref<GrTexture> copy(
100 fResourceProvider->refScratchTexture(desc, GrTextureProvider::kApprox_ScratchTexMatch));
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
119 this->getGpu()->saveActiveTraceMarkers();
120
121 this->onFlush();
122
123 this->getGpu()->restoreActiveTraceMarkers();
124
125 fFlushing = false;
126 this->reset();
127}
128
joshualitt4d8da812015-01-28 12:53:54 -0800129void GrDrawTarget::drawBatch(GrPipelineBuilder* pipelineBuilder,
joshualitt99c7c072015-05-01 13:43:30 -0700130 GrBatch* batch) {
joshualitt4d8da812015-01-28 12:53:54 -0800131 SkASSERT(pipelineBuilder);
132 // TODO some kind of checkdraw, but not at this level
133
134 // Setup clip
135 GrScissorState scissorState;
bsalomon6be6f7c2015-02-26 13:05:21 -0800136 GrPipelineBuilder::AutoRestoreFragmentProcessors arfp;
joshualitt4d8da812015-01-28 12:53:54 -0800137 GrPipelineBuilder::AutoRestoreStencil ars;
joshualitt99c7c072015-05-01 13:43:30 -0700138 if (!this->setupClip(pipelineBuilder, &arfp, &ars, &scissorState, &batch->bounds())) {
joshualitt4d8da812015-01-28 12:53:54 -0800139 return;
140 }
141
joshualitt99c7c072015-05-01 13:43:30 -0700142 // Batch bounds are tight, so for dev copies
143 // TODO move this into setupDstReadIfNecessary when paths are in batch
144 SkRect bounds = batch->bounds();
145 bounds.outset(0.5f, 0.5f);
146
147 GrDrawTarget::PipelineInfo pipelineInfo(pipelineBuilder, &scissorState, batch, &bounds,
148 this);
egdaniele36914c2015-02-13 09:00:33 -0800149 if (pipelineInfo.mustSkipDraw()) {
150 return;
151 }
152
153 this->onDrawBatch(batch, pipelineInfo);
joshualitt4d8da812015-01-28 12:53:54 -0800154}
155
joshualitt2c93efe2014-11-06 12:57:13 -0800156static const GrStencilSettings& winding_path_stencil_settings() {
157 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
158 kIncClamp_StencilOp,
159 kIncClamp_StencilOp,
160 kAlwaysIfInClip_StencilFunc,
161 0xFFFF, 0xFFFF, 0xFFFF);
162 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
163}
164
165static const GrStencilSettings& even_odd_path_stencil_settings() {
166 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
167 kInvert_StencilOp,
168 kInvert_StencilOp,
169 kAlwaysIfInClip_StencilFunc,
170 0xFFFF, 0xFFFF, 0xFFFF);
171 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
172}
173
174void GrDrawTarget::getPathStencilSettingsForFilltype(GrPathRendering::FillType fill,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700175 const GrStencilAttachment* sb,
joshualitt2c93efe2014-11-06 12:57:13 -0800176 GrStencilSettings* outStencilSettings) {
177
178 switch (fill) {
179 default:
180 SkFAIL("Unexpected path fill.");
181 case GrPathRendering::kWinding_FillType:
182 *outStencilSettings = winding_path_stencil_settings();
183 break;
184 case GrPathRendering::kEvenOdd_FillType:
185 *outStencilSettings = even_odd_path_stencil_settings();
186 break;
187 }
joshualitt9853cce2014-11-17 14:22:48 -0800188 this->clipMaskManager()->adjustPathStencilParams(sb, outStencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800189}
190
egdaniel8dd688b2015-01-22 10:16:09 -0800191void GrDrawTarget::stencilPath(GrPipelineBuilder* pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800192 const GrPathProcessor* pathProc,
joshualitt9853cce2014-11-17 14:22:48 -0800193 const GrPath* path,
194 GrPathRendering::FillType fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000195 // TODO: extract portions of checkDraw that are relevant to path stenciling.
bsalomon49f085d2014-09-05 13:34:00 -0700196 SkASSERT(path);
jvanverthe9c0fc62015-04-29 11:18:05 -0700197 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
egdaniel8dd688b2015-01-22 10:16:09 -0800198 SkASSERT(pipelineBuilder);
joshualitt2c93efe2014-11-06 12:57:13 -0800199
200 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800201 GrScissorState scissorState;
bsalomon6be6f7c2015-02-26 13:05:21 -0800202 GrPipelineBuilder::AutoRestoreFragmentProcessors arfp;
egdaniel8dd688b2015-01-22 10:16:09 -0800203 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomon6be6f7c2015-02-26 13:05:21 -0800204 if (!this->setupClip(pipelineBuilder, &arfp, &ars, &scissorState, NULL)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800205 return;
206 }
207
208 // set stencil settings for path
209 GrStencilSettings stencilSettings;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800210 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
egdaniel8dd688b2015-01-22 10:16:09 -0800214 this->onStencilPath(*pipelineBuilder, pathProc, path, scissorState, stencilSettings);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000215}
216
egdaniel8dd688b2015-01-22 10:16:09 -0800217void GrDrawTarget::drawPath(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());
egdaniel8dd688b2015-01-22 10:16:09 -0800224 SkASSERT(pipelineBuilder);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000225
joshualitt92e496f2014-10-31 13:56:50 -0700226 SkRect devBounds = path->getBounds();
joshualitt8059eb92014-12-29 15:10:07 -0800227 pathProc->viewMatrix().mapRect(&devBounds);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000228
joshualitt2c93efe2014-11-06 12:57:13 -0800229 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800230 GrScissorState scissorState;
bsalomon6be6f7c2015-02-26 13:05:21 -0800231 GrPipelineBuilder::AutoRestoreFragmentProcessors arfp;
egdaniel8dd688b2015-01-22 10:16:09 -0800232 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomon6be6f7c2015-02-26 13:05:21 -0800233 if (!this->setupClip(pipelineBuilder, &arfp, &ars, &scissorState, &devBounds)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800234 return;
235 }
236
237 // set stencil settings for path
238 GrStencilSettings stencilSettings;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800239 GrRenderTarget* rt = pipelineBuilder->getRenderTarget();
egdaniel8dc7c3a2015-04-16 11:22:42 -0700240 GrStencilAttachment* sb = rt->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800241 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800242
egdaniele36914c2015-02-13 09:00:33 -0800243 GrDrawTarget::PipelineInfo pipelineInfo(pipelineBuilder, &scissorState, pathProc, &devBounds,
244 this);
245 if (pipelineInfo.mustSkipDraw()) {
246 return;
247 }
248
249 this->onDrawPath(pathProc, path, stencilSettings, pipelineInfo);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000250}
251
egdaniel8dd688b2015-01-22 10:16:09 -0800252void GrDrawTarget::drawPaths(GrPipelineBuilder* pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800253 const GrPathProcessor* pathProc,
joshualitt9853cce2014-11-17 14:22:48 -0800254 const GrPathRange* pathRange,
cdalton55b24af2014-11-25 11:00:56 -0800255 const void* indices,
256 PathIndexType indexType,
257 const float transformValues[],
258 PathTransformType transformType,
joshualitt9853cce2014-11-17 14:22:48 -0800259 int count,
joshualitt92e496f2014-10-31 13:56:50 -0700260 GrPathRendering::FillType fill) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700261 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
bsalomon49f085d2014-09-05 13:34:00 -0700262 SkASSERT(pathRange);
263 SkASSERT(indices);
cdalton55b24af2014-11-25 11:00:56 -0800264 SkASSERT(0 == reinterpret_cast<long>(indices) % GrPathRange::PathIndexSizeInBytes(indexType));
265 SkASSERT(transformValues);
egdaniel8dd688b2015-01-22 10:16:09 -0800266 SkASSERT(pipelineBuilder);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000267
joshualitt2c93efe2014-11-06 12:57:13 -0800268 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800269 GrScissorState scissorState;
bsalomon6be6f7c2015-02-26 13:05:21 -0800270 GrPipelineBuilder::AutoRestoreFragmentProcessors arfp;
egdaniel8dd688b2015-01-22 10:16:09 -0800271 GrPipelineBuilder::AutoRestoreStencil ars;
joshualitt2c93efe2014-11-06 12:57:13 -0800272
bsalomon6be6f7c2015-02-26 13:05:21 -0800273 if (!this->setupClip(pipelineBuilder, &arfp, &ars, &scissorState, NULL)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800274 return;
275 }
276
277 // set stencil settings for path
278 GrStencilSettings stencilSettings;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800279 GrRenderTarget* rt = pipelineBuilder->getRenderTarget();
egdaniel8dc7c3a2015-04-16 11:22:42 -0700280 GrStencilAttachment* sb = rt->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800281 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800282
bsalomon50785a32015-02-06 07:02:37 -0800283 // Don't compute a bounding box for dst copy texture, we'll opt
cdaltonb85a0aa2014-07-21 15:32:44 -0700284 // instead for it to just copy the entire dst. Realistically this is a moot
285 // point, because any context that supports NV_path_rendering will also
286 // support NV_blend_equation_advanced.
egdaniele36914c2015-02-13 09:00:33 -0800287 GrDrawTarget::PipelineInfo pipelineInfo(pipelineBuilder, &scissorState, pathProc, NULL, this);
288 if (pipelineInfo.mustSkipDraw()) {
289 return;
290 }
291
292 this->onDrawPaths(pathProc, pathRange, indices, indexType, transformValues,
293 transformType, count, stencilSettings, pipelineInfo);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000294}
295
robertphillipsea461502015-05-26 11:38:03 -0700296void GrDrawTarget::drawBWRect(GrPipelineBuilder* pipelineBuilder,
joshualittad17cfc2015-05-05 10:45:57 -0700297 GrColor color,
298 const SkMatrix& viewMatrix,
299 const SkRect& rect,
300 const SkRect* localRect,
301 const SkMatrix* localMatrix) {
302 SkAutoTUnref<GrBatch> batch(GrRectBatch::Create(color, viewMatrix, rect, localRect,
303 localMatrix));
304 this->drawBatch(pipelineBuilder, batch);
305}
306
robertphillipsea461502015-05-26 11:38:03 -0700307void GrDrawTarget::drawAARect(GrPipelineBuilder* pipelineBuilder,
308 GrColor color,
309 const SkMatrix& viewMatrix,
310 const SkRect& rect,
311 const SkRect& devRect) {
312 GrAARectRenderer::FillAARect(this, pipelineBuilder, color, viewMatrix, rect, devRect);
313}
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) {
319 if (fCaps->useDrawInsteadOfClear()) {
320 // This works around a driver bug with clear by drawing a rect instead.
321 // The driver will ignore a clear if it is the only thing rendered to a
322 // target before the target is read.
323 SkIRect rtRect = SkIRect::MakeWH(renderTarget->width(), renderTarget->height());
324 if (NULL == rect || canIgnoreRect || rect->contains(rtRect)) {
325 rect = &rtRect;
326 // We first issue a discard() since that may help tilers.
327 this->discard(renderTarget);
328 }
bsalomon63b21962014-11-05 07:05:34 -0800329
egdaniel8dd688b2015-01-22 10:16:09 -0800330 GrPipelineBuilder pipelineBuilder;
331 pipelineBuilder.setRenderTarget(renderTarget);
joshualitt9853cce2014-11-17 14:22:48 -0800332
egdaniel8dd688b2015-01-22 10:16:09 -0800333 this->drawSimpleRect(&pipelineBuilder, color, SkMatrix::I(), *rect);
bsalomon63b21962014-11-05 07:05:34 -0800334 } else {
335 this->onClear(rect, color, canIgnoreRect, renderTarget);
336 }
337}
338
egdaniel3eee3832014-06-18 13:09:11 -0700339typedef GrTraceMarkerSet::Iter TMIter;
340void GrDrawTarget::saveActiveTraceMarkers() {
341 if (this->caps()->gpuTracingSupport()) {
342 SkASSERT(0 == fStoredTraceMarkers.count());
343 fStoredTraceMarkers.addSet(fActiveTraceMarkers);
344 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
345 this->removeGpuTraceMarker(&(*iter));
346 }
347 }
348}
349
350void GrDrawTarget::restoreActiveTraceMarkers() {
351 if (this->caps()->gpuTracingSupport()) {
352 SkASSERT(0 == fActiveTraceMarkers.count());
353 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
354 this->addGpuTraceMarker(&(*iter));
355 }
356 for (TMIter iter = fActiveTraceMarkers.begin(); iter != fActiveTraceMarkers.end(); ++iter) {
357 this->fStoredTraceMarkers.remove(*iter);
358 }
359 }
360}
361
362void GrDrawTarget::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000363 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000364 SkASSERT(fGpuTraceMarkerCount >= 0);
365 this->fActiveTraceMarkers.add(*marker);
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000366 ++fGpuTraceMarkerCount;
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000367 }
368}
369
egdaniel3eee3832014-06-18 13:09:11 -0700370void GrDrawTarget::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000371 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000372 SkASSERT(fGpuTraceMarkerCount >= 1);
373 this->fActiveTraceMarkers.remove(*marker);
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000374 --fGpuTraceMarkerCount;
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000375 }
376}
377
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000378////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000379
bsalomon@google.com116ad842013-04-09 15:38:19 +0000380namespace {
381// returns true if the read/written rect intersects the src/dst and false if not.
382bool clip_srcrect_and_dstpoint(const GrSurface* dst,
383 const GrSurface* src,
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000384 const SkIRect& srcRect,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000385 const SkIPoint& dstPoint,
386 SkIRect* clippedSrcRect,
387 SkIPoint* clippedDstPoint) {
388 *clippedSrcRect = srcRect;
389 *clippedDstPoint = dstPoint;
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000390
bsalomon@google.com116ad842013-04-09 15:38:19 +0000391 // clip the left edge to src and dst bounds, adjusting dstPoint if necessary
392 if (clippedSrcRect->fLeft < 0) {
393 clippedDstPoint->fX -= clippedSrcRect->fLeft;
394 clippedSrcRect->fLeft = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000395 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000396 if (clippedDstPoint->fX < 0) {
397 clippedSrcRect->fLeft -= clippedDstPoint->fX;
398 clippedDstPoint->fX = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000399 }
400
bsalomon@google.com116ad842013-04-09 15:38:19 +0000401 // clip the top edge to src and dst bounds, adjusting dstPoint if necessary
402 if (clippedSrcRect->fTop < 0) {
403 clippedDstPoint->fY -= clippedSrcRect->fTop;
404 clippedSrcRect->fTop = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000405 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000406 if (clippedDstPoint->fY < 0) {
407 clippedSrcRect->fTop -= clippedDstPoint->fY;
408 clippedDstPoint->fY = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000409 }
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000410
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000411 // clip the right edge to the src and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000412 if (clippedSrcRect->fRight > src->width()) {
413 clippedSrcRect->fRight = src->width();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000414 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000415 if (clippedDstPoint->fX + clippedSrcRect->width() > dst->width()) {
416 clippedSrcRect->fRight = clippedSrcRect->fLeft + dst->width() - clippedDstPoint->fX;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000417 }
418
419 // clip the bottom edge to the src and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000420 if (clippedSrcRect->fBottom > src->height()) {
421 clippedSrcRect->fBottom = src->height();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000422 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000423 if (clippedDstPoint->fY + clippedSrcRect->height() > dst->height()) {
424 clippedSrcRect->fBottom = clippedSrcRect->fTop + dst->height() - clippedDstPoint->fY;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000425 }
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000426
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000427 // The above clipping steps may have inverted the rect if it didn't intersect either the src or
428 // dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000429 return !clippedSrcRect->isEmpty();
430}
431}
432
bsalomon6df86402015-06-01 10:41:49 -0700433void GrDrawTarget::copySurface(GrSurface* dst,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000434 GrSurface* src,
435 const SkIRect& srcRect,
436 const SkIPoint& dstPoint) {
bsalomon49f085d2014-09-05 13:34:00 -0700437 SkASSERT(dst);
438 SkASSERT(src);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000439
440 SkIRect clippedSrcRect;
441 SkIPoint clippedDstPoint;
442 // If the rect is outside the src or dst then we've already succeeded.
443 if (!clip_srcrect_and_dstpoint(dst,
444 src,
445 srcRect,
446 dstPoint,
447 &clippedSrcRect,
448 &clippedDstPoint)) {
bsalomon6df86402015-06-01 10:41:49 -0700449 return;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000450 }
451
bsalomon6df86402015-06-01 10:41:49 -0700452 this->onCopySurface(dst, src, clippedSrcRect, clippedDstPoint);
bsalomon@google.comeb851172013-04-15 13:51:00 +0000453}
454
egdaniele36914c2015-02-13 09:00:33 -0800455void GrDrawTarget::setupPipeline(const PipelineInfo& pipelineInfo,
456 GrPipeline* pipeline) {
457 SkNEW_PLACEMENT_ARGS(pipeline, GrPipeline, (*pipelineInfo.fPipelineBuilder,
458 pipelineInfo.fColorPOI,
459 pipelineInfo.fCoveragePOI,
460 *this->caps(),
461 *pipelineInfo.fScissor,
bsalomon6a44c6a2015-05-26 09:49:05 -0700462 &pipelineInfo.fDstTexture));
egdaniele36914c2015-02-13 09:00:33 -0800463}
464///////////////////////////////////////////////////////////////////////////////
465
466GrDrawTarget::PipelineInfo::PipelineInfo(GrPipelineBuilder* pipelineBuilder,
467 GrScissorState* scissor,
468 const GrPrimitiveProcessor* primProc,
469 const SkRect* devBounds,
470 GrDrawTarget* target)
471 : fPipelineBuilder(pipelineBuilder)
472 , fScissor(scissor) {
473 fColorPOI = fPipelineBuilder->colorProcInfo(primProc);
474 fCoveragePOI = fPipelineBuilder->coverageProcInfo(primProc);
475 if (!target->setupDstReadIfNecessary(*fPipelineBuilder, fColorPOI, fCoveragePOI,
bsalomon6a44c6a2015-05-26 09:49:05 -0700476 &fDstTexture, devBounds)) {
egdaniele36914c2015-02-13 09:00:33 -0800477 fPipelineBuilder = NULL;
478 }
479}
480
481GrDrawTarget::PipelineInfo::PipelineInfo(GrPipelineBuilder* pipelineBuilder,
482 GrScissorState* scissor,
483 const GrBatch* batch,
484 const SkRect* devBounds,
485 GrDrawTarget* target)
486 : fPipelineBuilder(pipelineBuilder)
487 , fScissor(scissor) {
488 fColorPOI = fPipelineBuilder->colorProcInfo(batch);
489 fCoveragePOI = fPipelineBuilder->coverageProcInfo(batch);
490 if (!target->setupDstReadIfNecessary(*fPipelineBuilder, fColorPOI, fCoveragePOI,
bsalomon6a44c6a2015-05-26 09:49:05 -0700491 &fDstTexture, devBounds)) {
egdaniele36914c2015-02-13 09:00:33 -0800492 fPipelineBuilder = NULL;
493 }
494}
495
bsalomon@google.combcce8922013-03-25 15:38:39 +0000496///////////////////////////////////////////////////////////////////////////////
bsalomon4061b122015-05-29 10:26:19 -0700497GrClipTarget::GrClipTarget(GrContext* context)
498 : INHERITED(context->getGpu(), context->resourceProvider())
499 , fContext(context) {
bsalomonedd77a12015-05-29 09:45:57 -0700500 fClipMaskManager.reset(SkNEW_ARGS(GrClipMaskManager, (this)));
501}
502
bsalomon@google.combcce8922013-03-25 15:38:39 +0000503
egdaniel8dd688b2015-01-22 10:16:09 -0800504bool GrClipTarget::setupClip(GrPipelineBuilder* pipelineBuilder,
bsalomon6be6f7c2015-02-26 13:05:21 -0800505 GrPipelineBuilder::AutoRestoreFragmentProcessors* arfp,
egdaniel8dd688b2015-01-22 10:16:09 -0800506 GrPipelineBuilder::AutoRestoreStencil* ars,
joshualitt8059eb92014-12-29 15:10:07 -0800507 GrScissorState* scissorState,
508 const SkRect* devBounds) {
bsalomonedd77a12015-05-29 09:45:57 -0700509 return fClipMaskManager->setupClipping(pipelineBuilder,
bsalomon6be6f7c2015-02-26 13:05:21 -0800510 arfp,
joshualitt2c93efe2014-11-06 12:57:13 -0800511 ars,
joshualitt9853cce2014-11-17 14:22:48 -0800512 scissorState,
joshualitt9853cce2014-11-17 14:22:48 -0800513 devBounds);
joshualitt2c93efe2014-11-06 12:57:13 -0800514}
bsalomonedd77a12015-05-29 09:45:57 -0700515
516void GrClipTarget::purgeResources() {
517 // The clip mask manager can rebuild all its clip masks so just
518 // get rid of them all.
519 fClipMaskManager->purgeResources();
520};