blob: 75fb6e123a92a0143fcad46b885a0a0952c2028e [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 Phillips56181ba2019-03-08 12:00:45 -050076 bool sortOpLists = this->explicitlyAllocateGPUResources();
77 if (GrContextOptions::Enable::kNo == this->options().fSortRenderTargets) {
78 sortOpLists = false;
79 } else if (GrContextOptions::Enable::kYes == this->options().fSortRenderTargets) {
80 sortOpLists = true;
81 }
82
83 this->setupDrawingManager(this->explicitlyAllocateGPUResources(), sortOpLists);
84
Robert Phillipsbb606772019-02-04 17:50:57 -050085 SkASSERT(this->caps());
86
Robert Phillipsa3457b82018-03-08 11:30:12 -050087 GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
Robert Phillipsc1541ae2019-02-04 12:05:37 -050088 if (GrContextOptions::Enable::kNo == this->options().fAllowMultipleGlyphCacheTextures ||
Robert Phillipsa3457b82018-03-08 11:30:12 -050089 // multitexturing supported only if range can represent the index + texcoords fully
Robert Phillipsbb606772019-02-04 17:50:57 -050090 !(this->caps()->shaderCaps()->floatIs32Bits() ||
91 this->caps()->shaderCaps()->integerSupport())) {
Robert Phillipsa3457b82018-03-08 11:30:12 -050092 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
93 } else {
94 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
95 }
96
Herb Derbya00da612019-03-04 17:10:01 -050097 GrStrikeCache* glyphCache = this->priv().getGrStrikeCache();
Robert Phillips9da87e02019-02-04 13:26:26 -050098 GrProxyProvider* proxyProvider = this->priv().proxyProvider();
Robert Phillipsa3457b82018-03-08 11:30:12 -050099
100 fAtlasManager = new GrAtlasManager(proxyProvider, glyphCache,
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500101 this->options().fGlyphCacheTextureMaximumBytes,
Robert Phillipsa3457b82018-03-08 11:30:12 -0500102 allowMultitexturing);
Robert Phillips9da87e02019-02-04 13:26:26 -0500103 this->priv().addOnFlushCallbackObject(fAtlasManager);
Robert Phillipsa3457b82018-03-08 11:30:12 -0500104
Robert Phillipsa3457b82018-03-08 11:30:12 -0500105 return true;
106 }
107
108 GrAtlasManager* onGetAtlasManager() override { return fAtlasManager; }
109
110private:
111 GrAtlasManager* fAtlasManager;
112
113 typedef GrContext INHERITED;
114};
115
Robert Phillipsa3457b82018-03-08 11:30:12 -0500116sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface) {
117 GrContextOptions defaultOptions;
118 return MakeGL(std::move(interface), defaultOptions);
119}
120
Brian Salomonc1b9c102018-04-06 09:18:00 -0400121sk_sp<GrContext> GrContext::MakeGL(const GrContextOptions& options) {
122 return MakeGL(nullptr, options);
123}
124
125sk_sp<GrContext> GrContext::MakeGL() {
126 GrContextOptions defaultOptions;
127 return MakeGL(nullptr, defaultOptions);
128}
129
Robert Phillipsa3457b82018-03-08 11:30:12 -0500130sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface,
131 const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500132 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kOpenGL, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500133
134 context->fGpu = GrGLGpu::Make(std::move(interface), options, context.get());
135 if (!context->fGpu) {
136 return nullptr;
137 }
138
Robert Phillipsbb606772019-02-04 17:50:57 -0500139 if (!context->init(context->fGpu->refCaps(), nullptr)) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500140 return nullptr;
141 }
142 return context;
143}
144
145sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) {
146 GrContextOptions defaultOptions;
147 return MakeMock(mockOptions, defaultOptions);
148}
149
150sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions,
151 const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500152 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMock, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500153
154 context->fGpu = GrMockGpu::Make(mockOptions, options, context.get());
155 if (!context->fGpu) {
156 return nullptr;
157 }
158
Robert Phillipsbb606772019-02-04 17:50:57 -0500159 if (!context->init(context->fGpu->refCaps(), nullptr)) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500160 return nullptr;
161 }
162 return context;
163}
164
Mike Kleina55e2142018-10-03 16:34:11 +0000165sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext) {
Greg Danielb4d89562018-10-03 18:44:49 +0000166#ifdef SK_VULKAN
Greg Daniel10a83da2018-06-14 09:31:11 -0400167 GrContextOptions defaultOptions;
168 return MakeVulkan(backendContext, defaultOptions);
Greg Danielb4d89562018-10-03 18:44:49 +0000169#else
170 return nullptr;
171#endif
Greg Daniel10a83da2018-06-14 09:31:11 -0400172}
173
174sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext,
175 const GrContextOptions& options) {
Greg Danielb4d89562018-10-03 18:44:49 +0000176#ifdef SK_VULKAN
177 GrContextOptions defaultOptions;
Robert Phillipse8345792019-02-07 10:48:24 -0500178 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kVulkan, options));
Greg Daniel10a83da2018-06-14 09:31:11 -0400179
Greg Danielf730c182018-07-02 20:15:37 +0000180 context->fGpu = GrVkGpu::Make(backendContext, options, context.get());
Robert Phillipsa3457b82018-03-08 11:30:12 -0500181 if (!context->fGpu) {
182 return nullptr;
183 }
184
Robert Phillipsbb606772019-02-04 17:50:57 -0500185 if (!context->init(context->fGpu->refCaps(), nullptr)) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500186 return nullptr;
187 }
188 return context;
Greg Danielb4d89562018-10-03 18:44:49 +0000189#else
190 return nullptr;
Mike Kleina55e2142018-10-03 16:34:11 +0000191#endif
Greg Danielb4d89562018-10-03 18:44:49 +0000192}
Robert Phillipsa3457b82018-03-08 11:30:12 -0500193
194#ifdef SK_METAL
195sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) {
196 GrContextOptions defaultOptions;
197 return MakeMetal(device, queue, defaultOptions);
198}
199
200sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500201 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMetal, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500202
203 context->fGpu = GrMtlTrampoline::MakeGpu(context.get(), options, device, queue);
204 if (!context->fGpu) {
205 return nullptr;
206 }
Timothy Liang4e85e802018-06-28 16:37:18 -0400207
Robert Phillipsbb606772019-02-04 17:50:57 -0500208 if (!context->init(context->fGpu->refCaps(), nullptr)) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500209 return nullptr;
210 }
211 return context;
212}
213#endif
214