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