blob: d03726e0eeed039285894d48fc36dc32a3e692d7 [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 Phillipsb7bfbc22020-07-01 12:55:01 -04009#include "include/gpu/GrDirectContext.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
Brian Salomon24069eb2020-06-24 10:19:52 -040034#if GR_TEST_UTILS
35# include "include/utils/SkRandom.h"
36# if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS)
37# include <sanitizer/lsan_interface.h>
38# endif
39#endif
40
Robert Phillips6db27c22019-05-01 10:43:56 -040041#ifdef SK_DISABLE_REDUCE_OPLIST_SPLITTING
Greg Danielf41b2bd2019-08-22 16:19:24 -040042static const bool kDefaultReduceOpsTaskSplitting = false;
Robert Phillips6db27c22019-05-01 10:43:56 -040043#else
Greg Danielf41b2bd2019-08-22 16:19:24 -040044static const bool kDefaultReduceOpsTaskSplitting = false;
Robert Phillips6db27c22019-05-01 10:43:56 -040045#endif
46
Robert Phillipsad248452020-06-30 09:27:52 -040047GrDirectContext::GrDirectContext(GrBackendApi backend, const GrContextOptions& options)
48 : INHERITED(GrContextThreadSafeProxyPriv::Make(backend, options))
49 , fAtlasManager(nullptr) {
50}
Robert Phillipsa3457b82018-03-08 11:30:12 -050051
Robert Phillipsad248452020-06-30 09:27:52 -040052GrDirectContext::~GrDirectContext() {
53 // this if-test protects against the case where the context is being destroyed
54 // before having been fully created
55 if (this->priv().getGpu()) {
Greg Daniel0a2464f2020-05-14 15:45:44 -040056 this->flushAndSubmit();
Robert Phillipsa3457b82018-03-08 11:30:12 -050057 }
58
Robert Phillipsad248452020-06-30 09:27:52 -040059 delete fAtlasManager;
60}
Robert Phillipsa3457b82018-03-08 11:30:12 -050061
Robert Phillipsad248452020-06-30 09:27:52 -040062void GrDirectContext::abandonContext() {
63 INHERITED::abandonContext();
64 fAtlasManager->freeAll();
65}
Robert Phillipsa3457b82018-03-08 11:30:12 -050066
Robert Phillipsad248452020-06-30 09:27:52 -040067void GrDirectContext::releaseResourcesAndAbandonContext() {
68 INHERITED::releaseResourcesAndAbandonContext();
69 fAtlasManager->freeAll();
70}
Robert Phillips6db27c22019-05-01 10:43:56 -040071
Robert Phillipsad248452020-06-30 09:27:52 -040072void GrDirectContext::freeGpuResources() {
73 this->flushAndSubmit();
74 fAtlasManager->freeAll();
Robert Phillips56181ba2019-03-08 12:00:45 -050075
Robert Phillipsad248452020-06-30 09:27:52 -040076 INHERITED::freeGpuResources();
77}
Robert Phillipsa3457b82018-03-08 11:30:12 -050078
Robert Phillipsad248452020-06-30 09:27:52 -040079bool GrDirectContext::init() {
80 const GrGpu* gpu = this->priv().getGpu();
81 if (!gpu) {
82 return false;
Robert Phillipsa3457b82018-03-08 11:30:12 -050083 }
84
Robert Phillipsad248452020-06-30 09:27:52 -040085 fThreadSafeProxy->priv().init(gpu->refCaps());
86 if (!INHERITED::init()) {
87 return false;
88 }
Robert Phillipsa3457b82018-03-08 11:30:12 -050089
Robert Phillipsad248452020-06-30 09:27:52 -040090 bool reduceOpsTaskSplitting = kDefaultReduceOpsTaskSplitting;
91 if (GrContextOptions::Enable::kNo == this->options().fReduceOpsTaskSplitting) {
92 reduceOpsTaskSplitting = false;
93 } else if (GrContextOptions::Enable::kYes == this->options().fReduceOpsTaskSplitting) {
94 reduceOpsTaskSplitting = true;
95 }
Robert Phillipsa3457b82018-03-08 11:30:12 -050096
Robert Phillipsad248452020-06-30 09:27:52 -040097 this->setupDrawingManager(true, reduceOpsTaskSplitting);
98
99 GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
100 if (GrContextOptions::Enable::kNo == this->options().fAllowMultipleGlyphCacheTextures ||
101 // multitexturing supported only if range can represent the index + texcoords fully
102 !(this->caps()->shaderCaps()->floatIs32Bits() ||
103 this->caps()->shaderCaps()->integerSupport())) {
104 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
105 } else {
106 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
107 }
108
109 GrProxyProvider* proxyProvider = this->priv().proxyProvider();
110
111 fAtlasManager = new GrAtlasManager(proxyProvider,
112 this->options().fGlyphCacheTextureMaximumBytes,
113 allowMultitexturing);
114 this->priv().addOnFlushCallbackObject(fAtlasManager);
115
116 return true;
117}
Robert Phillipsa3457b82018-03-08 11:30:12 -0500118
John Rosascoa9b348f2019-11-08 13:18:15 -0800119#ifdef SK_GL
Robert Phillipsf4f80112020-07-13 16:13:31 -0400120/*************************************************************************************************/
Robert Phillipsc7228c62020-07-14 12:57:39 -0400121#ifndef SK_DISABLE_LEGACY_CONTEXT_FACTORIES
122
Jim Van Verth03b8ab22020-02-24 11:36:15 -0500123sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> glInterface) {
Robert Phillipsf4f80112020-07-13 16:13:31 -0400124 return GrDirectContext::MakeGL(std::move(glInterface));
125}
126
127sk_sp<GrContext> GrContext::MakeGL(const GrContextOptions& options) {
128 return GrDirectContext::MakeGL(options);
129}
130
131sk_sp<GrContext> GrContext::MakeGL() {
132 return GrDirectContext::MakeGL();
133}
134
135sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> glInterface,
136 const GrContextOptions& options) {
137 return GrDirectContext::MakeGL(std::move(glInterface), options);
138}
139
Robert Phillipsc7228c62020-07-14 12:57:39 -0400140#endif
141
Robert Phillipsf4f80112020-07-13 16:13:31 -0400142/*************************************************************************************************/
143sk_sp<GrDirectContext> GrDirectContext::MakeGL(sk_sp<const GrGLInterface> glInterface) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500144 GrContextOptions defaultOptions;
Jim Van Verth03b8ab22020-02-24 11:36:15 -0500145 return MakeGL(std::move(glInterface), defaultOptions);
Robert Phillipsa3457b82018-03-08 11:30:12 -0500146}
147
Robert Phillipsf4f80112020-07-13 16:13:31 -0400148sk_sp<GrDirectContext> GrDirectContext::MakeGL(const GrContextOptions& options) {
Brian Salomonc1b9c102018-04-06 09:18:00 -0400149 return MakeGL(nullptr, options);
150}
151
Robert Phillipsf4f80112020-07-13 16:13:31 -0400152sk_sp<GrDirectContext> GrDirectContext::MakeGL() {
Brian Salomonc1b9c102018-04-06 09:18:00 -0400153 GrContextOptions defaultOptions;
154 return MakeGL(nullptr, defaultOptions);
155}
156
Brian Salomon24069eb2020-06-24 10:19:52 -0400157#if GR_TEST_UTILS
158GrGLFunction<GrGLGetErrorFn> make_get_error_with_random_oom(GrGLFunction<GrGLGetErrorFn> original) {
159 // A SkRandom and a GrGLFunction<GrGLGetErrorFn> are too big to be captured by a
160 // GrGLFunction<GrGLGetError> (surprise, surprise). So we make a context object and
161 // capture that by pointer. However, GrGLFunction doesn't support calling a destructor
162 // on the thing it captures. So we leak the context.
163 struct GetErrorContext {
164 SkRandom fRandom;
165 GrGLFunction<GrGLGetErrorFn> fGetError;
166 };
167
168 auto errorContext = new GetErrorContext;
169
170#if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS)
171 __lsan_ignore_object(errorContext);
172#endif
173
174 errorContext->fGetError = original;
175
176 return GrGLFunction<GrGLGetErrorFn>([errorContext]() {
177 GrGLenum error = errorContext->fGetError();
178 if (error == GR_GL_NO_ERROR && (errorContext->fRandom.nextU() % 300) == 0) {
179 error = GR_GL_OUT_OF_MEMORY;
180 }
181 return error;
182 });
183}
184#endif
185
Robert Phillipsf4f80112020-07-13 16:13:31 -0400186sk_sp<GrDirectContext> GrDirectContext::MakeGL(sk_sp<const GrGLInterface> glInterface,
187 const GrContextOptions& options) {
188 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kOpenGL, options));
Brian Salomon24069eb2020-06-24 10:19:52 -0400189#if GR_TEST_UTILS
190 if (options.fRandomGLOOM) {
191 auto copy = sk_make_sp<GrGLInterface>(*glInterface);
192 copy->fFunctions.fGetError =
193 make_get_error_with_random_oom(glInterface->fFunctions.fGetError);
194#if GR_GL_CHECK_ERROR
195 // Suppress logging GL errors since we'll be synthetically generating them.
196 copy->suppressErrorLogging();
197#endif
198 glInterface = std::move(copy);
199 }
200#endif
Robert Phillipsf4f80112020-07-13 16:13:31 -0400201 direct->fGpu = GrGLGpu::Make(std::move(glInterface), options, direct.get());
202 if (!direct->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500203 return nullptr;
204 }
Robert Phillipsf4f80112020-07-13 16:13:31 -0400205 return direct;
Robert Phillipsa3457b82018-03-08 11:30:12 -0500206}
John Rosascoa9b348f2019-11-08 13:18:15 -0800207#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -0500208
Robert Phillipsf4f80112020-07-13 16:13:31 -0400209/*************************************************************************************************/
Robert Phillipsc7228c62020-07-14 12:57:39 -0400210#ifndef SK_DISABLE_LEGACY_CONTEXT_FACTORIES
211
Robert Phillipsa3457b82018-03-08 11:30:12 -0500212sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) {
Robert Phillipsf4f80112020-07-13 16:13:31 -0400213 return GrDirectContext::MakeMock(mockOptions);
Robert Phillipsa3457b82018-03-08 11:30:12 -0500214}
215
216sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions,
217 const GrContextOptions& options) {
Robert Phillipsf4f80112020-07-13 16:13:31 -0400218 return GrDirectContext::MakeMock(mockOptions, options);
219}
Robert Phillipsa3457b82018-03-08 11:30:12 -0500220
Robert Phillipsc7228c62020-07-14 12:57:39 -0400221#endif
222
Robert Phillipsf4f80112020-07-13 16:13:31 -0400223/*************************************************************************************************/
224sk_sp<GrDirectContext> GrDirectContext::MakeMock(const GrMockOptions* mockOptions) {
225 GrContextOptions defaultOptions;
226 return MakeMock(mockOptions, defaultOptions);
227}
228
229sk_sp<GrDirectContext> GrDirectContext::MakeMock(const GrMockOptions* mockOptions,
230 const GrContextOptions& options) {
231 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kMock, options));
232
233 direct->fGpu = GrMockGpu::Make(mockOptions, options, direct.get());
234 if (!direct->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500235 return nullptr;
236 }
Chris Daltona378b452019-12-11 13:24:11 -0500237
Robert Phillipsf4f80112020-07-13 16:13:31 -0400238 return direct;
Robert Phillipsa3457b82018-03-08 11:30:12 -0500239}
240
Greg Danielb4d89562018-10-03 18:44:49 +0000241#ifdef SK_VULKAN
Robert Phillipsf4f80112020-07-13 16:13:31 -0400242/*************************************************************************************************/
Robert Phillipsc7228c62020-07-14 12:57:39 -0400243#ifndef SK_DISABLE_LEGACY_CONTEXT_FACTORIES
244
Robert Phillipsf4f80112020-07-13 16:13:31 -0400245sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext) {
246 return GrDirectContext::MakeVulkan(backendContext);
Greg Daniel10a83da2018-06-14 09:31:11 -0400247}
248
249sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext,
250 const GrContextOptions& options) {
Robert Phillipsf4f80112020-07-13 16:13:31 -0400251 return GrDirectContext::MakeVulkan(backendContext, options);
252}
Greg Daniel10a83da2018-06-14 09:31:11 -0400253
Robert Phillipsc7228c62020-07-14 12:57:39 -0400254#endif
255
Robert Phillipsf4f80112020-07-13 16:13:31 -0400256/*************************************************************************************************/
257sk_sp<GrDirectContext> GrDirectContext::MakeVulkan(const GrVkBackendContext& backendContext) {
258 GrContextOptions defaultOptions;
259 return MakeVulkan(backendContext, defaultOptions);
260}
261
262sk_sp<GrDirectContext> GrDirectContext::MakeVulkan(const GrVkBackendContext& backendContext,
263 const GrContextOptions& options) {
264 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kVulkan, options));
265
266 direct->fGpu = GrVkGpu::Make(backendContext, options, direct.get());
267 if (!direct->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500268 return nullptr;
269 }
270
Robert Phillipsf4f80112020-07-13 16:13:31 -0400271 return direct;
Greg Danielb4d89562018-10-03 18:44:49 +0000272}
Robert Phillipsf4f80112020-07-13 16:13:31 -0400273#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -0500274
275#ifdef SK_METAL
Robert Phillipsf4f80112020-07-13 16:13:31 -0400276/*************************************************************************************************/
Robert Phillipsc7228c62020-07-14 12:57:39 -0400277#ifndef SK_DISABLE_LEGACY_CONTEXT_FACTORIES
278
Robert Phillipsa3457b82018-03-08 11:30:12 -0500279sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) {
Robert Phillipsf4f80112020-07-13 16:13:31 -0400280 return GrDirectContext::MakeMetal(device, queue);
281}
282
283sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) {
284 return GrDirectContext::MakeMetal(device, queue, options);
285}
286
Robert Phillipsc7228c62020-07-14 12:57:39 -0400287#endif
288
Robert Phillipsf4f80112020-07-13 16:13:31 -0400289/*************************************************************************************************/
290sk_sp<GrDirectContext> GrDirectContext::MakeMetal(void* device, void* queue) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500291 GrContextOptions defaultOptions;
292 return MakeMetal(device, queue, defaultOptions);
293}
294
Robert Phillipsf4f80112020-07-13 16:13:31 -0400295sk_sp<GrDirectContext> GrDirectContext::MakeMetal(void* device, void* queue,
296 const GrContextOptions& options) {
297 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kMetal, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500298
Robert Phillipsf4f80112020-07-13 16:13:31 -0400299 direct->fGpu = GrMtlTrampoline::MakeGpu(direct.get(), options, device, queue);
300 if (!direct->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500301 return nullptr;
302 }
Timothy Liang4e85e802018-06-28 16:37:18 -0400303
Robert Phillipsf4f80112020-07-13 16:13:31 -0400304 return direct;
Robert Phillipsa3457b82018-03-08 11:30:12 -0500305}
306#endif
307
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500308#ifdef SK_DIRECT3D
Robert Phillipsf4f80112020-07-13 16:13:31 -0400309/*************************************************************************************************/
Robert Phillipsc7228c62020-07-14 12:57:39 -0400310#ifndef SK_DISABLE_LEGACY_CONTEXT_FACTORIES
311
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500312sk_sp<GrContext> GrContext::MakeDirect3D(const GrD3DBackendContext& backendContext) {
Robert Phillipsf4f80112020-07-13 16:13:31 -0400313 return GrDirectContext::MakeDirect3D(backendContext);
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500314}
315
316sk_sp<GrContext> GrContext::MakeDirect3D(const GrD3DBackendContext& backendContext,
317 const GrContextOptions& options) {
Robert Phillipsf4f80112020-07-13 16:13:31 -0400318 return GrDirectContext::MakeDirect3D(backendContext, options);
319}
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500320
Robert Phillipsc7228c62020-07-14 12:57:39 -0400321#endif
322
Robert Phillipsf4f80112020-07-13 16:13:31 -0400323/*************************************************************************************************/
324sk_sp<GrDirectContext> GrDirectContext::MakeDirect3D(const GrD3DBackendContext& backendContext) {
325 GrContextOptions defaultOptions;
326 return MakeDirect3D(backendContext, defaultOptions);
327}
328
329sk_sp<GrDirectContext> GrDirectContext::MakeDirect3D(const GrD3DBackendContext& backendContext,
330 const GrContextOptions& options) {
331 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kDirect3D, options));
332
333 direct->fGpu = GrD3DGpu::Make(backendContext, options, direct.get());
334 if (!direct->init()) {
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500335 return nullptr;
336 }
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500337
Robert Phillipsf4f80112020-07-13 16:13:31 -0400338 return direct;
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500339}
340#endif
341
Stephen White985741a2019-07-18 11:43:45 -0400342#ifdef SK_DAWN
Robert Phillipsf4f80112020-07-13 16:13:31 -0400343/*************************************************************************************************/
Robert Phillipsc7228c62020-07-14 12:57:39 -0400344#ifndef SK_DISABLE_LEGACY_CONTEXT_FACTORIES
345
Stephen Whitea521c962019-12-04 09:57:48 -0500346sk_sp<GrContext> GrContext::MakeDawn(const wgpu::Device& device) {
Robert Phillipsf4f80112020-07-13 16:13:31 -0400347 return GrDirectContext::MakeDawn(device);
348}
349
350sk_sp<GrContext> GrContext::MakeDawn(const wgpu::Device& device, const GrContextOptions& options) {
351 return GrDirectContext::MakeDawn(device, options);
352}
353
Robert Phillipsc7228c62020-07-14 12:57:39 -0400354#endif
355
Robert Phillipsf4f80112020-07-13 16:13:31 -0400356/*************************************************************************************************/
357sk_sp<GrDirectContext> GrDirectContext::MakeDawn(const wgpu::Device& device) {
Stephen White985741a2019-07-18 11:43:45 -0400358 GrContextOptions defaultOptions;
359 return MakeDawn(device, defaultOptions);
360}
361
Robert Phillipsf4f80112020-07-13 16:13:31 -0400362sk_sp<GrDirectContext> GrDirectContext::MakeDawn(const wgpu::Device& device,
363 const GrContextOptions& options) {
364 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kDawn, options));
Stephen White985741a2019-07-18 11:43:45 -0400365
Robert Phillipsf4f80112020-07-13 16:13:31 -0400366 direct->fGpu = GrDawnGpu::Make(device, options, direct.get());
367 if (!direct->init()) {
Stephen White985741a2019-07-18 11:43:45 -0400368 return nullptr;
369 }
370
Robert Phillipsf4f80112020-07-13 16:13:31 -0400371 return direct;
Stephen White985741a2019-07-18 11:43:45 -0400372}
Robert Phillipsf4f80112020-07-13 16:13:31 -0400373
Stephen White985741a2019-07-18 11:43:45 -0400374#endif