epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
joshualitt | c2893c5 | 2015-01-28 06:54:30 -0800 | [diff] [blame] | 9 | #include "GrDrawTarget.h" |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 10 | |
bsalomon | eb1cb5c | 2015-05-22 08:01:09 -0700 | [diff] [blame] | 11 | #include "GrCaps.h" |
bsalomon | 4061b12 | 2015-05-29 10:26:19 -0700 | [diff] [blame] | 12 | #include "GrGpu.h" |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 13 | #include "GrPath.h" |
egdaniel | e36914c | 2015-02-13 09:00:33 -0800 | [diff] [blame] | 14 | #include "GrPipeline.h" |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 15 | #include "GrMemoryPool.h" |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 16 | #include "GrRenderTarget.h" |
bsalomon | 4061b12 | 2015-05-29 10:26:19 -0700 | [diff] [blame] | 17 | #include "GrResourceProvider.h" |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 18 | #include "GrRenderTargetPriv.h" |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 19 | #include "GrSurfacePriv.h" |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 20 | #include "GrTexture.h" |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 21 | #include "GrVertexBuffer.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 22 | |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 23 | #include "batches/GrClearBatch.h" |
bsalomon | 872062c | 2015-08-18 12:12:35 -0700 | [diff] [blame] | 24 | #include "batches/GrCopySurfaceBatch.h" |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 25 | #include "batches/GrDiscardBatch.h" |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 26 | #include "batches/GrDrawBatch.h" |
bsalomon | add79ef | 2015-08-19 13:26:49 -0700 | [diff] [blame] | 27 | #include "batches/GrDrawPathBatch.h" |
joshualitt | ecd1a69 | 2015-08-10 10:08:26 -0700 | [diff] [blame] | 28 | #include "batches/GrRectBatchFactory.h" |
bsalomon | a44919e | 2015-08-18 13:28:19 -0700 | [diff] [blame] | 29 | #include "batches/GrStencilPathBatch.h" |
joshualitt | 7441782 | 2015-08-07 11:42:16 -0700 | [diff] [blame] | 30 | |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 31 | #include "SkStrokeRec.h" |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 32 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 33 | //////////////////////////////////////////////////////////////////////////////// |
| 34 | |
bsalomon | 4061b12 | 2015-05-29 10:26:19 -0700 | [diff] [blame] | 35 | GrDrawTarget::GrDrawTarget(GrGpu* gpu, GrResourceProvider* resourceProvider) |
| 36 | : fGpu(SkRef(gpu)) |
| 37 | , fCaps(SkRef(gpu->caps())) |
| 38 | , fResourceProvider(resourceProvider) |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 39 | , fFlushing(false) |
| 40 | , fLastFlushToken(0) { |
bsalomon | 4061b12 | 2015-05-29 10:26:19 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | GrDrawTarget::~GrDrawTarget() { |
| 44 | fGpu->unref(); |
| 45 | fCaps->unref(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | //////////////////////////////////////////////////////////////////////////////// |
| 49 | |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 50 | bool GrDrawTarget::setupDstReadIfNecessary(const GrPipelineBuilder& pipelineBuilder, |
egdaniel | e36914c | 2015-02-13 09:00:33 -0800 | [diff] [blame] | 51 | const GrProcOptInfo& colorPOI, |
| 52 | const GrProcOptInfo& coveragePOI, |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 53 | GrXferProcessor::DstTexture* dstTexture, |
bsalomon | ad792c1 | 2015-09-10 11:10:50 -0700 | [diff] [blame^] | 54 | const SkRect& batchBounds) { |
| 55 | SkRect bounds = batchBounds; |
| 56 | bounds.outset(0.5f, 0.5f); |
| 57 | |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 58 | if (!pipelineBuilder.willXPNeedDstTexture(*this->caps(), colorPOI, coveragePOI)) { |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 59 | return true; |
| 60 | } |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 61 | |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 62 | GrRenderTarget* rt = pipelineBuilder.getRenderTarget(); |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 63 | |
| 64 | if (this->caps()->textureBarrierSupport()) { |
| 65 | if (GrTexture* rtTex = rt->asTexture()) { |
bsalomon | dc47ff7 | 2015-05-26 12:16:59 -0700 | [diff] [blame] | 66 | // The render target is a texture, so we can read from it directly in the shader. The XP |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 67 | // will be responsible to detect this situation and request a texture barrier. |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 68 | dstTexture->setTexture(rtTex); |
| 69 | dstTexture->setOffset(0, 0); |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 70 | return true; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | SkIRect copyRect; |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 75 | pipelineBuilder.clip().getConservativeBounds(rt, ©Rect); |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 76 | |
bsalomon | ad792c1 | 2015-09-10 11:10:50 -0700 | [diff] [blame^] | 77 | SkIRect drawIBounds; |
| 78 | bounds.roundOut(&drawIBounds); |
| 79 | if (!copyRect.intersect(drawIBounds)) { |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 80 | #ifdef SK_DEBUG |
bsalomon | ad792c1 | 2015-09-10 11:10:50 -0700 | [diff] [blame^] | 81 | GrCapsDebugf(fCaps, "Missed an early reject. " |
| 82 | "Bailing on draw from setupDstReadIfNecessary.\n"); |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 83 | #endif |
bsalomon | ad792c1 | 2015-09-10 11:10:50 -0700 | [diff] [blame^] | 84 | return false; |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 85 | } |
skia.committer@gmail.com | 05a2ee0 | 2013-04-02 07:01:34 +0000 | [diff] [blame] | 86 | |
commit-bot@chromium.org | 63150af | 2013-04-11 22:00:22 +0000 | [diff] [blame] | 87 | // MSAA consideration: When there is support for reading MSAA samples in the shader we could |
| 88 | // have per-sample dst values by making the copy multisampled. |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 89 | GrSurfaceDesc desc; |
bsalomon | a73239a | 2015-04-28 13:35:17 -0700 | [diff] [blame] | 90 | if (!this->getGpu()->initCopySurfaceDstDesc(rt, &desc)) { |
| 91 | desc.fOrigin = kDefault_GrSurfaceOrigin; |
| 92 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 93 | desc.fConfig = rt->config(); |
| 94 | } |
| 95 | |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 96 | desc.fWidth = copyRect.width(); |
| 97 | desc.fHeight = copyRect.height(); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 98 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 99 | static const uint32_t kFlags = 0; |
| 100 | SkAutoTUnref<GrTexture> copy(fResourceProvider->createApproxTexture(desc, kFlags)); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 101 | |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 102 | if (!copy) { |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 103 | SkDebugf("Failed to create temporary copy of destination texture.\n"); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 104 | return false; |
| 105 | } |
bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 106 | SkIPoint dstPoint = {0, 0}; |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 107 | this->copySurface(copy, rt, copyRect, dstPoint); |
| 108 | dstTexture->setTexture(copy); |
| 109 | dstTexture->setOffset(copyRect.fLeft, copyRect.fTop); |
| 110 | return true; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 111 | } |
| 112 | |
bsalomon | a73239a | 2015-04-28 13:35:17 -0700 | [diff] [blame] | 113 | void GrDrawTarget::flush() { |
| 114 | if (fFlushing) { |
| 115 | return; |
| 116 | } |
| 117 | fFlushing = true; |
| 118 | |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 119 | GrBatchFlushState flushState(fGpu, fResourceProvider, fLastFlushToken); |
| 120 | |
| 121 | // Loop over all batches and generate geometry |
| 122 | for (int i = 0; i < fBatches.count(); ++i) { |
| 123 | fBatches[i]->prepare(&flushState); |
| 124 | } |
| 125 | |
| 126 | // Upload all data to the GPU |
| 127 | flushState.preIssueDraws(); |
| 128 | |
| 129 | // Draw all the generated geometry. |
| 130 | for (int i = 0; i < fBatches.count(); ++i) { |
| 131 | fBatches[i]->draw(&flushState); |
| 132 | } |
| 133 | |
| 134 | fLastFlushToken = flushState.lastFlushedToken(); |
bsalomon | a73239a | 2015-04-28 13:35:17 -0700 | [diff] [blame] | 135 | |
bsalomon | a73239a | 2015-04-28 13:35:17 -0700 | [diff] [blame] | 136 | fFlushing = false; |
| 137 | this->reset(); |
| 138 | } |
| 139 | |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 140 | void GrDrawTarget::reset() { |
| 141 | fBatches.reset(); |
| 142 | } |
| 143 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 144 | void GrDrawTarget::drawBatch(const GrPipelineBuilder& pipelineBuilder, GrDrawBatch* batch) { |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 145 | // Setup clip |
| 146 | GrScissorState scissorState; |
joshualitt | 4421a4c | 2015-07-13 09:36:41 -0700 | [diff] [blame] | 147 | GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps; |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 148 | GrPipelineBuilder::AutoRestoreStencil ars; |
joshualitt | 4421a4c | 2015-07-13 09:36:41 -0700 | [diff] [blame] | 149 | if (!this->setupClip(pipelineBuilder, &arfps, &ars, &scissorState, &batch->bounds())) { |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 150 | return; |
| 151 | } |
| 152 | |
bsalomon | ad792c1 | 2015-09-10 11:10:50 -0700 | [diff] [blame^] | 153 | GrPipeline::CreateArgs args; |
| 154 | if (!this->installPipelineInDrawBatch(&pipelineBuilder, &scissorState, batch)) { |
egdaniel | e36914c | 2015-02-13 09:00:33 -0800 | [diff] [blame] | 155 | return; |
| 156 | } |
bsalomon | ad792c1 | 2015-09-10 11:10:50 -0700 | [diff] [blame^] | 157 | |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 158 | this->recordBatch(batch); |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 159 | } |
| 160 | |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 161 | static const GrStencilSettings& winding_path_stencil_settings() { |
| 162 | GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings, |
| 163 | kIncClamp_StencilOp, |
| 164 | kIncClamp_StencilOp, |
| 165 | kAlwaysIfInClip_StencilFunc, |
| 166 | 0xFFFF, 0xFFFF, 0xFFFF); |
| 167 | return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings); |
| 168 | } |
| 169 | |
| 170 | static const GrStencilSettings& even_odd_path_stencil_settings() { |
| 171 | GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings, |
| 172 | kInvert_StencilOp, |
| 173 | kInvert_StencilOp, |
| 174 | kAlwaysIfInClip_StencilFunc, |
| 175 | 0xFFFF, 0xFFFF, 0xFFFF); |
| 176 | return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings); |
| 177 | } |
| 178 | |
| 179 | void GrDrawTarget::getPathStencilSettingsForFilltype(GrPathRendering::FillType fill, |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 180 | const GrStencilAttachment* sb, |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 181 | GrStencilSettings* outStencilSettings) { |
| 182 | |
| 183 | switch (fill) { |
| 184 | default: |
| 185 | SkFAIL("Unexpected path fill."); |
| 186 | case GrPathRendering::kWinding_FillType: |
| 187 | *outStencilSettings = winding_path_stencil_settings(); |
| 188 | break; |
| 189 | case GrPathRendering::kEvenOdd_FillType: |
| 190 | *outStencilSettings = even_odd_path_stencil_settings(); |
| 191 | break; |
| 192 | } |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 193 | this->clipMaskManager()->adjustPathStencilParams(sb, outStencilSettings); |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 194 | } |
| 195 | |
joshualitt | 1c73548 | 2015-07-13 08:08:25 -0700 | [diff] [blame] | 196 | void GrDrawTarget::stencilPath(const GrPipelineBuilder& pipelineBuilder, |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame] | 197 | const SkMatrix& viewMatrix, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 198 | const GrPath* path, |
| 199 | GrPathRendering::FillType fill) { |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 200 | // TODO: extract portions of checkDraw that are relevant to path stenciling. |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 201 | SkASSERT(path); |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 202 | SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport()); |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 203 | |
| 204 | // Setup clip |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 205 | GrScissorState scissorState; |
joshualitt | 4421a4c | 2015-07-13 09:36:41 -0700 | [diff] [blame] | 206 | GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps; |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 207 | GrPipelineBuilder::AutoRestoreStencil ars; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 208 | if (!this->setupClip(pipelineBuilder, &arfps, &ars, &scissorState, nullptr)) { |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 209 | return; |
| 210 | } |
| 211 | |
| 212 | // set stencil settings for path |
| 213 | GrStencilSettings stencilSettings; |
joshualitt | 1c73548 | 2015-07-13 08:08:25 -0700 | [diff] [blame] | 214 | GrRenderTarget* rt = pipelineBuilder.getRenderTarget(); |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 215 | GrStencilAttachment* sb = rt->renderTargetPriv().attachStencilAttachment(); |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 216 | this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings); |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 217 | |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame] | 218 | GrBatch* batch = GrStencilPathBatch::Create(viewMatrix, |
bsalomon | a44919e | 2015-08-18 13:28:19 -0700 | [diff] [blame] | 219 | pipelineBuilder.isHWAntialias(), |
| 220 | stencilSettings, scissorState, |
| 221 | pipelineBuilder.getRenderTarget(), |
| 222 | path); |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 223 | this->recordBatch(batch); |
bsalomon | a44919e | 2015-08-18 13:28:19 -0700 | [diff] [blame] | 224 | batch->unref(); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 225 | } |
| 226 | |
joshualitt | 1c73548 | 2015-07-13 08:08:25 -0700 | [diff] [blame] | 227 | void GrDrawTarget::drawPath(const GrPipelineBuilder& pipelineBuilder, |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame] | 228 | const SkMatrix& viewMatrix, |
| 229 | GrColor color, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 230 | const GrPath* path, |
| 231 | GrPathRendering::FillType fill) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 232 | SkASSERT(path); |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 233 | SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport()); |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 234 | |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame] | 235 | GrDrawPathBatchBase* batch = GrDrawPathBatch::Create(viewMatrix, color, path); |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 236 | this->drawPathBatch(pipelineBuilder, batch, fill); |
| 237 | batch->unref(); |
| 238 | } |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 239 | |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 240 | void GrDrawTarget::drawPathsFromRange(const GrPipelineBuilder& pipelineBuilder, |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame] | 241 | const SkMatrix& viewMatrix, |
| 242 | const SkMatrix& localMatrix, |
| 243 | GrColor color, |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 244 | GrPathRangeDraw* draw, |
| 245 | GrPathRendering::FillType fill) { |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame] | 246 | GrDrawPathBatchBase* batch = GrDrawPathRangeBatch::Create(viewMatrix, localMatrix, color, draw); |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 247 | this->drawPathBatch(pipelineBuilder, batch, fill); |
| 248 | batch->unref(); |
| 249 | } |
| 250 | |
| 251 | void GrDrawTarget::drawPathBatch(const GrPipelineBuilder& pipelineBuilder, |
| 252 | GrDrawPathBatchBase* batch, |
| 253 | GrPathRendering::FillType fill) { |
bsalomon | add79ef | 2015-08-19 13:26:49 -0700 | [diff] [blame] | 254 | // This looks like drawBatch() but there is an added wrinkle that stencil settings get inserted |
| 255 | // after setupClip() but before onDrawBatch(). TODO: Figure out a better model for handling |
| 256 | // stencil settings WRT interactions between pipeline(builder), clipmaskmanager, and batches. |
| 257 | |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 258 | GrScissorState scissorState; |
joshualitt | 4421a4c | 2015-07-13 09:36:41 -0700 | [diff] [blame] | 259 | GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps; |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 260 | GrPipelineBuilder::AutoRestoreStencil ars; |
bsalomon | add79ef | 2015-08-19 13:26:49 -0700 | [diff] [blame] | 261 | if (!this->setupClip(pipelineBuilder, &arfps, &ars, &scissorState, &batch->bounds())) { |
| 262 | return; |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 263 | } |
| 264 | |
bsalomon | add79ef | 2015-08-19 13:26:49 -0700 | [diff] [blame] | 265 | // Ensure the render target has a stencil buffer and get the stencil settings. |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 266 | GrStencilSettings stencilSettings; |
joshualitt | 1c73548 | 2015-07-13 08:08:25 -0700 | [diff] [blame] | 267 | GrRenderTarget* rt = pipelineBuilder.getRenderTarget(); |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 268 | GrStencilAttachment* sb = rt->renderTargetPriv().attachStencilAttachment(); |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 269 | this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings); |
bsalomon | add79ef | 2015-08-19 13:26:49 -0700 | [diff] [blame] | 270 | batch->setStencilSettings(stencilSettings); |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 271 | |
bsalomon | ad792c1 | 2015-09-10 11:10:50 -0700 | [diff] [blame^] | 272 | GrPipeline::CreateArgs args; |
| 273 | if (!this->installPipelineInDrawBatch(&pipelineBuilder, &scissorState, batch)) { |
bsalomon | add79ef | 2015-08-19 13:26:49 -0700 | [diff] [blame] | 274 | return; |
| 275 | } |
egdaniel | e36914c | 2015-02-13 09:00:33 -0800 | [diff] [blame] | 276 | |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 277 | this->recordBatch(batch); |
commit-bot@chromium.org | 9b62aa1 | 2014-03-25 11:59:40 +0000 | [diff] [blame] | 278 | } |
| 279 | |
joshualitt | d2b23e0 | 2015-08-21 10:53:34 -0700 | [diff] [blame] | 280 | void GrDrawTarget::drawNonAARect(const GrPipelineBuilder& pipelineBuilder, |
joshualitt | 1c73548 | 2015-07-13 08:08:25 -0700 | [diff] [blame] | 281 | GrColor color, |
| 282 | const SkMatrix& viewMatrix, |
joshualitt | b6b513b | 2015-08-21 10:25:18 -0700 | [diff] [blame] | 283 | const SkRect& rect) { |
joshualitt | d2b23e0 | 2015-08-21 10:53:34 -0700 | [diff] [blame] | 284 | SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect, |
| 285 | nullptr, nullptr)); |
joshualitt | ad17cfc | 2015-05-05 10:45:57 -0700 | [diff] [blame] | 286 | this->drawBatch(pipelineBuilder, batch); |
| 287 | } |
| 288 | |
joshualitt | d2b23e0 | 2015-08-21 10:53:34 -0700 | [diff] [blame] | 289 | void GrDrawTarget::drawNonAARect(const GrPipelineBuilder& pipelineBuilder, |
joshualitt | b6b513b | 2015-08-21 10:25:18 -0700 | [diff] [blame] | 290 | GrColor color, |
| 291 | const SkMatrix& viewMatrix, |
| 292 | const SkRect& rect, |
| 293 | const SkMatrix& localMatrix) { |
joshualitt | d2b23e0 | 2015-08-21 10:53:34 -0700 | [diff] [blame] | 294 | SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect, |
| 295 | nullptr, &localMatrix)); |
joshualitt | b6b513b | 2015-08-21 10:25:18 -0700 | [diff] [blame] | 296 | this->drawBatch(pipelineBuilder, batch); |
| 297 | } |
| 298 | |
joshualitt | d2b23e0 | 2015-08-21 10:53:34 -0700 | [diff] [blame] | 299 | void GrDrawTarget::drawNonAARect(const GrPipelineBuilder& pipelineBuilder, |
joshualitt | b6b513b | 2015-08-21 10:25:18 -0700 | [diff] [blame] | 300 | GrColor color, |
| 301 | const SkMatrix& viewMatrix, |
| 302 | const SkRect& rect, |
| 303 | const SkRect& localRect) { |
joshualitt | d2b23e0 | 2015-08-21 10:53:34 -0700 | [diff] [blame] | 304 | SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect, |
| 305 | &localRect, nullptr)); |
joshualitt | b6b513b | 2015-08-21 10:25:18 -0700 | [diff] [blame] | 306 | this->drawBatch(pipelineBuilder, batch); |
| 307 | } |
| 308 | |
| 309 | |
joshualitt | 1c73548 | 2015-07-13 08:08:25 -0700 | [diff] [blame] | 310 | void GrDrawTarget::drawAARect(const GrPipelineBuilder& pipelineBuilder, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 311 | GrColor color, |
| 312 | const SkMatrix& viewMatrix, |
| 313 | const SkRect& rect, |
| 314 | const SkRect& devRect) { |
joshualitt | d2b23e0 | 2015-08-21 10:53:34 -0700 | [diff] [blame] | 315 | SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateAAFill(color, viewMatrix, rect, |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 316 | devRect)); |
joshualitt | 14205b1 | 2015-08-10 11:40:56 -0700 | [diff] [blame] | 317 | this->drawBatch(pipelineBuilder, batch); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 318 | } |
| 319 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 320 | void GrDrawTarget::clear(const SkIRect* rect, |
| 321 | GrColor color, |
| 322 | bool canIgnoreRect, |
bsalomon | 63b2196 | 2014-11-05 07:05:34 -0800 | [diff] [blame] | 323 | GrRenderTarget* renderTarget) { |
egdaniel | 51c8d40 | 2015-08-06 10:54:13 -0700 | [diff] [blame] | 324 | SkIRect rtRect = SkIRect::MakeWH(renderTarget->width(), renderTarget->height()); |
| 325 | SkIRect clippedRect; |
| 326 | if (!rect || |
| 327 | (canIgnoreRect && this->caps()->fullClearIsFree()) || |
| 328 | rect->contains(rtRect)) { |
| 329 | rect = &rtRect; |
| 330 | } else { |
| 331 | clippedRect = *rect; |
| 332 | if (!clippedRect.intersect(rtRect)) { |
| 333 | return; |
| 334 | } |
| 335 | rect = &clippedRect; |
| 336 | } |
| 337 | |
bsalomon | 63b2196 | 2014-11-05 07:05:34 -0800 | [diff] [blame] | 338 | if (fCaps->useDrawInsteadOfClear()) { |
| 339 | // This works around a driver bug with clear by drawing a rect instead. |
| 340 | // The driver will ignore a clear if it is the only thing rendered to a |
| 341 | // target before the target is read. |
egdaniel | 51c8d40 | 2015-08-06 10:54:13 -0700 | [diff] [blame] | 342 | if (rect == &rtRect) { |
bsalomon | 63b2196 | 2014-11-05 07:05:34 -0800 | [diff] [blame] | 343 | this->discard(renderTarget); |
| 344 | } |
bsalomon | 63b2196 | 2014-11-05 07:05:34 -0800 | [diff] [blame] | 345 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 346 | GrPipelineBuilder pipelineBuilder; |
| 347 | pipelineBuilder.setRenderTarget(renderTarget); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 348 | |
joshualitt | d2b23e0 | 2015-08-21 10:53:34 -0700 | [diff] [blame] | 349 | this->drawNonAARect(pipelineBuilder, color, SkMatrix::I(), *rect); |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 350 | } else { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 351 | GrBatch* batch = new GrClearBatch(*rect, color, renderTarget); |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 352 | this->recordBatch(batch); |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 353 | batch->unref(); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | void GrDrawTarget::discard(GrRenderTarget* renderTarget) { |
| 358 | if (this->caps()->discardRenderTargetSupport()) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 359 | GrBatch* batch = new GrDiscardBatch(renderTarget); |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 360 | this->recordBatch(batch); |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 361 | batch->unref(); |
bsalomon | 63b2196 | 2014-11-05 07:05:34 -0800 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 365 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 366 | |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 367 | void GrDrawTarget::copySurface(GrSurface* dst, |
bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 368 | GrSurface* src, |
| 369 | const SkIRect& srcRect, |
| 370 | const SkIPoint& dstPoint) { |
bsalomon | 872062c | 2015-08-18 12:12:35 -0700 | [diff] [blame] | 371 | GrBatch* batch = GrCopySurfaceBatch::Create(dst, src, srcRect, dstPoint); |
| 372 | if (batch) { |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 373 | this->recordBatch(batch); |
bsalomon | 872062c | 2015-08-18 12:12:35 -0700 | [diff] [blame] | 374 | batch->unref(); |
bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 375 | } |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 376 | } |
| 377 | |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 378 | template <class Left, class Right> static bool intersect(const Left& a, const Right& b) { |
| 379 | SkASSERT(a.fLeft <= a.fRight && a.fTop <= a.fBottom && |
| 380 | b.fLeft <= b.fRight && b.fTop <= b.fBottom); |
| 381 | return a.fLeft < b.fRight && b.fLeft < a.fRight && a.fTop < b.fBottom && b.fTop < a.fBottom; |
| 382 | } |
| 383 | |
| 384 | void GrDrawTarget::recordBatch(GrBatch* batch) { |
| 385 | // Check if there is a Batch Draw we can batch with by linearly searching back until we either |
| 386 | // 1) check every draw |
| 387 | // 2) intersect with something |
| 388 | // 3) find a 'blocker' |
| 389 | // Experimentally we have found that most batching occurs within the first 10 comparisons. |
| 390 | static const int kMaxLookback = 10; |
| 391 | |
| 392 | GrBATCH_INFO("Re-Recording (%s, B%u)\n" |
| 393 | "\tBounds (%f, %f, %f, %f)\n", |
| 394 | batch->name(), |
| 395 | batch->uniqueID(), |
| 396 | batch->bounds().fLeft, batch->bounds().fRight, |
| 397 | batch->bounds().fTop, batch->bounds().fBottom); |
| 398 | GrBATCH_INFO(SkTabString(batch->dumpInfo(), 1).c_str()); |
| 399 | GrBATCH_INFO("\tOutcome:\n"); |
| 400 | int maxCandidates = SkTMin(kMaxLookback, fBatches.count()); |
| 401 | if (maxCandidates) { |
| 402 | int i = 0; |
| 403 | while (true) { |
| 404 | GrBatch* candidate = fBatches.fromBack(i); |
| 405 | // We cannot continue to search backwards if the render target changes |
| 406 | if (candidate->renderTargetUniqueID() != batch->renderTargetUniqueID()) { |
| 407 | GrBATCH_INFO("\t\tBreaking because of (%s, B%u) Rendertarget\n", |
| 408 | candidate->name(), candidate->uniqueID()); |
| 409 | break; |
| 410 | } |
| 411 | if (candidate->combineIfPossible(batch, *this->caps())) { |
| 412 | GrBATCH_INFO("\t\tCombining with (%s, B%u)\n", candidate->name(), |
| 413 | candidate->uniqueID()); |
| 414 | return; |
| 415 | } |
| 416 | // Stop going backwards if we would cause a painter's order violation. |
| 417 | if (intersect(candidate->bounds(), batch->bounds())) { |
| 418 | GrBATCH_INFO("\t\tIntersects with (%s, B%u)\n", candidate->name(), |
| 419 | candidate->uniqueID()); |
| 420 | break; |
| 421 | } |
| 422 | ++i; |
| 423 | if (i == maxCandidates) { |
| 424 | GrBATCH_INFO("\t\tReached max lookback or beginning of batch array %d\n", i); |
| 425 | break; |
| 426 | } |
| 427 | } |
| 428 | } else { |
| 429 | GrBATCH_INFO("\t\tFirstBatch\n"); |
| 430 | } |
| 431 | fBatches.push_back().reset(SkRef(batch)); |
| 432 | } |
| 433 | |
egdaniel | e36914c | 2015-02-13 09:00:33 -0800 | [diff] [blame] | 434 | /////////////////////////////////////////////////////////////////////////////// |
| 435 | |
bsalomon | ad792c1 | 2015-09-10 11:10:50 -0700 | [diff] [blame^] | 436 | bool GrDrawTarget::installPipelineInDrawBatch(const GrPipelineBuilder* pipelineBuilder, |
| 437 | const GrScissorState* scissor, |
| 438 | GrDrawBatch* batch) { |
| 439 | GrPipeline::CreateArgs args; |
| 440 | args.fPipelineBuilder = pipelineBuilder; |
| 441 | args.fCaps = this->caps(); |
| 442 | args.fScissor = scissor; |
| 443 | args.fColorPOI = pipelineBuilder->colorProcInfo(batch); |
| 444 | args.fCoveragePOI = pipelineBuilder->coverageProcInfo(batch); |
| 445 | if (!this->setupDstReadIfNecessary(*pipelineBuilder, args.fColorPOI, |
| 446 | args.fCoveragePOI, &args.fDstTexture, |
| 447 | batch->bounds())) { |
| 448 | return false; |
egdaniel | e36914c | 2015-02-13 09:00:33 -0800 | [diff] [blame] | 449 | } |
bsalomon | ad792c1 | 2015-09-10 11:10:50 -0700 | [diff] [blame^] | 450 | |
| 451 | if (!batch->installPipeline(args)) { |
| 452 | return false; |
| 453 | } |
| 454 | |
| 455 | return true; |
egdaniel | e36914c | 2015-02-13 09:00:33 -0800 | [diff] [blame] | 456 | } |
| 457 | |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 458 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon | 4061b12 | 2015-05-29 10:26:19 -0700 | [diff] [blame] | 459 | GrClipTarget::GrClipTarget(GrContext* context) |
| 460 | : INHERITED(context->getGpu(), context->resourceProvider()) |
| 461 | , fContext(context) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 462 | fClipMaskManager.reset(new GrClipMaskManager(this)); |
bsalomon | edd77a1 | 2015-05-29 09:45:57 -0700 | [diff] [blame] | 463 | } |
| 464 | |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 465 | |
joshualitt | 1c73548 | 2015-07-13 08:08:25 -0700 | [diff] [blame] | 466 | bool GrClipTarget::setupClip(const GrPipelineBuilder& pipelineBuilder, |
joshualitt | 4421a4c | 2015-07-13 09:36:41 -0700 | [diff] [blame] | 467 | GrPipelineBuilder::AutoRestoreFragmentProcessorState* arfps, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 468 | GrPipelineBuilder::AutoRestoreStencil* ars, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 469 | GrScissorState* scissorState, |
| 470 | const SkRect* devBounds) { |
joshualitt | 1c73548 | 2015-07-13 08:08:25 -0700 | [diff] [blame] | 471 | return fClipMaskManager->setupClipping(pipelineBuilder, |
joshualitt | 4421a4c | 2015-07-13 09:36:41 -0700 | [diff] [blame] | 472 | arfps, |
joshualitt | 5e6ba21 | 2015-07-13 07:35:05 -0700 | [diff] [blame] | 473 | ars, |
joshualitt | 5e6ba21 | 2015-07-13 07:35:05 -0700 | [diff] [blame] | 474 | scissorState, |
| 475 | devBounds); |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 476 | } |
bsalomon | edd77a1 | 2015-05-29 09:45:57 -0700 | [diff] [blame] | 477 | |
| 478 | void GrClipTarget::purgeResources() { |
| 479 | // The clip mask manager can rebuild all its clip masks so just |
| 480 | // get rid of them all. |
| 481 | fClipMaskManager->purgeResources(); |
| 482 | }; |
bsalomon | 5ea0363 | 2015-08-18 10:33:30 -0700 | [diff] [blame] | 483 | |
| 484 | void GrClipTarget::clearStencilClip(const SkIRect& rect, bool insideClip, GrRenderTarget* rt) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 485 | GrBatch* batch = new GrClearStencilClipBatch(rect, insideClip, rt); |
bsalomon | 512be53 | 2015-09-10 10:42:55 -0700 | [diff] [blame] | 486 | this->recordBatch(batch); |
bsalomon | 5ea0363 | 2015-08-18 10:33:30 -0700 | [diff] [blame] | 487 | batch->unref(); |
| 488 | } |