blob: c7c7162d9cdb935c0f9a3fc3743f58686915c40f [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"
Herb Derbya08bde62020-06-12 15:46:06 -040033#include "src/gpu/text/GrSDFTOptions.h"
Robert Phillips41bd97d2020-04-07 14:19:37 -040034#include "src/gpu/text/GrStrikeCache.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/gpu/text/GrTextBlobCache.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
Adlai Holler65888b82020-06-22 14:06:55 -040059 if (this->drawingManager()) {
60 this->drawingManager()->cleanup();
61 }
Greg Daniel5ed3c112020-06-18 15:59:17 -040062 fMappedBufferManager.reset();
Robert Phillips292a6b22019-02-14 14:49:02 -050063 delete fResourceProvider;
64 delete fResourceCache;
Robert Phillips292a6b22019-02-14 14:49:02 -050065}
66
Adlai Hollere219d1c2020-06-02 11:23:16 -040067bool GrContext::init() {
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
Adlai Hollere219d1c2020-06-02 11:23:16 -040071 if (!INHERITED::init()) {
Robert Phillipsbb606772019-02-04 17:50:57 -050072 return false;
73 }
74
Robert Phillips2184fb72019-02-21 16:11:41 -050075 SkASSERT(this->getTextBlobCache());
Robert Phillipsbb606772019-02-04 17:50:57 -050076
Robert Phillips88260b52018-01-19 12:56:09 -050077 if (fGpu) {
Robert Phillips4d932d12020-04-09 08:58:52 -040078 fStrikeCache.reset(new GrStrikeCache{});
Robert Phillipsa41c6852019-02-07 10:44:10 -050079 fResourceCache = new GrResourceCache(this->caps(), this->singleOwner(), this->contextID());
Robert Phillips12c46292019-04-23 07:36:17 -040080 fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, this->singleOwner());
Mike Kleinf46d5ca2019-12-11 10:45:01 -050081 fMappedBufferManager = std::make_unique<GrClientMappedBufferManager>(this->contextID());
Robert Phillips88260b52018-01-19 12:56:09 -050082 }
83
Robert Phillips88260b52018-01-19 12:56:09 -050084 if (fResourceCache) {
Robert Phillipsa41c6852019-02-07 10:44:10 -050085 fResourceCache->setProxyProvider(this->proxyProvider());
Robert Phillips88260b52018-01-19 12:56:09 -050086 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050087
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000088 fDidTestPMConversions = false;
89
Robert Phillipsfde6fa02018-03-02 08:53:14 -050090 // DDL TODO: we need to think through how the task group & persistent cache
91 // get passed on to/shared between all the DDLRecorders created with this context.
Robert Phillipsc1541ae2019-02-04 12:05:37 -050092 if (this->options().fExecutor) {
Mike Kleinf46d5ca2019-12-11 10:45:01 -050093 fTaskGroup = std::make_unique<SkTaskGroup>(*this->options().fExecutor);
Brian Osman51279982017-08-23 10:12:00 -040094 }
95
Robert Phillipsc1541ae2019-02-04 12:05:37 -050096 fPersistentCache = this->options().fPersistentCache;
Brian Osman5e7fbfd2019-05-03 13:13:35 -040097 fShaderErrorHandler = this->options().fShaderErrorHandler;
98 if (!fShaderErrorHandler) {
99 fShaderErrorHandler = GrShaderUtils::DefaultShaderErrorHandler();
100 }
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400101
Brian Salomon91a3e522017-06-23 10:58:19 -0400102 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000103}
104
Robert Phillips4217ea72019-01-30 13:08:28 -0500105sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
Adlai Hollere219d1c2020-06-02 11:23:16 -0400106 return INHERITED::threadSafeProxy();
Robert Phillips4217ea72019-01-30 13:08:28 -0500107}
108
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400109//////////////////////////////////////////////////////////////////////////////
110
bsalomon2354f842014-07-28 13:48:36 -0700111void GrContext::abandonContext() {
Greg Daniel6e35a002020-04-01 13:29:59 -0400112 if (INHERITED::abandoned()) {
Robert Phillipsa9162df2019-02-11 14:12:03 -0500113 return;
114 }
joshualitt1de610a2016-01-06 08:26:09 -0800115
Robert Phillipsa9162df2019-02-11 14:12:03 -0500116 INHERITED::abandonContext();
117
Robert Phillips4d932d12020-04-09 08:58:52 -0400118 fStrikeCache->freeAll();
119
Brian Salomon9241a6d2019-10-03 13:26:54 -0400120 fMappedBufferManager->abandon();
121
bsalomond309e7a2015-04-30 14:18:54 -0700122 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800123
Adlai Holler65888b82020-06-22 14:06:55 -0400124 // Need to cleanup the drawing manager first so all the render targets
125 // will be released/forgotten before they too are abandoned.
126 this->drawingManager()->cleanup();
127
bsalomon@google.com205d4602011-04-25 12:43:45 +0000128 // abandon first to so destructors
129 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800130 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700131
bsalomon6e2aad42016-04-01 11:54:31 -0700132 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400133
134 fMappedBufferManager.reset();
Khushalc421ca12018-06-26 14:38:34 -0700135}
136
bsalomon6e2aad42016-04-01 11:54:31 -0700137void GrContext::releaseResourcesAndAbandonContext() {
Greg Daniel6e35a002020-04-01 13:29:59 -0400138 if (INHERITED::abandoned()) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500139 return;
140 }
Robert Phillipsa9162df2019-02-11 14:12:03 -0500141
142 INHERITED::abandonContext();
143
Brian Salomon9241a6d2019-10-03 13:26:54 -0400144 fMappedBufferManager.reset();
145
bsalomon6e2aad42016-04-01 11:54:31 -0700146 fResourceProvider->abandon();
147
Adlai Holler65888b82020-06-22 14:06:55 -0400148 // Need to cleanup the drawing manager first so all the render targets
149 // will be released/forgotten before they too are abandoned.
150 this->drawingManager()->cleanup();
151
bsalomon6e2aad42016-04-01 11:54:31 -0700152 // Release all resources in the backend 3D API.
153 fResourceCache->releaseAll();
154
155 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000156}
157
Greg Daniel6e35a002020-04-01 13:29:59 -0400158bool GrContext::abandoned() {
159 if (INHERITED::abandoned()) {
160 return true;
161 }
162
163 if (fGpu && fGpu->isDeviceLost()) {
164 this->abandonContext();
165 return true;
166 }
167 return false;
168}
169
Brian Salomon24069eb2020-06-24 10:19:52 -0400170bool GrContext::oomed() { return fGpu ? fGpu->checkAndResetOOMed() : false; }
171
Brian Salomon1f05d452019-02-08 12:33:08 -0500172void GrContext::resetGLTextureBindings() {
173 if (this->abandoned() || this->backend() != GrBackendApi::kOpenGL) {
174 return;
175 }
176 fGpu->resetTextureBindings();
177}
178
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000179void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800180 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000181 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000182}
183
184void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800185 ASSERT_SINGLE_OWNER
186
Adlai Holler9d8a41c2020-06-25 14:01:58 +0000187 if (this->abandoned()) {
188 return;
189 }
190
Robert Phillips2184fb72019-02-21 16:11:41 -0500191 // TODO: the glyph cache doesn't hold any GpuResources so this call should not be needed here.
192 // Some slack in the GrTextBlob's implementation requires it though. That could be fixed.
Robert Phillips4d932d12020-04-09 08:58:52 -0400193 fStrikeCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700194
Robert Phillips6a6de562019-02-15 15:19:15 -0500195 this->drawingManager()->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700196
197 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000198}
199
Robert Phillips6eba0632018-03-28 12:25:42 -0400200void GrContext::purgeUnlockedResources(bool scratchResourcesOnly) {
201 ASSERT_SINGLE_OWNER
Robert Phillipsddc21482019-10-16 14:30:09 -0400202
203 if (this->abandoned()) {
204 return;
205 }
206
Robert Phillips6eba0632018-03-28 12:25:42 -0400207 fResourceCache->purgeUnlockedResources(scratchResourcesOnly);
208 fResourceCache->purgeAsNeeded();
Robert Phillips2184fb72019-02-21 16:11:41 -0500209
210 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
211 // place to purge stale blobs
212 this->getTextBlobCache()->purgeStaleBlobs();
Robert Phillips6eba0632018-03-28 12:25:42 -0400213}
214
Jim Van Verth76d917c2017-12-13 09:26:37 -0500215void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Yuqian Li40aa85f2019-07-02 13:45:00 -0700216 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
217
Brian Salomon5e150852017-03-22 14:53:13 -0400218 ASSERT_SINGLE_OWNER
Chris Dalton6c3879d2018-11-01 11:13:19 -0600219
Brian Salomon9241a6d2019-10-03 13:26:54 -0400220 if (this->abandoned()) {
221 return;
222 }
223
224 fMappedBufferManager->process();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600225 auto purgeTime = GrStdSteadyClock::now() - msNotUsed;
226
Jim Van Verth76d917c2017-12-13 09:26:37 -0500227 fResourceCache->purgeAsNeeded();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600228 fResourceCache->purgeResourcesNotUsedSince(purgeTime);
229
Robert Phillips6a6de562019-02-15 15:19:15 -0500230 if (auto ccpr = this->drawingManager()->getCoverageCountingPathRenderer()) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500231 ccpr->purgeCacheEntriesOlderThan(this->proxyProvider(), purgeTime);
Chris Dalton6c3879d2018-11-01 11:13:19 -0600232 }
Jim Van Verth76d917c2017-12-13 09:26:37 -0500233
Robert Phillips2184fb72019-02-21 16:11:41 -0500234 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
235 // place to purge stale blobs
236 this->getTextBlobCache()->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400237}
238
Derek Sollenberger5480a182017-05-25 16:43:59 -0400239void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
240 ASSERT_SINGLE_OWNER
Robert Phillipsddc21482019-10-16 14:30:09 -0400241
242 if (this->abandoned()) {
243 return;
244 }
245
Derek Sollenberger5480a182017-05-25 16:43:59 -0400246 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
247}
248
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000249void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800250 ASSERT_SINGLE_OWNER
251
bsalomon71cb0c22014-11-14 12:10:14 -0800252 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800253 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800254 }
255 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800256 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800257 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000258}
259
Derek Sollenbergeree479142017-05-24 11:41:33 -0400260size_t GrContext::getResourceCachePurgeableBytes() const {
261 ASSERT_SINGLE_OWNER
262 return fResourceCache->getPurgeableBytes();
263}
264
Greg Daniel8b666172019-10-09 12:38:22 -0400265size_t GrContext::ComputeImageSize(sk_sp<SkImage> image, GrMipMapped mipMapped, bool useNextPow2) {
266 if (!image->isTextureBacked()) {
267 return 0;
268 }
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400269 SkImage_GpuBase* gpuImage = static_cast<SkImage_GpuBase*>(as_IB(image.get()));
270 GrTextureProxy* proxy = gpuImage->peekProxy();
271 if (!proxy) {
272 return 0;
273 }
274
275 const GrCaps& caps = *gpuImage->context()->priv().caps();
Greg Daniel8b666172019-10-09 12:38:22 -0400276 int colorSamplesPerPixel = 1;
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400277 return GrSurface::ComputeSize(caps, proxy->backendFormat(), image->dimensions(),
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400278 colorSamplesPerPixel, mipMapped, useNextPow2);
Greg Daniel8b666172019-10-09 12:38:22 -0400279}
280
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000281////////////////////////////////////////////////////////////////////////////////
282
Robert Phillipsbb606772019-02-04 17:50:57 -0500283int GrContext::maxTextureSize() const { return this->caps()->maxTextureSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400284
Robert Phillipsbb606772019-02-04 17:50:57 -0500285int GrContext::maxRenderTargetSize() const { return this->caps()->maxRenderTargetSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400286
Brian Salomonbdecacf2018-02-02 20:32:49 -0500287bool GrContext::colorTypeSupportedAsImage(SkColorType colorType) const {
Greg Daniel7bfc9132019-08-14 14:23:53 -0400288 GrBackendFormat format =
289 this->caps()->getDefaultBackendFormat(SkColorTypeToGrColorType(colorType),
290 GrRenderable::kNo);
291 return format.isValid();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500292}
293
294int GrContext::maxSurfaceSampleCountForColorType(SkColorType colorType) const {
Greg Danieleadfac92019-08-02 09:03:53 -0400295 GrBackendFormat format =
296 this->caps()->getDefaultBackendFormat(SkColorTypeToGrColorType(colorType),
297 GrRenderable::kYes);
298 return this->caps()->maxRenderTargetSampleCount(format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500299}
300
301////////////////////////////////////////////////////////////////////////////////
302
Greg Daniel06be0792019-04-22 15:53:23 -0400303bool GrContext::wait(int numSemaphores, const GrBackendSemaphore waitSemaphores[]) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400304 if (!fGpu || fGpu->caps()->semaphoreSupport()) {
Greg Daniel06be0792019-04-22 15:53:23 -0400305 return false;
306 }
307 for (int i = 0; i < numSemaphores; ++i) {
Greg Daniel301015c2019-11-18 14:06:46 -0500308 std::unique_ptr<GrSemaphore> sema = fResourceProvider->wrapBackendSemaphore(
Greg Daniel06be0792019-04-22 15:53:23 -0400309 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
310 kAdopt_GrWrapOwnership);
Greg Daniel301015c2019-11-18 14:06:46 -0500311 fGpu->waitSemaphore(sema.get());
Greg Daniel06be0792019-04-22 15:53:23 -0400312 }
313 return true;
314}
315
316////////////////////////////////////////////////////////////////////////////////
317
Greg Daniele8d3cca2020-06-10 10:04:48 -0400318GrSemaphoresSubmitted GrContext::flush(const GrFlushInfo& info) {
Greg Daniel51316782017-08-02 15:10:09 +0000319 ASSERT_SINGLE_OWNER
Robert Phillipsa9162df2019-02-11 14:12:03 -0500320 if (this->abandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400321 if (info.fFinishedProc) {
322 info.fFinishedProc(info.fFinishedContext);
323 }
324 if (info.fSubmittedProc) {
325 info.fSubmittedProc(info.fSubmittedContext, false);
326 }
Robert Phillipsa9162df2019-02-11 14:12:03 -0500327 return GrSemaphoresSubmitted::kNo;
328 }
Greg Daniel51316782017-08-02 15:10:09 +0000329
Greg Daniel04283f32020-05-20 13:16:00 -0400330 bool flushed = this->drawingManager()->flush(
Greg Daniel9efe3862020-06-11 11:51:06 -0400331 nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, info, nullptr);
Greg Danielfe159622020-04-10 17:43:51 +0000332
Greg Daniel04283f32020-05-20 13:16:00 -0400333 if (!flushed || (!this->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
Greg Danielfe159622020-04-10 17:43:51 +0000334 return GrSemaphoresSubmitted::kNo;
335 }
336 return GrSemaphoresSubmitted::kYes;
Greg Daniel51316782017-08-02 15:10:09 +0000337}
338
Greg Daniel04283f32020-05-20 13:16:00 -0400339bool GrContext::submit(bool syncCpu) {
340 ASSERT_SINGLE_OWNER
341 if (this->abandoned()) {
342 return false;
343 }
344
345 if (!fGpu) {
346 return false;
347 }
348
349 return fGpu->submitToGpu(syncCpu);
Greg Danielda50cb82020-05-13 14:07:40 -0400350}
351
Greg Daniela870b462019-01-08 15:49:46 -0500352////////////////////////////////////////////////////////////////////////////////
353
Brian Salomonb0d8b762019-05-06 16:58:22 -0400354void GrContext::checkAsyncWorkCompletion() {
355 if (fGpu) {
356 fGpu->checkFinishProcs();
357 }
358}
359
360////////////////////////////////////////////////////////////////////////////////
361
Greg Daniela870b462019-01-08 15:49:46 -0500362void GrContext::storeVkPipelineCacheData() {
363 if (fGpu) {
364 fGpu->storeVkPipelineCacheData();
365 }
366}
367
368////////////////////////////////////////////////////////////////////////////////
369
Khushal3e7548c2018-05-23 15:45:01 -0700370bool GrContext::supportsDistanceFieldText() const {
Robert Phillipsbb606772019-02-04 17:50:57 -0500371 return this->caps()->shaderCaps()->supportsDistanceFieldText();
Khushal3e7548c2018-05-23 15:45:01 -0700372}
373
bsalomon37f9a262015-02-02 13:00:10 -0800374//////////////////////////////////////////////////////////////////////////////
375
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500376void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800377 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500378 if (maxResources) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400379 *maxResources = -1;
bsalomon37f9a262015-02-02 13:00:10 -0800380 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500381 if (maxResourceBytes) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400382 *maxResourceBytes = this->getResourceCacheLimit();
bsalomon37f9a262015-02-02 13:00:10 -0800383 }
384}
385
Robert Phillipscf39f372019-09-03 10:29:20 -0400386size_t GrContext::getResourceCacheLimit() const {
joshualitt1de610a2016-01-06 08:26:09 -0800387 ASSERT_SINGLE_OWNER
Robert Phillipscf39f372019-09-03 10:29:20 -0400388 return fResourceCache->getMaxResourceBytes();
389}
390
391void GrContext::setResourceCacheLimits(int unused, size_t maxResourceBytes) {
392 ASSERT_SINGLE_OWNER
393 this->setResourceCacheLimit(maxResourceBytes);
394}
395
396void GrContext::setResourceCacheLimit(size_t maxResourceBytes) {
397 ASSERT_SINGLE_OWNER
398 fResourceCache->setLimit(maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800399}
400
ericrk0a5fa482015-09-15 14:16:10 -0700401//////////////////////////////////////////////////////////////////////////////
ericrk0a5fa482015-09-15 14:16:10 -0700402void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800403 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700404 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
Khushal71652e22018-10-29 13:05:36 -0700405 traceMemoryDump->dumpNumericValue("skia/gr_text_blob_cache", "size", "bytes",
Robert Phillips2184fb72019-02-21 16:11:41 -0500406 this->getTextBlobCache()->usedBytes());
ericrk0a5fa482015-09-15 14:16:10 -0700407}
Brian Osman71a18892017-08-10 10:23:25 -0400408
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400409//////////////////////////////////////////////////////////////////////////////
410GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400411 const GrBackendFormat& backendFormat,
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400412 GrMipMapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400413 GrRenderable renderable,
414 GrProtected isProtected) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400415 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400416 if (!this->asDirectContext()) {
417 return GrBackendTexture();
418 }
419
420 if (this->abandoned()) {
421 return GrBackendTexture();
422 }
423
Robert Phillips4277f012020-01-21 14:28:34 -0500424 return fGpu->createBackendTexture({width, height}, backendFormat, renderable,
Greg Daniel16032b32020-05-06 15:31:10 -0400425 mipMapped, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400426}
427
428GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400429 SkColorType skColorType,
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400430 GrMipMapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400431 GrRenderable renderable,
432 GrProtected isProtected) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400433 if (!this->asDirectContext()) {
434 return GrBackendTexture();
435 }
436
437 if (this->abandoned()) {
438 return GrBackendTexture();
439 }
440
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400441 const GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400442
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400443 return this->createBackendTexture(width, height, format, mipMapped, renderable, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400444}
445
Robert Phillips02dc0302019-07-02 17:58:27 -0400446GrBackendTexture GrContext::createBackendTexture(const SkSurfaceCharacterization& c) {
Robert Phillips02dc0302019-07-02 17:58:27 -0400447 if (!this->asDirectContext() || !c.isValid()) {
448 return GrBackendTexture();
449 }
450
451 if (this->abandoned()) {
452 return GrBackendTexture();
453 }
454
455 if (c.usesGLFBO0()) {
456 // If we are making the surface we will never use FBO0.
457 return GrBackendTexture();
458 }
459
460 if (c.vulkanSecondaryCBCompatible()) {
461 return {};
462 }
463
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400464 const GrBackendFormat format = this->defaultBackendFormat(c.colorType(), GrRenderable::kYes);
Robert Phillips02dc0302019-07-02 17:58:27 -0400465 if (!format.isValid()) {
466 return GrBackendTexture();
467 }
468
Robert Phillips02dc0302019-07-02 17:58:27 -0400469 GrBackendTexture result = this->createBackendTexture(c.width(), c.height(), format,
470 GrMipMapped(c.isMipMapped()),
471 GrRenderable::kYes,
Robert Phillips3cd54322019-07-10 09:28:59 -0400472 c.isProtected());
Robert Phillips02dc0302019-07-02 17:58:27 -0400473 SkASSERT(c.isCompatible(result));
474 return result;
475}
476
Greg Daniel25597782020-06-11 13:15:08 -0400477static GrBackendTexture create_and_update_backend_texture(
478 GrContext* context,
479 SkISize dimensions,
480 const GrBackendFormat& backendFormat,
481 GrMipMapped mipMapped,
482 GrRenderable renderable,
483 GrProtected isProtected,
484 sk_sp<GrRefCntedCallback> finishedCallback,
485 const GrGpu::BackendTextureData* data) {
Greg Daniel16032b32020-05-06 15:31:10 -0400486 GrGpu* gpu = context->priv().getGpu();
487
488 GrBackendTexture beTex = gpu->createBackendTexture(dimensions, backendFormat, renderable,
489 mipMapped, isProtected);
490 if (!beTex.isValid()) {
491 return {};
492 }
493
Greg Daniel25597782020-06-11 13:15:08 -0400494 if (!context->priv().getGpu()->updateBackendTexture(beTex, std::move(finishedCallback), data)) {
Greg Daniel16032b32020-05-06 15:31:10 -0400495 context->deleteBackendTexture(beTex);
496 return {};
497 }
498 return beTex;
499}
500
Greg Daniel25597782020-06-11 13:15:08 -0400501
502GrBackendTexture GrContext::createBackendTexture(const SkSurfaceCharacterization& c,
503 const SkColor4f& color,
504 GrGpuFinishedProc finishedProc,
505 GrGpuFinishedContext finishedContext) {
506 sk_sp<GrRefCntedCallback> finishedCallback;
507 if (finishedProc) {
508 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
509 }
510
511 if (!this->asDirectContext() || !c.isValid()) {
512 return {};
513 }
514
515 if (this->abandoned()) {
516 return {};
517 }
518
519 if (c.usesGLFBO0()) {
520 // If we are making the surface we will never use FBO0.
521 return {};
522 }
523
524 if (c.vulkanSecondaryCBCompatible()) {
525 return {};
526 }
527
528 const GrBackendFormat format = this->defaultBackendFormat(c.colorType(), GrRenderable::kYes);
529 if (!format.isValid()) {
530 return {};
531 }
532
533 GrGpu::BackendTextureData data(color);
534 GrBackendTexture result = create_and_update_backend_texture(
535 this, {c.width(), c.height()}, format, GrMipMapped(c.isMipMapped()), GrRenderable::kYes,
536 c.isProtected(), std::move(finishedCallback), &data);
537
538 SkASSERT(c.isCompatible(result));
539 return result;
540}
541
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400542GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400543 const GrBackendFormat& backendFormat,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400544 const SkColor4f& color,
545 GrMipMapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400546 GrRenderable renderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400547 GrProtected isProtected,
548 GrGpuFinishedProc finishedProc,
549 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400550 sk_sp<GrRefCntedCallback> finishedCallback;
551 if (finishedProc) {
552 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
553 }
554
Brian Salomonc42eb662019-06-24 17:13:00 -0400555 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400556 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400557 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400558 }
559
560 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400561 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400562 }
563
Brian Salomon85c3d682019-11-04 15:04:54 -0500564 GrGpu::BackendTextureData data(color);
Greg Daniel16032b32020-05-06 15:31:10 -0400565 return create_and_update_backend_texture(this, {width, height}, backendFormat, mipMapped,
Greg Daniel25597782020-06-11 13:15:08 -0400566 renderable, isProtected, std::move(finishedCallback),
Greg Daniel16032b32020-05-06 15:31:10 -0400567 &data);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400568}
569
570GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400571 SkColorType skColorType,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400572 const SkColor4f& color,
573 GrMipMapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400574 GrRenderable renderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400575 GrProtected isProtected,
576 GrGpuFinishedProc finishedProc,
577 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400578 sk_sp<GrRefCntedCallback> finishedCallback;
579 if (finishedProc) {
580 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
581 }
582
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400583 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400584 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400585 }
586
587 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400588 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400589 }
590
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400591 GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400592 if (!format.isValid()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400593 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400594 }
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400595
596 GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
Brian Salomon982f5462020-03-30 12:52:33 -0400597 SkColor4f swizzledColor = this->caps()->getWriteSwizzle(format, grColorType).applyTo(color);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400598
Greg Daniel25597782020-06-11 13:15:08 -0400599 GrGpu::BackendTextureData data(swizzledColor);
600 return create_and_update_backend_texture(this, {width, height}, format, mipMapped,
601 renderable, isProtected, std::move(finishedCallback),
602 &data);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400603}
604
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500605GrBackendTexture GrContext::createBackendTexture(const SkPixmap srcData[], int numProvidedLevels,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400606 GrRenderable renderable, GrProtected isProtected,
607 GrGpuFinishedProc finishedProc,
608 GrGpuFinishedContext finishedContext) {
Robert Phillips66944402019-09-30 13:21:25 -0400609 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
610
Greg Daniel25597782020-06-11 13:15:08 -0400611 sk_sp<GrRefCntedCallback> finishedCallback;
612 if (finishedProc) {
613 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
614 }
615
Robert Phillips66944402019-09-30 13:21:25 -0400616 if (!this->asDirectContext()) {
617 return {};
618 }
619
620 if (this->abandoned()) {
621 return {};
622 }
623
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500624 if (!srcData || numProvidedLevels <= 0) {
Robert Phillips66944402019-09-30 13:21:25 -0400625 return {};
626 }
627
628 int baseWidth = srcData[0].width();
629 int baseHeight = srcData[0].height();
630 SkColorType colorType = srcData[0].colorType();
631
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500632 GrMipMapped mipMapped = GrMipMapped::kNo;
633 int numExpectedLevels = 1;
634 if (numProvidedLevels > 1) {
635 numExpectedLevels = SkMipMap::ComputeLevelCount(baseWidth, baseHeight) + 1;
636 mipMapped = GrMipMapped::kYes;
637 }
638
639 if (numProvidedLevels != numExpectedLevels) {
640 return {};
641 }
642
Robert Phillips66944402019-09-30 13:21:25 -0400643 GrBackendFormat backendFormat = this->defaultBackendFormat(colorType, renderable);
644
Brian Salomon85c3d682019-11-04 15:04:54 -0500645 GrGpu::BackendTextureData data(srcData);
Greg Daniel16032b32020-05-06 15:31:10 -0400646 return create_and_update_backend_texture(this, {baseWidth, baseHeight}, backendFormat,
Greg Daniel25597782020-06-11 13:15:08 -0400647 mipMapped, renderable, isProtected,
648 std::move(finishedCallback), &data);
Robert Phillips66944402019-09-30 13:21:25 -0400649}
650
Greg Danielb2365d82020-05-13 15:32:04 -0400651bool GrContext::updateBackendTexture(const GrBackendTexture& backendTexture,
652 const SkColor4f& color,
653 GrGpuFinishedProc finishedProc,
654 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400655 sk_sp<GrRefCntedCallback> finishedCallback;
656 if (finishedProc) {
657 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
658 }
659
Greg Danielb2365d82020-05-13 15:32:04 -0400660 if (!this->asDirectContext()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400661 return false;
662 }
663
664 if (this->abandoned()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400665 return false;
666 }
667
668 GrGpu::BackendTextureData data(color);
Greg Daniel25597782020-06-11 13:15:08 -0400669 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
Greg Danielb2365d82020-05-13 15:32:04 -0400670}
671
672bool GrContext::updateBackendTexture(const GrBackendTexture& backendTexture,
673 const SkPixmap srcData[],
674 int numLevels,
675 GrGpuFinishedProc finishedProc,
676 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400677 sk_sp<GrRefCntedCallback> finishedCallback;
678 if (finishedProc) {
679 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
680 }
681
Greg Danielb2365d82020-05-13 15:32:04 -0400682 if (!this->asDirectContext()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400683 return false;
684 }
685
686 if (this->abandoned()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400687 return false;
688 }
689
690 if (!srcData || numLevels <= 0) {
Greg Danielb2365d82020-05-13 15:32:04 -0400691 return false;
692 }
693
694 int numExpectedLevels = 1;
695 if (backendTexture.hasMipMaps()) {
696 numExpectedLevels = SkMipMap::ComputeLevelCount(backendTexture.width(),
697 backendTexture.height()) + 1;
698 }
699 if (numLevels != numExpectedLevels) {
Greg Danielb2365d82020-05-13 15:32:04 -0400700 return false;
701 }
702
703 GrGpu::BackendTextureData data(srcData);
Greg Daniel25597782020-06-11 13:15:08 -0400704 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
Greg Danielb2365d82020-05-13 15:32:04 -0400705}
706
Robert Phillipsb915c942019-12-17 14:44:37 -0500707//////////////////////////////////////////////////////////////////////////////
708
709GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
710 const GrBackendFormat& backendFormat,
711 const SkColor4f& color,
712 GrMipMapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400713 GrProtected isProtected,
714 GrGpuFinishedProc finishedProc,
715 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500716 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel25597782020-06-11 13:15:08 -0400717 sk_sp<GrRefCntedCallback> finishedCallback;
718 if (finishedProc) {
719 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
720 }
721
Robert Phillipsb915c942019-12-17 14:44:37 -0500722 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400723 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500724 }
725
726 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400727 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500728 }
729
730 GrGpu::BackendTextureData data(color);
Robert Phillips4277f012020-01-21 14:28:34 -0500731 return fGpu->createCompressedBackendTexture({width, height}, backendFormat,
Greg Daniel25597782020-06-11 13:15:08 -0400732 mipMapped, isProtected, std::move(finishedCallback),
733 &data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500734}
735
736GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
737 SkImage::CompressionType compression,
738 const SkColor4f& color,
739 GrMipMapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400740 GrProtected isProtected,
741 GrGpuFinishedProc finishedProc,
742 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500743 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillipsb915c942019-12-17 14:44:37 -0500744 GrBackendFormat format = this->compressedBackendFormat(compression);
745 return this->createCompressedBackendTexture(width, height, format, color,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400746 mipMapped, isProtected, finishedProc,
747 finishedContext);
Robert Phillipsb915c942019-12-17 14:44:37 -0500748}
749
750GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
751 const GrBackendFormat& backendFormat,
752 const void* compressedData,
753 size_t dataSize,
754 GrMipMapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400755 GrProtected isProtected,
756 GrGpuFinishedProc finishedProc,
757 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500758 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel25597782020-06-11 13:15:08 -0400759 sk_sp<GrRefCntedCallback> finishedCallback;
760 if (finishedProc) {
761 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
762 }
763
Robert Phillipsb915c942019-12-17 14:44:37 -0500764 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400765 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500766 }
767
768 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400769 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500770 }
771
772 GrGpu::BackendTextureData data(compressedData, dataSize);
Robert Phillips4277f012020-01-21 14:28:34 -0500773 return fGpu->createCompressedBackendTexture({width, height}, backendFormat,
Greg Daniel25597782020-06-11 13:15:08 -0400774 mipMapped, isProtected, std::move(finishedCallback),
775 &data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500776}
777
778GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
779 SkImage::CompressionType compression,
780 const void* data, size_t dataSize,
781 GrMipMapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400782 GrProtected isProtected,
783 GrGpuFinishedProc finishedProc,
784 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500785 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillipsb915c942019-12-17 14:44:37 -0500786 GrBackendFormat format = this->compressedBackendFormat(compression);
Greg Daniel25597782020-06-11 13:15:08 -0400787 return this->createCompressedBackendTexture(width, height, format, data, dataSize, mipMapped,
788 isProtected, finishedProc, finishedContext);
Robert Phillipsb915c942019-12-17 14:44:37 -0500789}
790
Greg Daniel1db8e792020-06-09 17:29:32 -0400791bool GrContext::setBackendTextureState(const GrBackendTexture& backendTexture,
792 const GrBackendSurfaceMutableState& state,
793 GrGpuFinishedProc finishedProc,
794 GrGpuFinishedContext finishedContext) {
Greg Daniel1db8e792020-06-09 17:29:32 -0400795 sk_sp<GrRefCntedCallback> callback;
796 if (finishedProc) {
797 callback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
798 }
Greg Daniel25597782020-06-11 13:15:08 -0400799
800 if (!this->asDirectContext()) {
801 return false;
802 }
803
804 if (this->abandoned()) {
805 return false;
806 }
807
Greg Daniel1db8e792020-06-09 17:29:32 -0400808 return fGpu->setBackendTextureState(backendTexture, state, std::move(callback));
809}
810
811bool GrContext::setBackendRenderTargetState(const GrBackendRenderTarget& backendRenderTarget,
812 const GrBackendSurfaceMutableState& state,
813 GrGpuFinishedProc finishedProc,
814 GrGpuFinishedContext finishedContext) {
Greg Daniel1db8e792020-06-09 17:29:32 -0400815 sk_sp<GrRefCntedCallback> callback;
816 if (finishedProc) {
817 callback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
818 }
Greg Daniel25597782020-06-11 13:15:08 -0400819
820 if (!this->asDirectContext()) {
821 return false;
822 }
823
824 if (this->abandoned()) {
825 return false;
826 }
827
Greg Daniel1db8e792020-06-09 17:29:32 -0400828 return fGpu->setBackendRenderTargetState(backendRenderTarget, state, std::move(callback));
829}
830
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400831void GrContext::deleteBackendTexture(GrBackendTexture backendTex) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400832 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danielf0e04f02019-12-04 15:17:54 -0500833 // For the Vulkan backend we still must destroy the backend texture when the context is
834 // abandoned.
835 if ((this->abandoned() && this->backend() != GrBackendApi::kVulkan) || !backendTex.isValid()) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400836 return;
837 }
838
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400839 fGpu->deleteBackendTexture(backendTex);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400840}
841
Greg Daniel1db8e792020-06-09 17:29:32 -0400842//////////////////////////////////////////////////////////////////////////////
843
Brian Osmaned58e002019-09-06 14:42:43 -0400844bool GrContext::precompileShader(const SkData& key, const SkData& data) {
845 return fGpu->precompileShader(key, data);
846}
847
Brian Salomonec22b1a2019-08-09 09:41:48 -0400848#ifdef SK_ENABLE_DUMP_GPU
Michael Ludwigdd205452020-03-30 17:16:34 -0400849#include "include/core/SkString.h"
Brian Salomonec22b1a2019-08-09 09:41:48 -0400850#include "src/utils/SkJSONWriter.h"
851SkString GrContext::dump() const {
852 SkDynamicMemoryWStream stream;
853 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
854 writer.beginObject();
855
856 writer.appendString("backend", GrBackendApiToStr(this->backend()));
857
858 writer.appendName("caps");
859 this->caps()->dumpJSON(&writer);
860
861 writer.appendName("gpu");
862 this->fGpu->dumpJSON(&writer);
863
Robert Phillips273f1072020-05-05 13:03:07 -0400864 writer.appendName("context");
865 this->dumpJSON(&writer);
866
Brian Salomonec22b1a2019-08-09 09:41:48 -0400867 // Flush JSON to the memory stream
868 writer.endObject();
869 writer.flush();
870
871 // Null terminate the JSON data in the memory stream
872 stream.write8(0);
873
874 // Allocate a string big enough to hold all the data, then copy out of the stream
875 SkString result(stream.bytesWritten());
876 stream.copyToAndReset(result.writable_str());
877 return result;
878}
879#endif