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()); |
Adlai Holler | b34270e | 2021-04-16 11:23:52 -0400 | [diff] [blame] | 219 | #if GR_TEST_UTILS |
| 220 | if (this->options().fResourceCacheLimitOverride != -1) { |
| 221 | this->setResourceCacheLimit(this->options().fResourceCacheLimitOverride); |
| 222 | } |
| 223 | #endif |
Adlai Holler | 9555f29 | 2020-10-09 09:41:14 -0400 | [diff] [blame] | 224 | fResourceProvider = std::make_unique<GrResourceProvider>(fGpu.get(), fResourceCache.get(), |
| 225 | this->singleOwner()); |
Robert Phillips | 82ad7af | 2021-03-11 16:00:10 -0500 | [diff] [blame] | 226 | fMappedBufferManager = std::make_unique<GrClientMappedBufferManager>(this->directContextID()); |
Adlai Holler | 9555f29 | 2020-10-09 09:41:14 -0400 | [diff] [blame] | 227 | |
| 228 | fDidTestPMConversions = false; |
| 229 | |
| 230 | // DDL TODO: we need to think through how the task group & persistent cache |
| 231 | // get passed on to/shared between all the DDLRecorders created with this context. |
| 232 | if (this->options().fExecutor) { |
| 233 | fTaskGroup = std::make_unique<SkTaskGroup>(*this->options().fExecutor); |
| 234 | } |
| 235 | |
| 236 | fPersistentCache = this->options().fPersistentCache; |
| 237 | fShaderErrorHandler = this->options().fShaderErrorHandler; |
| 238 | if (!fShaderErrorHandler) { |
| 239 | fShaderErrorHandler = GrShaderUtils::DefaultShaderErrorHandler(); |
| 240 | } |
| 241 | |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 242 | GrDrawOpAtlas::AllowMultitexturing allowMultitexturing; |
| 243 | if (GrContextOptions::Enable::kNo == this->options().fAllowMultipleGlyphCacheTextures || |
| 244 | // multitexturing supported only if range can represent the index + texcoords fully |
| 245 | !(this->caps()->shaderCaps()->floatIs32Bits() || |
| 246 | this->caps()->shaderCaps()->integerSupport())) { |
| 247 | allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo; |
| 248 | } else { |
| 249 | allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes; |
| 250 | } |
| 251 | |
| 252 | GrProxyProvider* proxyProvider = this->priv().proxyProvider(); |
| 253 | |
Robert Phillips | 3262bc8 | 2020-08-10 12:11:58 -0400 | [diff] [blame] | 254 | fAtlasManager = std::make_unique<GrAtlasManager>(proxyProvider, |
| 255 | this->options().fGlyphCacheTextureMaximumBytes, |
| 256 | allowMultitexturing); |
| 257 | this->priv().addOnFlushCallbackObject(fAtlasManager.get()); |
Robert Phillips | ad24845 | 2020-06-30 09:27:52 -0400 | [diff] [blame] | 258 | |
| 259 | return true; |
| 260 | } |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 261 | |
Adlai Holler | 3a508e9 | 2020-10-12 13:58:01 -0400 | [diff] [blame] | 262 | void GrDirectContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const { |
| 263 | ASSERT_SINGLE_OWNER |
| 264 | |
| 265 | if (resourceCount) { |
| 266 | *resourceCount = fResourceCache->getBudgetedResourceCount(); |
| 267 | } |
| 268 | if (resourceBytes) { |
| 269 | *resourceBytes = fResourceCache->getBudgetedResourceBytes(); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | size_t GrDirectContext::getResourceCachePurgeableBytes() const { |
| 274 | ASSERT_SINGLE_OWNER |
| 275 | return fResourceCache->getPurgeableBytes(); |
| 276 | } |
| 277 | |
| 278 | void GrDirectContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const { |
| 279 | ASSERT_SINGLE_OWNER |
| 280 | if (maxResources) { |
| 281 | *maxResources = -1; |
| 282 | } |
| 283 | if (maxResourceBytes) { |
| 284 | *maxResourceBytes = this->getResourceCacheLimit(); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | size_t GrDirectContext::getResourceCacheLimit() const { |
| 289 | ASSERT_SINGLE_OWNER |
| 290 | return fResourceCache->getMaxResourceBytes(); |
| 291 | } |
| 292 | |
| 293 | void GrDirectContext::setResourceCacheLimits(int unused, size_t maxResourceBytes) { |
| 294 | ASSERT_SINGLE_OWNER |
| 295 | this->setResourceCacheLimit(maxResourceBytes); |
| 296 | } |
| 297 | |
| 298 | void GrDirectContext::setResourceCacheLimit(size_t maxResourceBytes) { |
| 299 | ASSERT_SINGLE_OWNER |
| 300 | fResourceCache->setLimit(maxResourceBytes); |
| 301 | } |
| 302 | |
Adlai Holler | 4aa4c60 | 2020-10-12 13:58:52 -0400 | [diff] [blame] | 303 | void GrDirectContext::purgeUnlockedResources(bool scratchResourcesOnly) { |
| 304 | ASSERT_SINGLE_OWNER |
| 305 | |
| 306 | if (this->abandoned()) { |
| 307 | return; |
| 308 | } |
| 309 | |
| 310 | fResourceCache->purgeUnlockedResources(scratchResourcesOnly); |
| 311 | fResourceCache->purgeAsNeeded(); |
| 312 | |
| 313 | // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient |
| 314 | // place to purge stale blobs |
| 315 | this->getTextBlobCache()->purgeStaleBlobs(); |
Greg Daniel | 428523f | 2021-03-30 14:22:54 -0400 | [diff] [blame] | 316 | |
| 317 | fGpu->releaseUnlockedBackendObjects(); |
Adlai Holler | 4aa4c60 | 2020-10-12 13:58:52 -0400 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | void GrDirectContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) { |
| 321 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
| 322 | |
| 323 | ASSERT_SINGLE_OWNER |
| 324 | |
| 325 | if (this->abandoned()) { |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | this->checkAsyncWorkCompletion(); |
| 330 | fMappedBufferManager->process(); |
| 331 | auto purgeTime = GrStdSteadyClock::now() - msNotUsed; |
| 332 | |
| 333 | fResourceCache->purgeAsNeeded(); |
| 334 | fResourceCache->purgeResourcesNotUsedSince(purgeTime); |
| 335 | |
Adlai Holler | 4aa4c60 | 2020-10-12 13:58:52 -0400 | [diff] [blame] | 336 | // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient |
| 337 | // place to purge stale blobs |
| 338 | this->getTextBlobCache()->purgeStaleBlobs(); |
| 339 | } |
| 340 | |
| 341 | void GrDirectContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) { |
| 342 | ASSERT_SINGLE_OWNER |
| 343 | |
| 344 | if (this->abandoned()) { |
| 345 | return; |
| 346 | } |
| 347 | |
| 348 | fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources); |
| 349 | } |
| 350 | |
Adlai Holler | 3acc69a | 2020-10-13 08:20:51 -0400 | [diff] [blame] | 351 | //////////////////////////////////////////////////////////////////////////////// |
| 352 | bool GrDirectContext::wait(int numSemaphores, const GrBackendSemaphore waitSemaphores[], |
| 353 | bool deleteSemaphoresAfterWait) { |
Greg Daniel | 063fdce | 2021-05-06 19:45:55 +0000 | [diff] [blame^] | 354 | if (!fGpu || !fGpu->caps()->semaphoreSupport()) { |
Adlai Holler | 3acc69a | 2020-10-13 08:20:51 -0400 | [diff] [blame] | 355 | return false; |
| 356 | } |
| 357 | GrWrapOwnership ownership = |
| 358 | deleteSemaphoresAfterWait ? kAdopt_GrWrapOwnership : kBorrow_GrWrapOwnership; |
| 359 | for (int i = 0; i < numSemaphores; ++i) { |
| 360 | std::unique_ptr<GrSemaphore> sema = fResourceProvider->wrapBackendSemaphore( |
| 361 | waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait, ownership); |
| 362 | // If we failed to wrap the semaphore it means the client didn't give us a valid semaphore |
| 363 | // to begin with. Therefore, it is fine to not wait on it. |
| 364 | if (sema) { |
| 365 | fGpu->waitSemaphore(sema.get()); |
| 366 | } |
| 367 | } |
| 368 | return true; |
| 369 | } |
Adlai Holler | 4aa4c60 | 2020-10-12 13:58:52 -0400 | [diff] [blame] | 370 | |
Robert Phillips | 5edf510 | 2020-08-10 16:30:36 -0400 | [diff] [blame] | 371 | GrSmallPathAtlasMgr* GrDirectContext::onGetSmallPathAtlasMgr() { |
Robert Phillips | 079455c | 2020-08-11 15:18:46 -0400 | [diff] [blame] | 372 | if (!fSmallPathAtlasMgr) { |
| 373 | fSmallPathAtlasMgr = std::make_unique<GrSmallPathAtlasMgr>(); |
| 374 | |
| 375 | this->priv().addOnFlushCallbackObject(fSmallPathAtlasMgr.get()); |
| 376 | } |
| 377 | |
| 378 | if (!fSmallPathAtlasMgr->initAtlas(this->proxyProvider(), this->caps())) { |
| 379 | return nullptr; |
| 380 | } |
| 381 | |
| 382 | return fSmallPathAtlasMgr.get(); |
Robert Phillips | 5edf510 | 2020-08-10 16:30:36 -0400 | [diff] [blame] | 383 | } |
| 384 | |
Adlai Holler | 3acc69a | 2020-10-13 08:20:51 -0400 | [diff] [blame] | 385 | //////////////////////////////////////////////////////////////////////////////// |
| 386 | |
| 387 | GrSemaphoresSubmitted GrDirectContext::flush(const GrFlushInfo& info) { |
| 388 | ASSERT_SINGLE_OWNER |
| 389 | if (this->abandoned()) { |
| 390 | if (info.fFinishedProc) { |
| 391 | info.fFinishedProc(info.fFinishedContext); |
| 392 | } |
| 393 | if (info.fSubmittedProc) { |
| 394 | info.fSubmittedProc(info.fSubmittedContext, false); |
| 395 | } |
| 396 | return GrSemaphoresSubmitted::kNo; |
| 397 | } |
| 398 | |
Robert Phillips | 80bfda8 | 2020-11-12 09:23:36 -0500 | [diff] [blame] | 399 | return this->drawingManager()->flushSurfaces({}, SkSurface::BackendSurfaceAccess::kNoAccess, |
| 400 | info, nullptr); |
Adlai Holler | 3acc69a | 2020-10-13 08:20:51 -0400 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | bool GrDirectContext::submit(bool syncCpu) { |
| 404 | ASSERT_SINGLE_OWNER |
| 405 | if (this->abandoned()) { |
| 406 | return false; |
| 407 | } |
| 408 | |
| 409 | if (!fGpu) { |
| 410 | return false; |
| 411 | } |
| 412 | |
| 413 | return fGpu->submitToGpu(syncCpu); |
| 414 | } |
| 415 | |
| 416 | //////////////////////////////////////////////////////////////////////////////// |
| 417 | |
| 418 | void GrDirectContext::checkAsyncWorkCompletion() { |
| 419 | if (fGpu) { |
| 420 | fGpu->checkFinishProcs(); |
| 421 | } |
| 422 | } |
| 423 | |
Greg Daniel | a89b430 | 2021-01-29 10:48:40 -0500 | [diff] [blame] | 424 | void GrDirectContext::syncAllOutstandingGpuWork(bool shouldExecuteWhileAbandoned) { |
| 425 | if (fGpu && (!this->abandoned() || shouldExecuteWhileAbandoned)) { |
| 426 | fGpu->finishOutstandingGpuWork(); |
| 427 | this->checkAsyncWorkCompletion(); |
| 428 | } |
| 429 | } |
| 430 | |
Adlai Holler | 3acc69a | 2020-10-13 08:20:51 -0400 | [diff] [blame] | 431 | //////////////////////////////////////////////////////////////////////////////// |
| 432 | |
| 433 | void GrDirectContext::storeVkPipelineCacheData() { |
| 434 | if (fGpu) { |
| 435 | fGpu->storeVkPipelineCacheData(); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | //////////////////////////////////////////////////////////////////////////////// |
| 440 | |
| 441 | bool GrDirectContext::supportsDistanceFieldText() const { |
| 442 | return this->caps()->shaderCaps()->supportsDistanceFieldText(); |
| 443 | } |
| 444 | |
| 445 | ////////////////////////////////////////////////////////////////////////////// |
| 446 | |
| 447 | void GrDirectContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const { |
| 448 | ASSERT_SINGLE_OWNER |
| 449 | fResourceCache->dumpMemoryStatistics(traceMemoryDump); |
| 450 | traceMemoryDump->dumpNumericValue("skia/gr_text_blob_cache", "size", "bytes", |
| 451 | this->getTextBlobCache()->usedBytes()); |
| 452 | } |
| 453 | |
Adlai Holler | 98dd004 | 2020-10-13 10:04:00 -0400 | [diff] [blame] | 454 | GrBackendTexture GrDirectContext::createBackendTexture(int width, int height, |
| 455 | const GrBackendFormat& backendFormat, |
| 456 | GrMipmapped mipMapped, |
| 457 | GrRenderable renderable, |
| 458 | GrProtected isProtected) { |
| 459 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
| 460 | if (this->abandoned()) { |
| 461 | return GrBackendTexture(); |
| 462 | } |
| 463 | |
| 464 | return fGpu->createBackendTexture({width, height}, backendFormat, renderable, |
| 465 | mipMapped, isProtected); |
| 466 | } |
| 467 | |
| 468 | GrBackendTexture GrDirectContext::createBackendTexture(int width, int height, |
| 469 | SkColorType skColorType, |
| 470 | GrMipmapped mipMapped, |
| 471 | GrRenderable renderable, |
| 472 | GrProtected isProtected) { |
| 473 | if (this->abandoned()) { |
| 474 | return GrBackendTexture(); |
| 475 | } |
| 476 | |
| 477 | const GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable); |
| 478 | |
| 479 | return this->createBackendTexture(width, height, format, mipMapped, renderable, isProtected); |
| 480 | } |
| 481 | |
Brian Salomon | 7128323 | 2021-04-08 12:45:58 -0400 | [diff] [blame] | 482 | static GrBackendTexture create_and_clear_backend_texture(GrDirectContext* dContext, |
| 483 | SkISize dimensions, |
| 484 | const GrBackendFormat& backendFormat, |
| 485 | GrMipmapped mipMapped, |
| 486 | GrRenderable renderable, |
| 487 | GrProtected isProtected, |
| 488 | sk_sp<GrRefCntedCallback> finishedCallback, |
| 489 | std::array<float, 4> color) { |
Adlai Holler | 98dd004 | 2020-10-13 10:04:00 -0400 | [diff] [blame] | 490 | GrGpu* gpu = dContext->priv().getGpu(); |
Adlai Holler | 98dd004 | 2020-10-13 10:04:00 -0400 | [diff] [blame] | 491 | GrBackendTexture beTex = gpu->createBackendTexture(dimensions, backendFormat, renderable, |
| 492 | mipMapped, isProtected); |
| 493 | if (!beTex.isValid()) { |
| 494 | return {}; |
| 495 | } |
| 496 | |
Brian Salomon | 7128323 | 2021-04-08 12:45:58 -0400 | [diff] [blame] | 497 | if (!dContext->priv().getGpu()->clearBackendTexture(beTex, |
| 498 | std::move(finishedCallback), |
| 499 | color)) { |
Adlai Holler | 98dd004 | 2020-10-13 10:04:00 -0400 | [diff] [blame] | 500 | dContext->deleteBackendTexture(beTex); |
| 501 | return {}; |
| 502 | } |
| 503 | return beTex; |
| 504 | } |
| 505 | |
Brian Salomon | ea1d39b | 2021-04-01 17:06:52 -0400 | [diff] [blame] | 506 | static bool update_texture_with_pixmaps(GrDirectContext* context, |
| 507 | const SkPixmap src[], |
Brian Salomon | b5f880a | 2020-12-07 11:30:16 -0500 | [diff] [blame] | 508 | int numLevels, |
| 509 | const GrBackendTexture& backendTexture, |
| 510 | GrSurfaceOrigin textureOrigin, |
| 511 | sk_sp<GrRefCntedCallback> finishedCallback) { |
Brian Salomon | ea1d39b | 2021-04-01 17:06:52 -0400 | [diff] [blame] | 512 | GrColorType ct = SkColorTypeToGrColorType(src[0].colorType()); |
| 513 | const GrBackendFormat& format = backendTexture.getBackendFormat(); |
Brian Salomon | 759217e | 2021-01-31 13:16:39 -0500 | [diff] [blame] | 514 | |
Brian Salomon | ea1d39b | 2021-04-01 17:06:52 -0400 | [diff] [blame] | 515 | if (!context->priv().caps()->areColorTypeAndFormatCompatible(ct, format)) { |
| 516 | return false; |
| 517 | } |
| 518 | |
| 519 | auto proxy = context->priv().proxyProvider()->wrapBackendTexture(backendTexture, |
| 520 | kBorrow_GrWrapOwnership, |
| 521 | GrWrapCacheable::kNo, |
| 522 | kRW_GrIOType, |
| 523 | std::move(finishedCallback)); |
| 524 | if (!proxy) { |
| 525 | return false; |
| 526 | } |
| 527 | |
| 528 | GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(format, ct); |
| 529 | GrSurfaceProxyView view(std::move(proxy), textureOrigin, swizzle); |
| 530 | GrSurfaceContext surfaceContext(context, std::move(view), src[0].info().colorInfo()); |
| 531 | SkAutoSTArray<15, GrCPixmap> tmpSrc(numLevels); |
Brian Salomon | 759217e | 2021-01-31 13:16:39 -0500 | [diff] [blame] | 532 | for (int i = 0; i < numLevels; ++i) { |
Brian Salomon | ea1d39b | 2021-04-01 17:06:52 -0400 | [diff] [blame] | 533 | tmpSrc[i] = src[i]; |
| 534 | } |
Brian Salomon | 75ee737 | 2021-04-06 15:04:35 -0400 | [diff] [blame] | 535 | if (!surfaceContext.writePixels(context, tmpSrc.get(), numLevels)) { |
Brian Salomon | ea1d39b | 2021-04-01 17:06:52 -0400 | [diff] [blame] | 536 | return false; |
Brian Salomon | 759217e | 2021-01-31 13:16:39 -0500 | [diff] [blame] | 537 | } |
| 538 | |
Brian Salomon | ea1d39b | 2021-04-01 17:06:52 -0400 | [diff] [blame] | 539 | GrSurfaceProxy* p = surfaceContext.asSurfaceProxy(); |
| 540 | GrFlushInfo info; |
| 541 | context->priv().drawingManager()->flushSurfaces({&p, 1}, |
| 542 | SkSurface::BackendSurfaceAccess::kNoAccess, |
| 543 | info, |
| 544 | nullptr); |
| 545 | return true; |
Brian Salomon | b5f880a | 2020-12-07 11:30:16 -0500 | [diff] [blame] | 546 | } |
| 547 | |
Adlai Holler | 98dd004 | 2020-10-13 10:04:00 -0400 | [diff] [blame] | 548 | GrBackendTexture GrDirectContext::createBackendTexture(int width, int height, |
| 549 | const GrBackendFormat& backendFormat, |
| 550 | const SkColor4f& color, |
| 551 | GrMipmapped mipMapped, |
| 552 | GrRenderable renderable, |
| 553 | GrProtected isProtected, |
| 554 | GrGpuFinishedProc finishedProc, |
| 555 | GrGpuFinishedContext finishedContext) { |
Brian Salomon | 694ff17 | 2020-11-04 16:54:28 -0500 | [diff] [blame] | 556 | auto finishedCallback = GrRefCntedCallback::Make(finishedProc, finishedContext); |
Adlai Holler | 98dd004 | 2020-10-13 10:04:00 -0400 | [diff] [blame] | 557 | |
| 558 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
| 559 | if (this->abandoned()) { |
| 560 | return {}; |
| 561 | } |
| 562 | |
Brian Salomon | 7128323 | 2021-04-08 12:45:58 -0400 | [diff] [blame] | 563 | return create_and_clear_backend_texture(this, |
| 564 | {width, height}, |
| 565 | backendFormat, |
| 566 | mipMapped, |
| 567 | renderable, |
| 568 | isProtected, |
| 569 | std::move(finishedCallback), |
| 570 | color.array()); |
Adlai Holler | 98dd004 | 2020-10-13 10:04:00 -0400 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | GrBackendTexture GrDirectContext::createBackendTexture(int width, int height, |
| 574 | SkColorType skColorType, |
| 575 | const SkColor4f& color, |
| 576 | GrMipmapped mipMapped, |
| 577 | GrRenderable renderable, |
| 578 | GrProtected isProtected, |
| 579 | GrGpuFinishedProc finishedProc, |
| 580 | GrGpuFinishedContext finishedContext) { |
Brian Salomon | 694ff17 | 2020-11-04 16:54:28 -0500 | [diff] [blame] | 581 | auto finishedCallback = GrRefCntedCallback::Make(finishedProc, finishedContext); |
Adlai Holler | 98dd004 | 2020-10-13 10:04:00 -0400 | [diff] [blame] | 582 | |
| 583 | if (this->abandoned()) { |
| 584 | return {}; |
| 585 | } |
| 586 | |
| 587 | GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable); |
| 588 | if (!format.isValid()) { |
| 589 | return {}; |
| 590 | } |
| 591 | |
| 592 | GrColorType grColorType = SkColorTypeToGrColorType(skColorType); |
| 593 | SkColor4f swizzledColor = this->caps()->getWriteSwizzle(format, grColorType).applyTo(color); |
| 594 | |
Brian Salomon | 7128323 | 2021-04-08 12:45:58 -0400 | [diff] [blame] | 595 | return create_and_clear_backend_texture(this, |
| 596 | {width, height}, |
| 597 | format, |
| 598 | mipMapped, |
| 599 | renderable, |
| 600 | isProtected, |
| 601 | std::move(finishedCallback), |
| 602 | swizzledColor.array()); |
Adlai Holler | 98dd004 | 2020-10-13 10:04:00 -0400 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | GrBackendTexture GrDirectContext::createBackendTexture(const SkPixmap srcData[], |
| 606 | int numProvidedLevels, |
Brian Salomon | b5f880a | 2020-12-07 11:30:16 -0500 | [diff] [blame] | 607 | GrSurfaceOrigin textureOrigin, |
Adlai Holler | 98dd004 | 2020-10-13 10:04:00 -0400 | [diff] [blame] | 608 | GrRenderable renderable, |
| 609 | GrProtected isProtected, |
| 610 | GrGpuFinishedProc finishedProc, |
| 611 | GrGpuFinishedContext finishedContext) { |
| 612 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
| 613 | |
Brian Salomon | 694ff17 | 2020-11-04 16:54:28 -0500 | [diff] [blame] | 614 | auto finishedCallback = GrRefCntedCallback::Make(finishedProc, finishedContext); |
Adlai Holler | 98dd004 | 2020-10-13 10:04:00 -0400 | [diff] [blame] | 615 | |
| 616 | if (this->abandoned()) { |
| 617 | return {}; |
| 618 | } |
| 619 | |
| 620 | if (!srcData || numProvidedLevels <= 0) { |
| 621 | return {}; |
| 622 | } |
| 623 | |
Adlai Holler | 98dd004 | 2020-10-13 10:04:00 -0400 | [diff] [blame] | 624 | SkColorType colorType = srcData[0].colorType(); |
| 625 | |
| 626 | GrMipmapped mipMapped = GrMipmapped::kNo; |
Adlai Holler | 98dd004 | 2020-10-13 10:04:00 -0400 | [diff] [blame] | 627 | if (numProvidedLevels > 1) { |
Adlai Holler | 98dd004 | 2020-10-13 10:04:00 -0400 | [diff] [blame] | 628 | mipMapped = GrMipmapped::kYes; |
| 629 | } |
| 630 | |
Adlai Holler | 98dd004 | 2020-10-13 10:04:00 -0400 | [diff] [blame] | 631 | GrBackendFormat backendFormat = this->defaultBackendFormat(colorType, renderable); |
Brian Salomon | b5f880a | 2020-12-07 11:30:16 -0500 | [diff] [blame] | 632 | GrBackendTexture beTex = this->createBackendTexture(srcData[0].width(), |
| 633 | srcData[0].height(), |
| 634 | backendFormat, |
| 635 | mipMapped, |
| 636 | renderable, |
| 637 | isProtected); |
| 638 | if (!beTex.isValid()) { |
| 639 | return {}; |
| 640 | } |
Brian Salomon | ea1d39b | 2021-04-01 17:06:52 -0400 | [diff] [blame] | 641 | if (!update_texture_with_pixmaps(this, |
Brian Salomon | b5f880a | 2020-12-07 11:30:16 -0500 | [diff] [blame] | 642 | srcData, |
| 643 | numProvidedLevels, |
| 644 | beTex, |
| 645 | textureOrigin, |
| 646 | std::move(finishedCallback))) { |
| 647 | this->deleteBackendTexture(beTex); |
| 648 | return {}; |
| 649 | } |
| 650 | return beTex; |
Adlai Holler | 98dd004 | 2020-10-13 10:04:00 -0400 | [diff] [blame] | 651 | } |
| 652 | |
Adlai Holler | 2e0c70d | 2020-10-13 08:21:37 -0400 | [diff] [blame] | 653 | bool GrDirectContext::updateBackendTexture(const GrBackendTexture& backendTexture, |
| 654 | const SkColor4f& color, |
| 655 | GrGpuFinishedProc finishedProc, |
| 656 | GrGpuFinishedContext finishedContext) { |
Brian Salomon | 694ff17 | 2020-11-04 16:54:28 -0500 | [diff] [blame] | 657 | auto finishedCallback = GrRefCntedCallback::Make(finishedProc, finishedContext); |
Adlai Holler | 2e0c70d | 2020-10-13 08:21:37 -0400 | [diff] [blame] | 658 | |
| 659 | if (this->abandoned()) { |
| 660 | return false; |
| 661 | } |
| 662 | |
Brian Salomon | 7128323 | 2021-04-08 12:45:58 -0400 | [diff] [blame] | 663 | return fGpu->clearBackendTexture(backendTexture, std::move(finishedCallback), color.array()); |
Adlai Holler | 2e0c70d | 2020-10-13 08:21:37 -0400 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | bool GrDirectContext::updateBackendTexture(const GrBackendTexture& backendTexture, |
| 667 | SkColorType skColorType, |
| 668 | const SkColor4f& color, |
| 669 | GrGpuFinishedProc finishedProc, |
| 670 | GrGpuFinishedContext finishedContext) { |
Brian Salomon | 694ff17 | 2020-11-04 16:54:28 -0500 | [diff] [blame] | 671 | auto finishedCallback = GrRefCntedCallback::Make(finishedProc, finishedContext); |
Adlai Holler | 2e0c70d | 2020-10-13 08:21:37 -0400 | [diff] [blame] | 672 | |
| 673 | if (this->abandoned()) { |
| 674 | return false; |
| 675 | } |
| 676 | |
| 677 | GrBackendFormat format = backendTexture.getBackendFormat(); |
| 678 | GrColorType grColorType = SkColorTypeAndFormatToGrColorType(this->caps(), skColorType, format); |
| 679 | |
| 680 | if (!this->caps()->areColorTypeAndFormatCompatible(grColorType, format)) { |
| 681 | return false; |
| 682 | } |
| 683 | |
| 684 | GrSwizzle swizzle = this->caps()->getWriteSwizzle(format, grColorType); |
Brian Salomon | 7128323 | 2021-04-08 12:45:58 -0400 | [diff] [blame] | 685 | SkColor4f swizzledColor = swizzle.applyTo(color); |
Adlai Holler | 2e0c70d | 2020-10-13 08:21:37 -0400 | [diff] [blame] | 686 | |
Brian Salomon | 7128323 | 2021-04-08 12:45:58 -0400 | [diff] [blame] | 687 | return fGpu->clearBackendTexture(backendTexture, |
| 688 | std::move(finishedCallback), |
| 689 | swizzledColor.array()); |
Adlai Holler | 2e0c70d | 2020-10-13 08:21:37 -0400 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | bool GrDirectContext::updateBackendTexture(const GrBackendTexture& backendTexture, |
| 693 | const SkPixmap srcData[], |
| 694 | int numLevels, |
Brian Salomon | b5f880a | 2020-12-07 11:30:16 -0500 | [diff] [blame] | 695 | GrSurfaceOrigin textureOrigin, |
Adlai Holler | 2e0c70d | 2020-10-13 08:21:37 -0400 | [diff] [blame] | 696 | GrGpuFinishedProc finishedProc, |
| 697 | GrGpuFinishedContext finishedContext) { |
Brian Salomon | 694ff17 | 2020-11-04 16:54:28 -0500 | [diff] [blame] | 698 | auto finishedCallback = GrRefCntedCallback::Make(finishedProc, finishedContext); |
Adlai Holler | 2e0c70d | 2020-10-13 08:21:37 -0400 | [diff] [blame] | 699 | |
| 700 | if (this->abandoned()) { |
| 701 | return false; |
| 702 | } |
| 703 | |
| 704 | if (!srcData || numLevels <= 0) { |
| 705 | return false; |
| 706 | } |
| 707 | |
Brian Salomon | ea1d39b | 2021-04-01 17:06:52 -0400 | [diff] [blame] | 708 | // 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] | 709 | int numExpectedLevels = 1; |
| 710 | if (backendTexture.hasMipmaps()) { |
| 711 | numExpectedLevels = SkMipmap::ComputeLevelCount(backendTexture.width(), |
| 712 | backendTexture.height()) + 1; |
| 713 | } |
| 714 | if (numLevels != numExpectedLevels) { |
| 715 | return false; |
| 716 | } |
Brian Salomon | ea1d39b | 2021-04-01 17:06:52 -0400 | [diff] [blame] | 717 | return update_texture_with_pixmaps(this, |
Brian Salomon | b5f880a | 2020-12-07 11:30:16 -0500 | [diff] [blame] | 718 | srcData, |
| 719 | numLevels, |
| 720 | backendTexture, |
| 721 | textureOrigin, |
| 722 | std::move(finishedCallback)); |
Adlai Holler | 2e0c70d | 2020-10-13 08:21:37 -0400 | [diff] [blame] | 723 | } |
| 724 | |
Adlai Holler | 64e1383 | 2020-10-13 08:21:56 -0400 | [diff] [blame] | 725 | ////////////////////////////////////////////////////////////////////////////// |
| 726 | |
| 727 | static GrBackendTexture create_and_update_compressed_backend_texture( |
| 728 | GrDirectContext* dContext, |
| 729 | SkISize dimensions, |
| 730 | const GrBackendFormat& backendFormat, |
| 731 | GrMipmapped mipMapped, |
| 732 | GrProtected isProtected, |
| 733 | sk_sp<GrRefCntedCallback> finishedCallback, |
Brian Salomon | 7128323 | 2021-04-08 12:45:58 -0400 | [diff] [blame] | 734 | const void* data, |
| 735 | size_t size) { |
Adlai Holler | 64e1383 | 2020-10-13 08:21:56 -0400 | [diff] [blame] | 736 | GrGpu* gpu = dContext->priv().getGpu(); |
| 737 | |
| 738 | GrBackendTexture beTex = gpu->createCompressedBackendTexture(dimensions, backendFormat, |
| 739 | mipMapped, isProtected); |
| 740 | if (!beTex.isValid()) { |
| 741 | return {}; |
| 742 | } |
| 743 | |
| 744 | if (!dContext->priv().getGpu()->updateCompressedBackendTexture( |
Brian Salomon | 7128323 | 2021-04-08 12:45:58 -0400 | [diff] [blame] | 745 | beTex, std::move(finishedCallback), data, size)) { |
Adlai Holler | 64e1383 | 2020-10-13 08:21:56 -0400 | [diff] [blame] | 746 | dContext->deleteBackendTexture(beTex); |
| 747 | return {}; |
| 748 | } |
| 749 | return beTex; |
| 750 | } |
| 751 | |
Brian Salomon | 7128323 | 2021-04-08 12:45:58 -0400 | [diff] [blame] | 752 | GrBackendTexture GrDirectContext::createCompressedBackendTexture( |
| 753 | int width, int height, |
| 754 | const GrBackendFormat& backendFormat, |
| 755 | const SkColor4f& color, |
| 756 | GrMipmapped mipmapped, |
| 757 | GrProtected isProtected, |
| 758 | GrGpuFinishedProc finishedProc, |
| 759 | GrGpuFinishedContext finishedContext) { |
Adlai Holler | 64e1383 | 2020-10-13 08:21:56 -0400 | [diff] [blame] | 760 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
Brian Salomon | 694ff17 | 2020-11-04 16:54:28 -0500 | [diff] [blame] | 761 | auto finishedCallback = GrRefCntedCallback::Make(finishedProc, finishedContext); |
Adlai Holler | 64e1383 | 2020-10-13 08:21:56 -0400 | [diff] [blame] | 762 | |
| 763 | if (this->abandoned()) { |
| 764 | return {}; |
| 765 | } |
| 766 | |
Brian Salomon | 7128323 | 2021-04-08 12:45:58 -0400 | [diff] [blame] | 767 | SkImage::CompressionType compression = GrBackendFormatToCompressionType(backendFormat); |
| 768 | if (compression == SkImage::CompressionType::kNone) { |
| 769 | return {}; |
| 770 | } |
| 771 | |
| 772 | size_t size = SkCompressedDataSize(compression, |
| 773 | {width, height}, |
| 774 | nullptr, |
| 775 | mipmapped == GrMipmapped::kYes); |
| 776 | auto storage = std::make_unique<char[]>(size); |
| 777 | GrFillInCompressedData(compression, {width, height}, mipmapped, storage.get(), color); |
| 778 | return create_and_update_compressed_backend_texture(this, |
| 779 | {width, height}, |
| 780 | backendFormat, |
| 781 | mipmapped, |
| 782 | isProtected, |
| 783 | std::move(finishedCallback), |
| 784 | storage.get(), |
| 785 | size); |
Adlai Holler | 64e1383 | 2020-10-13 08:21:56 -0400 | [diff] [blame] | 786 | } |
| 787 | |
Brian Salomon | 7128323 | 2021-04-08 12:45:58 -0400 | [diff] [blame] | 788 | GrBackendTexture GrDirectContext::createCompressedBackendTexture( |
| 789 | int width, int height, |
| 790 | SkImage::CompressionType compression, |
| 791 | const SkColor4f& color, |
| 792 | GrMipmapped mipMapped, |
| 793 | GrProtected isProtected, |
| 794 | GrGpuFinishedProc finishedProc, |
| 795 | GrGpuFinishedContext finishedContext) { |
Adlai Holler | 64e1383 | 2020-10-13 08:21:56 -0400 | [diff] [blame] | 796 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
| 797 | GrBackendFormat format = this->compressedBackendFormat(compression); |
| 798 | return this->createCompressedBackendTexture(width, height, format, color, |
| 799 | mipMapped, isProtected, finishedProc, |
| 800 | finishedContext); |
| 801 | } |
| 802 | |
Brian Salomon | 7128323 | 2021-04-08 12:45:58 -0400 | [diff] [blame] | 803 | GrBackendTexture GrDirectContext::createCompressedBackendTexture( |
| 804 | int width, int height, |
| 805 | const GrBackendFormat& backendFormat, |
| 806 | const void* compressedData, |
| 807 | size_t dataSize, |
| 808 | GrMipmapped mipMapped, |
| 809 | GrProtected isProtected, |
| 810 | GrGpuFinishedProc finishedProc, |
| 811 | GrGpuFinishedContext finishedContext) { |
Adlai Holler | 64e1383 | 2020-10-13 08:21:56 -0400 | [diff] [blame] | 812 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
Brian Salomon | 694ff17 | 2020-11-04 16:54:28 -0500 | [diff] [blame] | 813 | auto finishedCallback = GrRefCntedCallback::Make(finishedProc, finishedContext); |
Adlai Holler | 64e1383 | 2020-10-13 08:21:56 -0400 | [diff] [blame] | 814 | |
| 815 | if (this->abandoned()) { |
| 816 | return {}; |
| 817 | } |
| 818 | |
Brian Salomon | 7128323 | 2021-04-08 12:45:58 -0400 | [diff] [blame] | 819 | return create_and_update_compressed_backend_texture(this, |
| 820 | {width, height}, |
| 821 | backendFormat, |
| 822 | mipMapped, |
| 823 | isProtected, |
| 824 | std::move(finishedCallback), |
| 825 | compressedData, |
| 826 | dataSize); |
Adlai Holler | 64e1383 | 2020-10-13 08:21:56 -0400 | [diff] [blame] | 827 | } |
| 828 | |
Brian Salomon | 7128323 | 2021-04-08 12:45:58 -0400 | [diff] [blame] | 829 | GrBackendTexture GrDirectContext::createCompressedBackendTexture( |
| 830 | int width, int height, |
| 831 | SkImage::CompressionType compression, |
| 832 | const void* data, size_t dataSize, |
| 833 | GrMipmapped mipMapped, |
| 834 | GrProtected isProtected, |
| 835 | GrGpuFinishedProc finishedProc, |
| 836 | GrGpuFinishedContext finishedContext) { |
Adlai Holler | 64e1383 | 2020-10-13 08:21:56 -0400 | [diff] [blame] | 837 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
| 838 | GrBackendFormat format = this->compressedBackendFormat(compression); |
| 839 | return this->createCompressedBackendTexture(width, height, format, data, dataSize, mipMapped, |
| 840 | isProtected, finishedProc, finishedContext); |
| 841 | } |
| 842 | |
| 843 | bool GrDirectContext::updateCompressedBackendTexture(const GrBackendTexture& backendTexture, |
| 844 | const SkColor4f& color, |
| 845 | GrGpuFinishedProc finishedProc, |
| 846 | GrGpuFinishedContext finishedContext) { |
Brian Salomon | 694ff17 | 2020-11-04 16:54:28 -0500 | [diff] [blame] | 847 | auto finishedCallback = GrRefCntedCallback::Make(finishedProc, finishedContext); |
Adlai Holler | 64e1383 | 2020-10-13 08:21:56 -0400 | [diff] [blame] | 848 | |
| 849 | if (this->abandoned()) { |
| 850 | return false; |
| 851 | } |
| 852 | |
Brian Salomon | 7128323 | 2021-04-08 12:45:58 -0400 | [diff] [blame] | 853 | SkImage::CompressionType compression = |
| 854 | GrBackendFormatToCompressionType(backendTexture.getBackendFormat()); |
| 855 | if (compression == SkImage::CompressionType::kNone) { |
| 856 | return {}; |
| 857 | } |
| 858 | size_t size = SkCompressedDataSize(compression, |
| 859 | backendTexture.dimensions(), |
| 860 | nullptr, |
| 861 | backendTexture.hasMipmaps()); |
| 862 | SkAutoMalloc storage(size); |
| 863 | GrFillInCompressedData(compression, |
| 864 | backendTexture.dimensions(), |
| 865 | backendTexture.mipmapped(), |
| 866 | static_cast<char*>(storage.get()), |
| 867 | color); |
| 868 | return fGpu->updateCompressedBackendTexture(backendTexture, |
| 869 | std::move(finishedCallback), |
| 870 | storage.get(), |
| 871 | size); |
Adlai Holler | 64e1383 | 2020-10-13 08:21:56 -0400 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | bool GrDirectContext::updateCompressedBackendTexture(const GrBackendTexture& backendTexture, |
| 875 | const void* compressedData, |
| 876 | size_t dataSize, |
| 877 | GrGpuFinishedProc finishedProc, |
| 878 | GrGpuFinishedContext finishedContext) { |
Brian Salomon | 694ff17 | 2020-11-04 16:54:28 -0500 | [diff] [blame] | 879 | auto finishedCallback = GrRefCntedCallback::Make(finishedProc, finishedContext); |
Adlai Holler | 64e1383 | 2020-10-13 08:21:56 -0400 | [diff] [blame] | 880 | |
| 881 | if (this->abandoned()) { |
| 882 | return false; |
| 883 | } |
| 884 | |
| 885 | if (!compressedData) { |
| 886 | return false; |
| 887 | } |
| 888 | |
Brian Salomon | 7128323 | 2021-04-08 12:45:58 -0400 | [diff] [blame] | 889 | return fGpu->updateCompressedBackendTexture(backendTexture, |
| 890 | std::move(finishedCallback), |
| 891 | compressedData, |
| 892 | dataSize); |
Adlai Holler | 64e1383 | 2020-10-13 08:21:56 -0400 | [diff] [blame] | 893 | } |
| 894 | |
Adlai Holler | 6d0745b | 2020-10-13 13:29:00 -0400 | [diff] [blame] | 895 | ////////////////////////////////////////////////////////////////////////////// |
| 896 | |
| 897 | bool GrDirectContext::setBackendTextureState(const GrBackendTexture& backendTexture, |
| 898 | const GrBackendSurfaceMutableState& state, |
| 899 | GrBackendSurfaceMutableState* previousState, |
| 900 | GrGpuFinishedProc finishedProc, |
| 901 | GrGpuFinishedContext finishedContext) { |
Brian Salomon | 694ff17 | 2020-11-04 16:54:28 -0500 | [diff] [blame] | 902 | auto callback = GrRefCntedCallback::Make(finishedProc, finishedContext); |
Adlai Holler | 6d0745b | 2020-10-13 13:29:00 -0400 | [diff] [blame] | 903 | |
| 904 | if (this->abandoned()) { |
| 905 | return false; |
| 906 | } |
| 907 | |
| 908 | return fGpu->setBackendTextureState(backendTexture, state, previousState, std::move(callback)); |
| 909 | } |
| 910 | |
| 911 | |
| 912 | bool GrDirectContext::setBackendRenderTargetState(const GrBackendRenderTarget& backendRenderTarget, |
| 913 | const GrBackendSurfaceMutableState& state, |
| 914 | GrBackendSurfaceMutableState* previousState, |
| 915 | GrGpuFinishedProc finishedProc, |
| 916 | GrGpuFinishedContext finishedContext) { |
Brian Salomon | 694ff17 | 2020-11-04 16:54:28 -0500 | [diff] [blame] | 917 | auto callback = GrRefCntedCallback::Make(finishedProc, finishedContext); |
Adlai Holler | 6d0745b | 2020-10-13 13:29:00 -0400 | [diff] [blame] | 918 | |
| 919 | if (this->abandoned()) { |
| 920 | return false; |
| 921 | } |
| 922 | |
| 923 | return fGpu->setBackendRenderTargetState(backendRenderTarget, state, previousState, |
| 924 | std::move(callback)); |
| 925 | } |
| 926 | |
| 927 | void GrDirectContext::deleteBackendTexture(GrBackendTexture backendTex) { |
| 928 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
| 929 | // For the Vulkan backend we still must destroy the backend texture when the context is |
| 930 | // abandoned. |
| 931 | if ((this->abandoned() && this->backend() != GrBackendApi::kVulkan) || !backendTex.isValid()) { |
| 932 | return; |
| 933 | } |
| 934 | |
| 935 | fGpu->deleteBackendTexture(backendTex); |
| 936 | } |
| 937 | |
| 938 | ////////////////////////////////////////////////////////////////////////////// |
| 939 | |
| 940 | bool GrDirectContext::precompileShader(const SkData& key, const SkData& data) { |
| 941 | return fGpu->precompileShader(key, data); |
| 942 | } |
| 943 | |
| 944 | #ifdef SK_ENABLE_DUMP_GPU |
| 945 | #include "include/core/SkString.h" |
| 946 | #include "src/utils/SkJSONWriter.h" |
| 947 | SkString GrDirectContext::dump() const { |
| 948 | SkDynamicMemoryWStream stream; |
| 949 | SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty); |
| 950 | writer.beginObject(); |
| 951 | |
| 952 | writer.appendString("backend", GrBackendApiToStr(this->backend())); |
| 953 | |
| 954 | writer.appendName("caps"); |
| 955 | this->caps()->dumpJSON(&writer); |
| 956 | |
| 957 | writer.appendName("gpu"); |
| 958 | this->fGpu->dumpJSON(&writer); |
| 959 | |
| 960 | writer.appendName("context"); |
| 961 | this->dumpJSON(&writer); |
| 962 | |
| 963 | // Flush JSON to the memory stream |
| 964 | writer.endObject(); |
| 965 | writer.flush(); |
| 966 | |
| 967 | // Null terminate the JSON data in the memory stream |
| 968 | stream.write8(0); |
| 969 | |
| 970 | // Allocate a string big enough to hold all the data, then copy out of the stream |
| 971 | SkString result(stream.bytesWritten()); |
| 972 | stream.copyToAndReset(result.writable_str()); |
| 973 | return result; |
| 974 | } |
| 975 | #endif |
| 976 | |
John Rosasco | a9b348f | 2019-11-08 13:18:15 -0800 | [diff] [blame] | 977 | #ifdef SK_GL |
Robert Phillips | c7228c6 | 2020-07-14 12:57:39 -0400 | [diff] [blame] | 978 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 979 | /*************************************************************************************************/ |
| 980 | sk_sp<GrDirectContext> GrDirectContext::MakeGL(sk_sp<const GrGLInterface> glInterface) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 981 | GrContextOptions defaultOptions; |
Jim Van Verth | 03b8ab2 | 2020-02-24 11:36:15 -0500 | [diff] [blame] | 982 | return MakeGL(std::move(glInterface), defaultOptions); |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 983 | } |
| 984 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 985 | sk_sp<GrDirectContext> GrDirectContext::MakeGL(const GrContextOptions& options) { |
Brian Salomon | c1b9c10 | 2018-04-06 09:18:00 -0400 | [diff] [blame] | 986 | return MakeGL(nullptr, options); |
| 987 | } |
| 988 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 989 | sk_sp<GrDirectContext> GrDirectContext::MakeGL() { |
Brian Salomon | c1b9c10 | 2018-04-06 09:18:00 -0400 | [diff] [blame] | 990 | GrContextOptions defaultOptions; |
| 991 | return MakeGL(nullptr, defaultOptions); |
| 992 | } |
| 993 | |
Brian Salomon | 24069eb | 2020-06-24 10:19:52 -0400 | [diff] [blame] | 994 | #if GR_TEST_UTILS |
| 995 | GrGLFunction<GrGLGetErrorFn> make_get_error_with_random_oom(GrGLFunction<GrGLGetErrorFn> original) { |
| 996 | // A SkRandom and a GrGLFunction<GrGLGetErrorFn> are too big to be captured by a |
| 997 | // GrGLFunction<GrGLGetError> (surprise, surprise). So we make a context object and |
| 998 | // capture that by pointer. However, GrGLFunction doesn't support calling a destructor |
| 999 | // on the thing it captures. So we leak the context. |
| 1000 | struct GetErrorContext { |
| 1001 | SkRandom fRandom; |
| 1002 | GrGLFunction<GrGLGetErrorFn> fGetError; |
| 1003 | }; |
| 1004 | |
| 1005 | auto errorContext = new GetErrorContext; |
| 1006 | |
| 1007 | #if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS) |
| 1008 | __lsan_ignore_object(errorContext); |
| 1009 | #endif |
| 1010 | |
| 1011 | errorContext->fGetError = original; |
| 1012 | |
| 1013 | return GrGLFunction<GrGLGetErrorFn>([errorContext]() { |
| 1014 | GrGLenum error = errorContext->fGetError(); |
| 1015 | if (error == GR_GL_NO_ERROR && (errorContext->fRandom.nextU() % 300) == 0) { |
| 1016 | error = GR_GL_OUT_OF_MEMORY; |
| 1017 | } |
| 1018 | return error; |
| 1019 | }); |
| 1020 | } |
| 1021 | #endif |
| 1022 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1023 | sk_sp<GrDirectContext> GrDirectContext::MakeGL(sk_sp<const GrGLInterface> glInterface, |
| 1024 | const GrContextOptions& options) { |
| 1025 | sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kOpenGL, options)); |
Brian Salomon | 24069eb | 2020-06-24 10:19:52 -0400 | [diff] [blame] | 1026 | #if GR_TEST_UTILS |
| 1027 | if (options.fRandomGLOOM) { |
| 1028 | auto copy = sk_make_sp<GrGLInterface>(*glInterface); |
| 1029 | copy->fFunctions.fGetError = |
| 1030 | make_get_error_with_random_oom(glInterface->fFunctions.fGetError); |
| 1031 | #if GR_GL_CHECK_ERROR |
| 1032 | // Suppress logging GL errors since we'll be synthetically generating them. |
| 1033 | copy->suppressErrorLogging(); |
| 1034 | #endif |
| 1035 | glInterface = std::move(copy); |
| 1036 | } |
| 1037 | #endif |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1038 | direct->fGpu = GrGLGpu::Make(std::move(glInterface), options, direct.get()); |
| 1039 | if (!direct->init()) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 1040 | return nullptr; |
| 1041 | } |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1042 | return direct; |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 1043 | } |
John Rosasco | a9b348f | 2019-11-08 13:18:15 -0800 | [diff] [blame] | 1044 | #endif |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 1045 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1046 | /*************************************************************************************************/ |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1047 | sk_sp<GrDirectContext> GrDirectContext::MakeMock(const GrMockOptions* mockOptions) { |
| 1048 | GrContextOptions defaultOptions; |
| 1049 | return MakeMock(mockOptions, defaultOptions); |
| 1050 | } |
| 1051 | |
| 1052 | sk_sp<GrDirectContext> GrDirectContext::MakeMock(const GrMockOptions* mockOptions, |
| 1053 | const GrContextOptions& options) { |
| 1054 | sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kMock, options)); |
| 1055 | |
| 1056 | direct->fGpu = GrMockGpu::Make(mockOptions, options, direct.get()); |
| 1057 | if (!direct->init()) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 1058 | return nullptr; |
| 1059 | } |
Chris Dalton | a378b45 | 2019-12-11 13:24:11 -0500 | [diff] [blame] | 1060 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1061 | return direct; |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 1062 | } |
| 1063 | |
Greg Daniel | b4d8956 | 2018-10-03 18:44:49 +0000 | [diff] [blame] | 1064 | #ifdef SK_VULKAN |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1065 | /*************************************************************************************************/ |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1066 | sk_sp<GrDirectContext> GrDirectContext::MakeVulkan(const GrVkBackendContext& backendContext) { |
| 1067 | GrContextOptions defaultOptions; |
| 1068 | return MakeVulkan(backendContext, defaultOptions); |
| 1069 | } |
| 1070 | |
| 1071 | sk_sp<GrDirectContext> GrDirectContext::MakeVulkan(const GrVkBackendContext& backendContext, |
| 1072 | const GrContextOptions& options) { |
| 1073 | sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kVulkan, options)); |
| 1074 | |
| 1075 | direct->fGpu = GrVkGpu::Make(backendContext, options, direct.get()); |
| 1076 | if (!direct->init()) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 1077 | return nullptr; |
| 1078 | } |
| 1079 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1080 | return direct; |
Greg Daniel | b4d8956 | 2018-10-03 18:44:49 +0000 | [diff] [blame] | 1081 | } |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1082 | #endif |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 1083 | |
| 1084 | #ifdef SK_METAL |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1085 | /*************************************************************************************************/ |
Jim Van Verth | 351c9b5 | 2020-11-12 15:21:11 -0500 | [diff] [blame] | 1086 | sk_sp<GrDirectContext> GrDirectContext::MakeMetal(const GrMtlBackendContext& backendContext) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 1087 | GrContextOptions defaultOptions; |
Jim Van Verth | 351c9b5 | 2020-11-12 15:21:11 -0500 | [diff] [blame] | 1088 | return MakeMetal(backendContext, defaultOptions); |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 1089 | } |
| 1090 | |
Jim Van Verth | 351c9b5 | 2020-11-12 15:21:11 -0500 | [diff] [blame] | 1091 | sk_sp<GrDirectContext> GrDirectContext::MakeMetal(const GrMtlBackendContext& backendContext, |
| 1092 | const GrContextOptions& options) { |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1093 | sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kMetal, options)); |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 1094 | |
Jim Van Verth | 351c9b5 | 2020-11-12 15:21:11 -0500 | [diff] [blame] | 1095 | direct->fGpu = GrMtlTrampoline::MakeGpu(backendContext, options, direct.get()); |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1096 | if (!direct->init()) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 1097 | return nullptr; |
| 1098 | } |
Timothy Liang | 4e85e80 | 2018-06-28 16:37:18 -0400 | [diff] [blame] | 1099 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1100 | return direct; |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 1101 | } |
Jim Van Verth | 351c9b5 | 2020-11-12 15:21:11 -0500 | [diff] [blame] | 1102 | |
| 1103 | // deprecated |
| 1104 | sk_sp<GrDirectContext> GrDirectContext::MakeMetal(void* device, void* queue) { |
| 1105 | GrContextOptions defaultOptions; |
| 1106 | return MakeMetal(device, queue, defaultOptions); |
| 1107 | } |
| 1108 | |
| 1109 | // deprecated |
| 1110 | // remove include/gpu/mtl/GrMtlBackendContext.h, above, when removed |
| 1111 | sk_sp<GrDirectContext> GrDirectContext::MakeMetal(void* device, void* queue, |
| 1112 | const GrContextOptions& options) { |
| 1113 | sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kMetal, options)); |
| 1114 | GrMtlBackendContext backendContext = {}; |
| 1115 | backendContext.fDevice.reset(device); |
| 1116 | backendContext.fQueue.reset(queue); |
| 1117 | |
| 1118 | return GrDirectContext::MakeMetal(backendContext, options); |
| 1119 | } |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 1120 | #endif |
| 1121 | |
Jim Van Verth | b01e12b | 2020-02-18 14:34:38 -0500 | [diff] [blame] | 1122 | #ifdef SK_DIRECT3D |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1123 | /*************************************************************************************************/ |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1124 | sk_sp<GrDirectContext> GrDirectContext::MakeDirect3D(const GrD3DBackendContext& backendContext) { |
| 1125 | GrContextOptions defaultOptions; |
| 1126 | return MakeDirect3D(backendContext, defaultOptions); |
| 1127 | } |
| 1128 | |
| 1129 | sk_sp<GrDirectContext> GrDirectContext::MakeDirect3D(const GrD3DBackendContext& backendContext, |
| 1130 | const GrContextOptions& options) { |
| 1131 | sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kDirect3D, options)); |
| 1132 | |
| 1133 | direct->fGpu = GrD3DGpu::Make(backendContext, options, direct.get()); |
| 1134 | if (!direct->init()) { |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame] | 1135 | return nullptr; |
| 1136 | } |
Jim Van Verth | b01e12b | 2020-02-18 14:34:38 -0500 | [diff] [blame] | 1137 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1138 | return direct; |
Jim Van Verth | b01e12b | 2020-02-18 14:34:38 -0500 | [diff] [blame] | 1139 | } |
| 1140 | #endif |
| 1141 | |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 1142 | #ifdef SK_DAWN |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1143 | /*************************************************************************************************/ |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1144 | sk_sp<GrDirectContext> GrDirectContext::MakeDawn(const wgpu::Device& device) { |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 1145 | GrContextOptions defaultOptions; |
| 1146 | return MakeDawn(device, defaultOptions); |
| 1147 | } |
| 1148 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1149 | sk_sp<GrDirectContext> GrDirectContext::MakeDawn(const wgpu::Device& device, |
| 1150 | const GrContextOptions& options) { |
| 1151 | sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kDawn, options)); |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 1152 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1153 | direct->fGpu = GrDawnGpu::Make(device, options, direct.get()); |
| 1154 | if (!direct->init()) { |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 1155 | return nullptr; |
| 1156 | } |
| 1157 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1158 | return direct; |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 1159 | } |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 1160 | |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 1161 | #endif |