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 | |
Adlai Holler | 3acc69a | 2020-10-13 08:20:51 -0400 | [diff] [blame] | 11 | #include "include/core/SkTraceMemoryDump.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/gpu/GrContextThreadSafeProxy.h" |
Adlai Holler | 9555f29 | 2020-10-09 09:41:14 -0400 | [diff] [blame] | 13 | #include "src/core/SkTaskGroup.h" |
| 14 | #include "src/gpu/GrClientMappedBufferManager.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/gpu/GrContextPriv.h" |
| 16 | #include "src/gpu/GrContextThreadSafeProxyPriv.h" |
Adlai Holler | 4aa4c60 | 2020-10-12 13:58:52 -0400 | [diff] [blame] | 17 | #include "src/gpu/GrDrawingManager.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/gpu/GrGpu.h" |
Adlai Holler | 9555f29 | 2020-10-09 09:41:14 -0400 | [diff] [blame] | 19 | #include "src/gpu/GrResourceProvider.h" |
| 20 | #include "src/gpu/GrShaderUtils.h" |
Adlai Holler | 3acc69a | 2020-10-13 08:20:51 -0400 | [diff] [blame] | 21 | #include "src/image/SkImage_GpuBase.h" |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 22 | |
Adlai Holler | 4aa4c60 | 2020-10-12 13:58:52 -0400 | [diff] [blame] | 23 | #include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "src/gpu/effects/GrSkSLFP.h" |
| 25 | #include "src/gpu/gl/GrGLGpu.h" |
| 26 | #include "src/gpu/mock/GrMockGpu.h" |
Robert Phillips | 5edf510 | 2020-08-10 16:30:36 -0400 | [diff] [blame] | 27 | #include "src/gpu/ops/GrSmallPathAtlasMgr.h" |
Robert Phillips | e19babf | 2020-04-06 13:57:30 -0400 | [diff] [blame] | 28 | #include "src/gpu/text/GrAtlasManager.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 29 | #include "src/gpu/text/GrStrikeCache.h" |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 30 | #ifdef SK_METAL |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 31 | #include "src/gpu/mtl/GrMtlTrampoline.h" |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 32 | #endif |
| 33 | #ifdef SK_VULKAN |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 34 | #include "src/gpu/vk/GrVkGpu.h" |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 35 | #endif |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 36 | #ifdef SK_DIRECT3D |
| 37 | #include "src/gpu/d3d/GrD3DGpu.h" |
| 38 | #endif |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 39 | #ifdef SK_DAWN |
Mike Klein | 52337de | 2019-07-25 09:00:52 -0500 | [diff] [blame] | 40 | #include "src/gpu/dawn/GrDawnGpu.h" |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 41 | #endif |
Adlai Holler | 6d0745b | 2020-10-13 13:29:00 -0400 | [diff] [blame^] | 42 | #include <memory> |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 43 | |
Brian Salomon | 24069eb | 2020-06-24 10:19:52 -0400 | [diff] [blame] | 44 | #if GR_TEST_UTILS |
| 45 | # include "include/utils/SkRandom.h" |
| 46 | # if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS) |
| 47 | # include <sanitizer/lsan_interface.h> |
| 48 | # endif |
| 49 | #endif |
| 50 | |
Robert Phillips | 6db27c2 | 2019-05-01 10:43:56 -0400 | [diff] [blame] | 51 | #ifdef SK_DISABLE_REDUCE_OPLIST_SPLITTING |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 52 | static const bool kDefaultReduceOpsTaskSplitting = false; |
Robert Phillips | 6db27c2 | 2019-05-01 10:43:56 -0400 | [diff] [blame] | 53 | #else |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 54 | static const bool kDefaultReduceOpsTaskSplitting = false; |
Robert Phillips | 6db27c2 | 2019-05-01 10:43:56 -0400 | [diff] [blame] | 55 | #endif |
| 56 | |
Adlai Holler | 9555f29 | 2020-10-09 09:41:14 -0400 | [diff] [blame] | 57 | #define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner()) |
| 58 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 59 | GrDirectContext::GrDirectContext(GrBackendApi backend, const GrContextOptions& options) |
Robert Phillips | 3262bc8 | 2020-08-10 12:11:58 -0400 | [diff] [blame] | 60 | : INHERITED(GrContextThreadSafeProxyPriv::Make(backend, options)) { |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 61 | } |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 62 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 63 | GrDirectContext::~GrDirectContext() { |
Adlai Holler | 9555f29 | 2020-10-09 09:41:14 -0400 | [diff] [blame] | 64 | ASSERT_SINGLE_OWNER |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 65 | // this if-test protects against the case where the context is being destroyed |
| 66 | // before having been fully created |
Adlai Holler | 9555f29 | 2020-10-09 09:41:14 -0400 | [diff] [blame] | 67 | if (fGpu) { |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 68 | this->flushAndSubmit(); |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 69 | } |
Adlai Holler | 9555f29 | 2020-10-09 09:41:14 -0400 | [diff] [blame] | 70 | |
| 71 | this->destroyDrawingManager(); |
| 72 | fMappedBufferManager.reset(); |
| 73 | |
| 74 | // Ideally we could just let the ptr drop, but resource cache queries this ptr in releaseAll. |
| 75 | if (fResourceCache) { |
| 76 | fResourceCache->releaseAll(); |
| 77 | } |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 78 | } |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 79 | |
Adlai Holler | 61a591c | 2020-10-12 12:38:33 -0400 | [diff] [blame] | 80 | sk_sp<GrContextThreadSafeProxy> GrDirectContext::threadSafeProxy() { |
| 81 | return INHERITED::threadSafeProxy(); |
| 82 | } |
| 83 | |
Adlai Holler | a7a4044 | 2020-10-09 09:49:42 -0400 | [diff] [blame] | 84 | void GrDirectContext::resetGLTextureBindings() { |
| 85 | if (this->abandoned() || this->backend() != GrBackendApi::kOpenGL) { |
| 86 | return; |
| 87 | } |
| 88 | fGpu->resetTextureBindings(); |
| 89 | } |
| 90 | |
| 91 | void GrDirectContext::resetContext(uint32_t state) { |
| 92 | ASSERT_SINGLE_OWNER |
| 93 | fGpu->markContextDirty(state); |
| 94 | } |
| 95 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 96 | void GrDirectContext::abandonContext() { |
Adlai Holler | a7a4044 | 2020-10-09 09:49:42 -0400 | [diff] [blame] | 97 | if (INHERITED::abandoned()) { |
| 98 | return; |
| 99 | } |
| 100 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 101 | INHERITED::abandonContext(); |
Adlai Holler | a7a4044 | 2020-10-09 09:49:42 -0400 | [diff] [blame] | 102 | |
| 103 | fStrikeCache->freeAll(); |
| 104 | |
| 105 | fMappedBufferManager->abandon(); |
| 106 | |
| 107 | fResourceProvider->abandon(); |
| 108 | |
| 109 | // abandon first to so destructors |
| 110 | // don't try to free the resources in the API. |
| 111 | fResourceCache->abandonAll(); |
| 112 | |
| 113 | fGpu->disconnect(GrGpu::DisconnectType::kAbandon); |
| 114 | |
| 115 | fMappedBufferManager.reset(); |
Robert Phillips | 079455c | 2020-08-11 15:18:46 -0400 | [diff] [blame] | 116 | if (fSmallPathAtlasMgr) { |
| 117 | fSmallPathAtlasMgr->reset(); |
| 118 | } |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 119 | fAtlasManager->freeAll(); |
| 120 | } |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 121 | |
Adlai Holler | a7a4044 | 2020-10-09 09:49:42 -0400 | [diff] [blame] | 122 | bool GrDirectContext::abandoned() { |
| 123 | if (INHERITED::abandoned()) { |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | if (fGpu && fGpu->isDeviceLost()) { |
| 128 | this->abandonContext(); |
| 129 | return true; |
| 130 | } |
| 131 | return false; |
| 132 | } |
| 133 | |
Adlai Holler | 61a591c | 2020-10-12 12:38:33 -0400 | [diff] [blame] | 134 | bool GrDirectContext::oomed() { return fGpu ? fGpu->checkAndResetOOMed() : false; } |
| 135 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 136 | void GrDirectContext::releaseResourcesAndAbandonContext() { |
Adlai Holler | 61a591c | 2020-10-12 12:38:33 -0400 | [diff] [blame] | 137 | if (INHERITED::abandoned()) { |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | INHERITED::abandonContext(); |
| 142 | |
| 143 | fMappedBufferManager.reset(); |
| 144 | |
| 145 | fResourceProvider->abandon(); |
| 146 | |
| 147 | // Release all resources in the backend 3D API. |
| 148 | fResourceCache->releaseAll(); |
| 149 | |
| 150 | fGpu->disconnect(GrGpu::DisconnectType::kCleanup); |
Robert Phillips | 079455c | 2020-08-11 15:18:46 -0400 | [diff] [blame] | 151 | if (fSmallPathAtlasMgr) { |
| 152 | fSmallPathAtlasMgr->reset(); |
| 153 | } |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 154 | fAtlasManager->freeAll(); |
| 155 | } |
Robert Phillips | 6db27c2 | 2019-05-01 10:43:56 -0400 | [diff] [blame] | 156 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 157 | void GrDirectContext::freeGpuResources() { |
Adlai Holler | 4aa4c60 | 2020-10-12 13:58:52 -0400 | [diff] [blame] | 158 | ASSERT_SINGLE_OWNER |
| 159 | |
| 160 | if (this->abandoned()) { |
| 161 | return; |
| 162 | } |
| 163 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 164 | this->flushAndSubmit(); |
Robert Phillips | 079455c | 2020-08-11 15:18:46 -0400 | [diff] [blame] | 165 | if (fSmallPathAtlasMgr) { |
| 166 | fSmallPathAtlasMgr->reset(); |
| 167 | } |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 168 | fAtlasManager->freeAll(); |
Robert Phillips | 56181ba | 2019-03-08 12:00:45 -0500 | [diff] [blame] | 169 | |
Adlai Holler | 4aa4c60 | 2020-10-12 13:58:52 -0400 | [diff] [blame] | 170 | // TODO: the glyph cache doesn't hold any GpuResources so this call should not be needed here. |
| 171 | // Some slack in the GrTextBlob's implementation requires it though. That could be fixed. |
| 172 | fStrikeCache->freeAll(); |
| 173 | |
| 174 | this->drawingManager()->freeGpuResources(); |
| 175 | |
| 176 | fResourceCache->purgeAllUnlocked(); |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 177 | } |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 178 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 179 | bool GrDirectContext::init() { |
Adlai Holler | 9555f29 | 2020-10-09 09:41:14 -0400 | [diff] [blame] | 180 | ASSERT_SINGLE_OWNER |
| 181 | if (!fGpu) { |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 182 | return false; |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 183 | } |
| 184 | |
Adlai Holler | 9555f29 | 2020-10-09 09:41:14 -0400 | [diff] [blame] | 185 | fThreadSafeProxy->priv().init(fGpu->refCaps()); |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 186 | if (!INHERITED::init()) { |
| 187 | return false; |
| 188 | } |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 189 | |
Adlai Holler | 9555f29 | 2020-10-09 09:41:14 -0400 | [diff] [blame] | 190 | SkASSERT(this->getTextBlobCache()); |
| 191 | SkASSERT(this->threadSafeCache()); |
| 192 | |
| 193 | fStrikeCache = std::make_unique<GrStrikeCache>(); |
| 194 | fResourceCache = std::make_unique<GrResourceCache>(this->caps(), this->singleOwner(), |
| 195 | this->contextID()); |
| 196 | fResourceCache->setProxyProvider(this->proxyProvider()); |
| 197 | fResourceCache->setThreadSafeCache(this->threadSafeCache()); |
| 198 | fResourceProvider = std::make_unique<GrResourceProvider>(fGpu.get(), fResourceCache.get(), |
| 199 | this->singleOwner()); |
| 200 | fMappedBufferManager = std::make_unique<GrClientMappedBufferManager>(this->contextID()); |
| 201 | |
| 202 | fDidTestPMConversions = false; |
| 203 | |
| 204 | // DDL TODO: we need to think through how the task group & persistent cache |
| 205 | // get passed on to/shared between all the DDLRecorders created with this context. |
| 206 | if (this->options().fExecutor) { |
| 207 | fTaskGroup = std::make_unique<SkTaskGroup>(*this->options().fExecutor); |
| 208 | } |
| 209 | |
| 210 | fPersistentCache = this->options().fPersistentCache; |
| 211 | fShaderErrorHandler = this->options().fShaderErrorHandler; |
| 212 | if (!fShaderErrorHandler) { |
| 213 | fShaderErrorHandler = GrShaderUtils::DefaultShaderErrorHandler(); |
| 214 | } |
| 215 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 216 | bool reduceOpsTaskSplitting = kDefaultReduceOpsTaskSplitting; |
| 217 | if (GrContextOptions::Enable::kNo == this->options().fReduceOpsTaskSplitting) { |
| 218 | reduceOpsTaskSplitting = false; |
| 219 | } else if (GrContextOptions::Enable::kYes == this->options().fReduceOpsTaskSplitting) { |
| 220 | reduceOpsTaskSplitting = true; |
| 221 | } |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 222 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 223 | this->setupDrawingManager(true, reduceOpsTaskSplitting); |
| 224 | |
| 225 | GrDrawOpAtlas::AllowMultitexturing allowMultitexturing; |
| 226 | if (GrContextOptions::Enable::kNo == this->options().fAllowMultipleGlyphCacheTextures || |
| 227 | // multitexturing supported only if range can represent the index + texcoords fully |
| 228 | !(this->caps()->shaderCaps()->floatIs32Bits() || |
| 229 | this->caps()->shaderCaps()->integerSupport())) { |
| 230 | allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo; |
| 231 | } else { |
| 232 | allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes; |
| 233 | } |
| 234 | |
| 235 | GrProxyProvider* proxyProvider = this->priv().proxyProvider(); |
| 236 | |
Robert Phillips | 3262bc8 | 2020-08-10 12:11:58 -0400 | [diff] [blame] | 237 | fAtlasManager = std::make_unique<GrAtlasManager>(proxyProvider, |
| 238 | this->options().fGlyphCacheTextureMaximumBytes, |
| 239 | allowMultitexturing); |
| 240 | this->priv().addOnFlushCallbackObject(fAtlasManager.get()); |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 241 | |
| 242 | return true; |
| 243 | } |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 244 | |
Adlai Holler | 3a508e9 | 2020-10-12 13:58:01 -0400 | [diff] [blame] | 245 | void GrDirectContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const { |
| 246 | ASSERT_SINGLE_OWNER |
| 247 | |
| 248 | if (resourceCount) { |
| 249 | *resourceCount = fResourceCache->getBudgetedResourceCount(); |
| 250 | } |
| 251 | if (resourceBytes) { |
| 252 | *resourceBytes = fResourceCache->getBudgetedResourceBytes(); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | size_t GrDirectContext::getResourceCachePurgeableBytes() const { |
| 257 | ASSERT_SINGLE_OWNER |
| 258 | return fResourceCache->getPurgeableBytes(); |
| 259 | } |
| 260 | |
| 261 | void GrDirectContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const { |
| 262 | ASSERT_SINGLE_OWNER |
| 263 | if (maxResources) { |
| 264 | *maxResources = -1; |
| 265 | } |
| 266 | if (maxResourceBytes) { |
| 267 | *maxResourceBytes = this->getResourceCacheLimit(); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | size_t GrDirectContext::getResourceCacheLimit() const { |
| 272 | ASSERT_SINGLE_OWNER |
| 273 | return fResourceCache->getMaxResourceBytes(); |
| 274 | } |
| 275 | |
| 276 | void GrDirectContext::setResourceCacheLimits(int unused, size_t maxResourceBytes) { |
| 277 | ASSERT_SINGLE_OWNER |
| 278 | this->setResourceCacheLimit(maxResourceBytes); |
| 279 | } |
| 280 | |
| 281 | void GrDirectContext::setResourceCacheLimit(size_t maxResourceBytes) { |
| 282 | ASSERT_SINGLE_OWNER |
| 283 | fResourceCache->setLimit(maxResourceBytes); |
| 284 | } |
| 285 | |
Adlai Holler | 4aa4c60 | 2020-10-12 13:58:52 -0400 | [diff] [blame] | 286 | void GrDirectContext::purgeUnlockedResources(bool scratchResourcesOnly) { |
| 287 | ASSERT_SINGLE_OWNER |
| 288 | |
| 289 | if (this->abandoned()) { |
| 290 | return; |
| 291 | } |
| 292 | |
| 293 | fResourceCache->purgeUnlockedResources(scratchResourcesOnly); |
| 294 | fResourceCache->purgeAsNeeded(); |
| 295 | |
| 296 | // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient |
| 297 | // place to purge stale blobs |
| 298 | this->getTextBlobCache()->purgeStaleBlobs(); |
| 299 | } |
| 300 | |
| 301 | void GrDirectContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) { |
| 302 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
| 303 | |
| 304 | ASSERT_SINGLE_OWNER |
| 305 | |
| 306 | if (this->abandoned()) { |
| 307 | return; |
| 308 | } |
| 309 | |
| 310 | this->checkAsyncWorkCompletion(); |
| 311 | fMappedBufferManager->process(); |
| 312 | auto purgeTime = GrStdSteadyClock::now() - msNotUsed; |
| 313 | |
| 314 | fResourceCache->purgeAsNeeded(); |
| 315 | fResourceCache->purgeResourcesNotUsedSince(purgeTime); |
| 316 | |
| 317 | if (auto ccpr = this->drawingManager()->getCoverageCountingPathRenderer()) { |
| 318 | ccpr->purgeCacheEntriesOlderThan(this->proxyProvider(), purgeTime); |
| 319 | } |
| 320 | |
| 321 | // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient |
| 322 | // place to purge stale blobs |
| 323 | this->getTextBlobCache()->purgeStaleBlobs(); |
| 324 | } |
| 325 | |
| 326 | void GrDirectContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) { |
| 327 | ASSERT_SINGLE_OWNER |
| 328 | |
| 329 | if (this->abandoned()) { |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources); |
| 334 | } |
| 335 | |
Adlai Holler | 3acc69a | 2020-10-13 08:20:51 -0400 | [diff] [blame] | 336 | //////////////////////////////////////////////////////////////////////////////// |
| 337 | bool GrDirectContext::wait(int numSemaphores, const GrBackendSemaphore waitSemaphores[], |
| 338 | bool deleteSemaphoresAfterWait) { |
| 339 | if (!fGpu || fGpu->caps()->semaphoreSupport()) { |
| 340 | return false; |
| 341 | } |
| 342 | GrWrapOwnership ownership = |
| 343 | deleteSemaphoresAfterWait ? kAdopt_GrWrapOwnership : kBorrow_GrWrapOwnership; |
| 344 | for (int i = 0; i < numSemaphores; ++i) { |
| 345 | std::unique_ptr<GrSemaphore> sema = fResourceProvider->wrapBackendSemaphore( |
| 346 | waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait, ownership); |
| 347 | // If we failed to wrap the semaphore it means the client didn't give us a valid semaphore |
| 348 | // to begin with. Therefore, it is fine to not wait on it. |
| 349 | if (sema) { |
| 350 | fGpu->waitSemaphore(sema.get()); |
| 351 | } |
| 352 | } |
| 353 | return true; |
| 354 | } |
Adlai Holler | 4aa4c60 | 2020-10-12 13:58:52 -0400 | [diff] [blame] | 355 | |
Robert Phillips | 5edf510 | 2020-08-10 16:30:36 -0400 | [diff] [blame] | 356 | GrSmallPathAtlasMgr* GrDirectContext::onGetSmallPathAtlasMgr() { |
Robert Phillips | 079455c | 2020-08-11 15:18:46 -0400 | [diff] [blame] | 357 | if (!fSmallPathAtlasMgr) { |
| 358 | fSmallPathAtlasMgr = std::make_unique<GrSmallPathAtlasMgr>(); |
| 359 | |
| 360 | this->priv().addOnFlushCallbackObject(fSmallPathAtlasMgr.get()); |
| 361 | } |
| 362 | |
| 363 | if (!fSmallPathAtlasMgr->initAtlas(this->proxyProvider(), this->caps())) { |
| 364 | return nullptr; |
| 365 | } |
| 366 | |
| 367 | return fSmallPathAtlasMgr.get(); |
Robert Phillips | 5edf510 | 2020-08-10 16:30:36 -0400 | [diff] [blame] | 368 | } |
| 369 | |
Adlai Holler | 3acc69a | 2020-10-13 08:20:51 -0400 | [diff] [blame] | 370 | //////////////////////////////////////////////////////////////////////////////// |
| 371 | |
| 372 | GrSemaphoresSubmitted GrDirectContext::flush(const GrFlushInfo& info) { |
| 373 | ASSERT_SINGLE_OWNER |
| 374 | if (this->abandoned()) { |
| 375 | if (info.fFinishedProc) { |
| 376 | info.fFinishedProc(info.fFinishedContext); |
| 377 | } |
| 378 | if (info.fSubmittedProc) { |
| 379 | info.fSubmittedProc(info.fSubmittedContext, false); |
| 380 | } |
| 381 | return GrSemaphoresSubmitted::kNo; |
| 382 | } |
| 383 | |
| 384 | bool flushed = this->drawingManager()->flush( |
| 385 | nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, info, nullptr); |
| 386 | |
| 387 | if (!flushed || (!this->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) { |
| 388 | return GrSemaphoresSubmitted::kNo; |
| 389 | } |
| 390 | return GrSemaphoresSubmitted::kYes; |
| 391 | } |
| 392 | |
| 393 | bool GrDirectContext::submit(bool syncCpu) { |
| 394 | ASSERT_SINGLE_OWNER |
| 395 | if (this->abandoned()) { |
| 396 | return false; |
| 397 | } |
| 398 | |
| 399 | if (!fGpu) { |
| 400 | return false; |
| 401 | } |
| 402 | |
| 403 | return fGpu->submitToGpu(syncCpu); |
| 404 | } |
| 405 | |
| 406 | //////////////////////////////////////////////////////////////////////////////// |
| 407 | |
| 408 | void GrDirectContext::checkAsyncWorkCompletion() { |
| 409 | if (fGpu) { |
| 410 | fGpu->checkFinishProcs(); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | //////////////////////////////////////////////////////////////////////////////// |
| 415 | |
| 416 | void GrDirectContext::storeVkPipelineCacheData() { |
| 417 | if (fGpu) { |
| 418 | fGpu->storeVkPipelineCacheData(); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | //////////////////////////////////////////////////////////////////////////////// |
| 423 | |
| 424 | bool GrDirectContext::supportsDistanceFieldText() const { |
| 425 | return this->caps()->shaderCaps()->supportsDistanceFieldText(); |
| 426 | } |
| 427 | |
| 428 | ////////////////////////////////////////////////////////////////////////////// |
| 429 | |
| 430 | void GrDirectContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const { |
| 431 | ASSERT_SINGLE_OWNER |
| 432 | fResourceCache->dumpMemoryStatistics(traceMemoryDump); |
| 433 | traceMemoryDump->dumpNumericValue("skia/gr_text_blob_cache", "size", "bytes", |
| 434 | this->getTextBlobCache()->usedBytes()); |
| 435 | } |
| 436 | |
| 437 | size_t GrDirectContext::ComputeImageSize(sk_sp<SkImage> image, GrMipmapped mipMapped, |
| 438 | bool useNextPow2) { |
| 439 | if (!image->isTextureBacked()) { |
| 440 | return 0; |
| 441 | } |
| 442 | SkImage_GpuBase* gpuImage = static_cast<SkImage_GpuBase*>(as_IB(image.get())); |
| 443 | GrTextureProxy* proxy = gpuImage->peekProxy(); |
| 444 | if (!proxy) { |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | int colorSamplesPerPixel = 1; |
| 449 | return GrSurface::ComputeSize(proxy->backendFormat(), image->dimensions(), |
| 450 | colorSamplesPerPixel, mipMapped, useNextPow2); |
| 451 | } |
| 452 | |
Adlai Holler | 98dd004 | 2020-10-13 10:04:00 -0400 | [diff] [blame] | 453 | GrBackendTexture GrDirectContext::createBackendTexture(int width, int height, |
| 454 | const GrBackendFormat& backendFormat, |
| 455 | GrMipmapped mipMapped, |
| 456 | GrRenderable renderable, |
| 457 | GrProtected isProtected) { |
| 458 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
| 459 | if (this->abandoned()) { |
| 460 | return GrBackendTexture(); |
| 461 | } |
| 462 | |
| 463 | return fGpu->createBackendTexture({width, height}, backendFormat, renderable, |
| 464 | mipMapped, isProtected); |
| 465 | } |
| 466 | |
| 467 | GrBackendTexture GrDirectContext::createBackendTexture(int width, int height, |
| 468 | SkColorType skColorType, |
| 469 | GrMipmapped mipMapped, |
| 470 | GrRenderable renderable, |
| 471 | GrProtected isProtected) { |
| 472 | if (this->abandoned()) { |
| 473 | return GrBackendTexture(); |
| 474 | } |
| 475 | |
| 476 | const GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable); |
| 477 | |
| 478 | return this->createBackendTexture(width, height, format, mipMapped, renderable, isProtected); |
| 479 | } |
| 480 | |
| 481 | static GrBackendTexture create_and_update_backend_texture( |
| 482 | GrDirectContext* dContext, |
| 483 | SkISize dimensions, |
| 484 | const GrBackendFormat& backendFormat, |
| 485 | GrMipmapped mipMapped, |
| 486 | GrRenderable renderable, |
| 487 | GrProtected isProtected, |
| 488 | sk_sp<GrRefCntedCallback> finishedCallback, |
| 489 | const GrGpu::BackendTextureData* data) { |
| 490 | GrGpu* gpu = dContext->priv().getGpu(); |
| 491 | |
| 492 | GrBackendTexture beTex = gpu->createBackendTexture(dimensions, backendFormat, renderable, |
| 493 | mipMapped, isProtected); |
| 494 | if (!beTex.isValid()) { |
| 495 | return {}; |
| 496 | } |
| 497 | |
| 498 | if (!dContext->priv().getGpu()->updateBackendTexture(beTex, |
| 499 | std::move(finishedCallback), |
| 500 | data)) { |
| 501 | dContext->deleteBackendTexture(beTex); |
| 502 | return {}; |
| 503 | } |
| 504 | return beTex; |
| 505 | } |
| 506 | |
| 507 | GrBackendTexture GrDirectContext::createBackendTexture(int width, int height, |
| 508 | const GrBackendFormat& backendFormat, |
| 509 | const SkColor4f& color, |
| 510 | GrMipmapped mipMapped, |
| 511 | GrRenderable renderable, |
| 512 | GrProtected isProtected, |
| 513 | GrGpuFinishedProc finishedProc, |
| 514 | GrGpuFinishedContext finishedContext) { |
| 515 | sk_sp<GrRefCntedCallback> finishedCallback; |
| 516 | if (finishedProc) { |
| 517 | finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext)); |
| 518 | } |
| 519 | |
| 520 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
| 521 | if (this->abandoned()) { |
| 522 | return {}; |
| 523 | } |
| 524 | |
| 525 | GrGpu::BackendTextureData data(color); |
| 526 | return create_and_update_backend_texture(this, {width, height}, |
| 527 | backendFormat, mipMapped, renderable, isProtected, |
| 528 | std::move(finishedCallback), &data); |
| 529 | } |
| 530 | |
| 531 | GrBackendTexture GrDirectContext::createBackendTexture(int width, int height, |
| 532 | SkColorType skColorType, |
| 533 | const SkColor4f& color, |
| 534 | GrMipmapped mipMapped, |
| 535 | GrRenderable renderable, |
| 536 | GrProtected isProtected, |
| 537 | GrGpuFinishedProc finishedProc, |
| 538 | GrGpuFinishedContext finishedContext) { |
| 539 | sk_sp<GrRefCntedCallback> finishedCallback; |
| 540 | if (finishedProc) { |
| 541 | finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext)); |
| 542 | } |
| 543 | |
| 544 | if (this->abandoned()) { |
| 545 | return {}; |
| 546 | } |
| 547 | |
| 548 | GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable); |
| 549 | if (!format.isValid()) { |
| 550 | return {}; |
| 551 | } |
| 552 | |
| 553 | GrColorType grColorType = SkColorTypeToGrColorType(skColorType); |
| 554 | SkColor4f swizzledColor = this->caps()->getWriteSwizzle(format, grColorType).applyTo(color); |
| 555 | |
| 556 | GrGpu::BackendTextureData data(swizzledColor); |
| 557 | return create_and_update_backend_texture(this, {width, height}, format, |
| 558 | mipMapped, renderable, isProtected, |
| 559 | std::move(finishedCallback), &data); |
| 560 | } |
| 561 | |
| 562 | GrBackendTexture GrDirectContext::createBackendTexture(const SkPixmap srcData[], |
| 563 | int numProvidedLevels, |
| 564 | GrRenderable renderable, |
| 565 | GrProtected isProtected, |
| 566 | GrGpuFinishedProc finishedProc, |
| 567 | GrGpuFinishedContext finishedContext) { |
| 568 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
| 569 | |
| 570 | sk_sp<GrRefCntedCallback> finishedCallback; |
| 571 | if (finishedProc) { |
| 572 | finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext)); |
| 573 | } |
| 574 | |
| 575 | if (this->abandoned()) { |
| 576 | return {}; |
| 577 | } |
| 578 | |
| 579 | if (!srcData || numProvidedLevels <= 0) { |
| 580 | return {}; |
| 581 | } |
| 582 | |
| 583 | int baseWidth = srcData[0].width(); |
| 584 | int baseHeight = srcData[0].height(); |
| 585 | SkColorType colorType = srcData[0].colorType(); |
| 586 | |
| 587 | GrMipmapped mipMapped = GrMipmapped::kNo; |
| 588 | int numExpectedLevels = 1; |
| 589 | if (numProvidedLevels > 1) { |
| 590 | numExpectedLevels = SkMipmap::ComputeLevelCount(baseWidth, baseHeight) + 1; |
| 591 | mipMapped = GrMipmapped::kYes; |
| 592 | } |
| 593 | |
| 594 | if (numProvidedLevels != numExpectedLevels) { |
| 595 | return {}; |
| 596 | } |
| 597 | |
| 598 | GrBackendFormat backendFormat = this->defaultBackendFormat(colorType, renderable); |
| 599 | |
| 600 | GrGpu::BackendTextureData data(srcData); |
| 601 | return create_and_update_backend_texture(this, {baseWidth, baseHeight}, |
| 602 | backendFormat, mipMapped, renderable, isProtected, |
| 603 | std::move(finishedCallback), &data); |
| 604 | } |
| 605 | |
Adlai Holler | 2e0c70d | 2020-10-13 08:21:37 -0400 | [diff] [blame] | 606 | bool GrDirectContext::updateBackendTexture(const GrBackendTexture& backendTexture, |
| 607 | const SkColor4f& color, |
| 608 | GrGpuFinishedProc finishedProc, |
| 609 | GrGpuFinishedContext finishedContext) { |
| 610 | sk_sp<GrRefCntedCallback> finishedCallback; |
| 611 | if (finishedProc) { |
| 612 | finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext)); |
| 613 | } |
| 614 | |
| 615 | if (this->abandoned()) { |
| 616 | return false; |
| 617 | } |
| 618 | |
| 619 | GrGpu::BackendTextureData data(color); |
| 620 | return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data); |
| 621 | } |
| 622 | |
| 623 | bool GrDirectContext::updateBackendTexture(const GrBackendTexture& backendTexture, |
| 624 | SkColorType skColorType, |
| 625 | const SkColor4f& color, |
| 626 | GrGpuFinishedProc finishedProc, |
| 627 | GrGpuFinishedContext finishedContext) { |
| 628 | sk_sp<GrRefCntedCallback> finishedCallback; |
| 629 | if (finishedProc) { |
| 630 | finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext)); |
| 631 | } |
| 632 | |
| 633 | if (this->abandoned()) { |
| 634 | return false; |
| 635 | } |
| 636 | |
| 637 | GrBackendFormat format = backendTexture.getBackendFormat(); |
| 638 | GrColorType grColorType = SkColorTypeAndFormatToGrColorType(this->caps(), skColorType, format); |
| 639 | |
| 640 | if (!this->caps()->areColorTypeAndFormatCompatible(grColorType, format)) { |
| 641 | return false; |
| 642 | } |
| 643 | |
| 644 | GrSwizzle swizzle = this->caps()->getWriteSwizzle(format, grColorType); |
| 645 | GrGpu::BackendTextureData data(swizzle.applyTo(color)); |
| 646 | |
| 647 | return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data); |
| 648 | } |
| 649 | |
| 650 | bool GrDirectContext::updateBackendTexture(const GrBackendTexture& backendTexture, |
| 651 | const SkPixmap srcData[], |
| 652 | int numLevels, |
| 653 | GrGpuFinishedProc finishedProc, |
| 654 | GrGpuFinishedContext finishedContext) { |
| 655 | sk_sp<GrRefCntedCallback> finishedCallback; |
| 656 | if (finishedProc) { |
| 657 | finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext)); |
| 658 | } |
| 659 | |
| 660 | if (this->abandoned()) { |
| 661 | return false; |
| 662 | } |
| 663 | |
| 664 | if (!srcData || numLevels <= 0) { |
| 665 | return false; |
| 666 | } |
| 667 | |
| 668 | int numExpectedLevels = 1; |
| 669 | if (backendTexture.hasMipmaps()) { |
| 670 | numExpectedLevels = SkMipmap::ComputeLevelCount(backendTexture.width(), |
| 671 | backendTexture.height()) + 1; |
| 672 | } |
| 673 | if (numLevels != numExpectedLevels) { |
| 674 | return false; |
| 675 | } |
| 676 | |
| 677 | GrGpu::BackendTextureData data(srcData); |
| 678 | return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data); |
| 679 | } |
| 680 | |
Adlai Holler | 64e1383 | 2020-10-13 08:21:56 -0400 | [diff] [blame] | 681 | ////////////////////////////////////////////////////////////////////////////// |
| 682 | |
| 683 | static GrBackendTexture create_and_update_compressed_backend_texture( |
| 684 | GrDirectContext* dContext, |
| 685 | SkISize dimensions, |
| 686 | const GrBackendFormat& backendFormat, |
| 687 | GrMipmapped mipMapped, |
| 688 | GrProtected isProtected, |
| 689 | sk_sp<GrRefCntedCallback> finishedCallback, |
| 690 | const GrGpu::BackendTextureData* data) { |
| 691 | GrGpu* gpu = dContext->priv().getGpu(); |
| 692 | |
| 693 | GrBackendTexture beTex = gpu->createCompressedBackendTexture(dimensions, backendFormat, |
| 694 | mipMapped, isProtected); |
| 695 | if (!beTex.isValid()) { |
| 696 | return {}; |
| 697 | } |
| 698 | |
| 699 | if (!dContext->priv().getGpu()->updateCompressedBackendTexture( |
| 700 | beTex, std::move(finishedCallback), data)) { |
| 701 | dContext->deleteBackendTexture(beTex); |
| 702 | return {}; |
| 703 | } |
| 704 | return beTex; |
| 705 | } |
| 706 | |
| 707 | GrBackendTexture GrDirectContext::createCompressedBackendTexture(int width, int height, |
| 708 | const GrBackendFormat& backendFormat, |
| 709 | const SkColor4f& color, |
| 710 | GrMipmapped mipMapped, |
| 711 | GrProtected isProtected, |
| 712 | GrGpuFinishedProc finishedProc, |
| 713 | GrGpuFinishedContext finishedContext) { |
| 714 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
| 715 | sk_sp<GrRefCntedCallback> finishedCallback; |
| 716 | if (finishedProc) { |
| 717 | finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext)); |
| 718 | } |
| 719 | |
| 720 | if (this->abandoned()) { |
| 721 | return {}; |
| 722 | } |
| 723 | |
| 724 | GrGpu::BackendTextureData data(color); |
| 725 | return create_and_update_compressed_backend_texture(this, {width, height}, |
| 726 | backendFormat, mipMapped, isProtected, |
| 727 | std::move(finishedCallback), &data); |
| 728 | } |
| 729 | |
| 730 | GrBackendTexture GrDirectContext::createCompressedBackendTexture(int width, int height, |
| 731 | SkImage::CompressionType compression, |
| 732 | const SkColor4f& color, |
| 733 | GrMipmapped mipMapped, |
| 734 | GrProtected isProtected, |
| 735 | GrGpuFinishedProc finishedProc, |
| 736 | GrGpuFinishedContext finishedContext) { |
| 737 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
| 738 | GrBackendFormat format = this->compressedBackendFormat(compression); |
| 739 | return this->createCompressedBackendTexture(width, height, format, color, |
| 740 | mipMapped, isProtected, finishedProc, |
| 741 | finishedContext); |
| 742 | } |
| 743 | |
| 744 | GrBackendTexture GrDirectContext::createCompressedBackendTexture(int width, int height, |
| 745 | const GrBackendFormat& backendFormat, |
| 746 | const void* compressedData, |
| 747 | size_t dataSize, |
| 748 | GrMipmapped mipMapped, |
| 749 | GrProtected isProtected, |
| 750 | GrGpuFinishedProc finishedProc, |
| 751 | GrGpuFinishedContext finishedContext) { |
| 752 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
| 753 | sk_sp<GrRefCntedCallback> finishedCallback; |
| 754 | if (finishedProc) { |
| 755 | finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext)); |
| 756 | } |
| 757 | |
| 758 | if (this->abandoned()) { |
| 759 | return {}; |
| 760 | } |
| 761 | |
| 762 | GrGpu::BackendTextureData data(compressedData, dataSize); |
| 763 | return create_and_update_compressed_backend_texture(this, {width, height}, |
| 764 | backendFormat, mipMapped, isProtected, |
| 765 | std::move(finishedCallback), &data); |
| 766 | } |
| 767 | |
| 768 | GrBackendTexture GrDirectContext::createCompressedBackendTexture(int width, int height, |
| 769 | SkImage::CompressionType compression, |
| 770 | const void* data, size_t dataSize, |
| 771 | GrMipmapped mipMapped, |
| 772 | GrProtected isProtected, |
| 773 | GrGpuFinishedProc finishedProc, |
| 774 | GrGpuFinishedContext finishedContext) { |
| 775 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
| 776 | GrBackendFormat format = this->compressedBackendFormat(compression); |
| 777 | return this->createCompressedBackendTexture(width, height, format, data, dataSize, mipMapped, |
| 778 | isProtected, finishedProc, finishedContext); |
| 779 | } |
| 780 | |
| 781 | bool GrDirectContext::updateCompressedBackendTexture(const GrBackendTexture& backendTexture, |
| 782 | const SkColor4f& color, |
| 783 | GrGpuFinishedProc finishedProc, |
| 784 | GrGpuFinishedContext finishedContext) { |
| 785 | sk_sp<GrRefCntedCallback> finishedCallback; |
| 786 | if (finishedProc) { |
| 787 | finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext)); |
| 788 | } |
| 789 | |
| 790 | if (this->abandoned()) { |
| 791 | return false; |
| 792 | } |
| 793 | |
| 794 | GrGpu::BackendTextureData data(color); |
| 795 | return fGpu->updateCompressedBackendTexture(backendTexture, std::move(finishedCallback), &data); |
| 796 | } |
| 797 | |
| 798 | bool GrDirectContext::updateCompressedBackendTexture(const GrBackendTexture& backendTexture, |
| 799 | const void* compressedData, |
| 800 | size_t dataSize, |
| 801 | GrGpuFinishedProc finishedProc, |
| 802 | GrGpuFinishedContext finishedContext) { |
| 803 | sk_sp<GrRefCntedCallback> finishedCallback; |
| 804 | if (finishedProc) { |
| 805 | finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext)); |
| 806 | } |
| 807 | |
| 808 | if (this->abandoned()) { |
| 809 | return false; |
| 810 | } |
| 811 | |
| 812 | if (!compressedData) { |
| 813 | return false; |
| 814 | } |
| 815 | |
| 816 | GrGpu::BackendTextureData data(compressedData, dataSize); |
| 817 | |
| 818 | return fGpu->updateCompressedBackendTexture(backendTexture, std::move(finishedCallback), &data); |
| 819 | } |
| 820 | |
Adlai Holler | 6d0745b | 2020-10-13 13:29:00 -0400 | [diff] [blame^] | 821 | ////////////////////////////////////////////////////////////////////////////// |
| 822 | |
| 823 | bool GrDirectContext::setBackendTextureState(const GrBackendTexture& backendTexture, |
| 824 | const GrBackendSurfaceMutableState& state, |
| 825 | GrBackendSurfaceMutableState* previousState, |
| 826 | GrGpuFinishedProc finishedProc, |
| 827 | GrGpuFinishedContext finishedContext) { |
| 828 | sk_sp<GrRefCntedCallback> callback; |
| 829 | if (finishedProc) { |
| 830 | callback.reset(new GrRefCntedCallback(finishedProc, finishedContext)); |
| 831 | } |
| 832 | |
| 833 | if (this->abandoned()) { |
| 834 | return false; |
| 835 | } |
| 836 | |
| 837 | return fGpu->setBackendTextureState(backendTexture, state, previousState, std::move(callback)); |
| 838 | } |
| 839 | |
| 840 | |
| 841 | bool GrDirectContext::setBackendRenderTargetState(const GrBackendRenderTarget& backendRenderTarget, |
| 842 | const GrBackendSurfaceMutableState& state, |
| 843 | GrBackendSurfaceMutableState* previousState, |
| 844 | GrGpuFinishedProc finishedProc, |
| 845 | GrGpuFinishedContext finishedContext) { |
| 846 | sk_sp<GrRefCntedCallback> callback; |
| 847 | if (finishedProc) { |
| 848 | callback.reset(new GrRefCntedCallback(finishedProc, finishedContext)); |
| 849 | } |
| 850 | |
| 851 | if (this->abandoned()) { |
| 852 | return false; |
| 853 | } |
| 854 | |
| 855 | return fGpu->setBackendRenderTargetState(backendRenderTarget, state, previousState, |
| 856 | std::move(callback)); |
| 857 | } |
| 858 | |
| 859 | void GrDirectContext::deleteBackendTexture(GrBackendTexture backendTex) { |
| 860 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
| 861 | // For the Vulkan backend we still must destroy the backend texture when the context is |
| 862 | // abandoned. |
| 863 | if ((this->abandoned() && this->backend() != GrBackendApi::kVulkan) || !backendTex.isValid()) { |
| 864 | return; |
| 865 | } |
| 866 | |
| 867 | fGpu->deleteBackendTexture(backendTex); |
| 868 | } |
| 869 | |
| 870 | ////////////////////////////////////////////////////////////////////////////// |
| 871 | |
| 872 | bool GrDirectContext::precompileShader(const SkData& key, const SkData& data) { |
| 873 | return fGpu->precompileShader(key, data); |
| 874 | } |
| 875 | |
| 876 | #ifdef SK_ENABLE_DUMP_GPU |
| 877 | #include "include/core/SkString.h" |
| 878 | #include "src/utils/SkJSONWriter.h" |
| 879 | SkString GrDirectContext::dump() const { |
| 880 | SkDynamicMemoryWStream stream; |
| 881 | SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty); |
| 882 | writer.beginObject(); |
| 883 | |
| 884 | writer.appendString("backend", GrBackendApiToStr(this->backend())); |
| 885 | |
| 886 | writer.appendName("caps"); |
| 887 | this->caps()->dumpJSON(&writer); |
| 888 | |
| 889 | writer.appendName("gpu"); |
| 890 | this->fGpu->dumpJSON(&writer); |
| 891 | |
| 892 | writer.appendName("context"); |
| 893 | this->dumpJSON(&writer); |
| 894 | |
| 895 | // Flush JSON to the memory stream |
| 896 | writer.endObject(); |
| 897 | writer.flush(); |
| 898 | |
| 899 | // Null terminate the JSON data in the memory stream |
| 900 | stream.write8(0); |
| 901 | |
| 902 | // Allocate a string big enough to hold all the data, then copy out of the stream |
| 903 | SkString result(stream.bytesWritten()); |
| 904 | stream.copyToAndReset(result.writable_str()); |
| 905 | return result; |
| 906 | } |
| 907 | #endif |
| 908 | |
John Rosasco | a9b348f | 2019-11-08 13:18:15 -0800 | [diff] [blame] | 909 | #ifdef SK_GL |
Robert Phillips | c7228c6 | 2020-07-14 12:57:39 -0400 | [diff] [blame] | 910 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 911 | /*************************************************************************************************/ |
| 912 | sk_sp<GrDirectContext> GrDirectContext::MakeGL(sk_sp<const GrGLInterface> glInterface) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 913 | GrContextOptions defaultOptions; |
Jim Van Verth | 03b8ab2 | 2020-02-24 11:36:15 -0500 | [diff] [blame] | 914 | return MakeGL(std::move(glInterface), defaultOptions); |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 915 | } |
| 916 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 917 | sk_sp<GrDirectContext> GrDirectContext::MakeGL(const GrContextOptions& options) { |
Brian Salomon | c1b9c10 | 2018-04-06 09:18:00 -0400 | [diff] [blame] | 918 | return MakeGL(nullptr, options); |
| 919 | } |
| 920 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 921 | sk_sp<GrDirectContext> GrDirectContext::MakeGL() { |
Brian Salomon | c1b9c10 | 2018-04-06 09:18:00 -0400 | [diff] [blame] | 922 | GrContextOptions defaultOptions; |
| 923 | return MakeGL(nullptr, defaultOptions); |
| 924 | } |
| 925 | |
Brian Salomon | 24069eb | 2020-06-24 10:19:52 -0400 | [diff] [blame] | 926 | #if GR_TEST_UTILS |
| 927 | GrGLFunction<GrGLGetErrorFn> make_get_error_with_random_oom(GrGLFunction<GrGLGetErrorFn> original) { |
| 928 | // A SkRandom and a GrGLFunction<GrGLGetErrorFn> are too big to be captured by a |
| 929 | // GrGLFunction<GrGLGetError> (surprise, surprise). So we make a context object and |
| 930 | // capture that by pointer. However, GrGLFunction doesn't support calling a destructor |
| 931 | // on the thing it captures. So we leak the context. |
| 932 | struct GetErrorContext { |
| 933 | SkRandom fRandom; |
| 934 | GrGLFunction<GrGLGetErrorFn> fGetError; |
| 935 | }; |
| 936 | |
| 937 | auto errorContext = new GetErrorContext; |
| 938 | |
| 939 | #if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS) |
| 940 | __lsan_ignore_object(errorContext); |
| 941 | #endif |
| 942 | |
| 943 | errorContext->fGetError = original; |
| 944 | |
| 945 | return GrGLFunction<GrGLGetErrorFn>([errorContext]() { |
| 946 | GrGLenum error = errorContext->fGetError(); |
| 947 | if (error == GR_GL_NO_ERROR && (errorContext->fRandom.nextU() % 300) == 0) { |
| 948 | error = GR_GL_OUT_OF_MEMORY; |
| 949 | } |
| 950 | return error; |
| 951 | }); |
| 952 | } |
| 953 | #endif |
| 954 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 955 | sk_sp<GrDirectContext> GrDirectContext::MakeGL(sk_sp<const GrGLInterface> glInterface, |
| 956 | const GrContextOptions& options) { |
| 957 | sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kOpenGL, options)); |
Brian Salomon | 24069eb | 2020-06-24 10:19:52 -0400 | [diff] [blame] | 958 | #if GR_TEST_UTILS |
| 959 | if (options.fRandomGLOOM) { |
| 960 | auto copy = sk_make_sp<GrGLInterface>(*glInterface); |
| 961 | copy->fFunctions.fGetError = |
| 962 | make_get_error_with_random_oom(glInterface->fFunctions.fGetError); |
| 963 | #if GR_GL_CHECK_ERROR |
| 964 | // Suppress logging GL errors since we'll be synthetically generating them. |
| 965 | copy->suppressErrorLogging(); |
| 966 | #endif |
| 967 | glInterface = std::move(copy); |
| 968 | } |
| 969 | #endif |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 970 | direct->fGpu = GrGLGpu::Make(std::move(glInterface), options, direct.get()); |
| 971 | if (!direct->init()) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 972 | return nullptr; |
| 973 | } |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 974 | return direct; |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 975 | } |
John Rosasco | a9b348f | 2019-11-08 13:18:15 -0800 | [diff] [blame] | 976 | #endif |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 977 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 978 | /*************************************************************************************************/ |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 979 | sk_sp<GrDirectContext> GrDirectContext::MakeMock(const GrMockOptions* mockOptions) { |
| 980 | GrContextOptions defaultOptions; |
| 981 | return MakeMock(mockOptions, defaultOptions); |
| 982 | } |
| 983 | |
| 984 | sk_sp<GrDirectContext> GrDirectContext::MakeMock(const GrMockOptions* mockOptions, |
| 985 | const GrContextOptions& options) { |
| 986 | sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kMock, options)); |
| 987 | |
| 988 | direct->fGpu = GrMockGpu::Make(mockOptions, options, direct.get()); |
| 989 | if (!direct->init()) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 990 | return nullptr; |
| 991 | } |
Chris Dalton | a378b45 | 2019-12-11 13:24:11 -0500 | [diff] [blame] | 992 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 993 | return direct; |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 994 | } |
| 995 | |
Greg Daniel | b4d8956 | 2018-10-03 18:44:49 +0000 | [diff] [blame] | 996 | #ifdef SK_VULKAN |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 997 | /*************************************************************************************************/ |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 998 | sk_sp<GrDirectContext> GrDirectContext::MakeVulkan(const GrVkBackendContext& backendContext) { |
| 999 | GrContextOptions defaultOptions; |
| 1000 | return MakeVulkan(backendContext, defaultOptions); |
| 1001 | } |
| 1002 | |
| 1003 | sk_sp<GrDirectContext> GrDirectContext::MakeVulkan(const GrVkBackendContext& backendContext, |
| 1004 | const GrContextOptions& options) { |
| 1005 | sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kVulkan, options)); |
| 1006 | |
| 1007 | direct->fGpu = GrVkGpu::Make(backendContext, options, direct.get()); |
| 1008 | if (!direct->init()) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 1009 | return nullptr; |
| 1010 | } |
| 1011 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1012 | return direct; |
Greg Daniel | b4d8956 | 2018-10-03 18:44:49 +0000 | [diff] [blame] | 1013 | } |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1014 | #endif |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 1015 | |
| 1016 | #ifdef SK_METAL |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1017 | /*************************************************************************************************/ |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1018 | sk_sp<GrDirectContext> GrDirectContext::MakeMetal(void* device, void* queue) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 1019 | GrContextOptions defaultOptions; |
| 1020 | return MakeMetal(device, queue, defaultOptions); |
| 1021 | } |
| 1022 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1023 | sk_sp<GrDirectContext> GrDirectContext::MakeMetal(void* device, void* queue, |
| 1024 | const GrContextOptions& options) { |
| 1025 | sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kMetal, options)); |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 1026 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1027 | direct->fGpu = GrMtlTrampoline::MakeGpu(direct.get(), options, device, queue); |
| 1028 | if (!direct->init()) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 1029 | return nullptr; |
| 1030 | } |
Timothy Liang | 4e85e80 | 2018-06-28 16:37:18 -0400 | [diff] [blame] | 1031 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1032 | return direct; |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 1033 | } |
| 1034 | #endif |
| 1035 | |
Jim Van Verth | b01e12b | 2020-02-18 14:34:38 -0500 | [diff] [blame] | 1036 | #ifdef SK_DIRECT3D |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1037 | /*************************************************************************************************/ |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1038 | sk_sp<GrDirectContext> GrDirectContext::MakeDirect3D(const GrD3DBackendContext& backendContext) { |
| 1039 | GrContextOptions defaultOptions; |
| 1040 | return MakeDirect3D(backendContext, defaultOptions); |
| 1041 | } |
| 1042 | |
| 1043 | sk_sp<GrDirectContext> GrDirectContext::MakeDirect3D(const GrD3DBackendContext& backendContext, |
| 1044 | const GrContextOptions& options) { |
| 1045 | sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kDirect3D, options)); |
| 1046 | |
| 1047 | direct->fGpu = GrD3DGpu::Make(backendContext, options, direct.get()); |
| 1048 | if (!direct->init()) { |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 1049 | return nullptr; |
| 1050 | } |
Jim Van Verth | b01e12b | 2020-02-18 14:34:38 -0500 | [diff] [blame] | 1051 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1052 | return direct; |
Jim Van Verth | b01e12b | 2020-02-18 14:34:38 -0500 | [diff] [blame] | 1053 | } |
| 1054 | #endif |
| 1055 | |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 1056 | #ifdef SK_DAWN |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1057 | /*************************************************************************************************/ |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1058 | sk_sp<GrDirectContext> GrDirectContext::MakeDawn(const wgpu::Device& device) { |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 1059 | GrContextOptions defaultOptions; |
| 1060 | return MakeDawn(device, defaultOptions); |
| 1061 | } |
| 1062 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1063 | sk_sp<GrDirectContext> GrDirectContext::MakeDawn(const wgpu::Device& device, |
| 1064 | const GrContextOptions& options) { |
| 1065 | sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kDawn, options)); |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 1066 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1067 | direct->fGpu = GrDawnGpu::Make(device, options, direct.get()); |
| 1068 | if (!direct->init()) { |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 1069 | return nullptr; |
| 1070 | } |
| 1071 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1072 | return direct; |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 1073 | } |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1074 | |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 1075 | #endif |