blob: 988f567c7655b7ddf7401025ea560e5a665f5407 [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/GrContextThreadSafeProxyPriv.h"
Adlai Hollera0693042020-10-14 11:23:11 -040016#include "src/gpu/GrDirectContextPriv.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
Jim Van Verth351c9b52020-11-12 15:21:11 -050031#include "include/gpu/mtl/GrMtlBackendContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/gpu/mtl/GrMtlTrampoline.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050033#endif
34#ifdef SK_VULKAN
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/gpu/vk/GrVkGpu.h"
Robert Phillipsa3457b82018-03-08 11:30:12 -050036#endif
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -050037#ifdef SK_DIRECT3D
38#include "src/gpu/d3d/GrD3DGpu.h"
39#endif
Stephen White985741a2019-07-18 11:43:45 -040040#ifdef SK_DAWN
Mike Klein52337de2019-07-25 09:00:52 -050041#include "src/gpu/dawn/GrDawnGpu.h"
Stephen White985741a2019-07-18 11:43:45 -040042#endif
Adlai Holler6d0745b2020-10-13 13:29:00 -040043#include <memory>
Robert Phillipsa3457b82018-03-08 11:30:12 -050044
Brian Salomon24069eb2020-06-24 10:19:52 -040045#if GR_TEST_UTILS
46# include "include/utils/SkRandom.h"
47# if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS)
48# include <sanitizer/lsan_interface.h>
49# endif
50#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
Robert Phillipseb999bc2020-11-03 08:41:47 -0500104 // abandon first so destructors don't try to free the resources in the API.
Adlai Hollera7a40442020-10-09 09:49:42 -0400105 fResourceCache->abandonAll();
106
107 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
108
109 fMappedBufferManager.reset();
Robert Phillips079455c2020-08-11 15:18:46 -0400110 if (fSmallPathAtlasMgr) {
111 fSmallPathAtlasMgr->reset();
112 }
Robert Phillipsad248452020-06-30 09:27:52 -0400113 fAtlasManager->freeAll();
114}
Robert Phillipsa3457b82018-03-08 11:30:12 -0500115
Adlai Hollera7a40442020-10-09 09:49:42 -0400116bool GrDirectContext::abandoned() {
117 if (INHERITED::abandoned()) {
118 return true;
119 }
120
121 if (fGpu && fGpu->isDeviceLost()) {
122 this->abandonContext();
123 return true;
124 }
125 return false;
126}
127
Adlai Holler61a591c2020-10-12 12:38:33 -0400128bool GrDirectContext::oomed() { return fGpu ? fGpu->checkAndResetOOMed() : false; }
129
Robert Phillipsad248452020-06-30 09:27:52 -0400130void GrDirectContext::releaseResourcesAndAbandonContext() {
Adlai Holler61a591c2020-10-12 12:38:33 -0400131 if (INHERITED::abandoned()) {
132 return;
133 }
134
135 INHERITED::abandonContext();
136
137 fMappedBufferManager.reset();
138
139 fResourceProvider->abandon();
140
141 // Release all resources in the backend 3D API.
142 fResourceCache->releaseAll();
143
144 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
Robert Phillips079455c2020-08-11 15:18:46 -0400145 if (fSmallPathAtlasMgr) {
146 fSmallPathAtlasMgr->reset();
147 }
Robert Phillipsad248452020-06-30 09:27:52 -0400148 fAtlasManager->freeAll();
149}
Robert Phillips6db27c22019-05-01 10:43:56 -0400150
Robert Phillipsad248452020-06-30 09:27:52 -0400151void GrDirectContext::freeGpuResources() {
Adlai Holler4aa4c602020-10-12 13:58:52 -0400152 ASSERT_SINGLE_OWNER
153
154 if (this->abandoned()) {
155 return;
156 }
157
Robert Phillipsad248452020-06-30 09:27:52 -0400158 this->flushAndSubmit();
Robert Phillips079455c2020-08-11 15:18:46 -0400159 if (fSmallPathAtlasMgr) {
160 fSmallPathAtlasMgr->reset();
161 }
Robert Phillipsad248452020-06-30 09:27:52 -0400162 fAtlasManager->freeAll();
Robert Phillips56181ba2019-03-08 12:00:45 -0500163
Adlai Holler4aa4c602020-10-12 13:58:52 -0400164 // TODO: the glyph cache doesn't hold any GpuResources so this call should not be needed here.
165 // Some slack in the GrTextBlob's implementation requires it though. That could be fixed.
166 fStrikeCache->freeAll();
167
168 this->drawingManager()->freeGpuResources();
169
170 fResourceCache->purgeAllUnlocked();
Robert Phillipsad248452020-06-30 09:27:52 -0400171}
Robert Phillipsa3457b82018-03-08 11:30:12 -0500172
Robert Phillipsad248452020-06-30 09:27:52 -0400173bool GrDirectContext::init() {
Adlai Holler9555f292020-10-09 09:41:14 -0400174 ASSERT_SINGLE_OWNER
175 if (!fGpu) {
Robert Phillipsad248452020-06-30 09:27:52 -0400176 return false;
Robert Phillipsa3457b82018-03-08 11:30:12 -0500177 }
178
Adlai Holler9555f292020-10-09 09:41:14 -0400179 fThreadSafeProxy->priv().init(fGpu->refCaps());
Robert Phillipsad248452020-06-30 09:27:52 -0400180 if (!INHERITED::init()) {
181 return false;
182 }
Robert Phillipsa3457b82018-03-08 11:30:12 -0500183
Adlai Holler9555f292020-10-09 09:41:14 -0400184 SkASSERT(this->getTextBlobCache());
185 SkASSERT(this->threadSafeCache());
186
187 fStrikeCache = std::make_unique<GrStrikeCache>();
188 fResourceCache = std::make_unique<GrResourceCache>(this->caps(), this->singleOwner(),
189 this->contextID());
190 fResourceCache->setProxyProvider(this->proxyProvider());
191 fResourceCache->setThreadSafeCache(this->threadSafeCache());
192 fResourceProvider = std::make_unique<GrResourceProvider>(fGpu.get(), fResourceCache.get(),
193 this->singleOwner());
194 fMappedBufferManager = std::make_unique<GrClientMappedBufferManager>(this->contextID());
195
196 fDidTestPMConversions = false;
197
198 // DDL TODO: we need to think through how the task group & persistent cache
199 // get passed on to/shared between all the DDLRecorders created with this context.
200 if (this->options().fExecutor) {
201 fTaskGroup = std::make_unique<SkTaskGroup>(*this->options().fExecutor);
202 }
203
204 fPersistentCache = this->options().fPersistentCache;
205 fShaderErrorHandler = this->options().fShaderErrorHandler;
206 if (!fShaderErrorHandler) {
207 fShaderErrorHandler = GrShaderUtils::DefaultShaderErrorHandler();
208 }
209
Robert Phillipsad248452020-06-30 09:27:52 -0400210 GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
211 if (GrContextOptions::Enable::kNo == this->options().fAllowMultipleGlyphCacheTextures ||
212 // multitexturing supported only if range can represent the index + texcoords fully
213 !(this->caps()->shaderCaps()->floatIs32Bits() ||
214 this->caps()->shaderCaps()->integerSupport())) {
215 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
216 } else {
217 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
218 }
219
220 GrProxyProvider* proxyProvider = this->priv().proxyProvider();
221
Robert Phillips3262bc82020-08-10 12:11:58 -0400222 fAtlasManager = std::make_unique<GrAtlasManager>(proxyProvider,
223 this->options().fGlyphCacheTextureMaximumBytes,
224 allowMultitexturing);
225 this->priv().addOnFlushCallbackObject(fAtlasManager.get());
Robert Phillipsad248452020-06-30 09:27:52 -0400226
227 return true;
228}
Robert Phillipsa3457b82018-03-08 11:30:12 -0500229
Adlai Holler3a508e92020-10-12 13:58:01 -0400230void GrDirectContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
231 ASSERT_SINGLE_OWNER
232
233 if (resourceCount) {
234 *resourceCount = fResourceCache->getBudgetedResourceCount();
235 }
236 if (resourceBytes) {
237 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
238 }
239}
240
241size_t GrDirectContext::getResourceCachePurgeableBytes() const {
242 ASSERT_SINGLE_OWNER
243 return fResourceCache->getPurgeableBytes();
244}
245
246void GrDirectContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
247 ASSERT_SINGLE_OWNER
248 if (maxResources) {
249 *maxResources = -1;
250 }
251 if (maxResourceBytes) {
252 *maxResourceBytes = this->getResourceCacheLimit();
253 }
254}
255
256size_t GrDirectContext::getResourceCacheLimit() const {
257 ASSERT_SINGLE_OWNER
258 return fResourceCache->getMaxResourceBytes();
259}
260
261void GrDirectContext::setResourceCacheLimits(int unused, size_t maxResourceBytes) {
262 ASSERT_SINGLE_OWNER
263 this->setResourceCacheLimit(maxResourceBytes);
264}
265
266void GrDirectContext::setResourceCacheLimit(size_t maxResourceBytes) {
267 ASSERT_SINGLE_OWNER
268 fResourceCache->setLimit(maxResourceBytes);
269}
270
Adlai Holler4aa4c602020-10-12 13:58:52 -0400271void GrDirectContext::purgeUnlockedResources(bool scratchResourcesOnly) {
272 ASSERT_SINGLE_OWNER
273
274 if (this->abandoned()) {
275 return;
276 }
277
278 fResourceCache->purgeUnlockedResources(scratchResourcesOnly);
279 fResourceCache->purgeAsNeeded();
280
281 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
282 // place to purge stale blobs
283 this->getTextBlobCache()->purgeStaleBlobs();
284}
285
286void GrDirectContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
287 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
288
289 ASSERT_SINGLE_OWNER
290
291 if (this->abandoned()) {
292 return;
293 }
294
295 this->checkAsyncWorkCompletion();
296 fMappedBufferManager->process();
297 auto purgeTime = GrStdSteadyClock::now() - msNotUsed;
298
299 fResourceCache->purgeAsNeeded();
300 fResourceCache->purgeResourcesNotUsedSince(purgeTime);
301
302 if (auto ccpr = this->drawingManager()->getCoverageCountingPathRenderer()) {
303 ccpr->purgeCacheEntriesOlderThan(this->proxyProvider(), purgeTime);
304 }
305
306 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
307 // place to purge stale blobs
308 this->getTextBlobCache()->purgeStaleBlobs();
309}
310
311void GrDirectContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
312 ASSERT_SINGLE_OWNER
313
314 if (this->abandoned()) {
315 return;
316 }
317
318 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
319}
320
Adlai Holler3acc69a2020-10-13 08:20:51 -0400321////////////////////////////////////////////////////////////////////////////////
322bool GrDirectContext::wait(int numSemaphores, const GrBackendSemaphore waitSemaphores[],
323 bool deleteSemaphoresAfterWait) {
324 if (!fGpu || fGpu->caps()->semaphoreSupport()) {
325 return false;
326 }
327 GrWrapOwnership ownership =
328 deleteSemaphoresAfterWait ? kAdopt_GrWrapOwnership : kBorrow_GrWrapOwnership;
329 for (int i = 0; i < numSemaphores; ++i) {
330 std::unique_ptr<GrSemaphore> sema = fResourceProvider->wrapBackendSemaphore(
331 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait, ownership);
332 // If we failed to wrap the semaphore it means the client didn't give us a valid semaphore
333 // to begin with. Therefore, it is fine to not wait on it.
334 if (sema) {
335 fGpu->waitSemaphore(sema.get());
336 }
337 }
338 return true;
339}
Adlai Holler4aa4c602020-10-12 13:58:52 -0400340
Robert Phillips5edf5102020-08-10 16:30:36 -0400341GrSmallPathAtlasMgr* GrDirectContext::onGetSmallPathAtlasMgr() {
Robert Phillips079455c2020-08-11 15:18:46 -0400342 if (!fSmallPathAtlasMgr) {
343 fSmallPathAtlasMgr = std::make_unique<GrSmallPathAtlasMgr>();
344
345 this->priv().addOnFlushCallbackObject(fSmallPathAtlasMgr.get());
346 }
347
348 if (!fSmallPathAtlasMgr->initAtlas(this->proxyProvider(), this->caps())) {
349 return nullptr;
350 }
351
352 return fSmallPathAtlasMgr.get();
Robert Phillips5edf5102020-08-10 16:30:36 -0400353}
354
Adlai Holler3acc69a2020-10-13 08:20:51 -0400355////////////////////////////////////////////////////////////////////////////////
356
357GrSemaphoresSubmitted GrDirectContext::flush(const GrFlushInfo& info) {
358 ASSERT_SINGLE_OWNER
359 if (this->abandoned()) {
360 if (info.fFinishedProc) {
361 info.fFinishedProc(info.fFinishedContext);
362 }
363 if (info.fSubmittedProc) {
364 info.fSubmittedProc(info.fSubmittedContext, false);
365 }
366 return GrSemaphoresSubmitted::kNo;
367 }
368
Robert Phillips80bfda82020-11-12 09:23:36 -0500369 return this->drawingManager()->flushSurfaces({}, SkSurface::BackendSurfaceAccess::kNoAccess,
370 info, nullptr);
Adlai Holler3acc69a2020-10-13 08:20:51 -0400371}
372
373bool GrDirectContext::submit(bool syncCpu) {
374 ASSERT_SINGLE_OWNER
375 if (this->abandoned()) {
376 return false;
377 }
378
379 if (!fGpu) {
380 return false;
381 }
382
383 return fGpu->submitToGpu(syncCpu);
384}
385
386////////////////////////////////////////////////////////////////////////////////
387
388void GrDirectContext::checkAsyncWorkCompletion() {
389 if (fGpu) {
390 fGpu->checkFinishProcs();
391 }
392}
393
394////////////////////////////////////////////////////////////////////////////////
395
396void GrDirectContext::storeVkPipelineCacheData() {
397 if (fGpu) {
398 fGpu->storeVkPipelineCacheData();
399 }
400}
401
402////////////////////////////////////////////////////////////////////////////////
403
404bool GrDirectContext::supportsDistanceFieldText() const {
405 return this->caps()->shaderCaps()->supportsDistanceFieldText();
406}
407
408//////////////////////////////////////////////////////////////////////////////
409
410void GrDirectContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
411 ASSERT_SINGLE_OWNER
412 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
413 traceMemoryDump->dumpNumericValue("skia/gr_text_blob_cache", "size", "bytes",
414 this->getTextBlobCache()->usedBytes());
415}
416
417size_t GrDirectContext::ComputeImageSize(sk_sp<SkImage> image, GrMipmapped mipMapped,
418 bool useNextPow2) {
419 if (!image->isTextureBacked()) {
420 return 0;
421 }
422 SkImage_GpuBase* gpuImage = static_cast<SkImage_GpuBase*>(as_IB(image.get()));
423 GrTextureProxy* proxy = gpuImage->peekProxy();
424 if (!proxy) {
425 return 0;
426 }
427
428 int colorSamplesPerPixel = 1;
429 return GrSurface::ComputeSize(proxy->backendFormat(), image->dimensions(),
430 colorSamplesPerPixel, mipMapped, useNextPow2);
431}
432
Adlai Holler98dd0042020-10-13 10:04:00 -0400433GrBackendTexture GrDirectContext::createBackendTexture(int width, int height,
434 const GrBackendFormat& backendFormat,
435 GrMipmapped mipMapped,
436 GrRenderable renderable,
437 GrProtected isProtected) {
438 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
439 if (this->abandoned()) {
440 return GrBackendTexture();
441 }
442
443 return fGpu->createBackendTexture({width, height}, backendFormat, renderable,
444 mipMapped, isProtected);
445}
446
447GrBackendTexture GrDirectContext::createBackendTexture(int width, int height,
448 SkColorType skColorType,
449 GrMipmapped mipMapped,
450 GrRenderable renderable,
451 GrProtected isProtected) {
452 if (this->abandoned()) {
453 return GrBackendTexture();
454 }
455
456 const GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
457
458 return this->createBackendTexture(width, height, format, mipMapped, renderable, isProtected);
459}
460
461static GrBackendTexture create_and_update_backend_texture(
462 GrDirectContext* dContext,
463 SkISize dimensions,
464 const GrBackendFormat& backendFormat,
465 GrMipmapped mipMapped,
466 GrRenderable renderable,
467 GrProtected isProtected,
468 sk_sp<GrRefCntedCallback> finishedCallback,
469 const GrGpu::BackendTextureData* data) {
470 GrGpu* gpu = dContext->priv().getGpu();
471
472 GrBackendTexture beTex = gpu->createBackendTexture(dimensions, backendFormat, renderable,
473 mipMapped, isProtected);
474 if (!beTex.isValid()) {
475 return {};
476 }
477
478 if (!dContext->priv().getGpu()->updateBackendTexture(beTex,
479 std::move(finishedCallback),
480 data)) {
481 dContext->deleteBackendTexture(beTex);
482 return {};
483 }
484 return beTex;
485}
486
Brian Salomonb5f880a2020-12-07 11:30:16 -0500487static bool update_texture_with_pixmaps(GrGpu* gpu,
488 const SkPixmap* srcData,
489 int numLevels,
490 const GrBackendTexture& backendTexture,
491 GrSurfaceOrigin textureOrigin,
492 sk_sp<GrRefCntedCallback> finishedCallback) {
493 std::unique_ptr<char[]> tempStorage;
494 SkAutoSTArray<15, SkPixmap> tempPixmaps;
495 if (textureOrigin == kBottomLeft_GrSurfaceOrigin) {
496 size_t size = 0;
497 for (int i = 0; i < numLevels; ++i) {
498 size += srcData[i].info().minRowBytes()*srcData[i].height();
499 }
500 tempStorage.reset(new char[size]);
501 tempPixmaps.reset(numLevels);
502 size = 0;
503 for (int i = 0; i < numLevels; ++i) {
504 size_t tempRB = srcData[i].info().minRowBytes();
505 tempPixmaps[i].reset(srcData[i].info(), tempStorage.get() + size, tempRB);
506 SkAssertResult(GrConvertPixels(tempPixmaps[i], srcData[i], /*flip*/ true));
507 size += tempRB*srcData[i].height();
508 }
509 srcData = tempPixmaps.get();
510 }
511 GrGpu::BackendTextureData data(srcData);
512 return gpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
513}
514
Adlai Holler98dd0042020-10-13 10:04:00 -0400515GrBackendTexture GrDirectContext::createBackendTexture(int width, int height,
516 const GrBackendFormat& backendFormat,
517 const SkColor4f& color,
518 GrMipmapped mipMapped,
519 GrRenderable renderable,
520 GrProtected isProtected,
521 GrGpuFinishedProc finishedProc,
522 GrGpuFinishedContext finishedContext) {
Brian Salomon694ff172020-11-04 16:54:28 -0500523 auto finishedCallback = GrRefCntedCallback::Make(finishedProc, finishedContext);
Adlai Holler98dd0042020-10-13 10:04:00 -0400524
525 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
526 if (this->abandoned()) {
527 return {};
528 }
529
530 GrGpu::BackendTextureData data(color);
531 return create_and_update_backend_texture(this, {width, height},
532 backendFormat, mipMapped, renderable, isProtected,
533 std::move(finishedCallback), &data);
534}
535
536GrBackendTexture GrDirectContext::createBackendTexture(int width, int height,
537 SkColorType skColorType,
538 const SkColor4f& color,
539 GrMipmapped mipMapped,
540 GrRenderable renderable,
541 GrProtected isProtected,
542 GrGpuFinishedProc finishedProc,
543 GrGpuFinishedContext finishedContext) {
Brian Salomon694ff172020-11-04 16:54:28 -0500544 auto finishedCallback = GrRefCntedCallback::Make(finishedProc, finishedContext);
Adlai Holler98dd0042020-10-13 10:04:00 -0400545
546 if (this->abandoned()) {
547 return {};
548 }
549
550 GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
551 if (!format.isValid()) {
552 return {};
553 }
554
555 GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
556 SkColor4f swizzledColor = this->caps()->getWriteSwizzle(format, grColorType).applyTo(color);
557
558 GrGpu::BackendTextureData data(swizzledColor);
559 return create_and_update_backend_texture(this, {width, height}, format,
560 mipMapped, renderable, isProtected,
561 std::move(finishedCallback), &data);
562}
563
564GrBackendTexture GrDirectContext::createBackendTexture(const SkPixmap srcData[],
565 int numProvidedLevels,
Brian Salomonb5f880a2020-12-07 11:30:16 -0500566 GrSurfaceOrigin textureOrigin,
Adlai Holler98dd0042020-10-13 10:04:00 -0400567 GrRenderable renderable,
568 GrProtected isProtected,
569 GrGpuFinishedProc finishedProc,
570 GrGpuFinishedContext finishedContext) {
571 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
572
Brian Salomon694ff172020-11-04 16:54:28 -0500573 auto finishedCallback = GrRefCntedCallback::Make(finishedProc, finishedContext);
Adlai Holler98dd0042020-10-13 10:04:00 -0400574
575 if (this->abandoned()) {
576 return {};
577 }
578
579 if (!srcData || numProvidedLevels <= 0) {
580 return {};
581 }
582
583 int baseWidth = srcData[0].width();
584 int baseHeight = srcData[0].height();
585 SkColorType colorType = srcData[0].colorType();
586
587 GrMipmapped mipMapped = GrMipmapped::kNo;
588 int numExpectedLevels = 1;
589 if (numProvidedLevels > 1) {
590 numExpectedLevels = SkMipmap::ComputeLevelCount(baseWidth, baseHeight) + 1;
591 mipMapped = GrMipmapped::kYes;
592 }
593
594 if (numProvidedLevels != numExpectedLevels) {
595 return {};
596 }
597
598 GrBackendFormat backendFormat = this->defaultBackendFormat(colorType, renderable);
Brian Salomonb5f880a2020-12-07 11:30:16 -0500599 GrBackendTexture beTex = this->createBackendTexture(srcData[0].width(),
600 srcData[0].height(),
601 backendFormat,
602 mipMapped,
603 renderable,
604 isProtected);
605 if (!beTex.isValid()) {
606 return {};
607 }
608 if (!update_texture_with_pixmaps(this->priv().getGpu(),
609 srcData,
610 numProvidedLevels,
611 beTex,
612 textureOrigin,
613 std::move(finishedCallback))) {
614 this->deleteBackendTexture(beTex);
615 return {};
616 }
617 return beTex;
Adlai Holler98dd0042020-10-13 10:04:00 -0400618}
619
Adlai Holler2e0c70d2020-10-13 08:21:37 -0400620bool GrDirectContext::updateBackendTexture(const GrBackendTexture& backendTexture,
621 const SkColor4f& color,
622 GrGpuFinishedProc finishedProc,
623 GrGpuFinishedContext finishedContext) {
Brian Salomon694ff172020-11-04 16:54:28 -0500624 auto finishedCallback = GrRefCntedCallback::Make(finishedProc, finishedContext);
Adlai Holler2e0c70d2020-10-13 08:21:37 -0400625
626 if (this->abandoned()) {
627 return false;
628 }
629
630 GrGpu::BackendTextureData data(color);
631 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
632}
633
634bool GrDirectContext::updateBackendTexture(const GrBackendTexture& backendTexture,
635 SkColorType skColorType,
636 const SkColor4f& color,
637 GrGpuFinishedProc finishedProc,
638 GrGpuFinishedContext finishedContext) {
Brian Salomon694ff172020-11-04 16:54:28 -0500639 auto finishedCallback = GrRefCntedCallback::Make(finishedProc, finishedContext);
Adlai Holler2e0c70d2020-10-13 08:21:37 -0400640
641 if (this->abandoned()) {
642 return false;
643 }
644
645 GrBackendFormat format = backendTexture.getBackendFormat();
646 GrColorType grColorType = SkColorTypeAndFormatToGrColorType(this->caps(), skColorType, format);
647
648 if (!this->caps()->areColorTypeAndFormatCompatible(grColorType, format)) {
649 return false;
650 }
651
652 GrSwizzle swizzle = this->caps()->getWriteSwizzle(format, grColorType);
653 GrGpu::BackendTextureData data(swizzle.applyTo(color));
654
655 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
656}
657
658bool GrDirectContext::updateBackendTexture(const GrBackendTexture& backendTexture,
659 const SkPixmap srcData[],
660 int numLevels,
Brian Salomonb5f880a2020-12-07 11:30:16 -0500661 GrSurfaceOrigin textureOrigin,
Adlai Holler2e0c70d2020-10-13 08:21:37 -0400662 GrGpuFinishedProc finishedProc,
663 GrGpuFinishedContext finishedContext) {
Brian Salomon694ff172020-11-04 16:54:28 -0500664 auto finishedCallback = GrRefCntedCallback::Make(finishedProc, finishedContext);
Adlai Holler2e0c70d2020-10-13 08:21:37 -0400665
666 if (this->abandoned()) {
667 return false;
668 }
669
670 if (!srcData || numLevels <= 0) {
671 return false;
672 }
673
674 int numExpectedLevels = 1;
675 if (backendTexture.hasMipmaps()) {
676 numExpectedLevels = SkMipmap::ComputeLevelCount(backendTexture.width(),
677 backendTexture.height()) + 1;
678 }
679 if (numLevels != numExpectedLevels) {
680 return false;
681 }
Brian Salomonb5f880a2020-12-07 11:30:16 -0500682 return update_texture_with_pixmaps(fGpu.get(),
683 srcData,
684 numLevels,
685 backendTexture,
686 textureOrigin,
687 std::move(finishedCallback));
Adlai Holler2e0c70d2020-10-13 08:21:37 -0400688}
689
Adlai Holler64e13832020-10-13 08:21:56 -0400690//////////////////////////////////////////////////////////////////////////////
691
692static GrBackendTexture create_and_update_compressed_backend_texture(
693 GrDirectContext* dContext,
694 SkISize dimensions,
695 const GrBackendFormat& backendFormat,
696 GrMipmapped mipMapped,
697 GrProtected isProtected,
698 sk_sp<GrRefCntedCallback> finishedCallback,
699 const GrGpu::BackendTextureData* data) {
700 GrGpu* gpu = dContext->priv().getGpu();
701
702 GrBackendTexture beTex = gpu->createCompressedBackendTexture(dimensions, backendFormat,
703 mipMapped, isProtected);
704 if (!beTex.isValid()) {
705 return {};
706 }
707
708 if (!dContext->priv().getGpu()->updateCompressedBackendTexture(
709 beTex, std::move(finishedCallback), data)) {
710 dContext->deleteBackendTexture(beTex);
711 return {};
712 }
713 return beTex;
714}
715
716GrBackendTexture GrDirectContext::createCompressedBackendTexture(int width, int height,
717 const GrBackendFormat& backendFormat,
718 const SkColor4f& color,
719 GrMipmapped mipMapped,
720 GrProtected isProtected,
721 GrGpuFinishedProc finishedProc,
722 GrGpuFinishedContext finishedContext) {
723 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomon694ff172020-11-04 16:54:28 -0500724 auto finishedCallback = GrRefCntedCallback::Make(finishedProc, finishedContext);
Adlai Holler64e13832020-10-13 08:21:56 -0400725
726 if (this->abandoned()) {
727 return {};
728 }
729
730 GrGpu::BackendTextureData data(color);
731 return create_and_update_compressed_backend_texture(this, {width, height},
732 backendFormat, mipMapped, isProtected,
733 std::move(finishedCallback), &data);
734}
735
736GrBackendTexture GrDirectContext::createCompressedBackendTexture(int width, int height,
737 SkImage::CompressionType compression,
738 const SkColor4f& color,
739 GrMipmapped mipMapped,
740 GrProtected isProtected,
741 GrGpuFinishedProc finishedProc,
742 GrGpuFinishedContext finishedContext) {
743 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
744 GrBackendFormat format = this->compressedBackendFormat(compression);
745 return this->createCompressedBackendTexture(width, height, format, color,
746 mipMapped, isProtected, finishedProc,
747 finishedContext);
748}
749
750GrBackendTexture GrDirectContext::createCompressedBackendTexture(int width, int height,
751 const GrBackendFormat& backendFormat,
752 const void* compressedData,
753 size_t dataSize,
754 GrMipmapped mipMapped,
755 GrProtected isProtected,
756 GrGpuFinishedProc finishedProc,
757 GrGpuFinishedContext finishedContext) {
758 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomon694ff172020-11-04 16:54:28 -0500759 auto finishedCallback = GrRefCntedCallback::Make(finishedProc, finishedContext);
Adlai Holler64e13832020-10-13 08:21:56 -0400760
761 if (this->abandoned()) {
762 return {};
763 }
764
765 GrGpu::BackendTextureData data(compressedData, dataSize);
766 return create_and_update_compressed_backend_texture(this, {width, height},
767 backendFormat, mipMapped, isProtected,
768 std::move(finishedCallback), &data);
769}
770
771GrBackendTexture GrDirectContext::createCompressedBackendTexture(int width, int height,
772 SkImage::CompressionType compression,
773 const void* data, size_t dataSize,
774 GrMipmapped mipMapped,
775 GrProtected isProtected,
776 GrGpuFinishedProc finishedProc,
777 GrGpuFinishedContext finishedContext) {
778 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
779 GrBackendFormat format = this->compressedBackendFormat(compression);
780 return this->createCompressedBackendTexture(width, height, format, data, dataSize, mipMapped,
781 isProtected, finishedProc, finishedContext);
782}
783
784bool GrDirectContext::updateCompressedBackendTexture(const GrBackendTexture& backendTexture,
785 const SkColor4f& color,
786 GrGpuFinishedProc finishedProc,
787 GrGpuFinishedContext finishedContext) {
Brian Salomon694ff172020-11-04 16:54:28 -0500788 auto finishedCallback = GrRefCntedCallback::Make(finishedProc, finishedContext);
Adlai Holler64e13832020-10-13 08:21:56 -0400789
790 if (this->abandoned()) {
791 return false;
792 }
793
794 GrGpu::BackendTextureData data(color);
795 return fGpu->updateCompressedBackendTexture(backendTexture, std::move(finishedCallback), &data);
796}
797
798bool GrDirectContext::updateCompressedBackendTexture(const GrBackendTexture& backendTexture,
799 const void* compressedData,
800 size_t dataSize,
801 GrGpuFinishedProc finishedProc,
802 GrGpuFinishedContext finishedContext) {
Brian Salomon694ff172020-11-04 16:54:28 -0500803 auto finishedCallback = GrRefCntedCallback::Make(finishedProc, finishedContext);
Adlai Holler64e13832020-10-13 08:21:56 -0400804
805 if (this->abandoned()) {
806 return false;
807 }
808
809 if (!compressedData) {
810 return false;
811 }
812
813 GrGpu::BackendTextureData data(compressedData, dataSize);
814
815 return fGpu->updateCompressedBackendTexture(backendTexture, std::move(finishedCallback), &data);
816}
817
Adlai Holler6d0745b2020-10-13 13:29:00 -0400818//////////////////////////////////////////////////////////////////////////////
819
820bool GrDirectContext::setBackendTextureState(const GrBackendTexture& backendTexture,
821 const GrBackendSurfaceMutableState& state,
822 GrBackendSurfaceMutableState* previousState,
823 GrGpuFinishedProc finishedProc,
824 GrGpuFinishedContext finishedContext) {
Brian Salomon694ff172020-11-04 16:54:28 -0500825 auto callback = GrRefCntedCallback::Make(finishedProc, finishedContext);
Adlai Holler6d0745b2020-10-13 13:29:00 -0400826
827 if (this->abandoned()) {
828 return false;
829 }
830
831 return fGpu->setBackendTextureState(backendTexture, state, previousState, std::move(callback));
832}
833
834
835bool GrDirectContext::setBackendRenderTargetState(const GrBackendRenderTarget& backendRenderTarget,
836 const GrBackendSurfaceMutableState& state,
837 GrBackendSurfaceMutableState* previousState,
838 GrGpuFinishedProc finishedProc,
839 GrGpuFinishedContext finishedContext) {
Brian Salomon694ff172020-11-04 16:54:28 -0500840 auto callback = GrRefCntedCallback::Make(finishedProc, finishedContext);
Adlai Holler6d0745b2020-10-13 13:29:00 -0400841
842 if (this->abandoned()) {
843 return false;
844 }
845
846 return fGpu->setBackendRenderTargetState(backendRenderTarget, state, previousState,
847 std::move(callback));
848}
849
850void GrDirectContext::deleteBackendTexture(GrBackendTexture backendTex) {
851 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
852 // For the Vulkan backend we still must destroy the backend texture when the context is
853 // abandoned.
854 if ((this->abandoned() && this->backend() != GrBackendApi::kVulkan) || !backendTex.isValid()) {
855 return;
856 }
857
858 fGpu->deleteBackendTexture(backendTex);
859}
860
861//////////////////////////////////////////////////////////////////////////////
862
863bool GrDirectContext::precompileShader(const SkData& key, const SkData& data) {
864 return fGpu->precompileShader(key, data);
865}
866
867#ifdef SK_ENABLE_DUMP_GPU
868#include "include/core/SkString.h"
869#include "src/utils/SkJSONWriter.h"
870SkString GrDirectContext::dump() const {
871 SkDynamicMemoryWStream stream;
872 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
873 writer.beginObject();
874
875 writer.appendString("backend", GrBackendApiToStr(this->backend()));
876
877 writer.appendName("caps");
878 this->caps()->dumpJSON(&writer);
879
880 writer.appendName("gpu");
881 this->fGpu->dumpJSON(&writer);
882
883 writer.appendName("context");
884 this->dumpJSON(&writer);
885
886 // Flush JSON to the memory stream
887 writer.endObject();
888 writer.flush();
889
890 // Null terminate the JSON data in the memory stream
891 stream.write8(0);
892
893 // Allocate a string big enough to hold all the data, then copy out of the stream
894 SkString result(stream.bytesWritten());
895 stream.copyToAndReset(result.writable_str());
896 return result;
897}
898#endif
899
John Rosascoa9b348f2019-11-08 13:18:15 -0800900#ifdef SK_GL
Robert Phillipsc7228c62020-07-14 12:57:39 -0400901
Robert Phillipsf4f80112020-07-13 16:13:31 -0400902/*************************************************************************************************/
903sk_sp<GrDirectContext> GrDirectContext::MakeGL(sk_sp<const GrGLInterface> glInterface) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500904 GrContextOptions defaultOptions;
Jim Van Verth03b8ab22020-02-24 11:36:15 -0500905 return MakeGL(std::move(glInterface), defaultOptions);
Robert Phillipsa3457b82018-03-08 11:30:12 -0500906}
907
Robert Phillipsf4f80112020-07-13 16:13:31 -0400908sk_sp<GrDirectContext> GrDirectContext::MakeGL(const GrContextOptions& options) {
Brian Salomonc1b9c102018-04-06 09:18:00 -0400909 return MakeGL(nullptr, options);
910}
911
Robert Phillipsf4f80112020-07-13 16:13:31 -0400912sk_sp<GrDirectContext> GrDirectContext::MakeGL() {
Brian Salomonc1b9c102018-04-06 09:18:00 -0400913 GrContextOptions defaultOptions;
914 return MakeGL(nullptr, defaultOptions);
915}
916
Brian Salomon24069eb2020-06-24 10:19:52 -0400917#if GR_TEST_UTILS
918GrGLFunction<GrGLGetErrorFn> make_get_error_with_random_oom(GrGLFunction<GrGLGetErrorFn> original) {
919 // A SkRandom and a GrGLFunction<GrGLGetErrorFn> are too big to be captured by a
920 // GrGLFunction<GrGLGetError> (surprise, surprise). So we make a context object and
921 // capture that by pointer. However, GrGLFunction doesn't support calling a destructor
922 // on the thing it captures. So we leak the context.
923 struct GetErrorContext {
924 SkRandom fRandom;
925 GrGLFunction<GrGLGetErrorFn> fGetError;
926 };
927
928 auto errorContext = new GetErrorContext;
929
930#if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS)
931 __lsan_ignore_object(errorContext);
932#endif
933
934 errorContext->fGetError = original;
935
936 return GrGLFunction<GrGLGetErrorFn>([errorContext]() {
937 GrGLenum error = errorContext->fGetError();
938 if (error == GR_GL_NO_ERROR && (errorContext->fRandom.nextU() % 300) == 0) {
939 error = GR_GL_OUT_OF_MEMORY;
940 }
941 return error;
942 });
943}
944#endif
945
Robert Phillipsf4f80112020-07-13 16:13:31 -0400946sk_sp<GrDirectContext> GrDirectContext::MakeGL(sk_sp<const GrGLInterface> glInterface,
947 const GrContextOptions& options) {
948 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kOpenGL, options));
Brian Salomon24069eb2020-06-24 10:19:52 -0400949#if GR_TEST_UTILS
950 if (options.fRandomGLOOM) {
951 auto copy = sk_make_sp<GrGLInterface>(*glInterface);
952 copy->fFunctions.fGetError =
953 make_get_error_with_random_oom(glInterface->fFunctions.fGetError);
954#if GR_GL_CHECK_ERROR
955 // Suppress logging GL errors since we'll be synthetically generating them.
956 copy->suppressErrorLogging();
957#endif
958 glInterface = std::move(copy);
959 }
960#endif
Robert Phillipsf4f80112020-07-13 16:13:31 -0400961 direct->fGpu = GrGLGpu::Make(std::move(glInterface), options, direct.get());
962 if (!direct->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500963 return nullptr;
964 }
Robert Phillipsf4f80112020-07-13 16:13:31 -0400965 return direct;
Robert Phillipsa3457b82018-03-08 11:30:12 -0500966}
John Rosascoa9b348f2019-11-08 13:18:15 -0800967#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -0500968
Robert Phillipsf4f80112020-07-13 16:13:31 -0400969/*************************************************************************************************/
Robert Phillipsf4f80112020-07-13 16:13:31 -0400970sk_sp<GrDirectContext> GrDirectContext::MakeMock(const GrMockOptions* mockOptions) {
971 GrContextOptions defaultOptions;
972 return MakeMock(mockOptions, defaultOptions);
973}
974
975sk_sp<GrDirectContext> GrDirectContext::MakeMock(const GrMockOptions* mockOptions,
976 const GrContextOptions& options) {
977 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kMock, options));
978
979 direct->fGpu = GrMockGpu::Make(mockOptions, options, direct.get());
980 if (!direct->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -0500981 return nullptr;
982 }
Chris Daltona378b452019-12-11 13:24:11 -0500983
Robert Phillipsf4f80112020-07-13 16:13:31 -0400984 return direct;
Robert Phillipsa3457b82018-03-08 11:30:12 -0500985}
986
Greg Danielb4d89562018-10-03 18:44:49 +0000987#ifdef SK_VULKAN
Robert Phillipsf4f80112020-07-13 16:13:31 -0400988/*************************************************************************************************/
Robert Phillipsf4f80112020-07-13 16:13:31 -0400989sk_sp<GrDirectContext> GrDirectContext::MakeVulkan(const GrVkBackendContext& backendContext) {
990 GrContextOptions defaultOptions;
991 return MakeVulkan(backendContext, defaultOptions);
992}
993
994sk_sp<GrDirectContext> GrDirectContext::MakeVulkan(const GrVkBackendContext& backendContext,
995 const GrContextOptions& options) {
996 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kVulkan, options));
997
998 direct->fGpu = GrVkGpu::Make(backendContext, options, direct.get());
999 if (!direct->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -05001000 return nullptr;
1001 }
1002
Robert Phillipsf4f80112020-07-13 16:13:31 -04001003 return direct;
Greg Danielb4d89562018-10-03 18:44:49 +00001004}
Robert Phillipsf4f80112020-07-13 16:13:31 -04001005#endif
Robert Phillipsa3457b82018-03-08 11:30:12 -05001006
1007#ifdef SK_METAL
Robert Phillipsf4f80112020-07-13 16:13:31 -04001008/*************************************************************************************************/
Jim Van Verth351c9b52020-11-12 15:21:11 -05001009sk_sp<GrDirectContext> GrDirectContext::MakeMetal(const GrMtlBackendContext& backendContext) {
Robert Phillipsa3457b82018-03-08 11:30:12 -05001010 GrContextOptions defaultOptions;
Jim Van Verth351c9b52020-11-12 15:21:11 -05001011 return MakeMetal(backendContext, defaultOptions);
Robert Phillipsa3457b82018-03-08 11:30:12 -05001012}
1013
Jim Van Verth351c9b52020-11-12 15:21:11 -05001014sk_sp<GrDirectContext> GrDirectContext::MakeMetal(const GrMtlBackendContext& backendContext,
1015 const GrContextOptions& options) {
Robert Phillipsf4f80112020-07-13 16:13:31 -04001016 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kMetal, options));
Robert Phillipsa3457b82018-03-08 11:30:12 -05001017
Jim Van Verth351c9b52020-11-12 15:21:11 -05001018 direct->fGpu = GrMtlTrampoline::MakeGpu(backendContext, options, direct.get());
Robert Phillipsf4f80112020-07-13 16:13:31 -04001019 if (!direct->init()) {
Robert Phillipsa3457b82018-03-08 11:30:12 -05001020 return nullptr;
1021 }
Timothy Liang4e85e802018-06-28 16:37:18 -04001022
Robert Phillipsf4f80112020-07-13 16:13:31 -04001023 return direct;
Robert Phillipsa3457b82018-03-08 11:30:12 -05001024}
Jim Van Verth351c9b52020-11-12 15:21:11 -05001025
1026// deprecated
1027sk_sp<GrDirectContext> GrDirectContext::MakeMetal(void* device, void* queue) {
1028 GrContextOptions defaultOptions;
1029 return MakeMetal(device, queue, defaultOptions);
1030}
1031
1032// deprecated
1033// remove include/gpu/mtl/GrMtlBackendContext.h, above, when removed
1034sk_sp<GrDirectContext> GrDirectContext::MakeMetal(void* device, void* queue,
1035 const GrContextOptions& options) {
1036 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kMetal, options));
1037 GrMtlBackendContext backendContext = {};
1038 backendContext.fDevice.reset(device);
1039 backendContext.fQueue.reset(queue);
1040
1041 return GrDirectContext::MakeMetal(backendContext, options);
1042}
Robert Phillipsa3457b82018-03-08 11:30:12 -05001043#endif
1044
Jim Van Verthb01e12b2020-02-18 14:34:38 -05001045#ifdef SK_DIRECT3D
Robert Phillipsf4f80112020-07-13 16:13:31 -04001046/*************************************************************************************************/
Robert Phillipsf4f80112020-07-13 16:13:31 -04001047sk_sp<GrDirectContext> GrDirectContext::MakeDirect3D(const GrD3DBackendContext& backendContext) {
1048 GrContextOptions defaultOptions;
1049 return MakeDirect3D(backendContext, defaultOptions);
1050}
1051
1052sk_sp<GrDirectContext> GrDirectContext::MakeDirect3D(const GrD3DBackendContext& backendContext,
1053 const GrContextOptions& options) {
1054 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kDirect3D, options));
1055
1056 direct->fGpu = GrD3DGpu::Make(backendContext, options, direct.get());
1057 if (!direct->init()) {
Jim Van Verthd2d4c5e2020-02-19 14:57:58 -05001058 return nullptr;
1059 }
Jim Van Verthb01e12b2020-02-18 14:34:38 -05001060
Robert Phillipsf4f80112020-07-13 16:13:31 -04001061 return direct;
Jim Van Verthb01e12b2020-02-18 14:34:38 -05001062}
1063#endif
1064
Stephen White985741a2019-07-18 11:43:45 -04001065#ifdef SK_DAWN
Robert Phillipsf4f80112020-07-13 16:13:31 -04001066/*************************************************************************************************/
Robert Phillipsf4f80112020-07-13 16:13:31 -04001067sk_sp<GrDirectContext> GrDirectContext::MakeDawn(const wgpu::Device& device) {
Stephen White985741a2019-07-18 11:43:45 -04001068 GrContextOptions defaultOptions;
1069 return MakeDawn(device, defaultOptions);
1070}
1071
Robert Phillipsf4f80112020-07-13 16:13:31 -04001072sk_sp<GrDirectContext> GrDirectContext::MakeDawn(const wgpu::Device& device,
1073 const GrContextOptions& options) {
1074 sk_sp<GrDirectContext> direct(new GrDirectContext(GrBackendApi::kDawn, options));
Stephen White985741a2019-07-18 11:43:45 -04001075
Robert Phillipsf4f80112020-07-13 16:13:31 -04001076 direct->fGpu = GrDawnGpu::Make(device, options, direct.get());
1077 if (!direct->init()) {
Stephen White985741a2019-07-18 11:43:45 -04001078 return nullptr;
1079 }
1080
Robert Phillipsf4f80112020-07-13 16:13:31 -04001081 return direct;
Stephen White985741a2019-07-18 11:43:45 -04001082}
Robert Phillipsf4f80112020-07-13 16:13:31 -04001083
Stephen White985741a2019-07-18 11:43:45 -04001084#endif