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