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