blob: f5c6f953162c6c64463441e0d9848842d94e6e9a [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)
Robert Phillips3262bc82020-08-10 12:11:58 -040048 : INHERITED(GrContextThreadSafeProxyPriv::Make(backend, options)) {
Robert Phillipsad248452020-06-30 09:27:52 -040049}
Robert Phillipsa3457b82018-03-08 11:30:12 -050050
Robert Phillipsad248452020-06-30 09:27:52 -040051GrDirectContext::~GrDirectContext() {
52 // this if-test protects against the case where the context is being destroyed
53 // before having been fully created
54 if (this->priv().getGpu()) {
Greg Daniel0a2464f2020-05-14 15:45:44 -040055 this->flushAndSubmit();
Robert Phillipsa3457b82018-03-08 11:30:12 -050056 }
Robert Phillipsad248452020-06-30 09:27:52 -040057}
Robert Phillipsa3457b82018-03-08 11:30:12 -050058
Robert Phillipsad248452020-06-30 09:27:52 -040059void GrDirectContext::abandonContext() {
60 INHERITED::abandonContext();
61 fAtlasManager->freeAll();
62}
Robert Phillipsa3457b82018-03-08 11:30:12 -050063
Robert Phillipsad248452020-06-30 09:27:52 -040064void GrDirectContext::releaseResourcesAndAbandonContext() {
65 INHERITED::releaseResourcesAndAbandonContext();
66 fAtlasManager->freeAll();
67}
Robert Phillips6db27c22019-05-01 10:43:56 -040068
Robert Phillipsad248452020-06-30 09:27:52 -040069void GrDirectContext::freeGpuResources() {
70 this->flushAndSubmit();
71 fAtlasManager->freeAll();
Robert Phillips56181ba2019-03-08 12:00:45 -050072
Robert Phillipsad248452020-06-30 09:27:52 -040073 INHERITED::freeGpuResources();
74}
Robert Phillipsa3457b82018-03-08 11:30:12 -050075
Robert Phillipsad248452020-06-30 09:27:52 -040076bool GrDirectContext::init() {
77 const GrGpu* gpu = this->priv().getGpu();
78 if (!gpu) {
79 return false;
Robert Phillipsa3457b82018-03-08 11:30:12 -050080 }
81
Robert Phillipsad248452020-06-30 09:27:52 -040082 fThreadSafeProxy->priv().init(gpu->refCaps());
83 if (!INHERITED::init()) {
84 return false;
85 }
Robert Phillipsa3457b82018-03-08 11:30:12 -050086
Robert Phillipsad248452020-06-30 09:27:52 -040087 bool reduceOpsTaskSplitting = kDefaultReduceOpsTaskSplitting;
88 if (GrContextOptions::Enable::kNo == this->options().fReduceOpsTaskSplitting) {
89 reduceOpsTaskSplitting = false;
90 } else if (GrContextOptions::Enable::kYes == this->options().fReduceOpsTaskSplitting) {
91 reduceOpsTaskSplitting = true;
92 }
Robert Phillipsa3457b82018-03-08 11:30:12 -050093
Robert Phillipsad248452020-06-30 09:27:52 -040094 this->setupDrawingManager(true, reduceOpsTaskSplitting);
95
96 GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
97 if (GrContextOptions::Enable::kNo == this->options().fAllowMultipleGlyphCacheTextures ||
98 // multitexturing supported only if range can represent the index + texcoords fully
99 !(this->caps()->shaderCaps()->floatIs32Bits() ||
100 this->caps()->shaderCaps()->integerSupport())) {
101 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
102 } else {
103 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
104 }
105
106 GrProxyProvider* proxyProvider = this->priv().proxyProvider();
107
Robert Phillips3262bc82020-08-10 12:11:58 -0400108 fAtlasManager = std::make_unique<GrAtlasManager>(proxyProvider,
109 this->options().fGlyphCacheTextureMaximumBytes,
110 allowMultitexturing);
111 this->priv().addOnFlushCallbackObject(fAtlasManager.get());
Robert Phillipsad248452020-06-30 09:27:52 -0400112
113 return true;
114}
Robert Phillipsa3457b82018-03-08 11:30:12 -0500115
John Rosascoa9b348f2019-11-08 13:18:15 -0800116#ifdef SK_GL
Robert Phillipsf4f80112020-07-13 16:13:31 -0400117/*************************************************************************************************/
Robert Phillipsc7228c62020-07-14 12:57:39 -0400118#ifndef SK_DISABLE_LEGACY_CONTEXT_FACTORIES
119
Jim Van Verth03b8ab22020-02-24 11:36:15 -0500120sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> glInterface) {
Robert Phillipsf4f80112020-07-13 16:13:31 -0400121 return GrDirectContext::MakeGL(std::move(glInterface));
122}
123
124sk_sp<GrContext> GrContext::MakeGL(const GrContextOptions& options) {
125 return GrDirectContext::MakeGL(options);
126}
127
128sk_sp<GrContext> GrContext::MakeGL() {
129 return GrDirectContext::MakeGL();
130}
131
132sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> glInterface,
133 const GrContextOptions& options) {
134 return GrDirectContext::MakeGL(std::move(glInterface), options);
135}
136
Robert Phillipsc7228c62020-07-14 12:57:39 -0400137#endif
138
Robert Phillipsf4f80112020-07-13 16:13:31 -0400139/*************************************************************************************************/
140sk_sp<GrDirectContext> GrDirectContext::MakeGL(sk_sp<const GrGLInterface> glInterface) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500141 GrContextOptions defaultOptions;
Jim Van Verth03b8ab22020-02-24 11:36:15 -0500142 return MakeGL(std::move(glInterface), defaultOptions);
Robert Phillipsa3457b82018-03-08 11:30:12 -0500143}
144
Robert Phillipsf4f80112020-07-13 16:13:31 -0400145sk_sp<GrDirectContext> GrDirectContext::MakeGL(const GrContextOptions& options) {
Brian Salomonc1b9c102018-04-06 09:18:00 -0400146 return MakeGL(nullptr, options);
147}
148
Robert Phillipsf4f80112020-07-13 16:13:31 -0400149sk_sp<GrDirectContext> GrDirectContext::MakeGL() {
Brian Salomonc1b9c102018-04-06 09:18:00 -0400150 GrContextOptions defaultOptions;
151 return MakeGL(nullptr, defaultOptions);
152}
153
Brian Salomon24069eb2020-06-24 10:19:52 -0400154#if GR_TEST_UTILS
155GrGLFunction<GrGLGetErrorFn> make_get_error_with_random_oom(GrGLFunction<GrGLGetErrorFn> original) {
156 // A SkRandom and a GrGLFunction<GrGLGetErrorFn> are too big to be captured by a
157 // GrGLFunction<GrGLGetError> (surprise, surprise). So we make a context object and
158 // capture that by pointer. However, GrGLFunction doesn't support calling a destructor
159 // on the thing it captures. So we leak the context.
160 struct GetErrorContext {
161 SkRandom fRandom;
162 GrGLFunction<GrGLGetErrorFn> fGetError;
163 };
164
165 auto errorContext = new GetErrorContext;
166
167#if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS)
168 __lsan_ignore_object(errorContext);
169#endif
170
171 errorContext->fGetError = original;
172
173 return GrGLFunction<GrGLGetErrorFn>([errorContext]() {
174 GrGLenum error = errorContext->fGetError();
175 if (error == GR_GL_NO_ERROR && (errorContext->fRandom.nextU() % 300) == 0) {
176 error = GR_GL_OUT_OF_MEMORY;
177 }
178 return error;
179 });
180}
181#endif
182
Robert Phillipsf4f80112020-07-13 16:13:31 -0400183sk_sp<GrDirectContext> GrDirectContext::MakeGL(sk_sp<const GrGLInterface> glInterface,
184 const GrContextOptions& options) {
185 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kOpenGL, options));
Brian Salomon24069eb2020-06-24 10:19:52 -0400186#if GR_TEST_UTILS
187 if (options.fRandomGLOOM) {
188 auto copy = sk_make_sp<GrGLInterface>(*glInterface);
189 copy->fFunctions.fGetError =
190 make_get_error_with_random_oom(glInterface->fFunctions.fGetError);
191#if GR_GL_CHECK_ERROR
192 // Suppress logging GL errors since we'll be synthetically generating them.
193 copy->suppressErrorLogging();
194#endif
195 glInterface = std::move(copy);
196 }
197#endif
Robert Phillipsf4f80112020-07-13 16:13:31 -0400198 direct->fGpu = GrGLGpu::Make(std::move(glInterface), options, direct.get());
199 if (!direct->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500200 return nullptr;
201 }
Robert Phillipsf4f80112020-07-13 16:13:31 -0400202 return direct;
Robert Phillipsa3457b82018-03-08 11:30:12 -0500203}
John Rosascoa9b348f2019-11-08 13:18:15 -0800204#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -0500205
Robert Phillipsf4f80112020-07-13 16:13:31 -0400206/*************************************************************************************************/
Robert Phillipsc7228c62020-07-14 12:57:39 -0400207#ifndef SK_DISABLE_LEGACY_CONTEXT_FACTORIES
208
Robert Phillipsa3457b82018-03-08 11:30:12 -0500209sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) {
Robert Phillipsf4f80112020-07-13 16:13:31 -0400210 return GrDirectContext::MakeMock(mockOptions);
Robert Phillipsa3457b82018-03-08 11:30:12 -0500211}
212
213sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions,
214 const GrContextOptions& options) {
Robert Phillipsf4f80112020-07-13 16:13:31 -0400215 return GrDirectContext::MakeMock(mockOptions, options);
216}
Robert Phillipsa3457b82018-03-08 11:30:12 -0500217
Robert Phillipsc7228c62020-07-14 12:57:39 -0400218#endif
219
Robert Phillipsf4f80112020-07-13 16:13:31 -0400220/*************************************************************************************************/
221sk_sp<GrDirectContext> GrDirectContext::MakeMock(const GrMockOptions* mockOptions) {
222 GrContextOptions defaultOptions;
223 return MakeMock(mockOptions, defaultOptions);
224}
225
226sk_sp<GrDirectContext> GrDirectContext::MakeMock(const GrMockOptions* mockOptions,
227 const GrContextOptions& options) {
228 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kMock, options));
229
230 direct->fGpu = GrMockGpu::Make(mockOptions, options, direct.get());
231 if (!direct->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500232 return nullptr;
233 }
Chris Daltona378b452019-12-11 13:24:11 -0500234
Robert Phillipsf4f80112020-07-13 16:13:31 -0400235 return direct;
Robert Phillipsa3457b82018-03-08 11:30:12 -0500236}
237
Greg Danielb4d89562018-10-03 18:44:49 +0000238#ifdef SK_VULKAN
Robert Phillipsf4f80112020-07-13 16:13:31 -0400239/*************************************************************************************************/
Robert Phillipsc7228c62020-07-14 12:57:39 -0400240#ifndef SK_DISABLE_LEGACY_CONTEXT_FACTORIES
241
Robert Phillipsf4f80112020-07-13 16:13:31 -0400242sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext) {
243 return GrDirectContext::MakeVulkan(backendContext);
Greg Daniel10a83da2018-06-14 09:31:11 -0400244}
245
246sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext,
247 const GrContextOptions& options) {
Robert Phillipsf4f80112020-07-13 16:13:31 -0400248 return GrDirectContext::MakeVulkan(backendContext, options);
249}
Greg Daniel10a83da2018-06-14 09:31:11 -0400250
Robert Phillipsc7228c62020-07-14 12:57:39 -0400251#endif
252
Robert Phillipsf4f80112020-07-13 16:13:31 -0400253/*************************************************************************************************/
254sk_sp<GrDirectContext> GrDirectContext::MakeVulkan(const GrVkBackendContext& backendContext) {
255 GrContextOptions defaultOptions;
256 return MakeVulkan(backendContext, defaultOptions);
257}
258
259sk_sp<GrDirectContext> GrDirectContext::MakeVulkan(const GrVkBackendContext& backendContext,
260 const GrContextOptions& options) {
261 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kVulkan, options));
262
263 direct->fGpu = GrVkGpu::Make(backendContext, options, direct.get());
264 if (!direct->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500265 return nullptr;
266 }
267
Robert Phillipsf4f80112020-07-13 16:13:31 -0400268 return direct;
Greg Danielb4d89562018-10-03 18:44:49 +0000269}
Robert Phillipsf4f80112020-07-13 16:13:31 -0400270#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -0500271
272#ifdef SK_METAL
Robert Phillipsf4f80112020-07-13 16:13:31 -0400273/*************************************************************************************************/
Robert Phillipsc7228c62020-07-14 12:57:39 -0400274#ifndef SK_DISABLE_LEGACY_CONTEXT_FACTORIES
275
Robert Phillipsa3457b82018-03-08 11:30:12 -0500276sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) {
Robert Phillipsf4f80112020-07-13 16:13:31 -0400277 return GrDirectContext::MakeMetal(device, queue);
278}
279
280sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) {
281 return GrDirectContext::MakeMetal(device, queue, options);
282}
283
Robert Phillipsc7228c62020-07-14 12:57:39 -0400284#endif
285
Robert Phillipsf4f80112020-07-13 16:13:31 -0400286/*************************************************************************************************/
287sk_sp<GrDirectContext> GrDirectContext::MakeMetal(void* device, void* queue) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500288 GrContextOptions defaultOptions;
289 return MakeMetal(device, queue, defaultOptions);
290}
291
Robert Phillipsf4f80112020-07-13 16:13:31 -0400292sk_sp<GrDirectContext> GrDirectContext::MakeMetal(void* device, void* queue,
293 const GrContextOptions& options) {
294 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kMetal, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500295
Robert Phillipsf4f80112020-07-13 16:13:31 -0400296 direct->fGpu = GrMtlTrampoline::MakeGpu(direct.get(), options, device, queue);
297 if (!direct->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500298 return nullptr;
299 }
Timothy Liang4e85e802018-06-28 16:37:18 -0400300
Robert Phillipsf4f80112020-07-13 16:13:31 -0400301 return direct;
Robert Phillipsa3457b82018-03-08 11:30:12 -0500302}
303#endif
304
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500305#ifdef SK_DIRECT3D
Robert Phillipsf4f80112020-07-13 16:13:31 -0400306/*************************************************************************************************/
Robert Phillipsc7228c62020-07-14 12:57:39 -0400307#ifndef SK_DISABLE_LEGACY_CONTEXT_FACTORIES
308
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500309sk_sp<GrContext> GrContext::MakeDirect3D(const GrD3DBackendContext& backendContext) {
Robert Phillipsf4f80112020-07-13 16:13:31 -0400310 return GrDirectContext::MakeDirect3D(backendContext);
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500311}
312
313sk_sp<GrContext> GrContext::MakeDirect3D(const GrD3DBackendContext& backendContext,
314 const GrContextOptions& options) {
Robert Phillipsf4f80112020-07-13 16:13:31 -0400315 return GrDirectContext::MakeDirect3D(backendContext, options);
316}
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500317
Robert Phillipsc7228c62020-07-14 12:57:39 -0400318#endif
319
Robert Phillipsf4f80112020-07-13 16:13:31 -0400320/*************************************************************************************************/
321sk_sp<GrDirectContext> GrDirectContext::MakeDirect3D(const GrD3DBackendContext& backendContext) {
322 GrContextOptions defaultOptions;
323 return MakeDirect3D(backendContext, defaultOptions);
324}
325
326sk_sp<GrDirectContext> GrDirectContext::MakeDirect3D(const GrD3DBackendContext& backendContext,
327 const GrContextOptions& options) {
328 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kDirect3D, options));
329
330 direct->fGpu = GrD3DGpu::Make(backendContext, options, direct.get());
331 if (!direct->init()) {
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500332 return nullptr;
333 }
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500334
Robert Phillipsf4f80112020-07-13 16:13:31 -0400335 return direct;
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500336}
337#endif
338
Stephen White985741a2019-07-18 11:43:45 -0400339#ifdef SK_DAWN
Robert Phillipsf4f80112020-07-13 16:13:31 -0400340/*************************************************************************************************/
Robert Phillipsc7228c62020-07-14 12:57:39 -0400341#ifndef SK_DISABLE_LEGACY_CONTEXT_FACTORIES
342
Stephen Whitea521c962019-12-04 09:57:48 -0500343sk_sp<GrContext> GrContext::MakeDawn(const wgpu::Device& device) {
Robert Phillipsf4f80112020-07-13 16:13:31 -0400344 return GrDirectContext::MakeDawn(device);
345}
346
347sk_sp<GrContext> GrContext::MakeDawn(const wgpu::Device& device, const GrContextOptions& options) {
348 return GrDirectContext::MakeDawn(device, options);
349}
350
Robert Phillipsc7228c62020-07-14 12:57:39 -0400351#endif
352
Robert Phillipsf4f80112020-07-13 16:13:31 -0400353/*************************************************************************************************/
354sk_sp<GrDirectContext> GrDirectContext::MakeDawn(const wgpu::Device& device) {
Stephen White985741a2019-07-18 11:43:45 -0400355 GrContextOptions defaultOptions;
356 return MakeDawn(device, defaultOptions);
357}
358
Robert Phillipsf4f80112020-07-13 16:13:31 -0400359sk_sp<GrDirectContext> GrDirectContext::MakeDawn(const wgpu::Device& device,
360 const GrContextOptions& options) {
361 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kDawn, options));
Stephen White985741a2019-07-18 11:43:45 -0400362
Robert Phillipsf4f80112020-07-13 16:13:31 -0400363 direct->fGpu = GrDawnGpu::Make(device, options, direct.get());
364 if (!direct->init()) {
Stephen White985741a2019-07-18 11:43:45 -0400365 return nullptr;
366 }
367
Robert Phillipsf4f80112020-07-13 16:13:31 -0400368 return direct;
Stephen White985741a2019-07-18 11:43:45 -0400369}
Robert Phillipsf4f80112020-07-13 16:13:31 -0400370
Stephen White985741a2019-07-18 11:43:45 -0400371#endif