blob: ba06c66e8b063c542cb523d53174204ce9d8e9d2 [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
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;
bsalomon6be6f7c2015-02-26 13:05:21 -0800134 GrPipelineBuilder::AutoRestoreFragmentProcessors arfp;
joshualitt4d8da812015-01-28 12:53:54 -0800135 GrPipelineBuilder::AutoRestoreStencil ars;
joshualitt5e6ba212015-07-13 07:35:05 -0700136 GrPipelineBuilder::AutoRestoreProcessorDataManager arpdm;
137 if (!this->setupClip(pipelineBuilder, &arfp, &ars, &arpdm, &scissorState, &batch->bounds())) {
joshualitt4d8da812015-01-28 12:53:54 -0800138 return;
139 }
140
joshualitt99c7c072015-05-01 13:43:30 -0700141 // Batch bounds are tight, so for dev copies
142 // TODO move this into setupDstReadIfNecessary when paths are in batch
143 SkRect bounds = batch->bounds();
144 bounds.outset(0.5f, 0.5f);
145
146 GrDrawTarget::PipelineInfo pipelineInfo(pipelineBuilder, &scissorState, batch, &bounds,
147 this);
egdaniele36914c2015-02-13 09:00:33 -0800148 if (pipelineInfo.mustSkipDraw()) {
149 return;
150 }
151
152 this->onDrawBatch(batch, pipelineInfo);
joshualitt4d8da812015-01-28 12:53:54 -0800153}
154
joshualitt2c93efe2014-11-06 12:57:13 -0800155static const GrStencilSettings& winding_path_stencil_settings() {
156 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
157 kIncClamp_StencilOp,
158 kIncClamp_StencilOp,
159 kAlwaysIfInClip_StencilFunc,
160 0xFFFF, 0xFFFF, 0xFFFF);
161 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
162}
163
164static const GrStencilSettings& even_odd_path_stencil_settings() {
165 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
166 kInvert_StencilOp,
167 kInvert_StencilOp,
168 kAlwaysIfInClip_StencilFunc,
169 0xFFFF, 0xFFFF, 0xFFFF);
170 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
171}
172
173void GrDrawTarget::getPathStencilSettingsForFilltype(GrPathRendering::FillType fill,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700174 const GrStencilAttachment* sb,
joshualitt2c93efe2014-11-06 12:57:13 -0800175 GrStencilSettings* outStencilSettings) {
176
177 switch (fill) {
178 default:
179 SkFAIL("Unexpected path fill.");
180 case GrPathRendering::kWinding_FillType:
181 *outStencilSettings = winding_path_stencil_settings();
182 break;
183 case GrPathRendering::kEvenOdd_FillType:
184 *outStencilSettings = even_odd_path_stencil_settings();
185 break;
186 }
joshualitt9853cce2014-11-17 14:22:48 -0800187 this->clipMaskManager()->adjustPathStencilParams(sb, outStencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800188}
189
joshualitt1c735482015-07-13 08:08:25 -0700190void GrDrawTarget::stencilPath(const GrPipelineBuilder& pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800191 const GrPathProcessor* pathProc,
joshualitt9853cce2014-11-17 14:22:48 -0800192 const GrPath* path,
193 GrPathRendering::FillType fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000194 // TODO: extract portions of checkDraw that are relevant to path stenciling.
bsalomon49f085d2014-09-05 13:34:00 -0700195 SkASSERT(path);
jvanverthe9c0fc62015-04-29 11:18:05 -0700196 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
joshualitt2c93efe2014-11-06 12:57:13 -0800197
198 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800199 GrScissorState scissorState;
bsalomon6be6f7c2015-02-26 13:05:21 -0800200 GrPipelineBuilder::AutoRestoreFragmentProcessors arfp;
egdaniel8dd688b2015-01-22 10:16:09 -0800201 GrPipelineBuilder::AutoRestoreStencil ars;
joshualitt5e6ba212015-07-13 07:35:05 -0700202 GrPipelineBuilder::AutoRestoreProcessorDataManager arpdm;
203 if (!this->setupClip(pipelineBuilder, &arfp, &ars, &arpdm, &scissorState, NULL)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800204 return;
205 }
206
207 // set stencil settings for path
208 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700209 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdaniel8dc7c3a2015-04-16 11:22:42 -0700210 GrStencilAttachment* sb = rt->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800211 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800212
joshualitt1c735482015-07-13 08:08:25 -0700213 this->onStencilPath(pipelineBuilder, pathProc, path, scissorState, stencilSettings);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000214}
215
joshualitt1c735482015-07-13 08:08:25 -0700216void GrDrawTarget::drawPath(const GrPipelineBuilder& pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800217 const GrPathProcessor* pathProc,
joshualitt9853cce2014-11-17 14:22:48 -0800218 const GrPath* path,
219 GrPathRendering::FillType fill) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000220 // TODO: extract portions of checkDraw that are relevant to path rendering.
bsalomon49f085d2014-09-05 13:34:00 -0700221 SkASSERT(path);
jvanverthe9c0fc62015-04-29 11:18:05 -0700222 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000223
joshualitt92e496f2014-10-31 13:56:50 -0700224 SkRect devBounds = path->getBounds();
joshualitt8059eb92014-12-29 15:10:07 -0800225 pathProc->viewMatrix().mapRect(&devBounds);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000226
joshualitt2c93efe2014-11-06 12:57:13 -0800227 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800228 GrScissorState scissorState;
bsalomon6be6f7c2015-02-26 13:05:21 -0800229 GrPipelineBuilder::AutoRestoreFragmentProcessors arfp;
egdaniel8dd688b2015-01-22 10:16:09 -0800230 GrPipelineBuilder::AutoRestoreStencil ars;
joshualitt5e6ba212015-07-13 07:35:05 -0700231 GrPipelineBuilder::AutoRestoreProcessorDataManager arpdm;
232 if (!this->setupClip(pipelineBuilder, &arfp, &ars, &arpdm, &scissorState, &devBounds)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800233 return;
234 }
235
236 // set stencil settings for path
237 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700238 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdaniel8dc7c3a2015-04-16 11:22:42 -0700239 GrStencilAttachment* sb = rt->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800240 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800241
egdaniele36914c2015-02-13 09:00:33 -0800242 GrDrawTarget::PipelineInfo pipelineInfo(pipelineBuilder, &scissorState, pathProc, &devBounds,
243 this);
244 if (pipelineInfo.mustSkipDraw()) {
245 return;
246 }
247
248 this->onDrawPath(pathProc, path, stencilSettings, pipelineInfo);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000249}
250
joshualitt1c735482015-07-13 08:08:25 -0700251void GrDrawTarget::drawPaths(const GrPipelineBuilder& pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800252 const GrPathProcessor* pathProc,
joshualitt9853cce2014-11-17 14:22:48 -0800253 const GrPathRange* pathRange,
cdalton55b24af2014-11-25 11:00:56 -0800254 const void* indices,
255 PathIndexType indexType,
256 const float transformValues[],
257 PathTransformType transformType,
joshualitt9853cce2014-11-17 14:22:48 -0800258 int count,
joshualitt92e496f2014-10-31 13:56:50 -0700259 GrPathRendering::FillType fill) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700260 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
bsalomon49f085d2014-09-05 13:34:00 -0700261 SkASSERT(pathRange);
262 SkASSERT(indices);
cdalton55b24af2014-11-25 11:00:56 -0800263 SkASSERT(0 == reinterpret_cast<long>(indices) % GrPathRange::PathIndexSizeInBytes(indexType));
264 SkASSERT(transformValues);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000265
joshualitt2c93efe2014-11-06 12:57:13 -0800266 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800267 GrScissorState scissorState;
bsalomon6be6f7c2015-02-26 13:05:21 -0800268 GrPipelineBuilder::AutoRestoreFragmentProcessors arfp;
egdaniel8dd688b2015-01-22 10:16:09 -0800269 GrPipelineBuilder::AutoRestoreStencil ars;
joshualitt5e6ba212015-07-13 07:35:05 -0700270 GrPipelineBuilder::AutoRestoreProcessorDataManager arpdm;
271 if (!this->setupClip(pipelineBuilder, &arfp, &ars, &arpdm, &scissorState, NULL)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800272 return;
273 }
274
275 // set stencil settings for path
276 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700277 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdaniel8dc7c3a2015-04-16 11:22:42 -0700278 GrStencilAttachment* sb = rt->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800279 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800280
bsalomon50785a32015-02-06 07:02:37 -0800281 // Don't compute a bounding box for dst copy texture, we'll opt
cdaltonb85a0aa2014-07-21 15:32:44 -0700282 // instead for it to just copy the entire dst. Realistically this is a moot
283 // point, because any context that supports NV_path_rendering will also
284 // support NV_blend_equation_advanced.
egdaniele36914c2015-02-13 09:00:33 -0800285 GrDrawTarget::PipelineInfo pipelineInfo(pipelineBuilder, &scissorState, pathProc, NULL, this);
286 if (pipelineInfo.mustSkipDraw()) {
287 return;
288 }
289
290 this->onDrawPaths(pathProc, pathRange, indices, indexType, transformValues,
291 transformType, count, stencilSettings, pipelineInfo);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000292}
293
joshualitt1c735482015-07-13 08:08:25 -0700294void GrDrawTarget::drawBWRect(const GrPipelineBuilder& pipelineBuilder,
295 GrColor color,
296 const SkMatrix& viewMatrix,
297 const SkRect& rect,
298 const SkRect* localRect,
299 const SkMatrix* localMatrix) {
joshualittad17cfc2015-05-05 10:45:57 -0700300 SkAutoTUnref<GrBatch> batch(GrRectBatch::Create(color, viewMatrix, rect, localRect,
301 localMatrix));
302 this->drawBatch(pipelineBuilder, batch);
303}
304
joshualitt1c735482015-07-13 08:08:25 -0700305void GrDrawTarget::drawAARect(const GrPipelineBuilder& pipelineBuilder,
robertphillipsea461502015-05-26 11:38:03 -0700306 GrColor color,
307 const SkMatrix& viewMatrix,
308 const SkRect& rect,
309 const SkRect& devRect) {
310 GrAARectRenderer::FillAARect(this, pipelineBuilder, color, viewMatrix, rect, devRect);
311}
312
joshualitt9853cce2014-11-17 14:22:48 -0800313void GrDrawTarget::clear(const SkIRect* rect,
314 GrColor color,
315 bool canIgnoreRect,
bsalomon63b21962014-11-05 07:05:34 -0800316 GrRenderTarget* renderTarget) {
317 if (fCaps->useDrawInsteadOfClear()) {
318 // This works around a driver bug with clear by drawing a rect instead.
319 // The driver will ignore a clear if it is the only thing rendered to a
320 // target before the target is read.
321 SkIRect rtRect = SkIRect::MakeWH(renderTarget->width(), renderTarget->height());
322 if (NULL == rect || canIgnoreRect || rect->contains(rtRect)) {
323 rect = &rtRect;
324 // We first issue a discard() since that may help tilers.
325 this->discard(renderTarget);
326 }
bsalomon63b21962014-11-05 07:05:34 -0800327
egdaniel8dd688b2015-01-22 10:16:09 -0800328 GrPipelineBuilder pipelineBuilder;
329 pipelineBuilder.setRenderTarget(renderTarget);
joshualitt9853cce2014-11-17 14:22:48 -0800330
joshualitt1c735482015-07-13 08:08:25 -0700331 this->drawSimpleRect(pipelineBuilder, color, SkMatrix::I(), *rect);
bsalomon63b21962014-11-05 07:05:34 -0800332 } else {
333 this->onClear(rect, color, canIgnoreRect, renderTarget);
334 }
335}
336
egdaniel3eee3832014-06-18 13:09:11 -0700337typedef GrTraceMarkerSet::Iter TMIter;
338void GrDrawTarget::saveActiveTraceMarkers() {
339 if (this->caps()->gpuTracingSupport()) {
340 SkASSERT(0 == fStoredTraceMarkers.count());
341 fStoredTraceMarkers.addSet(fActiveTraceMarkers);
342 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
343 this->removeGpuTraceMarker(&(*iter));
344 }
345 }
346}
347
348void GrDrawTarget::restoreActiveTraceMarkers() {
349 if (this->caps()->gpuTracingSupport()) {
350 SkASSERT(0 == fActiveTraceMarkers.count());
351 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
352 this->addGpuTraceMarker(&(*iter));
353 }
354 for (TMIter iter = fActiveTraceMarkers.begin(); iter != fActiveTraceMarkers.end(); ++iter) {
355 this->fStoredTraceMarkers.remove(*iter);
356 }
357 }
358}
359
360void GrDrawTarget::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000361 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000362 SkASSERT(fGpuTraceMarkerCount >= 0);
363 this->fActiveTraceMarkers.add(*marker);
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000364 ++fGpuTraceMarkerCount;
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000365 }
366}
367
egdaniel3eee3832014-06-18 13:09:11 -0700368void GrDrawTarget::removeGpuTraceMarker(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 >= 1);
371 this->fActiveTraceMarkers.remove(*marker);
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000372 --fGpuTraceMarkerCount;
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000373 }
374}
375
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000376////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000377
bsalomon@google.com116ad842013-04-09 15:38:19 +0000378namespace {
379// returns true if the read/written rect intersects the src/dst and false if not.
380bool clip_srcrect_and_dstpoint(const GrSurface* dst,
381 const GrSurface* src,
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000382 const SkIRect& srcRect,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000383 const SkIPoint& dstPoint,
384 SkIRect* clippedSrcRect,
385 SkIPoint* clippedDstPoint) {
386 *clippedSrcRect = srcRect;
387 *clippedDstPoint = dstPoint;
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000388
bsalomon@google.com116ad842013-04-09 15:38:19 +0000389 // clip the left edge to src and dst bounds, adjusting dstPoint if necessary
390 if (clippedSrcRect->fLeft < 0) {
391 clippedDstPoint->fX -= clippedSrcRect->fLeft;
392 clippedSrcRect->fLeft = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000393 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000394 if (clippedDstPoint->fX < 0) {
395 clippedSrcRect->fLeft -= clippedDstPoint->fX;
396 clippedDstPoint->fX = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000397 }
398
bsalomon@google.com116ad842013-04-09 15:38:19 +0000399 // clip the top edge to src and dst bounds, adjusting dstPoint if necessary
400 if (clippedSrcRect->fTop < 0) {
401 clippedDstPoint->fY -= clippedSrcRect->fTop;
402 clippedSrcRect->fTop = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000403 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000404 if (clippedDstPoint->fY < 0) {
405 clippedSrcRect->fTop -= clippedDstPoint->fY;
406 clippedDstPoint->fY = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000407 }
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000408
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000409 // clip the right edge to the src and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000410 if (clippedSrcRect->fRight > src->width()) {
411 clippedSrcRect->fRight = src->width();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000412 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000413 if (clippedDstPoint->fX + clippedSrcRect->width() > dst->width()) {
414 clippedSrcRect->fRight = clippedSrcRect->fLeft + dst->width() - clippedDstPoint->fX;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000415 }
416
417 // clip the bottom edge to the src and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000418 if (clippedSrcRect->fBottom > src->height()) {
419 clippedSrcRect->fBottom = src->height();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000420 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000421 if (clippedDstPoint->fY + clippedSrcRect->height() > dst->height()) {
422 clippedSrcRect->fBottom = clippedSrcRect->fTop + dst->height() - clippedDstPoint->fY;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000423 }
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000424
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000425 // The above clipping steps may have inverted the rect if it didn't intersect either the src or
426 // dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000427 return !clippedSrcRect->isEmpty();
428}
429}
430
bsalomon6df86402015-06-01 10:41:49 -0700431void GrDrawTarget::copySurface(GrSurface* dst,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000432 GrSurface* src,
433 const SkIRect& srcRect,
434 const SkIPoint& dstPoint) {
bsalomon49f085d2014-09-05 13:34:00 -0700435 SkASSERT(dst);
436 SkASSERT(src);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000437
438 SkIRect clippedSrcRect;
439 SkIPoint clippedDstPoint;
440 // If the rect is outside the src or dst then we've already succeeded.
441 if (!clip_srcrect_and_dstpoint(dst,
442 src,
443 srcRect,
444 dstPoint,
445 &clippedSrcRect,
446 &clippedDstPoint)) {
bsalomon6df86402015-06-01 10:41:49 -0700447 return;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000448 }
449
bsalomon6df86402015-06-01 10:41:49 -0700450 this->onCopySurface(dst, src, clippedSrcRect, clippedDstPoint);
bsalomon@google.comeb851172013-04-15 13:51:00 +0000451}
452
egdaniele36914c2015-02-13 09:00:33 -0800453void GrDrawTarget::setupPipeline(const PipelineInfo& pipelineInfo,
454 GrPipeline* pipeline) {
455 SkNEW_PLACEMENT_ARGS(pipeline, GrPipeline, (*pipelineInfo.fPipelineBuilder,
456 pipelineInfo.fColorPOI,
457 pipelineInfo.fCoveragePOI,
458 *this->caps(),
459 *pipelineInfo.fScissor,
bsalomon6a44c6a2015-05-26 09:49:05 -0700460 &pipelineInfo.fDstTexture));
egdaniele36914c2015-02-13 09:00:33 -0800461}
462///////////////////////////////////////////////////////////////////////////////
463
joshualitt1c735482015-07-13 08:08:25 -0700464GrDrawTarget::PipelineInfo::PipelineInfo(const GrPipelineBuilder& pipelineBuilder,
egdaniele36914c2015-02-13 09:00:33 -0800465 GrScissorState* scissor,
466 const GrPrimitiveProcessor* primProc,
467 const SkRect* devBounds,
468 GrDrawTarget* target)
joshualitt1c735482015-07-13 08:08:25 -0700469 : fPipelineBuilder(&pipelineBuilder)
egdaniele36914c2015-02-13 09:00:33 -0800470 , fScissor(scissor) {
471 fColorPOI = fPipelineBuilder->colorProcInfo(primProc);
472 fCoveragePOI = fPipelineBuilder->coverageProcInfo(primProc);
473 if (!target->setupDstReadIfNecessary(*fPipelineBuilder, fColorPOI, fCoveragePOI,
bsalomon6a44c6a2015-05-26 09:49:05 -0700474 &fDstTexture, devBounds)) {
egdaniele36914c2015-02-13 09:00:33 -0800475 fPipelineBuilder = NULL;
476 }
477}
478
joshualitt1c735482015-07-13 08:08:25 -0700479GrDrawTarget::PipelineInfo::PipelineInfo(const GrPipelineBuilder& pipelineBuilder,
egdaniele36914c2015-02-13 09:00:33 -0800480 GrScissorState* scissor,
481 const GrBatch* batch,
482 const SkRect* devBounds,
483 GrDrawTarget* target)
joshualitt1c735482015-07-13 08:08:25 -0700484 : fPipelineBuilder(&pipelineBuilder)
egdaniele36914c2015-02-13 09:00:33 -0800485 , fScissor(scissor) {
486 fColorPOI = fPipelineBuilder->colorProcInfo(batch);
487 fCoveragePOI = fPipelineBuilder->coverageProcInfo(batch);
488 if (!target->setupDstReadIfNecessary(*fPipelineBuilder, fColorPOI, fCoveragePOI,
bsalomon6a44c6a2015-05-26 09:49:05 -0700489 &fDstTexture, devBounds)) {
egdaniele36914c2015-02-13 09:00:33 -0800490 fPipelineBuilder = NULL;
491 }
492}
493
bsalomon@google.combcce8922013-03-25 15:38:39 +0000494///////////////////////////////////////////////////////////////////////////////
bsalomon4061b122015-05-29 10:26:19 -0700495GrClipTarget::GrClipTarget(GrContext* context)
496 : INHERITED(context->getGpu(), context->resourceProvider())
497 , fContext(context) {
bsalomonedd77a12015-05-29 09:45:57 -0700498 fClipMaskManager.reset(SkNEW_ARGS(GrClipMaskManager, (this)));
499}
500
bsalomon@google.combcce8922013-03-25 15:38:39 +0000501
joshualitt1c735482015-07-13 08:08:25 -0700502bool GrClipTarget::setupClip(const GrPipelineBuilder& pipelineBuilder,
bsalomon6be6f7c2015-02-26 13:05:21 -0800503 GrPipelineBuilder::AutoRestoreFragmentProcessors* arfp,
egdaniel8dd688b2015-01-22 10:16:09 -0800504 GrPipelineBuilder::AutoRestoreStencil* ars,
joshualitt5e6ba212015-07-13 07:35:05 -0700505 GrPipelineBuilder::AutoRestoreProcessorDataManager* arpdm,
joshualitt8059eb92014-12-29 15:10:07 -0800506 GrScissorState* scissorState,
507 const SkRect* devBounds) {
joshualitt1c735482015-07-13 08:08:25 -0700508 return fClipMaskManager->setupClipping(pipelineBuilder,
joshualitt5e6ba212015-07-13 07:35:05 -0700509 arfp,
510 ars,
511 arpdm,
512 scissorState,
513 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};