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" |
joshualitt | 96880d9 | 2016-02-16 10:36:53 -0800 | [diff] [blame] | 11 | #include "GrPathRenderingDrawContext.h" |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 12 | #include "GrResourceProvider.h" |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 13 | #include "GrSoftwarePathRenderer.h" |
brianosman | 0e22eb8 | 2016-08-30 07:07:59 -0700 | [diff] [blame] | 14 | #include "SkSurface_Gpu.h" |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 15 | #include "SkTTopoSort.h" |
| 16 | |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 17 | #include "instanced/InstancedRendering.h" |
| 18 | |
joshualitt | e804292 | 2015-12-11 06:11:21 -0800 | [diff] [blame] | 19 | #include "text/GrAtlasTextContext.h" |
| 20 | #include "text/GrStencilAndCoverTextContext.h" |
robertphillips | 498d7ac | 2015-10-30 10:11:30 -0700 | [diff] [blame] | 21 | |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 22 | using gr_instanced::InstancedRendering; |
| 23 | |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 24 | void GrDrawingManager::cleanup() { |
| 25 | for (int i = 0; i < fDrawTargets.count(); ++i) { |
robertphillips | 0dfa62c | 2015-11-16 06:23:31 -0800 | [diff] [blame] | 26 | fDrawTargets[i]->makeClosed(); // no drawTarget should receive a new command after this |
| 27 | fDrawTargets[i]->clearRT(); |
| 28 | |
joshualitt | 20ccd40 | 2016-01-05 08:56:56 -0800 | [diff] [blame] | 29 | // We shouldn't need to do this, but it turns out some clients still hold onto drawtargets |
| 30 | // after a cleanup |
| 31 | fDrawTargets[i]->reset(); |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 32 | fDrawTargets[i]->unref(); |
| 33 | } |
| 34 | |
| 35 | fDrawTargets.reset(); |
| 36 | |
robertphillips | 13391dd | 2015-10-30 05:15:11 -0700 | [diff] [blame] | 37 | delete fPathRendererChain; |
| 38 | fPathRendererChain = nullptr; |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 39 | SkSafeSetNull(fSoftwarePathRenderer); |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | GrDrawingManager::~GrDrawingManager() { |
| 43 | this->cleanup(); |
| 44 | } |
| 45 | |
| 46 | void GrDrawingManager::abandon() { |
| 47 | fAbandoned = true; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 48 | for (int i = 0; i < fDrawTargets.count(); ++i) { |
csmartdalton | e0d3629 | 2016-07-29 08:14:20 -0700 | [diff] [blame] | 49 | if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) { |
| 50 | InstancedRendering* ir = fDrawTargets[i]->instancedRendering(); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 51 | ir->resetGpuResources(InstancedRendering::ResetType::kAbandon); |
| 52 | } |
| 53 | } |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 54 | this->cleanup(); |
| 55 | } |
| 56 | |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 57 | void GrDrawingManager::freeGpuResources() { |
| 58 | // a path renderer may be holding onto resources |
robertphillips | 13391dd | 2015-10-30 05:15:11 -0700 | [diff] [blame] | 59 | delete fPathRendererChain; |
| 60 | fPathRendererChain = nullptr; |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 61 | SkSafeSetNull(fSoftwarePathRenderer); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 62 | for (int i = 0; i < fDrawTargets.count(); ++i) { |
csmartdalton | e0d3629 | 2016-07-29 08:14:20 -0700 | [diff] [blame] | 63 | if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) { |
| 64 | InstancedRendering* ir = fDrawTargets[i]->instancedRendering(); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 65 | ir->resetGpuResources(InstancedRendering::ResetType::kDestroy); |
| 66 | } |
| 67 | } |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 68 | } |
| 69 | |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 70 | void GrDrawingManager::reset() { |
| 71 | for (int i = 0; i < fDrawTargets.count(); ++i) { |
| 72 | fDrawTargets[i]->reset(); |
| 73 | } |
robertphillips | a13e202 | 2015-11-11 12:01:09 -0800 | [diff] [blame] | 74 | fFlushState.reset(); |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 75 | } |
| 76 | |
bsalomon | dc43898 | 2016-08-31 11:53:49 -0700 | [diff] [blame^] | 77 | bool GrDrawingManager::flush() { |
robertphillips | 7761d61 | 2016-05-16 09:14:53 -0700 | [diff] [blame] | 78 | if (fFlushing || this->wasAbandoned()) { |
bsalomon | dc43898 | 2016-08-31 11:53:49 -0700 | [diff] [blame^] | 79 | return false; |
joshualitt | b8918c4 | 2015-12-18 09:59:46 -0800 | [diff] [blame] | 80 | } |
| 81 | fFlushing = true; |
bsalomon | dc43898 | 2016-08-31 11:53:49 -0700 | [diff] [blame^] | 82 | bool flushed = false; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 83 | SkDEBUGCODE(bool result =) |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 84 | SkTTopoSort<GrDrawTarget, GrDrawTarget::TopoSortTraits>(&fDrawTargets); |
| 85 | SkASSERT(result); |
| 86 | |
robertphillips | 27fe1f8 | 2016-06-29 13:43:22 -0700 | [diff] [blame] | 87 | for (int i = 0; i < fDrawTargets.count(); ++i) { |
| 88 | fDrawTargets[i]->prepareBatches(&fFlushState); |
| 89 | } |
| 90 | |
robertphillips | 26726d1 | 2016-06-28 14:45:31 -0700 | [diff] [blame] | 91 | // Enable this to print out verbose batching information |
robertphillips | a13e202 | 2015-11-11 12:01:09 -0800 | [diff] [blame] | 92 | #if 0 |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 93 | for (int i = 0; i < fDrawTargets.count(); ++i) { |
robertphillips | a13e202 | 2015-11-11 12:01:09 -0800 | [diff] [blame] | 94 | SkDEBUGCODE(fDrawTargets[i]->dump();) |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 95 | } |
robertphillips | a13e202 | 2015-11-11 12:01:09 -0800 | [diff] [blame] | 96 | #endif |
| 97 | |
robertphillips | a13e202 | 2015-11-11 12:01:09 -0800 | [diff] [blame] | 98 | // Upload all data to the GPU |
| 99 | fFlushState.preIssueDraws(); |
| 100 | |
| 101 | for (int i = 0; i < fDrawTargets.count(); ++i) { |
bsalomon | dc43898 | 2016-08-31 11:53:49 -0700 | [diff] [blame^] | 102 | if (fDrawTargets[i]->drawBatches(&fFlushState)) { |
| 103 | flushed = true; |
| 104 | } |
robertphillips | a13e202 | 2015-11-11 12:01:09 -0800 | [diff] [blame] | 105 | } |
| 106 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 107 | SkASSERT(fFlushState.nextDrawToken() == fFlushState.nextTokenToFlush()); |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 108 | |
robertphillips | 0dfa62c | 2015-11-16 06:23:31 -0800 | [diff] [blame] | 109 | for (int i = 0; i < fDrawTargets.count(); ++i) { |
| 110 | fDrawTargets[i]->reset(); |
| 111 | #ifdef ENABLE_MDB |
| 112 | fDrawTargets[i]->unref(); |
| 113 | #endif |
| 114 | } |
| 115 | |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 116 | #ifndef ENABLE_MDB |
| 117 | // When MDB is disabled we keep reusing the same drawTarget |
| 118 | if (fDrawTargets.count()) { |
| 119 | SkASSERT(fDrawTargets.count() == 1); |
robertphillips | 0dfa62c | 2015-11-16 06:23:31 -0800 | [diff] [blame] | 120 | // Clear out this flag so the topological sort's SkTTopoSort_CheckAllUnmarked check |
| 121 | // won't bark |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 122 | fDrawTargets[0]->resetFlag(GrDrawTarget::kWasOutput_Flag); |
| 123 | } |
robertphillips | 0dfa62c | 2015-11-16 06:23:31 -0800 | [diff] [blame] | 124 | #else |
| 125 | fDrawTargets.reset(); |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 126 | #endif |
robertphillips | a13e202 | 2015-11-11 12:01:09 -0800 | [diff] [blame] | 127 | |
robertphillips | a13e202 | 2015-11-11 12:01:09 -0800 | [diff] [blame] | 128 | fFlushState.reset(); |
joshualitt | b8918c4 | 2015-12-18 09:59:46 -0800 | [diff] [blame] | 129 | fFlushing = false; |
bsalomon | dc43898 | 2016-08-31 11:53:49 -0700 | [diff] [blame^] | 130 | return flushed; |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 131 | } |
| 132 | |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 133 | GrDrawTarget* GrDrawingManager::newDrawTarget(GrRenderTarget* rt) { |
| 134 | SkASSERT(fContext); |
| 135 | |
| 136 | #ifndef ENABLE_MDB |
| 137 | // When MDB is disabled we always just return the single drawTarget |
| 138 | if (fDrawTargets.count()) { |
| 139 | SkASSERT(fDrawTargets.count() == 1); |
robertphillips | 0dfa62c | 2015-11-16 06:23:31 -0800 | [diff] [blame] | 140 | // In the non-MDB-world the same drawTarget gets reused for multiple render targets. |
| 141 | // Update this pointer so all the asserts are happy |
| 142 | rt->setLastDrawTarget(fDrawTargets[0]); |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 143 | // DrawingManager gets the creation ref - this ref is for the caller |
| 144 | return SkRef(fDrawTargets[0]); |
| 145 | } |
| 146 | #endif |
| 147 | |
bsalomon | 69cfe95 | 2015-11-30 13:27:47 -0800 | [diff] [blame] | 148 | GrDrawTarget* dt = new GrDrawTarget(rt, fContext->getGpu(), fContext->resourceProvider(), |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 149 | fContext->getAuditTrail(), fOptionsForDrawTargets); |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 150 | |
| 151 | *fDrawTargets.append() = dt; |
| 152 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 153 | // DrawingManager gets the creation ref - this ref is for the caller |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 154 | return SkRef(dt); |
| 155 | } |
| 156 | |
brianosman | 86e7626 | 2016-08-11 12:17:31 -0700 | [diff] [blame] | 157 | GrAtlasTextContext* GrDrawingManager::getAtlasTextContext() { |
| 158 | if (!fAtlasTextContext) { |
| 159 | fAtlasTextContext.reset(GrAtlasTextContext::Create()); |
| 160 | } |
| 161 | |
| 162 | return fAtlasTextContext.get(); |
| 163 | } |
| 164 | |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 165 | /* |
| 166 | * This method finds a path renderer that can draw the specified path on |
| 167 | * the provided target. |
| 168 | * Due to its expense, the software path renderer has split out so it can |
| 169 | * can be individually allowed/disallowed via the "allowSW" boolean. |
| 170 | */ |
| 171 | GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args, |
| 172 | bool allowSW, |
| 173 | GrPathRendererChain::DrawType drawType, |
| 174 | GrPathRenderer::StencilSupport* stencilSupport) { |
| 175 | |
| 176 | if (!fPathRendererChain) { |
| 177 | fPathRendererChain = new GrPathRendererChain(fContext); |
| 178 | } |
| 179 | |
| 180 | GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport); |
| 181 | if (!pr && allowSW) { |
| 182 | if (!fSoftwarePathRenderer) { |
robertphillips | 0152d73 | 2016-05-20 06:38:43 -0700 | [diff] [blame] | 183 | fSoftwarePathRenderer = new GrSoftwarePathRenderer(fContext->textureProvider()); |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 184 | } |
| 185 | pr = fSoftwarePathRenderer; |
| 186 | } |
| 187 | |
| 188 | return pr; |
| 189 | } |
| 190 | |
robertphillips | 29f9fe4 | 2016-08-05 09:28:20 -0700 | [diff] [blame] | 191 | sk_sp<GrDrawContext> GrDrawingManager::makeDrawContext(sk_sp<GrRenderTarget> rt, |
| 192 | sk_sp<SkColorSpace> colorSpace, |
| 193 | const SkSurfaceProps* surfaceProps) { |
robertphillips | 7761d61 | 2016-05-16 09:14:53 -0700 | [diff] [blame] | 194 | if (this->wasAbandoned()) { |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 195 | return nullptr; |
| 196 | } |
| 197 | |
brianosman | 0e22eb8 | 2016-08-30 07:07:59 -0700 | [diff] [blame] | 198 | // SkSurface catches bad color space usage at creation. This check handles anything that slips |
| 199 | // by, including internal usage. We allow a null color space here, for read/write pixels and |
| 200 | // other special code paths. If a color space is provided, though, enforce all other rules. |
| 201 | if (colorSpace && !SkSurface_Gpu::Valid(fContext, rt->config(), colorSpace.get())) { |
| 202 | return nullptr; |
| 203 | } |
joshualitt | 96880d9 | 2016-02-16 10:36:53 -0800 | [diff] [blame] | 204 | |
| 205 | bool useDIF = false; |
| 206 | if (surfaceProps) { |
| 207 | useDIF = surfaceProps->isUseDeviceIndependentFonts(); |
| 208 | } |
| 209 | |
| 210 | if (useDIF && fContext->caps()->shaderCaps()->pathRenderingSupport() && |
| 211 | rt->isStencilBufferMultisampled()) { |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 212 | GrStencilAttachment* sb = fContext->resourceProvider()->attachStencilAttachment(rt.get()); |
joshualitt | 96880d9 | 2016-02-16 10:36:53 -0800 | [diff] [blame] | 213 | if (sb) { |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 214 | return sk_sp<GrDrawContext>(new GrPathRenderingDrawContext( |
brianosman | dfe4f2e | 2016-07-21 13:28:36 -0700 | [diff] [blame] | 215 | fContext, this, std::move(rt), |
| 216 | std::move(colorSpace), surfaceProps, |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 217 | fContext->getAuditTrail(), fSingleOwner)); |
joshualitt | 96880d9 | 2016-02-16 10:36:53 -0800 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
brianosman | dfe4f2e | 2016-07-21 13:28:36 -0700 | [diff] [blame] | 221 | return sk_sp<GrDrawContext>(new GrDrawContext(fContext, this, std::move(rt), |
| 222 | std::move(colorSpace), surfaceProps, |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 223 | fContext->getAuditTrail(), |
| 224 | fSingleOwner)); |
robertphillips | 3dc6ae5 | 2015-10-20 09:54:32 -0700 | [diff] [blame] | 225 | } |