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