blob: 956a2e5e570851dddb623f7c4480a21f008288b2 [file] [log] [blame]
bsalomon@google.com27847de2011-02-22 20:59:41 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 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.
bsalomon@google.com27847de2011-02-22 20:59:41 +00006 */
7
Brian Salomon9241a6d2019-10-03 13:26:54 -04008#include "include/gpu/GrContext.h"
9
Robert Phillips4d5594d2020-02-21 14:24:40 -050010#include "include/core/SkDeferredDisplayList.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkTraceMemoryDump.h"
12#include "include/gpu/GrBackendSemaphore.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/private/SkImageInfoPriv.h"
Brian Salomon85c3d682019-11-04 15:04:54 -050014#include "src/core/SkMipMap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/core/SkTaskGroup.h"
Brian Salomon9241a6d2019-10-03 13:26:54 -040016#include "src/gpu/GrClientMappedBufferManager.h"
Greg Daniel7fd7a8a2019-10-10 16:10:31 -040017#include "src/gpu/GrContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrDrawingManager.h"
19#include "src/gpu/GrGpu.h"
20#include "src/gpu/GrMemoryPool.h"
21#include "src/gpu/GrPathRendererChain.h"
22#include "src/gpu/GrProxyProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040023#include "src/gpu/GrRenderTargetProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrResourceCache.h"
25#include "src/gpu/GrResourceProvider.h"
26#include "src/gpu/GrSemaphore.h"
Brian Osman5e7fbfd2019-05-03 13:13:35 -040027#include "src/gpu/GrShaderUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/GrSoftwarePathRenderer.h"
29#include "src/gpu/GrTracing.h"
30#include "src/gpu/SkGr.h"
31#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
32#include "src/gpu/effects/GrSkSLFP.h"
Robert Phillips41bd97d2020-04-07 14:19:37 -040033#include "src/gpu/text/GrStrikeCache.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/gpu/text/GrTextBlobCache.h"
35#include "src/gpu/text/GrTextContext.h"
Greg Daniel7fd7a8a2019-10-10 16:10:31 -040036#include "src/image/SkImage_GpuBase.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "src/image/SkSurface_Gpu.h"
Mike Klein0ec1c572018-12-04 11:52:51 -050038#include <atomic>
Greg Danielb76a72a2017-07-13 15:07:54 -040039
Robert Phillipse78b7252017-04-06 07:59:41 -040040#define ASSERT_OWNED_PROXY(P) \
Brian Salomonfd98c2c2018-07-31 17:25:29 -040041 SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == this)
Robert Phillips7ee385e2017-03-30 08:02:11 -040042
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000043#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
Adlai Holler33dbd652020-06-01 12:35:42 -040044#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner())
Robert Phillipsa9162df2019-02-11 14:12:03 -050045#define RETURN_IF_ABANDONED if (this->abandoned()) { return; }
46#define RETURN_FALSE_IF_ABANDONED if (this->abandoned()) { return false; }
47#define RETURN_NULL_IF_ABANDONED if (this->abandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000048
robertphillipsea461502015-05-26 11:38:03 -070049////////////////////////////////////////////////////////////////////////////////
50
Robert Phillipsa41c6852019-02-07 10:44:10 -050051GrContext::GrContext(GrBackendApi backend, const GrContextOptions& options, int32_t contextID)
52 : INHERITED(backend, options, contextID) {
halcanary96fcdcc2015-08-27 07:41:13 -070053 fResourceCache = nullptr;
54 fResourceProvider = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000055}
56
Robert Phillips292a6b22019-02-14 14:49:02 -050057GrContext::~GrContext() {
58 ASSERT_SINGLE_OWNER
59
Robert Phillips6a6de562019-02-15 15:19:15 -050060 if (this->drawingManager()) {
61 this->drawingManager()->cleanup();
Robert Phillips292a6b22019-02-14 14:49:02 -050062 }
63 delete fResourceProvider;
64 delete fResourceCache;
Robert Phillips292a6b22019-02-14 14:49:02 -050065}
66
Brian Osman7b1678a2019-12-16 09:17:25 -050067bool GrContext::init(sk_sp<const GrCaps> caps) {
Greg Danielb76a72a2017-07-13 15:07:54 -040068 ASSERT_SINGLE_OWNER
Robert Phillipsa41c6852019-02-07 10:44:10 -050069 SkASSERT(this->proxyProvider());
Robert Phillips88260b52018-01-19 12:56:09 -050070
Brian Osman7b1678a2019-12-16 09:17:25 -050071 if (!INHERITED::init(std::move(caps))) {
Robert Phillipsbb606772019-02-04 17:50:57 -050072 return false;
73 }
74
75 SkASSERT(this->caps());
Robert Phillips2184fb72019-02-21 16:11:41 -050076 SkASSERT(this->getTextBlobCache());
Robert Phillipsbb606772019-02-04 17:50:57 -050077
Robert Phillips88260b52018-01-19 12:56:09 -050078 if (fGpu) {
Robert Phillips4d932d12020-04-09 08:58:52 -040079 fStrikeCache.reset(new GrStrikeCache{});
Robert Phillipsa41c6852019-02-07 10:44:10 -050080 fResourceCache = new GrResourceCache(this->caps(), this->singleOwner(), this->contextID());
Robert Phillips12c46292019-04-23 07:36:17 -040081 fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, this->singleOwner());
Mike Kleinf46d5ca2019-12-11 10:45:01 -050082 fMappedBufferManager = std::make_unique<GrClientMappedBufferManager>(this->contextID());
Robert Phillips88260b52018-01-19 12:56:09 -050083 }
84
Robert Phillips88260b52018-01-19 12:56:09 -050085 if (fResourceCache) {
Robert Phillipsa41c6852019-02-07 10:44:10 -050086 fResourceCache->setProxyProvider(this->proxyProvider());
Robert Phillips88260b52018-01-19 12:56:09 -050087 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050088
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000089 fDidTestPMConversions = false;
90
Robert Phillipsfde6fa02018-03-02 08:53:14 -050091 // DDL TODO: we need to think through how the task group & persistent cache
92 // get passed on to/shared between all the DDLRecorders created with this context.
Robert Phillipsc1541ae2019-02-04 12:05:37 -050093 if (this->options().fExecutor) {
Mike Kleinf46d5ca2019-12-11 10:45:01 -050094 fTaskGroup = std::make_unique<SkTaskGroup>(*this->options().fExecutor);
Brian Osman51279982017-08-23 10:12:00 -040095 }
96
Robert Phillipsc1541ae2019-02-04 12:05:37 -050097 fPersistentCache = this->options().fPersistentCache;
Brian Osman5e7fbfd2019-05-03 13:13:35 -040098 fShaderErrorHandler = this->options().fShaderErrorHandler;
99 if (!fShaderErrorHandler) {
100 fShaderErrorHandler = GrShaderUtils::DefaultShaderErrorHandler();
101 }
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400102
Brian Salomon91a3e522017-06-23 10:58:19 -0400103 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000104}
105
Robert Phillips4217ea72019-01-30 13:08:28 -0500106sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
107 return fThreadSafeProxy;
108}
109
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400110//////////////////////////////////////////////////////////////////////////////
111
bsalomon2354f842014-07-28 13:48:36 -0700112void GrContext::abandonContext() {
Greg Daniel6e35a002020-04-01 13:29:59 -0400113 if (INHERITED::abandoned()) {
Robert Phillipsa9162df2019-02-11 14:12:03 -0500114 return;
115 }
joshualitt1de610a2016-01-06 08:26:09 -0800116
Robert Phillipsa9162df2019-02-11 14:12:03 -0500117 INHERITED::abandonContext();
118
Robert Phillips4d932d12020-04-09 08:58:52 -0400119 fStrikeCache->freeAll();
120
Brian Salomon9241a6d2019-10-03 13:26:54 -0400121 fMappedBufferManager->abandon();
122
bsalomond309e7a2015-04-30 14:18:54 -0700123 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800124
Robert Phillipsa9162df2019-02-11 14:12:03 -0500125 // Need to cleanup the drawing manager first so all the render targets
robertphillips0dfa62c2015-11-16 06:23:31 -0800126 // will be released/forgotten before they too are abandoned.
Robert Phillips6a6de562019-02-15 15:19:15 -0500127 this->drawingManager()->cleanup();
robertphillips0dfa62c2015-11-16 06:23:31 -0800128
bsalomon@google.com205d4602011-04-25 12:43:45 +0000129 // abandon first to so destructors
130 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800131 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700132
bsalomon6e2aad42016-04-01 11:54:31 -0700133 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400134
135 fMappedBufferManager.reset();
Khushalc421ca12018-06-26 14:38:34 -0700136}
137
bsalomon6e2aad42016-04-01 11:54:31 -0700138void GrContext::releaseResourcesAndAbandonContext() {
Greg Daniel6e35a002020-04-01 13:29:59 -0400139 if (INHERITED::abandoned()) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500140 return;
141 }
Robert Phillipsa9162df2019-02-11 14:12:03 -0500142
143 INHERITED::abandonContext();
144
Brian Salomon9241a6d2019-10-03 13:26:54 -0400145 fMappedBufferManager.reset();
146
bsalomon6e2aad42016-04-01 11:54:31 -0700147 fResourceProvider->abandon();
148
Robert Phillipsa9162df2019-02-11 14:12:03 -0500149 // Need to cleanup the drawing manager first so all the render targets
bsalomon6e2aad42016-04-01 11:54:31 -0700150 // will be released/forgotten before they too are abandoned.
Robert Phillips6a6de562019-02-15 15:19:15 -0500151 this->drawingManager()->cleanup();
bsalomon6e2aad42016-04-01 11:54:31 -0700152
153 // Release all resources in the backend 3D API.
154 fResourceCache->releaseAll();
155
156 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000157}
158
Greg Daniel6e35a002020-04-01 13:29:59 -0400159bool GrContext::abandoned() {
160 if (INHERITED::abandoned()) {
161 return true;
162 }
163
164 if (fGpu && fGpu->isDeviceLost()) {
165 this->abandonContext();
166 return true;
167 }
168 return false;
169}
170
Brian Salomon1f05d452019-02-08 12:33:08 -0500171void GrContext::resetGLTextureBindings() {
172 if (this->abandoned() || this->backend() != GrBackendApi::kOpenGL) {
173 return;
174 }
175 fGpu->resetTextureBindings();
176}
177
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000178void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800179 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000180 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000181}
182
183void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800184 ASSERT_SINGLE_OWNER
185
Robert Phillips2184fb72019-02-21 16:11:41 -0500186 // TODO: the glyph cache doesn't hold any GpuResources so this call should not be needed here.
187 // Some slack in the GrTextBlob's implementation requires it though. That could be fixed.
Robert Phillips4d932d12020-04-09 08:58:52 -0400188 fStrikeCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700189
Robert Phillips6a6de562019-02-15 15:19:15 -0500190 this->drawingManager()->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700191
192 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000193}
194
Robert Phillips6eba0632018-03-28 12:25:42 -0400195void GrContext::purgeUnlockedResources(bool scratchResourcesOnly) {
196 ASSERT_SINGLE_OWNER
Robert Phillipsddc21482019-10-16 14:30:09 -0400197
198 if (this->abandoned()) {
199 return;
200 }
201
Robert Phillips6eba0632018-03-28 12:25:42 -0400202 fResourceCache->purgeUnlockedResources(scratchResourcesOnly);
203 fResourceCache->purgeAsNeeded();
Robert Phillips2184fb72019-02-21 16:11:41 -0500204
205 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
206 // place to purge stale blobs
207 this->getTextBlobCache()->purgeStaleBlobs();
Robert Phillips6eba0632018-03-28 12:25:42 -0400208}
209
Jim Van Verth76d917c2017-12-13 09:26:37 -0500210void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Yuqian Li40aa85f2019-07-02 13:45:00 -0700211 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
212
Brian Salomon5e150852017-03-22 14:53:13 -0400213 ASSERT_SINGLE_OWNER
Chris Dalton6c3879d2018-11-01 11:13:19 -0600214
Brian Salomon9241a6d2019-10-03 13:26:54 -0400215 if (this->abandoned()) {
216 return;
217 }
218
219 fMappedBufferManager->process();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600220 auto purgeTime = GrStdSteadyClock::now() - msNotUsed;
221
Jim Van Verth76d917c2017-12-13 09:26:37 -0500222 fResourceCache->purgeAsNeeded();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600223 fResourceCache->purgeResourcesNotUsedSince(purgeTime);
224
Robert Phillips6a6de562019-02-15 15:19:15 -0500225 if (auto ccpr = this->drawingManager()->getCoverageCountingPathRenderer()) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500226 ccpr->purgeCacheEntriesOlderThan(this->proxyProvider(), purgeTime);
Chris Dalton6c3879d2018-11-01 11:13:19 -0600227 }
Jim Van Verth76d917c2017-12-13 09:26:37 -0500228
Robert Phillips2184fb72019-02-21 16:11:41 -0500229 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
230 // place to purge stale blobs
231 this->getTextBlobCache()->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400232}
233
Derek Sollenberger5480a182017-05-25 16:43:59 -0400234void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
235 ASSERT_SINGLE_OWNER
Robert Phillipsddc21482019-10-16 14:30:09 -0400236
237 if (this->abandoned()) {
238 return;
239 }
240
Derek Sollenberger5480a182017-05-25 16:43:59 -0400241 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
242}
243
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000244void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800245 ASSERT_SINGLE_OWNER
246
bsalomon71cb0c22014-11-14 12:10:14 -0800247 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800248 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800249 }
250 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800251 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800252 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000253}
254
Derek Sollenbergeree479142017-05-24 11:41:33 -0400255size_t GrContext::getResourceCachePurgeableBytes() const {
256 ASSERT_SINGLE_OWNER
257 return fResourceCache->getPurgeableBytes();
258}
259
Greg Daniel8b666172019-10-09 12:38:22 -0400260size_t GrContext::ComputeImageSize(sk_sp<SkImage> image, GrMipMapped mipMapped, bool useNextPow2) {
261 if (!image->isTextureBacked()) {
262 return 0;
263 }
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400264 SkImage_GpuBase* gpuImage = static_cast<SkImage_GpuBase*>(as_IB(image.get()));
265 GrTextureProxy* proxy = gpuImage->peekProxy();
266 if (!proxy) {
267 return 0;
268 }
269
270 const GrCaps& caps = *gpuImage->context()->priv().caps();
Greg Daniel8b666172019-10-09 12:38:22 -0400271 int colorSamplesPerPixel = 1;
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400272 return GrSurface::ComputeSize(caps, proxy->backendFormat(), image->dimensions(),
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400273 colorSamplesPerPixel, mipMapped, useNextPow2);
Greg Daniel8b666172019-10-09 12:38:22 -0400274}
275
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000276////////////////////////////////////////////////////////////////////////////////
277
Robert Phillipsbb606772019-02-04 17:50:57 -0500278int GrContext::maxTextureSize() const { return this->caps()->maxTextureSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400279
Robert Phillipsbb606772019-02-04 17:50:57 -0500280int GrContext::maxRenderTargetSize() const { return this->caps()->maxRenderTargetSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400281
Brian Salomonbdecacf2018-02-02 20:32:49 -0500282bool GrContext::colorTypeSupportedAsImage(SkColorType colorType) const {
Greg Daniel7bfc9132019-08-14 14:23:53 -0400283 GrBackendFormat format =
284 this->caps()->getDefaultBackendFormat(SkColorTypeToGrColorType(colorType),
285 GrRenderable::kNo);
286 return format.isValid();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500287}
288
289int GrContext::maxSurfaceSampleCountForColorType(SkColorType colorType) const {
Greg Danieleadfac92019-08-02 09:03:53 -0400290 GrBackendFormat format =
291 this->caps()->getDefaultBackendFormat(SkColorTypeToGrColorType(colorType),
292 GrRenderable::kYes);
293 return this->caps()->maxRenderTargetSampleCount(format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500294}
295
296////////////////////////////////////////////////////////////////////////////////
297
Greg Daniel06be0792019-04-22 15:53:23 -0400298bool GrContext::wait(int numSemaphores, const GrBackendSemaphore waitSemaphores[]) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400299 if (!fGpu || fGpu->caps()->semaphoreSupport()) {
Greg Daniel06be0792019-04-22 15:53:23 -0400300 return false;
301 }
302 for (int i = 0; i < numSemaphores; ++i) {
Greg Daniel301015c2019-11-18 14:06:46 -0500303 std::unique_ptr<GrSemaphore> sema = fResourceProvider->wrapBackendSemaphore(
Greg Daniel06be0792019-04-22 15:53:23 -0400304 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
305 kAdopt_GrWrapOwnership);
Greg Daniel301015c2019-11-18 14:06:46 -0500306 fGpu->waitSemaphore(sema.get());
Greg Daniel06be0792019-04-22 15:53:23 -0400307 }
308 return true;
309}
310
311////////////////////////////////////////////////////////////////////////////////
312
Greg Daniel797efca2019-05-09 14:04:20 -0400313GrSemaphoresSubmitted GrContext::flush(const GrFlushInfo& info,
314 const GrPrepareForExternalIORequests& externalRequests) {
Greg Daniel51316782017-08-02 15:10:09 +0000315 ASSERT_SINGLE_OWNER
Robert Phillipsa9162df2019-02-11 14:12:03 -0500316 if (this->abandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400317 if (info.fFinishedProc) {
318 info.fFinishedProc(info.fFinishedContext);
319 }
320 if (info.fSubmittedProc) {
321 info.fSubmittedProc(info.fSubmittedContext, false);
322 }
Robert Phillipsa9162df2019-02-11 14:12:03 -0500323 return GrSemaphoresSubmitted::kNo;
324 }
Greg Daniel51316782017-08-02 15:10:09 +0000325
Greg Daniel04283f32020-05-20 13:16:00 -0400326 bool flushed = this->drawingManager()->flush(
327 nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, info, externalRequests);
Greg Danielfe159622020-04-10 17:43:51 +0000328
Greg Daniel04283f32020-05-20 13:16:00 -0400329 if (!flushed || (!this->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
Greg Danielfe159622020-04-10 17:43:51 +0000330 return GrSemaphoresSubmitted::kNo;
331 }
332 return GrSemaphoresSubmitted::kYes;
Greg Daniel51316782017-08-02 15:10:09 +0000333}
334
Greg Daniel04283f32020-05-20 13:16:00 -0400335bool GrContext::submit(bool syncCpu) {
336 ASSERT_SINGLE_OWNER
337 if (this->abandoned()) {
338 return false;
339 }
340
341 if (!fGpu) {
342 return false;
343 }
344
345 return fGpu->submitToGpu(syncCpu);
Greg Danielda50cb82020-05-13 14:07:40 -0400346}
347
Greg Daniela870b462019-01-08 15:49:46 -0500348////////////////////////////////////////////////////////////////////////////////
349
Brian Salomonb0d8b762019-05-06 16:58:22 -0400350void GrContext::checkAsyncWorkCompletion() {
351 if (fGpu) {
352 fGpu->checkFinishProcs();
353 }
354}
355
356////////////////////////////////////////////////////////////////////////////////
357
Greg Daniela870b462019-01-08 15:49:46 -0500358void GrContext::storeVkPipelineCacheData() {
359 if (fGpu) {
360 fGpu->storeVkPipelineCacheData();
361 }
362}
363
364////////////////////////////////////////////////////////////////////////////////
365
Khushal3e7548c2018-05-23 15:45:01 -0700366bool GrContext::supportsDistanceFieldText() const {
Robert Phillipsbb606772019-02-04 17:50:57 -0500367 return this->caps()->shaderCaps()->supportsDistanceFieldText();
Khushal3e7548c2018-05-23 15:45:01 -0700368}
369
bsalomon37f9a262015-02-02 13:00:10 -0800370//////////////////////////////////////////////////////////////////////////////
371
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500372void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800373 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500374 if (maxResources) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400375 *maxResources = -1;
bsalomon37f9a262015-02-02 13:00:10 -0800376 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500377 if (maxResourceBytes) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400378 *maxResourceBytes = this->getResourceCacheLimit();
bsalomon37f9a262015-02-02 13:00:10 -0800379 }
380}
381
Robert Phillipscf39f372019-09-03 10:29:20 -0400382size_t GrContext::getResourceCacheLimit() const {
joshualitt1de610a2016-01-06 08:26:09 -0800383 ASSERT_SINGLE_OWNER
Robert Phillipscf39f372019-09-03 10:29:20 -0400384 return fResourceCache->getMaxResourceBytes();
385}
386
387void GrContext::setResourceCacheLimits(int unused, size_t maxResourceBytes) {
388 ASSERT_SINGLE_OWNER
389 this->setResourceCacheLimit(maxResourceBytes);
390}
391
392void GrContext::setResourceCacheLimit(size_t maxResourceBytes) {
393 ASSERT_SINGLE_OWNER
394 fResourceCache->setLimit(maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800395}
396
ericrk0a5fa482015-09-15 14:16:10 -0700397//////////////////////////////////////////////////////////////////////////////
ericrk0a5fa482015-09-15 14:16:10 -0700398void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800399 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700400 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
Khushal71652e22018-10-29 13:05:36 -0700401 traceMemoryDump->dumpNumericValue("skia/gr_text_blob_cache", "size", "bytes",
Robert Phillips2184fb72019-02-21 16:11:41 -0500402 this->getTextBlobCache()->usedBytes());
ericrk0a5fa482015-09-15 14:16:10 -0700403}
Brian Osman71a18892017-08-10 10:23:25 -0400404
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400405//////////////////////////////////////////////////////////////////////////////
406GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400407 const GrBackendFormat& backendFormat,
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400408 GrMipMapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400409 GrRenderable renderable,
410 GrProtected isProtected) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400411 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400412 if (!this->asDirectContext()) {
413 return GrBackendTexture();
414 }
415
416 if (this->abandoned()) {
417 return GrBackendTexture();
418 }
419
Robert Phillips4277f012020-01-21 14:28:34 -0500420 return fGpu->createBackendTexture({width, height}, backendFormat, renderable,
Greg Daniel16032b32020-05-06 15:31:10 -0400421 mipMapped, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400422}
423
424GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400425 SkColorType skColorType,
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400426 GrMipMapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400427 GrRenderable renderable,
428 GrProtected isProtected) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400429 if (!this->asDirectContext()) {
430 return GrBackendTexture();
431 }
432
433 if (this->abandoned()) {
434 return GrBackendTexture();
435 }
436
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400437 const GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400438
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400439 return this->createBackendTexture(width, height, format, mipMapped, renderable, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400440}
441
Robert Phillips02dc0302019-07-02 17:58:27 -0400442GrBackendTexture GrContext::createBackendTexture(const SkSurfaceCharacterization& c) {
Robert Phillips02dc0302019-07-02 17:58:27 -0400443 if (!this->asDirectContext() || !c.isValid()) {
444 return GrBackendTexture();
445 }
446
447 if (this->abandoned()) {
448 return GrBackendTexture();
449 }
450
451 if (c.usesGLFBO0()) {
452 // If we are making the surface we will never use FBO0.
453 return GrBackendTexture();
454 }
455
456 if (c.vulkanSecondaryCBCompatible()) {
457 return {};
458 }
459
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400460 const GrBackendFormat format = this->defaultBackendFormat(c.colorType(), GrRenderable::kYes);
Robert Phillips02dc0302019-07-02 17:58:27 -0400461 if (!format.isValid()) {
462 return GrBackendTexture();
463 }
464
Robert Phillips02dc0302019-07-02 17:58:27 -0400465 GrBackendTexture result = this->createBackendTexture(c.width(), c.height(), format,
466 GrMipMapped(c.isMipMapped()),
467 GrRenderable::kYes,
Robert Phillips3cd54322019-07-10 09:28:59 -0400468 c.isProtected());
Robert Phillips02dc0302019-07-02 17:58:27 -0400469 SkASSERT(c.isCompatible(result));
470 return result;
471}
472
473GrBackendTexture GrContext::createBackendTexture(const SkSurfaceCharacterization& c,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400474 const SkColor4f& color,
475 GrGpuFinishedProc finishedProc,
476 GrGpuFinishedContext finishedContext) {
Robert Phillips02dc0302019-07-02 17:58:27 -0400477 if (!this->asDirectContext() || !c.isValid()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400478 finishedProc(finishedContext);
479 return {};
Robert Phillips02dc0302019-07-02 17:58:27 -0400480 }
481
482 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400483 finishedProc(finishedContext);
484 return {};
Robert Phillips02dc0302019-07-02 17:58:27 -0400485 }
486
487 if (c.usesGLFBO0()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400488 finishedProc(finishedContext);
Robert Phillips02dc0302019-07-02 17:58:27 -0400489 // If we are making the surface we will never use FBO0.
Greg Danielc1ad77c2020-05-06 11:40:03 -0400490 return {};
Robert Phillips02dc0302019-07-02 17:58:27 -0400491 }
492
493 if (c.vulkanSecondaryCBCompatible()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400494 finishedProc(finishedContext);
Robert Phillips02dc0302019-07-02 17:58:27 -0400495 return {};
496 }
497
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400498 const GrBackendFormat format = this->defaultBackendFormat(c.colorType(), GrRenderable::kYes);
Robert Phillips02dc0302019-07-02 17:58:27 -0400499 if (!format.isValid()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400500 finishedProc(finishedContext);
501 return {};
Robert Phillips02dc0302019-07-02 17:58:27 -0400502 }
503
Robert Phillips02dc0302019-07-02 17:58:27 -0400504 GrBackendTexture result = this->createBackendTexture(c.width(), c.height(), format, color,
505 GrMipMapped(c.isMipMapped()),
506 GrRenderable::kYes,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400507 c.isProtected(), finishedProc,
508 finishedContext);
Robert Phillips02dc0302019-07-02 17:58:27 -0400509 SkASSERT(c.isCompatible(result));
510 return result;
511}
512
Greg Daniel16032b32020-05-06 15:31:10 -0400513static GrBackendTexture create_and_update_backend_texture(GrContext* context,
514 SkISize dimensions,
515 const GrBackendFormat& backendFormat,
516 GrMipMapped mipMapped,
517 GrRenderable renderable,
518 GrProtected isProtected,
519 GrGpuFinishedProc finishedProc,
520 GrGpuFinishedContext finishedContext,
521 const GrGpu::BackendTextureData* data) {
522 GrGpu* gpu = context->priv().getGpu();
523
524 GrBackendTexture beTex = gpu->createBackendTexture(dimensions, backendFormat, renderable,
525 mipMapped, isProtected);
526 if (!beTex.isValid()) {
527 return {};
528 }
529
530 if (!context->priv().getGpu()->updateBackendTexture(beTex, finishedProc, finishedContext,
531 data)) {
532 context->deleteBackendTexture(beTex);
533 return {};
534 }
535 return beTex;
536}
537
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400538GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400539 const GrBackendFormat& backendFormat,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400540 const SkColor4f& color,
541 GrMipMapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400542 GrRenderable renderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400543 GrProtected isProtected,
544 GrGpuFinishedProc finishedProc,
545 GrGpuFinishedContext finishedContext) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400546 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400547 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400548 finishedProc(finishedContext);
549 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400550 }
551
552 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400553 finishedProc(finishedContext);
554 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400555 }
556
Brian Salomon85c3d682019-11-04 15:04:54 -0500557 GrGpu::BackendTextureData data(color);
Greg Daniel16032b32020-05-06 15:31:10 -0400558 return create_and_update_backend_texture(this, {width, height}, backendFormat, mipMapped,
559 renderable, isProtected, finishedProc, finishedContext,
560 &data);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400561}
562
563GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400564 SkColorType skColorType,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400565 const SkColor4f& color,
566 GrMipMapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400567 GrRenderable renderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400568 GrProtected isProtected,
569 GrGpuFinishedProc finishedProc,
570 GrGpuFinishedContext finishedContext) {
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400571 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400572 finishedProc(finishedContext);
573 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400574 }
575
576 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400577 finishedProc(finishedContext);
578 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400579 }
580
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400581 GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400582 if (!format.isValid()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400583 finishedProc(finishedContext);
584 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400585 }
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400586
587 GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
Brian Salomon982f5462020-03-30 12:52:33 -0400588 SkColor4f swizzledColor = this->caps()->getWriteSwizzle(format, grColorType).applyTo(color);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400589
Brian Salomonb450f3b2019-07-09 09:36:51 -0400590 return this->createBackendTexture(width, height, format, swizzledColor, mipMapped, renderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400591 isProtected, finishedProc, finishedContext);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400592}
593
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500594GrBackendTexture GrContext::createBackendTexture(const SkPixmap srcData[], int numProvidedLevels,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400595 GrRenderable renderable, GrProtected isProtected,
596 GrGpuFinishedProc finishedProc,
597 GrGpuFinishedContext finishedContext) {
Robert Phillips66944402019-09-30 13:21:25 -0400598 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
599
600 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400601 finishedProc(finishedContext);
Robert Phillips66944402019-09-30 13:21:25 -0400602 return {};
603 }
604
605 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400606 finishedProc(finishedContext);
Robert Phillips66944402019-09-30 13:21:25 -0400607 return {};
608 }
609
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500610 if (!srcData || numProvidedLevels <= 0) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400611 finishedProc(finishedContext);
Robert Phillips66944402019-09-30 13:21:25 -0400612 return {};
613 }
614
615 int baseWidth = srcData[0].width();
616 int baseHeight = srcData[0].height();
617 SkColorType colorType = srcData[0].colorType();
618
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500619 GrMipMapped mipMapped = GrMipMapped::kNo;
620 int numExpectedLevels = 1;
621 if (numProvidedLevels > 1) {
622 numExpectedLevels = SkMipMap::ComputeLevelCount(baseWidth, baseHeight) + 1;
623 mipMapped = GrMipMapped::kYes;
624 }
625
626 if (numProvidedLevels != numExpectedLevels) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400627 finishedProc(finishedContext);
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500628 return {};
629 }
630
Robert Phillips66944402019-09-30 13:21:25 -0400631 GrBackendFormat backendFormat = this->defaultBackendFormat(colorType, renderable);
632
Brian Salomon85c3d682019-11-04 15:04:54 -0500633 GrGpu::BackendTextureData data(srcData);
Greg Daniel16032b32020-05-06 15:31:10 -0400634 return create_and_update_backend_texture(this, {baseWidth, baseHeight}, backendFormat,
635 mipMapped, renderable, isProtected, finishedProc,
636 finishedContext, &data);
Robert Phillips66944402019-09-30 13:21:25 -0400637}
638
Greg Danielb2365d82020-05-13 15:32:04 -0400639bool GrContext::updateBackendTexture(const GrBackendTexture& backendTexture,
640 const SkColor4f& color,
641 GrGpuFinishedProc finishedProc,
642 GrGpuFinishedContext finishedContext) {
643 if (!this->asDirectContext()) {
644 finishedProc(finishedContext);
645 return false;
646 }
647
648 if (this->abandoned()) {
649 finishedProc(finishedContext);
650 return false;
651 }
652
653 GrGpu::BackendTextureData data(color);
654 return fGpu->updateBackendTexture(backendTexture, finishedProc, finishedContext, &data);
655}
656
657bool GrContext::updateBackendTexture(const GrBackendTexture& backendTexture,
658 const SkPixmap srcData[],
659 int numLevels,
660 GrGpuFinishedProc finishedProc,
661 GrGpuFinishedContext finishedContext) {
662 if (!this->asDirectContext()) {
663 finishedProc(finishedContext);
664 return false;
665 }
666
667 if (this->abandoned()) {
668 finishedProc(finishedContext);
669 return false;
670 }
671
672 if (!srcData || numLevels <= 0) {
673 finishedProc(finishedContext);
674 return false;
675 }
676
677 int numExpectedLevels = 1;
678 if (backendTexture.hasMipMaps()) {
679 numExpectedLevels = SkMipMap::ComputeLevelCount(backendTexture.width(),
680 backendTexture.height()) + 1;
681 }
682 if (numLevels != numExpectedLevels) {
683 finishedProc(finishedContext);
684 return false;
685 }
686
687 GrGpu::BackendTextureData data(srcData);
688 return fGpu->updateBackendTexture(backendTexture, finishedProc, finishedContext, &data);
689}
690
Robert Phillipsb915c942019-12-17 14:44:37 -0500691//////////////////////////////////////////////////////////////////////////////
692
693GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
694 const GrBackendFormat& backendFormat,
695 const SkColor4f& color,
696 GrMipMapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400697 GrProtected isProtected,
698 GrGpuFinishedProc finishedProc,
699 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500700 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
701 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400702 finishedProc(finishedContext);
703 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500704 }
705
706 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400707 finishedProc(finishedContext);
708 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500709 }
710
711 GrGpu::BackendTextureData data(color);
Robert Phillips4277f012020-01-21 14:28:34 -0500712 return fGpu->createCompressedBackendTexture({width, height}, backendFormat,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400713 mipMapped, isProtected, finishedProc,
714 finishedContext, &data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500715}
716
717GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
718 SkImage::CompressionType compression,
719 const SkColor4f& color,
720 GrMipMapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400721 GrProtected isProtected,
722 GrGpuFinishedProc finishedProc,
723 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500724 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
725 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400726 finishedProc(finishedContext);
727 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500728 }
729
730 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400731 finishedProc(finishedContext);
732 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500733 }
734
735 GrBackendFormat format = this->compressedBackendFormat(compression);
736 return this->createCompressedBackendTexture(width, height, format, color,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400737 mipMapped, isProtected, finishedProc,
738 finishedContext);
Robert Phillipsb915c942019-12-17 14:44:37 -0500739}
740
741GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
742 const GrBackendFormat& backendFormat,
743 const void* compressedData,
744 size_t dataSize,
745 GrMipMapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400746 GrProtected isProtected,
747 GrGpuFinishedProc finishedProc,
748 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500749 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
750 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400751 finishedProc(finishedContext);
752 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500753 }
754
755 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400756 finishedProc(finishedContext);
757 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500758 }
759
760 GrGpu::BackendTextureData data(compressedData, dataSize);
Robert Phillips4277f012020-01-21 14:28:34 -0500761 return fGpu->createCompressedBackendTexture({width, height}, backendFormat,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400762 mipMapped, isProtected, finishedProc,
763 finishedContext, &data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500764}
765
766GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
767 SkImage::CompressionType compression,
768 const void* data, size_t dataSize,
769 GrMipMapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400770 GrProtected isProtected,
771 GrGpuFinishedProc finishedProc,
772 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500773 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
774 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400775 finishedProc(finishedContext);
776 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500777 }
778
779 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400780 finishedProc(finishedContext);
781 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500782 }
783
784 GrBackendFormat format = this->compressedBackendFormat(compression);
785 return this->createCompressedBackendTexture(width, height, format, data, dataSize,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400786 mipMapped, isProtected, finishedProc,
787 finishedContext);
Robert Phillipsb915c942019-12-17 14:44:37 -0500788}
789
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400790void GrContext::deleteBackendTexture(GrBackendTexture backendTex) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400791 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danielf0e04f02019-12-04 15:17:54 -0500792 // For the Vulkan backend we still must destroy the backend texture when the context is
793 // abandoned.
794 if ((this->abandoned() && this->backend() != GrBackendApi::kVulkan) || !backendTex.isValid()) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400795 return;
796 }
797
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400798 fGpu->deleteBackendTexture(backendTex);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400799}
800
Brian Osmaned58e002019-09-06 14:42:43 -0400801bool GrContext::precompileShader(const SkData& key, const SkData& data) {
802 return fGpu->precompileShader(key, data);
803}
804
Brian Salomonec22b1a2019-08-09 09:41:48 -0400805#ifdef SK_ENABLE_DUMP_GPU
Michael Ludwigdd205452020-03-30 17:16:34 -0400806#include "include/core/SkString.h"
Brian Salomonec22b1a2019-08-09 09:41:48 -0400807#include "src/utils/SkJSONWriter.h"
808SkString GrContext::dump() const {
809 SkDynamicMemoryWStream stream;
810 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
811 writer.beginObject();
812
813 writer.appendString("backend", GrBackendApiToStr(this->backend()));
814
815 writer.appendName("caps");
816 this->caps()->dumpJSON(&writer);
817
818 writer.appendName("gpu");
819 this->fGpu->dumpJSON(&writer);
820
Robert Phillips273f1072020-05-05 13:03:07 -0400821 writer.appendName("context");
822 this->dumpJSON(&writer);
823
Brian Salomonec22b1a2019-08-09 09:41:48 -0400824 // Flush JSON to the memory stream
825 writer.endObject();
826 writer.flush();
827
828 // Null terminate the JSON data in the memory stream
829 stream.write8(0);
830
831 // Allocate a string big enough to hold all the data, then copy out of the stream
832 SkString result(stream.bytesWritten());
833 stream.copyToAndReset(result.writable_str());
834 return result;
835}
836#endif