blob: c2a9eb0aa727ab992987e821424e11f70fd096cc [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"
bsalomoneb1cb5c2015-05-22 08:01:09 -070012#include "GrCaps.h"
bsalomon4061b122015-05-29 10:26:19 -070013#include "GrGpu.h"
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000014#include "GrPath.h"
egdaniele36914c2015-02-13 09:00:33 -080015#include "GrPipeline.h"
joshualittb7133be2015-04-08 09:08:31 -070016#include "GrMemoryPool.h"
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000017#include "GrRenderTarget.h"
bsalomon4061b122015-05-29 10:26:19 -070018#include "GrResourceProvider.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080019#include "GrRenderTargetPriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070020#include "GrSurfacePriv.h"
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000021#include "GrTexture.h"
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000022#include "GrVertexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000023
joshualitt74417822015-08-07 11:42:16 -070024#include "batches/GrBatch.h"
joshualittecd1a692015-08-10 10:08:26 -070025#include "batches/GrRectBatchFactory.h"
joshualitt74417822015-08-07 11:42:16 -070026
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
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
119 this->getGpu()->saveActiveTraceMarkers();
120
121 this->onFlush();
122
123 this->getGpu()->restoreActiveTraceMarkers();
124
125 fFlushing = false;
126 this->reset();
127}
128
joshualitt1c735482015-07-13 08:08:25 -0700129void GrDrawTarget::drawBatch(const GrPipelineBuilder& pipelineBuilder, GrBatch* batch) {
joshualitt4d8da812015-01-28 12:53:54 -0800130 // TODO some kind of checkdraw, but not at this level
131
132 // Setup clip
133 GrScissorState scissorState;
joshualitt4421a4c2015-07-13 09:36:41 -0700134 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
joshualitt4d8da812015-01-28 12:53:54 -0800135 GrPipelineBuilder::AutoRestoreStencil ars;
joshualitt4421a4c2015-07-13 09:36:41 -0700136 if (!this->setupClip(pipelineBuilder, &arfps, &ars, &scissorState, &batch->bounds())) {
joshualitt4d8da812015-01-28 12:53:54 -0800137 return;
138 }
139
joshualitt99c7c072015-05-01 13:43:30 -0700140 // Batch bounds are tight, so for dev copies
141 // TODO move this into setupDstReadIfNecessary when paths are in batch
142 SkRect bounds = batch->bounds();
143 bounds.outset(0.5f, 0.5f);
144
145 GrDrawTarget::PipelineInfo pipelineInfo(pipelineBuilder, &scissorState, batch, &bounds,
146 this);
egdaniele36914c2015-02-13 09:00:33 -0800147 if (pipelineInfo.mustSkipDraw()) {
148 return;
149 }
150
151 this->onDrawBatch(batch, pipelineInfo);
joshualitt4d8da812015-01-28 12:53:54 -0800152}
153
joshualitt2c93efe2014-11-06 12:57:13 -0800154static const GrStencilSettings& winding_path_stencil_settings() {
155 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
156 kIncClamp_StencilOp,
157 kIncClamp_StencilOp,
158 kAlwaysIfInClip_StencilFunc,
159 0xFFFF, 0xFFFF, 0xFFFF);
160 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
161}
162
163static const GrStencilSettings& even_odd_path_stencil_settings() {
164 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
165 kInvert_StencilOp,
166 kInvert_StencilOp,
167 kAlwaysIfInClip_StencilFunc,
168 0xFFFF, 0xFFFF, 0xFFFF);
169 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
170}
171
172void GrDrawTarget::getPathStencilSettingsForFilltype(GrPathRendering::FillType fill,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700173 const GrStencilAttachment* sb,
joshualitt2c93efe2014-11-06 12:57:13 -0800174 GrStencilSettings* outStencilSettings) {
175
176 switch (fill) {
177 default:
178 SkFAIL("Unexpected path fill.");
179 case GrPathRendering::kWinding_FillType:
180 *outStencilSettings = winding_path_stencil_settings();
181 break;
182 case GrPathRendering::kEvenOdd_FillType:
183 *outStencilSettings = even_odd_path_stencil_settings();
184 break;
185 }
joshualitt9853cce2014-11-17 14:22:48 -0800186 this->clipMaskManager()->adjustPathStencilParams(sb, outStencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800187}
188
joshualitt1c735482015-07-13 08:08:25 -0700189void GrDrawTarget::stencilPath(const GrPipelineBuilder& pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800190 const GrPathProcessor* pathProc,
joshualitt9853cce2014-11-17 14:22:48 -0800191 const GrPath* path,
192 GrPathRendering::FillType fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000193 // TODO: extract portions of checkDraw that are relevant to path stenciling.
bsalomon49f085d2014-09-05 13:34:00 -0700194 SkASSERT(path);
jvanverthe9c0fc62015-04-29 11:18:05 -0700195 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
joshualitt2c93efe2014-11-06 12:57:13 -0800196
197 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800198 GrScissorState scissorState;
joshualitt4421a4c2015-07-13 09:36:41 -0700199 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
egdaniel8dd688b2015-01-22 10:16:09 -0800200 GrPipelineBuilder::AutoRestoreStencil ars;
joshualitt4421a4c2015-07-13 09:36:41 -0700201 if (!this->setupClip(pipelineBuilder, &arfps, &ars, &scissorState, NULL)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800202 return;
203 }
204
205 // set stencil settings for path
206 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700207 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdaniel8dc7c3a2015-04-16 11:22:42 -0700208 GrStencilAttachment* sb = rt->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800209 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800210
joshualitt1c735482015-07-13 08:08:25 -0700211 this->onStencilPath(pipelineBuilder, pathProc, path, scissorState, stencilSettings);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000212}
213
joshualitt1c735482015-07-13 08:08:25 -0700214void GrDrawTarget::drawPath(const GrPipelineBuilder& pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800215 const GrPathProcessor* pathProc,
joshualitt9853cce2014-11-17 14:22:48 -0800216 const GrPath* path,
217 GrPathRendering::FillType fill) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000218 // TODO: extract portions of checkDraw that are relevant to path rendering.
bsalomon49f085d2014-09-05 13:34:00 -0700219 SkASSERT(path);
jvanverthe9c0fc62015-04-29 11:18:05 -0700220 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000221
joshualitt92e496f2014-10-31 13:56:50 -0700222 SkRect devBounds = path->getBounds();
joshualitt8059eb92014-12-29 15:10:07 -0800223 pathProc->viewMatrix().mapRect(&devBounds);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000224
joshualitt2c93efe2014-11-06 12:57:13 -0800225 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800226 GrScissorState scissorState;
joshualitt4421a4c2015-07-13 09:36:41 -0700227 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
egdaniel8dd688b2015-01-22 10:16:09 -0800228 GrPipelineBuilder::AutoRestoreStencil ars;
joshualitt4421a4c2015-07-13 09:36:41 -0700229 if (!this->setupClip(pipelineBuilder, &arfps, &ars, &scissorState, &devBounds)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800230 return;
231 }
232
233 // set stencil settings for path
234 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700235 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdaniel8dc7c3a2015-04-16 11:22:42 -0700236 GrStencilAttachment* sb = rt->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800237 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800238
egdaniele36914c2015-02-13 09:00:33 -0800239 GrDrawTarget::PipelineInfo pipelineInfo(pipelineBuilder, &scissorState, pathProc, &devBounds,
240 this);
241 if (pipelineInfo.mustSkipDraw()) {
242 return;
243 }
244
245 this->onDrawPath(pathProc, path, stencilSettings, pipelineInfo);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000246}
247
joshualitt1c735482015-07-13 08:08:25 -0700248void GrDrawTarget::drawPaths(const GrPipelineBuilder& pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800249 const GrPathProcessor* pathProc,
joshualitt9853cce2014-11-17 14:22:48 -0800250 const GrPathRange* pathRange,
cdalton55b24af2014-11-25 11:00:56 -0800251 const void* indices,
252 PathIndexType indexType,
253 const float transformValues[],
254 PathTransformType transformType,
joshualitt9853cce2014-11-17 14:22:48 -0800255 int count,
joshualitt92e496f2014-10-31 13:56:50 -0700256 GrPathRendering::FillType fill) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700257 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
bsalomon49f085d2014-09-05 13:34:00 -0700258 SkASSERT(pathRange);
259 SkASSERT(indices);
bsalomonebc1c102015-08-06 17:33:16 -0700260 SkASSERT(0 == reinterpret_cast<intptr_t>(indices) %
261 GrPathRange::PathIndexSizeInBytes(indexType));
cdalton55b24af2014-11-25 11:00:56 -0800262 SkASSERT(transformValues);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000263
joshualitt2c93efe2014-11-06 12:57:13 -0800264 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800265 GrScissorState scissorState;
joshualitt4421a4c2015-07-13 09:36:41 -0700266 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
egdaniel8dd688b2015-01-22 10:16:09 -0800267 GrPipelineBuilder::AutoRestoreStencil ars;
joshualitt4421a4c2015-07-13 09:36:41 -0700268 if (!this->setupClip(pipelineBuilder, &arfps, &ars, &scissorState, NULL)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800269 return;
270 }
271
272 // set stencil settings for path
273 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700274 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdaniel8dc7c3a2015-04-16 11:22:42 -0700275 GrStencilAttachment* sb = rt->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800276 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800277
bsalomon50785a32015-02-06 07:02:37 -0800278 // Don't compute a bounding box for dst copy texture, we'll opt
cdaltonb85a0aa2014-07-21 15:32:44 -0700279 // instead for it to just copy the entire dst. Realistically this is a moot
280 // point, because any context that supports NV_path_rendering will also
281 // support NV_blend_equation_advanced.
egdaniele36914c2015-02-13 09:00:33 -0800282 GrDrawTarget::PipelineInfo pipelineInfo(pipelineBuilder, &scissorState, pathProc, NULL, this);
283 if (pipelineInfo.mustSkipDraw()) {
284 return;
285 }
286
287 this->onDrawPaths(pathProc, pathRange, indices, indexType, transformValues,
288 transformType, count, stencilSettings, pipelineInfo);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000289}
290
joshualitt1c735482015-07-13 08:08:25 -0700291void GrDrawTarget::drawBWRect(const GrPipelineBuilder& pipelineBuilder,
292 GrColor color,
293 const SkMatrix& viewMatrix,
294 const SkRect& rect,
295 const SkRect* localRect,
296 const SkMatrix* localMatrix) {
joshualittecd1a692015-08-10 10:08:26 -0700297 SkAutoTUnref<GrBatch> batch(GrRectBatchFactory::Create(color, viewMatrix, rect, localRect,
298 localMatrix));
joshualittad17cfc2015-05-05 10:45:57 -0700299 this->drawBatch(pipelineBuilder, batch);
300}
301
joshualitt1c735482015-07-13 08:08:25 -0700302void GrDrawTarget::drawAARect(const GrPipelineBuilder& pipelineBuilder,
robertphillipsea461502015-05-26 11:38:03 -0700303 GrColor color,
304 const SkMatrix& viewMatrix,
305 const SkRect& rect,
306 const SkRect& devRect) {
307 GrAARectRenderer::FillAARect(this, pipelineBuilder, color, viewMatrix, rect, devRect);
308}
309
joshualitt9853cce2014-11-17 14:22:48 -0800310void GrDrawTarget::clear(const SkIRect* rect,
311 GrColor color,
312 bool canIgnoreRect,
bsalomon63b21962014-11-05 07:05:34 -0800313 GrRenderTarget* renderTarget) {
egdaniel51c8d402015-08-06 10:54:13 -0700314 SkIRect rtRect = SkIRect::MakeWH(renderTarget->width(), renderTarget->height());
315 SkIRect clippedRect;
316 if (!rect ||
317 (canIgnoreRect && this->caps()->fullClearIsFree()) ||
318 rect->contains(rtRect)) {
319 rect = &rtRect;
320 } else {
321 clippedRect = *rect;
322 if (!clippedRect.intersect(rtRect)) {
323 return;
324 }
325 rect = &clippedRect;
326 }
327
bsalomon63b21962014-11-05 07:05:34 -0800328 if (fCaps->useDrawInsteadOfClear()) {
329 // This works around a driver bug with clear by drawing a rect instead.
330 // The driver will ignore a clear if it is the only thing rendered to a
331 // target before the target is read.
egdaniel51c8d402015-08-06 10:54:13 -0700332 if (rect == &rtRect) {
bsalomon63b21962014-11-05 07:05:34 -0800333 this->discard(renderTarget);
334 }
bsalomon63b21962014-11-05 07:05:34 -0800335
egdaniel8dd688b2015-01-22 10:16:09 -0800336 GrPipelineBuilder pipelineBuilder;
337 pipelineBuilder.setRenderTarget(renderTarget);
joshualitt9853cce2014-11-17 14:22:48 -0800338
joshualitt1c735482015-07-13 08:08:25 -0700339 this->drawSimpleRect(pipelineBuilder, color, SkMatrix::I(), *rect);
bsalomon63b21962014-11-05 07:05:34 -0800340 } else {
egdaniel51c8d402015-08-06 10:54:13 -0700341 this->onClear(*rect, color, renderTarget);
bsalomon63b21962014-11-05 07:05:34 -0800342 }
343}
344
egdaniel3eee3832014-06-18 13:09:11 -0700345typedef GrTraceMarkerSet::Iter TMIter;
346void GrDrawTarget::saveActiveTraceMarkers() {
347 if (this->caps()->gpuTracingSupport()) {
348 SkASSERT(0 == fStoredTraceMarkers.count());
349 fStoredTraceMarkers.addSet(fActiveTraceMarkers);
350 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
351 this->removeGpuTraceMarker(&(*iter));
352 }
353 }
354}
355
356void GrDrawTarget::restoreActiveTraceMarkers() {
357 if (this->caps()->gpuTracingSupport()) {
358 SkASSERT(0 == fActiveTraceMarkers.count());
359 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
360 this->addGpuTraceMarker(&(*iter));
361 }
362 for (TMIter iter = fActiveTraceMarkers.begin(); iter != fActiveTraceMarkers.end(); ++iter) {
363 this->fStoredTraceMarkers.remove(*iter);
364 }
365 }
366}
367
368void GrDrawTarget::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000369 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000370 SkASSERT(fGpuTraceMarkerCount >= 0);
371 this->fActiveTraceMarkers.add(*marker);
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000372 ++fGpuTraceMarkerCount;
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000373 }
374}
375
egdaniel3eee3832014-06-18 13:09:11 -0700376void GrDrawTarget::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000377 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000378 SkASSERT(fGpuTraceMarkerCount >= 1);
379 this->fActiveTraceMarkers.remove(*marker);
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000380 --fGpuTraceMarkerCount;
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000381 }
382}
383
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000384////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000385
bsalomon@google.com116ad842013-04-09 15:38:19 +0000386namespace {
387// returns true if the read/written rect intersects the src/dst and false if not.
388bool clip_srcrect_and_dstpoint(const GrSurface* dst,
389 const GrSurface* src,
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000390 const SkIRect& srcRect,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000391 const SkIPoint& dstPoint,
392 SkIRect* clippedSrcRect,
393 SkIPoint* clippedDstPoint) {
394 *clippedSrcRect = srcRect;
395 *clippedDstPoint = dstPoint;
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000396
bsalomon@google.com116ad842013-04-09 15:38:19 +0000397 // clip the left edge to src and dst bounds, adjusting dstPoint if necessary
398 if (clippedSrcRect->fLeft < 0) {
399 clippedDstPoint->fX -= clippedSrcRect->fLeft;
400 clippedSrcRect->fLeft = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000401 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000402 if (clippedDstPoint->fX < 0) {
403 clippedSrcRect->fLeft -= clippedDstPoint->fX;
404 clippedDstPoint->fX = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000405 }
406
bsalomon@google.com116ad842013-04-09 15:38:19 +0000407 // clip the top edge to src and dst bounds, adjusting dstPoint if necessary
408 if (clippedSrcRect->fTop < 0) {
409 clippedDstPoint->fY -= clippedSrcRect->fTop;
410 clippedSrcRect->fTop = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000411 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000412 if (clippedDstPoint->fY < 0) {
413 clippedSrcRect->fTop -= clippedDstPoint->fY;
414 clippedDstPoint->fY = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000415 }
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000416
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000417 // clip the right edge to the src and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000418 if (clippedSrcRect->fRight > src->width()) {
419 clippedSrcRect->fRight = src->width();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000420 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000421 if (clippedDstPoint->fX + clippedSrcRect->width() > dst->width()) {
422 clippedSrcRect->fRight = clippedSrcRect->fLeft + dst->width() - clippedDstPoint->fX;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000423 }
424
425 // clip the bottom edge to the src and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000426 if (clippedSrcRect->fBottom > src->height()) {
427 clippedSrcRect->fBottom = src->height();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000428 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000429 if (clippedDstPoint->fY + clippedSrcRect->height() > dst->height()) {
430 clippedSrcRect->fBottom = clippedSrcRect->fTop + dst->height() - clippedDstPoint->fY;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000431 }
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000432
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000433 // The above clipping steps may have inverted the rect if it didn't intersect either the src or
434 // dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000435 return !clippedSrcRect->isEmpty();
436}
437}
438
bsalomon6df86402015-06-01 10:41:49 -0700439void GrDrawTarget::copySurface(GrSurface* dst,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000440 GrSurface* src,
441 const SkIRect& srcRect,
442 const SkIPoint& dstPoint) {
bsalomon49f085d2014-09-05 13:34:00 -0700443 SkASSERT(dst);
444 SkASSERT(src);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000445
446 SkIRect clippedSrcRect;
447 SkIPoint clippedDstPoint;
448 // If the rect is outside the src or dst then we've already succeeded.
449 if (!clip_srcrect_and_dstpoint(dst,
450 src,
451 srcRect,
452 dstPoint,
453 &clippedSrcRect,
454 &clippedDstPoint)) {
bsalomon6df86402015-06-01 10:41:49 -0700455 return;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000456 }
457
bsalomon6df86402015-06-01 10:41:49 -0700458 this->onCopySurface(dst, src, clippedSrcRect, clippedDstPoint);
bsalomon@google.comeb851172013-04-15 13:51:00 +0000459}
460
bsalomon85cd78d2015-07-31 12:15:50 -0700461const GrPipeline* GrDrawTarget::setupPipeline(const PipelineInfo& pipelineInfo,
462 void* pipelineAddr) {
bsalomon47dfc362015-08-10 08:23:11 -0700463 return GrPipeline::CreateAt(pipelineAddr, *pipelineInfo.fPipelineBuilder,
464 pipelineInfo.fColorPOI,
465 pipelineInfo.fCoveragePOI,
466 *this->caps(),
467 *pipelineInfo.fScissor,
468 &pipelineInfo.fDstTexture);
egdaniele36914c2015-02-13 09:00:33 -0800469}
bsalomon47dfc362015-08-10 08:23:11 -0700470
egdaniele36914c2015-02-13 09:00:33 -0800471///////////////////////////////////////////////////////////////////////////////
472
joshualitt1c735482015-07-13 08:08:25 -0700473GrDrawTarget::PipelineInfo::PipelineInfo(const GrPipelineBuilder& pipelineBuilder,
egdaniele36914c2015-02-13 09:00:33 -0800474 GrScissorState* scissor,
475 const GrPrimitiveProcessor* primProc,
476 const SkRect* devBounds,
477 GrDrawTarget* target)
joshualitt1c735482015-07-13 08:08:25 -0700478 : fPipelineBuilder(&pipelineBuilder)
egdaniele36914c2015-02-13 09:00:33 -0800479 , fScissor(scissor) {
480 fColorPOI = fPipelineBuilder->colorProcInfo(primProc);
481 fCoveragePOI = fPipelineBuilder->coverageProcInfo(primProc);
482 if (!target->setupDstReadIfNecessary(*fPipelineBuilder, fColorPOI, fCoveragePOI,
bsalomon6a44c6a2015-05-26 09:49:05 -0700483 &fDstTexture, devBounds)) {
egdaniele36914c2015-02-13 09:00:33 -0800484 fPipelineBuilder = NULL;
485 }
486}
487
joshualitt1c735482015-07-13 08:08:25 -0700488GrDrawTarget::PipelineInfo::PipelineInfo(const GrPipelineBuilder& pipelineBuilder,
egdaniele36914c2015-02-13 09:00:33 -0800489 GrScissorState* scissor,
490 const GrBatch* batch,
491 const SkRect* devBounds,
492 GrDrawTarget* target)
joshualitt1c735482015-07-13 08:08:25 -0700493 : fPipelineBuilder(&pipelineBuilder)
egdaniele36914c2015-02-13 09:00:33 -0800494 , fScissor(scissor) {
495 fColorPOI = fPipelineBuilder->colorProcInfo(batch);
496 fCoveragePOI = fPipelineBuilder->coverageProcInfo(batch);
497 if (!target->setupDstReadIfNecessary(*fPipelineBuilder, fColorPOI, fCoveragePOI,
bsalomon6a44c6a2015-05-26 09:49:05 -0700498 &fDstTexture, devBounds)) {
egdaniele36914c2015-02-13 09:00:33 -0800499 fPipelineBuilder = NULL;
500 }
501}
502
bsalomon@google.combcce8922013-03-25 15:38:39 +0000503///////////////////////////////////////////////////////////////////////////////
bsalomon4061b122015-05-29 10:26:19 -0700504GrClipTarget::GrClipTarget(GrContext* context)
505 : INHERITED(context->getGpu(), context->resourceProvider())
506 , fContext(context) {
bsalomonedd77a12015-05-29 09:45:57 -0700507 fClipMaskManager.reset(SkNEW_ARGS(GrClipMaskManager, (this)));
508}
509
bsalomon@google.combcce8922013-03-25 15:38:39 +0000510
joshualitt1c735482015-07-13 08:08:25 -0700511bool GrClipTarget::setupClip(const GrPipelineBuilder& pipelineBuilder,
joshualitt4421a4c2015-07-13 09:36:41 -0700512 GrPipelineBuilder::AutoRestoreFragmentProcessorState* arfps,
egdaniel8dd688b2015-01-22 10:16:09 -0800513 GrPipelineBuilder::AutoRestoreStencil* ars,
joshualitt8059eb92014-12-29 15:10:07 -0800514 GrScissorState* scissorState,
515 const SkRect* devBounds) {
joshualitt1c735482015-07-13 08:08:25 -0700516 return fClipMaskManager->setupClipping(pipelineBuilder,
joshualitt4421a4c2015-07-13 09:36:41 -0700517 arfps,
joshualitt5e6ba212015-07-13 07:35:05 -0700518 ars,
joshualitt5e6ba212015-07-13 07:35:05 -0700519 scissorState,
520 devBounds);
joshualitt2c93efe2014-11-06 12:57:13 -0800521}
bsalomonedd77a12015-05-29 09:45:57 -0700522
523void GrClipTarget::purgeResources() {
524 // The clip mask manager can rebuild all its clip masks so just
525 // get rid of them all.
526 fClipMaskManager->purgeResources();
527};