blob: 7d5a4f992fa5d904324321e8361aacf0af864432 [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"
bsalomon@google.com26e18b52013-03-29 19:22:36 +000014#include "GrContext.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"
bsalomon6bc1b5f2015-02-23 09:06:38 -080020#include "GrRenderTargetPriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070021#include "GrSurfacePriv.h"
bsalomon62c447d2014-08-08 08:08:50 -070022#include "GrTemplates.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
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000030#define DEBUG_INVAL_BUFFER 0xdeadcafe
31#define DEBUG_INVAL_START_IDX -1
32
robertphillipse40d3972015-05-07 09:51:43 -070033GrDrawTarget::GrDrawTarget(GrContext* context)
joshualitt44701df2015-02-23 14:44:57 -080034 : fContext(context)
bsalomona73239a2015-04-28 13:35:17 -070035 , fCaps(SkRef(context->getGpu()->caps()))
36 , fGpuTraceMarkerCount(0)
bsalomona73239a2015-04-28 13:35:17 -070037 , fFlushing(false) {
bsalomon49f085d2014-09-05 13:34:00 -070038 SkASSERT(context);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000039}
40
41////////////////////////////////////////////////////////////////////////////////
42
bsalomon50785a32015-02-06 07:02:37 -080043bool GrDrawTarget::setupDstReadIfNecessary(const GrPipelineBuilder& pipelineBuilder,
egdaniele36914c2015-02-13 09:00:33 -080044 const GrProcOptInfo& colorPOI,
45 const GrProcOptInfo& coveragePOI,
bsalomon6a44c6a2015-05-26 09:49:05 -070046 GrXferProcessor::DstTexture* dstTexture,
joshualitt9853cce2014-11-17 14:22:48 -080047 const SkRect* drawBounds) {
bsalomon6a44c6a2015-05-26 09:49:05 -070048 if (!pipelineBuilder.willXPNeedDstTexture(*this->caps(), colorPOI, coveragePOI)) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +000049 return true;
50 }
cdalton9954bc32015-04-29 14:17:00 -070051
bsalomon50785a32015-02-06 07:02:37 -080052 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
cdalton9954bc32015-04-29 14:17:00 -070053
54 if (this->caps()->textureBarrierSupport()) {
55 if (GrTexture* rtTex = rt->asTexture()) {
bsalomondc47ff72015-05-26 12:16:59 -070056 // The render target is a texture, so we can read from it directly in the shader. The XP
cdalton9954bc32015-04-29 14:17:00 -070057 // will be responsible to detect this situation and request a texture barrier.
bsalomon6a44c6a2015-05-26 09:49:05 -070058 dstTexture->setTexture(rtTex);
59 dstTexture->setOffset(0, 0);
cdalton9954bc32015-04-29 14:17:00 -070060 return true;
61 }
62 }
63
64 SkIRect copyRect;
joshualitt44701df2015-02-23 14:44:57 -080065 pipelineBuilder.clip().getConservativeBounds(rt, &copyRect);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000066
bsalomon49f085d2014-09-05 13:34:00 -070067 if (drawBounds) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000068 SkIRect drawIBounds;
69 drawBounds->roundOut(&drawIBounds);
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +000070 if (!copyRect.intersect(drawIBounds)) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000071#ifdef SK_DEBUG
bsalomon682c2692015-05-22 14:01:46 -070072 GrCapsDebugf(fCaps, "Missed an early reject. "
73 "Bailing on draw from setupDstReadIfNecessary.\n");
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +000074#endif
75 return false;
76 }
77 } else {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000078#ifdef SK_DEBUG
tfarina38406c82014-10-31 07:11:12 -070079 //SkDebugf("No dev bounds when dst copy is made.\n");
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +000080#endif
81 }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +000082
commit-bot@chromium.org63150af2013-04-11 22:00:22 +000083 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
84 // have per-sample dst values by making the copy multisampled.
bsalomonf2703d82014-10-28 14:33:06 -070085 GrSurfaceDesc desc;
bsalomona73239a2015-04-28 13:35:17 -070086 if (!this->getGpu()->initCopySurfaceDstDesc(rt, &desc)) {
87 desc.fOrigin = kDefault_GrSurfaceOrigin;
88 desc.fFlags = kRenderTarget_GrSurfaceFlag;
89 desc.fConfig = rt->config();
90 }
91
92
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +000093 desc.fWidth = copyRect.width();
94 desc.fHeight = copyRect.height();
bsalomon@google.com26e18b52013-03-29 19:22:36 +000095
bsalomond309e7a2015-04-30 14:18:54 -070096 SkAutoTUnref<GrTexture> copy(fContext->textureProvider()->refScratchTexture(desc,
97 GrTextureProvider::kApprox_ScratchTexMatch));
bsalomon@google.com26e18b52013-03-29 19:22:36 +000098
bsalomone3059732014-10-14 11:47:22 -070099 if (!copy) {
tfarina38406c82014-10-31 07:11:12 -0700100 SkDebugf("Failed to create temporary copy of destination texture.\n");
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000101 return false;
102 }
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000103 SkIPoint dstPoint = {0, 0};
mtklein404b3b22015-05-18 09:29:10 -0700104 if (this->copySurface(copy, rt, copyRect, dstPoint)) {
bsalomon6a44c6a2015-05-26 09:49:05 -0700105 dstTexture->setTexture(copy);
106 dstTexture->setOffset(copyRect.fLeft, copyRect.fTop);
mtklein404b3b22015-05-18 09:29:10 -0700107 return true;
108 } else {
109 return false;
110 }
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
mtklein404b3b22015-05-18 09:29:10 -0700433bool 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)) {
mtklein404b3b22015-05-18 09:29:10 -0700449 return true;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000450 }
451
mtklein404b3b22015-05-18 09:29:10 -0700452 if (this->getGpu()->canCopySurface(dst, src, clippedSrcRect, clippedDstPoint)) {
453 this->onCopySurface(dst, src, clippedSrcRect, clippedDstPoint);
454 return true;
455 }
456
457 GrRenderTarget* rt = dst->asRenderTarget();
458 GrTexture* tex = src->asTexture();
459
460 if ((dst == src) || !rt || !tex) {
461 return false;
462 }
463
464 GrPipelineBuilder pipelineBuilder;
465 pipelineBuilder.setRenderTarget(rt);
466 SkMatrix matrix;
467 matrix.setTranslate(SkIntToScalar(clippedSrcRect.fLeft - clippedDstPoint.fX),
468 SkIntToScalar(clippedSrcRect.fTop - clippedDstPoint.fY));
469 matrix.postIDiv(tex->width(), tex->height());
470 pipelineBuilder.addColorTextureProcessor(tex, matrix);
471 SkIRect dstRect = SkIRect::MakeXYWH(clippedDstPoint.fX,
472 clippedDstPoint.fY,
473 clippedSrcRect.width(),
474 clippedSrcRect.height());
475 this->drawSimpleRect(&pipelineBuilder, GrColor_WHITE, SkMatrix::I(), dstRect);
476 return true;
477}
478
479bool GrDrawTarget::canCopySurface(const GrSurface* dst,
480 const GrSurface* src,
481 const SkIRect& srcRect,
482 const SkIPoint& dstPoint) {
483 SkASSERT(dst);
484 SkASSERT(src);
485
486 SkIRect clippedSrcRect;
487 SkIPoint clippedDstPoint;
488 // If the rect is outside the src or dst then we're guaranteed success
489 if (!clip_srcrect_and_dstpoint(dst,
490 src,
491 srcRect,
492 dstPoint,
493 &clippedSrcRect,
494 &clippedDstPoint)) {
495 return true;
496 }
497 return ((dst != src) && dst->asRenderTarget() && src->asTexture()) ||
498 this->getGpu()->canCopySurface(dst, src, clippedSrcRect, clippedDstPoint);
bsalomon@google.comeb851172013-04-15 13:51:00 +0000499}
500
egdaniele36914c2015-02-13 09:00:33 -0800501void GrDrawTarget::setupPipeline(const PipelineInfo& pipelineInfo,
502 GrPipeline* pipeline) {
503 SkNEW_PLACEMENT_ARGS(pipeline, GrPipeline, (*pipelineInfo.fPipelineBuilder,
504 pipelineInfo.fColorPOI,
505 pipelineInfo.fCoveragePOI,
506 *this->caps(),
507 *pipelineInfo.fScissor,
bsalomon6a44c6a2015-05-26 09:49:05 -0700508 &pipelineInfo.fDstTexture));
egdaniele36914c2015-02-13 09:00:33 -0800509}
510///////////////////////////////////////////////////////////////////////////////
511
512GrDrawTarget::PipelineInfo::PipelineInfo(GrPipelineBuilder* pipelineBuilder,
513 GrScissorState* scissor,
514 const GrPrimitiveProcessor* primProc,
515 const SkRect* devBounds,
516 GrDrawTarget* target)
517 : fPipelineBuilder(pipelineBuilder)
518 , fScissor(scissor) {
519 fColorPOI = fPipelineBuilder->colorProcInfo(primProc);
520 fCoveragePOI = fPipelineBuilder->coverageProcInfo(primProc);
521 if (!target->setupDstReadIfNecessary(*fPipelineBuilder, fColorPOI, fCoveragePOI,
bsalomon6a44c6a2015-05-26 09:49:05 -0700522 &fDstTexture, devBounds)) {
egdaniele36914c2015-02-13 09:00:33 -0800523 fPipelineBuilder = NULL;
524 }
525}
526
527GrDrawTarget::PipelineInfo::PipelineInfo(GrPipelineBuilder* pipelineBuilder,
528 GrScissorState* scissor,
529 const GrBatch* batch,
530 const SkRect* devBounds,
531 GrDrawTarget* target)
532 : fPipelineBuilder(pipelineBuilder)
533 , fScissor(scissor) {
534 fColorPOI = fPipelineBuilder->colorProcInfo(batch);
535 fCoveragePOI = fPipelineBuilder->coverageProcInfo(batch);
536 if (!target->setupDstReadIfNecessary(*fPipelineBuilder, fColorPOI, fCoveragePOI,
bsalomon6a44c6a2015-05-26 09:49:05 -0700537 &fDstTexture, devBounds)) {
egdaniele36914c2015-02-13 09:00:33 -0800538 fPipelineBuilder = NULL;
539 }
540}
541
bsalomon@google.combcce8922013-03-25 15:38:39 +0000542///////////////////////////////////////////////////////////////////////////////
543
egdaniel8dd688b2015-01-22 10:16:09 -0800544bool GrClipTarget::setupClip(GrPipelineBuilder* pipelineBuilder,
bsalomon6be6f7c2015-02-26 13:05:21 -0800545 GrPipelineBuilder::AutoRestoreFragmentProcessors* arfp,
egdaniel8dd688b2015-01-22 10:16:09 -0800546 GrPipelineBuilder::AutoRestoreStencil* ars,
joshualitt8059eb92014-12-29 15:10:07 -0800547 GrScissorState* scissorState,
548 const SkRect* devBounds) {
egdaniel8dd688b2015-01-22 10:16:09 -0800549 return fClipMaskManager.setupClipping(pipelineBuilder,
bsalomon6be6f7c2015-02-26 13:05:21 -0800550 arfp,
joshualitt2c93efe2014-11-06 12:57:13 -0800551 ars,
joshualitt9853cce2014-11-17 14:22:48 -0800552 scissorState,
joshualitt9853cce2014-11-17 14:22:48 -0800553 devBounds);
joshualitt2c93efe2014-11-06 12:57:13 -0800554}