blob: 9436c44696c833298e7ca0f16eb80f1b85567f57 [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 Holler61a591c2020-10-12 12:38:33 -040075sk_sp<GrContextThreadSafeProxy> GrDirectContext::threadSafeProxy() {
76 return INHERITED::threadSafeProxy();
77}
78
Adlai Hollera7a40442020-10-09 09:49:42 -040079void GrDirectContext::resetGLTextureBindings() {
80 if (this->abandoned() || this->backend() != GrBackendApi::kOpenGL) {
81 return;
82 }
83 fGpu->resetTextureBindings();
84}
85
86void GrDirectContext::resetContext(uint32_t state) {
87 ASSERT_SINGLE_OWNER
88 fGpu->markContextDirty(state);
89}
90
Robert Phillipsad248452020-06-30 09:27:52 -040091void GrDirectContext::abandonContext() {
Adlai Hollera7a40442020-10-09 09:49:42 -040092 if (INHERITED::abandoned()) {
93 return;
94 }
95
Robert Phillipsad248452020-06-30 09:27:52 -040096 INHERITED::abandonContext();
Adlai Hollera7a40442020-10-09 09:49:42 -040097
98 fStrikeCache->freeAll();
99
100 fMappedBufferManager->abandon();
101
102 fResourceProvider->abandon();
103
104 // abandon first to so destructors
105 // don't try to free the resources in the API.
106 fResourceCache->abandonAll();
107
108 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
109
110 fMappedBufferManager.reset();
Robert Phillips079455c2020-08-11 15:18:46 -0400111 if (fSmallPathAtlasMgr) {
112 fSmallPathAtlasMgr->reset();
113 }
Robert Phillipsad248452020-06-30 09:27:52 -0400114 fAtlasManager->freeAll();
115}
Robert Phillipsa3457b82018-03-08 11:30:12 -0500116
Adlai Hollera7a40442020-10-09 09:49:42 -0400117bool GrDirectContext::abandoned() {
118 if (INHERITED::abandoned()) {
119 return true;
120 }
121
122 if (fGpu && fGpu->isDeviceLost()) {
123 this->abandonContext();
124 return true;
125 }
126 return false;
127}
128
Adlai Holler61a591c2020-10-12 12:38:33 -0400129bool GrDirectContext::oomed() { return fGpu ? fGpu->checkAndResetOOMed() : false; }
130
Robert Phillipsad248452020-06-30 09:27:52 -0400131void GrDirectContext::releaseResourcesAndAbandonContext() {
Adlai Holler61a591c2020-10-12 12:38:33 -0400132 if (INHERITED::abandoned()) {
133 return;
134 }
135
136 INHERITED::abandonContext();
137
138 fMappedBufferManager.reset();
139
140 fResourceProvider->abandon();
141
142 // Release all resources in the backend 3D API.
143 fResourceCache->releaseAll();
144
145 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
Robert Phillips079455c2020-08-11 15:18:46 -0400146 if (fSmallPathAtlasMgr) {
147 fSmallPathAtlasMgr->reset();
148 }
Robert Phillipsad248452020-06-30 09:27:52 -0400149 fAtlasManager->freeAll();
150}
Robert Phillips6db27c22019-05-01 10:43:56 -0400151
Robert Phillipsad248452020-06-30 09:27:52 -0400152void GrDirectContext::freeGpuResources() {
153 this->flushAndSubmit();
Robert Phillips079455c2020-08-11 15:18:46 -0400154 if (fSmallPathAtlasMgr) {
155 fSmallPathAtlasMgr->reset();
156 }
Robert Phillipsad248452020-06-30 09:27:52 -0400157 fAtlasManager->freeAll();
Robert Phillips56181ba2019-03-08 12:00:45 -0500158
Robert Phillipsad248452020-06-30 09:27:52 -0400159 INHERITED::freeGpuResources();
160}
Robert Phillipsa3457b82018-03-08 11:30:12 -0500161
Robert Phillipsad248452020-06-30 09:27:52 -0400162bool GrDirectContext::init() {
Adlai Holler9555f292020-10-09 09:41:14 -0400163 ASSERT_SINGLE_OWNER
164 if (!fGpu) {
Robert Phillipsad248452020-06-30 09:27:52 -0400165 return false;
Robert Phillipsa3457b82018-03-08 11:30:12 -0500166 }
167
Adlai Holler9555f292020-10-09 09:41:14 -0400168 fThreadSafeProxy->priv().init(fGpu->refCaps());
Robert Phillipsad248452020-06-30 09:27:52 -0400169 if (!INHERITED::init()) {
170 return false;
171 }
Robert Phillipsa3457b82018-03-08 11:30:12 -0500172
Adlai Holler9555f292020-10-09 09:41:14 -0400173 SkASSERT(this->getTextBlobCache());
174 SkASSERT(this->threadSafeCache());
175
176 fStrikeCache = std::make_unique<GrStrikeCache>();
177 fResourceCache = std::make_unique<GrResourceCache>(this->caps(), this->singleOwner(),
178 this->contextID());
179 fResourceCache->setProxyProvider(this->proxyProvider());
180 fResourceCache->setThreadSafeCache(this->threadSafeCache());
181 fResourceProvider = std::make_unique<GrResourceProvider>(fGpu.get(), fResourceCache.get(),
182 this->singleOwner());
183 fMappedBufferManager = std::make_unique<GrClientMappedBufferManager>(this->contextID());
184
185 fDidTestPMConversions = false;
186
187 // DDL TODO: we need to think through how the task group & persistent cache
188 // get passed on to/shared between all the DDLRecorders created with this context.
189 if (this->options().fExecutor) {
190 fTaskGroup = std::make_unique<SkTaskGroup>(*this->options().fExecutor);
191 }
192
193 fPersistentCache = this->options().fPersistentCache;
194 fShaderErrorHandler = this->options().fShaderErrorHandler;
195 if (!fShaderErrorHandler) {
196 fShaderErrorHandler = GrShaderUtils::DefaultShaderErrorHandler();
197 }
198
Robert Phillipsad248452020-06-30 09:27:52 -0400199 bool reduceOpsTaskSplitting = kDefaultReduceOpsTaskSplitting;
200 if (GrContextOptions::Enable::kNo == this->options().fReduceOpsTaskSplitting) {
201 reduceOpsTaskSplitting = false;
202 } else if (GrContextOptions::Enable::kYes == this->options().fReduceOpsTaskSplitting) {
203 reduceOpsTaskSplitting = true;
204 }
Robert Phillipsa3457b82018-03-08 11:30:12 -0500205
Robert Phillipsad248452020-06-30 09:27:52 -0400206 this->setupDrawingManager(true, reduceOpsTaskSplitting);
207
208 GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
209 if (GrContextOptions::Enable::kNo == this->options().fAllowMultipleGlyphCacheTextures ||
210 // multitexturing supported only if range can represent the index + texcoords fully
211 !(this->caps()->shaderCaps()->floatIs32Bits() ||
212 this->caps()->shaderCaps()->integerSupport())) {
213 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
214 } else {
215 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
216 }
217
218 GrProxyProvider* proxyProvider = this->priv().proxyProvider();
219
Robert Phillips3262bc82020-08-10 12:11:58 -0400220 fAtlasManager = std::make_unique<GrAtlasManager>(proxyProvider,
221 this->options().fGlyphCacheTextureMaximumBytes,
222 allowMultitexturing);
223 this->priv().addOnFlushCallbackObject(fAtlasManager.get());
Robert Phillipsad248452020-06-30 09:27:52 -0400224
225 return true;
226}
Robert Phillipsa3457b82018-03-08 11:30:12 -0500227
Robert Phillips5edf5102020-08-10 16:30:36 -0400228GrSmallPathAtlasMgr* GrDirectContext::onGetSmallPathAtlasMgr() {
Robert Phillips079455c2020-08-11 15:18:46 -0400229 if (!fSmallPathAtlasMgr) {
230 fSmallPathAtlasMgr = std::make_unique<GrSmallPathAtlasMgr>();
231
232 this->priv().addOnFlushCallbackObject(fSmallPathAtlasMgr.get());
233 }
234
235 if (!fSmallPathAtlasMgr->initAtlas(this->proxyProvider(), this->caps())) {
236 return nullptr;
237 }
238
239 return fSmallPathAtlasMgr.get();
Robert Phillips5edf5102020-08-10 16:30:36 -0400240}
241
John Rosascoa9b348f2019-11-08 13:18:15 -0800242#ifdef SK_GL
Robert Phillipsc7228c62020-07-14 12:57:39 -0400243
Robert Phillipsf4f80112020-07-13 16:13:31 -0400244/*************************************************************************************************/
245sk_sp<GrDirectContext> GrDirectContext::MakeGL(sk_sp<const GrGLInterface> glInterface) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500246 GrContextOptions defaultOptions;
Jim Van Verth03b8ab22020-02-24 11:36:15 -0500247 return MakeGL(std::move(glInterface), defaultOptions);
Robert Phillipsa3457b82018-03-08 11:30:12 -0500248}
249
Robert Phillipsf4f80112020-07-13 16:13:31 -0400250sk_sp<GrDirectContext> GrDirectContext::MakeGL(const GrContextOptions& options) {
Brian Salomonc1b9c102018-04-06 09:18:00 -0400251 return MakeGL(nullptr, options);
252}
253
Robert Phillipsf4f80112020-07-13 16:13:31 -0400254sk_sp<GrDirectContext> GrDirectContext::MakeGL() {
Brian Salomonc1b9c102018-04-06 09:18:00 -0400255 GrContextOptions defaultOptions;
256 return MakeGL(nullptr, defaultOptions);
257}
258
Brian Salomon24069eb2020-06-24 10:19:52 -0400259#if GR_TEST_UTILS
260GrGLFunction<GrGLGetErrorFn> make_get_error_with_random_oom(GrGLFunction<GrGLGetErrorFn> original) {
261 // A SkRandom and a GrGLFunction<GrGLGetErrorFn> are too big to be captured by a
262 // GrGLFunction<GrGLGetError> (surprise, surprise). So we make a context object and
263 // capture that by pointer. However, GrGLFunction doesn't support calling a destructor
264 // on the thing it captures. So we leak the context.
265 struct GetErrorContext {
266 SkRandom fRandom;
267 GrGLFunction<GrGLGetErrorFn> fGetError;
268 };
269
270 auto errorContext = new GetErrorContext;
271
272#if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS)
273 __lsan_ignore_object(errorContext);
274#endif
275
276 errorContext->fGetError = original;
277
278 return GrGLFunction<GrGLGetErrorFn>([errorContext]() {
279 GrGLenum error = errorContext->fGetError();
280 if (error == GR_GL_NO_ERROR && (errorContext->fRandom.nextU() % 300) == 0) {
281 error = GR_GL_OUT_OF_MEMORY;
282 }
283 return error;
284 });
285}
286#endif
287
Robert Phillipsf4f80112020-07-13 16:13:31 -0400288sk_sp<GrDirectContext> GrDirectContext::MakeGL(sk_sp<const GrGLInterface> glInterface,
289 const GrContextOptions& options) {
290 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kOpenGL, options));
Brian Salomon24069eb2020-06-24 10:19:52 -0400291#if GR_TEST_UTILS
292 if (options.fRandomGLOOM) {
293 auto copy = sk_make_sp<GrGLInterface>(*glInterface);
294 copy->fFunctions.fGetError =
295 make_get_error_with_random_oom(glInterface->fFunctions.fGetError);
296#if GR_GL_CHECK_ERROR
297 // Suppress logging GL errors since we'll be synthetically generating them.
298 copy->suppressErrorLogging();
299#endif
300 glInterface = std::move(copy);
301 }
302#endif
Robert Phillipsf4f80112020-07-13 16:13:31 -0400303 direct->fGpu = GrGLGpu::Make(std::move(glInterface), options, direct.get());
304 if (!direct->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500305 return nullptr;
306 }
Robert Phillipsf4f80112020-07-13 16:13:31 -0400307 return direct;
Robert Phillipsa3457b82018-03-08 11:30:12 -0500308}
John Rosascoa9b348f2019-11-08 13:18:15 -0800309#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -0500310
Robert Phillipsf4f80112020-07-13 16:13:31 -0400311/*************************************************************************************************/
Robert Phillipsf4f80112020-07-13 16:13:31 -0400312sk_sp<GrDirectContext> GrDirectContext::MakeMock(const GrMockOptions* mockOptions) {
313 GrContextOptions defaultOptions;
314 return MakeMock(mockOptions, defaultOptions);
315}
316
317sk_sp<GrDirectContext> GrDirectContext::MakeMock(const GrMockOptions* mockOptions,
318 const GrContextOptions& options) {
319 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kMock, options));
320
321 direct->fGpu = GrMockGpu::Make(mockOptions, options, direct.get());
322 if (!direct->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500323 return nullptr;
324 }
Chris Daltona378b452019-12-11 13:24:11 -0500325
Robert Phillipsf4f80112020-07-13 16:13:31 -0400326 return direct;
Robert Phillipsa3457b82018-03-08 11:30:12 -0500327}
328
Greg Danielb4d89562018-10-03 18:44:49 +0000329#ifdef SK_VULKAN
Robert Phillipsf4f80112020-07-13 16:13:31 -0400330/*************************************************************************************************/
Robert Phillipsf4f80112020-07-13 16:13:31 -0400331sk_sp<GrDirectContext> GrDirectContext::MakeVulkan(const GrVkBackendContext& backendContext) {
332 GrContextOptions defaultOptions;
333 return MakeVulkan(backendContext, defaultOptions);
334}
335
336sk_sp<GrDirectContext> GrDirectContext::MakeVulkan(const GrVkBackendContext& backendContext,
337 const GrContextOptions& options) {
338 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kVulkan, options));
339
340 direct->fGpu = GrVkGpu::Make(backendContext, options, direct.get());
341 if (!direct->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500342 return nullptr;
343 }
344
Robert Phillipsf4f80112020-07-13 16:13:31 -0400345 return direct;
Greg Danielb4d89562018-10-03 18:44:49 +0000346}
Robert Phillipsf4f80112020-07-13 16:13:31 -0400347#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -0500348
349#ifdef SK_METAL
Robert Phillipsf4f80112020-07-13 16:13:31 -0400350/*************************************************************************************************/
Robert Phillipsf4f80112020-07-13 16:13:31 -0400351sk_sp<GrDirectContext> GrDirectContext::MakeMetal(void* device, void* queue) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500352 GrContextOptions defaultOptions;
353 return MakeMetal(device, queue, defaultOptions);
354}
355
Robert Phillipsf4f80112020-07-13 16:13:31 -0400356sk_sp<GrDirectContext> GrDirectContext::MakeMetal(void* device, void* queue,
357 const GrContextOptions& options) {
358 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kMetal, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500359
Robert Phillipsf4f80112020-07-13 16:13:31 -0400360 direct->fGpu = GrMtlTrampoline::MakeGpu(direct.get(), options, device, queue);
361 if (!direct->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500362 return nullptr;
363 }
Timothy Liang4e85e802018-06-28 16:37:18 -0400364
Robert Phillipsf4f80112020-07-13 16:13:31 -0400365 return direct;
Robert Phillipsa3457b82018-03-08 11:30:12 -0500366}
367#endif
368
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500369#ifdef SK_DIRECT3D
Robert Phillipsf4f80112020-07-13 16:13:31 -0400370/*************************************************************************************************/
Robert Phillipsf4f80112020-07-13 16:13:31 -0400371sk_sp<GrDirectContext> GrDirectContext::MakeDirect3D(const GrD3DBackendContext& backendContext) {
372 GrContextOptions defaultOptions;
373 return MakeDirect3D(backendContext, defaultOptions);
374}
375
376sk_sp<GrDirectContext> GrDirectContext::MakeDirect3D(const GrD3DBackendContext& backendContext,
377 const GrContextOptions& options) {
378 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kDirect3D, options));
379
380 direct->fGpu = GrD3DGpu::Make(backendContext, options, direct.get());
381 if (!direct->init()) {
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500382 return nullptr;
383 }
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500384
Robert Phillipsf4f80112020-07-13 16:13:31 -0400385 return direct;
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500386}
387#endif
388
Stephen White985741a2019-07-18 11:43:45 -0400389#ifdef SK_DAWN
Robert Phillipsf4f80112020-07-13 16:13:31 -0400390/*************************************************************************************************/
Robert Phillipsf4f80112020-07-13 16:13:31 -0400391sk_sp<GrDirectContext> GrDirectContext::MakeDawn(const wgpu::Device& device) {
Stephen White985741a2019-07-18 11:43:45 -0400392 GrContextOptions defaultOptions;
393 return MakeDawn(device, defaultOptions);
394}
395
Robert Phillipsf4f80112020-07-13 16:13:31 -0400396sk_sp<GrDirectContext> GrDirectContext::MakeDawn(const wgpu::Device& device,
397 const GrContextOptions& options) {
398 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kDawn, options));
Stephen White985741a2019-07-18 11:43:45 -0400399
Robert Phillipsf4f80112020-07-13 16:13:31 -0400400 direct->fGpu = GrDawnGpu::Make(device, options, direct.get());
401 if (!direct->init()) {
Stephen White985741a2019-07-18 11:43:45 -0400402 return nullptr;
403 }
404
Robert Phillipsf4f80112020-07-13 16:13:31 -0400405 return direct;
Stephen White985741a2019-07-18 11:43:45 -0400406}
Robert Phillipsf4f80112020-07-13 16:13:31 -0400407
Stephen White985741a2019-07-18 11:43:45 -0400408#endif