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