Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | |
Greg Daniel | 54bfb18 | 2018-11-20 17:12:36 -0500 | [diff] [blame] | 8 | |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 9 | #include "include/gpu/GrDirectContext.h" |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/gpu/GrContextThreadSafeProxy.h" |
Adlai Holler | 9555f29 | 2020-10-09 09:41:14 -0400 | [diff] [blame] | 12 | #include "src/core/SkTaskGroup.h" |
| 13 | #include "src/gpu/GrClientMappedBufferManager.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/gpu/GrContextPriv.h" |
| 15 | #include "src/gpu/GrContextThreadSafeProxyPriv.h" |
Adlai Holler | 4aa4c60 | 2020-10-12 13:58:52 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrDrawingManager.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/gpu/GrGpu.h" |
Adlai Holler | 9555f29 | 2020-10-09 09:41:14 -0400 | [diff] [blame] | 18 | #include "src/gpu/GrResourceProvider.h" |
| 19 | #include "src/gpu/GrShaderUtils.h" |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 20 | |
Adlai Holler | 4aa4c60 | 2020-10-12 13:58:52 -0400 | [diff] [blame] | 21 | #include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "src/gpu/effects/GrSkSLFP.h" |
| 23 | #include "src/gpu/gl/GrGLGpu.h" |
| 24 | #include "src/gpu/mock/GrMockGpu.h" |
Robert Phillips | 5edf510 | 2020-08-10 16:30:36 -0400 | [diff] [blame] | 25 | #include "src/gpu/ops/GrSmallPathAtlasMgr.h" |
Robert Phillips | e19babf | 2020-04-06 13:57:30 -0400 | [diff] [blame] | 26 | #include "src/gpu/text/GrAtlasManager.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 27 | #include "src/gpu/text/GrStrikeCache.h" |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 28 | #ifdef SK_METAL |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 29 | #include "src/gpu/mtl/GrMtlTrampoline.h" |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 30 | #endif |
| 31 | #ifdef SK_VULKAN |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 32 | #include "src/gpu/vk/GrVkGpu.h" |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 33 | #endif |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 34 | #ifdef SK_DIRECT3D |
| 35 | #include "src/gpu/d3d/GrD3DGpu.h" |
| 36 | #endif |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 37 | #ifdef SK_DAWN |
Mike Klein | 52337de | 2019-07-25 09:00:52 -0500 | [diff] [blame] | 38 | #include "src/gpu/dawn/GrDawnGpu.h" |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 39 | #endif |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 40 | |
Brian Salomon | 24069eb | 2020-06-24 10:19:52 -0400 | [diff] [blame] | 41 | #if GR_TEST_UTILS |
| 42 | # include "include/utils/SkRandom.h" |
| 43 | # if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS) |
| 44 | # include <sanitizer/lsan_interface.h> |
| 45 | # endif |
| 46 | #endif |
| 47 | |
Robert Phillips | 6db27c2 | 2019-05-01 10:43:56 -0400 | [diff] [blame] | 48 | #ifdef SK_DISABLE_REDUCE_OPLIST_SPLITTING |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 49 | static const bool kDefaultReduceOpsTaskSplitting = false; |
Robert Phillips | 6db27c2 | 2019-05-01 10:43:56 -0400 | [diff] [blame] | 50 | #else |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 51 | static const bool kDefaultReduceOpsTaskSplitting = false; |
Robert Phillips | 6db27c2 | 2019-05-01 10:43:56 -0400 | [diff] [blame] | 52 | #endif |
| 53 | |
Adlai Holler | 9555f29 | 2020-10-09 09:41:14 -0400 | [diff] [blame] | 54 | #define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner()) |
| 55 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 56 | GrDirectContext::GrDirectContext(GrBackendApi backend, const GrContextOptions& options) |
Robert Phillips | 3262bc8 | 2020-08-10 12:11:58 -0400 | [diff] [blame] | 57 | : INHERITED(GrContextThreadSafeProxyPriv::Make(backend, options)) { |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 58 | } |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 59 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 60 | GrDirectContext::~GrDirectContext() { |
Adlai Holler | 9555f29 | 2020-10-09 09:41:14 -0400 | [diff] [blame] | 61 | ASSERT_SINGLE_OWNER |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 62 | // this if-test protects against the case where the context is being destroyed |
| 63 | // before having been fully created |
Adlai Holler | 9555f29 | 2020-10-09 09:41:14 -0400 | [diff] [blame] | 64 | if (fGpu) { |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 65 | this->flushAndSubmit(); |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 66 | } |
Adlai Holler | 9555f29 | 2020-10-09 09:41:14 -0400 | [diff] [blame] | 67 | |
| 68 | this->destroyDrawingManager(); |
| 69 | fMappedBufferManager.reset(); |
| 70 | |
| 71 | // Ideally we could just let the ptr drop, but resource cache queries this ptr in releaseAll. |
| 72 | if (fResourceCache) { |
| 73 | fResourceCache->releaseAll(); |
| 74 | } |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 75 | } |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 76 | |
Adlai Holler | 61a591c | 2020-10-12 12:38:33 -0400 | [diff] [blame] | 77 | sk_sp<GrContextThreadSafeProxy> GrDirectContext::threadSafeProxy() { |
| 78 | return INHERITED::threadSafeProxy(); |
| 79 | } |
| 80 | |
Adlai Holler | a7a4044 | 2020-10-09 09:49:42 -0400 | [diff] [blame] | 81 | void GrDirectContext::resetGLTextureBindings() { |
| 82 | if (this->abandoned() || this->backend() != GrBackendApi::kOpenGL) { |
| 83 | return; |
| 84 | } |
| 85 | fGpu->resetTextureBindings(); |
| 86 | } |
| 87 | |
| 88 | void GrDirectContext::resetContext(uint32_t state) { |
| 89 | ASSERT_SINGLE_OWNER |
| 90 | fGpu->markContextDirty(state); |
| 91 | } |
| 92 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 93 | void GrDirectContext::abandonContext() { |
Adlai Holler | a7a4044 | 2020-10-09 09:49:42 -0400 | [diff] [blame] | 94 | if (INHERITED::abandoned()) { |
| 95 | return; |
| 96 | } |
| 97 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 98 | INHERITED::abandonContext(); |
Adlai Holler | a7a4044 | 2020-10-09 09:49:42 -0400 | [diff] [blame] | 99 | |
| 100 | fStrikeCache->freeAll(); |
| 101 | |
| 102 | fMappedBufferManager->abandon(); |
| 103 | |
| 104 | fResourceProvider->abandon(); |
| 105 | |
| 106 | // abandon first to so destructors |
| 107 | // don't try to free the resources in the API. |
| 108 | fResourceCache->abandonAll(); |
| 109 | |
| 110 | fGpu->disconnect(GrGpu::DisconnectType::kAbandon); |
| 111 | |
| 112 | fMappedBufferManager.reset(); |
Robert Phillips | 079455c | 2020-08-11 15:18:46 -0400 | [diff] [blame] | 113 | if (fSmallPathAtlasMgr) { |
| 114 | fSmallPathAtlasMgr->reset(); |
| 115 | } |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 116 | fAtlasManager->freeAll(); |
| 117 | } |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 118 | |
Adlai Holler | a7a4044 | 2020-10-09 09:49:42 -0400 | [diff] [blame] | 119 | bool GrDirectContext::abandoned() { |
| 120 | if (INHERITED::abandoned()) { |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | if (fGpu && fGpu->isDeviceLost()) { |
| 125 | this->abandonContext(); |
| 126 | return true; |
| 127 | } |
| 128 | return false; |
| 129 | } |
| 130 | |
Adlai Holler | 61a591c | 2020-10-12 12:38:33 -0400 | [diff] [blame] | 131 | bool GrDirectContext::oomed() { return fGpu ? fGpu->checkAndResetOOMed() : false; } |
| 132 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 133 | void GrDirectContext::releaseResourcesAndAbandonContext() { |
Adlai Holler | 61a591c | 2020-10-12 12:38:33 -0400 | [diff] [blame] | 134 | if (INHERITED::abandoned()) { |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | INHERITED::abandonContext(); |
| 139 | |
| 140 | fMappedBufferManager.reset(); |
| 141 | |
| 142 | fResourceProvider->abandon(); |
| 143 | |
| 144 | // Release all resources in the backend 3D API. |
| 145 | fResourceCache->releaseAll(); |
| 146 | |
| 147 | fGpu->disconnect(GrGpu::DisconnectType::kCleanup); |
Robert Phillips | 079455c | 2020-08-11 15:18:46 -0400 | [diff] [blame] | 148 | if (fSmallPathAtlasMgr) { |
| 149 | fSmallPathAtlasMgr->reset(); |
| 150 | } |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 151 | fAtlasManager->freeAll(); |
| 152 | } |
Robert Phillips | 6db27c2 | 2019-05-01 10:43:56 -0400 | [diff] [blame] | 153 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 154 | void GrDirectContext::freeGpuResources() { |
Adlai Holler | 4aa4c60 | 2020-10-12 13:58:52 -0400 | [diff] [blame] | 155 | ASSERT_SINGLE_OWNER |
| 156 | |
| 157 | if (this->abandoned()) { |
| 158 | return; |
| 159 | } |
| 160 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 161 | this->flushAndSubmit(); |
Robert Phillips | 079455c | 2020-08-11 15:18:46 -0400 | [diff] [blame] | 162 | if (fSmallPathAtlasMgr) { |
| 163 | fSmallPathAtlasMgr->reset(); |
| 164 | } |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 165 | fAtlasManager->freeAll(); |
Robert Phillips | 56181ba | 2019-03-08 12:00:45 -0500 | [diff] [blame] | 166 | |
Adlai Holler | 4aa4c60 | 2020-10-12 13:58:52 -0400 | [diff] [blame] | 167 | // TODO: the glyph cache doesn't hold any GpuResources so this call should not be needed here. |
| 168 | // Some slack in the GrTextBlob's implementation requires it though. That could be fixed. |
| 169 | fStrikeCache->freeAll(); |
| 170 | |
| 171 | this->drawingManager()->freeGpuResources(); |
| 172 | |
| 173 | fResourceCache->purgeAllUnlocked(); |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 174 | } |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 175 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 176 | bool GrDirectContext::init() { |
Adlai Holler | 9555f29 | 2020-10-09 09:41:14 -0400 | [diff] [blame] | 177 | ASSERT_SINGLE_OWNER |
| 178 | if (!fGpu) { |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 179 | return false; |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 180 | } |
| 181 | |
Adlai Holler | 9555f29 | 2020-10-09 09:41:14 -0400 | [diff] [blame] | 182 | fThreadSafeProxy->priv().init(fGpu->refCaps()); |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 183 | if (!INHERITED::init()) { |
| 184 | return false; |
| 185 | } |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 186 | |
Adlai Holler | 9555f29 | 2020-10-09 09:41:14 -0400 | [diff] [blame] | 187 | SkASSERT(this->getTextBlobCache()); |
| 188 | SkASSERT(this->threadSafeCache()); |
| 189 | |
| 190 | fStrikeCache = std::make_unique<GrStrikeCache>(); |
| 191 | fResourceCache = std::make_unique<GrResourceCache>(this->caps(), this->singleOwner(), |
| 192 | this->contextID()); |
| 193 | fResourceCache->setProxyProvider(this->proxyProvider()); |
| 194 | fResourceCache->setThreadSafeCache(this->threadSafeCache()); |
| 195 | fResourceProvider = std::make_unique<GrResourceProvider>(fGpu.get(), fResourceCache.get(), |
| 196 | this->singleOwner()); |
| 197 | fMappedBufferManager = std::make_unique<GrClientMappedBufferManager>(this->contextID()); |
| 198 | |
| 199 | fDidTestPMConversions = false; |
| 200 | |
| 201 | // DDL TODO: we need to think through how the task group & persistent cache |
| 202 | // get passed on to/shared between all the DDLRecorders created with this context. |
| 203 | if (this->options().fExecutor) { |
| 204 | fTaskGroup = std::make_unique<SkTaskGroup>(*this->options().fExecutor); |
| 205 | } |
| 206 | |
| 207 | fPersistentCache = this->options().fPersistentCache; |
| 208 | fShaderErrorHandler = this->options().fShaderErrorHandler; |
| 209 | if (!fShaderErrorHandler) { |
| 210 | fShaderErrorHandler = GrShaderUtils::DefaultShaderErrorHandler(); |
| 211 | } |
| 212 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 213 | bool reduceOpsTaskSplitting = kDefaultReduceOpsTaskSplitting; |
| 214 | if (GrContextOptions::Enable::kNo == this->options().fReduceOpsTaskSplitting) { |
| 215 | reduceOpsTaskSplitting = false; |
| 216 | } else if (GrContextOptions::Enable::kYes == this->options().fReduceOpsTaskSplitting) { |
| 217 | reduceOpsTaskSplitting = true; |
| 218 | } |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 219 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 220 | this->setupDrawingManager(true, reduceOpsTaskSplitting); |
| 221 | |
| 222 | GrDrawOpAtlas::AllowMultitexturing allowMultitexturing; |
| 223 | if (GrContextOptions::Enable::kNo == this->options().fAllowMultipleGlyphCacheTextures || |
| 224 | // multitexturing supported only if range can represent the index + texcoords fully |
| 225 | !(this->caps()->shaderCaps()->floatIs32Bits() || |
| 226 | this->caps()->shaderCaps()->integerSupport())) { |
| 227 | allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo; |
| 228 | } else { |
| 229 | allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes; |
| 230 | } |
| 231 | |
| 232 | GrProxyProvider* proxyProvider = this->priv().proxyProvider(); |
| 233 | |
Robert Phillips | 3262bc8 | 2020-08-10 12:11:58 -0400 | [diff] [blame] | 234 | fAtlasManager = std::make_unique<GrAtlasManager>(proxyProvider, |
| 235 | this->options().fGlyphCacheTextureMaximumBytes, |
| 236 | allowMultitexturing); |
| 237 | this->priv().addOnFlushCallbackObject(fAtlasManager.get()); |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 238 | |
| 239 | return true; |
| 240 | } |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 241 | |
Adlai Holler | 3a508e9 | 2020-10-12 13:58:01 -0400 | [diff] [blame] | 242 | void GrDirectContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const { |
| 243 | ASSERT_SINGLE_OWNER |
| 244 | |
| 245 | if (resourceCount) { |
| 246 | *resourceCount = fResourceCache->getBudgetedResourceCount(); |
| 247 | } |
| 248 | if (resourceBytes) { |
| 249 | *resourceBytes = fResourceCache->getBudgetedResourceBytes(); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | size_t GrDirectContext::getResourceCachePurgeableBytes() const { |
| 254 | ASSERT_SINGLE_OWNER |
| 255 | return fResourceCache->getPurgeableBytes(); |
| 256 | } |
| 257 | |
| 258 | void GrDirectContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const { |
| 259 | ASSERT_SINGLE_OWNER |
| 260 | if (maxResources) { |
| 261 | *maxResources = -1; |
| 262 | } |
| 263 | if (maxResourceBytes) { |
| 264 | *maxResourceBytes = this->getResourceCacheLimit(); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | size_t GrDirectContext::getResourceCacheLimit() const { |
| 269 | ASSERT_SINGLE_OWNER |
| 270 | return fResourceCache->getMaxResourceBytes(); |
| 271 | } |
| 272 | |
| 273 | void GrDirectContext::setResourceCacheLimits(int unused, size_t maxResourceBytes) { |
| 274 | ASSERT_SINGLE_OWNER |
| 275 | this->setResourceCacheLimit(maxResourceBytes); |
| 276 | } |
| 277 | |
| 278 | void GrDirectContext::setResourceCacheLimit(size_t maxResourceBytes) { |
| 279 | ASSERT_SINGLE_OWNER |
| 280 | fResourceCache->setLimit(maxResourceBytes); |
| 281 | } |
| 282 | |
Adlai Holler | 4aa4c60 | 2020-10-12 13:58:52 -0400 | [diff] [blame] | 283 | void GrDirectContext::purgeUnlockedResources(bool scratchResourcesOnly) { |
| 284 | ASSERT_SINGLE_OWNER |
| 285 | |
| 286 | if (this->abandoned()) { |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | fResourceCache->purgeUnlockedResources(scratchResourcesOnly); |
| 291 | fResourceCache->purgeAsNeeded(); |
| 292 | |
| 293 | // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient |
| 294 | // place to purge stale blobs |
| 295 | this->getTextBlobCache()->purgeStaleBlobs(); |
| 296 | } |
| 297 | |
| 298 | void GrDirectContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) { |
| 299 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
| 300 | |
| 301 | ASSERT_SINGLE_OWNER |
| 302 | |
| 303 | if (this->abandoned()) { |
| 304 | return; |
| 305 | } |
| 306 | |
| 307 | this->checkAsyncWorkCompletion(); |
| 308 | fMappedBufferManager->process(); |
| 309 | auto purgeTime = GrStdSteadyClock::now() - msNotUsed; |
| 310 | |
| 311 | fResourceCache->purgeAsNeeded(); |
| 312 | fResourceCache->purgeResourcesNotUsedSince(purgeTime); |
| 313 | |
| 314 | if (auto ccpr = this->drawingManager()->getCoverageCountingPathRenderer()) { |
| 315 | ccpr->purgeCacheEntriesOlderThan(this->proxyProvider(), purgeTime); |
| 316 | } |
| 317 | |
| 318 | // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient |
| 319 | // place to purge stale blobs |
| 320 | this->getTextBlobCache()->purgeStaleBlobs(); |
| 321 | } |
| 322 | |
| 323 | void GrDirectContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) { |
| 324 | ASSERT_SINGLE_OWNER |
| 325 | |
| 326 | if (this->abandoned()) { |
| 327 | return; |
| 328 | } |
| 329 | |
| 330 | fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources); |
| 331 | } |
| 332 | |
| 333 | |
Robert Phillips | 5edf510 | 2020-08-10 16:30:36 -0400 | [diff] [blame] | 334 | GrSmallPathAtlasMgr* GrDirectContext::onGetSmallPathAtlasMgr() { |
Robert Phillips | 079455c | 2020-08-11 15:18:46 -0400 | [diff] [blame] | 335 | if (!fSmallPathAtlasMgr) { |
| 336 | fSmallPathAtlasMgr = std::make_unique<GrSmallPathAtlasMgr>(); |
| 337 | |
| 338 | this->priv().addOnFlushCallbackObject(fSmallPathAtlasMgr.get()); |
| 339 | } |
| 340 | |
| 341 | if (!fSmallPathAtlasMgr->initAtlas(this->proxyProvider(), this->caps())) { |
| 342 | return nullptr; |
| 343 | } |
| 344 | |
| 345 | return fSmallPathAtlasMgr.get(); |
Robert Phillips | 5edf510 | 2020-08-10 16:30:36 -0400 | [diff] [blame] | 346 | } |
| 347 | |
John Rosasco | a9b348f | 2019-11-08 13:18:15 -0800 | [diff] [blame] | 348 | #ifdef SK_GL |
Robert Phillips | c7228c6 | 2020-07-14 12:57:39 -0400 | [diff] [blame] | 349 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 350 | /*************************************************************************************************/ |
| 351 | sk_sp<GrDirectContext> GrDirectContext::MakeGL(sk_sp<const GrGLInterface> glInterface) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 352 | GrContextOptions defaultOptions; |
Jim Van Verth | 03b8ab2 | 2020-02-24 11:36:15 -0500 | [diff] [blame] | 353 | return MakeGL(std::move(glInterface), defaultOptions); |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 354 | } |
| 355 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 356 | sk_sp<GrDirectContext> GrDirectContext::MakeGL(const GrContextOptions& options) { |
Brian Salomon | c1b9c10 | 2018-04-06 09:18:00 -0400 | [diff] [blame] | 357 | return MakeGL(nullptr, options); |
| 358 | } |
| 359 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 360 | sk_sp<GrDirectContext> GrDirectContext::MakeGL() { |
Brian Salomon | c1b9c10 | 2018-04-06 09:18:00 -0400 | [diff] [blame] | 361 | GrContextOptions defaultOptions; |
| 362 | return MakeGL(nullptr, defaultOptions); |
| 363 | } |
| 364 | |
Brian Salomon | 24069eb | 2020-06-24 10:19:52 -0400 | [diff] [blame] | 365 | #if GR_TEST_UTILS |
| 366 | GrGLFunction<GrGLGetErrorFn> make_get_error_with_random_oom(GrGLFunction<GrGLGetErrorFn> original) { |
| 367 | // A SkRandom and a GrGLFunction<GrGLGetErrorFn> are too big to be captured by a |
| 368 | // GrGLFunction<GrGLGetError> (surprise, surprise). So we make a context object and |
| 369 | // capture that by pointer. However, GrGLFunction doesn't support calling a destructor |
| 370 | // on the thing it captures. So we leak the context. |
| 371 | struct GetErrorContext { |
| 372 | SkRandom fRandom; |
| 373 | GrGLFunction<GrGLGetErrorFn> fGetError; |
| 374 | }; |
| 375 | |
| 376 | auto errorContext = new GetErrorContext; |
| 377 | |
| 378 | #if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS) |
| 379 | __lsan_ignore_object(errorContext); |
| 380 | #endif |
| 381 | |
| 382 | errorContext->fGetError = original; |
| 383 | |
| 384 | return GrGLFunction<GrGLGetErrorFn>([errorContext]() { |
| 385 | GrGLenum error = errorContext->fGetError(); |
| 386 | if (error == GR_GL_NO_ERROR && (errorContext->fRandom.nextU() % 300) == 0) { |
| 387 | error = GR_GL_OUT_OF_MEMORY; |
| 388 | } |
| 389 | return error; |
| 390 | }); |
| 391 | } |
| 392 | #endif |
| 393 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 394 | sk_sp<GrDirectContext> GrDirectContext::MakeGL(sk_sp<const GrGLInterface> glInterface, |
| 395 | const GrContextOptions& options) { |
| 396 | sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kOpenGL, options)); |
Brian Salomon | 24069eb | 2020-06-24 10:19:52 -0400 | [diff] [blame] | 397 | #if GR_TEST_UTILS |
| 398 | if (options.fRandomGLOOM) { |
| 399 | auto copy = sk_make_sp<GrGLInterface>(*glInterface); |
| 400 | copy->fFunctions.fGetError = |
| 401 | make_get_error_with_random_oom(glInterface->fFunctions.fGetError); |
| 402 | #if GR_GL_CHECK_ERROR |
| 403 | // Suppress logging GL errors since we'll be synthetically generating them. |
| 404 | copy->suppressErrorLogging(); |
| 405 | #endif |
| 406 | glInterface = std::move(copy); |
| 407 | } |
| 408 | #endif |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 409 | direct->fGpu = GrGLGpu::Make(std::move(glInterface), options, direct.get()); |
| 410 | if (!direct->init()) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 411 | return nullptr; |
| 412 | } |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 413 | return direct; |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 414 | } |
John Rosasco | a9b348f | 2019-11-08 13:18:15 -0800 | [diff] [blame] | 415 | #endif |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 416 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 417 | /*************************************************************************************************/ |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 418 | sk_sp<GrDirectContext> GrDirectContext::MakeMock(const GrMockOptions* mockOptions) { |
| 419 | GrContextOptions defaultOptions; |
| 420 | return MakeMock(mockOptions, defaultOptions); |
| 421 | } |
| 422 | |
| 423 | sk_sp<GrDirectContext> GrDirectContext::MakeMock(const GrMockOptions* mockOptions, |
| 424 | const GrContextOptions& options) { |
| 425 | sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kMock, options)); |
| 426 | |
| 427 | direct->fGpu = GrMockGpu::Make(mockOptions, options, direct.get()); |
| 428 | if (!direct->init()) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 429 | return nullptr; |
| 430 | } |
Chris Dalton | a378b45 | 2019-12-11 13:24:11 -0500 | [diff] [blame] | 431 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 432 | return direct; |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 433 | } |
| 434 | |
Greg Daniel | b4d8956 | 2018-10-03 18:44:49 +0000 | [diff] [blame] | 435 | #ifdef SK_VULKAN |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 436 | /*************************************************************************************************/ |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 437 | sk_sp<GrDirectContext> GrDirectContext::MakeVulkan(const GrVkBackendContext& backendContext) { |
| 438 | GrContextOptions defaultOptions; |
| 439 | return MakeVulkan(backendContext, defaultOptions); |
| 440 | } |
| 441 | |
| 442 | sk_sp<GrDirectContext> GrDirectContext::MakeVulkan(const GrVkBackendContext& backendContext, |
| 443 | const GrContextOptions& options) { |
| 444 | sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kVulkan, options)); |
| 445 | |
| 446 | direct->fGpu = GrVkGpu::Make(backendContext, options, direct.get()); |
| 447 | if (!direct->init()) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 448 | return nullptr; |
| 449 | } |
| 450 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 451 | return direct; |
Greg Daniel | b4d8956 | 2018-10-03 18:44:49 +0000 | [diff] [blame] | 452 | } |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 453 | #endif |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 454 | |
| 455 | #ifdef SK_METAL |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 456 | /*************************************************************************************************/ |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 457 | sk_sp<GrDirectContext> GrDirectContext::MakeMetal(void* device, void* queue) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 458 | GrContextOptions defaultOptions; |
| 459 | return MakeMetal(device, queue, defaultOptions); |
| 460 | } |
| 461 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 462 | sk_sp<GrDirectContext> GrDirectContext::MakeMetal(void* device, void* queue, |
| 463 | const GrContextOptions& options) { |
| 464 | sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kMetal, options)); |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 465 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 466 | direct->fGpu = GrMtlTrampoline::MakeGpu(direct.get(), options, device, queue); |
| 467 | if (!direct->init()) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 468 | return nullptr; |
| 469 | } |
Timothy Liang | 4e85e80 | 2018-06-28 16:37:18 -0400 | [diff] [blame] | 470 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 471 | return direct; |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 472 | } |
| 473 | #endif |
| 474 | |
Jim Van Verth | b01e12b | 2020-02-18 14:34:38 -0500 | [diff] [blame] | 475 | #ifdef SK_DIRECT3D |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 476 | /*************************************************************************************************/ |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 477 | sk_sp<GrDirectContext> GrDirectContext::MakeDirect3D(const GrD3DBackendContext& backendContext) { |
| 478 | GrContextOptions defaultOptions; |
| 479 | return MakeDirect3D(backendContext, defaultOptions); |
| 480 | } |
| 481 | |
| 482 | sk_sp<GrDirectContext> GrDirectContext::MakeDirect3D(const GrD3DBackendContext& backendContext, |
| 483 | const GrContextOptions& options) { |
| 484 | sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kDirect3D, options)); |
| 485 | |
| 486 | direct->fGpu = GrD3DGpu::Make(backendContext, options, direct.get()); |
| 487 | if (!direct->init()) { |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 488 | return nullptr; |
| 489 | } |
Jim Van Verth | b01e12b | 2020-02-18 14:34:38 -0500 | [diff] [blame] | 490 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 491 | return direct; |
Jim Van Verth | b01e12b | 2020-02-18 14:34:38 -0500 | [diff] [blame] | 492 | } |
| 493 | #endif |
| 494 | |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 495 | #ifdef SK_DAWN |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 496 | /*************************************************************************************************/ |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 497 | sk_sp<GrDirectContext> GrDirectContext::MakeDawn(const wgpu::Device& device) { |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 498 | GrContextOptions defaultOptions; |
| 499 | return MakeDawn(device, defaultOptions); |
| 500 | } |
| 501 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 502 | sk_sp<GrDirectContext> GrDirectContext::MakeDawn(const wgpu::Device& device, |
| 503 | const GrContextOptions& options) { |
| 504 | sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kDawn, options)); |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 505 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 506 | direct->fGpu = GrDawnGpu::Make(device, options, direct.get()); |
| 507 | if (!direct->init()) { |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 508 | return nullptr; |
| 509 | } |
| 510 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 511 | return direct; |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 512 | } |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 513 | |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 514 | #endif |