blob: b760e027fd50eb9c16a2890e256c218fd8b68f98 [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 Phillips7f11fb52019-12-03 13:35:19 -050015#include "src/gpu/GrSkSLFPFactoryCache.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050016
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/effects/GrSkSLFP.h"
18#include "src/gpu/gl/GrGLGpu.h"
19#include "src/gpu/mock/GrMockGpu.h"
20#include "src/gpu/text/GrStrikeCache.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050021#ifdef SK_METAL
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/mtl/GrMtlTrampoline.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050023#endif
24#ifdef SK_VULKAN
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/vk/GrVkGpu.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050026#endif
Stephen White985741a2019-07-18 11:43:45 -040027#ifdef SK_DAWN
Mike Klein52337de2019-07-25 09:00:52 -050028#include "src/gpu/dawn/GrDawnGpu.h"
Stephen White985741a2019-07-18 11:43:45 -040029#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -050030
Robert Phillips6db27c22019-05-01 10:43:56 -040031#ifdef SK_DISABLE_REDUCE_OPLIST_SPLITTING
Greg Danielf41b2bd2019-08-22 16:19:24 -040032static const bool kDefaultReduceOpsTaskSplitting = false;
Robert Phillips6db27c22019-05-01 10:43:56 -040033#else
Greg Danielf41b2bd2019-08-22 16:19:24 -040034static const bool kDefaultReduceOpsTaskSplitting = false;
Robert Phillips6db27c22019-05-01 10:43:56 -040035#endif
36
Brian Salomon57f211b2019-08-21 15:21:09 -040037class GrLegacyDirectContext : public GrContext {
Robert Phillipsa3457b82018-03-08 11:30:12 -050038public:
Robert Phillipse8345792019-02-07 10:48:24 -050039 GrLegacyDirectContext(GrBackendApi backend, const GrContextOptions& options)
Robert Phillipsc1541ae2019-02-04 12:05:37 -050040 : INHERITED(backend, options)
Robert Phillipsa3457b82018-03-08 11:30:12 -050041 , fAtlasManager(nullptr) {
42 }
43
Robert Phillipse8345792019-02-07 10:48:24 -050044 ~GrLegacyDirectContext() override {
Robert Phillipsa3457b82018-03-08 11:30:12 -050045 // this if-test protects against the case where the context is being destroyed
46 // before having been fully created
Robert Phillips9da87e02019-02-04 13:26:26 -050047 if (this->priv().getGpu()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -050048 this->flush();
49 }
50
51 delete fAtlasManager;
52 }
53
54 void abandonContext() override {
55 INHERITED::abandonContext();
56 fAtlasManager->freeAll();
57 }
58
59 void releaseResourcesAndAbandonContext() override {
60 INHERITED::releaseResourcesAndAbandonContext();
61 fAtlasManager->freeAll();
62 }
63
64 void freeGpuResources() override {
65 this->flush();
66 fAtlasManager->freeAll();
67
68 INHERITED::freeGpuResources();
69 }
70
71protected:
Robert Phillipsbb606772019-02-04 17:50:57 -050072 bool init(sk_sp<const GrCaps> caps, sk_sp<GrSkSLFPFactoryCache> FPFactoryCache) override {
73 SkASSERT(caps && !FPFactoryCache);
Robert Phillipsa3457b82018-03-08 11:30:12 -050074 SkASSERT(!fThreadSafeProxy);
Robert Phillipsa3457b82018-03-08 11:30:12 -050075
Robert Phillips7f11fb52019-12-03 13:35:19 -050076 FPFactoryCache.reset(new GrSkSLFPFactoryCache(caps->refShaderCaps()));
Robert Phillipsbb606772019-02-04 17:50:57 -050077 fThreadSafeProxy = GrContextThreadSafeProxyPriv::Make(this->backend(),
78 this->options(),
79 this->contextID(),
80 caps, FPFactoryCache);
81
82 if (!INHERITED::init(std::move(caps), std::move(FPFactoryCache))) {
Robert Phillipsa3457b82018-03-08 11:30:12 -050083 return false;
84 }
85
Greg Danielf41b2bd2019-08-22 16:19:24 -040086 bool reduceOpsTaskSplitting = kDefaultReduceOpsTaskSplitting;
Greg Daniel93138742019-08-22 17:15:39 -040087 if (GrContextOptions::Enable::kNo == this->options().fReduceOpsTaskSplitting) {
Greg Danielf41b2bd2019-08-22 16:19:24 -040088 reduceOpsTaskSplitting = false;
Greg Daniel93138742019-08-22 17:15:39 -040089 } else if (GrContextOptions::Enable::kYes == this->options().fReduceOpsTaskSplitting) {
Greg Danielf41b2bd2019-08-22 16:19:24 -040090 reduceOpsTaskSplitting = true;
Robert Phillips6db27c22019-05-01 10:43:56 -040091 }
92
Greg Danielf41b2bd2019-08-22 16:19:24 -040093 this->setupDrawingManager(true, reduceOpsTaskSplitting);
Robert Phillips56181ba2019-03-08 12:00:45 -050094
Robert Phillipsbb606772019-02-04 17:50:57 -050095 SkASSERT(this->caps());
96
Robert Phillipsa3457b82018-03-08 11:30:12 -050097 GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
Robert Phillipsc1541ae2019-02-04 12:05:37 -050098 if (GrContextOptions::Enable::kNo == this->options().fAllowMultipleGlyphCacheTextures ||
Robert Phillipsa3457b82018-03-08 11:30:12 -050099 // multitexturing supported only if range can represent the index + texcoords fully
Robert Phillipsbb606772019-02-04 17:50:57 -0500100 !(this->caps()->shaderCaps()->floatIs32Bits() ||
101 this->caps()->shaderCaps()->integerSupport())) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500102 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
103 } else {
104 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
105 }
106
Herb Derbya00da612019-03-04 17:10:01 -0500107 GrStrikeCache* glyphCache = this->priv().getGrStrikeCache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500108 GrProxyProvider* proxyProvider = this->priv().proxyProvider();
Robert Phillipsa3457b82018-03-08 11:30:12 -0500109
110 fAtlasManager = new GrAtlasManager(proxyProvider, glyphCache,
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500111 this->options().fGlyphCacheTextureMaximumBytes,
Robert Phillipsa3457b82018-03-08 11:30:12 -0500112 allowMultitexturing);
Robert Phillips9da87e02019-02-04 13:26:26 -0500113 this->priv().addOnFlushCallbackObject(fAtlasManager);
Robert Phillipsa3457b82018-03-08 11:30:12 -0500114
Robert Phillipsa3457b82018-03-08 11:30:12 -0500115 return true;
116 }
117
118 GrAtlasManager* onGetAtlasManager() override { return fAtlasManager; }
119
120private:
121 GrAtlasManager* fAtlasManager;
122
123 typedef GrContext INHERITED;
124};
125
John Rosascoa9b348f2019-11-08 13:18:15 -0800126#ifdef SK_GL
Robert Phillipsa3457b82018-03-08 11:30:12 -0500127sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface) {
128 GrContextOptions defaultOptions;
129 return MakeGL(std::move(interface), defaultOptions);
130}
131
Brian Salomonc1b9c102018-04-06 09:18:00 -0400132sk_sp<GrContext> GrContext::MakeGL(const GrContextOptions& options) {
133 return MakeGL(nullptr, options);
134}
135
136sk_sp<GrContext> GrContext::MakeGL() {
137 GrContextOptions defaultOptions;
138 return MakeGL(nullptr, defaultOptions);
139}
140
Robert Phillipsa3457b82018-03-08 11:30:12 -0500141sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface,
142 const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500143 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kOpenGL, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500144
145 context->fGpu = GrGLGpu::Make(std::move(interface), 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}
John Rosascoa9b348f2019-11-08 13:18:15 -0800155#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -0500156
157sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) {
158 GrContextOptions defaultOptions;
159 return MakeMock(mockOptions, defaultOptions);
160}
161
162sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions,
163 const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500164 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMock, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500165
166 context->fGpu = GrMockGpu::Make(mockOptions, options, context.get());
167 if (!context->fGpu) {
168 return nullptr;
169 }
170
Robert Phillipsbb606772019-02-04 17:50:57 -0500171 if (!context->init(context->fGpu->refCaps(), nullptr)) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500172 return nullptr;
173 }
Chris Daltona378b452019-12-11 13:24:11 -0500174
175#if GR_TEST_UTILS
176 if (mockOptions && mockOptions->fFailTextureAllocations) {
177 context->testingOnly_setSuppressAllocationWarnings();
178 }
179#endif
180
Robert Phillipsa3457b82018-03-08 11:30:12 -0500181 return context;
182}
183
Mike Kleina55e2142018-10-03 16:34:11 +0000184sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext) {
Greg Danielb4d89562018-10-03 18:44:49 +0000185#ifdef SK_VULKAN
Greg Daniel10a83da2018-06-14 09:31:11 -0400186 GrContextOptions defaultOptions;
187 return MakeVulkan(backendContext, defaultOptions);
Greg Danielb4d89562018-10-03 18:44:49 +0000188#else
189 return nullptr;
190#endif
Greg Daniel10a83da2018-06-14 09:31:11 -0400191}
192
193sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext,
194 const GrContextOptions& options) {
Greg Danielb4d89562018-10-03 18:44:49 +0000195#ifdef SK_VULKAN
196 GrContextOptions defaultOptions;
Robert Phillipse8345792019-02-07 10:48:24 -0500197 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kVulkan, options));
Greg Daniel10a83da2018-06-14 09:31:11 -0400198
Greg Danielf730c182018-07-02 20:15:37 +0000199 context->fGpu = GrVkGpu::Make(backendContext, options, context.get());
Robert Phillipsa3457b82018-03-08 11:30:12 -0500200 if (!context->fGpu) {
201 return nullptr;
202 }
203
Robert Phillipsbb606772019-02-04 17:50:57 -0500204 if (!context->init(context->fGpu->refCaps(), nullptr)) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500205 return nullptr;
206 }
207 return context;
Greg Danielb4d89562018-10-03 18:44:49 +0000208#else
209 return nullptr;
Mike Kleina55e2142018-10-03 16:34:11 +0000210#endif
Greg Danielb4d89562018-10-03 18:44:49 +0000211}
Robert Phillipsa3457b82018-03-08 11:30:12 -0500212
213#ifdef SK_METAL
214sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) {
215 GrContextOptions defaultOptions;
216 return MakeMetal(device, queue, defaultOptions);
217}
218
219sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500220 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMetal, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500221
222 context->fGpu = GrMtlTrampoline::MakeGpu(context.get(), options, device, queue);
223 if (!context->fGpu) {
224 return nullptr;
225 }
Timothy Liang4e85e802018-06-28 16:37:18 -0400226
Robert Phillipsbb606772019-02-04 17:50:57 -0500227 if (!context->init(context->fGpu->refCaps(), nullptr)) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500228 return nullptr;
229 }
230 return context;
231}
232#endif
233
Stephen White985741a2019-07-18 11:43:45 -0400234#ifdef SK_DAWN
Stephen Whitea521c962019-12-04 09:57:48 -0500235sk_sp<GrContext> GrContext::MakeDawn(const wgpu::Device& device) {
Stephen White985741a2019-07-18 11:43:45 -0400236 GrContextOptions defaultOptions;
237 return MakeDawn(device, defaultOptions);
238}
239
Stephen Whitea521c962019-12-04 09:57:48 -0500240sk_sp<GrContext> GrContext::MakeDawn(const wgpu::Device& device, const GrContextOptions& options) {
Stephen White985741a2019-07-18 11:43:45 -0400241 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kDawn, options));
242
243 context->fGpu = GrDawnGpu::Make(device, options, context.get());
244 if (!context->fGpu) {
245 return nullptr;
246 }
247
248 if (!context->init(context->fGpu->refCaps(), nullptr)) {
249 return nullptr;
250 }
251 return context;
252}
253#endif