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