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