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