blob: 595807ed79bf4583859858f6d4c6aabef6c9ef79 [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"
Robert Phillipse19babf2020-04-06 13:57:30 -040019#include "src/gpu/text/GrAtlasManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#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
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -050027#ifdef SK_DIRECT3D
28#include "src/gpu/d3d/GrD3DGpu.h"
29#endif
Stephen White985741a2019-07-18 11:43:45 -040030#ifdef SK_DAWN
Mike Klein52337de2019-07-25 09:00:52 -050031#include "src/gpu/dawn/GrDawnGpu.h"
Stephen White985741a2019-07-18 11:43:45 -040032#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -050033
Robert Phillips6db27c22019-05-01 10:43:56 -040034#ifdef SK_DISABLE_REDUCE_OPLIST_SPLITTING
Greg Danielf41b2bd2019-08-22 16:19:24 -040035static const bool kDefaultReduceOpsTaskSplitting = false;
Robert Phillips6db27c22019-05-01 10:43:56 -040036#else
Greg Danielf41b2bd2019-08-22 16:19:24 -040037static const bool kDefaultReduceOpsTaskSplitting = false;
Robert Phillips6db27c22019-05-01 10:43:56 -040038#endif
39
Brian Salomon57f211b2019-08-21 15:21:09 -040040class GrLegacyDirectContext : public GrContext {
Robert Phillipsa3457b82018-03-08 11:30:12 -050041public:
Robert Phillipse8345792019-02-07 10:48:24 -050042 GrLegacyDirectContext(GrBackendApi backend, const GrContextOptions& options)
Robert Phillipsc1541ae2019-02-04 12:05:37 -050043 : INHERITED(backend, options)
Robert Phillipsa3457b82018-03-08 11:30:12 -050044 , fAtlasManager(nullptr) {
45 }
46
Robert Phillipse8345792019-02-07 10:48:24 -050047 ~GrLegacyDirectContext() override {
Robert Phillipsa3457b82018-03-08 11:30:12 -050048 // this if-test protects against the case where the context is being destroyed
49 // before having been fully created
Robert Phillips9da87e02019-02-04 13:26:26 -050050 if (this->priv().getGpu()) {
Greg Daniel0a2464f2020-05-14 15:45:44 -040051 this->flushAndSubmit();
Robert Phillipsa3457b82018-03-08 11:30:12 -050052 }
53
54 delete fAtlasManager;
55 }
56
57 void abandonContext() override {
58 INHERITED::abandonContext();
59 fAtlasManager->freeAll();
60 }
61
62 void releaseResourcesAndAbandonContext() override {
63 INHERITED::releaseResourcesAndAbandonContext();
64 fAtlasManager->freeAll();
65 }
66
67 void freeGpuResources() override {
Greg Daniel0a2464f2020-05-14 15:45:44 -040068 this->flushAndSubmit();
Robert Phillipsa3457b82018-03-08 11:30:12 -050069 fAtlasManager->freeAll();
70
71 INHERITED::freeGpuResources();
72 }
73
74protected:
Brian Osman7b1678a2019-12-16 09:17:25 -050075 bool init(sk_sp<const GrCaps> caps) override {
76 SkASSERT(caps);
Robert Phillipsa3457b82018-03-08 11:30:12 -050077 SkASSERT(!fThreadSafeProxy);
Robert Phillipsa3457b82018-03-08 11:30:12 -050078
Robert Phillipsbb606772019-02-04 17:50:57 -050079 fThreadSafeProxy = GrContextThreadSafeProxyPriv::Make(this->backend(),
80 this->options(),
81 this->contextID(),
Brian Osman7b1678a2019-12-16 09:17:25 -050082 caps);
Robert Phillipsbb606772019-02-04 17:50:57 -050083
Brian Osman7b1678a2019-12-16 09:17:25 -050084 if (!INHERITED::init(std::move(caps))) {
Robert Phillipsa3457b82018-03-08 11:30:12 -050085 return false;
86 }
87
Greg Danielf41b2bd2019-08-22 16:19:24 -040088 bool reduceOpsTaskSplitting = kDefaultReduceOpsTaskSplitting;
Greg Daniel93138742019-08-22 17:15:39 -040089 if (GrContextOptions::Enable::kNo == this->options().fReduceOpsTaskSplitting) {
Greg Danielf41b2bd2019-08-22 16:19:24 -040090 reduceOpsTaskSplitting = false;
Greg Daniel93138742019-08-22 17:15:39 -040091 } else if (GrContextOptions::Enable::kYes == this->options().fReduceOpsTaskSplitting) {
Greg Danielf41b2bd2019-08-22 16:19:24 -040092 reduceOpsTaskSplitting = true;
Robert Phillips6db27c22019-05-01 10:43:56 -040093 }
94
Greg Danielf41b2bd2019-08-22 16:19:24 -040095 this->setupDrawingManager(true, reduceOpsTaskSplitting);
Robert Phillips56181ba2019-03-08 12:00:45 -050096
Robert Phillipsbb606772019-02-04 17:50:57 -050097 SkASSERT(this->caps());
98
Robert Phillipsa3457b82018-03-08 11:30:12 -050099 GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500100 if (GrContextOptions::Enable::kNo == this->options().fAllowMultipleGlyphCacheTextures ||
Robert Phillipsa3457b82018-03-08 11:30:12 -0500101 // multitexturing supported only if range can represent the index + texcoords fully
Robert Phillipsbb606772019-02-04 17:50:57 -0500102 !(this->caps()->shaderCaps()->floatIs32Bits() ||
103 this->caps()->shaderCaps()->integerSupport())) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500104 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
105 } else {
106 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
107 }
108
Robert Phillips9da87e02019-02-04 13:26:26 -0500109 GrProxyProvider* proxyProvider = this->priv().proxyProvider();
Robert Phillipsa3457b82018-03-08 11:30:12 -0500110
Robert Phillips207d24b2020-04-09 10:23:42 -0400111 fAtlasManager = new GrAtlasManager(proxyProvider,
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500112 this->options().fGlyphCacheTextureMaximumBytes,
Robert Phillipsa3457b82018-03-08 11:30:12 -0500113 allowMultitexturing);
Robert Phillips9da87e02019-02-04 13:26:26 -0500114 this->priv().addOnFlushCallbackObject(fAtlasManager);
Robert Phillipsa3457b82018-03-08 11:30:12 -0500115
Robert Phillipsa3457b82018-03-08 11:30:12 -0500116 return true;
117 }
118
119 GrAtlasManager* onGetAtlasManager() override { return fAtlasManager; }
120
121private:
122 GrAtlasManager* fAtlasManager;
123
124 typedef GrContext INHERITED;
125};
126
John Rosascoa9b348f2019-11-08 13:18:15 -0800127#ifdef SK_GL
Jim Van Verth03b8ab22020-02-24 11:36:15 -0500128sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> glInterface) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500129 GrContextOptions defaultOptions;
Jim Van Verth03b8ab22020-02-24 11:36:15 -0500130 return MakeGL(std::move(glInterface), defaultOptions);
Robert Phillipsa3457b82018-03-08 11:30:12 -0500131}
132
Brian Salomonc1b9c102018-04-06 09:18:00 -0400133sk_sp<GrContext> GrContext::MakeGL(const GrContextOptions& options) {
134 return MakeGL(nullptr, options);
135}
136
137sk_sp<GrContext> GrContext::MakeGL() {
138 GrContextOptions defaultOptions;
139 return MakeGL(nullptr, defaultOptions);
140}
141
Jim Van Verth03b8ab22020-02-24 11:36:15 -0500142sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> glInterface,
Robert Phillipsa3457b82018-03-08 11:30:12 -0500143 const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500144 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kOpenGL, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500145
Jim Van Verth03b8ab22020-02-24 11:36:15 -0500146 context->fGpu = GrGLGpu::Make(std::move(glInterface), options, context.get());
Robert Phillipsa3457b82018-03-08 11:30:12 -0500147 if (!context->fGpu) {
148 return nullptr;
149 }
150
Brian Osman7b1678a2019-12-16 09:17:25 -0500151 if (!context->init(context->fGpu->refCaps())) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500152 return nullptr;
153 }
154 return context;
155}
John Rosascoa9b348f2019-11-08 13:18:15 -0800156#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -0500157
158sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) {
159 GrContextOptions defaultOptions;
160 return MakeMock(mockOptions, defaultOptions);
161}
162
163sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions,
164 const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500165 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMock, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500166
167 context->fGpu = GrMockGpu::Make(mockOptions, options, context.get());
168 if (!context->fGpu) {
169 return nullptr;
170 }
171
Brian Osman7b1678a2019-12-16 09:17:25 -0500172 if (!context->init(context->fGpu->refCaps())) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500173 return nullptr;
174 }
Chris Daltona378b452019-12-11 13:24:11 -0500175
Robert Phillipsa3457b82018-03-08 11:30:12 -0500176 return context;
177}
178
Mike Kleina55e2142018-10-03 16:34:11 +0000179sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext) {
Greg Danielb4d89562018-10-03 18:44:49 +0000180#ifdef SK_VULKAN
Greg Daniel10a83da2018-06-14 09:31:11 -0400181 GrContextOptions defaultOptions;
182 return MakeVulkan(backendContext, defaultOptions);
Greg Danielb4d89562018-10-03 18:44:49 +0000183#else
184 return nullptr;
185#endif
Greg Daniel10a83da2018-06-14 09:31:11 -0400186}
187
188sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext,
189 const GrContextOptions& options) {
Greg Danielb4d89562018-10-03 18:44:49 +0000190#ifdef SK_VULKAN
Robert Phillipse8345792019-02-07 10:48:24 -0500191 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kVulkan, options));
Greg Daniel10a83da2018-06-14 09:31:11 -0400192
Greg Danielf730c182018-07-02 20:15:37 +0000193 context->fGpu = GrVkGpu::Make(backendContext, options, context.get());
Robert Phillipsa3457b82018-03-08 11:30:12 -0500194 if (!context->fGpu) {
195 return nullptr;
196 }
197
Brian Osman7b1678a2019-12-16 09:17:25 -0500198 if (!context->init(context->fGpu->refCaps())) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500199 return nullptr;
200 }
201 return context;
Greg Danielb4d89562018-10-03 18:44:49 +0000202#else
203 return nullptr;
Mike Kleina55e2142018-10-03 16:34:11 +0000204#endif
Greg Danielb4d89562018-10-03 18:44:49 +0000205}
Robert Phillipsa3457b82018-03-08 11:30:12 -0500206
207#ifdef SK_METAL
208sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) {
209 GrContextOptions defaultOptions;
210 return MakeMetal(device, queue, defaultOptions);
211}
212
213sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) {
Robert Phillipse8345792019-02-07 10:48:24 -0500214 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMetal, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500215
216 context->fGpu = GrMtlTrampoline::MakeGpu(context.get(), options, device, queue);
217 if (!context->fGpu) {
218 return nullptr;
219 }
Timothy Liang4e85e802018-06-28 16:37:18 -0400220
Brian Osman7b1678a2019-12-16 09:17:25 -0500221 if (!context->init(context->fGpu->refCaps())) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500222 return nullptr;
223 }
224 return context;
225}
226#endif
227
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500228#ifdef SK_DIRECT3D
229sk_sp<GrContext> GrContext::MakeDirect3D(const GrD3DBackendContext& backendContext) {
230 GrContextOptions defaultOptions;
231 return MakeDirect3D(backendContext, defaultOptions);
232}
233
234sk_sp<GrContext> GrContext::MakeDirect3D(const GrD3DBackendContext& backendContext,
235 const GrContextOptions& options) {
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500236 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kDirect3D, options));
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500237
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500238 context->fGpu = GrD3DGpu::Make(backendContext, options, context.get());
239 if (!context->fGpu) {
240 return nullptr;
241 }
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500242
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500243 if (!context->init(context->fGpu->refCaps())) {
244 return nullptr;
245 }
246 return context;
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500247}
248#endif
249
Stephen White985741a2019-07-18 11:43:45 -0400250#ifdef SK_DAWN
Stephen Whitea521c962019-12-04 09:57:48 -0500251sk_sp<GrContext> GrContext::MakeDawn(const wgpu::Device& device) {
Stephen White985741a2019-07-18 11:43:45 -0400252 GrContextOptions defaultOptions;
253 return MakeDawn(device, defaultOptions);
254}
255
Stephen Whitea521c962019-12-04 09:57:48 -0500256sk_sp<GrContext> GrContext::MakeDawn(const wgpu::Device& device, const GrContextOptions& options) {
Stephen White985741a2019-07-18 11:43:45 -0400257 sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kDawn, options));
258
259 context->fGpu = GrDawnGpu::Make(device, options, context.get());
260 if (!context->fGpu) {
261 return nullptr;
262 }
263
Brian Osman7b1678a2019-12-16 09:17:25 -0500264 if (!context->init(context->fGpu->refCaps())) {
Stephen White985741a2019-07-18 11:43:45 -0400265 return nullptr;
266 }
267 return context;
268}
269#endif