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