blob: ac5722a0740b3424575cbd8a10aef8cd4b0fa57f [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
Adlai Holler3acc69a2020-10-13 08:20:51 -040011#include "include/core/SkTraceMemoryDump.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/gpu/GrContextThreadSafeProxy.h"
Adlai Holler9555f292020-10-09 09:41:14 -040013#include "src/core/SkTaskGroup.h"
14#include "src/gpu/GrClientMappedBufferManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrContextPriv.h"
16#include "src/gpu/GrContextThreadSafeProxyPriv.h"
Adlai Holler4aa4c602020-10-12 13:58:52 -040017#include "src/gpu/GrDrawingManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrGpu.h"
Adlai Holler9555f292020-10-09 09:41:14 -040019#include "src/gpu/GrResourceProvider.h"
20#include "src/gpu/GrShaderUtils.h"
Adlai Holler3acc69a2020-10-13 08:20:51 -040021#include "src/image/SkImage_GpuBase.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050022
Adlai Holler4aa4c602020-10-12 13:58:52 -040023#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/effects/GrSkSLFP.h"
25#include "src/gpu/gl/GrGLGpu.h"
26#include "src/gpu/mock/GrMockGpu.h"
Robert Phillips5edf5102020-08-10 16:30:36 -040027#include "src/gpu/ops/GrSmallPathAtlasMgr.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040028#include "src/gpu/text/GrAtlasManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/text/GrStrikeCache.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050030#ifdef SK_METAL
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/mtl/GrMtlTrampoline.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050032#endif
33#ifdef SK_VULKAN
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/gpu/vk/GrVkGpu.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050035#endif
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -050036#ifdef SK_DIRECT3D
37#include "src/gpu/d3d/GrD3DGpu.h"
38#endif
Stephen White985741a2019-07-18 11:43:45 -040039#ifdef SK_DAWN
Mike Klein52337de2019-07-25 09:00:52 -050040#include "src/gpu/dawn/GrDawnGpu.h"
Stephen White985741a2019-07-18 11:43:45 -040041#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -050042
Brian Salomon24069eb2020-06-24 10:19:52 -040043#if GR_TEST_UTILS
44# include "include/utils/SkRandom.h"
45# if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS)
46# include <sanitizer/lsan_interface.h>
47# endif
48#endif
49
Robert Phillips6db27c22019-05-01 10:43:56 -040050#ifdef SK_DISABLE_REDUCE_OPLIST_SPLITTING
Greg Danielf41b2bd2019-08-22 16:19:24 -040051static const bool kDefaultReduceOpsTaskSplitting = false;
Robert Phillips6db27c22019-05-01 10:43:56 -040052#else
Greg Danielf41b2bd2019-08-22 16:19:24 -040053static const bool kDefaultReduceOpsTaskSplitting = false;
Robert Phillips6db27c22019-05-01 10:43:56 -040054#endif
55
Adlai Holler9555f292020-10-09 09:41:14 -040056#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner())
57
Robert Phillipsad248452020-06-30 09:27:52 -040058GrDirectContext::GrDirectContext(GrBackendApi backend, const GrContextOptions& options)
Robert Phillips3262bc82020-08-10 12:11:58 -040059 : INHERITED(GrContextThreadSafeProxyPriv::Make(backend, options)) {
Robert Phillipsad248452020-06-30 09:27:52 -040060}
Robert Phillipsa3457b82018-03-08 11:30:12 -050061
Robert Phillipsad248452020-06-30 09:27:52 -040062GrDirectContext::~GrDirectContext() {
Adlai Holler9555f292020-10-09 09:41:14 -040063 ASSERT_SINGLE_OWNER
Robert Phillipsad248452020-06-30 09:27:52 -040064 // this if-test protects against the case where the context is being destroyed
65 // before having been fully created
Adlai Holler9555f292020-10-09 09:41:14 -040066 if (fGpu) {
Greg Daniel0a2464f2020-05-14 15:45:44 -040067 this->flushAndSubmit();
Robert Phillipsa3457b82018-03-08 11:30:12 -050068 }
Adlai Holler9555f292020-10-09 09:41:14 -040069
70 this->destroyDrawingManager();
71 fMappedBufferManager.reset();
72
73 // Ideally we could just let the ptr drop, but resource cache queries this ptr in releaseAll.
74 if (fResourceCache) {
75 fResourceCache->releaseAll();
76 }
Robert Phillipsad248452020-06-30 09:27:52 -040077}
Robert Phillipsa3457b82018-03-08 11:30:12 -050078
Adlai Holler61a591c2020-10-12 12:38:33 -040079sk_sp<GrContextThreadSafeProxy> GrDirectContext::threadSafeProxy() {
80 return INHERITED::threadSafeProxy();
81}
82
Adlai Hollera7a40442020-10-09 09:49:42 -040083void GrDirectContext::resetGLTextureBindings() {
84 if (this->abandoned() || this->backend() != GrBackendApi::kOpenGL) {
85 return;
86 }
87 fGpu->resetTextureBindings();
88}
89
90void GrDirectContext::resetContext(uint32_t state) {
91 ASSERT_SINGLE_OWNER
92 fGpu->markContextDirty(state);
93}
94
Robert Phillipsad248452020-06-30 09:27:52 -040095void GrDirectContext::abandonContext() {
Adlai Hollera7a40442020-10-09 09:49:42 -040096 if (INHERITED::abandoned()) {
97 return;
98 }
99
Robert Phillipsad248452020-06-30 09:27:52 -0400100 INHERITED::abandonContext();
Adlai Hollera7a40442020-10-09 09:49:42 -0400101
102 fStrikeCache->freeAll();
103
104 fMappedBufferManager->abandon();
105
106 fResourceProvider->abandon();
107
108 // abandon first to so destructors
109 // don't try to free the resources in the API.
110 fResourceCache->abandonAll();
111
112 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
113
114 fMappedBufferManager.reset();
Robert Phillips079455c2020-08-11 15:18:46 -0400115 if (fSmallPathAtlasMgr) {
116 fSmallPathAtlasMgr->reset();
117 }
Robert Phillipsad248452020-06-30 09:27:52 -0400118 fAtlasManager->freeAll();
119}
Robert Phillipsa3457b82018-03-08 11:30:12 -0500120
Adlai Hollera7a40442020-10-09 09:49:42 -0400121bool GrDirectContext::abandoned() {
122 if (INHERITED::abandoned()) {
123 return true;
124 }
125
126 if (fGpu && fGpu->isDeviceLost()) {
127 this->abandonContext();
128 return true;
129 }
130 return false;
131}
132
Adlai Holler61a591c2020-10-12 12:38:33 -0400133bool GrDirectContext::oomed() { return fGpu ? fGpu->checkAndResetOOMed() : false; }
134
Robert Phillipsad248452020-06-30 09:27:52 -0400135void GrDirectContext::releaseResourcesAndAbandonContext() {
Adlai Holler61a591c2020-10-12 12:38:33 -0400136 if (INHERITED::abandoned()) {
137 return;
138 }
139
140 INHERITED::abandonContext();
141
142 fMappedBufferManager.reset();
143
144 fResourceProvider->abandon();
145
146 // Release all resources in the backend 3D API.
147 fResourceCache->releaseAll();
148
149 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
Robert Phillips079455c2020-08-11 15:18:46 -0400150 if (fSmallPathAtlasMgr) {
151 fSmallPathAtlasMgr->reset();
152 }
Robert Phillipsad248452020-06-30 09:27:52 -0400153 fAtlasManager->freeAll();
154}
Robert Phillips6db27c22019-05-01 10:43:56 -0400155
Robert Phillipsad248452020-06-30 09:27:52 -0400156void GrDirectContext::freeGpuResources() {
Adlai Holler4aa4c602020-10-12 13:58:52 -0400157 ASSERT_SINGLE_OWNER
158
159 if (this->abandoned()) {
160 return;
161 }
162
Robert Phillipsad248452020-06-30 09:27:52 -0400163 this->flushAndSubmit();
Robert Phillips079455c2020-08-11 15:18:46 -0400164 if (fSmallPathAtlasMgr) {
165 fSmallPathAtlasMgr->reset();
166 }
Robert Phillipsad248452020-06-30 09:27:52 -0400167 fAtlasManager->freeAll();
Robert Phillips56181ba2019-03-08 12:00:45 -0500168
Adlai Holler4aa4c602020-10-12 13:58:52 -0400169 // TODO: the glyph cache doesn't hold any GpuResources so this call should not be needed here.
170 // Some slack in the GrTextBlob's implementation requires it though. That could be fixed.
171 fStrikeCache->freeAll();
172
173 this->drawingManager()->freeGpuResources();
174
175 fResourceCache->purgeAllUnlocked();
Robert Phillipsad248452020-06-30 09:27:52 -0400176}
Robert Phillipsa3457b82018-03-08 11:30:12 -0500177
Robert Phillipsad248452020-06-30 09:27:52 -0400178bool GrDirectContext::init() {
Adlai Holler9555f292020-10-09 09:41:14 -0400179 ASSERT_SINGLE_OWNER
180 if (!fGpu) {
Robert Phillipsad248452020-06-30 09:27:52 -0400181 return false;
Robert Phillipsa3457b82018-03-08 11:30:12 -0500182 }
183
Adlai Holler9555f292020-10-09 09:41:14 -0400184 fThreadSafeProxy->priv().init(fGpu->refCaps());
Robert Phillipsad248452020-06-30 09:27:52 -0400185 if (!INHERITED::init()) {
186 return false;
187 }
Robert Phillipsa3457b82018-03-08 11:30:12 -0500188
Adlai Holler9555f292020-10-09 09:41:14 -0400189 SkASSERT(this->getTextBlobCache());
190 SkASSERT(this->threadSafeCache());
191
192 fStrikeCache = std::make_unique<GrStrikeCache>();
193 fResourceCache = std::make_unique<GrResourceCache>(this->caps(), this->singleOwner(),
194 this->contextID());
195 fResourceCache->setProxyProvider(this->proxyProvider());
196 fResourceCache->setThreadSafeCache(this->threadSafeCache());
197 fResourceProvider = std::make_unique<GrResourceProvider>(fGpu.get(), fResourceCache.get(),
198 this->singleOwner());
199 fMappedBufferManager = std::make_unique<GrClientMappedBufferManager>(this->contextID());
200
201 fDidTestPMConversions = false;
202
203 // DDL TODO: we need to think through how the task group & persistent cache
204 // get passed on to/shared between all the DDLRecorders created with this context.
205 if (this->options().fExecutor) {
206 fTaskGroup = std::make_unique<SkTaskGroup>(*this->options().fExecutor);
207 }
208
209 fPersistentCache = this->options().fPersistentCache;
210 fShaderErrorHandler = this->options().fShaderErrorHandler;
211 if (!fShaderErrorHandler) {
212 fShaderErrorHandler = GrShaderUtils::DefaultShaderErrorHandler();
213 }
214
Robert Phillipsad248452020-06-30 09:27:52 -0400215 bool reduceOpsTaskSplitting = kDefaultReduceOpsTaskSplitting;
216 if (GrContextOptions::Enable::kNo == this->options().fReduceOpsTaskSplitting) {
217 reduceOpsTaskSplitting = false;
218 } else if (GrContextOptions::Enable::kYes == this->options().fReduceOpsTaskSplitting) {
219 reduceOpsTaskSplitting = true;
220 }
Robert Phillipsa3457b82018-03-08 11:30:12 -0500221
Robert Phillipsad248452020-06-30 09:27:52 -0400222 this->setupDrawingManager(true, reduceOpsTaskSplitting);
223
224 GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
225 if (GrContextOptions::Enable::kNo == this->options().fAllowMultipleGlyphCacheTextures ||
226 // multitexturing supported only if range can represent the index + texcoords fully
227 !(this->caps()->shaderCaps()->floatIs32Bits() ||
228 this->caps()->shaderCaps()->integerSupport())) {
229 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
230 } else {
231 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
232 }
233
234 GrProxyProvider* proxyProvider = this->priv().proxyProvider();
235
Robert Phillips3262bc82020-08-10 12:11:58 -0400236 fAtlasManager = std::make_unique<GrAtlasManager>(proxyProvider,
237 this->options().fGlyphCacheTextureMaximumBytes,
238 allowMultitexturing);
239 this->priv().addOnFlushCallbackObject(fAtlasManager.get());
Robert Phillipsad248452020-06-30 09:27:52 -0400240
241 return true;
242}
Robert Phillipsa3457b82018-03-08 11:30:12 -0500243
Adlai Holler3a508e92020-10-12 13:58:01 -0400244void GrDirectContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
245 ASSERT_SINGLE_OWNER
246
247 if (resourceCount) {
248 *resourceCount = fResourceCache->getBudgetedResourceCount();
249 }
250 if (resourceBytes) {
251 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
252 }
253}
254
255size_t GrDirectContext::getResourceCachePurgeableBytes() const {
256 ASSERT_SINGLE_OWNER
257 return fResourceCache->getPurgeableBytes();
258}
259
260void GrDirectContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
261 ASSERT_SINGLE_OWNER
262 if (maxResources) {
263 *maxResources = -1;
264 }
265 if (maxResourceBytes) {
266 *maxResourceBytes = this->getResourceCacheLimit();
267 }
268}
269
270size_t GrDirectContext::getResourceCacheLimit() const {
271 ASSERT_SINGLE_OWNER
272 return fResourceCache->getMaxResourceBytes();
273}
274
275void GrDirectContext::setResourceCacheLimits(int unused, size_t maxResourceBytes) {
276 ASSERT_SINGLE_OWNER
277 this->setResourceCacheLimit(maxResourceBytes);
278}
279
280void GrDirectContext::setResourceCacheLimit(size_t maxResourceBytes) {
281 ASSERT_SINGLE_OWNER
282 fResourceCache->setLimit(maxResourceBytes);
283}
284
Adlai Holler4aa4c602020-10-12 13:58:52 -0400285void GrDirectContext::purgeUnlockedResources(bool scratchResourcesOnly) {
286 ASSERT_SINGLE_OWNER
287
288 if (this->abandoned()) {
289 return;
290 }
291
292 fResourceCache->purgeUnlockedResources(scratchResourcesOnly);
293 fResourceCache->purgeAsNeeded();
294
295 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
296 // place to purge stale blobs
297 this->getTextBlobCache()->purgeStaleBlobs();
298}
299
300void GrDirectContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
301 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
302
303 ASSERT_SINGLE_OWNER
304
305 if (this->abandoned()) {
306 return;
307 }
308
309 this->checkAsyncWorkCompletion();
310 fMappedBufferManager->process();
311 auto purgeTime = GrStdSteadyClock::now() - msNotUsed;
312
313 fResourceCache->purgeAsNeeded();
314 fResourceCache->purgeResourcesNotUsedSince(purgeTime);
315
316 if (auto ccpr = this->drawingManager()->getCoverageCountingPathRenderer()) {
317 ccpr->purgeCacheEntriesOlderThan(this->proxyProvider(), purgeTime);
318 }
319
320 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
321 // place to purge stale blobs
322 this->getTextBlobCache()->purgeStaleBlobs();
323}
324
325void GrDirectContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
326 ASSERT_SINGLE_OWNER
327
328 if (this->abandoned()) {
329 return;
330 }
331
332 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
333}
334
Adlai Holler3acc69a2020-10-13 08:20:51 -0400335////////////////////////////////////////////////////////////////////////////////
336bool GrDirectContext::wait(int numSemaphores, const GrBackendSemaphore waitSemaphores[],
337 bool deleteSemaphoresAfterWait) {
338 if (!fGpu || fGpu->caps()->semaphoreSupport()) {
339 return false;
340 }
341 GrWrapOwnership ownership =
342 deleteSemaphoresAfterWait ? kAdopt_GrWrapOwnership : kBorrow_GrWrapOwnership;
343 for (int i = 0; i < numSemaphores; ++i) {
344 std::unique_ptr<GrSemaphore> sema = fResourceProvider->wrapBackendSemaphore(
345 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait, ownership);
346 // If we failed to wrap the semaphore it means the client didn't give us a valid semaphore
347 // to begin with. Therefore, it is fine to not wait on it.
348 if (sema) {
349 fGpu->waitSemaphore(sema.get());
350 }
351 }
352 return true;
353}
Adlai Holler4aa4c602020-10-12 13:58:52 -0400354
Robert Phillips5edf5102020-08-10 16:30:36 -0400355GrSmallPathAtlasMgr* GrDirectContext::onGetSmallPathAtlasMgr() {
Robert Phillips079455c2020-08-11 15:18:46 -0400356 if (!fSmallPathAtlasMgr) {
357 fSmallPathAtlasMgr = std::make_unique<GrSmallPathAtlasMgr>();
358
359 this->priv().addOnFlushCallbackObject(fSmallPathAtlasMgr.get());
360 }
361
362 if (!fSmallPathAtlasMgr->initAtlas(this->proxyProvider(), this->caps())) {
363 return nullptr;
364 }
365
366 return fSmallPathAtlasMgr.get();
Robert Phillips5edf5102020-08-10 16:30:36 -0400367}
368
Adlai Holler3acc69a2020-10-13 08:20:51 -0400369////////////////////////////////////////////////////////////////////////////////
370
371GrSemaphoresSubmitted GrDirectContext::flush(const GrFlushInfo& info) {
372 ASSERT_SINGLE_OWNER
373 if (this->abandoned()) {
374 if (info.fFinishedProc) {
375 info.fFinishedProc(info.fFinishedContext);
376 }
377 if (info.fSubmittedProc) {
378 info.fSubmittedProc(info.fSubmittedContext, false);
379 }
380 return GrSemaphoresSubmitted::kNo;
381 }
382
383 bool flushed = this->drawingManager()->flush(
384 nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, info, nullptr);
385
386 if (!flushed || (!this->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
387 return GrSemaphoresSubmitted::kNo;
388 }
389 return GrSemaphoresSubmitted::kYes;
390}
391
392bool GrDirectContext::submit(bool syncCpu) {
393 ASSERT_SINGLE_OWNER
394 if (this->abandoned()) {
395 return false;
396 }
397
398 if (!fGpu) {
399 return false;
400 }
401
402 return fGpu->submitToGpu(syncCpu);
403}
404
405////////////////////////////////////////////////////////////////////////////////
406
407void GrDirectContext::checkAsyncWorkCompletion() {
408 if (fGpu) {
409 fGpu->checkFinishProcs();
410 }
411}
412
413////////////////////////////////////////////////////////////////////////////////
414
415void GrDirectContext::storeVkPipelineCacheData() {
416 if (fGpu) {
417 fGpu->storeVkPipelineCacheData();
418 }
419}
420
421////////////////////////////////////////////////////////////////////////////////
422
423bool GrDirectContext::supportsDistanceFieldText() const {
424 return this->caps()->shaderCaps()->supportsDistanceFieldText();
425}
426
427//////////////////////////////////////////////////////////////////////////////
428
429void GrDirectContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
430 ASSERT_SINGLE_OWNER
431 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
432 traceMemoryDump->dumpNumericValue("skia/gr_text_blob_cache", "size", "bytes",
433 this->getTextBlobCache()->usedBytes());
434}
435
436size_t GrDirectContext::ComputeImageSize(sk_sp<SkImage> image, GrMipmapped mipMapped,
437 bool useNextPow2) {
438 if (!image->isTextureBacked()) {
439 return 0;
440 }
441 SkImage_GpuBase* gpuImage = static_cast<SkImage_GpuBase*>(as_IB(image.get()));
442 GrTextureProxy* proxy = gpuImage->peekProxy();
443 if (!proxy) {
444 return 0;
445 }
446
447 int colorSamplesPerPixel = 1;
448 return GrSurface::ComputeSize(proxy->backendFormat(), image->dimensions(),
449 colorSamplesPerPixel, mipMapped, useNextPow2);
450}
451
Adlai Holler98dd0042020-10-13 10:04:00 -0400452GrBackendTexture GrDirectContext::createBackendTexture(int width, int height,
453 const GrBackendFormat& backendFormat,
454 GrMipmapped mipMapped,
455 GrRenderable renderable,
456 GrProtected isProtected) {
457 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
458 if (this->abandoned()) {
459 return GrBackendTexture();
460 }
461
462 return fGpu->createBackendTexture({width, height}, backendFormat, renderable,
463 mipMapped, isProtected);
464}
465
466GrBackendTexture GrDirectContext::createBackendTexture(int width, int height,
467 SkColorType skColorType,
468 GrMipmapped mipMapped,
469 GrRenderable renderable,
470 GrProtected isProtected) {
471 if (this->abandoned()) {
472 return GrBackendTexture();
473 }
474
475 const GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
476
477 return this->createBackendTexture(width, height, format, mipMapped, renderable, isProtected);
478}
479
480static GrBackendTexture create_and_update_backend_texture(
481 GrDirectContext* dContext,
482 SkISize dimensions,
483 const GrBackendFormat& backendFormat,
484 GrMipmapped mipMapped,
485 GrRenderable renderable,
486 GrProtected isProtected,
487 sk_sp<GrRefCntedCallback> finishedCallback,
488 const GrGpu::BackendTextureData* data) {
489 GrGpu* gpu = dContext->priv().getGpu();
490
491 GrBackendTexture beTex = gpu->createBackendTexture(dimensions, backendFormat, renderable,
492 mipMapped, isProtected);
493 if (!beTex.isValid()) {
494 return {};
495 }
496
497 if (!dContext->priv().getGpu()->updateBackendTexture(beTex,
498 std::move(finishedCallback),
499 data)) {
500 dContext->deleteBackendTexture(beTex);
501 return {};
502 }
503 return beTex;
504}
505
506GrBackendTexture GrDirectContext::createBackendTexture(int width, int height,
507 const GrBackendFormat& backendFormat,
508 const SkColor4f& color,
509 GrMipmapped mipMapped,
510 GrRenderable renderable,
511 GrProtected isProtected,
512 GrGpuFinishedProc finishedProc,
513 GrGpuFinishedContext finishedContext) {
514 sk_sp<GrRefCntedCallback> finishedCallback;
515 if (finishedProc) {
516 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
517 }
518
519 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
520 if (this->abandoned()) {
521 return {};
522 }
523
524 GrGpu::BackendTextureData data(color);
525 return create_and_update_backend_texture(this, {width, height},
526 backendFormat, mipMapped, renderable, isProtected,
527 std::move(finishedCallback), &data);
528}
529
530GrBackendTexture GrDirectContext::createBackendTexture(int width, int height,
531 SkColorType skColorType,
532 const SkColor4f& color,
533 GrMipmapped mipMapped,
534 GrRenderable renderable,
535 GrProtected isProtected,
536 GrGpuFinishedProc finishedProc,
537 GrGpuFinishedContext finishedContext) {
538 sk_sp<GrRefCntedCallback> finishedCallback;
539 if (finishedProc) {
540 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
541 }
542
543 if (this->abandoned()) {
544 return {};
545 }
546
547 GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
548 if (!format.isValid()) {
549 return {};
550 }
551
552 GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
553 SkColor4f swizzledColor = this->caps()->getWriteSwizzle(format, grColorType).applyTo(color);
554
555 GrGpu::BackendTextureData data(swizzledColor);
556 return create_and_update_backend_texture(this, {width, height}, format,
557 mipMapped, renderable, isProtected,
558 std::move(finishedCallback), &data);
559}
560
561GrBackendTexture GrDirectContext::createBackendTexture(const SkPixmap srcData[],
562 int numProvidedLevels,
563 GrRenderable renderable,
564 GrProtected isProtected,
565 GrGpuFinishedProc finishedProc,
566 GrGpuFinishedContext finishedContext) {
567 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
568
569 sk_sp<GrRefCntedCallback> finishedCallback;
570 if (finishedProc) {
571 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
572 }
573
574 if (this->abandoned()) {
575 return {};
576 }
577
578 if (!srcData || numProvidedLevels <= 0) {
579 return {};
580 }
581
582 int baseWidth = srcData[0].width();
583 int baseHeight = srcData[0].height();
584 SkColorType colorType = srcData[0].colorType();
585
586 GrMipmapped mipMapped = GrMipmapped::kNo;
587 int numExpectedLevels = 1;
588 if (numProvidedLevels > 1) {
589 numExpectedLevels = SkMipmap::ComputeLevelCount(baseWidth, baseHeight) + 1;
590 mipMapped = GrMipmapped::kYes;
591 }
592
593 if (numProvidedLevels != numExpectedLevels) {
594 return {};
595 }
596
597 GrBackendFormat backendFormat = this->defaultBackendFormat(colorType, renderable);
598
599 GrGpu::BackendTextureData data(srcData);
600 return create_and_update_backend_texture(this, {baseWidth, baseHeight},
601 backendFormat, mipMapped, renderable, isProtected,
602 std::move(finishedCallback), &data);
603}
604
John Rosascoa9b348f2019-11-08 13:18:15 -0800605#ifdef SK_GL
Robert Phillipsc7228c62020-07-14 12:57:39 -0400606
Robert Phillipsf4f80112020-07-13 16:13:31 -0400607/*************************************************************************************************/
608sk_sp<GrDirectContext> GrDirectContext::MakeGL(sk_sp<const GrGLInterface> glInterface) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500609 GrContextOptions defaultOptions;
Jim Van Verth03b8ab22020-02-24 11:36:15 -0500610 return MakeGL(std::move(glInterface), defaultOptions);
Robert Phillipsa3457b82018-03-08 11:30:12 -0500611}
612
Robert Phillipsf4f80112020-07-13 16:13:31 -0400613sk_sp<GrDirectContext> GrDirectContext::MakeGL(const GrContextOptions& options) {
Brian Salomonc1b9c102018-04-06 09:18:00 -0400614 return MakeGL(nullptr, options);
615}
616
Robert Phillipsf4f80112020-07-13 16:13:31 -0400617sk_sp<GrDirectContext> GrDirectContext::MakeGL() {
Brian Salomonc1b9c102018-04-06 09:18:00 -0400618 GrContextOptions defaultOptions;
619 return MakeGL(nullptr, defaultOptions);
620}
621
Brian Salomon24069eb2020-06-24 10:19:52 -0400622#if GR_TEST_UTILS
623GrGLFunction<GrGLGetErrorFn> make_get_error_with_random_oom(GrGLFunction<GrGLGetErrorFn> original) {
624 // A SkRandom and a GrGLFunction<GrGLGetErrorFn> are too big to be captured by a
625 // GrGLFunction<GrGLGetError> (surprise, surprise). So we make a context object and
626 // capture that by pointer. However, GrGLFunction doesn't support calling a destructor
627 // on the thing it captures. So we leak the context.
628 struct GetErrorContext {
629 SkRandom fRandom;
630 GrGLFunction<GrGLGetErrorFn> fGetError;
631 };
632
633 auto errorContext = new GetErrorContext;
634
635#if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS)
636 __lsan_ignore_object(errorContext);
637#endif
638
639 errorContext->fGetError = original;
640
641 return GrGLFunction<GrGLGetErrorFn>([errorContext]() {
642 GrGLenum error = errorContext->fGetError();
643 if (error == GR_GL_NO_ERROR && (errorContext->fRandom.nextU() % 300) == 0) {
644 error = GR_GL_OUT_OF_MEMORY;
645 }
646 return error;
647 });
648}
649#endif
650
Robert Phillipsf4f80112020-07-13 16:13:31 -0400651sk_sp<GrDirectContext> GrDirectContext::MakeGL(sk_sp<const GrGLInterface> glInterface,
652 const GrContextOptions& options) {
653 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kOpenGL, options));
Brian Salomon24069eb2020-06-24 10:19:52 -0400654#if GR_TEST_UTILS
655 if (options.fRandomGLOOM) {
656 auto copy = sk_make_sp<GrGLInterface>(*glInterface);
657 copy->fFunctions.fGetError =
658 make_get_error_with_random_oom(glInterface->fFunctions.fGetError);
659#if GR_GL_CHECK_ERROR
660 // Suppress logging GL errors since we'll be synthetically generating them.
661 copy->suppressErrorLogging();
662#endif
663 glInterface = std::move(copy);
664 }
665#endif
Robert Phillipsf4f80112020-07-13 16:13:31 -0400666 direct->fGpu = GrGLGpu::Make(std::move(glInterface), options, direct.get());
667 if (!direct->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500668 return nullptr;
669 }
Robert Phillipsf4f80112020-07-13 16:13:31 -0400670 return direct;
Robert Phillipsa3457b82018-03-08 11:30:12 -0500671}
John Rosascoa9b348f2019-11-08 13:18:15 -0800672#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -0500673
Robert Phillipsf4f80112020-07-13 16:13:31 -0400674/*************************************************************************************************/
Robert Phillipsf4f80112020-07-13 16:13:31 -0400675sk_sp<GrDirectContext> GrDirectContext::MakeMock(const GrMockOptions* mockOptions) {
676 GrContextOptions defaultOptions;
677 return MakeMock(mockOptions, defaultOptions);
678}
679
680sk_sp<GrDirectContext> GrDirectContext::MakeMock(const GrMockOptions* mockOptions,
681 const GrContextOptions& options) {
682 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kMock, options));
683
684 direct->fGpu = GrMockGpu::Make(mockOptions, options, direct.get());
685 if (!direct->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500686 return nullptr;
687 }
Chris Daltona378b452019-12-11 13:24:11 -0500688
Robert Phillipsf4f80112020-07-13 16:13:31 -0400689 return direct;
Robert Phillipsa3457b82018-03-08 11:30:12 -0500690}
691
Greg Danielb4d89562018-10-03 18:44:49 +0000692#ifdef SK_VULKAN
Robert Phillipsf4f80112020-07-13 16:13:31 -0400693/*************************************************************************************************/
Robert Phillipsf4f80112020-07-13 16:13:31 -0400694sk_sp<GrDirectContext> GrDirectContext::MakeVulkan(const GrVkBackendContext& backendContext) {
695 GrContextOptions defaultOptions;
696 return MakeVulkan(backendContext, defaultOptions);
697}
698
699sk_sp<GrDirectContext> GrDirectContext::MakeVulkan(const GrVkBackendContext& backendContext,
700 const GrContextOptions& options) {
701 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kVulkan, options));
702
703 direct->fGpu = GrVkGpu::Make(backendContext, options, direct.get());
704 if (!direct->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500705 return nullptr;
706 }
707
Robert Phillipsf4f80112020-07-13 16:13:31 -0400708 return direct;
Greg Danielb4d89562018-10-03 18:44:49 +0000709}
Robert Phillipsf4f80112020-07-13 16:13:31 -0400710#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -0500711
712#ifdef SK_METAL
Robert Phillipsf4f80112020-07-13 16:13:31 -0400713/*************************************************************************************************/
Robert Phillipsf4f80112020-07-13 16:13:31 -0400714sk_sp<GrDirectContext> GrDirectContext::MakeMetal(void* device, void* queue) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500715 GrContextOptions defaultOptions;
716 return MakeMetal(device, queue, defaultOptions);
717}
718
Robert Phillipsf4f80112020-07-13 16:13:31 -0400719sk_sp<GrDirectContext> GrDirectContext::MakeMetal(void* device, void* queue,
720 const GrContextOptions& options) {
721 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kMetal, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500722
Robert Phillipsf4f80112020-07-13 16:13:31 -0400723 direct->fGpu = GrMtlTrampoline::MakeGpu(direct.get(), options, device, queue);
724 if (!direct->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500725 return nullptr;
726 }
Timothy Liang4e85e802018-06-28 16:37:18 -0400727
Robert Phillipsf4f80112020-07-13 16:13:31 -0400728 return direct;
Robert Phillipsa3457b82018-03-08 11:30:12 -0500729}
730#endif
731
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500732#ifdef SK_DIRECT3D
Robert Phillipsf4f80112020-07-13 16:13:31 -0400733/*************************************************************************************************/
Robert Phillipsf4f80112020-07-13 16:13:31 -0400734sk_sp<GrDirectContext> GrDirectContext::MakeDirect3D(const GrD3DBackendContext& backendContext) {
735 GrContextOptions defaultOptions;
736 return MakeDirect3D(backendContext, defaultOptions);
737}
738
739sk_sp<GrDirectContext> GrDirectContext::MakeDirect3D(const GrD3DBackendContext& backendContext,
740 const GrContextOptions& options) {
741 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kDirect3D, options));
742
743 direct->fGpu = GrD3DGpu::Make(backendContext, options, direct.get());
744 if (!direct->init()) {
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500745 return nullptr;
746 }
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500747
Robert Phillipsf4f80112020-07-13 16:13:31 -0400748 return direct;
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500749}
750#endif
751
Stephen White985741a2019-07-18 11:43:45 -0400752#ifdef SK_DAWN
Robert Phillipsf4f80112020-07-13 16:13:31 -0400753/*************************************************************************************************/
Robert Phillipsf4f80112020-07-13 16:13:31 -0400754sk_sp<GrDirectContext> GrDirectContext::MakeDawn(const wgpu::Device& device) {
Stephen White985741a2019-07-18 11:43:45 -0400755 GrContextOptions defaultOptions;
756 return MakeDawn(device, defaultOptions);
757}
758
Robert Phillipsf4f80112020-07-13 16:13:31 -0400759sk_sp<GrDirectContext> GrDirectContext::MakeDawn(const wgpu::Device& device,
760 const GrContextOptions& options) {
761 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kDawn, options));
Stephen White985741a2019-07-18 11:43:45 -0400762
Robert Phillipsf4f80112020-07-13 16:13:31 -0400763 direct->fGpu = GrDawnGpu::Make(device, options, direct.get());
764 if (!direct->init()) {
Stephen White985741a2019-07-18 11:43:45 -0400765 return nullptr;
766 }
767
Robert Phillipsf4f80112020-07-13 16:13:31 -0400768 return direct;
Stephen White985741a2019-07-18 11:43:45 -0400769}
Robert Phillipsf4f80112020-07-13 16:13:31 -0400770
Stephen White985741a2019-07-18 11:43:45 -0400771#endif