blob: c87078b98be2dd0c28394ad7592e311dbf100361 [file] [log] [blame]
Robert Phillipsa3457b82018-03-08 11:30:12 -05001/*
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 Nicholas00543112018-07-31 09:44:36 -040013#include "effects/GrSkSLFP.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050014#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
24class SK_API GrDirectContext : public GrContext {
25public:
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
58protected:
59 bool init(const GrContextOptions& options) override {
60 SkASSERT(fCaps); // should've been set in ctor
61 SkASSERT(!fThreadSafeProxy);
Ethan Nicholas00543112018-07-31 09:44:36 -040062 SkASSERT(!fFPFactoryCache);
63 fFPFactoryCache.reset(new GrSkSLFPFactoryCache());
Robert Phillipsa3457b82018-03-08 11:30:12 -050064 fThreadSafeProxy.reset(new GrContextThreadSafeProxy(fCaps, this->uniqueID(),
Ethan Nicholas00543112018-07-31 09:44:36 -040065 fBackend, options, fFPFactoryCache));
Robert Phillipsa3457b82018-03-08 11:30:12 -050066
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
88 SkASSERT(glyphCache->getGlyphSizeLimit() == fAtlasManager->getGlyphSizeLimit());
89 return true;
90 }
91
92 GrAtlasManager* onGetAtlasManager() override { return fAtlasManager; }
93
94private:
95 GrAtlasManager* fAtlasManager;
96
97 typedef GrContext INHERITED;
98};
99
Robert Phillipsa3457b82018-03-08 11:30:12 -0500100sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface) {
101 GrContextOptions defaultOptions;
102 return MakeGL(std::move(interface), defaultOptions);
103}
104
Brian Salomonc1b9c102018-04-06 09:18:00 -0400105sk_sp<GrContext> GrContext::MakeGL(const GrContextOptions& options) {
106 return MakeGL(nullptr, options);
107}
108
109sk_sp<GrContext> GrContext::MakeGL() {
110 GrContextOptions defaultOptions;
111 return MakeGL(nullptr, defaultOptions);
112}
113
Robert Phillipsa3457b82018-03-08 11:30:12 -0500114sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface,
115 const GrContextOptions& options) {
116 sk_sp<GrContext> context(new GrDirectContext(kOpenGL_GrBackend));
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
130sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) {
131 GrContextOptions defaultOptions;
132 return MakeMock(mockOptions, defaultOptions);
133}
134
135sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions,
136 const GrContextOptions& options) {
137 sk_sp<GrContext> context(new GrDirectContext(kMock_GrBackend));
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
151#ifdef SK_VULKAN
Greg Daniel10a83da2018-06-14 09:31:11 -0400152sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext) {
153 GrContextOptions defaultOptions;
154 return MakeVulkan(backendContext, defaultOptions);
155}
156
157sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext,
158 const GrContextOptions& options) {
159 sk_sp<GrContext> context(new GrDirectContext(kVulkan_GrBackend));
160
Greg Danielf730c182018-07-02 20:15:37 +0000161 context->fGpu = GrVkGpu::Make(backendContext, options, context.get());
Robert Phillipsa3457b82018-03-08 11:30:12 -0500162 if (!context->fGpu) {
163 return nullptr;
164 }
165
166 context->fCaps = context->fGpu->refCaps();
167 if (!context->init(options)) {
168 return nullptr;
169 }
170 return context;
171}
172#endif
173
174#ifdef SK_METAL
175sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) {
176 GrContextOptions defaultOptions;
177 return MakeMetal(device, queue, defaultOptions);
178}
179
180sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) {
181 sk_sp<GrContext> context(new GrDirectContext(kMetal_GrBackend));
182
183 context->fGpu = GrMtlTrampoline::MakeGpu(context.get(), options, device, queue);
184 if (!context->fGpu) {
185 return nullptr;
186 }
Timothy Liang4e85e802018-06-28 16:37:18 -0400187
188 context->fCaps = context->fGpu->refCaps();
Robert Phillipsa3457b82018-03-08 11:30:12 -0500189 if (!context->init(options)) {
190 return nullptr;
191 }
192 return context;
193}
194#endif
195