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 | bb60677 | 2019-02-04 17:50:57 -0500 | [diff] [blame] | 76 | SkASSERT(this->caps()); |
| 77 | |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 78 | GrDrawOpAtlas::AllowMultitexturing allowMultitexturing; |
Robert Phillips | c1541ae | 2019-02-04 12:05:37 -0500 | [diff] [blame] | 79 | if (GrContextOptions::Enable::kNo == this->options().fAllowMultipleGlyphCacheTextures || |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 80 | // multitexturing supported only if range can represent the index + texcoords fully |
Robert Phillips | bb60677 | 2019-02-04 17:50:57 -0500 | [diff] [blame] | 81 | !(this->caps()->shaderCaps()->floatIs32Bits() || |
| 82 | this->caps()->shaderCaps()->integerSupport())) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 83 | allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo; |
| 84 | } else { |
| 85 | allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes; |
| 86 | } |
| 87 | |
Herb Derby | a00da61 | 2019-03-04 17:10:01 -0500 | [diff] [blame] | 88 | GrStrikeCache* glyphCache = this->priv().getGrStrikeCache(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 89 | GrProxyProvider* proxyProvider = this->priv().proxyProvider(); |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 90 | |
| 91 | fAtlasManager = new GrAtlasManager(proxyProvider, glyphCache, |
Robert Phillips | c1541ae | 2019-02-04 12:05:37 -0500 | [diff] [blame] | 92 | this->options().fGlyphCacheTextureMaximumBytes, |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 93 | allowMultitexturing); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 94 | this->priv().addOnFlushCallbackObject(fAtlasManager); |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 95 | |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 96 | return true; |
| 97 | } |
| 98 | |
| 99 | GrAtlasManager* onGetAtlasManager() override { return fAtlasManager; } |
| 100 | |
| 101 | private: |
| 102 | GrAtlasManager* fAtlasManager; |
| 103 | |
| 104 | typedef GrContext INHERITED; |
| 105 | }; |
| 106 | |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 107 | sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface) { |
| 108 | GrContextOptions defaultOptions; |
| 109 | return MakeGL(std::move(interface), defaultOptions); |
| 110 | } |
| 111 | |
Brian Salomon | c1b9c10 | 2018-04-06 09:18:00 -0400 | [diff] [blame] | 112 | sk_sp<GrContext> GrContext::MakeGL(const GrContextOptions& options) { |
| 113 | return MakeGL(nullptr, options); |
| 114 | } |
| 115 | |
| 116 | sk_sp<GrContext> GrContext::MakeGL() { |
| 117 | GrContextOptions defaultOptions; |
| 118 | return MakeGL(nullptr, defaultOptions); |
| 119 | } |
| 120 | |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 121 | sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface, |
| 122 | const GrContextOptions& options) { |
Robert Phillips | e834579 | 2019-02-07 10:48:24 -0500 | [diff] [blame] | 123 | sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kOpenGL, options)); |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 124 | |
| 125 | context->fGpu = GrGLGpu::Make(std::move(interface), options, context.get()); |
| 126 | if (!context->fGpu) { |
| 127 | return nullptr; |
| 128 | } |
| 129 | |
Robert Phillips | bb60677 | 2019-02-04 17:50:57 -0500 | [diff] [blame] | 130 | if (!context->init(context->fGpu->refCaps(), nullptr)) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 131 | return nullptr; |
| 132 | } |
| 133 | return context; |
| 134 | } |
| 135 | |
| 136 | sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) { |
| 137 | GrContextOptions defaultOptions; |
| 138 | return MakeMock(mockOptions, defaultOptions); |
| 139 | } |
| 140 | |
| 141 | sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions, |
| 142 | const GrContextOptions& options) { |
Robert Phillips | e834579 | 2019-02-07 10:48:24 -0500 | [diff] [blame] | 143 | sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMock, options)); |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 144 | |
| 145 | context->fGpu = GrMockGpu::Make(mockOptions, options, context.get()); |
| 146 | if (!context->fGpu) { |
| 147 | return nullptr; |
| 148 | } |
| 149 | |
Robert Phillips | bb60677 | 2019-02-04 17:50:57 -0500 | [diff] [blame] | 150 | if (!context->init(context->fGpu->refCaps(), nullptr)) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 151 | return nullptr; |
| 152 | } |
| 153 | return context; |
| 154 | } |
| 155 | |
Mike Klein | a55e214 | 2018-10-03 16:34:11 +0000 | [diff] [blame] | 156 | sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext) { |
Greg Daniel | b4d8956 | 2018-10-03 18:44:49 +0000 | [diff] [blame] | 157 | #ifdef SK_VULKAN |
Greg Daniel | 10a83da | 2018-06-14 09:31:11 -0400 | [diff] [blame] | 158 | GrContextOptions defaultOptions; |
| 159 | return MakeVulkan(backendContext, defaultOptions); |
Greg Daniel | b4d8956 | 2018-10-03 18:44:49 +0000 | [diff] [blame] | 160 | #else |
| 161 | return nullptr; |
| 162 | #endif |
Greg Daniel | 10a83da | 2018-06-14 09:31:11 -0400 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext, |
| 166 | const GrContextOptions& options) { |
Greg Daniel | b4d8956 | 2018-10-03 18:44:49 +0000 | [diff] [blame] | 167 | #ifdef SK_VULKAN |
| 168 | GrContextOptions defaultOptions; |
Robert Phillips | e834579 | 2019-02-07 10:48:24 -0500 | [diff] [blame] | 169 | sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kVulkan, options)); |
Greg Daniel | 10a83da | 2018-06-14 09:31:11 -0400 | [diff] [blame] | 170 | |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 171 | context->fGpu = GrVkGpu::Make(backendContext, options, context.get()); |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 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; |
Greg Daniel | b4d8956 | 2018-10-03 18:44:49 +0000 | [diff] [blame] | 180 | #else |
| 181 | return nullptr; |
Mike Klein | a55e214 | 2018-10-03 16:34:11 +0000 | [diff] [blame] | 182 | #endif |
Greg Daniel | b4d8956 | 2018-10-03 18:44:49 +0000 | [diff] [blame] | 183 | } |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 184 | |
| 185 | #ifdef SK_METAL |
| 186 | sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) { |
| 187 | GrContextOptions defaultOptions; |
| 188 | return MakeMetal(device, queue, defaultOptions); |
| 189 | } |
| 190 | |
| 191 | sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) { |
Robert Phillips | e834579 | 2019-02-07 10:48:24 -0500 | [diff] [blame] | 192 | sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMetal, options)); |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 193 | |
| 194 | context->fGpu = GrMtlTrampoline::MakeGpu(context.get(), options, device, queue); |
| 195 | if (!context->fGpu) { |
| 196 | return nullptr; |
| 197 | } |
Timothy Liang | 4e85e80 | 2018-06-28 16:37:18 -0400 | [diff] [blame] | 198 | |
Robert Phillips | bb60677 | 2019-02-04 17:50:57 -0500 | [diff] [blame] | 199 | if (!context->init(context->fGpu->refCaps(), nullptr)) { |
Robert Phillips | a3457b8 | 2018-03-08 11:30:12 -0500 | [diff] [blame] | 200 | return nullptr; |
| 201 | } |
| 202 | return context; |
| 203 | } |
| 204 | #endif |
| 205 | |