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