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