blob: d187b2af3bf42763d2be5a4be17cbf4e5912a0cc [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/gpu/GrContext.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/gpu/GrContextThreadSafeProxy.h"
12#include "src/gpu/GrContextPriv.h"
13#include "src/gpu/GrContextThreadSafeProxyPriv.h"
14#include "src/gpu/GrGpu.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050015
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/effects/GrSkSLFP.h"
17#include "src/gpu/gl/GrGLGpu.h"
18#include "src/gpu/mock/GrMockGpu.h"
19#include "src/gpu/text/GrStrikeCache.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050020#ifdef SK_METAL
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/mtl/GrMtlTrampoline.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050022#endif
23#ifdef SK_VULKAN
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/vk/GrVkGpu.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050025#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 Phillipsd425dee2019-04-26 11:12:24 -040076 this->setupDrawingManager(true);
Robert Phillips56181ba2019-03-08 12:00:45 -050077
Robert Phillipsbb606772019-02-04 17:50:57 -050078 SkASSERT(this->caps());
79
Robert Phillipsa3457b82018-03-08 11:30:12 -050080 GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
Robert Phillipsc1541ae2019-02-04 12:05:37 -050081 if (GrContextOptions::Enable::kNo == this->options().fAllowMultipleGlyphCacheTextures ||
Robert Phillipsa3457b82018-03-08 11:30:12 -050082 // multitexturing supported only if range can represent the index + texcoords fully
Robert Phillipsbb606772019-02-04 17:50:57 -050083 !(this->caps()->shaderCaps()->floatIs32Bits() ||
84 this->caps()->shaderCaps()->integerSupport())) {
Robert Phillipsa3457b82018-03-08 11:30:12 -050085 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
86 } else {
87 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
88 }
89
Herb Derbya00da612019-03-04 17:10:01 -050090 GrStrikeCache* glyphCache = this->priv().getGrStrikeCache();
Robert Phillips9da87e02019-02-04 13:26:26 -050091 GrProxyProvider* proxyProvider = this->priv().proxyProvider();
Robert Phillipsa3457b82018-03-08 11:30:12 -050092
93 fAtlasManager = new GrAtlasManager(proxyProvider, glyphCache,
Robert Phillipsc1541ae2019-02-04 12:05:37 -050094 this->options().fGlyphCacheTextureMaximumBytes,
Robert Phillipsa3457b82018-03-08 11:30:12 -050095 allowMultitexturing);
Robert Phillips9da87e02019-02-04 13:26:26 -050096 this->priv().addOnFlushCallbackObject(fAtlasManager);
Robert Phillipsa3457b82018-03-08 11:30:12 -050097
Robert Phillipsa3457b82018-03-08 11:30:12 -050098 return true;
99 }
100
101 GrAtlasManager* onGetAtlasManager() override { return fAtlasManager; }
102
103private:
104 GrAtlasManager* fAtlasManager;
105
106 typedef GrContext INHERITED;
107};
108
Robert Phillipsa3457b82018-03-08 11:30:12 -0500109sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface) {
110 GrContextOptions defaultOptions;
111 return MakeGL(std::move(interface), defaultOptions);
112}
113
Brian Salomonc1b9c102018-04-06 09:18:00 -0400114sk_sp<GrContext> GrContext::MakeGL(const GrContextOptions& options) {
115 return MakeGL(nullptr, options);
116}
117
118sk_sp<GrContext> GrContext::MakeGL() {
119 GrContextOptions defaultOptions;
120 return MakeGL(nullptr, defaultOptions);
121}
122
Robert Phillipsa3457b82018-03-08 11:30:12 -0500123sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface,
124 const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500125 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kOpenGL, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500126
127 context->fGpu = GrGLGpu::Make(std::move(interface), options, context.get());
128 if (!context->fGpu) {
129 return nullptr;
130 }
131
Robert Phillipsbb606772019-02-04 17:50:57 -0500132 if (!context->init(context->fGpu->refCaps(), nullptr)) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500133 return nullptr;
134 }
135 return context;
136}
137
138sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) {
139 GrContextOptions defaultOptions;
140 return MakeMock(mockOptions, defaultOptions);
141}
142
143sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions,
144 const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500145 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMock, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500146
147 context->fGpu = GrMockGpu::Make(mockOptions, options, context.get());
148 if (!context->fGpu) {
149 return nullptr;
150 }
151
Robert Phillipsbb606772019-02-04 17:50:57 -0500152 if (!context->init(context->fGpu->refCaps(), nullptr)) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500153 return nullptr;
154 }
155 return context;
156}
157
Mike Kleina55e2142018-10-03 16:34:11 +0000158sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext) {
Greg Danielb4d89562018-10-03 18:44:49 +0000159#ifdef SK_VULKAN
Greg Daniel10a83da2018-06-14 09:31:11 -0400160 GrContextOptions defaultOptions;
161 return MakeVulkan(backendContext, defaultOptions);
Greg Danielb4d89562018-10-03 18:44:49 +0000162#else
163 return nullptr;
164#endif
Greg Daniel10a83da2018-06-14 09:31:11 -0400165}
166
167sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext,
168 const GrContextOptions& options) {
Greg Danielb4d89562018-10-03 18:44:49 +0000169#ifdef SK_VULKAN
170 GrContextOptions defaultOptions;
Robert Phillipse8345792019-02-07 10:48:24 -0500171 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kVulkan, options));
Greg Daniel10a83da2018-06-14 09:31:11 -0400172
Greg Danielf730c182018-07-02 20:15:37 +0000173 context->fGpu = GrVkGpu::Make(backendContext, options, context.get());
Robert Phillipsa3457b82018-03-08 11:30:12 -0500174 if (!context->fGpu) {
175 return nullptr;
176 }
177
Robert Phillipsbb606772019-02-04 17:50:57 -0500178 if (!context->init(context->fGpu->refCaps(), nullptr)) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500179 return nullptr;
180 }
181 return context;
Greg Danielb4d89562018-10-03 18:44:49 +0000182#else
183 return nullptr;
Mike Kleina55e2142018-10-03 16:34:11 +0000184#endif
Greg Danielb4d89562018-10-03 18:44:49 +0000185}
Robert Phillipsa3457b82018-03-08 11:30:12 -0500186
187#ifdef SK_METAL
188sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) {
189 GrContextOptions defaultOptions;
190 return MakeMetal(device, queue, defaultOptions);
191}
192
193sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500194 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMetal, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500195
196 context->fGpu = GrMtlTrampoline::MakeGpu(context.get(), options, device, queue);
197 if (!context->fGpu) {
198 return nullptr;
199 }
Timothy Liang4e85e802018-06-28 16:37:18 -0400200
Robert Phillipsbb606772019-02-04 17:50:57 -0500201 if (!context->init(context->fGpu->refCaps(), nullptr)) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500202 return nullptr;
203 }
204 return context;
205}
206#endif
207