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