blob: 34002a88faeb22f4c4104a21d34a4c891ddf8e15 [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
Stephen White985741a2019-07-18 11:43:45 -040026#ifdef SK_DAWN
Mike Klein52337de2019-07-25 09:00:52 -050027#include "src/gpu/dawn/GrDawnGpu.h"
Stephen White985741a2019-07-18 11:43:45 -040028#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -050029
Robert Phillips6db27c22019-05-01 10:43:56 -040030#ifdef SK_DISABLE_REDUCE_OPLIST_SPLITTING
Greg Danielf41b2bd2019-08-22 16:19:24 -040031static const bool kDefaultReduceOpsTaskSplitting = false;
Robert Phillips6db27c22019-05-01 10:43:56 -040032#else
Greg Danielf41b2bd2019-08-22 16:19:24 -040033static const bool kDefaultReduceOpsTaskSplitting = false;
Robert Phillips6db27c22019-05-01 10:43:56 -040034#endif
35
Brian Salomon57f211b2019-08-21 15:21:09 -040036class GrLegacyDirectContext : public GrContext {
Robert Phillipsa3457b82018-03-08 11:30:12 -050037public:
Robert Phillipse8345792019-02-07 10:48:24 -050038 GrLegacyDirectContext(GrBackendApi backend, const GrContextOptions& options)
Robert Phillipsc1541ae2019-02-04 12:05:37 -050039 : INHERITED(backend, options)
Robert Phillipsa3457b82018-03-08 11:30:12 -050040 , fAtlasManager(nullptr) {
41 }
42
Robert Phillipse8345792019-02-07 10:48:24 -050043 ~GrLegacyDirectContext() override {
Robert Phillipsa3457b82018-03-08 11:30:12 -050044 // this if-test protects against the case where the context is being destroyed
45 // before having been fully created
Robert Phillips9da87e02019-02-04 13:26:26 -050046 if (this->priv().getGpu()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -050047 this->flush();
48 }
49
50 delete fAtlasManager;
51 }
52
53 void abandonContext() override {
54 INHERITED::abandonContext();
55 fAtlasManager->freeAll();
56 }
57
58 void releaseResourcesAndAbandonContext() override {
59 INHERITED::releaseResourcesAndAbandonContext();
60 fAtlasManager->freeAll();
61 }
62
63 void freeGpuResources() override {
64 this->flush();
65 fAtlasManager->freeAll();
66
67 INHERITED::freeGpuResources();
68 }
69
70protected:
Robert Phillipsbb606772019-02-04 17:50:57 -050071 bool init(sk_sp<const GrCaps> caps, sk_sp<GrSkSLFPFactoryCache> FPFactoryCache) override {
72 SkASSERT(caps && !FPFactoryCache);
Robert Phillipsa3457b82018-03-08 11:30:12 -050073 SkASSERT(!fThreadSafeProxy);
Robert Phillipsa3457b82018-03-08 11:30:12 -050074
Robert Phillipsbb606772019-02-04 17:50:57 -050075 FPFactoryCache.reset(new GrSkSLFPFactoryCache());
76 fThreadSafeProxy = GrContextThreadSafeProxyPriv::Make(this->backend(),
77 this->options(),
78 this->contextID(),
79 caps, FPFactoryCache);
80
81 if (!INHERITED::init(std::move(caps), std::move(FPFactoryCache))) {
Robert Phillipsa3457b82018-03-08 11:30:12 -050082 return false;
83 }
84
Greg Danielf41b2bd2019-08-22 16:19:24 -040085 bool reduceOpsTaskSplitting = kDefaultReduceOpsTaskSplitting;
Greg Daniel93138742019-08-22 17:15:39 -040086 if (GrContextOptions::Enable::kNo == this->options().fReduceOpsTaskSplitting) {
Greg Danielf41b2bd2019-08-22 16:19:24 -040087 reduceOpsTaskSplitting = false;
Greg Daniel93138742019-08-22 17:15:39 -040088 } else if (GrContextOptions::Enable::kYes == this->options().fReduceOpsTaskSplitting) {
Greg Danielf41b2bd2019-08-22 16:19:24 -040089 reduceOpsTaskSplitting = true;
Robert Phillips6db27c22019-05-01 10:43:56 -040090 }
91
Greg Danielf41b2bd2019-08-22 16:19:24 -040092 this->setupDrawingManager(true, reduceOpsTaskSplitting);
Robert Phillips56181ba2019-03-08 12:00:45 -050093
Robert Phillipsbb606772019-02-04 17:50:57 -050094 SkASSERT(this->caps());
95
Robert Phillipsa3457b82018-03-08 11:30:12 -050096 GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
Robert Phillipsc1541ae2019-02-04 12:05:37 -050097 if (GrContextOptions::Enable::kNo == this->options().fAllowMultipleGlyphCacheTextures ||
Robert Phillipsa3457b82018-03-08 11:30:12 -050098 // multitexturing supported only if range can represent the index + texcoords fully
Robert Phillipsbb606772019-02-04 17:50:57 -050099 !(this->caps()->shaderCaps()->floatIs32Bits() ||
100 this->caps()->shaderCaps()->integerSupport())) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500101 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
102 } else {
103 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
104 }
105
Herb Derbya00da612019-03-04 17:10:01 -0500106 GrStrikeCache* glyphCache = this->priv().getGrStrikeCache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500107 GrProxyProvider* proxyProvider = this->priv().proxyProvider();
Robert Phillipsa3457b82018-03-08 11:30:12 -0500108
109 fAtlasManager = new GrAtlasManager(proxyProvider, glyphCache,
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500110 this->options().fGlyphCacheTextureMaximumBytes,
Robert Phillipsa3457b82018-03-08 11:30:12 -0500111 allowMultitexturing);
Robert Phillips9da87e02019-02-04 13:26:26 -0500112 this->priv().addOnFlushCallbackObject(fAtlasManager);
Robert Phillipsa3457b82018-03-08 11:30:12 -0500113
Robert Phillipsa3457b82018-03-08 11:30:12 -0500114 return true;
115 }
116
117 GrAtlasManager* onGetAtlasManager() override { return fAtlasManager; }
118
119private:
120 GrAtlasManager* fAtlasManager;
121
122 typedef GrContext INHERITED;
123};
124
John Rosascoa9b348f2019-11-08 13:18:15 -0800125#ifdef SK_GL
Robert Phillipsa3457b82018-03-08 11:30:12 -0500126sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface) {
127 GrContextOptions defaultOptions;
128 return MakeGL(std::move(interface), defaultOptions);
129}
130
Brian Salomonc1b9c102018-04-06 09:18:00 -0400131sk_sp<GrContext> GrContext::MakeGL(const GrContextOptions& options) {
132 return MakeGL(nullptr, options);
133}
134
135sk_sp<GrContext> GrContext::MakeGL() {
136 GrContextOptions defaultOptions;
137 return MakeGL(nullptr, defaultOptions);
138}
139
Robert Phillipsa3457b82018-03-08 11:30:12 -0500140sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface,
141 const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500142 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kOpenGL, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500143
144 context->fGpu = GrGLGpu::Make(std::move(interface), options, context.get());
145 if (!context->fGpu) {
146 return nullptr;
147 }
148
Robert Phillipsbb606772019-02-04 17:50:57 -0500149 if (!context->init(context->fGpu->refCaps(), nullptr)) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500150 return nullptr;
151 }
152 return context;
153}
John Rosascoa9b348f2019-11-08 13:18:15 -0800154#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -0500155
156sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) {
157 GrContextOptions defaultOptions;
158 return MakeMock(mockOptions, defaultOptions);
159}
160
161sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions,
162 const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500163 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMock, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500164
165 context->fGpu = GrMockGpu::Make(mockOptions, options, context.get());
166 if (!context->fGpu) {
167 return nullptr;
168 }
169
Robert Phillipsbb606772019-02-04 17:50:57 -0500170 if (!context->init(context->fGpu->refCaps(), nullptr)) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500171 return nullptr;
172 }
173 return context;
174}
175
Mike Kleina55e2142018-10-03 16:34:11 +0000176sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext) {
Greg Danielb4d89562018-10-03 18:44:49 +0000177#ifdef SK_VULKAN
Greg Daniel10a83da2018-06-14 09:31:11 -0400178 GrContextOptions defaultOptions;
179 return MakeVulkan(backendContext, defaultOptions);
Greg Danielb4d89562018-10-03 18:44:49 +0000180#else
181 return nullptr;
182#endif
Greg Daniel10a83da2018-06-14 09:31:11 -0400183}
184
185sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext,
186 const GrContextOptions& options) {
Greg Danielb4d89562018-10-03 18:44:49 +0000187#ifdef SK_VULKAN
188 GrContextOptions defaultOptions;
Robert Phillipse8345792019-02-07 10:48:24 -0500189 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kVulkan, options));
Greg Daniel10a83da2018-06-14 09:31:11 -0400190
Greg Danielf730c182018-07-02 20:15:37 +0000191 context->fGpu = GrVkGpu::Make(backendContext, options, context.get());
Robert Phillipsa3457b82018-03-08 11:30:12 -0500192 if (!context->fGpu) {
193 return nullptr;
194 }
195
Robert Phillipsbb606772019-02-04 17:50:57 -0500196 if (!context->init(context->fGpu->refCaps(), nullptr)) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500197 return nullptr;
198 }
199 return context;
Greg Danielb4d89562018-10-03 18:44:49 +0000200#else
201 return nullptr;
Mike Kleina55e2142018-10-03 16:34:11 +0000202#endif
Greg Danielb4d89562018-10-03 18:44:49 +0000203}
Robert Phillipsa3457b82018-03-08 11:30:12 -0500204
205#ifdef SK_METAL
206sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) {
207 GrContextOptions defaultOptions;
208 return MakeMetal(device, queue, defaultOptions);
209}
210
211sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500212 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMetal, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500213
214 context->fGpu = GrMtlTrampoline::MakeGpu(context.get(), options, device, queue);
215 if (!context->fGpu) {
216 return nullptr;
217 }
Timothy Liang4e85e802018-06-28 16:37:18 -0400218
Robert Phillipsbb606772019-02-04 17:50:57 -0500219 if (!context->init(context->fGpu->refCaps(), nullptr)) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500220 return nullptr;
221 }
222 return context;
223}
224#endif
225
Stephen White985741a2019-07-18 11:43:45 -0400226#ifdef SK_DAWN
Stephen Whitea521c962019-12-04 09:57:48 -0500227sk_sp<GrContext> GrContext::MakeDawn(const wgpu::Device& device) {
Stephen White985741a2019-07-18 11:43:45 -0400228 GrContextOptions defaultOptions;
229 return MakeDawn(device, defaultOptions);
230}
231
Stephen Whitea521c962019-12-04 09:57:48 -0500232sk_sp<GrContext> GrContext::MakeDawn(const wgpu::Device& device, const GrContextOptions& options) {
Stephen White985741a2019-07-18 11:43:45 -0400233 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kDawn, options));
234
235 context->fGpu = GrDawnGpu::Make(device, options, context.get());
236 if (!context->fGpu) {
237 return nullptr;
238 }
239
240 if (!context->init(context->fGpu->refCaps(), nullptr)) {
241 return nullptr;
242 }
243 return context;
244}
245#endif