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