blob: 2171f1e93473ba168acede74efad9600d68029c8 [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) {
egdaniel51c8d402015-08-06 10:54:13 -0700312 SkIRect rtRect = SkIRect::MakeWH(renderTarget->width(), renderTarget->height());
313 SkIRect clippedRect;
314 if (!rect ||
315 (canIgnoreRect && this->caps()->fullClearIsFree()) ||
316 rect->contains(rtRect)) {
317 rect = &rtRect;
318 } else {
319 clippedRect = *rect;
320 if (!clippedRect.intersect(rtRect)) {
321 return;
322 }
323 rect = &clippedRect;
324 }
325
bsalomon63b21962014-11-05 07:05:34 -0800326 if (fCaps->useDrawInsteadOfClear()) {
327 // This works around a driver bug with clear by drawing a rect instead.
328 // The driver will ignore a clear if it is the only thing rendered to a
329 // target before the target is read.
egdaniel51c8d402015-08-06 10:54:13 -0700330 if (rect == &rtRect) {
bsalomon63b21962014-11-05 07:05:34 -0800331 this->discard(renderTarget);
332 }
bsalomon63b21962014-11-05 07:05:34 -0800333
egdaniel8dd688b2015-01-22 10:16:09 -0800334 GrPipelineBuilder pipelineBuilder;
335 pipelineBuilder.setRenderTarget(renderTarget);
joshualitt9853cce2014-11-17 14:22:48 -0800336
joshualitt1c735482015-07-13 08:08:25 -0700337 this->drawSimpleRect(pipelineBuilder, color, SkMatrix::I(), *rect);
bsalomon63b21962014-11-05 07:05:34 -0800338 } else {
egdaniel51c8d402015-08-06 10:54:13 -0700339 this->onClear(*rect, color, renderTarget);
bsalomon63b21962014-11-05 07:05:34 -0800340 }
341}
342
egdaniel3eee3832014-06-18 13:09:11 -0700343typedef GrTraceMarkerSet::Iter TMIter;
344void GrDrawTarget::saveActiveTraceMarkers() {
345 if (this->caps()->gpuTracingSupport()) {
346 SkASSERT(0 == fStoredTraceMarkers.count());
347 fStoredTraceMarkers.addSet(fActiveTraceMarkers);
348 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
349 this->removeGpuTraceMarker(&(*iter));
350 }
351 }
352}
353
354void GrDrawTarget::restoreActiveTraceMarkers() {
355 if (this->caps()->gpuTracingSupport()) {
356 SkASSERT(0 == fActiveTraceMarkers.count());
357 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
358 this->addGpuTraceMarker(&(*iter));
359 }
360 for (TMIter iter = fActiveTraceMarkers.begin(); iter != fActiveTraceMarkers.end(); ++iter) {
361 this->fStoredTraceMarkers.remove(*iter);
362 }
363 }
364}
365
366void GrDrawTarget::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000367 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000368 SkASSERT(fGpuTraceMarkerCount >= 0);
369 this->fActiveTraceMarkers.add(*marker);
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000370 ++fGpuTraceMarkerCount;
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000371 }
372}
373
egdaniel3eee3832014-06-18 13:09:11 -0700374void GrDrawTarget::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000375 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000376 SkASSERT(fGpuTraceMarkerCount >= 1);
377 this->fActiveTraceMarkers.remove(*marker);
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000378 --fGpuTraceMarkerCount;
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000379 }
380}
381
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000382////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000383
bsalomon@google.com116ad842013-04-09 15:38:19 +0000384namespace {
385// returns true if the read/written rect intersects the src/dst and false if not.
386bool clip_srcrect_and_dstpoint(const GrSurface* dst,
387 const GrSurface* src,
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000388 const SkIRect& srcRect,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000389 const SkIPoint& dstPoint,
390 SkIRect* clippedSrcRect,
391 SkIPoint* clippedDstPoint) {
392 *clippedSrcRect = srcRect;
393 *clippedDstPoint = dstPoint;
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000394
bsalomon@google.com116ad842013-04-09 15:38:19 +0000395 // clip the left edge to src and dst bounds, adjusting dstPoint if necessary
396 if (clippedSrcRect->fLeft < 0) {
397 clippedDstPoint->fX -= clippedSrcRect->fLeft;
398 clippedSrcRect->fLeft = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000399 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000400 if (clippedDstPoint->fX < 0) {
401 clippedSrcRect->fLeft -= clippedDstPoint->fX;
402 clippedDstPoint->fX = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000403 }
404
bsalomon@google.com116ad842013-04-09 15:38:19 +0000405 // clip the top edge to src and dst bounds, adjusting dstPoint if necessary
406 if (clippedSrcRect->fTop < 0) {
407 clippedDstPoint->fY -= clippedSrcRect->fTop;
408 clippedSrcRect->fTop = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000409 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000410 if (clippedDstPoint->fY < 0) {
411 clippedSrcRect->fTop -= clippedDstPoint->fY;
412 clippedDstPoint->fY = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000413 }
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000414
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000415 // clip the right edge to the src and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000416 if (clippedSrcRect->fRight > src->width()) {
417 clippedSrcRect->fRight = src->width();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000418 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000419 if (clippedDstPoint->fX + clippedSrcRect->width() > dst->width()) {
420 clippedSrcRect->fRight = clippedSrcRect->fLeft + dst->width() - clippedDstPoint->fX;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000421 }
422
423 // clip the bottom edge to the src and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000424 if (clippedSrcRect->fBottom > src->height()) {
425 clippedSrcRect->fBottom = src->height();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000426 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000427 if (clippedDstPoint->fY + clippedSrcRect->height() > dst->height()) {
428 clippedSrcRect->fBottom = clippedSrcRect->fTop + dst->height() - clippedDstPoint->fY;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000429 }
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000430
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000431 // The above clipping steps may have inverted the rect if it didn't intersect either the src or
432 // dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000433 return !clippedSrcRect->isEmpty();
434}
435}
436
bsalomon6df86402015-06-01 10:41:49 -0700437void GrDrawTarget::copySurface(GrSurface* dst,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000438 GrSurface* src,
439 const SkIRect& srcRect,
440 const SkIPoint& dstPoint) {
bsalomon49f085d2014-09-05 13:34:00 -0700441 SkASSERT(dst);
442 SkASSERT(src);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000443
444 SkIRect clippedSrcRect;
445 SkIPoint clippedDstPoint;
446 // If the rect is outside the src or dst then we've already succeeded.
447 if (!clip_srcrect_and_dstpoint(dst,
448 src,
449 srcRect,
450 dstPoint,
451 &clippedSrcRect,
452 &clippedDstPoint)) {
bsalomon6df86402015-06-01 10:41:49 -0700453 return;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000454 }
455
bsalomon6df86402015-06-01 10:41:49 -0700456 this->onCopySurface(dst, src, clippedSrcRect, clippedDstPoint);
bsalomon@google.comeb851172013-04-15 13:51:00 +0000457}
458
bsalomon85cd78d2015-07-31 12:15:50 -0700459const GrPipeline* GrDrawTarget::setupPipeline(const PipelineInfo& pipelineInfo,
460 void* pipelineAddr) {
461 return SkNEW_PLACEMENT_ARGS(pipelineAddr, GrPipeline, (*pipelineInfo.fPipelineBuilder,
462 pipelineInfo.fColorPOI,
463 pipelineInfo.fCoveragePOI,
464 *this->caps(),
465 *pipelineInfo.fScissor,
466 &pipelineInfo.fDstTexture));
egdaniele36914c2015-02-13 09:00:33 -0800467}
468///////////////////////////////////////////////////////////////////////////////
469
joshualitt1c735482015-07-13 08:08:25 -0700470GrDrawTarget::PipelineInfo::PipelineInfo(const GrPipelineBuilder& pipelineBuilder,
egdaniele36914c2015-02-13 09:00:33 -0800471 GrScissorState* scissor,
472 const GrPrimitiveProcessor* primProc,
473 const SkRect* devBounds,
474 GrDrawTarget* target)
joshualitt1c735482015-07-13 08:08:25 -0700475 : fPipelineBuilder(&pipelineBuilder)
egdaniele36914c2015-02-13 09:00:33 -0800476 , fScissor(scissor) {
477 fColorPOI = fPipelineBuilder->colorProcInfo(primProc);
478 fCoveragePOI = fPipelineBuilder->coverageProcInfo(primProc);
479 if (!target->setupDstReadIfNecessary(*fPipelineBuilder, fColorPOI, fCoveragePOI,
bsalomon6a44c6a2015-05-26 09:49:05 -0700480 &fDstTexture, devBounds)) {
egdaniele36914c2015-02-13 09:00:33 -0800481 fPipelineBuilder = NULL;
482 }
483}
484
joshualitt1c735482015-07-13 08:08:25 -0700485GrDrawTarget::PipelineInfo::PipelineInfo(const GrPipelineBuilder& pipelineBuilder,
egdaniele36914c2015-02-13 09:00:33 -0800486 GrScissorState* scissor,
487 const GrBatch* batch,
488 const SkRect* devBounds,
489 GrDrawTarget* target)
joshualitt1c735482015-07-13 08:08:25 -0700490 : fPipelineBuilder(&pipelineBuilder)
egdaniele36914c2015-02-13 09:00:33 -0800491 , fScissor(scissor) {
492 fColorPOI = fPipelineBuilder->colorProcInfo(batch);
493 fCoveragePOI = fPipelineBuilder->coverageProcInfo(batch);
494 if (!target->setupDstReadIfNecessary(*fPipelineBuilder, fColorPOI, fCoveragePOI,
bsalomon6a44c6a2015-05-26 09:49:05 -0700495 &fDstTexture, devBounds)) {
egdaniele36914c2015-02-13 09:00:33 -0800496 fPipelineBuilder = NULL;
497 }
498}
499
bsalomon@google.combcce8922013-03-25 15:38:39 +0000500///////////////////////////////////////////////////////////////////////////////
bsalomon4061b122015-05-29 10:26:19 -0700501GrClipTarget::GrClipTarget(GrContext* context)
502 : INHERITED(context->getGpu(), context->resourceProvider())
503 , fContext(context) {
bsalomonedd77a12015-05-29 09:45:57 -0700504 fClipMaskManager.reset(SkNEW_ARGS(GrClipMaskManager, (this)));
505}
506
bsalomon@google.combcce8922013-03-25 15:38:39 +0000507
joshualitt1c735482015-07-13 08:08:25 -0700508bool GrClipTarget::setupClip(const GrPipelineBuilder& pipelineBuilder,
joshualitt4421a4c2015-07-13 09:36:41 -0700509 GrPipelineBuilder::AutoRestoreFragmentProcessorState* arfps,
egdaniel8dd688b2015-01-22 10:16:09 -0800510 GrPipelineBuilder::AutoRestoreStencil* ars,
joshualitt8059eb92014-12-29 15:10:07 -0800511 GrScissorState* scissorState,
512 const SkRect* devBounds) {
joshualitt1c735482015-07-13 08:08:25 -0700513 return fClipMaskManager->setupClipping(pipelineBuilder,
joshualitt4421a4c2015-07-13 09:36:41 -0700514 arfps,
joshualitt5e6ba212015-07-13 07:35:05 -0700515 ars,
joshualitt5e6ba212015-07-13 07:35:05 -0700516 scissorState,
517 devBounds);
joshualitt2c93efe2014-11-06 12:57:13 -0800518}
bsalomonedd77a12015-05-29 09:45:57 -0700519
520void GrClipTarget::purgeResources() {
521 // The clip mask manager can rebuild all its clip masks so just
522 // get rid of them all.
523 fClipMaskManager->purgeResources();
524};