blob: 490b62b4f8f9cb51a6e63136541458ce3cd2b05a [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
Greg Daniel54bfb182018-11-20 17:12:36 -05008
Robert Phillipsa3457b82018-03-08 11:30:12 -05009#include "GrContext.h"
10
11#include "GrContextPriv.h"
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050012#include "GrContextThreadSafeProxy.h"
Robert Phillipsbb606772019-02-04 17:50:57 -050013#include "GrContextThreadSafeProxyPriv.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050014#include "GrGpu.h"
15
Ethan Nicholas00543112018-07-31 09:44:36 -040016#include "effects/GrSkSLFP.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050017#include "gl/GrGLGpu.h"
18#include "mock/GrMockGpu.h"
Herb Derby081e6f32019-01-16 13:46:02 -050019#include "text/GrStrikeCache.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050020#ifdef SK_METAL
21#include "mtl/GrMtlTrampoline.h"
22#endif
23#ifdef SK_VULKAN
24#include "vk/GrVkGpu.h"
25#endif
26
Robert Phillipse8345792019-02-07 10:48:24 -050027class SK_API GrLegacyDirectContext : public GrContext {
Robert Phillipsa3457b82018-03-08 11:30:12 -050028public:
Robert Phillipse8345792019-02-07 10:48:24 -050029 GrLegacyDirectContext(GrBackendApi backend, const GrContextOptions& options)
Robert Phillipsc1541ae2019-02-04 12:05:37 -050030 : INHERITED(backend, options)
Robert Phillipsa3457b82018-03-08 11:30:12 -050031 , fAtlasManager(nullptr) {
32 }
33
Robert Phillipse8345792019-02-07 10:48:24 -050034 ~GrLegacyDirectContext() override {
Robert Phillipsa3457b82018-03-08 11:30:12 -050035 // this if-test protects against the case where the context is being destroyed
36 // before having been fully created
Robert Phillips9da87e02019-02-04 13:26:26 -050037 if (this->priv().getGpu()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -050038 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
61protected:
Robert Phillipsbb606772019-02-04 17:50:57 -050062 bool init(sk_sp<const GrCaps> caps, sk_sp<GrSkSLFPFactoryCache> FPFactoryCache) override {
63 SkASSERT(caps && !FPFactoryCache);
Robert Phillipsa3457b82018-03-08 11:30:12 -050064 SkASSERT(!fThreadSafeProxy);
Robert Phillipsa3457b82018-03-08 11:30:12 -050065
Robert Phillipsbb606772019-02-04 17:50:57 -050066 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 Phillipsa3457b82018-03-08 11:30:12 -050073 return false;
74 }
75
Robert Phillipsbb606772019-02-04 17:50:57 -050076 SkASSERT(this->caps());
77
Robert Phillipsa3457b82018-03-08 11:30:12 -050078 GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
Robert Phillipsc1541ae2019-02-04 12:05:37 -050079 if (GrContextOptions::Enable::kNo == this->options().fAllowMultipleGlyphCacheTextures ||
Robert Phillipsa3457b82018-03-08 11:30:12 -050080 // multitexturing supported only if range can represent the index + texcoords fully
Robert Phillipsbb606772019-02-04 17:50:57 -050081 !(this->caps()->shaderCaps()->floatIs32Bits() ||
82 this->caps()->shaderCaps()->integerSupport())) {
Robert Phillipsa3457b82018-03-08 11:30:12 -050083 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
84 } else {
85 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
86 }
87
Robert Phillips9da87e02019-02-04 13:26:26 -050088 GrStrikeCache* glyphCache = this->priv().getGlyphCache();
89 GrProxyProvider* proxyProvider = this->priv().proxyProvider();
Robert Phillipsa3457b82018-03-08 11:30:12 -050090
91 fAtlasManager = new GrAtlasManager(proxyProvider, glyphCache,
Robert Phillipsc1541ae2019-02-04 12:05:37 -050092 this->options().fGlyphCacheTextureMaximumBytes,
Robert Phillipsa3457b82018-03-08 11:30:12 -050093 allowMultitexturing);
Robert Phillips9da87e02019-02-04 13:26:26 -050094 this->priv().addOnFlushCallbackObject(fAtlasManager);
Robert Phillipsa3457b82018-03-08 11:30:12 -050095
Robert Phillipsa3457b82018-03-08 11:30:12 -050096 return true;
97 }
98
99 GrAtlasManager* onGetAtlasManager() override { return fAtlasManager; }
100
101private:
102 GrAtlasManager* fAtlasManager;
103
104 typedef GrContext INHERITED;
105};
106
Robert Phillipsa3457b82018-03-08 11:30:12 -0500107sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface) {
108 GrContextOptions defaultOptions;
109 return MakeGL(std::move(interface), defaultOptions);
110}
111
Brian Salomonc1b9c102018-04-06 09:18:00 -0400112sk_sp<GrContext> GrContext::MakeGL(const GrContextOptions& options) {
113 return MakeGL(nullptr, options);
114}
115
116sk_sp<GrContext> GrContext::MakeGL() {
117 GrContextOptions defaultOptions;
118 return MakeGL(nullptr, defaultOptions);
119}
120
Robert Phillipsa3457b82018-03-08 11:30:12 -0500121sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface,
122 const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500123 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kOpenGL, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500124
125 context->fGpu = GrGLGpu::Make(std::move(interface), options, context.get());
126 if (!context->fGpu) {
127 return nullptr;
128 }
129
Robert Phillipsbb606772019-02-04 17:50:57 -0500130 if (!context->init(context->fGpu->refCaps(), nullptr)) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500131 return nullptr;
132 }
133 return context;
134}
135
136sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) {
137 GrContextOptions defaultOptions;
138 return MakeMock(mockOptions, defaultOptions);
139}
140
141sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions,
142 const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500143 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMock, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500144
145 context->fGpu = GrMockGpu::Make(mockOptions, options, context.get());
146 if (!context->fGpu) {
147 return nullptr;
148 }
149
Robert Phillipsbb606772019-02-04 17:50:57 -0500150 if (!context->init(context->fGpu->refCaps(), nullptr)) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500151 return nullptr;
152 }
153 return context;
154}
155
Mike Kleina55e2142018-10-03 16:34:11 +0000156sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext) {
Greg Danielb4d89562018-10-03 18:44:49 +0000157#ifdef SK_VULKAN
Greg Daniel10a83da2018-06-14 09:31:11 -0400158 GrContextOptions defaultOptions;
159 return MakeVulkan(backendContext, defaultOptions);
Greg Danielb4d89562018-10-03 18:44:49 +0000160#else
161 return nullptr;
162#endif
Greg Daniel10a83da2018-06-14 09:31:11 -0400163}
164
165sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext,
166 const GrContextOptions& options) {
Greg Danielb4d89562018-10-03 18:44:49 +0000167#ifdef SK_VULKAN
168 GrContextOptions defaultOptions;
Robert Phillipse8345792019-02-07 10:48:24 -0500169 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kVulkan, options));
Greg Daniel10a83da2018-06-14 09:31:11 -0400170
Greg Danielf730c182018-07-02 20:15:37 +0000171 context->fGpu = GrVkGpu::Make(backendContext, options, context.get());
Robert Phillipsa3457b82018-03-08 11:30:12 -0500172 if (!context->fGpu) {
173 return nullptr;
174 }
175
Robert Phillipsbb606772019-02-04 17:50:57 -0500176 if (!context->init(context->fGpu->refCaps(), nullptr)) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500177 return nullptr;
178 }
179 return context;
Greg Danielb4d89562018-10-03 18:44:49 +0000180#else
181 return nullptr;
Mike Kleina55e2142018-10-03 16:34:11 +0000182#endif
Greg Danielb4d89562018-10-03 18:44:49 +0000183}
Robert Phillipsa3457b82018-03-08 11:30:12 -0500184
185#ifdef SK_METAL
186sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) {
187 GrContextOptions defaultOptions;
188 return MakeMetal(device, queue, defaultOptions);
189}
190
191sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500192 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMetal, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500193
194 context->fGpu = GrMtlTrampoline::MakeGpu(context.get(), options, device, queue);
195 if (!context->fGpu) {
196 return nullptr;
197 }
Timothy Liang4e85e802018-06-28 16:37:18 -0400198
Robert Phillipsbb606772019-02-04 17:50:57 -0500199 if (!context->init(context->fGpu->refCaps(), nullptr)) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500200 return nullptr;
201 }
202 return context;
203}
204#endif
205