robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 8 | #include "GrDrawContext.h" |
| 9 | #include "GrDrawingManager.h" |
| 10 | #include "GrDrawTarget.h" |
| 11 | #include "GrResourceProvider.h" |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 12 | #include "GrSoftwarePathRenderer.h" |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 13 | #include "SkTTopoSort.h" |
| 14 | |
joshualitt | e804292 | 2015-12-11 06:11:21 -0800 | [diff] [blame] | 15 | #include "text/GrAtlasTextContext.h" |
| 16 | #include "text/GrStencilAndCoverTextContext.h" |
robertphillips | 498d7ac | 2015-10-30 10:11:30 -0700 | [diff] [blame] | 17 | |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 18 | void GrDrawingManager::cleanup() { |
| 19 | for (int i = 0; i < fDrawTargets.count(); ++i) { |
robertphillips | 0dfa62c | 2015-11-16 06:23:31 -0800 | [diff] [blame] | 20 | fDrawTargets[i]->makeClosed(); // no drawTarget should receive a new command after this |
| 21 | fDrawTargets[i]->clearRT(); |
| 22 | |
joshualitt | 20ccd40 | 2016-01-05 08:56:56 -0800 | [diff] [blame] | 23 | // We shouldn't need to do this, but it turns out some clients still hold onto drawtargets |
| 24 | // after a cleanup |
| 25 | fDrawTargets[i]->reset(); |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 26 | fDrawTargets[i]->unref(); |
| 27 | } |
| 28 | |
| 29 | fDrawTargets.reset(); |
| 30 | |
| 31 | delete fNVPRTextContext; |
| 32 | fNVPRTextContext = nullptr; |
| 33 | |
| 34 | for (int i = 0; i < kNumPixelGeometries; ++i) { |
| 35 | delete fTextContexts[i][0]; |
| 36 | fTextContexts[i][0] = nullptr; |
| 37 | delete fTextContexts[i][1]; |
| 38 | fTextContexts[i][1] = nullptr; |
| 39 | } |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 40 | |
robertphillips | 13391dd | 2015-10-30 05:15:11 -0700 | [diff] [blame] | 41 | delete fPathRendererChain; |
| 42 | fPathRendererChain = nullptr; |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 43 | SkSafeSetNull(fSoftwarePathRenderer); |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | GrDrawingManager::~GrDrawingManager() { |
| 47 | this->cleanup(); |
| 48 | } |
| 49 | |
| 50 | void GrDrawingManager::abandon() { |
| 51 | fAbandoned = true; |
| 52 | this->cleanup(); |
| 53 | } |
| 54 | |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 55 | void GrDrawingManager::freeGpuResources() { |
| 56 | // a path renderer may be holding onto resources |
robertphillips | 13391dd | 2015-10-30 05:15:11 -0700 | [diff] [blame] | 57 | delete fPathRendererChain; |
| 58 | fPathRendererChain = nullptr; |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 59 | SkSafeSetNull(fSoftwarePathRenderer); |
| 60 | } |
| 61 | |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 62 | void GrDrawingManager::reset() { |
| 63 | for (int i = 0; i < fDrawTargets.count(); ++i) { |
| 64 | fDrawTargets[i]->reset(); |
| 65 | } |
robertphillips | a13e202 | 2015-11-11 12:01:09 -0800 | [diff] [blame] | 66 | fFlushState.reset(); |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void GrDrawingManager::flush() { |
joshualitt | b8918c4 | 2015-12-18 09:59:46 -0800 | [diff] [blame] | 70 | if (fFlushing) { |
| 71 | return; |
| 72 | } |
| 73 | fFlushing = true; |
| 74 | |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 75 | SkDEBUGCODE(bool result =) |
| 76 | SkTTopoSort<GrDrawTarget, GrDrawTarget::TopoSortTraits>(&fDrawTargets); |
| 77 | SkASSERT(result); |
| 78 | |
robertphillips | a13e202 | 2015-11-11 12:01:09 -0800 | [diff] [blame] | 79 | #if 0 |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 80 | for (int i = 0; i < fDrawTargets.count(); ++i) { |
robertphillips | a13e202 | 2015-11-11 12:01:09 -0800 | [diff] [blame] | 81 | SkDEBUGCODE(fDrawTargets[i]->dump();) |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 82 | } |
robertphillips | a13e202 | 2015-11-11 12:01:09 -0800 | [diff] [blame] | 83 | #endif |
| 84 | |
| 85 | for (int i = 0; i < fDrawTargets.count(); ++i) { |
| 86 | fDrawTargets[i]->prepareBatches(&fFlushState); |
| 87 | } |
| 88 | |
| 89 | // Upload all data to the GPU |
| 90 | fFlushState.preIssueDraws(); |
| 91 | |
| 92 | for (int i = 0; i < fDrawTargets.count(); ++i) { |
| 93 | fDrawTargets[i]->drawBatches(&fFlushState); |
| 94 | } |
| 95 | |
| 96 | SkASSERT(fFlushState.lastFlushedToken() == fFlushState.currentToken()); |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 97 | |
robertphillips | 0dfa62c | 2015-11-16 06:23:31 -0800 | [diff] [blame] | 98 | for (int i = 0; i < fDrawTargets.count(); ++i) { |
| 99 | fDrawTargets[i]->reset(); |
| 100 | #ifdef ENABLE_MDB |
| 101 | fDrawTargets[i]->unref(); |
| 102 | #endif |
| 103 | } |
| 104 | |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 105 | #ifndef ENABLE_MDB |
| 106 | // When MDB is disabled we keep reusing the same drawTarget |
| 107 | if (fDrawTargets.count()) { |
| 108 | SkASSERT(fDrawTargets.count() == 1); |
robertphillips | 0dfa62c | 2015-11-16 06:23:31 -0800 | [diff] [blame] | 109 | // Clear out this flag so the topological sort's SkTTopoSort_CheckAllUnmarked check |
| 110 | // won't bark |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 111 | fDrawTargets[0]->resetFlag(GrDrawTarget::kWasOutput_Flag); |
| 112 | } |
robertphillips | 0dfa62c | 2015-11-16 06:23:31 -0800 | [diff] [blame] | 113 | #else |
| 114 | fDrawTargets.reset(); |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 115 | #endif |
robertphillips | a13e202 | 2015-11-11 12:01:09 -0800 | [diff] [blame] | 116 | |
robertphillips | a13e202 | 2015-11-11 12:01:09 -0800 | [diff] [blame] | 117 | fFlushState.reset(); |
joshualitt | b8918c4 | 2015-12-18 09:59:46 -0800 | [diff] [blame] | 118 | fFlushing = false; |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | GrTextContext* GrDrawingManager::textContext(const SkSurfaceProps& props, |
| 122 | GrRenderTarget* rt) { |
| 123 | if (this->abandoned()) { |
| 124 | return nullptr; |
| 125 | } |
| 126 | |
| 127 | SkASSERT(props.pixelGeometry() < kNumPixelGeometries); |
| 128 | bool useDIF = props.isUseDeviceIndependentFonts(); |
| 129 | |
| 130 | if (useDIF && fContext->caps()->shaderCaps()->pathRenderingSupport() && |
| 131 | rt->isStencilBufferMultisampled()) { |
| 132 | GrStencilAttachment* sb = fContext->resourceProvider()->attachStencilAttachment(rt); |
| 133 | if (sb) { |
| 134 | if (!fNVPRTextContext) { |
| 135 | fNVPRTextContext = GrStencilAndCoverTextContext::Create(fContext, props); |
| 136 | } |
| 137 | |
| 138 | return fNVPRTextContext; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | if (!fTextContexts[props.pixelGeometry()][useDIF]) { |
| 143 | fTextContexts[props.pixelGeometry()][useDIF] = GrAtlasTextContext::Create(fContext, props); |
| 144 | } |
| 145 | |
| 146 | return fTextContexts[props.pixelGeometry()][useDIF]; |
| 147 | } |
| 148 | |
| 149 | GrDrawTarget* GrDrawingManager::newDrawTarget(GrRenderTarget* rt) { |
| 150 | SkASSERT(fContext); |
| 151 | |
| 152 | #ifndef ENABLE_MDB |
| 153 | // When MDB is disabled we always just return the single drawTarget |
| 154 | if (fDrawTargets.count()) { |
| 155 | SkASSERT(fDrawTargets.count() == 1); |
robertphillips | 0dfa62c | 2015-11-16 06:23:31 -0800 | [diff] [blame] | 156 | // In the non-MDB-world the same drawTarget gets reused for multiple render targets. |
| 157 | // Update this pointer so all the asserts are happy |
| 158 | rt->setLastDrawTarget(fDrawTargets[0]); |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 159 | // DrawingManager gets the creation ref - this ref is for the caller |
| 160 | return SkRef(fDrawTargets[0]); |
| 161 | } |
| 162 | #endif |
| 163 | |
bsalomon | 69cfe95 | 2015-11-30 13:27:47 -0800 | [diff] [blame] | 164 | GrDrawTarget* dt = new GrDrawTarget(rt, fContext->getGpu(), fContext->resourceProvider(), |
| 165 | fOptionsForDrawTargets); |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 166 | |
| 167 | *fDrawTargets.append() = dt; |
| 168 | |
| 169 | // DrawingManager gets the creation ref - this ref is for the caller |
| 170 | return SkRef(dt); |
| 171 | } |
| 172 | |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 173 | /* |
| 174 | * This method finds a path renderer that can draw the specified path on |
| 175 | * the provided target. |
| 176 | * Due to its expense, the software path renderer has split out so it can |
| 177 | * can be individually allowed/disallowed via the "allowSW" boolean. |
| 178 | */ |
| 179 | GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args, |
| 180 | bool allowSW, |
| 181 | GrPathRendererChain::DrawType drawType, |
| 182 | GrPathRenderer::StencilSupport* stencilSupport) { |
| 183 | |
| 184 | if (!fPathRendererChain) { |
| 185 | fPathRendererChain = new GrPathRendererChain(fContext); |
| 186 | } |
| 187 | |
| 188 | GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport); |
| 189 | if (!pr && allowSW) { |
| 190 | if (!fSoftwarePathRenderer) { |
| 191 | fSoftwarePathRenderer = new GrSoftwarePathRenderer(fContext); |
| 192 | } |
| 193 | pr = fSoftwarePathRenderer; |
| 194 | } |
| 195 | |
| 196 | return pr; |
| 197 | } |
| 198 | |
| 199 | GrDrawContext* GrDrawingManager::drawContext(GrRenderTarget* rt, |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 200 | const SkSurfaceProps* surfaceProps) { |
| 201 | if (this->abandoned()) { |
| 202 | return nullptr; |
| 203 | } |
| 204 | |
| 205 | return new GrDrawContext(this, rt, surfaceProps); |
| 206 | } |