blob: 77437a2564045d3f8f5536c124b1cdee9883bade [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
Adlai Hollere219d1c2020-06-02 11:23:16 -040051GrContext::GrContext(sk_sp<GrContextThreadSafeProxy> proxy) : INHERITED(std::move(proxy)) {
halcanary96fcdcc2015-08-27 07:41:13 -070052 fResourceCache = nullptr;
53 fResourceProvider = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000054}
55
Robert Phillips292a6b22019-02-14 14:49:02 -050056GrContext::~GrContext() {
57 ASSERT_SINGLE_OWNER
58
Robert Phillips6a6de562019-02-15 15:19:15 -050059 if (this->drawingManager()) {
60 this->drawingManager()->cleanup();
Robert Phillips292a6b22019-02-14 14:49:02 -050061 }
62 delete fResourceProvider;
63 delete fResourceCache;
Robert Phillips292a6b22019-02-14 14:49:02 -050064}
65
Adlai Hollere219d1c2020-06-02 11:23:16 -040066bool GrContext::init() {
Greg Danielb76a72a2017-07-13 15:07:54 -040067 ASSERT_SINGLE_OWNER
Robert Phillipsa41c6852019-02-07 10:44:10 -050068 SkASSERT(this->proxyProvider());
Robert Phillips88260b52018-01-19 12:56:09 -050069
Adlai Hollere219d1c2020-06-02 11:23:16 -040070 if (!INHERITED::init()) {
Robert Phillipsbb606772019-02-04 17:50:57 -050071 return false;
72 }
73
Robert Phillips2184fb72019-02-21 16:11:41 -050074 SkASSERT(this->getTextBlobCache());
Robert Phillipsbb606772019-02-04 17:50:57 -050075
Robert Phillips88260b52018-01-19 12:56:09 -050076 if (fGpu) {
Robert Phillips4d932d12020-04-09 08:58:52 -040077 fStrikeCache.reset(new GrStrikeCache{});
Robert Phillipsa41c6852019-02-07 10:44:10 -050078 fResourceCache = new GrResourceCache(this->caps(), this->singleOwner(), this->contextID());
Robert Phillips12c46292019-04-23 07:36:17 -040079 fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, this->singleOwner());
Mike Kleinf46d5ca2019-12-11 10:45:01 -050080 fMappedBufferManager = std::make_unique<GrClientMappedBufferManager>(this->contextID());
Robert Phillips88260b52018-01-19 12:56:09 -050081 }
82
Robert Phillips88260b52018-01-19 12:56:09 -050083 if (fResourceCache) {
Robert Phillipsa41c6852019-02-07 10:44:10 -050084 fResourceCache->setProxyProvider(this->proxyProvider());
Robert Phillips88260b52018-01-19 12:56:09 -050085 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050086
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000087 fDidTestPMConversions = false;
88
Robert Phillipsfde6fa02018-03-02 08:53:14 -050089 // DDL TODO: we need to think through how the task group & persistent cache
90 // get passed on to/shared between all the DDLRecorders created with this context.
Robert Phillipsc1541ae2019-02-04 12:05:37 -050091 if (this->options().fExecutor) {
Mike Kleinf46d5ca2019-12-11 10:45:01 -050092 fTaskGroup = std::make_unique<SkTaskGroup>(*this->options().fExecutor);
Brian Osman51279982017-08-23 10:12:00 -040093 }
94
Robert Phillipsc1541ae2019-02-04 12:05:37 -050095 fPersistentCache = this->options().fPersistentCache;
Brian Osman5e7fbfd2019-05-03 13:13:35 -040096 fShaderErrorHandler = this->options().fShaderErrorHandler;
97 if (!fShaderErrorHandler) {
98 fShaderErrorHandler = GrShaderUtils::DefaultShaderErrorHandler();
99 }
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400100
Brian Salomon91a3e522017-06-23 10:58:19 -0400101 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000102}
103
Robert Phillips4217ea72019-01-30 13:08:28 -0500104sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
Adlai Hollere219d1c2020-06-02 11:23:16 -0400105 return INHERITED::threadSafeProxy();
Robert Phillips4217ea72019-01-30 13:08:28 -0500106}
107
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400108//////////////////////////////////////////////////////////////////////////////
109
bsalomon2354f842014-07-28 13:48:36 -0700110void GrContext::abandonContext() {
Greg Daniel6e35a002020-04-01 13:29:59 -0400111 if (INHERITED::abandoned()) {
Robert Phillipsa9162df2019-02-11 14:12:03 -0500112 return;
113 }
joshualitt1de610a2016-01-06 08:26:09 -0800114
Robert Phillipsa9162df2019-02-11 14:12:03 -0500115 INHERITED::abandonContext();
116
Robert Phillips4d932d12020-04-09 08:58:52 -0400117 fStrikeCache->freeAll();
118
Brian Salomon9241a6d2019-10-03 13:26:54 -0400119 fMappedBufferManager->abandon();
120
bsalomond309e7a2015-04-30 14:18:54 -0700121 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800122
Robert Phillipsa9162df2019-02-11 14:12:03 -0500123 // Need to cleanup the drawing manager first so all the render targets
robertphillips0dfa62c2015-11-16 06:23:31 -0800124 // will be released/forgotten before they too are abandoned.
Robert Phillips6a6de562019-02-15 15:19:15 -0500125 this->drawingManager()->cleanup();
robertphillips0dfa62c2015-11-16 06:23:31 -0800126
bsalomon@google.com205d4602011-04-25 12:43:45 +0000127 // abandon first to so destructors
128 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800129 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700130
bsalomon6e2aad42016-04-01 11:54:31 -0700131 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400132
133 fMappedBufferManager.reset();
Khushalc421ca12018-06-26 14:38:34 -0700134}
135
bsalomon6e2aad42016-04-01 11:54:31 -0700136void GrContext::releaseResourcesAndAbandonContext() {
Greg Daniel6e35a002020-04-01 13:29:59 -0400137 if (INHERITED::abandoned()) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500138 return;
139 }
Robert Phillipsa9162df2019-02-11 14:12:03 -0500140
141 INHERITED::abandonContext();
142
Brian Salomon9241a6d2019-10-03 13:26:54 -0400143 fMappedBufferManager.reset();
144
bsalomon6e2aad42016-04-01 11:54:31 -0700145 fResourceProvider->abandon();
146
Robert Phillipsa9162df2019-02-11 14:12:03 -0500147 // Need to cleanup the drawing manager first so all the render targets
bsalomon6e2aad42016-04-01 11:54:31 -0700148 // will be released/forgotten before they too are abandoned.
Robert Phillips6a6de562019-02-15 15:19:15 -0500149 this->drawingManager()->cleanup();
bsalomon6e2aad42016-04-01 11:54:31 -0700150
151 // Release all resources in the backend 3D API.
152 fResourceCache->releaseAll();
153
154 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000155}
156
Greg Daniel6e35a002020-04-01 13:29:59 -0400157bool GrContext::abandoned() {
158 if (INHERITED::abandoned()) {
159 return true;
160 }
161
162 if (fGpu && fGpu->isDeviceLost()) {
163 this->abandonContext();
164 return true;
165 }
166 return false;
167}
168
Brian Salomon1f05d452019-02-08 12:33:08 -0500169void GrContext::resetGLTextureBindings() {
170 if (this->abandoned() || this->backend() != GrBackendApi::kOpenGL) {
171 return;
172 }
173 fGpu->resetTextureBindings();
174}
175
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000176void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800177 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000178 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000179}
180
181void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800182 ASSERT_SINGLE_OWNER
183
Robert Phillips2184fb72019-02-21 16:11:41 -0500184 // TODO: the glyph cache doesn't hold any GpuResources so this call should not be needed here.
185 // Some slack in the GrTextBlob's implementation requires it though. That could be fixed.
Robert Phillips4d932d12020-04-09 08:58:52 -0400186 fStrikeCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700187
Robert Phillips6a6de562019-02-15 15:19:15 -0500188 this->drawingManager()->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700189
190 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000191}
192
Robert Phillips6eba0632018-03-28 12:25:42 -0400193void GrContext::purgeUnlockedResources(bool scratchResourcesOnly) {
194 ASSERT_SINGLE_OWNER
Robert Phillipsddc21482019-10-16 14:30:09 -0400195
196 if (this->abandoned()) {
197 return;
198 }
199
Robert Phillips6eba0632018-03-28 12:25:42 -0400200 fResourceCache->purgeUnlockedResources(scratchResourcesOnly);
201 fResourceCache->purgeAsNeeded();
Robert Phillips2184fb72019-02-21 16:11:41 -0500202
203 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
204 // place to purge stale blobs
205 this->getTextBlobCache()->purgeStaleBlobs();
Robert Phillips6eba0632018-03-28 12:25:42 -0400206}
207
Jim Van Verth76d917c2017-12-13 09:26:37 -0500208void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Yuqian Li40aa85f2019-07-02 13:45:00 -0700209 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
210
Brian Salomon5e150852017-03-22 14:53:13 -0400211 ASSERT_SINGLE_OWNER
Chris Dalton6c3879d2018-11-01 11:13:19 -0600212
Brian Salomon9241a6d2019-10-03 13:26:54 -0400213 if (this->abandoned()) {
214 return;
215 }
216
217 fMappedBufferManager->process();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600218 auto purgeTime = GrStdSteadyClock::now() - msNotUsed;
219
Jim Van Verth76d917c2017-12-13 09:26:37 -0500220 fResourceCache->purgeAsNeeded();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600221 fResourceCache->purgeResourcesNotUsedSince(purgeTime);
222
Robert Phillips6a6de562019-02-15 15:19:15 -0500223 if (auto ccpr = this->drawingManager()->getCoverageCountingPathRenderer()) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500224 ccpr->purgeCacheEntriesOlderThan(this->proxyProvider(), purgeTime);
Chris Dalton6c3879d2018-11-01 11:13:19 -0600225 }
Jim Van Verth76d917c2017-12-13 09:26:37 -0500226
Robert Phillips2184fb72019-02-21 16:11:41 -0500227 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
228 // place to purge stale blobs
229 this->getTextBlobCache()->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400230}
231
Derek Sollenberger5480a182017-05-25 16:43:59 -0400232void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
233 ASSERT_SINGLE_OWNER
Robert Phillipsddc21482019-10-16 14:30:09 -0400234
235 if (this->abandoned()) {
236 return;
237 }
238
Derek Sollenberger5480a182017-05-25 16:43:59 -0400239 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
240}
241
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000242void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800243 ASSERT_SINGLE_OWNER
244
bsalomon71cb0c22014-11-14 12:10:14 -0800245 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800246 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800247 }
248 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800249 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800250 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000251}
252
Derek Sollenbergeree479142017-05-24 11:41:33 -0400253size_t GrContext::getResourceCachePurgeableBytes() const {
254 ASSERT_SINGLE_OWNER
255 return fResourceCache->getPurgeableBytes();
256}
257
Greg Daniel8b666172019-10-09 12:38:22 -0400258size_t GrContext::ComputeImageSize(sk_sp<SkImage> image, GrMipMapped mipMapped, bool useNextPow2) {
259 if (!image->isTextureBacked()) {
260 return 0;
261 }
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400262 SkImage_GpuBase* gpuImage = static_cast<SkImage_GpuBase*>(as_IB(image.get()));
263 GrTextureProxy* proxy = gpuImage->peekProxy();
264 if (!proxy) {
265 return 0;
266 }
267
268 const GrCaps& caps = *gpuImage->context()->priv().caps();
Greg Daniel8b666172019-10-09 12:38:22 -0400269 int colorSamplesPerPixel = 1;
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400270 return GrSurface::ComputeSize(caps, proxy->backendFormat(), image->dimensions(),
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400271 colorSamplesPerPixel, mipMapped, useNextPow2);
Greg Daniel8b666172019-10-09 12:38:22 -0400272}
273
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000274////////////////////////////////////////////////////////////////////////////////
275
Robert Phillipsbb606772019-02-04 17:50:57 -0500276int GrContext::maxTextureSize() const { return this->caps()->maxTextureSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400277
Robert Phillipsbb606772019-02-04 17:50:57 -0500278int GrContext::maxRenderTargetSize() const { return this->caps()->maxRenderTargetSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400279
Brian Salomonbdecacf2018-02-02 20:32:49 -0500280bool GrContext::colorTypeSupportedAsImage(SkColorType colorType) const {
Greg Daniel7bfc9132019-08-14 14:23:53 -0400281 GrBackendFormat format =
282 this->caps()->getDefaultBackendFormat(SkColorTypeToGrColorType(colorType),
283 GrRenderable::kNo);
284 return format.isValid();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500285}
286
287int GrContext::maxSurfaceSampleCountForColorType(SkColorType colorType) const {
Greg Danieleadfac92019-08-02 09:03:53 -0400288 GrBackendFormat format =
289 this->caps()->getDefaultBackendFormat(SkColorTypeToGrColorType(colorType),
290 GrRenderable::kYes);
291 return this->caps()->maxRenderTargetSampleCount(format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500292}
293
294////////////////////////////////////////////////////////////////////////////////
295
Greg Daniel06be0792019-04-22 15:53:23 -0400296bool GrContext::wait(int numSemaphores, const GrBackendSemaphore waitSemaphores[]) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400297 if (!fGpu || fGpu->caps()->semaphoreSupport()) {
Greg Daniel06be0792019-04-22 15:53:23 -0400298 return false;
299 }
300 for (int i = 0; i < numSemaphores; ++i) {
Greg Daniel301015c2019-11-18 14:06:46 -0500301 std::unique_ptr<GrSemaphore> sema = fResourceProvider->wrapBackendSemaphore(
Greg Daniel06be0792019-04-22 15:53:23 -0400302 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
303 kAdopt_GrWrapOwnership);
Greg Daniel301015c2019-11-18 14:06:46 -0500304 fGpu->waitSemaphore(sema.get());
Greg Daniel06be0792019-04-22 15:53:23 -0400305 }
306 return true;
307}
308
309////////////////////////////////////////////////////////////////////////////////
310
Greg Daniele8d3cca2020-06-10 10:04:48 -0400311GrSemaphoresSubmitted GrContext::flush(const GrFlushInfo& info) {
Greg Daniel51316782017-08-02 15:10:09 +0000312 ASSERT_SINGLE_OWNER
Robert Phillipsa9162df2019-02-11 14:12:03 -0500313 if (this->abandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400314 if (info.fFinishedProc) {
315 info.fFinishedProc(info.fFinishedContext);
316 }
317 if (info.fSubmittedProc) {
318 info.fSubmittedProc(info.fSubmittedContext, false);
319 }
Robert Phillipsa9162df2019-02-11 14:12:03 -0500320 return GrSemaphoresSubmitted::kNo;
321 }
Greg Daniel51316782017-08-02 15:10:09 +0000322
Greg Daniel04283f32020-05-20 13:16:00 -0400323 bool flushed = this->drawingManager()->flush(
Greg Daniel9efe3862020-06-11 11:51:06 -0400324 nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, info, nullptr);
Greg Danielfe159622020-04-10 17:43:51 +0000325
Greg Daniel04283f32020-05-20 13:16:00 -0400326 if (!flushed || (!this->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
Greg Danielfe159622020-04-10 17:43:51 +0000327 return GrSemaphoresSubmitted::kNo;
328 }
329 return GrSemaphoresSubmitted::kYes;
Greg Daniel51316782017-08-02 15:10:09 +0000330}
331
Greg Daniel04283f32020-05-20 13:16:00 -0400332bool GrContext::submit(bool syncCpu) {
333 ASSERT_SINGLE_OWNER
334 if (this->abandoned()) {
335 return false;
336 }
337
338 if (!fGpu) {
339 return false;
340 }
341
342 return fGpu->submitToGpu(syncCpu);
Greg Danielda50cb82020-05-13 14:07:40 -0400343}
344
Greg Daniela870b462019-01-08 15:49:46 -0500345////////////////////////////////////////////////////////////////////////////////
346
Brian Salomonb0d8b762019-05-06 16:58:22 -0400347void GrContext::checkAsyncWorkCompletion() {
348 if (fGpu) {
349 fGpu->checkFinishProcs();
350 }
351}
352
353////////////////////////////////////////////////////////////////////////////////
354
Greg Daniela870b462019-01-08 15:49:46 -0500355void GrContext::storeVkPipelineCacheData() {
356 if (fGpu) {
357 fGpu->storeVkPipelineCacheData();
358 }
359}
360
361////////////////////////////////////////////////////////////////////////////////
362
Khushal3e7548c2018-05-23 15:45:01 -0700363bool GrContext::supportsDistanceFieldText() const {
Robert Phillipsbb606772019-02-04 17:50:57 -0500364 return this->caps()->shaderCaps()->supportsDistanceFieldText();
Khushal3e7548c2018-05-23 15:45:01 -0700365}
366
bsalomon37f9a262015-02-02 13:00:10 -0800367//////////////////////////////////////////////////////////////////////////////
368
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500369void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800370 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500371 if (maxResources) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400372 *maxResources = -1;
bsalomon37f9a262015-02-02 13:00:10 -0800373 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500374 if (maxResourceBytes) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400375 *maxResourceBytes = this->getResourceCacheLimit();
bsalomon37f9a262015-02-02 13:00:10 -0800376 }
377}
378
Robert Phillipscf39f372019-09-03 10:29:20 -0400379size_t GrContext::getResourceCacheLimit() const {
joshualitt1de610a2016-01-06 08:26:09 -0800380 ASSERT_SINGLE_OWNER
Robert Phillipscf39f372019-09-03 10:29:20 -0400381 return fResourceCache->getMaxResourceBytes();
382}
383
384void GrContext::setResourceCacheLimits(int unused, size_t maxResourceBytes) {
385 ASSERT_SINGLE_OWNER
386 this->setResourceCacheLimit(maxResourceBytes);
387}
388
389void GrContext::setResourceCacheLimit(size_t maxResourceBytes) {
390 ASSERT_SINGLE_OWNER
391 fResourceCache->setLimit(maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800392}
393
ericrk0a5fa482015-09-15 14:16:10 -0700394//////////////////////////////////////////////////////////////////////////////
ericrk0a5fa482015-09-15 14:16:10 -0700395void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800396 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700397 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
Khushal71652e22018-10-29 13:05:36 -0700398 traceMemoryDump->dumpNumericValue("skia/gr_text_blob_cache", "size", "bytes",
Robert Phillips2184fb72019-02-21 16:11:41 -0500399 this->getTextBlobCache()->usedBytes());
ericrk0a5fa482015-09-15 14:16:10 -0700400}
Brian Osman71a18892017-08-10 10:23:25 -0400401
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400402//////////////////////////////////////////////////////////////////////////////
403GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400404 const GrBackendFormat& backendFormat,
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400405 GrMipMapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400406 GrRenderable renderable,
407 GrProtected isProtected) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400408 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400409 if (!this->asDirectContext()) {
410 return GrBackendTexture();
411 }
412
413 if (this->abandoned()) {
414 return GrBackendTexture();
415 }
416
Robert Phillips4277f012020-01-21 14:28:34 -0500417 return fGpu->createBackendTexture({width, height}, backendFormat, renderable,
Greg Daniel16032b32020-05-06 15:31:10 -0400418 mipMapped, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400419}
420
421GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400422 SkColorType skColorType,
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400423 GrMipMapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400424 GrRenderable renderable,
425 GrProtected isProtected) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400426 if (!this->asDirectContext()) {
427 return GrBackendTexture();
428 }
429
430 if (this->abandoned()) {
431 return GrBackendTexture();
432 }
433
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400434 const GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400435
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400436 return this->createBackendTexture(width, height, format, mipMapped, renderable, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400437}
438
Robert Phillips02dc0302019-07-02 17:58:27 -0400439GrBackendTexture GrContext::createBackendTexture(const SkSurfaceCharacterization& c) {
Robert Phillips02dc0302019-07-02 17:58:27 -0400440 if (!this->asDirectContext() || !c.isValid()) {
441 return GrBackendTexture();
442 }
443
444 if (this->abandoned()) {
445 return GrBackendTexture();
446 }
447
448 if (c.usesGLFBO0()) {
449 // If we are making the surface we will never use FBO0.
450 return GrBackendTexture();
451 }
452
453 if (c.vulkanSecondaryCBCompatible()) {
454 return {};
455 }
456
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400457 const GrBackendFormat format = this->defaultBackendFormat(c.colorType(), GrRenderable::kYes);
Robert Phillips02dc0302019-07-02 17:58:27 -0400458 if (!format.isValid()) {
459 return GrBackendTexture();
460 }
461
Robert Phillips02dc0302019-07-02 17:58:27 -0400462 GrBackendTexture result = this->createBackendTexture(c.width(), c.height(), format,
463 GrMipMapped(c.isMipMapped()),
464 GrRenderable::kYes,
Robert Phillips3cd54322019-07-10 09:28:59 -0400465 c.isProtected());
Robert Phillips02dc0302019-07-02 17:58:27 -0400466 SkASSERT(c.isCompatible(result));
467 return result;
468}
469
Greg Daniel25597782020-06-11 13:15:08 -0400470static GrBackendTexture create_and_update_backend_texture(
471 GrContext* context,
472 SkISize dimensions,
473 const GrBackendFormat& backendFormat,
474 GrMipMapped mipMapped,
475 GrRenderable renderable,
476 GrProtected isProtected,
477 sk_sp<GrRefCntedCallback> finishedCallback,
478 const GrGpu::BackendTextureData* data) {
Greg Daniel16032b32020-05-06 15:31:10 -0400479 GrGpu* gpu = context->priv().getGpu();
480
481 GrBackendTexture beTex = gpu->createBackendTexture(dimensions, backendFormat, renderable,
482 mipMapped, isProtected);
483 if (!beTex.isValid()) {
484 return {};
485 }
486
Greg Daniel25597782020-06-11 13:15:08 -0400487 if (!context->priv().getGpu()->updateBackendTexture(beTex, std::move(finishedCallback), data)) {
Greg Daniel16032b32020-05-06 15:31:10 -0400488 context->deleteBackendTexture(beTex);
489 return {};
490 }
491 return beTex;
492}
493
Greg Daniel25597782020-06-11 13:15:08 -0400494
495GrBackendTexture GrContext::createBackendTexture(const SkSurfaceCharacterization& c,
496 const SkColor4f& color,
497 GrGpuFinishedProc finishedProc,
498 GrGpuFinishedContext finishedContext) {
499 sk_sp<GrRefCntedCallback> finishedCallback;
500 if (finishedProc) {
501 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
502 }
503
504 if (!this->asDirectContext() || !c.isValid()) {
505 return {};
506 }
507
508 if (this->abandoned()) {
509 return {};
510 }
511
512 if (c.usesGLFBO0()) {
513 // If we are making the surface we will never use FBO0.
514 return {};
515 }
516
517 if (c.vulkanSecondaryCBCompatible()) {
518 return {};
519 }
520
521 const GrBackendFormat format = this->defaultBackendFormat(c.colorType(), GrRenderable::kYes);
522 if (!format.isValid()) {
523 return {};
524 }
525
526 GrGpu::BackendTextureData data(color);
527 GrBackendTexture result = create_and_update_backend_texture(
528 this, {c.width(), c.height()}, format, GrMipMapped(c.isMipMapped()), GrRenderable::kYes,
529 c.isProtected(), std::move(finishedCallback), &data);
530
531 SkASSERT(c.isCompatible(result));
532 return result;
533}
534
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400535GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400536 const GrBackendFormat& backendFormat,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400537 const SkColor4f& color,
538 GrMipMapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400539 GrRenderable renderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400540 GrProtected isProtected,
541 GrGpuFinishedProc finishedProc,
542 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400543 sk_sp<GrRefCntedCallback> finishedCallback;
544 if (finishedProc) {
545 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
546 }
547
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 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400551 }
552
553 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400554 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,
Greg Daniel25597782020-06-11 13:15:08 -0400559 renderable, isProtected, std::move(finishedCallback),
Greg Daniel16032b32020-05-06 15:31:10 -0400560 &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) {
Greg Daniel25597782020-06-11 13:15:08 -0400571 sk_sp<GrRefCntedCallback> finishedCallback;
572 if (finishedProc) {
573 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
574 }
575
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400576 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400577 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400578 }
579
580 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400581 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400582 }
583
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400584 GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400585 if (!format.isValid()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400586 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
Greg Daniel25597782020-06-11 13:15:08 -0400592 GrGpu::BackendTextureData data(swizzledColor);
593 return create_and_update_backend_texture(this, {width, height}, format, mipMapped,
594 renderable, isProtected, std::move(finishedCallback),
595 &data);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400596}
597
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500598GrBackendTexture GrContext::createBackendTexture(const SkPixmap srcData[], int numProvidedLevels,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400599 GrRenderable renderable, GrProtected isProtected,
600 GrGpuFinishedProc finishedProc,
601 GrGpuFinishedContext finishedContext) {
Robert Phillips66944402019-09-30 13:21:25 -0400602 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
603
Greg Daniel25597782020-06-11 13:15:08 -0400604 sk_sp<GrRefCntedCallback> finishedCallback;
605 if (finishedProc) {
606 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
607 }
608
Robert Phillips66944402019-09-30 13:21:25 -0400609 if (!this->asDirectContext()) {
610 return {};
611 }
612
613 if (this->abandoned()) {
614 return {};
615 }
616
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500617 if (!srcData || numProvidedLevels <= 0) {
Robert Phillips66944402019-09-30 13:21:25 -0400618 return {};
619 }
620
621 int baseWidth = srcData[0].width();
622 int baseHeight = srcData[0].height();
623 SkColorType colorType = srcData[0].colorType();
624
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500625 GrMipMapped mipMapped = GrMipMapped::kNo;
626 int numExpectedLevels = 1;
627 if (numProvidedLevels > 1) {
628 numExpectedLevels = SkMipMap::ComputeLevelCount(baseWidth, baseHeight) + 1;
629 mipMapped = GrMipMapped::kYes;
630 }
631
632 if (numProvidedLevels != numExpectedLevels) {
633 return {};
634 }
635
Robert Phillips66944402019-09-30 13:21:25 -0400636 GrBackendFormat backendFormat = this->defaultBackendFormat(colorType, renderable);
637
Brian Salomon85c3d682019-11-04 15:04:54 -0500638 GrGpu::BackendTextureData data(srcData);
Greg Daniel16032b32020-05-06 15:31:10 -0400639 return create_and_update_backend_texture(this, {baseWidth, baseHeight}, backendFormat,
Greg Daniel25597782020-06-11 13:15:08 -0400640 mipMapped, renderable, isProtected,
641 std::move(finishedCallback), &data);
Robert Phillips66944402019-09-30 13:21:25 -0400642}
643
Greg Danielb2365d82020-05-13 15:32:04 -0400644bool GrContext::updateBackendTexture(const GrBackendTexture& backendTexture,
645 const SkColor4f& color,
646 GrGpuFinishedProc finishedProc,
647 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400648 sk_sp<GrRefCntedCallback> finishedCallback;
649 if (finishedProc) {
650 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
651 }
652
Greg Danielb2365d82020-05-13 15:32:04 -0400653 if (!this->asDirectContext()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400654 return false;
655 }
656
657 if (this->abandoned()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400658 return false;
659 }
660
661 GrGpu::BackendTextureData data(color);
Greg Daniel25597782020-06-11 13:15:08 -0400662 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
Greg Danielb2365d82020-05-13 15:32:04 -0400663}
664
665bool GrContext::updateBackendTexture(const GrBackendTexture& backendTexture,
666 const SkPixmap srcData[],
667 int numLevels,
668 GrGpuFinishedProc finishedProc,
669 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400670 sk_sp<GrRefCntedCallback> finishedCallback;
671 if (finishedProc) {
672 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
673 }
674
Greg Danielb2365d82020-05-13 15:32:04 -0400675 if (!this->asDirectContext()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400676 return false;
677 }
678
679 if (this->abandoned()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400680 return false;
681 }
682
683 if (!srcData || numLevels <= 0) {
Greg Danielb2365d82020-05-13 15:32:04 -0400684 return false;
685 }
686
687 int numExpectedLevels = 1;
688 if (backendTexture.hasMipMaps()) {
689 numExpectedLevels = SkMipMap::ComputeLevelCount(backendTexture.width(),
690 backendTexture.height()) + 1;
691 }
692 if (numLevels != numExpectedLevels) {
Greg Danielb2365d82020-05-13 15:32:04 -0400693 return false;
694 }
695
696 GrGpu::BackendTextureData data(srcData);
Greg Daniel25597782020-06-11 13:15:08 -0400697 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
Greg Danielb2365d82020-05-13 15:32:04 -0400698}
699
Robert Phillipsb915c942019-12-17 14:44:37 -0500700//////////////////////////////////////////////////////////////////////////////
701
702GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
703 const GrBackendFormat& backendFormat,
704 const SkColor4f& color,
705 GrMipMapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400706 GrProtected isProtected,
707 GrGpuFinishedProc finishedProc,
708 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500709 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel25597782020-06-11 13:15:08 -0400710 sk_sp<GrRefCntedCallback> finishedCallback;
711 if (finishedProc) {
712 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
713 }
714
Robert Phillipsb915c942019-12-17 14:44:37 -0500715 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400716 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500717 }
718
719 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400720 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500721 }
722
723 GrGpu::BackendTextureData data(color);
Robert Phillips4277f012020-01-21 14:28:34 -0500724 return fGpu->createCompressedBackendTexture({width, height}, backendFormat,
Greg Daniel25597782020-06-11 13:15:08 -0400725 mipMapped, isProtected, std::move(finishedCallback),
726 &data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500727}
728
729GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
730 SkImage::CompressionType compression,
731 const SkColor4f& color,
732 GrMipMapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400733 GrProtected isProtected,
734 GrGpuFinishedProc finishedProc,
735 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500736 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillipsb915c942019-12-17 14:44:37 -0500737 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);
Greg Daniel25597782020-06-11 13:15:08 -0400752 sk_sp<GrRefCntedCallback> finishedCallback;
753 if (finishedProc) {
754 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
755 }
756
Robert Phillipsb915c942019-12-17 14:44:37 -0500757 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400758 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500759 }
760
761 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400762 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500763 }
764
765 GrGpu::BackendTextureData data(compressedData, dataSize);
Robert Phillips4277f012020-01-21 14:28:34 -0500766 return fGpu->createCompressedBackendTexture({width, height}, backendFormat,
Greg Daniel25597782020-06-11 13:15:08 -0400767 mipMapped, isProtected, std::move(finishedCallback),
768 &data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500769}
770
771GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
772 SkImage::CompressionType compression,
773 const void* data, size_t dataSize,
774 GrMipMapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400775 GrProtected isProtected,
776 GrGpuFinishedProc finishedProc,
777 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500778 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillipsb915c942019-12-17 14:44:37 -0500779 GrBackendFormat format = this->compressedBackendFormat(compression);
Greg Daniel25597782020-06-11 13:15:08 -0400780 return this->createCompressedBackendTexture(width, height, format, data, dataSize, mipMapped,
781 isProtected, finishedProc, finishedContext);
Robert Phillipsb915c942019-12-17 14:44:37 -0500782}
783
Greg Daniel1db8e792020-06-09 17:29:32 -0400784bool GrContext::setBackendTextureState(const GrBackendTexture& backendTexture,
785 const GrBackendSurfaceMutableState& state,
786 GrGpuFinishedProc finishedProc,
787 GrGpuFinishedContext finishedContext) {
Greg Daniel1db8e792020-06-09 17:29:32 -0400788 sk_sp<GrRefCntedCallback> callback;
789 if (finishedProc) {
790 callback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
791 }
Greg Daniel25597782020-06-11 13:15:08 -0400792
793 if (!this->asDirectContext()) {
794 return false;
795 }
796
797 if (this->abandoned()) {
798 return false;
799 }
800
Greg Daniel1db8e792020-06-09 17:29:32 -0400801 return fGpu->setBackendTextureState(backendTexture, state, std::move(callback));
802}
803
804bool GrContext::setBackendRenderTargetState(const GrBackendRenderTarget& backendRenderTarget,
805 const GrBackendSurfaceMutableState& state,
806 GrGpuFinishedProc finishedProc,
807 GrGpuFinishedContext finishedContext) {
Greg Daniel1db8e792020-06-09 17:29:32 -0400808 sk_sp<GrRefCntedCallback> callback;
809 if (finishedProc) {
810 callback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
811 }
Greg Daniel25597782020-06-11 13:15:08 -0400812
813 if (!this->asDirectContext()) {
814 return false;
815 }
816
817 if (this->abandoned()) {
818 return false;
819 }
820
Greg Daniel1db8e792020-06-09 17:29:32 -0400821 return fGpu->setBackendRenderTargetState(backendRenderTarget, state, std::move(callback));
822}
823
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400824void GrContext::deleteBackendTexture(GrBackendTexture backendTex) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400825 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danielf0e04f02019-12-04 15:17:54 -0500826 // For the Vulkan backend we still must destroy the backend texture when the context is
827 // abandoned.
828 if ((this->abandoned() && this->backend() != GrBackendApi::kVulkan) || !backendTex.isValid()) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400829 return;
830 }
831
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400832 fGpu->deleteBackendTexture(backendTex);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400833}
834
Greg Daniel1db8e792020-06-09 17:29:32 -0400835//////////////////////////////////////////////////////////////////////////////
836
Brian Osmaned58e002019-09-06 14:42:43 -0400837bool GrContext::precompileShader(const SkData& key, const SkData& data) {
838 return fGpu->precompileShader(key, data);
839}
840
Brian Salomonec22b1a2019-08-09 09:41:48 -0400841#ifdef SK_ENABLE_DUMP_GPU
Michael Ludwigdd205452020-03-30 17:16:34 -0400842#include "include/core/SkString.h"
Brian Salomonec22b1a2019-08-09 09:41:48 -0400843#include "src/utils/SkJSONWriter.h"
844SkString GrContext::dump() const {
845 SkDynamicMemoryWStream stream;
846 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
847 writer.beginObject();
848
849 writer.appendString("backend", GrBackendApiToStr(this->backend()));
850
851 writer.appendName("caps");
852 this->caps()->dumpJSON(&writer);
853
854 writer.appendName("gpu");
855 this->fGpu->dumpJSON(&writer);
856
Robert Phillips273f1072020-05-05 13:03:07 -0400857 writer.appendName("context");
858 this->dumpJSON(&writer);
859
Brian Salomonec22b1a2019-08-09 09:41:48 -0400860 // Flush JSON to the memory stream
861 writer.endObject();
862 writer.flush();
863
864 // Null terminate the JSON data in the memory stream
865 stream.write8(0);
866
867 // Allocate a string big enough to hold all the data, then copy out of the stream
868 SkString result(stream.bytesWritten());
869 stream.copyToAndReset(result.writable_str());
870 return result;
871}
872#endif