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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "include/gpu/GrContext.h" |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/gpu/GrContextThreadSafeProxy.h" |
| 12 | #include "src/gpu/GrContextPriv.h" |
| 13 | #include "src/gpu/GrContextThreadSafeProxyPriv.h" |
| 14 | #include "src/gpu/GrGpu.h" |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 15 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/gpu/effects/GrSkSLFP.h" |
| 17 | #include "src/gpu/gl/GrGLGpu.h" |
| 18 | #include "src/gpu/mock/GrMockGpu.h" |
| 19 | #include "src/gpu/text/GrStrikeCache.h" |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 20 | #ifdef SK_METAL |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "src/gpu/mtl/GrMtlTrampoline.h" |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 22 | #endif |
| 23 | #ifdef SK_VULKAN |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "src/gpu/vk/GrVkGpu.h" |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 25 | #endif |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame^] | 26 | #ifdef SK_DIRECT3D |
| 27 | #include "src/gpu/d3d/GrD3DGpu.h" |
| 28 | #endif |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 29 | #ifdef SK_DAWN |
Mike Klein | 52337de | 2019-07-25 09:00:52 -0500 | [diff] [blame] | 30 | #include "src/gpu/dawn/GrDawnGpu.h" |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 31 | #endif |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 32 | |
Robert Phillips | 6db27c2 | 2019-05-01 10:43:56 -0400 | [diff] [blame] | 33 | #ifdef SK_DISABLE_REDUCE_OPLIST_SPLITTING |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 34 | static const bool kDefaultReduceOpsTaskSplitting = false; |
Robert Phillips | 6db27c2 | 2019-05-01 10:43:56 -0400 | [diff] [blame] | 35 | #else |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 36 | static const bool kDefaultReduceOpsTaskSplitting = false; |
Robert Phillips | 6db27c2 | 2019-05-01 10:43:56 -0400 | [diff] [blame] | 37 | #endif |
| 38 | |
Brian Salomon | 57f211b | 2019-08-21 15:21:09 -0400 | [diff] [blame] | 39 | class GrLegacyDirectContext : public GrContext { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 40 | public: |
Robert Phillips | e834579 | 2019-02-07 10:48:24 -0500 | [diff] [blame] | 41 | GrLegacyDirectContext(GrBackendApi backend, const GrContextOptions& options) |
Robert Phillips | c1541ae | 2019-02-04 12:05:37 -0500 | [diff] [blame] | 42 | : INHERITED(backend, options) |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 43 | , fAtlasManager(nullptr) { |
| 44 | } |
| 45 | |
Robert Phillips | e834579 | 2019-02-07 10:48:24 -0500 | [diff] [blame] | 46 | ~GrLegacyDirectContext() override { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 47 | // this if-test protects against the case where the context is being destroyed |
| 48 | // before having been fully created |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 49 | if (this->priv().getGpu()) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 50 | this->flush(); |
| 51 | } |
| 52 | |
| 53 | delete fAtlasManager; |
| 54 | } |
| 55 | |
| 56 | void abandonContext() override { |
| 57 | INHERITED::abandonContext(); |
| 58 | fAtlasManager->freeAll(); |
| 59 | } |
| 60 | |
| 61 | void releaseResourcesAndAbandonContext() override { |
| 62 | INHERITED::releaseResourcesAndAbandonContext(); |
| 63 | fAtlasManager->freeAll(); |
| 64 | } |
| 65 | |
| 66 | void freeGpuResources() override { |
| 67 | this->flush(); |
| 68 | fAtlasManager->freeAll(); |
| 69 | |
| 70 | INHERITED::freeGpuResources(); |
| 71 | } |
| 72 | |
| 73 | protected: |
Brian Osman | 7b1678a | 2019-12-16 09:17:25 -0500 | [diff] [blame] | 74 | bool init(sk_sp<const GrCaps> caps) override { |
| 75 | SkASSERT(caps); |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 76 | SkASSERT(!fThreadSafeProxy); |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 77 | |
Robert Phillips | bb60677 | 2019-02-04 17:50:57 -0500 | [diff] [blame] | 78 | fThreadSafeProxy = GrContextThreadSafeProxyPriv::Make(this->backend(), |
| 79 | this->options(), |
| 80 | this->contextID(), |
Brian Osman | 7b1678a | 2019-12-16 09:17:25 -0500 | [diff] [blame] | 81 | caps); |
Robert Phillips | bb60677 | 2019-02-04 17:50:57 -0500 | [diff] [blame] | 82 | |
Brian Osman | 7b1678a | 2019-12-16 09:17:25 -0500 | [diff] [blame] | 83 | if (!INHERITED::init(std::move(caps))) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 84 | return false; |
| 85 | } |
| 86 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 87 | bool reduceOpsTaskSplitting = kDefaultReduceOpsTaskSplitting; |
Greg Daniel | 9313874 | 2019-08-22 17:15:39 -0400 | [diff] [blame] | 88 | if (GrContextOptions::Enable::kNo == this->options().fReduceOpsTaskSplitting) { |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 89 | reduceOpsTaskSplitting = false; |
Greg Daniel | 9313874 | 2019-08-22 17:15:39 -0400 | [diff] [blame] | 90 | } else if (GrContextOptions::Enable::kYes == this->options().fReduceOpsTaskSplitting) { |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 91 | reduceOpsTaskSplitting = true; |
Robert Phillips | 6db27c2 | 2019-05-01 10:43:56 -0400 | [diff] [blame] | 92 | } |
| 93 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 94 | this->setupDrawingManager(true, reduceOpsTaskSplitting); |
Robert Phillips | 56181ba | 2019-03-08 12:00:45 -0500 | [diff] [blame] | 95 | |
Robert Phillips | bb60677 | 2019-02-04 17:50:57 -0500 | [diff] [blame] | 96 | SkASSERT(this->caps()); |
| 97 | |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 98 | GrDrawOpAtlas::AllowMultitexturing allowMultitexturing; |
Robert Phillips | c1541ae | 2019-02-04 12:05:37 -0500 | [diff] [blame] | 99 | if (GrContextOptions::Enable::kNo == this->options().fAllowMultipleGlyphCacheTextures || |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 100 | // multitexturing supported only if range can represent the index + texcoords fully |
Robert Phillips | bb60677 | 2019-02-04 17:50:57 -0500 | [diff] [blame] | 101 | !(this->caps()->shaderCaps()->floatIs32Bits() || |
| 102 | this->caps()->shaderCaps()->integerSupport())) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 103 | allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo; |
| 104 | } else { |
| 105 | allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes; |
| 106 | } |
| 107 | |
Herb Derby | a00da61 | 2019-03-04 17:10:01 -0500 | [diff] [blame] | 108 | GrStrikeCache* glyphCache = this->priv().getGrStrikeCache(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 109 | GrProxyProvider* proxyProvider = this->priv().proxyProvider(); |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 110 | |
| 111 | fAtlasManager = new GrAtlasManager(proxyProvider, glyphCache, |
Robert Phillips | c1541ae | 2019-02-04 12:05:37 -0500 | [diff] [blame] | 112 | this->options().fGlyphCacheTextureMaximumBytes, |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 113 | allowMultitexturing); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 114 | this->priv().addOnFlushCallbackObject(fAtlasManager); |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 115 | |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 116 | return true; |
| 117 | } |
| 118 | |
| 119 | GrAtlasManager* onGetAtlasManager() override { return fAtlasManager; } |
| 120 | |
| 121 | private: |
| 122 | GrAtlasManager* fAtlasManager; |
| 123 | |
| 124 | typedef GrContext INHERITED; |
| 125 | }; |
| 126 | |
John Rosasco | a9b348f | 2019-11-08 13:18:15 -0800 | [diff] [blame] | 127 | #ifdef SK_GL |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 128 | sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface) { |
| 129 | GrContextOptions defaultOptions; |
| 130 | return MakeGL(std::move(interface), defaultOptions); |
| 131 | } |
| 132 | |
Brian Salomon | c1b9c10 | 2018-04-06 09:18:00 -0400 | [diff] [blame] | 133 | sk_sp<GrContext> GrContext::MakeGL(const GrContextOptions& options) { |
| 134 | return MakeGL(nullptr, options); |
| 135 | } |
| 136 | |
| 137 | sk_sp<GrContext> GrContext::MakeGL() { |
| 138 | GrContextOptions defaultOptions; |
| 139 | return MakeGL(nullptr, defaultOptions); |
| 140 | } |
| 141 | |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 142 | sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface, |
| 143 | const GrContextOptions& options) { |
Robert Phillips | e834579 | 2019-02-07 10:48:24 -0500 | [diff] [blame] | 144 | sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kOpenGL, options)); |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 145 | |
| 146 | context->fGpu = GrGLGpu::Make(std::move(interface), options, context.get()); |
| 147 | if (!context->fGpu) { |
| 148 | return nullptr; |
| 149 | } |
| 150 | |
Brian Osman | 7b1678a | 2019-12-16 09:17:25 -0500 | [diff] [blame] | 151 | if (!context->init(context->fGpu->refCaps())) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 152 | return nullptr; |
| 153 | } |
| 154 | return context; |
| 155 | } |
John Rosasco | a9b348f | 2019-11-08 13:18:15 -0800 | [diff] [blame] | 156 | #endif |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 157 | |
| 158 | sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) { |
| 159 | GrContextOptions defaultOptions; |
| 160 | return MakeMock(mockOptions, defaultOptions); |
| 161 | } |
| 162 | |
| 163 | sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions, |
| 164 | const GrContextOptions& options) { |
Robert Phillips | e834579 | 2019-02-07 10:48:24 -0500 | [diff] [blame] | 165 | sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMock, options)); |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 166 | |
| 167 | context->fGpu = GrMockGpu::Make(mockOptions, options, context.get()); |
| 168 | if (!context->fGpu) { |
| 169 | return nullptr; |
| 170 | } |
| 171 | |
Brian Osman | 7b1678a | 2019-12-16 09:17:25 -0500 | [diff] [blame] | 172 | if (!context->init(context->fGpu->refCaps())) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 173 | return nullptr; |
| 174 | } |
Chris Dalton | a378b45 | 2019-12-11 13:24:11 -0500 | [diff] [blame] | 175 | |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 176 | return context; |
| 177 | } |
| 178 | |
Mike Klein | a55e214 | 2018-10-03 16:34:11 +0000 | [diff] [blame] | 179 | sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext) { |
Greg Daniel | b4d8956 | 2018-10-03 18:44:49 +0000 | [diff] [blame] | 180 | #ifdef SK_VULKAN |
Greg Daniel | 10a83da | 2018-06-14 09:31:11 -0400 | [diff] [blame] | 181 | GrContextOptions defaultOptions; |
| 182 | return MakeVulkan(backendContext, defaultOptions); |
Greg Daniel | b4d8956 | 2018-10-03 18:44:49 +0000 | [diff] [blame] | 183 | #else |
| 184 | return nullptr; |
| 185 | #endif |
Greg Daniel | 10a83da | 2018-06-14 09:31:11 -0400 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext, |
| 189 | const GrContextOptions& options) { |
Greg Daniel | b4d8956 | 2018-10-03 18:44:49 +0000 | [diff] [blame] | 190 | #ifdef SK_VULKAN |
Robert Phillips | e834579 | 2019-02-07 10:48:24 -0500 | [diff] [blame] | 191 | sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kVulkan, options)); |
Greg Daniel | 10a83da | 2018-06-14 09:31:11 -0400 | [diff] [blame] | 192 | |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 193 | context->fGpu = GrVkGpu::Make(backendContext, options, context.get()); |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 194 | if (!context->fGpu) { |
| 195 | return nullptr; |
| 196 | } |
| 197 | |
Brian Osman | 7b1678a | 2019-12-16 09:17:25 -0500 | [diff] [blame] | 198 | if (!context->init(context->fGpu->refCaps())) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 199 | return nullptr; |
| 200 | } |
| 201 | return context; |
Greg Daniel | b4d8956 | 2018-10-03 18:44:49 +0000 | [diff] [blame] | 202 | #else |
| 203 | return nullptr; |
Mike Klein | a55e214 | 2018-10-03 16:34:11 +0000 | [diff] [blame] | 204 | #endif |
Greg Daniel | b4d8956 | 2018-10-03 18:44:49 +0000 | [diff] [blame] | 205 | } |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 206 | |
| 207 | #ifdef SK_METAL |
| 208 | sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) { |
| 209 | GrContextOptions defaultOptions; |
| 210 | return MakeMetal(device, queue, defaultOptions); |
| 211 | } |
| 212 | |
| 213 | sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) { |
Robert Phillips | e834579 | 2019-02-07 10:48:24 -0500 | [diff] [blame] | 214 | sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMetal, options)); |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 215 | |
| 216 | context->fGpu = GrMtlTrampoline::MakeGpu(context.get(), options, device, queue); |
| 217 | if (!context->fGpu) { |
| 218 | return nullptr; |
| 219 | } |
Timothy Liang | 4e85e80 | 2018-06-28 16:37:18 -0400 | [diff] [blame] | 220 | |
Brian Osman | 7b1678a | 2019-12-16 09:17:25 -0500 | [diff] [blame] | 221 | if (!context->init(context->fGpu->refCaps())) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 222 | return nullptr; |
| 223 | } |
| 224 | return context; |
| 225 | } |
| 226 | #endif |
| 227 | |
Jim Van Verth | b01e12b | 2020-02-18 14:34:38 -0500 | [diff] [blame] | 228 | #ifdef SK_DIRECT3D |
| 229 | sk_sp<GrContext> GrContext::MakeDirect3D(const GrD3DBackendContext& backendContext) { |
| 230 | GrContextOptions defaultOptions; |
| 231 | return MakeDirect3D(backendContext, defaultOptions); |
| 232 | } |
| 233 | |
| 234 | sk_sp<GrContext> GrContext::MakeDirect3D(const GrD3DBackendContext& backendContext, |
| 235 | const GrContextOptions& options) { |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame^] | 236 | sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kDirect3D, options)); |
Jim Van Verth | b01e12b | 2020-02-18 14:34:38 -0500 | [diff] [blame] | 237 | |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame^] | 238 | context->fGpu = GrD3DGpu::Make(backendContext, options, context.get()); |
| 239 | if (!context->fGpu) { |
| 240 | return nullptr; |
| 241 | } |
Jim Van Verth | b01e12b | 2020-02-18 14:34:38 -0500 | [diff] [blame] | 242 | |
Jim Van Verth | d2d4c5e | 2020-02-19 14:57:58 -0500 | [diff] [blame^] | 243 | if (!context->init(context->fGpu->refCaps())) { |
| 244 | return nullptr; |
| 245 | } |
| 246 | return context; |
Jim Van Verth | b01e12b | 2020-02-18 14:34:38 -0500 | [diff] [blame] | 247 | } |
| 248 | #endif |
| 249 | |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 250 | #ifdef SK_DAWN |
Stephen White | a521c96 | 2019-12-04 09:57:48 -0500 | [diff] [blame] | 251 | sk_sp<GrContext> GrContext::MakeDawn(const wgpu::Device& device) { |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 252 | GrContextOptions defaultOptions; |
| 253 | return MakeDawn(device, defaultOptions); |
| 254 | } |
| 255 | |
Stephen White | a521c96 | 2019-12-04 09:57:48 -0500 | [diff] [blame] | 256 | sk_sp<GrContext> GrContext::MakeDawn(const wgpu::Device& device, const GrContextOptions& options) { |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 257 | sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kDawn, options)); |
| 258 | |
| 259 | context->fGpu = GrDawnGpu::Make(device, options, context.get()); |
| 260 | if (!context->fGpu) { |
| 261 | return nullptr; |
| 262 | } |
| 263 | |
Brian Osman | 7b1678a | 2019-12-16 09:17:25 -0500 | [diff] [blame] | 264 | if (!context->init(context->fGpu->refCaps())) { |
Stephen White | 985741a | 2019-07-18 11:43:45 -0400 | [diff] [blame] | 265 | return nullptr; |
| 266 | } |
| 267 | return context; |
| 268 | } |
| 269 | #endif |