blob: d03b1741fe72c19b6a864ff9b2fa0b9dff32b8c8 [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)
joshualitt1de610a2016-01-06 08:26:09 -080044#define ASSERT_SINGLE_OWNER \
Robert Phillipsa41c6852019-02-07 10:44:10 -050045 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
Robert Phillipsa9162df2019-02-11 14:12:03 -050046#define RETURN_IF_ABANDONED if (this->abandoned()) { return; }
47#define RETURN_FALSE_IF_ABANDONED if (this->abandoned()) { return false; }
48#define RETURN_NULL_IF_ABANDONED if (this->abandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000049
robertphillipsea461502015-05-26 11:38:03 -070050////////////////////////////////////////////////////////////////////////////////
51
Robert Phillipsa41c6852019-02-07 10:44:10 -050052GrContext::GrContext(GrBackendApi backend, const GrContextOptions& options, int32_t contextID)
53 : INHERITED(backend, options, contextID) {
halcanary96fcdcc2015-08-27 07:41:13 -070054 fResourceCache = nullptr;
55 fResourceProvider = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000056}
57
Robert Phillips292a6b22019-02-14 14:49:02 -050058GrContext::~GrContext() {
59 ASSERT_SINGLE_OWNER
60
Robert Phillips6a6de562019-02-15 15:19:15 -050061 if (this->drawingManager()) {
62 this->drawingManager()->cleanup();
Robert Phillips292a6b22019-02-14 14:49:02 -050063 }
64 delete fResourceProvider;
65 delete fResourceCache;
Robert Phillips292a6b22019-02-14 14:49:02 -050066}
67
Brian Osman7b1678a2019-12-16 09:17:25 -050068bool GrContext::init(sk_sp<const GrCaps> caps) {
Greg Danielb76a72a2017-07-13 15:07:54 -040069 ASSERT_SINGLE_OWNER
Robert Phillipsfde6fa02018-03-02 08:53:14 -050070 SkASSERT(fThreadSafeProxy); // needs to have been initialized by derived classes
Robert Phillipsa41c6852019-02-07 10:44:10 -050071 SkASSERT(this->proxyProvider());
Robert Phillips88260b52018-01-19 12:56:09 -050072
Brian Osman7b1678a2019-12-16 09:17:25 -050073 if (!INHERITED::init(std::move(caps))) {
Robert Phillipsbb606772019-02-04 17:50:57 -050074 return false;
75 }
76
77 SkASSERT(this->caps());
Robert Phillips2184fb72019-02-21 16:11:41 -050078 SkASSERT(this->getTextBlobCache());
Robert Phillipsbb606772019-02-04 17:50:57 -050079
Robert Phillips88260b52018-01-19 12:56:09 -050080 if (fGpu) {
Robert Phillips4d932d12020-04-09 08:58:52 -040081 fStrikeCache.reset(new GrStrikeCache{});
Robert Phillipsa41c6852019-02-07 10:44:10 -050082 fResourceCache = new GrResourceCache(this->caps(), this->singleOwner(), this->contextID());
Robert Phillips12c46292019-04-23 07:36:17 -040083 fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, this->singleOwner());
Mike Kleinf46d5ca2019-12-11 10:45:01 -050084 fMappedBufferManager = std::make_unique<GrClientMappedBufferManager>(this->contextID());
Robert Phillips88260b52018-01-19 12:56:09 -050085 }
86
Robert Phillips88260b52018-01-19 12:56:09 -050087 if (fResourceCache) {
Robert Phillipsa41c6852019-02-07 10:44:10 -050088 fResourceCache->setProxyProvider(this->proxyProvider());
Robert Phillips88260b52018-01-19 12:56:09 -050089 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050090
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000091 fDidTestPMConversions = false;
92
Robert Phillipsfde6fa02018-03-02 08:53:14 -050093 // DDL TODO: we need to think through how the task group & persistent cache
94 // get passed on to/shared between all the DDLRecorders created with this context.
Robert Phillipsc1541ae2019-02-04 12:05:37 -050095 if (this->options().fExecutor) {
Mike Kleinf46d5ca2019-12-11 10:45:01 -050096 fTaskGroup = std::make_unique<SkTaskGroup>(*this->options().fExecutor);
Brian Osman51279982017-08-23 10:12:00 -040097 }
98
Robert Phillipsc1541ae2019-02-04 12:05:37 -050099 fPersistentCache = this->options().fPersistentCache;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400100 fShaderErrorHandler = this->options().fShaderErrorHandler;
101 if (!fShaderErrorHandler) {
102 fShaderErrorHandler = GrShaderUtils::DefaultShaderErrorHandler();
103 }
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400104
Brian Salomon91a3e522017-06-23 10:58:19 -0400105 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000106}
107
Robert Phillips4217ea72019-01-30 13:08:28 -0500108sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
109 return fThreadSafeProxy;
110}
111
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400112//////////////////////////////////////////////////////////////////////////////
113
bsalomon2354f842014-07-28 13:48:36 -0700114void GrContext::abandonContext() {
Greg Daniel6e35a002020-04-01 13:29:59 -0400115 if (INHERITED::abandoned()) {
Robert Phillipsa9162df2019-02-11 14:12:03 -0500116 return;
117 }
joshualitt1de610a2016-01-06 08:26:09 -0800118
Robert Phillipsa9162df2019-02-11 14:12:03 -0500119 INHERITED::abandonContext();
120
Robert Phillips4d932d12020-04-09 08:58:52 -0400121 fStrikeCache->freeAll();
122
Brian Salomon9241a6d2019-10-03 13:26:54 -0400123 fMappedBufferManager->abandon();
124
bsalomond309e7a2015-04-30 14:18:54 -0700125 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800126
Robert Phillipsa9162df2019-02-11 14:12:03 -0500127 // Need to cleanup the drawing manager first so all the render targets
robertphillips0dfa62c2015-11-16 06:23:31 -0800128 // will be released/forgotten before they too are abandoned.
Robert Phillips6a6de562019-02-15 15:19:15 -0500129 this->drawingManager()->cleanup();
robertphillips0dfa62c2015-11-16 06:23:31 -0800130
bsalomon@google.com205d4602011-04-25 12:43:45 +0000131 // abandon first to so destructors
132 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800133 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700134
bsalomon6e2aad42016-04-01 11:54:31 -0700135 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400136
137 fMappedBufferManager.reset();
Khushalc421ca12018-06-26 14:38:34 -0700138}
139
bsalomon6e2aad42016-04-01 11:54:31 -0700140void GrContext::releaseResourcesAndAbandonContext() {
Greg Daniel6e35a002020-04-01 13:29:59 -0400141 if (INHERITED::abandoned()) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500142 return;
143 }
Robert Phillipsa9162df2019-02-11 14:12:03 -0500144
145 INHERITED::abandonContext();
146
Brian Salomon9241a6d2019-10-03 13:26:54 -0400147 fMappedBufferManager.reset();
148
bsalomon6e2aad42016-04-01 11:54:31 -0700149 fResourceProvider->abandon();
150
Robert Phillipsa9162df2019-02-11 14:12:03 -0500151 // Need to cleanup the drawing manager first so all the render targets
bsalomon6e2aad42016-04-01 11:54:31 -0700152 // will be released/forgotten before they too are abandoned.
Robert Phillips6a6de562019-02-15 15:19:15 -0500153 this->drawingManager()->cleanup();
bsalomon6e2aad42016-04-01 11:54:31 -0700154
155 // Release all resources in the backend 3D API.
156 fResourceCache->releaseAll();
157
158 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000159}
160
Greg Daniel6e35a002020-04-01 13:29:59 -0400161bool GrContext::abandoned() {
162 if (INHERITED::abandoned()) {
163 return true;
164 }
165
166 if (fGpu && fGpu->isDeviceLost()) {
167 this->abandonContext();
168 return true;
169 }
170 return false;
171}
172
Brian Salomon1f05d452019-02-08 12:33:08 -0500173void GrContext::resetGLTextureBindings() {
174 if (this->abandoned() || this->backend() != GrBackendApi::kOpenGL) {
175 return;
176 }
177 fGpu->resetTextureBindings();
178}
179
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000180void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800181 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000182 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000183}
184
185void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800186 ASSERT_SINGLE_OWNER
187
Robert Phillips2184fb72019-02-21 16:11:41 -0500188 // TODO: the glyph cache doesn't hold any GpuResources so this call should not be needed here.
189 // Some slack in the GrTextBlob's implementation requires it though. That could be fixed.
Robert Phillips4d932d12020-04-09 08:58:52 -0400190 fStrikeCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700191
Robert Phillips6a6de562019-02-15 15:19:15 -0500192 this->drawingManager()->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700193
194 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000195}
196
Robert Phillips6eba0632018-03-28 12:25:42 -0400197void GrContext::purgeUnlockedResources(bool scratchResourcesOnly) {
198 ASSERT_SINGLE_OWNER
Robert Phillipsddc21482019-10-16 14:30:09 -0400199
200 if (this->abandoned()) {
201 return;
202 }
203
Robert Phillips6eba0632018-03-28 12:25:42 -0400204 fResourceCache->purgeUnlockedResources(scratchResourcesOnly);
205 fResourceCache->purgeAsNeeded();
Robert Phillips2184fb72019-02-21 16:11:41 -0500206
207 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
208 // place to purge stale blobs
209 this->getTextBlobCache()->purgeStaleBlobs();
Robert Phillips6eba0632018-03-28 12:25:42 -0400210}
211
Jim Van Verth76d917c2017-12-13 09:26:37 -0500212void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Yuqian Li40aa85f2019-07-02 13:45:00 -0700213 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
214
Brian Salomon5e150852017-03-22 14:53:13 -0400215 ASSERT_SINGLE_OWNER
Chris Dalton6c3879d2018-11-01 11:13:19 -0600216
Brian Salomon9241a6d2019-10-03 13:26:54 -0400217 if (this->abandoned()) {
218 return;
219 }
220
221 fMappedBufferManager->process();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600222 auto purgeTime = GrStdSteadyClock::now() - msNotUsed;
223
Jim Van Verth76d917c2017-12-13 09:26:37 -0500224 fResourceCache->purgeAsNeeded();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600225 fResourceCache->purgeResourcesNotUsedSince(purgeTime);
226
Robert Phillips6a6de562019-02-15 15:19:15 -0500227 if (auto ccpr = this->drawingManager()->getCoverageCountingPathRenderer()) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500228 ccpr->purgeCacheEntriesOlderThan(this->proxyProvider(), purgeTime);
Chris Dalton6c3879d2018-11-01 11:13:19 -0600229 }
Jim Van Verth76d917c2017-12-13 09:26:37 -0500230
Robert Phillips2184fb72019-02-21 16:11:41 -0500231 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
232 // place to purge stale blobs
233 this->getTextBlobCache()->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400234}
235
Derek Sollenberger5480a182017-05-25 16:43:59 -0400236void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
237 ASSERT_SINGLE_OWNER
Robert Phillipsddc21482019-10-16 14:30:09 -0400238
239 if (this->abandoned()) {
240 return;
241 }
242
Derek Sollenberger5480a182017-05-25 16:43:59 -0400243 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
244}
245
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000246void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800247 ASSERT_SINGLE_OWNER
248
bsalomon71cb0c22014-11-14 12:10:14 -0800249 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800250 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800251 }
252 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800253 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800254 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000255}
256
Derek Sollenbergeree479142017-05-24 11:41:33 -0400257size_t GrContext::getResourceCachePurgeableBytes() const {
258 ASSERT_SINGLE_OWNER
259 return fResourceCache->getPurgeableBytes();
260}
261
Greg Daniel8b666172019-10-09 12:38:22 -0400262size_t GrContext::ComputeImageSize(sk_sp<SkImage> image, GrMipMapped mipMapped, bool useNextPow2) {
263 if (!image->isTextureBacked()) {
264 return 0;
265 }
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400266 SkImage_GpuBase* gpuImage = static_cast<SkImage_GpuBase*>(as_IB(image.get()));
267 GrTextureProxy* proxy = gpuImage->peekProxy();
268 if (!proxy) {
269 return 0;
270 }
271
272 const GrCaps& caps = *gpuImage->context()->priv().caps();
Greg Daniel8b666172019-10-09 12:38:22 -0400273 int colorSamplesPerPixel = 1;
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400274 return GrSurface::ComputeSize(caps, proxy->backendFormat(), image->dimensions(),
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400275 colorSamplesPerPixel, mipMapped, useNextPow2);
Greg Daniel8b666172019-10-09 12:38:22 -0400276}
277
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000278////////////////////////////////////////////////////////////////////////////////
279
Robert Phillipsbb606772019-02-04 17:50:57 -0500280int GrContext::maxTextureSize() const { return this->caps()->maxTextureSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400281
Robert Phillipsbb606772019-02-04 17:50:57 -0500282int GrContext::maxRenderTargetSize() const { return this->caps()->maxRenderTargetSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400283
Brian Salomonbdecacf2018-02-02 20:32:49 -0500284bool GrContext::colorTypeSupportedAsImage(SkColorType colorType) const {
Greg Daniel7bfc9132019-08-14 14:23:53 -0400285 GrBackendFormat format =
286 this->caps()->getDefaultBackendFormat(SkColorTypeToGrColorType(colorType),
287 GrRenderable::kNo);
288 return format.isValid();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500289}
290
291int GrContext::maxSurfaceSampleCountForColorType(SkColorType colorType) const {
Greg Danieleadfac92019-08-02 09:03:53 -0400292 GrBackendFormat format =
293 this->caps()->getDefaultBackendFormat(SkColorTypeToGrColorType(colorType),
294 GrRenderable::kYes);
295 return this->caps()->maxRenderTargetSampleCount(format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500296}
297
298////////////////////////////////////////////////////////////////////////////////
299
Greg Daniel06be0792019-04-22 15:53:23 -0400300bool GrContext::wait(int numSemaphores, const GrBackendSemaphore waitSemaphores[]) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400301 if (!fGpu || fGpu->caps()->semaphoreSupport()) {
Greg Daniel06be0792019-04-22 15:53:23 -0400302 return false;
303 }
304 for (int i = 0; i < numSemaphores; ++i) {
Greg Daniel301015c2019-11-18 14:06:46 -0500305 std::unique_ptr<GrSemaphore> sema = fResourceProvider->wrapBackendSemaphore(
Greg Daniel06be0792019-04-22 15:53:23 -0400306 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
307 kAdopt_GrWrapOwnership);
Greg Daniel301015c2019-11-18 14:06:46 -0500308 fGpu->waitSemaphore(sema.get());
Greg Daniel06be0792019-04-22 15:53:23 -0400309 }
310 return true;
311}
312
313////////////////////////////////////////////////////////////////////////////////
314
Greg Daniel797efca2019-05-09 14:04:20 -0400315GrSemaphoresSubmitted GrContext::flush(const GrFlushInfo& info,
316 const GrPrepareForExternalIORequests& externalRequests) {
Greg Daniel51316782017-08-02 15:10:09 +0000317 ASSERT_SINGLE_OWNER
Robert Phillipsa9162df2019-02-11 14:12:03 -0500318 if (this->abandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400319 if (info.fFinishedProc) {
320 info.fFinishedProc(info.fFinishedContext);
321 }
322 if (info.fSubmittedProc) {
323 info.fSubmittedProc(info.fSubmittedContext, false);
324 }
Robert Phillipsa9162df2019-02-11 14:12:03 -0500325 return GrSemaphoresSubmitted::kNo;
326 }
Greg Daniel51316782017-08-02 15:10:09 +0000327
Greg Daniel04283f32020-05-20 13:16:00 -0400328 bool flushed = this->drawingManager()->flush(
329 nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, info, externalRequests);
Greg Danielfe159622020-04-10 17:43:51 +0000330
Greg Daniel04283f32020-05-20 13:16:00 -0400331 if (!flushed || (!this->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
Greg Danielfe159622020-04-10 17:43:51 +0000332 return GrSemaphoresSubmitted::kNo;
333 }
334 return GrSemaphoresSubmitted::kYes;
Greg Daniel51316782017-08-02 15:10:09 +0000335}
336
Greg Daniel04283f32020-05-20 13:16:00 -0400337bool GrContext::submit(bool syncCpu) {
338 ASSERT_SINGLE_OWNER
339 if (this->abandoned()) {
340 return false;
341 }
342
343 if (!fGpu) {
344 return false;
345 }
346
347 return fGpu->submitToGpu(syncCpu);
Greg Danielda50cb82020-05-13 14:07:40 -0400348}
349
Greg Daniela870b462019-01-08 15:49:46 -0500350////////////////////////////////////////////////////////////////////////////////
351
Brian Salomonb0d8b762019-05-06 16:58:22 -0400352void GrContext::checkAsyncWorkCompletion() {
353 if (fGpu) {
354 fGpu->checkFinishProcs();
355 }
356}
357
358////////////////////////////////////////////////////////////////////////////////
359
Greg Daniela870b462019-01-08 15:49:46 -0500360void GrContext::storeVkPipelineCacheData() {
361 if (fGpu) {
362 fGpu->storeVkPipelineCacheData();
363 }
364}
365
366////////////////////////////////////////////////////////////////////////////////
367
Khushal3e7548c2018-05-23 15:45:01 -0700368bool GrContext::supportsDistanceFieldText() const {
Robert Phillipsbb606772019-02-04 17:50:57 -0500369 return this->caps()->shaderCaps()->supportsDistanceFieldText();
Khushal3e7548c2018-05-23 15:45:01 -0700370}
371
bsalomon37f9a262015-02-02 13:00:10 -0800372//////////////////////////////////////////////////////////////////////////////
373
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500374void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800375 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500376 if (maxResources) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400377 *maxResources = -1;
bsalomon37f9a262015-02-02 13:00:10 -0800378 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500379 if (maxResourceBytes) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400380 *maxResourceBytes = this->getResourceCacheLimit();
bsalomon37f9a262015-02-02 13:00:10 -0800381 }
382}
383
Robert Phillipscf39f372019-09-03 10:29:20 -0400384size_t GrContext::getResourceCacheLimit() const {
joshualitt1de610a2016-01-06 08:26:09 -0800385 ASSERT_SINGLE_OWNER
Robert Phillipscf39f372019-09-03 10:29:20 -0400386 return fResourceCache->getMaxResourceBytes();
387}
388
389void GrContext::setResourceCacheLimits(int unused, size_t maxResourceBytes) {
390 ASSERT_SINGLE_OWNER
391 this->setResourceCacheLimit(maxResourceBytes);
392}
393
394void GrContext::setResourceCacheLimit(size_t maxResourceBytes) {
395 ASSERT_SINGLE_OWNER
396 fResourceCache->setLimit(maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800397}
398
ericrk0a5fa482015-09-15 14:16:10 -0700399//////////////////////////////////////////////////////////////////////////////
ericrk0a5fa482015-09-15 14:16:10 -0700400void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800401 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700402 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
Khushal71652e22018-10-29 13:05:36 -0700403 traceMemoryDump->dumpNumericValue("skia/gr_text_blob_cache", "size", "bytes",
Robert Phillips2184fb72019-02-21 16:11:41 -0500404 this->getTextBlobCache()->usedBytes());
ericrk0a5fa482015-09-15 14:16:10 -0700405}
Brian Osman71a18892017-08-10 10:23:25 -0400406
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400407//////////////////////////////////////////////////////////////////////////////
408GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400409 const GrBackendFormat& backendFormat,
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400410 GrMipMapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400411 GrRenderable renderable,
412 GrProtected isProtected) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400413 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400414 if (!this->asDirectContext()) {
415 return GrBackendTexture();
416 }
417
418 if (this->abandoned()) {
419 return GrBackendTexture();
420 }
421
Robert Phillips4277f012020-01-21 14:28:34 -0500422 return fGpu->createBackendTexture({width, height}, backendFormat, renderable,
Greg Daniel16032b32020-05-06 15:31:10 -0400423 mipMapped, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400424}
425
426GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400427 SkColorType skColorType,
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400428 GrMipMapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400429 GrRenderable renderable,
430 GrProtected isProtected) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400431 if (!this->asDirectContext()) {
432 return GrBackendTexture();
433 }
434
435 if (this->abandoned()) {
436 return GrBackendTexture();
437 }
438
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400439 const GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400440
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400441 return this->createBackendTexture(width, height, format, mipMapped, renderable, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400442}
443
Robert Phillips02dc0302019-07-02 17:58:27 -0400444GrBackendTexture GrContext::createBackendTexture(const SkSurfaceCharacterization& c) {
Robert Phillips02dc0302019-07-02 17:58:27 -0400445 if (!this->asDirectContext() || !c.isValid()) {
446 return GrBackendTexture();
447 }
448
449 if (this->abandoned()) {
450 return GrBackendTexture();
451 }
452
453 if (c.usesGLFBO0()) {
454 // If we are making the surface we will never use FBO0.
455 return GrBackendTexture();
456 }
457
458 if (c.vulkanSecondaryCBCompatible()) {
459 return {};
460 }
461
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400462 const GrBackendFormat format = this->defaultBackendFormat(c.colorType(), GrRenderable::kYes);
Robert Phillips02dc0302019-07-02 17:58:27 -0400463 if (!format.isValid()) {
464 return GrBackendTexture();
465 }
466
Robert Phillips02dc0302019-07-02 17:58:27 -0400467 GrBackendTexture result = this->createBackendTexture(c.width(), c.height(), format,
468 GrMipMapped(c.isMipMapped()),
469 GrRenderable::kYes,
Robert Phillips3cd54322019-07-10 09:28:59 -0400470 c.isProtected());
Robert Phillips02dc0302019-07-02 17:58:27 -0400471 SkASSERT(c.isCompatible(result));
472 return result;
473}
474
475GrBackendTexture GrContext::createBackendTexture(const SkSurfaceCharacterization& c,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400476 const SkColor4f& color,
477 GrGpuFinishedProc finishedProc,
478 GrGpuFinishedContext finishedContext) {
Robert Phillips02dc0302019-07-02 17:58:27 -0400479 if (!this->asDirectContext() || !c.isValid()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400480 finishedProc(finishedContext);
481 return {};
Robert Phillips02dc0302019-07-02 17:58:27 -0400482 }
483
484 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400485 finishedProc(finishedContext);
486 return {};
Robert Phillips02dc0302019-07-02 17:58:27 -0400487 }
488
489 if (c.usesGLFBO0()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400490 finishedProc(finishedContext);
Robert Phillips02dc0302019-07-02 17:58:27 -0400491 // If we are making the surface we will never use FBO0.
Greg Danielc1ad77c2020-05-06 11:40:03 -0400492 return {};
Robert Phillips02dc0302019-07-02 17:58:27 -0400493 }
494
495 if (c.vulkanSecondaryCBCompatible()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400496 finishedProc(finishedContext);
Robert Phillips02dc0302019-07-02 17:58:27 -0400497 return {};
498 }
499
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400500 const GrBackendFormat format = this->defaultBackendFormat(c.colorType(), GrRenderable::kYes);
Robert Phillips02dc0302019-07-02 17:58:27 -0400501 if (!format.isValid()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400502 finishedProc(finishedContext);
503 return {};
Robert Phillips02dc0302019-07-02 17:58:27 -0400504 }
505
Robert Phillips02dc0302019-07-02 17:58:27 -0400506 GrBackendTexture result = this->createBackendTexture(c.width(), c.height(), format, color,
507 GrMipMapped(c.isMipMapped()),
508 GrRenderable::kYes,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400509 c.isProtected(), finishedProc,
510 finishedContext);
Robert Phillips02dc0302019-07-02 17:58:27 -0400511 SkASSERT(c.isCompatible(result));
512 return result;
513}
514
Greg Daniel16032b32020-05-06 15:31:10 -0400515static GrBackendTexture create_and_update_backend_texture(GrContext* context,
516 SkISize dimensions,
517 const GrBackendFormat& backendFormat,
518 GrMipMapped mipMapped,
519 GrRenderable renderable,
520 GrProtected isProtected,
521 GrGpuFinishedProc finishedProc,
522 GrGpuFinishedContext finishedContext,
523 const GrGpu::BackendTextureData* data) {
524 GrGpu* gpu = context->priv().getGpu();
525
526 GrBackendTexture beTex = gpu->createBackendTexture(dimensions, backendFormat, renderable,
527 mipMapped, isProtected);
528 if (!beTex.isValid()) {
529 return {};
530 }
531
532 if (!context->priv().getGpu()->updateBackendTexture(beTex, finishedProc, finishedContext,
533 data)) {
534 context->deleteBackendTexture(beTex);
535 return {};
536 }
537 return beTex;
538}
539
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400540GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400541 const GrBackendFormat& backendFormat,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400542 const SkColor4f& color,
543 GrMipMapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400544 GrRenderable renderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400545 GrProtected isProtected,
546 GrGpuFinishedProc finishedProc,
547 GrGpuFinishedContext finishedContext) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400548 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400549 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400550 finishedProc(finishedContext);
551 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400552 }
553
554 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400555 finishedProc(finishedContext);
556 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400557 }
558
Brian Salomon85c3d682019-11-04 15:04:54 -0500559 GrGpu::BackendTextureData data(color);
Greg Daniel16032b32020-05-06 15:31:10 -0400560 return create_and_update_backend_texture(this, {width, height}, backendFormat, mipMapped,
561 renderable, isProtected, finishedProc, finishedContext,
562 &data);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400563}
564
565GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400566 SkColorType skColorType,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400567 const SkColor4f& color,
568 GrMipMapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400569 GrRenderable renderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400570 GrProtected isProtected,
571 GrGpuFinishedProc finishedProc,
572 GrGpuFinishedContext finishedContext) {
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400573 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400574 finishedProc(finishedContext);
575 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400576 }
577
578 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400579 finishedProc(finishedContext);
580 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400581 }
582
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400583 GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400584 if (!format.isValid()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400585 finishedProc(finishedContext);
586 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400587 }
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400588
589 GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
Brian Salomon982f5462020-03-30 12:52:33 -0400590 SkColor4f swizzledColor = this->caps()->getWriteSwizzle(format, grColorType).applyTo(color);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400591
Brian Salomonb450f3b2019-07-09 09:36:51 -0400592 return this->createBackendTexture(width, height, format, swizzledColor, mipMapped, renderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400593 isProtected, finishedProc, finishedContext);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400594}
595
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500596GrBackendTexture GrContext::createBackendTexture(const SkPixmap srcData[], int numProvidedLevels,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400597 GrRenderable renderable, GrProtected isProtected,
598 GrGpuFinishedProc finishedProc,
599 GrGpuFinishedContext finishedContext) {
Robert Phillips66944402019-09-30 13:21:25 -0400600 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
601
602 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400603 finishedProc(finishedContext);
Robert Phillips66944402019-09-30 13:21:25 -0400604 return {};
605 }
606
607 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400608 finishedProc(finishedContext);
Robert Phillips66944402019-09-30 13:21:25 -0400609 return {};
610 }
611
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500612 if (!srcData || numProvidedLevels <= 0) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400613 finishedProc(finishedContext);
Robert Phillips66944402019-09-30 13:21:25 -0400614 return {};
615 }
616
617 int baseWidth = srcData[0].width();
618 int baseHeight = srcData[0].height();
619 SkColorType colorType = srcData[0].colorType();
620
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500621 GrMipMapped mipMapped = GrMipMapped::kNo;
622 int numExpectedLevels = 1;
623 if (numProvidedLevels > 1) {
624 numExpectedLevels = SkMipMap::ComputeLevelCount(baseWidth, baseHeight) + 1;
625 mipMapped = GrMipMapped::kYes;
626 }
627
628 if (numProvidedLevels != numExpectedLevels) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400629 finishedProc(finishedContext);
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500630 return {};
631 }
632
Robert Phillips66944402019-09-30 13:21:25 -0400633 GrBackendFormat backendFormat = this->defaultBackendFormat(colorType, renderable);
634
Brian Salomon85c3d682019-11-04 15:04:54 -0500635 GrGpu::BackendTextureData data(srcData);
Greg Daniel16032b32020-05-06 15:31:10 -0400636 return create_and_update_backend_texture(this, {baseWidth, baseHeight}, backendFormat,
637 mipMapped, renderable, isProtected, finishedProc,
638 finishedContext, &data);
Robert Phillips66944402019-09-30 13:21:25 -0400639}
640
Greg Danielb2365d82020-05-13 15:32:04 -0400641bool GrContext::updateBackendTexture(const GrBackendTexture& backendTexture,
642 const SkColor4f& color,
643 GrGpuFinishedProc finishedProc,
644 GrGpuFinishedContext finishedContext) {
645 if (!this->asDirectContext()) {
646 finishedProc(finishedContext);
647 return false;
648 }
649
650 if (this->abandoned()) {
651 finishedProc(finishedContext);
652 return false;
653 }
654
655 GrGpu::BackendTextureData data(color);
656 return fGpu->updateBackendTexture(backendTexture, finishedProc, finishedContext, &data);
657}
658
659bool GrContext::updateBackendTexture(const GrBackendTexture& backendTexture,
660 const SkPixmap srcData[],
661 int numLevels,
662 GrGpuFinishedProc finishedProc,
663 GrGpuFinishedContext finishedContext) {
664 if (!this->asDirectContext()) {
665 finishedProc(finishedContext);
666 return false;
667 }
668
669 if (this->abandoned()) {
670 finishedProc(finishedContext);
671 return false;
672 }
673
674 if (!srcData || numLevels <= 0) {
675 finishedProc(finishedContext);
676 return false;
677 }
678
679 int numExpectedLevels = 1;
680 if (backendTexture.hasMipMaps()) {
681 numExpectedLevels = SkMipMap::ComputeLevelCount(backendTexture.width(),
682 backendTexture.height()) + 1;
683 }
684 if (numLevels != numExpectedLevels) {
685 finishedProc(finishedContext);
686 return false;
687 }
688
689 GrGpu::BackendTextureData data(srcData);
690 return fGpu->updateBackendTexture(backendTexture, finishedProc, finishedContext, &data);
691}
692
Robert Phillipsb915c942019-12-17 14:44:37 -0500693//////////////////////////////////////////////////////////////////////////////
694
695GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
696 const GrBackendFormat& backendFormat,
697 const SkColor4f& color,
698 GrMipMapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400699 GrProtected isProtected,
700 GrGpuFinishedProc finishedProc,
701 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500702 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
703 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400704 finishedProc(finishedContext);
705 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500706 }
707
708 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400709 finishedProc(finishedContext);
710 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500711 }
712
713 GrGpu::BackendTextureData data(color);
Robert Phillips4277f012020-01-21 14:28:34 -0500714 return fGpu->createCompressedBackendTexture({width, height}, backendFormat,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400715 mipMapped, isProtected, finishedProc,
716 finishedContext, &data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500717}
718
719GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
720 SkImage::CompressionType compression,
721 const SkColor4f& color,
722 GrMipMapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400723 GrProtected isProtected,
724 GrGpuFinishedProc finishedProc,
725 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500726 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
727 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400728 finishedProc(finishedContext);
729 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500730 }
731
732 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400733 finishedProc(finishedContext);
734 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500735 }
736
737 GrBackendFormat format = this->compressedBackendFormat(compression);
738 return this->createCompressedBackendTexture(width, height, format, color,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400739 mipMapped, isProtected, finishedProc,
740 finishedContext);
Robert Phillipsb915c942019-12-17 14:44:37 -0500741}
742
743GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
744 const GrBackendFormat& backendFormat,
745 const void* compressedData,
746 size_t dataSize,
747 GrMipMapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400748 GrProtected isProtected,
749 GrGpuFinishedProc finishedProc,
750 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500751 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
752 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400753 finishedProc(finishedContext);
754 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500755 }
756
757 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400758 finishedProc(finishedContext);
759 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500760 }
761
762 GrGpu::BackendTextureData data(compressedData, dataSize);
Robert Phillips4277f012020-01-21 14:28:34 -0500763 return fGpu->createCompressedBackendTexture({width, height}, backendFormat,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400764 mipMapped, isProtected, finishedProc,
765 finishedContext, &data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500766}
767
768GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
769 SkImage::CompressionType compression,
770 const void* data, size_t dataSize,
771 GrMipMapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400772 GrProtected isProtected,
773 GrGpuFinishedProc finishedProc,
774 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500775 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
776 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400777 finishedProc(finishedContext);
778 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500779 }
780
781 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400782 finishedProc(finishedContext);
783 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500784 }
785
786 GrBackendFormat format = this->compressedBackendFormat(compression);
787 return this->createCompressedBackendTexture(width, height, format, data, dataSize,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400788 mipMapped, isProtected, finishedProc,
789 finishedContext);
Robert Phillipsb915c942019-12-17 14:44:37 -0500790}
791
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400792void GrContext::deleteBackendTexture(GrBackendTexture backendTex) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400793 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danielf0e04f02019-12-04 15:17:54 -0500794 // For the Vulkan backend we still must destroy the backend texture when the context is
795 // abandoned.
796 if ((this->abandoned() && this->backend() != GrBackendApi::kVulkan) || !backendTex.isValid()) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400797 return;
798 }
799
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400800 fGpu->deleteBackendTexture(backendTex);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400801}
802
Brian Osmaned58e002019-09-06 14:42:43 -0400803bool GrContext::precompileShader(const SkData& key, const SkData& data) {
804 return fGpu->precompileShader(key, data);
805}
806
Brian Salomonec22b1a2019-08-09 09:41:48 -0400807#ifdef SK_ENABLE_DUMP_GPU
Michael Ludwigdd205452020-03-30 17:16:34 -0400808#include "include/core/SkString.h"
Brian Salomonec22b1a2019-08-09 09:41:48 -0400809#include "src/utils/SkJSONWriter.h"
810SkString GrContext::dump() const {
811 SkDynamicMemoryWStream stream;
812 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
813 writer.beginObject();
814
815 writer.appendString("backend", GrBackendApiToStr(this->backend()));
816
817 writer.appendName("caps");
818 this->caps()->dumpJSON(&writer);
819
820 writer.appendName("gpu");
821 this->fGpu->dumpJSON(&writer);
822
Robert Phillips273f1072020-05-05 13:03:07 -0400823 writer.appendName("context");
824 this->dumpJSON(&writer);
825
Brian Salomonec22b1a2019-08-09 09:41:48 -0400826 // Flush JSON to the memory stream
827 writer.endObject();
828 writer.flush();
829
830 // Null terminate the JSON data in the memory stream
831 stream.write8(0);
832
833 // Allocate a string big enough to hold all the data, then copy out of the stream
834 SkString result(stream.bytesWritten());
835 stream.copyToAndReset(result.writable_str());
836 return result;
837}
838#endif