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