blob: 4ae1831ab662c8bfa0ba27de70827bb946bff5d0 [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 Phillips5edf5102020-08-10 16:30:36 -040019#include "src/gpu/ops/GrSmallPathAtlasMgr.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040020#include "src/gpu/text/GrAtlasManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/text/GrStrikeCache.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050022#ifdef SK_METAL
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/mtl/GrMtlTrampoline.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050024#endif
25#ifdef SK_VULKAN
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/vk/GrVkGpu.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050027#endif
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -050028#ifdef SK_DIRECT3D
29#include "src/gpu/d3d/GrD3DGpu.h"
30#endif
Stephen White985741a2019-07-18 11:43:45 -040031#ifdef SK_DAWN
Mike Klein52337de2019-07-25 09:00:52 -050032#include "src/gpu/dawn/GrDawnGpu.h"
Stephen White985741a2019-07-18 11:43:45 -040033#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -050034
Brian Salomon24069eb2020-06-24 10:19:52 -040035#if GR_TEST_UTILS
36# include "include/utils/SkRandom.h"
37# if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS)
38# include <sanitizer/lsan_interface.h>
39# endif
40#endif
41
Robert Phillips6db27c22019-05-01 10:43:56 -040042#ifdef SK_DISABLE_REDUCE_OPLIST_SPLITTING
Greg Danielf41b2bd2019-08-22 16:19:24 -040043static const bool kDefaultReduceOpsTaskSplitting = false;
Robert Phillips6db27c22019-05-01 10:43:56 -040044#else
Greg Danielf41b2bd2019-08-22 16:19:24 -040045static const bool kDefaultReduceOpsTaskSplitting = false;
Robert Phillips6db27c22019-05-01 10:43:56 -040046#endif
47
Robert Phillipsad248452020-06-30 09:27:52 -040048GrDirectContext::GrDirectContext(GrBackendApi backend, const GrContextOptions& options)
Robert Phillips3262bc82020-08-10 12:11:58 -040049 : INHERITED(GrContextThreadSafeProxyPriv::Make(backend, options)) {
Robert Phillipsad248452020-06-30 09:27:52 -040050}
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 }
Robert Phillipsad248452020-06-30 09:27:52 -040058}
Robert Phillipsa3457b82018-03-08 11:30:12 -050059
Robert Phillipsad248452020-06-30 09:27:52 -040060void GrDirectContext::abandonContext() {
61 INHERITED::abandonContext();
62 fAtlasManager->freeAll();
63}
Robert Phillipsa3457b82018-03-08 11:30:12 -050064
Robert Phillipsad248452020-06-30 09:27:52 -040065void GrDirectContext::releaseResourcesAndAbandonContext() {
66 INHERITED::releaseResourcesAndAbandonContext();
67 fAtlasManager->freeAll();
68}
Robert Phillips6db27c22019-05-01 10:43:56 -040069
Robert Phillipsad248452020-06-30 09:27:52 -040070void GrDirectContext::freeGpuResources() {
71 this->flushAndSubmit();
72 fAtlasManager->freeAll();
Robert Phillips56181ba2019-03-08 12:00:45 -050073
Robert Phillipsad248452020-06-30 09:27:52 -040074 INHERITED::freeGpuResources();
75}
Robert Phillipsa3457b82018-03-08 11:30:12 -050076
Robert Phillipsad248452020-06-30 09:27:52 -040077bool GrDirectContext::init() {
78 const GrGpu* gpu = this->priv().getGpu();
79 if (!gpu) {
80 return false;
Robert Phillipsa3457b82018-03-08 11:30:12 -050081 }
82
Robert Phillipsad248452020-06-30 09:27:52 -040083 fThreadSafeProxy->priv().init(gpu->refCaps());
84 if (!INHERITED::init()) {
85 return false;
86 }
Robert Phillipsa3457b82018-03-08 11:30:12 -050087
Robert Phillipsad248452020-06-30 09:27:52 -040088 bool reduceOpsTaskSplitting = kDefaultReduceOpsTaskSplitting;
89 if (GrContextOptions::Enable::kNo == this->options().fReduceOpsTaskSplitting) {
90 reduceOpsTaskSplitting = false;
91 } else if (GrContextOptions::Enable::kYes == this->options().fReduceOpsTaskSplitting) {
92 reduceOpsTaskSplitting = true;
93 }
Robert Phillipsa3457b82018-03-08 11:30:12 -050094
Robert Phillipsad248452020-06-30 09:27:52 -040095 this->setupDrawingManager(true, reduceOpsTaskSplitting);
96
97 GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
98 if (GrContextOptions::Enable::kNo == this->options().fAllowMultipleGlyphCacheTextures ||
99 // multitexturing supported only if range can represent the index + texcoords fully
100 !(this->caps()->shaderCaps()->floatIs32Bits() ||
101 this->caps()->shaderCaps()->integerSupport())) {
102 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
103 } else {
104 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
105 }
106
107 GrProxyProvider* proxyProvider = this->priv().proxyProvider();
108
Robert Phillips3262bc82020-08-10 12:11:58 -0400109 fAtlasManager = std::make_unique<GrAtlasManager>(proxyProvider,
110 this->options().fGlyphCacheTextureMaximumBytes,
111 allowMultitexturing);
112 this->priv().addOnFlushCallbackObject(fAtlasManager.get());
Robert Phillipsad248452020-06-30 09:27:52 -0400113
114 return true;
115}
Robert Phillipsa3457b82018-03-08 11:30:12 -0500116
Robert Phillips5edf5102020-08-10 16:30:36 -0400117GrSmallPathAtlasMgr* GrDirectContext::onGetSmallPathAtlasMgr() {
118 // The small path renderer atlas will be created here
119 return nullptr;
120}
121
John Rosascoa9b348f2019-11-08 13:18:15 -0800122#ifdef SK_GL
Robert Phillipsf4f80112020-07-13 16:13:31 -0400123/*************************************************************************************************/
Robert Phillipsc7228c62020-07-14 12:57:39 -0400124#ifndef SK_DISABLE_LEGACY_CONTEXT_FACTORIES
125
Jim Van Verth03b8ab22020-02-24 11:36:15 -0500126sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> glInterface) {
Robert Phillipsf4f80112020-07-13 16:13:31 -0400127 return GrDirectContext::MakeGL(std::move(glInterface));
128}
129
130sk_sp<GrContext> GrContext::MakeGL(const GrContextOptions& options) {
131 return GrDirectContext::MakeGL(options);
132}
133
134sk_sp<GrContext> GrContext::MakeGL() {
135 return GrDirectContext::MakeGL();
136}
137
138sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> glInterface,
139 const GrContextOptions& options) {
140 return GrDirectContext::MakeGL(std::move(glInterface), options);
141}
142
Robert Phillipsc7228c62020-07-14 12:57:39 -0400143#endif
144
Robert Phillipsf4f80112020-07-13 16:13:31 -0400145/*************************************************************************************************/
146sk_sp<GrDirectContext> GrDirectContext::MakeGL(sk_sp<const GrGLInterface> glInterface) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500147 GrContextOptions defaultOptions;
Jim Van Verth03b8ab22020-02-24 11:36:15 -0500148 return MakeGL(std::move(glInterface), defaultOptions);
Robert Phillipsa3457b82018-03-08 11:30:12 -0500149}
150
Robert Phillipsf4f80112020-07-13 16:13:31 -0400151sk_sp<GrDirectContext> GrDirectContext::MakeGL(const GrContextOptions& options) {
Brian Salomonc1b9c102018-04-06 09:18:00 -0400152 return MakeGL(nullptr, options);
153}
154
Robert Phillipsf4f80112020-07-13 16:13:31 -0400155sk_sp<GrDirectContext> GrDirectContext::MakeGL() {
Brian Salomonc1b9c102018-04-06 09:18:00 -0400156 GrContextOptions defaultOptions;
157 return MakeGL(nullptr, defaultOptions);
158}
159
Brian Salomon24069eb2020-06-24 10:19:52 -0400160#if GR_TEST_UTILS
161GrGLFunction<GrGLGetErrorFn> make_get_error_with_random_oom(GrGLFunction<GrGLGetErrorFn> original) {
162 // A SkRandom and a GrGLFunction<GrGLGetErrorFn> are too big to be captured by a
163 // GrGLFunction<GrGLGetError> (surprise, surprise). So we make a context object and
164 // capture that by pointer. However, GrGLFunction doesn't support calling a destructor
165 // on the thing it captures. So we leak the context.
166 struct GetErrorContext {
167 SkRandom fRandom;
168 GrGLFunction<GrGLGetErrorFn> fGetError;
169 };
170
171 auto errorContext = new GetErrorContext;
172
173#if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS)
174 __lsan_ignore_object(errorContext);
175#endif
176
177 errorContext->fGetError = original;
178
179 return GrGLFunction<GrGLGetErrorFn>([errorContext]() {
180 GrGLenum error = errorContext->fGetError();
181 if (error == GR_GL_NO_ERROR && (errorContext->fRandom.nextU() % 300) == 0) {
182 error = GR_GL_OUT_OF_MEMORY;
183 }
184 return error;
185 });
186}
187#endif
188
Robert Phillipsf4f80112020-07-13 16:13:31 -0400189sk_sp<GrDirectContext> GrDirectContext::MakeGL(sk_sp<const GrGLInterface> glInterface,
190 const GrContextOptions& options) {
191 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kOpenGL, options));
Brian Salomon24069eb2020-06-24 10:19:52 -0400192#if GR_TEST_UTILS
193 if (options.fRandomGLOOM) {
194 auto copy = sk_make_sp<GrGLInterface>(*glInterface);
195 copy->fFunctions.fGetError =
196 make_get_error_with_random_oom(glInterface->fFunctions.fGetError);
197#if GR_GL_CHECK_ERROR
198 // Suppress logging GL errors since we'll be synthetically generating them.
199 copy->suppressErrorLogging();
200#endif
201 glInterface = std::move(copy);
202 }
203#endif
Robert Phillipsf4f80112020-07-13 16:13:31 -0400204 direct->fGpu = GrGLGpu::Make(std::move(glInterface), options, direct.get());
205 if (!direct->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500206 return nullptr;
207 }
Robert Phillipsf4f80112020-07-13 16:13:31 -0400208 return direct;
Robert Phillipsa3457b82018-03-08 11:30:12 -0500209}
John Rosascoa9b348f2019-11-08 13:18:15 -0800210#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -0500211
Robert Phillipsf4f80112020-07-13 16:13:31 -0400212/*************************************************************************************************/
Robert Phillipsc7228c62020-07-14 12:57:39 -0400213#ifndef SK_DISABLE_LEGACY_CONTEXT_FACTORIES
214
Robert Phillipsa3457b82018-03-08 11:30:12 -0500215sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) {
Robert Phillipsf4f80112020-07-13 16:13:31 -0400216 return GrDirectContext::MakeMock(mockOptions);
Robert Phillipsa3457b82018-03-08 11:30:12 -0500217}
218
219sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions,
220 const GrContextOptions& options) {
Robert Phillipsf4f80112020-07-13 16:13:31 -0400221 return GrDirectContext::MakeMock(mockOptions, options);
222}
Robert Phillipsa3457b82018-03-08 11:30:12 -0500223
Robert Phillipsc7228c62020-07-14 12:57:39 -0400224#endif
225
Robert Phillipsf4f80112020-07-13 16:13:31 -0400226/*************************************************************************************************/
227sk_sp<GrDirectContext> GrDirectContext::MakeMock(const GrMockOptions* mockOptions) {
228 GrContextOptions defaultOptions;
229 return MakeMock(mockOptions, defaultOptions);
230}
231
232sk_sp<GrDirectContext> GrDirectContext::MakeMock(const GrMockOptions* mockOptions,
233 const GrContextOptions& options) {
234 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kMock, options));
235
236 direct->fGpu = GrMockGpu::Make(mockOptions, options, direct.get());
237 if (!direct->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500238 return nullptr;
239 }
Chris Daltona378b452019-12-11 13:24:11 -0500240
Robert Phillipsf4f80112020-07-13 16:13:31 -0400241 return direct;
Robert Phillipsa3457b82018-03-08 11:30:12 -0500242}
243
Greg Danielb4d89562018-10-03 18:44:49 +0000244#ifdef SK_VULKAN
Robert Phillipsf4f80112020-07-13 16:13:31 -0400245/*************************************************************************************************/
Robert Phillipsc7228c62020-07-14 12:57:39 -0400246#ifndef SK_DISABLE_LEGACY_CONTEXT_FACTORIES
247
Robert Phillipsf4f80112020-07-13 16:13:31 -0400248sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext) {
249 return GrDirectContext::MakeVulkan(backendContext);
Greg Daniel10a83da2018-06-14 09:31:11 -0400250}
251
252sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext,
253 const GrContextOptions& options) {
Robert Phillipsf4f80112020-07-13 16:13:31 -0400254 return GrDirectContext::MakeVulkan(backendContext, options);
255}
Greg Daniel10a83da2018-06-14 09:31:11 -0400256
Robert Phillipsc7228c62020-07-14 12:57:39 -0400257#endif
258
Robert Phillipsf4f80112020-07-13 16:13:31 -0400259/*************************************************************************************************/
260sk_sp<GrDirectContext> GrDirectContext::MakeVulkan(const GrVkBackendContext& backendContext) {
261 GrContextOptions defaultOptions;
262 return MakeVulkan(backendContext, defaultOptions);
263}
264
265sk_sp<GrDirectContext> GrDirectContext::MakeVulkan(const GrVkBackendContext& backendContext,
266 const GrContextOptions& options) {
267 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kVulkan, options));
268
269 direct->fGpu = GrVkGpu::Make(backendContext, options, direct.get());
270 if (!direct->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500271 return nullptr;
272 }
273
Robert Phillipsf4f80112020-07-13 16:13:31 -0400274 return direct;
Greg Danielb4d89562018-10-03 18:44:49 +0000275}
Robert Phillipsf4f80112020-07-13 16:13:31 -0400276#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -0500277
278#ifdef SK_METAL
Robert Phillipsf4f80112020-07-13 16:13:31 -0400279/*************************************************************************************************/
Robert Phillipsc7228c62020-07-14 12:57:39 -0400280#ifndef SK_DISABLE_LEGACY_CONTEXT_FACTORIES
281
Robert Phillipsa3457b82018-03-08 11:30:12 -0500282sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) {
Robert Phillipsf4f80112020-07-13 16:13:31 -0400283 return GrDirectContext::MakeMetal(device, queue);
284}
285
286sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) {
287 return GrDirectContext::MakeMetal(device, queue, options);
288}
289
Robert Phillipsc7228c62020-07-14 12:57:39 -0400290#endif
291
Robert Phillipsf4f80112020-07-13 16:13:31 -0400292/*************************************************************************************************/
293sk_sp<GrDirectContext> GrDirectContext::MakeMetal(void* device, void* queue) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500294 GrContextOptions defaultOptions;
295 return MakeMetal(device, queue, defaultOptions);
296}
297
Robert Phillipsf4f80112020-07-13 16:13:31 -0400298sk_sp<GrDirectContext> GrDirectContext::MakeMetal(void* device, void* queue,
299 const GrContextOptions& options) {
300 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kMetal, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500301
Robert Phillipsf4f80112020-07-13 16:13:31 -0400302 direct->fGpu = GrMtlTrampoline::MakeGpu(direct.get(), options, device, queue);
303 if (!direct->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500304 return nullptr;
305 }
Timothy Liang4e85e802018-06-28 16:37:18 -0400306
Robert Phillipsf4f80112020-07-13 16:13:31 -0400307 return direct;
Robert Phillipsa3457b82018-03-08 11:30:12 -0500308}
309#endif
310
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500311#ifdef SK_DIRECT3D
Robert Phillipsf4f80112020-07-13 16:13:31 -0400312/*************************************************************************************************/
Robert Phillipsc7228c62020-07-14 12:57:39 -0400313#ifndef SK_DISABLE_LEGACY_CONTEXT_FACTORIES
314
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500315sk_sp<GrContext> GrContext::MakeDirect3D(const GrD3DBackendContext& backendContext) {
Robert Phillipsf4f80112020-07-13 16:13:31 -0400316 return GrDirectContext::MakeDirect3D(backendContext);
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500317}
318
319sk_sp<GrContext> GrContext::MakeDirect3D(const GrD3DBackendContext& backendContext,
320 const GrContextOptions& options) {
Robert Phillipsf4f80112020-07-13 16:13:31 -0400321 return GrDirectContext::MakeDirect3D(backendContext, options);
322}
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500323
Robert Phillipsc7228c62020-07-14 12:57:39 -0400324#endif
325
Robert Phillipsf4f80112020-07-13 16:13:31 -0400326/*************************************************************************************************/
327sk_sp<GrDirectContext> GrDirectContext::MakeDirect3D(const GrD3DBackendContext& backendContext) {
328 GrContextOptions defaultOptions;
329 return MakeDirect3D(backendContext, defaultOptions);
330}
331
332sk_sp<GrDirectContext> GrDirectContext::MakeDirect3D(const GrD3DBackendContext& backendContext,
333 const GrContextOptions& options) {
334 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kDirect3D, options));
335
336 direct->fGpu = GrD3DGpu::Make(backendContext, options, direct.get());
337 if (!direct->init()) {
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500338 return nullptr;
339 }
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500340
Robert Phillipsf4f80112020-07-13 16:13:31 -0400341 return direct;
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500342}
343#endif
344
Stephen White985741a2019-07-18 11:43:45 -0400345#ifdef SK_DAWN
Robert Phillipsf4f80112020-07-13 16:13:31 -0400346/*************************************************************************************************/
Robert Phillipsc7228c62020-07-14 12:57:39 -0400347#ifndef SK_DISABLE_LEGACY_CONTEXT_FACTORIES
348
Stephen Whitea521c962019-12-04 09:57:48 -0500349sk_sp<GrContext> GrContext::MakeDawn(const wgpu::Device& device) {
Robert Phillipsf4f80112020-07-13 16:13:31 -0400350 return GrDirectContext::MakeDawn(device);
351}
352
353sk_sp<GrContext> GrContext::MakeDawn(const wgpu::Device& device, const GrContextOptions& options) {
354 return GrDirectContext::MakeDawn(device, options);
355}
356
Robert Phillipsc7228c62020-07-14 12:57:39 -0400357#endif
358
Robert Phillipsf4f80112020-07-13 16:13:31 -0400359/*************************************************************************************************/
360sk_sp<GrDirectContext> GrDirectContext::MakeDawn(const wgpu::Device& device) {
Stephen White985741a2019-07-18 11:43:45 -0400361 GrContextOptions defaultOptions;
362 return MakeDawn(device, defaultOptions);
363}
364
Robert Phillipsf4f80112020-07-13 16:13:31 -0400365sk_sp<GrDirectContext> GrDirectContext::MakeDawn(const wgpu::Device& device,
366 const GrContextOptions& options) {
367 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kDawn, options));
Stephen White985741a2019-07-18 11:43:45 -0400368
Robert Phillipsf4f80112020-07-13 16:13:31 -0400369 direct->fGpu = GrDawnGpu::Make(device, options, direct.get());
370 if (!direct->init()) {
Stephen White985741a2019-07-18 11:43:45 -0400371 return nullptr;
372 }
373
Robert Phillipsf4f80112020-07-13 16:13:31 -0400374 return direct;
Stephen White985741a2019-07-18 11:43:45 -0400375}
Robert Phillipsf4f80112020-07-13 16:13:31 -0400376
Stephen White985741a2019-07-18 11:43:45 -0400377#endif