blob: dd2c81848183e13c34da5c6f6428a7e9f4ea5987 [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"
Adlai Holler9555f292020-10-09 09:41:14 -040012#include "src/core/SkTaskGroup.h"
13#include "src/gpu/GrClientMappedBufferManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrContextPriv.h"
15#include "src/gpu/GrContextThreadSafeProxyPriv.h"
16#include "src/gpu/GrGpu.h"
Adlai Holler9555f292020-10-09 09:41:14 -040017#include "src/gpu/GrResourceProvider.h"
18#include "src/gpu/GrShaderUtils.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050019
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/effects/GrSkSLFP.h"
21#include "src/gpu/gl/GrGLGpu.h"
22#include "src/gpu/mock/GrMockGpu.h"
Robert Phillips5edf5102020-08-10 16:30:36 -040023#include "src/gpu/ops/GrSmallPathAtlasMgr.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040024#include "src/gpu/text/GrAtlasManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/text/GrStrikeCache.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050026#ifdef SK_METAL
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/mtl/GrMtlTrampoline.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050028#endif
29#ifdef SK_VULKAN
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/vk/GrVkGpu.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050031#endif
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -050032#ifdef SK_DIRECT3D
33#include "src/gpu/d3d/GrD3DGpu.h"
34#endif
Stephen White985741a2019-07-18 11:43:45 -040035#ifdef SK_DAWN
Mike Klein52337de2019-07-25 09:00:52 -050036#include "src/gpu/dawn/GrDawnGpu.h"
Stephen White985741a2019-07-18 11:43:45 -040037#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -050038
Brian Salomon24069eb2020-06-24 10:19:52 -040039#if GR_TEST_UTILS
40# include "include/utils/SkRandom.h"
41# if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS)
42# include <sanitizer/lsan_interface.h>
43# endif
44#endif
45
Robert Phillips6db27c22019-05-01 10:43:56 -040046#ifdef SK_DISABLE_REDUCE_OPLIST_SPLITTING
Greg Danielf41b2bd2019-08-22 16:19:24 -040047static const bool kDefaultReduceOpsTaskSplitting = false;
Robert Phillips6db27c22019-05-01 10:43:56 -040048#else
Greg Danielf41b2bd2019-08-22 16:19:24 -040049static const bool kDefaultReduceOpsTaskSplitting = false;
Robert Phillips6db27c22019-05-01 10:43:56 -040050#endif
51
Adlai Holler9555f292020-10-09 09:41:14 -040052#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner())
53
Robert Phillipsad248452020-06-30 09:27:52 -040054GrDirectContext::GrDirectContext(GrBackendApi backend, const GrContextOptions& options)
Robert Phillips3262bc82020-08-10 12:11:58 -040055 : INHERITED(GrContextThreadSafeProxyPriv::Make(backend, options)) {
Robert Phillipsad248452020-06-30 09:27:52 -040056}
Robert Phillipsa3457b82018-03-08 11:30:12 -050057
Robert Phillipsad248452020-06-30 09:27:52 -040058GrDirectContext::~GrDirectContext() {
Adlai Holler9555f292020-10-09 09:41:14 -040059 ASSERT_SINGLE_OWNER
Robert Phillipsad248452020-06-30 09:27:52 -040060 // this if-test protects against the case where the context is being destroyed
61 // before having been fully created
Adlai Holler9555f292020-10-09 09:41:14 -040062 if (fGpu) {
Greg Daniel0a2464f2020-05-14 15:45:44 -040063 this->flushAndSubmit();
Robert Phillipsa3457b82018-03-08 11:30:12 -050064 }
Adlai Holler9555f292020-10-09 09:41:14 -040065
66 this->destroyDrawingManager();
67 fMappedBufferManager.reset();
68
69 // Ideally we could just let the ptr drop, but resource cache queries this ptr in releaseAll.
70 if (fResourceCache) {
71 fResourceCache->releaseAll();
72 }
Robert Phillipsad248452020-06-30 09:27:52 -040073}
Robert Phillipsa3457b82018-03-08 11:30:12 -050074
Adlai Hollera7a40442020-10-09 09:49:42 -040075void GrDirectContext::resetGLTextureBindings() {
76 if (this->abandoned() || this->backend() != GrBackendApi::kOpenGL) {
77 return;
78 }
79 fGpu->resetTextureBindings();
80}
81
82void GrDirectContext::resetContext(uint32_t state) {
83 ASSERT_SINGLE_OWNER
84 fGpu->markContextDirty(state);
85}
86
Robert Phillipsad248452020-06-30 09:27:52 -040087void GrDirectContext::abandonContext() {
Adlai Hollera7a40442020-10-09 09:49:42 -040088 if (INHERITED::abandoned()) {
89 return;
90 }
91
Robert Phillipsad248452020-06-30 09:27:52 -040092 INHERITED::abandonContext();
Adlai Hollera7a40442020-10-09 09:49:42 -040093
94 fStrikeCache->freeAll();
95
96 fMappedBufferManager->abandon();
97
98 fResourceProvider->abandon();
99
100 // abandon first to so destructors
101 // don't try to free the resources in the API.
102 fResourceCache->abandonAll();
103
104 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
105
106 fMappedBufferManager.reset();
Robert Phillips079455c2020-08-11 15:18:46 -0400107 if (fSmallPathAtlasMgr) {
108 fSmallPathAtlasMgr->reset();
109 }
Robert Phillipsad248452020-06-30 09:27:52 -0400110 fAtlasManager->freeAll();
111}
Robert Phillipsa3457b82018-03-08 11:30:12 -0500112
Adlai Hollera7a40442020-10-09 09:49:42 -0400113bool GrDirectContext::abandoned() {
114 if (INHERITED::abandoned()) {
115 return true;
116 }
117
118 if (fGpu && fGpu->isDeviceLost()) {
119 this->abandonContext();
120 return true;
121 }
122 return false;
123}
124
Robert Phillipsad248452020-06-30 09:27:52 -0400125void GrDirectContext::releaseResourcesAndAbandonContext() {
126 INHERITED::releaseResourcesAndAbandonContext();
Robert Phillips079455c2020-08-11 15:18:46 -0400127 if (fSmallPathAtlasMgr) {
128 fSmallPathAtlasMgr->reset();
129 }
Robert Phillipsad248452020-06-30 09:27:52 -0400130 fAtlasManager->freeAll();
131}
Robert Phillips6db27c22019-05-01 10:43:56 -0400132
Robert Phillipsad248452020-06-30 09:27:52 -0400133void GrDirectContext::freeGpuResources() {
134 this->flushAndSubmit();
Robert Phillips079455c2020-08-11 15:18:46 -0400135 if (fSmallPathAtlasMgr) {
136 fSmallPathAtlasMgr->reset();
137 }
Robert Phillipsad248452020-06-30 09:27:52 -0400138 fAtlasManager->freeAll();
Robert Phillips56181ba2019-03-08 12:00:45 -0500139
Robert Phillipsad248452020-06-30 09:27:52 -0400140 INHERITED::freeGpuResources();
141}
Robert Phillipsa3457b82018-03-08 11:30:12 -0500142
Robert Phillipsad248452020-06-30 09:27:52 -0400143bool GrDirectContext::init() {
Adlai Holler9555f292020-10-09 09:41:14 -0400144 ASSERT_SINGLE_OWNER
145 if (!fGpu) {
Robert Phillipsad248452020-06-30 09:27:52 -0400146 return false;
Robert Phillipsa3457b82018-03-08 11:30:12 -0500147 }
148
Adlai Holler9555f292020-10-09 09:41:14 -0400149 fThreadSafeProxy->priv().init(fGpu->refCaps());
Robert Phillipsad248452020-06-30 09:27:52 -0400150 if (!INHERITED::init()) {
151 return false;
152 }
Robert Phillipsa3457b82018-03-08 11:30:12 -0500153
Adlai Holler9555f292020-10-09 09:41:14 -0400154 SkASSERT(this->getTextBlobCache());
155 SkASSERT(this->threadSafeCache());
156
157 fStrikeCache = std::make_unique<GrStrikeCache>();
158 fResourceCache = std::make_unique<GrResourceCache>(this->caps(), this->singleOwner(),
159 this->contextID());
160 fResourceCache->setProxyProvider(this->proxyProvider());
161 fResourceCache->setThreadSafeCache(this->threadSafeCache());
162 fResourceProvider = std::make_unique<GrResourceProvider>(fGpu.get(), fResourceCache.get(),
163 this->singleOwner());
164 fMappedBufferManager = std::make_unique<GrClientMappedBufferManager>(this->contextID());
165
166 fDidTestPMConversions = false;
167
168 // DDL TODO: we need to think through how the task group & persistent cache
169 // get passed on to/shared between all the DDLRecorders created with this context.
170 if (this->options().fExecutor) {
171 fTaskGroup = std::make_unique<SkTaskGroup>(*this->options().fExecutor);
172 }
173
174 fPersistentCache = this->options().fPersistentCache;
175 fShaderErrorHandler = this->options().fShaderErrorHandler;
176 if (!fShaderErrorHandler) {
177 fShaderErrorHandler = GrShaderUtils::DefaultShaderErrorHandler();
178 }
179
Robert Phillipsad248452020-06-30 09:27:52 -0400180 bool reduceOpsTaskSplitting = kDefaultReduceOpsTaskSplitting;
181 if (GrContextOptions::Enable::kNo == this->options().fReduceOpsTaskSplitting) {
182 reduceOpsTaskSplitting = false;
183 } else if (GrContextOptions::Enable::kYes == this->options().fReduceOpsTaskSplitting) {
184 reduceOpsTaskSplitting = true;
185 }
Robert Phillipsa3457b82018-03-08 11:30:12 -0500186
Robert Phillipsad248452020-06-30 09:27:52 -0400187 this->setupDrawingManager(true, reduceOpsTaskSplitting);
188
189 GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
190 if (GrContextOptions::Enable::kNo == this->options().fAllowMultipleGlyphCacheTextures ||
191 // multitexturing supported only if range can represent the index + texcoords fully
192 !(this->caps()->shaderCaps()->floatIs32Bits() ||
193 this->caps()->shaderCaps()->integerSupport())) {
194 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
195 } else {
196 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
197 }
198
199 GrProxyProvider* proxyProvider = this->priv().proxyProvider();
200
Robert Phillips3262bc82020-08-10 12:11:58 -0400201 fAtlasManager = std::make_unique<GrAtlasManager>(proxyProvider,
202 this->options().fGlyphCacheTextureMaximumBytes,
203 allowMultitexturing);
204 this->priv().addOnFlushCallbackObject(fAtlasManager.get());
Robert Phillipsad248452020-06-30 09:27:52 -0400205
206 return true;
207}
Robert Phillipsa3457b82018-03-08 11:30:12 -0500208
Robert Phillips5edf5102020-08-10 16:30:36 -0400209GrSmallPathAtlasMgr* GrDirectContext::onGetSmallPathAtlasMgr() {
Robert Phillips079455c2020-08-11 15:18:46 -0400210 if (!fSmallPathAtlasMgr) {
211 fSmallPathAtlasMgr = std::make_unique<GrSmallPathAtlasMgr>();
212
213 this->priv().addOnFlushCallbackObject(fSmallPathAtlasMgr.get());
214 }
215
216 if (!fSmallPathAtlasMgr->initAtlas(this->proxyProvider(), this->caps())) {
217 return nullptr;
218 }
219
220 return fSmallPathAtlasMgr.get();
Robert Phillips5edf5102020-08-10 16:30:36 -0400221}
222
John Rosascoa9b348f2019-11-08 13:18:15 -0800223#ifdef SK_GL
Robert Phillipsc7228c62020-07-14 12:57:39 -0400224
Robert Phillipsf4f80112020-07-13 16:13:31 -0400225/*************************************************************************************************/
226sk_sp<GrDirectContext> GrDirectContext::MakeGL(sk_sp<const GrGLInterface> glInterface) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500227 GrContextOptions defaultOptions;
Jim Van Verth03b8ab22020-02-24 11:36:15 -0500228 return MakeGL(std::move(glInterface), defaultOptions);
Robert Phillipsa3457b82018-03-08 11:30:12 -0500229}
230
Robert Phillipsf4f80112020-07-13 16:13:31 -0400231sk_sp<GrDirectContext> GrDirectContext::MakeGL(const GrContextOptions& options) {
Brian Salomonc1b9c102018-04-06 09:18:00 -0400232 return MakeGL(nullptr, options);
233}
234
Robert Phillipsf4f80112020-07-13 16:13:31 -0400235sk_sp<GrDirectContext> GrDirectContext::MakeGL() {
Brian Salomonc1b9c102018-04-06 09:18:00 -0400236 GrContextOptions defaultOptions;
237 return MakeGL(nullptr, defaultOptions);
238}
239
Brian Salomon24069eb2020-06-24 10:19:52 -0400240#if GR_TEST_UTILS
241GrGLFunction<GrGLGetErrorFn> make_get_error_with_random_oom(GrGLFunction<GrGLGetErrorFn> original) {
242 // A SkRandom and a GrGLFunction<GrGLGetErrorFn> are too big to be captured by a
243 // GrGLFunction<GrGLGetError> (surprise, surprise). So we make a context object and
244 // capture that by pointer. However, GrGLFunction doesn't support calling a destructor
245 // on the thing it captures. So we leak the context.
246 struct GetErrorContext {
247 SkRandom fRandom;
248 GrGLFunction<GrGLGetErrorFn> fGetError;
249 };
250
251 auto errorContext = new GetErrorContext;
252
253#if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS)
254 __lsan_ignore_object(errorContext);
255#endif
256
257 errorContext->fGetError = original;
258
259 return GrGLFunction<GrGLGetErrorFn>([errorContext]() {
260 GrGLenum error = errorContext->fGetError();
261 if (error == GR_GL_NO_ERROR && (errorContext->fRandom.nextU() % 300) == 0) {
262 error = GR_GL_OUT_OF_MEMORY;
263 }
264 return error;
265 });
266}
267#endif
268
Robert Phillipsf4f80112020-07-13 16:13:31 -0400269sk_sp<GrDirectContext> GrDirectContext::MakeGL(sk_sp<const GrGLInterface> glInterface,
270 const GrContextOptions& options) {
271 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kOpenGL, options));
Brian Salomon24069eb2020-06-24 10:19:52 -0400272#if GR_TEST_UTILS
273 if (options.fRandomGLOOM) {
274 auto copy = sk_make_sp<GrGLInterface>(*glInterface);
275 copy->fFunctions.fGetError =
276 make_get_error_with_random_oom(glInterface->fFunctions.fGetError);
277#if GR_GL_CHECK_ERROR
278 // Suppress logging GL errors since we'll be synthetically generating them.
279 copy->suppressErrorLogging();
280#endif
281 glInterface = std::move(copy);
282 }
283#endif
Robert Phillipsf4f80112020-07-13 16:13:31 -0400284 direct->fGpu = GrGLGpu::Make(std::move(glInterface), options, direct.get());
285 if (!direct->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500286 return nullptr;
287 }
Robert Phillipsf4f80112020-07-13 16:13:31 -0400288 return direct;
Robert Phillipsa3457b82018-03-08 11:30:12 -0500289}
John Rosascoa9b348f2019-11-08 13:18:15 -0800290#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -0500291
Robert Phillipsf4f80112020-07-13 16:13:31 -0400292/*************************************************************************************************/
Robert Phillipsf4f80112020-07-13 16:13:31 -0400293sk_sp<GrDirectContext> GrDirectContext::MakeMock(const GrMockOptions* mockOptions) {
294 GrContextOptions defaultOptions;
295 return MakeMock(mockOptions, defaultOptions);
296}
297
298sk_sp<GrDirectContext> GrDirectContext::MakeMock(const GrMockOptions* mockOptions,
299 const GrContextOptions& options) {
300 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kMock, options));
301
302 direct->fGpu = GrMockGpu::Make(mockOptions, options, direct.get());
303 if (!direct->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500304 return nullptr;
305 }
Chris Daltona378b452019-12-11 13:24:11 -0500306
Robert Phillipsf4f80112020-07-13 16:13:31 -0400307 return direct;
Robert Phillipsa3457b82018-03-08 11:30:12 -0500308}
309
Greg Danielb4d89562018-10-03 18:44:49 +0000310#ifdef SK_VULKAN
Robert Phillipsf4f80112020-07-13 16:13:31 -0400311/*************************************************************************************************/
Robert Phillipsf4f80112020-07-13 16:13:31 -0400312sk_sp<GrDirectContext> GrDirectContext::MakeVulkan(const GrVkBackendContext& backendContext) {
313 GrContextOptions defaultOptions;
314 return MakeVulkan(backendContext, defaultOptions);
315}
316
317sk_sp<GrDirectContext> GrDirectContext::MakeVulkan(const GrVkBackendContext& backendContext,
318 const GrContextOptions& options) {
319 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kVulkan, options));
320
321 direct->fGpu = GrVkGpu::Make(backendContext, options, direct.get());
322 if (!direct->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500323 return nullptr;
324 }
325
Robert Phillipsf4f80112020-07-13 16:13:31 -0400326 return direct;
Greg Danielb4d89562018-10-03 18:44:49 +0000327}
Robert Phillipsf4f80112020-07-13 16:13:31 -0400328#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -0500329
330#ifdef SK_METAL
Robert Phillipsf4f80112020-07-13 16:13:31 -0400331/*************************************************************************************************/
Robert Phillipsf4f80112020-07-13 16:13:31 -0400332sk_sp<GrDirectContext> GrDirectContext::MakeMetal(void* device, void* queue) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500333 GrContextOptions defaultOptions;
334 return MakeMetal(device, queue, defaultOptions);
335}
336
Robert Phillipsf4f80112020-07-13 16:13:31 -0400337sk_sp<GrDirectContext> GrDirectContext::MakeMetal(void* device, void* queue,
338 const GrContextOptions& options) {
339 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kMetal, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500340
Robert Phillipsf4f80112020-07-13 16:13:31 -0400341 direct->fGpu = GrMtlTrampoline::MakeGpu(direct.get(), options, device, queue);
342 if (!direct->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500343 return nullptr;
344 }
Timothy Liang4e85e802018-06-28 16:37:18 -0400345
Robert Phillipsf4f80112020-07-13 16:13:31 -0400346 return direct;
Robert Phillipsa3457b82018-03-08 11:30:12 -0500347}
348#endif
349
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500350#ifdef SK_DIRECT3D
Robert Phillipsf4f80112020-07-13 16:13:31 -0400351/*************************************************************************************************/
Robert Phillipsf4f80112020-07-13 16:13:31 -0400352sk_sp<GrDirectContext> GrDirectContext::MakeDirect3D(const GrD3DBackendContext& backendContext) {
353 GrContextOptions defaultOptions;
354 return MakeDirect3D(backendContext, defaultOptions);
355}
356
357sk_sp<GrDirectContext> GrDirectContext::MakeDirect3D(const GrD3DBackendContext& backendContext,
358 const GrContextOptions& options) {
359 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kDirect3D, options));
360
361 direct->fGpu = GrD3DGpu::Make(backendContext, options, direct.get());
362 if (!direct->init()) {
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500363 return nullptr;
364 }
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500365
Robert Phillipsf4f80112020-07-13 16:13:31 -0400366 return direct;
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500367}
368#endif
369
Stephen White985741a2019-07-18 11:43:45 -0400370#ifdef SK_DAWN
Robert Phillipsf4f80112020-07-13 16:13:31 -0400371/*************************************************************************************************/
Robert Phillipsf4f80112020-07-13 16:13:31 -0400372sk_sp<GrDirectContext> GrDirectContext::MakeDawn(const wgpu::Device& device) {
Stephen White985741a2019-07-18 11:43:45 -0400373 GrContextOptions defaultOptions;
374 return MakeDawn(device, defaultOptions);
375}
376
Robert Phillipsf4f80112020-07-13 16:13:31 -0400377sk_sp<GrDirectContext> GrDirectContext::MakeDawn(const wgpu::Device& device,
378 const GrContextOptions& options) {
379 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kDawn, options));
Stephen White985741a2019-07-18 11:43:45 -0400380
Robert Phillipsf4f80112020-07-13 16:13:31 -0400381 direct->fGpu = GrDawnGpu::Make(device, options, direct.get());
382 if (!direct->init()) {
Stephen White985741a2019-07-18 11:43:45 -0400383 return nullptr;
384 }
385
Robert Phillipsf4f80112020-07-13 16:13:31 -0400386 return direct;
Stephen White985741a2019-07-18 11:43:45 -0400387}
Robert Phillipsf4f80112020-07-13 16:13:31 -0400388
Stephen White985741a2019-07-18 11:43:45 -0400389#endif