blob: 0184890cf2ce8631bc4b19e09b2b08a53b1ff039 [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
bsalomoneb1cb5c2015-05-22 08:01:09 -070011#include "GrCaps.h"
bsalomon4061b122015-05-29 10:26:19 -070012#include "GrGpu.h"
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000013#include "GrPath.h"
egdaniele36914c2015-02-13 09:00:33 -080014#include "GrPipeline.h"
joshualittb7133be2015-04-08 09:08:31 -070015#include "GrMemoryPool.h"
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000016#include "GrRenderTarget.h"
bsalomon4061b122015-05-29 10:26:19 -070017#include "GrResourceProvider.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080018#include "GrRenderTargetPriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070019#include "GrSurfacePriv.h"
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000020#include "GrTexture.h"
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000021#include "GrVertexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000022
bsalomon16b99132015-08-13 14:55:50 -070023#include "batches/GrDrawBatch.h"
joshualittecd1a692015-08-10 10:08:26 -070024#include "batches/GrRectBatchFactory.h"
joshualitt74417822015-08-07 11:42:16 -070025
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
bsalomonabd30f52015-08-13 13:34:48 -0700128void GrDrawTarget::drawBatch(const GrPipelineBuilder& pipelineBuilder, GrDrawBatch* 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
bsalomona387a112015-08-11 14:47:42 -0700144 GrDrawTarget::PipelineInfo pipelineInfo(&pipelineBuilder, &scissorState, batch, &bounds,
joshualitt99c7c072015-05-01 13:43:30 -0700145 this);
bsalomona387a112015-08-11 14:47:42 -0700146 if (!pipelineInfo.valid()) {
egdaniele36914c2015-02-13 09:00:33 -0800147 return;
148 }
bsalomona387a112015-08-11 14:47:42 -0700149 if (!batch->installPipeline(pipelineInfo.pipelineCreateArgs())) {
150 return;
151 }
152 this->onDrawBatch(batch);
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;
joshualitt4421a4c2015-07-13 09:36:41 -0700200 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
egdaniel8dd688b2015-01-22 10:16:09 -0800201 GrPipelineBuilder::AutoRestoreStencil ars;
joshualitt4421a4c2015-07-13 09:36:41 -0700202 if (!this->setupClip(pipelineBuilder, &arfps, &ars, &scissorState, NULL)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800203 return;
204 }
205
206 // set stencil settings for path
207 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700208 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdaniel8dc7c3a2015-04-16 11:22:42 -0700209 GrStencilAttachment* sb = rt->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800210 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800211
joshualitt1c735482015-07-13 08:08:25 -0700212 this->onStencilPath(pipelineBuilder, pathProc, path, scissorState, stencilSettings);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000213}
214
joshualitt1c735482015-07-13 08:08:25 -0700215void GrDrawTarget::drawPath(const GrPipelineBuilder& pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800216 const GrPathProcessor* pathProc,
joshualitt9853cce2014-11-17 14:22:48 -0800217 const GrPath* path,
218 GrPathRendering::FillType fill) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000219 // TODO: extract portions of checkDraw that are relevant to path rendering.
bsalomon49f085d2014-09-05 13:34:00 -0700220 SkASSERT(path);
jvanverthe9c0fc62015-04-29 11:18:05 -0700221 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000222
joshualitt92e496f2014-10-31 13:56:50 -0700223 SkRect devBounds = path->getBounds();
joshualitt8059eb92014-12-29 15:10:07 -0800224 pathProc->viewMatrix().mapRect(&devBounds);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000225
joshualitt2c93efe2014-11-06 12:57:13 -0800226 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800227 GrScissorState scissorState;
joshualitt4421a4c2015-07-13 09:36:41 -0700228 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
egdaniel8dd688b2015-01-22 10:16:09 -0800229 GrPipelineBuilder::AutoRestoreStencil ars;
joshualitt4421a4c2015-07-13 09:36:41 -0700230 if (!this->setupClip(pipelineBuilder, &arfps, &ars, &scissorState, &devBounds)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800231 return;
232 }
233
234 // set stencil settings for path
235 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700236 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdaniel8dc7c3a2015-04-16 11:22:42 -0700237 GrStencilAttachment* sb = rt->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800238 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800239
bsalomona387a112015-08-11 14:47:42 -0700240 GrDrawTarget::PipelineInfo pipelineInfo(&pipelineBuilder, &scissorState, pathProc, &devBounds,
egdaniele36914c2015-02-13 09:00:33 -0800241 this);
bsalomona387a112015-08-11 14:47:42 -0700242 if (!pipelineInfo.valid()) {
egdaniele36914c2015-02-13 09:00:33 -0800243 return;
244 }
245
246 this->onDrawPath(pathProc, path, stencilSettings, pipelineInfo);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000247}
248
joshualitt1c735482015-07-13 08:08:25 -0700249void GrDrawTarget::drawPaths(const GrPipelineBuilder& pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800250 const GrPathProcessor* pathProc,
joshualitt9853cce2014-11-17 14:22:48 -0800251 const GrPathRange* pathRange,
cdalton55b24af2014-11-25 11:00:56 -0800252 const void* indices,
253 PathIndexType indexType,
254 const float transformValues[],
255 PathTransformType transformType,
joshualitt9853cce2014-11-17 14:22:48 -0800256 int count,
joshualitt92e496f2014-10-31 13:56:50 -0700257 GrPathRendering::FillType fill) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700258 SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
bsalomon49f085d2014-09-05 13:34:00 -0700259 SkASSERT(pathRange);
260 SkASSERT(indices);
bsalomonebc1c102015-08-06 17:33:16 -0700261 SkASSERT(0 == reinterpret_cast<intptr_t>(indices) %
262 GrPathRange::PathIndexSizeInBytes(indexType));
cdalton55b24af2014-11-25 11:00:56 -0800263 SkASSERT(transformValues);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000264
joshualitt2c93efe2014-11-06 12:57:13 -0800265 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800266 GrScissorState scissorState;
joshualitt4421a4c2015-07-13 09:36:41 -0700267 GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
egdaniel8dd688b2015-01-22 10:16:09 -0800268 GrPipelineBuilder::AutoRestoreStencil ars;
joshualitt4421a4c2015-07-13 09:36:41 -0700269 if (!this->setupClip(pipelineBuilder, &arfps, &ars, &scissorState, NULL)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800270 return;
271 }
272
273 // set stencil settings for path
274 GrStencilSettings stencilSettings;
joshualitt1c735482015-07-13 08:08:25 -0700275 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
egdaniel8dc7c3a2015-04-16 11:22:42 -0700276 GrStencilAttachment* sb = rt->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800277 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800278
bsalomon50785a32015-02-06 07:02:37 -0800279 // Don't compute a bounding box for dst copy texture, we'll opt
cdaltonb85a0aa2014-07-21 15:32:44 -0700280 // instead for it to just copy the entire dst. Realistically this is a moot
281 // point, because any context that supports NV_path_rendering will also
282 // support NV_blend_equation_advanced.
bsalomona387a112015-08-11 14:47:42 -0700283 GrDrawTarget::PipelineInfo pipelineInfo(&pipelineBuilder, &scissorState, pathProc, NULL, this);
284 if (!pipelineInfo.valid()) {
egdaniele36914c2015-02-13 09:00:33 -0800285 return;
286 }
287
288 this->onDrawPaths(pathProc, pathRange, indices, indexType, transformValues,
289 transformType, count, stencilSettings, pipelineInfo);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000290}
291
joshualitt1c735482015-07-13 08:08:25 -0700292void GrDrawTarget::drawBWRect(const GrPipelineBuilder& pipelineBuilder,
293 GrColor color,
294 const SkMatrix& viewMatrix,
295 const SkRect& rect,
296 const SkRect* localRect,
297 const SkMatrix* localMatrix) {
bsalomonabd30f52015-08-13 13:34:48 -0700298 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateFillBW(color, viewMatrix, rect,
299 localRect, localMatrix));
joshualittad17cfc2015-05-05 10:45:57 -0700300 this->drawBatch(pipelineBuilder, batch);
301}
302
joshualitt1c735482015-07-13 08:08:25 -0700303void GrDrawTarget::drawAARect(const GrPipelineBuilder& pipelineBuilder,
robertphillipsea461502015-05-26 11:38:03 -0700304 GrColor color,
305 const SkMatrix& viewMatrix,
306 const SkRect& rect,
307 const SkRect& devRect) {
bsalomonabd30f52015-08-13 13:34:48 -0700308 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateFillAA(color, viewMatrix, rect,
309 devRect));
joshualitt14205b12015-08-10 11:40:56 -0700310 this->drawBatch(pipelineBuilder, batch);
robertphillipsea461502015-05-26 11:38:03 -0700311}
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) {
egdaniel51c8d402015-08-06 10:54:13 -0700317 SkIRect rtRect = SkIRect::MakeWH(renderTarget->width(), renderTarget->height());
318 SkIRect clippedRect;
319 if (!rect ||
320 (canIgnoreRect && this->caps()->fullClearIsFree()) ||
321 rect->contains(rtRect)) {
322 rect = &rtRect;
323 } else {
324 clippedRect = *rect;
325 if (!clippedRect.intersect(rtRect)) {
326 return;
327 }
328 rect = &clippedRect;
329 }
330
bsalomon63b21962014-11-05 07:05:34 -0800331 if (fCaps->useDrawInsteadOfClear()) {
332 // This works around a driver bug with clear by drawing a rect instead.
333 // The driver will ignore a clear if it is the only thing rendered to a
334 // target before the target is read.
egdaniel51c8d402015-08-06 10:54:13 -0700335 if (rect == &rtRect) {
bsalomon63b21962014-11-05 07:05:34 -0800336 this->discard(renderTarget);
337 }
bsalomon63b21962014-11-05 07:05:34 -0800338
egdaniel8dd688b2015-01-22 10:16:09 -0800339 GrPipelineBuilder pipelineBuilder;
340 pipelineBuilder.setRenderTarget(renderTarget);
joshualitt9853cce2014-11-17 14:22:48 -0800341
joshualitt1c735482015-07-13 08:08:25 -0700342 this->drawSimpleRect(pipelineBuilder, color, SkMatrix::I(), *rect);
bsalomon63b21962014-11-05 07:05:34 -0800343 } else {
egdaniel51c8d402015-08-06 10:54:13 -0700344 this->onClear(*rect, color, renderTarget);
bsalomon63b21962014-11-05 07:05:34 -0800345 }
346}
347
egdaniel3eee3832014-06-18 13:09:11 -0700348typedef GrTraceMarkerSet::Iter TMIter;
349void GrDrawTarget::saveActiveTraceMarkers() {
350 if (this->caps()->gpuTracingSupport()) {
351 SkASSERT(0 == fStoredTraceMarkers.count());
352 fStoredTraceMarkers.addSet(fActiveTraceMarkers);
353 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
354 this->removeGpuTraceMarker(&(*iter));
355 }
356 }
357}
358
359void GrDrawTarget::restoreActiveTraceMarkers() {
360 if (this->caps()->gpuTracingSupport()) {
361 SkASSERT(0 == fActiveTraceMarkers.count());
362 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
363 this->addGpuTraceMarker(&(*iter));
364 }
365 for (TMIter iter = fActiveTraceMarkers.begin(); iter != fActiveTraceMarkers.end(); ++iter) {
366 this->fStoredTraceMarkers.remove(*iter);
367 }
368 }
369}
370
371void GrDrawTarget::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000372 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000373 SkASSERT(fGpuTraceMarkerCount >= 0);
374 this->fActiveTraceMarkers.add(*marker);
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000375 ++fGpuTraceMarkerCount;
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000376 }
377}
378
egdaniel3eee3832014-06-18 13:09:11 -0700379void GrDrawTarget::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000380 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000381 SkASSERT(fGpuTraceMarkerCount >= 1);
382 this->fActiveTraceMarkers.remove(*marker);
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000383 --fGpuTraceMarkerCount;
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000384 }
385}
386
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000387////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000388
bsalomon@google.com116ad842013-04-09 15:38:19 +0000389namespace {
390// returns true if the read/written rect intersects the src/dst and false if not.
391bool clip_srcrect_and_dstpoint(const GrSurface* dst,
392 const GrSurface* src,
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000393 const SkIRect& srcRect,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000394 const SkIPoint& dstPoint,
395 SkIRect* clippedSrcRect,
396 SkIPoint* clippedDstPoint) {
397 *clippedSrcRect = srcRect;
398 *clippedDstPoint = dstPoint;
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000399
bsalomon@google.com116ad842013-04-09 15:38:19 +0000400 // clip the left edge to src and dst bounds, adjusting dstPoint if necessary
401 if (clippedSrcRect->fLeft < 0) {
402 clippedDstPoint->fX -= clippedSrcRect->fLeft;
403 clippedSrcRect->fLeft = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000404 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000405 if (clippedDstPoint->fX < 0) {
406 clippedSrcRect->fLeft -= clippedDstPoint->fX;
407 clippedDstPoint->fX = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000408 }
409
bsalomon@google.com116ad842013-04-09 15:38:19 +0000410 // clip the top edge to src and dst bounds, adjusting dstPoint if necessary
411 if (clippedSrcRect->fTop < 0) {
412 clippedDstPoint->fY -= clippedSrcRect->fTop;
413 clippedSrcRect->fTop = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000414 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000415 if (clippedDstPoint->fY < 0) {
416 clippedSrcRect->fTop -= clippedDstPoint->fY;
417 clippedDstPoint->fY = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000418 }
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000419
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000420 // clip the right edge to the src and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000421 if (clippedSrcRect->fRight > src->width()) {
422 clippedSrcRect->fRight = src->width();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000423 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000424 if (clippedDstPoint->fX + clippedSrcRect->width() > dst->width()) {
425 clippedSrcRect->fRight = clippedSrcRect->fLeft + dst->width() - clippedDstPoint->fX;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000426 }
427
428 // clip the bottom edge to the src and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000429 if (clippedSrcRect->fBottom > src->height()) {
430 clippedSrcRect->fBottom = src->height();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000431 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000432 if (clippedDstPoint->fY + clippedSrcRect->height() > dst->height()) {
433 clippedSrcRect->fBottom = clippedSrcRect->fTop + dst->height() - clippedDstPoint->fY;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000434 }
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000435
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000436 // The above clipping steps may have inverted the rect if it didn't intersect either the src or
437 // dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000438 return !clippedSrcRect->isEmpty();
439}
440}
441
bsalomon6df86402015-06-01 10:41:49 -0700442void GrDrawTarget::copySurface(GrSurface* dst,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000443 GrSurface* src,
444 const SkIRect& srcRect,
445 const SkIPoint& dstPoint) {
bsalomon49f085d2014-09-05 13:34:00 -0700446 SkASSERT(dst);
447 SkASSERT(src);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000448
449 SkIRect clippedSrcRect;
450 SkIPoint clippedDstPoint;
451 // If the rect is outside the src or dst then we've already succeeded.
452 if (!clip_srcrect_and_dstpoint(dst,
453 src,
454 srcRect,
455 dstPoint,
456 &clippedSrcRect,
457 &clippedDstPoint)) {
bsalomon6df86402015-06-01 10:41:49 -0700458 return;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000459 }
460
bsalomon6df86402015-06-01 10:41:49 -0700461 this->onCopySurface(dst, src, clippedSrcRect, clippedDstPoint);
bsalomon@google.comeb851172013-04-15 13:51:00 +0000462}
463
egdaniele36914c2015-02-13 09:00:33 -0800464///////////////////////////////////////////////////////////////////////////////
465
bsalomona387a112015-08-11 14:47:42 -0700466GrDrawTarget::PipelineInfo::PipelineInfo(const GrPipelineBuilder* pipelineBuilder,
467 const GrScissorState* scissor,
egdaniele36914c2015-02-13 09:00:33 -0800468 const GrPrimitiveProcessor* primProc,
469 const SkRect* devBounds,
bsalomona387a112015-08-11 14:47:42 -0700470 GrDrawTarget* target) {
471 fArgs.fPipelineBuilder = pipelineBuilder;
472 fArgs.fCaps = target->caps();
473 fArgs.fScissor = scissor;
474 fArgs.fColorPOI = fArgs.fPipelineBuilder->colorProcInfo(primProc);
475 fArgs.fCoveragePOI = fArgs.fPipelineBuilder->coverageProcInfo(primProc);
476 if (!target->setupDstReadIfNecessary(*fArgs.fPipelineBuilder, fArgs.fColorPOI,
477 fArgs.fCoveragePOI, &fArgs.fDstTexture, devBounds)) {
478 fArgs.fPipelineBuilder = NULL;
egdaniele36914c2015-02-13 09:00:33 -0800479 }
480}
481
bsalomona387a112015-08-11 14:47:42 -0700482GrDrawTarget::PipelineInfo::PipelineInfo(const GrPipelineBuilder* pipelineBuilder,
483 const GrScissorState* scissor,
bsalomonabd30f52015-08-13 13:34:48 -0700484 const GrDrawBatch* batch,
egdaniele36914c2015-02-13 09:00:33 -0800485 const SkRect* devBounds,
bsalomona387a112015-08-11 14:47:42 -0700486 GrDrawTarget* target) {
487 fArgs.fPipelineBuilder = pipelineBuilder;
488 fArgs.fCaps = target->caps();
489 fArgs.fScissor = scissor;
490 fArgs.fColorPOI = fArgs.fPipelineBuilder->colorProcInfo(batch);
491 fArgs.fCoveragePOI = fArgs.fPipelineBuilder->coverageProcInfo(batch);
492 if (!target->setupDstReadIfNecessary(*fArgs.fPipelineBuilder, fArgs.fColorPOI,
493 fArgs.fCoveragePOI, &fArgs.fDstTexture, devBounds)) {
494 fArgs.fPipelineBuilder = NULL;
egdaniele36914c2015-02-13 09:00:33 -0800495 }
496}
497
bsalomon@google.combcce8922013-03-25 15:38:39 +0000498///////////////////////////////////////////////////////////////////////////////
bsalomon4061b122015-05-29 10:26:19 -0700499GrClipTarget::GrClipTarget(GrContext* context)
500 : INHERITED(context->getGpu(), context->resourceProvider())
501 , fContext(context) {
bsalomonedd77a12015-05-29 09:45:57 -0700502 fClipMaskManager.reset(SkNEW_ARGS(GrClipMaskManager, (this)));
503}
504
bsalomon@google.combcce8922013-03-25 15:38:39 +0000505
joshualitt1c735482015-07-13 08:08:25 -0700506bool GrClipTarget::setupClip(const GrPipelineBuilder& pipelineBuilder,
joshualitt4421a4c2015-07-13 09:36:41 -0700507 GrPipelineBuilder::AutoRestoreFragmentProcessorState* arfps,
egdaniel8dd688b2015-01-22 10:16:09 -0800508 GrPipelineBuilder::AutoRestoreStencil* ars,
joshualitt8059eb92014-12-29 15:10:07 -0800509 GrScissorState* scissorState,
510 const SkRect* devBounds) {
joshualitt1c735482015-07-13 08:08:25 -0700511 return fClipMaskManager->setupClipping(pipelineBuilder,
joshualitt4421a4c2015-07-13 09:36:41 -0700512 arfps,
joshualitt5e6ba212015-07-13 07:35:05 -0700513 ars,
joshualitt5e6ba212015-07-13 07:35:05 -0700514 scissorState,
515 devBounds);
joshualitt2c93efe2014-11-06 12:57:13 -0800516}
bsalomonedd77a12015-05-29 09:45:57 -0700517
518void GrClipTarget::purgeResources() {
519 // The clip mask manager can rebuild all its clip masks so just
520 // get rid of them all.
521 fClipMaskManager->purgeResources();
522};