blob: 472c3c606d512677ce4fd4e0c45056e6f91416c1 [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 Phillipsad248452020-06-30 09:27:52 -04009#include "include/private/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
Jim Van Verth03b8ab22020-02-24 11:36:15 -0500120sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> glInterface) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500121 GrContextOptions defaultOptions;
Jim Van Verth03b8ab22020-02-24 11:36:15 -0500122 return MakeGL(std::move(glInterface), defaultOptions);
Robert Phillipsa3457b82018-03-08 11:30:12 -0500123}
124
Brian Salomonc1b9c102018-04-06 09:18:00 -0400125sk_sp<GrContext> GrContext::MakeGL(const GrContextOptions& options) {
126 return MakeGL(nullptr, options);
127}
128
129sk_sp<GrContext> GrContext::MakeGL() {
130 GrContextOptions defaultOptions;
131 return MakeGL(nullptr, defaultOptions);
132}
133
Brian Salomon24069eb2020-06-24 10:19:52 -0400134#if GR_TEST_UTILS
135GrGLFunction<GrGLGetErrorFn> make_get_error_with_random_oom(GrGLFunction<GrGLGetErrorFn> original) {
136 // A SkRandom and a GrGLFunction<GrGLGetErrorFn> are too big to be captured by a
137 // GrGLFunction<GrGLGetError> (surprise, surprise). So we make a context object and
138 // capture that by pointer. However, GrGLFunction doesn't support calling a destructor
139 // on the thing it captures. So we leak the context.
140 struct GetErrorContext {
141 SkRandom fRandom;
142 GrGLFunction<GrGLGetErrorFn> fGetError;
143 };
144
145 auto errorContext = new GetErrorContext;
146
147#if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS)
148 __lsan_ignore_object(errorContext);
149#endif
150
151 errorContext->fGetError = original;
152
153 return GrGLFunction<GrGLGetErrorFn>([errorContext]() {
154 GrGLenum error = errorContext->fGetError();
155 if (error == GR_GL_NO_ERROR && (errorContext->fRandom.nextU() % 300) == 0) {
156 error = GR_GL_OUT_OF_MEMORY;
157 }
158 return error;
159 });
160}
161#endif
162
Jim Van Verth03b8ab22020-02-24 11:36:15 -0500163sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> glInterface,
Robert Phillipsa3457b82018-03-08 11:30:12 -0500164 const GrContextOptions& options) {
Robert Phillipsad248452020-06-30 09:27:52 -0400165 sk_sp<GrContext> context(new GrDirectContext(GrBackendApi::kOpenGL, options));
Brian Salomon24069eb2020-06-24 10:19:52 -0400166#if GR_TEST_UTILS
167 if (options.fRandomGLOOM) {
168 auto copy = sk_make_sp<GrGLInterface>(*glInterface);
169 copy->fFunctions.fGetError =
170 make_get_error_with_random_oom(glInterface->fFunctions.fGetError);
171#if GR_GL_CHECK_ERROR
172 // Suppress logging GL errors since we'll be synthetically generating them.
173 copy->suppressErrorLogging();
174#endif
175 glInterface = std::move(copy);
176 }
177#endif
Jim Van Verth03b8ab22020-02-24 11:36:15 -0500178 context->fGpu = GrGLGpu::Make(std::move(glInterface), options, context.get());
Adlai Hollere219d1c2020-06-02 11:23:16 -0400179 if (!context->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500180 return nullptr;
181 }
182 return context;
183}
John Rosascoa9b348f2019-11-08 13:18:15 -0800184#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -0500185
186sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) {
187 GrContextOptions defaultOptions;
188 return MakeMock(mockOptions, defaultOptions);
189}
190
191sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions,
192 const GrContextOptions& options) {
Robert Phillipsad248452020-06-30 09:27:52 -0400193 sk_sp<GrContext> context(new GrDirectContext(GrBackendApi::kMock, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500194
195 context->fGpu = GrMockGpu::Make(mockOptions, options, context.get());
Adlai Hollere219d1c2020-06-02 11:23:16 -0400196 if (!context->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500197 return nullptr;
198 }
Chris Daltona378b452019-12-11 13:24:11 -0500199
Robert Phillipsa3457b82018-03-08 11:30:12 -0500200 return context;
201}
202
Mike Kleina55e2142018-10-03 16:34:11 +0000203sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext) {
Greg Danielb4d89562018-10-03 18:44:49 +0000204#ifdef SK_VULKAN
Greg Daniel10a83da2018-06-14 09:31:11 -0400205 GrContextOptions defaultOptions;
206 return MakeVulkan(backendContext, defaultOptions);
Greg Danielb4d89562018-10-03 18:44:49 +0000207#else
208 return nullptr;
209#endif
Greg Daniel10a83da2018-06-14 09:31:11 -0400210}
211
212sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext,
213 const GrContextOptions& options) {
Greg Danielb4d89562018-10-03 18:44:49 +0000214#ifdef SK_VULKAN
Robert Phillipsad248452020-06-30 09:27:52 -0400215 sk_sp<GrContext> context(new GrDirectContext(GrBackendApi::kVulkan, options));
Greg Daniel10a83da2018-06-14 09:31:11 -0400216
Greg Danielf730c182018-07-02 20:15:37 +0000217 context->fGpu = GrVkGpu::Make(backendContext, options, context.get());
Adlai Hollere219d1c2020-06-02 11:23:16 -0400218 if (!context->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500219 return nullptr;
220 }
221
Robert Phillipsa3457b82018-03-08 11:30:12 -0500222 return context;
Greg Danielb4d89562018-10-03 18:44:49 +0000223#else
224 return nullptr;
Mike Kleina55e2142018-10-03 16:34:11 +0000225#endif
Greg Danielb4d89562018-10-03 18:44:49 +0000226}
Robert Phillipsa3457b82018-03-08 11:30:12 -0500227
228#ifdef SK_METAL
229sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) {
230 GrContextOptions defaultOptions;
231 return MakeMetal(device, queue, defaultOptions);
232}
233
234sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) {
Robert Phillipsad248452020-06-30 09:27:52 -0400235 sk_sp<GrContext> context(new GrDirectContext(GrBackendApi::kMetal, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500236
237 context->fGpu = GrMtlTrampoline::MakeGpu(context.get(), options, device, queue);
Adlai Hollere219d1c2020-06-02 11:23:16 -0400238 if (!context->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500239 return nullptr;
240 }
Timothy Liang4e85e802018-06-28 16:37:18 -0400241
Robert Phillipsa3457b82018-03-08 11:30:12 -0500242 return context;
243}
244#endif
245
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500246#ifdef SK_DIRECT3D
247sk_sp<GrContext> GrContext::MakeDirect3D(const GrD3DBackendContext& backendContext) {
248 GrContextOptions defaultOptions;
249 return MakeDirect3D(backendContext, defaultOptions);
250}
251
252sk_sp<GrContext> GrContext::MakeDirect3D(const GrD3DBackendContext& backendContext,
253 const GrContextOptions& options) {
Robert Phillipsad248452020-06-30 09:27:52 -0400254 sk_sp<GrContext> context(new GrDirectContext(GrBackendApi::kDirect3D, options));
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500255
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500256 context->fGpu = GrD3DGpu::Make(backendContext, options, context.get());
Adlai Hollere219d1c2020-06-02 11:23:16 -0400257 if (!context->init()) {
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500258 return nullptr;
259 }
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500260
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500261 return context;
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500262}
263#endif
264
Stephen White985741a2019-07-18 11:43:45 -0400265#ifdef SK_DAWN
Stephen Whitea521c962019-12-04 09:57:48 -0500266sk_sp<GrContext> GrContext::MakeDawn(const wgpu::Device& device) {
Stephen White985741a2019-07-18 11:43:45 -0400267 GrContextOptions defaultOptions;
268 return MakeDawn(device, defaultOptions);
269}
270
Stephen Whitea521c962019-12-04 09:57:48 -0500271sk_sp<GrContext> GrContext::MakeDawn(const wgpu::Device& device, const GrContextOptions& options) {
Robert Phillipsad248452020-06-30 09:27:52 -0400272 sk_sp<GrContext> context(new GrDirectContext(GrBackendApi::kDawn, options));
Stephen White985741a2019-07-18 11:43:45 -0400273
274 context->fGpu = GrDawnGpu::Make(device, options, context.get());
Adlai Hollere219d1c2020-06-02 11:23:16 -0400275 if (!context->init()) {
Stephen White985741a2019-07-18 11:43:45 -0400276 return nullptr;
277 }
278
Stephen White985741a2019-07-18 11:43:45 -0400279 return context;
280}
281#endif