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