blob: aafe5a6863e80c6f18ecc7055ca9a1bc5ef13dc2 [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
Adlai Holler2e0c70d2020-10-13 08:21:37 -0400605bool GrDirectContext::updateBackendTexture(const GrBackendTexture& backendTexture,
606 const SkColor4f& color,
607 GrGpuFinishedProc finishedProc,
608 GrGpuFinishedContext finishedContext) {
609 sk_sp<GrRefCntedCallback> finishedCallback;
610 if (finishedProc) {
611 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
612 }
613
614 if (this->abandoned()) {
615 return false;
616 }
617
618 GrGpu::BackendTextureData data(color);
619 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
620}
621
622bool GrDirectContext::updateBackendTexture(const GrBackendTexture& backendTexture,
623 SkColorType skColorType,
624 const SkColor4f& color,
625 GrGpuFinishedProc finishedProc,
626 GrGpuFinishedContext finishedContext) {
627 sk_sp<GrRefCntedCallback> finishedCallback;
628 if (finishedProc) {
629 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
630 }
631
632 if (this->abandoned()) {
633 return false;
634 }
635
636 GrBackendFormat format = backendTexture.getBackendFormat();
637 GrColorType grColorType = SkColorTypeAndFormatToGrColorType(this->caps(), skColorType, format);
638
639 if (!this->caps()->areColorTypeAndFormatCompatible(grColorType, format)) {
640 return false;
641 }
642
643 GrSwizzle swizzle = this->caps()->getWriteSwizzle(format, grColorType);
644 GrGpu::BackendTextureData data(swizzle.applyTo(color));
645
646 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
647}
648
649bool GrDirectContext::updateBackendTexture(const GrBackendTexture& backendTexture,
650 const SkPixmap srcData[],
651 int numLevels,
652 GrGpuFinishedProc finishedProc,
653 GrGpuFinishedContext finishedContext) {
654 sk_sp<GrRefCntedCallback> finishedCallback;
655 if (finishedProc) {
656 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
657 }
658
659 if (this->abandoned()) {
660 return false;
661 }
662
663 if (!srcData || numLevels <= 0) {
664 return false;
665 }
666
667 int numExpectedLevels = 1;
668 if (backendTexture.hasMipmaps()) {
669 numExpectedLevels = SkMipmap::ComputeLevelCount(backendTexture.width(),
670 backendTexture.height()) + 1;
671 }
672 if (numLevels != numExpectedLevels) {
673 return false;
674 }
675
676 GrGpu::BackendTextureData data(srcData);
677 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
678}
679
Adlai Holler64e13832020-10-13 08:21:56 -0400680//////////////////////////////////////////////////////////////////////////////
681
682static GrBackendTexture create_and_update_compressed_backend_texture(
683 GrDirectContext* dContext,
684 SkISize dimensions,
685 const GrBackendFormat& backendFormat,
686 GrMipmapped mipMapped,
687 GrProtected isProtected,
688 sk_sp<GrRefCntedCallback> finishedCallback,
689 const GrGpu::BackendTextureData* data) {
690 GrGpu* gpu = dContext->priv().getGpu();
691
692 GrBackendTexture beTex = gpu->createCompressedBackendTexture(dimensions, backendFormat,
693 mipMapped, isProtected);
694 if (!beTex.isValid()) {
695 return {};
696 }
697
698 if (!dContext->priv().getGpu()->updateCompressedBackendTexture(
699 beTex, std::move(finishedCallback), data)) {
700 dContext->deleteBackendTexture(beTex);
701 return {};
702 }
703 return beTex;
704}
705
706GrBackendTexture GrDirectContext::createCompressedBackendTexture(int width, int height,
707 const GrBackendFormat& backendFormat,
708 const SkColor4f& color,
709 GrMipmapped mipMapped,
710 GrProtected isProtected,
711 GrGpuFinishedProc finishedProc,
712 GrGpuFinishedContext finishedContext) {
713 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
714 sk_sp<GrRefCntedCallback> finishedCallback;
715 if (finishedProc) {
716 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
717 }
718
719 if (this->abandoned()) {
720 return {};
721 }
722
723 GrGpu::BackendTextureData data(color);
724 return create_and_update_compressed_backend_texture(this, {width, height},
725 backendFormat, mipMapped, isProtected,
726 std::move(finishedCallback), &data);
727}
728
729GrBackendTexture GrDirectContext::createCompressedBackendTexture(int width, int height,
730 SkImage::CompressionType compression,
731 const SkColor4f& color,
732 GrMipmapped mipMapped,
733 GrProtected isProtected,
734 GrGpuFinishedProc finishedProc,
735 GrGpuFinishedContext finishedContext) {
736 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
737 GrBackendFormat format = this->compressedBackendFormat(compression);
738 return this->createCompressedBackendTexture(width, height, format, color,
739 mipMapped, isProtected, finishedProc,
740 finishedContext);
741}
742
743GrBackendTexture GrDirectContext::createCompressedBackendTexture(int width, int height,
744 const GrBackendFormat& backendFormat,
745 const void* compressedData,
746 size_t dataSize,
747 GrMipmapped mipMapped,
748 GrProtected isProtected,
749 GrGpuFinishedProc finishedProc,
750 GrGpuFinishedContext finishedContext) {
751 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
752 sk_sp<GrRefCntedCallback> finishedCallback;
753 if (finishedProc) {
754 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
755 }
756
757 if (this->abandoned()) {
758 return {};
759 }
760
761 GrGpu::BackendTextureData data(compressedData, dataSize);
762 return create_and_update_compressed_backend_texture(this, {width, height},
763 backendFormat, mipMapped, isProtected,
764 std::move(finishedCallback), &data);
765}
766
767GrBackendTexture GrDirectContext::createCompressedBackendTexture(int width, int height,
768 SkImage::CompressionType compression,
769 const void* data, size_t dataSize,
770 GrMipmapped mipMapped,
771 GrProtected isProtected,
772 GrGpuFinishedProc finishedProc,
773 GrGpuFinishedContext finishedContext) {
774 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
775 GrBackendFormat format = this->compressedBackendFormat(compression);
776 return this->createCompressedBackendTexture(width, height, format, data, dataSize, mipMapped,
777 isProtected, finishedProc, finishedContext);
778}
779
780bool GrDirectContext::updateCompressedBackendTexture(const GrBackendTexture& backendTexture,
781 const SkColor4f& color,
782 GrGpuFinishedProc finishedProc,
783 GrGpuFinishedContext finishedContext) {
784 sk_sp<GrRefCntedCallback> finishedCallback;
785 if (finishedProc) {
786 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
787 }
788
789 if (this->abandoned()) {
790 return false;
791 }
792
793 GrGpu::BackendTextureData data(color);
794 return fGpu->updateCompressedBackendTexture(backendTexture, std::move(finishedCallback), &data);
795}
796
797bool GrDirectContext::updateCompressedBackendTexture(const GrBackendTexture& backendTexture,
798 const void* compressedData,
799 size_t dataSize,
800 GrGpuFinishedProc finishedProc,
801 GrGpuFinishedContext finishedContext) {
802 sk_sp<GrRefCntedCallback> finishedCallback;
803 if (finishedProc) {
804 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
805 }
806
807 if (this->abandoned()) {
808 return false;
809 }
810
811 if (!compressedData) {
812 return false;
813 }
814
815 GrGpu::BackendTextureData data(compressedData, dataSize);
816
817 return fGpu->updateCompressedBackendTexture(backendTexture, std::move(finishedCallback), &data);
818}
819
John Rosascoa9b348f2019-11-08 13:18:15 -0800820#ifdef SK_GL
Robert Phillipsc7228c62020-07-14 12:57:39 -0400821
Robert Phillipsf4f80112020-07-13 16:13:31 -0400822/*************************************************************************************************/
823sk_sp<GrDirectContext> GrDirectContext::MakeGL(sk_sp<const GrGLInterface> glInterface) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500824 GrContextOptions defaultOptions;
Jim Van Verth03b8ab22020-02-24 11:36:15 -0500825 return MakeGL(std::move(glInterface), defaultOptions);
Robert Phillipsa3457b82018-03-08 11:30:12 -0500826}
827
Robert Phillipsf4f80112020-07-13 16:13:31 -0400828sk_sp<GrDirectContext> GrDirectContext::MakeGL(const GrContextOptions& options) {
Brian Salomonc1b9c102018-04-06 09:18:00 -0400829 return MakeGL(nullptr, options);
830}
831
Robert Phillipsf4f80112020-07-13 16:13:31 -0400832sk_sp<GrDirectContext> GrDirectContext::MakeGL() {
Brian Salomonc1b9c102018-04-06 09:18:00 -0400833 GrContextOptions defaultOptions;
834 return MakeGL(nullptr, defaultOptions);
835}
836
Brian Salomon24069eb2020-06-24 10:19:52 -0400837#if GR_TEST_UTILS
838GrGLFunction<GrGLGetErrorFn> make_get_error_with_random_oom(GrGLFunction<GrGLGetErrorFn> original) {
839 // A SkRandom and a GrGLFunction<GrGLGetErrorFn> are too big to be captured by a
840 // GrGLFunction<GrGLGetError> (surprise, surprise). So we make a context object and
841 // capture that by pointer. However, GrGLFunction doesn't support calling a destructor
842 // on the thing it captures. So we leak the context.
843 struct GetErrorContext {
844 SkRandom fRandom;
845 GrGLFunction<GrGLGetErrorFn> fGetError;
846 };
847
848 auto errorContext = new GetErrorContext;
849
850#if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS)
851 __lsan_ignore_object(errorContext);
852#endif
853
854 errorContext->fGetError = original;
855
856 return GrGLFunction<GrGLGetErrorFn>([errorContext]() {
857 GrGLenum error = errorContext->fGetError();
858 if (error == GR_GL_NO_ERROR && (errorContext->fRandom.nextU() % 300) == 0) {
859 error = GR_GL_OUT_OF_MEMORY;
860 }
861 return error;
862 });
863}
864#endif
865
Robert Phillipsf4f80112020-07-13 16:13:31 -0400866sk_sp<GrDirectContext> GrDirectContext::MakeGL(sk_sp<const GrGLInterface> glInterface,
867 const GrContextOptions& options) {
868 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kOpenGL, options));
Brian Salomon24069eb2020-06-24 10:19:52 -0400869#if GR_TEST_UTILS
870 if (options.fRandomGLOOM) {
871 auto copy = sk_make_sp<GrGLInterface>(*glInterface);
872 copy->fFunctions.fGetError =
873 make_get_error_with_random_oom(glInterface->fFunctions.fGetError);
874#if GR_GL_CHECK_ERROR
875 // Suppress logging GL errors since we'll be synthetically generating them.
876 copy->suppressErrorLogging();
877#endif
878 glInterface = std::move(copy);
879 }
880#endif
Robert Phillipsf4f80112020-07-13 16:13:31 -0400881 direct->fGpu = GrGLGpu::Make(std::move(glInterface), options, direct.get());
882 if (!direct->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500883 return nullptr;
884 }
Robert Phillipsf4f80112020-07-13 16:13:31 -0400885 return direct;
Robert Phillipsa3457b82018-03-08 11:30:12 -0500886}
John Rosascoa9b348f2019-11-08 13:18:15 -0800887#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -0500888
Robert Phillipsf4f80112020-07-13 16:13:31 -0400889/*************************************************************************************************/
Robert Phillipsf4f80112020-07-13 16:13:31 -0400890sk_sp<GrDirectContext> GrDirectContext::MakeMock(const GrMockOptions* mockOptions) {
891 GrContextOptions defaultOptions;
892 return MakeMock(mockOptions, defaultOptions);
893}
894
895sk_sp<GrDirectContext> GrDirectContext::MakeMock(const GrMockOptions* mockOptions,
896 const GrContextOptions& options) {
897 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kMock, options));
898
899 direct->fGpu = GrMockGpu::Make(mockOptions, options, direct.get());
900 if (!direct->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500901 return nullptr;
902 }
Chris Daltona378b452019-12-11 13:24:11 -0500903
Robert Phillipsf4f80112020-07-13 16:13:31 -0400904 return direct;
Robert Phillipsa3457b82018-03-08 11:30:12 -0500905}
906
Greg Danielb4d89562018-10-03 18:44:49 +0000907#ifdef SK_VULKAN
Robert Phillipsf4f80112020-07-13 16:13:31 -0400908/*************************************************************************************************/
Robert Phillipsf4f80112020-07-13 16:13:31 -0400909sk_sp<GrDirectContext> GrDirectContext::MakeVulkan(const GrVkBackendContext& backendContext) {
910 GrContextOptions defaultOptions;
911 return MakeVulkan(backendContext, defaultOptions);
912}
913
914sk_sp<GrDirectContext> GrDirectContext::MakeVulkan(const GrVkBackendContext& backendContext,
915 const GrContextOptions& options) {
916 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kVulkan, options));
917
918 direct->fGpu = GrVkGpu::Make(backendContext, options, direct.get());
919 if (!direct->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500920 return nullptr;
921 }
922
Robert Phillipsf4f80112020-07-13 16:13:31 -0400923 return direct;
Greg Danielb4d89562018-10-03 18:44:49 +0000924}
Robert Phillipsf4f80112020-07-13 16:13:31 -0400925#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -0500926
927#ifdef SK_METAL
Robert Phillipsf4f80112020-07-13 16:13:31 -0400928/*************************************************************************************************/
Robert Phillipsf4f80112020-07-13 16:13:31 -0400929sk_sp<GrDirectContext> GrDirectContext::MakeMetal(void* device, void* queue) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500930 GrContextOptions defaultOptions;
931 return MakeMetal(device, queue, defaultOptions);
932}
933
Robert Phillipsf4f80112020-07-13 16:13:31 -0400934sk_sp<GrDirectContext> GrDirectContext::MakeMetal(void* device, void* queue,
935 const GrContextOptions& options) {
936 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kMetal, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -0500937
Robert Phillipsf4f80112020-07-13 16:13:31 -0400938 direct->fGpu = GrMtlTrampoline::MakeGpu(direct.get(), options, device, queue);
939 if (!direct->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500940 return nullptr;
941 }
Timothy Liang4e85e802018-06-28 16:37:18 -0400942
Robert Phillipsf4f80112020-07-13 16:13:31 -0400943 return direct;
Robert Phillipsa3457b82018-03-08 11:30:12 -0500944}
945#endif
946
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500947#ifdef SK_DIRECT3D
Robert Phillipsf4f80112020-07-13 16:13:31 -0400948/*************************************************************************************************/
Robert Phillipsf4f80112020-07-13 16:13:31 -0400949sk_sp<GrDirectContext> GrDirectContext::MakeDirect3D(const GrD3DBackendContext& backendContext) {
950 GrContextOptions defaultOptions;
951 return MakeDirect3D(backendContext, defaultOptions);
952}
953
954sk_sp<GrDirectContext> GrDirectContext::MakeDirect3D(const GrD3DBackendContext& backendContext,
955 const GrContextOptions& options) {
956 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kDirect3D, options));
957
958 direct->fGpu = GrD3DGpu::Make(backendContext, options, direct.get());
959 if (!direct->init()) {
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -0500960 return nullptr;
961 }
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500962
Robert Phillipsf4f80112020-07-13 16:13:31 -0400963 return direct;
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500964}
965#endif
966
Stephen White985741a2019-07-18 11:43:45 -0400967#ifdef SK_DAWN
Robert Phillipsf4f80112020-07-13 16:13:31 -0400968/*************************************************************************************************/
Robert Phillipsf4f80112020-07-13 16:13:31 -0400969sk_sp<GrDirectContext> GrDirectContext::MakeDawn(const wgpu::Device& device) {
Stephen White985741a2019-07-18 11:43:45 -0400970 GrContextOptions defaultOptions;
971 return MakeDawn(device, defaultOptions);
972}
973
Robert Phillipsf4f80112020-07-13 16:13:31 -0400974sk_sp<GrDirectContext> GrDirectContext::MakeDawn(const wgpu::Device& device,
975 const GrContextOptions& options) {
976 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kDawn, options));
Stephen White985741a2019-07-18 11:43:45 -0400977
Robert Phillipsf4f80112020-07-13 16:13:31 -0400978 direct->fGpu = GrDawnGpu::Make(device, options, direct.get());
979 if (!direct->init()) {
Stephen White985741a2019-07-18 11:43:45 -0400980 return nullptr;
981 }
982
Robert Phillipsf4f80112020-07-13 16:13:31 -0400983 return direct;
Stephen White985741a2019-07-18 11:43:45 -0400984}
Robert Phillipsf4f80112020-07-13 16:13:31 -0400985
Stephen White985741a2019-07-18 11:43:45 -0400986#endif