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