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