blob: 3a8b661be996e95a578c635da48a1181d482a4fe [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 Holler96ead542020-06-26 08:50:14 -040059 this->destroyDrawingManager();
Greg Daniel5ed3c112020-06-18 15:59:17 -040060 fMappedBufferManager.reset();
Robert Phillips292a6b22019-02-14 14:49:02 -050061 delete fResourceProvider;
62 delete fResourceCache;
Robert Phillips292a6b22019-02-14 14:49:02 -050063}
64
Adlai Hollere219d1c2020-06-02 11:23:16 -040065bool GrContext::init() {
Greg Danielb76a72a2017-07-13 15:07:54 -040066 ASSERT_SINGLE_OWNER
Robert Phillipsa41c6852019-02-07 10:44:10 -050067 SkASSERT(this->proxyProvider());
Robert Phillips88260b52018-01-19 12:56:09 -050068
Adlai Hollere219d1c2020-06-02 11:23:16 -040069 if (!INHERITED::init()) {
Robert Phillipsbb606772019-02-04 17:50:57 -050070 return false;
71 }
72
Robert Phillips2184fb72019-02-21 16:11:41 -050073 SkASSERT(this->getTextBlobCache());
Robert Phillipsbb606772019-02-04 17:50:57 -050074
Robert Phillips88260b52018-01-19 12:56:09 -050075 if (fGpu) {
Robert Phillips4d932d12020-04-09 08:58:52 -040076 fStrikeCache.reset(new GrStrikeCache{});
Robert Phillipsa41c6852019-02-07 10:44:10 -050077 fResourceCache = new GrResourceCache(this->caps(), this->singleOwner(), this->contextID());
Robert Phillips12c46292019-04-23 07:36:17 -040078 fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, this->singleOwner());
Mike Kleinf46d5ca2019-12-11 10:45:01 -050079 fMappedBufferManager = std::make_unique<GrClientMappedBufferManager>(this->contextID());
Robert Phillips88260b52018-01-19 12:56:09 -050080 }
81
Robert Phillips88260b52018-01-19 12:56:09 -050082 if (fResourceCache) {
Robert Phillipsa41c6852019-02-07 10:44:10 -050083 fResourceCache->setProxyProvider(this->proxyProvider());
Robert Phillips88260b52018-01-19 12:56:09 -050084 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050085
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000086 fDidTestPMConversions = false;
87
Robert Phillipsfde6fa02018-03-02 08:53:14 -050088 // DDL TODO: we need to think through how the task group & persistent cache
89 // get passed on to/shared between all the DDLRecorders created with this context.
Robert Phillipsc1541ae2019-02-04 12:05:37 -050090 if (this->options().fExecutor) {
Mike Kleinf46d5ca2019-12-11 10:45:01 -050091 fTaskGroup = std::make_unique<SkTaskGroup>(*this->options().fExecutor);
Brian Osman51279982017-08-23 10:12:00 -040092 }
93
Robert Phillipsc1541ae2019-02-04 12:05:37 -050094 fPersistentCache = this->options().fPersistentCache;
Brian Osman5e7fbfd2019-05-03 13:13:35 -040095 fShaderErrorHandler = this->options().fShaderErrorHandler;
96 if (!fShaderErrorHandler) {
97 fShaderErrorHandler = GrShaderUtils::DefaultShaderErrorHandler();
98 }
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -040099
Brian Salomon91a3e522017-06-23 10:58:19 -0400100 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000101}
102
Robert Phillips4217ea72019-01-30 13:08:28 -0500103sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
Adlai Hollere219d1c2020-06-02 11:23:16 -0400104 return INHERITED::threadSafeProxy();
Robert Phillips4217ea72019-01-30 13:08:28 -0500105}
106
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400107//////////////////////////////////////////////////////////////////////////////
108
bsalomon2354f842014-07-28 13:48:36 -0700109void GrContext::abandonContext() {
Greg Daniel6e35a002020-04-01 13:29:59 -0400110 if (INHERITED::abandoned()) {
Robert Phillipsa9162df2019-02-11 14:12:03 -0500111 return;
112 }
joshualitt1de610a2016-01-06 08:26:09 -0800113
Robert Phillipsa9162df2019-02-11 14:12:03 -0500114 INHERITED::abandonContext();
115
Robert Phillips4d932d12020-04-09 08:58:52 -0400116 fStrikeCache->freeAll();
117
Brian Salomon9241a6d2019-10-03 13:26:54 -0400118 fMappedBufferManager->abandon();
119
bsalomond309e7a2015-04-30 14:18:54 -0700120 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800121
bsalomon@google.com205d4602011-04-25 12:43:45 +0000122 // abandon first to so destructors
123 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800124 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700125
bsalomon6e2aad42016-04-01 11:54:31 -0700126 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400127
128 fMappedBufferManager.reset();
Khushalc421ca12018-06-26 14:38:34 -0700129}
130
bsalomon6e2aad42016-04-01 11:54:31 -0700131void GrContext::releaseResourcesAndAbandonContext() {
Greg Daniel6e35a002020-04-01 13:29:59 -0400132 if (INHERITED::abandoned()) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500133 return;
134 }
Robert Phillipsa9162df2019-02-11 14:12:03 -0500135
136 INHERITED::abandonContext();
137
Brian Salomon9241a6d2019-10-03 13:26:54 -0400138 fMappedBufferManager.reset();
139
bsalomon6e2aad42016-04-01 11:54:31 -0700140 fResourceProvider->abandon();
141
bsalomon6e2aad42016-04-01 11:54:31 -0700142 // Release all resources in the backend 3D API.
143 fResourceCache->releaseAll();
144
145 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000146}
147
Greg Daniel6e35a002020-04-01 13:29:59 -0400148bool GrContext::abandoned() {
149 if (INHERITED::abandoned()) {
150 return true;
151 }
152
153 if (fGpu && fGpu->isDeviceLost()) {
154 this->abandonContext();
155 return true;
156 }
157 return false;
158}
159
Brian Salomon24069eb2020-06-24 10:19:52 -0400160bool GrContext::oomed() { return fGpu ? fGpu->checkAndResetOOMed() : false; }
161
Brian Salomon1f05d452019-02-08 12:33:08 -0500162void GrContext::resetGLTextureBindings() {
163 if (this->abandoned() || this->backend() != GrBackendApi::kOpenGL) {
164 return;
165 }
166 fGpu->resetTextureBindings();
167}
168
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000169void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800170 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000171 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000172}
173
174void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800175 ASSERT_SINGLE_OWNER
176
Adlai Holler9d8a41c2020-06-25 14:01:58 +0000177 if (this->abandoned()) {
178 return;
179 }
180
Robert Phillips2184fb72019-02-21 16:11:41 -0500181 // TODO: the glyph cache doesn't hold any GpuResources so this call should not be needed here.
182 // Some slack in the GrTextBlob's implementation requires it though. That could be fixed.
Robert Phillips4d932d12020-04-09 08:58:52 -0400183 fStrikeCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700184
Robert Phillips6a6de562019-02-15 15:19:15 -0500185 this->drawingManager()->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700186
187 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000188}
189
Robert Phillips6eba0632018-03-28 12:25:42 -0400190void GrContext::purgeUnlockedResources(bool scratchResourcesOnly) {
191 ASSERT_SINGLE_OWNER
Robert Phillipsddc21482019-10-16 14:30:09 -0400192
193 if (this->abandoned()) {
194 return;
195 }
196
Robert Phillips6eba0632018-03-28 12:25:42 -0400197 fResourceCache->purgeUnlockedResources(scratchResourcesOnly);
198 fResourceCache->purgeAsNeeded();
Robert Phillips2184fb72019-02-21 16:11:41 -0500199
200 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
201 // place to purge stale blobs
202 this->getTextBlobCache()->purgeStaleBlobs();
Robert Phillips6eba0632018-03-28 12:25:42 -0400203}
204
Jim Van Verth76d917c2017-12-13 09:26:37 -0500205void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Yuqian Li40aa85f2019-07-02 13:45:00 -0700206 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
207
Brian Salomon5e150852017-03-22 14:53:13 -0400208 ASSERT_SINGLE_OWNER
Chris Dalton6c3879d2018-11-01 11:13:19 -0600209
Brian Salomon9241a6d2019-10-03 13:26:54 -0400210 if (this->abandoned()) {
211 return;
212 }
213
214 fMappedBufferManager->process();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600215 auto purgeTime = GrStdSteadyClock::now() - msNotUsed;
216
Jim Van Verth76d917c2017-12-13 09:26:37 -0500217 fResourceCache->purgeAsNeeded();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600218 fResourceCache->purgeResourcesNotUsedSince(purgeTime);
219
Robert Phillips6a6de562019-02-15 15:19:15 -0500220 if (auto ccpr = this->drawingManager()->getCoverageCountingPathRenderer()) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500221 ccpr->purgeCacheEntriesOlderThan(this->proxyProvider(), purgeTime);
Chris Dalton6c3879d2018-11-01 11:13:19 -0600222 }
Jim Van Verth76d917c2017-12-13 09:26:37 -0500223
Robert Phillips2184fb72019-02-21 16:11:41 -0500224 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
225 // place to purge stale blobs
226 this->getTextBlobCache()->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400227}
228
Derek Sollenberger5480a182017-05-25 16:43:59 -0400229void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
230 ASSERT_SINGLE_OWNER
Robert Phillipsddc21482019-10-16 14:30:09 -0400231
232 if (this->abandoned()) {
233 return;
234 }
235
Derek Sollenberger5480a182017-05-25 16:43:59 -0400236 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
237}
238
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000239void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800240 ASSERT_SINGLE_OWNER
241
bsalomon71cb0c22014-11-14 12:10:14 -0800242 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800243 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800244 }
245 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800246 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800247 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000248}
249
Derek Sollenbergeree479142017-05-24 11:41:33 -0400250size_t GrContext::getResourceCachePurgeableBytes() const {
251 ASSERT_SINGLE_OWNER
252 return fResourceCache->getPurgeableBytes();
253}
254
Greg Daniel8b666172019-10-09 12:38:22 -0400255size_t GrContext::ComputeImageSize(sk_sp<SkImage> image, GrMipMapped mipMapped, bool useNextPow2) {
256 if (!image->isTextureBacked()) {
257 return 0;
258 }
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400259 SkImage_GpuBase* gpuImage = static_cast<SkImage_GpuBase*>(as_IB(image.get()));
260 GrTextureProxy* proxy = gpuImage->peekProxy();
261 if (!proxy) {
262 return 0;
263 }
264
265 const GrCaps& caps = *gpuImage->context()->priv().caps();
Greg Daniel8b666172019-10-09 12:38:22 -0400266 int colorSamplesPerPixel = 1;
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400267 return GrSurface::ComputeSize(caps, proxy->backendFormat(), image->dimensions(),
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400268 colorSamplesPerPixel, mipMapped, useNextPow2);
Greg Daniel8b666172019-10-09 12:38:22 -0400269}
270
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000271////////////////////////////////////////////////////////////////////////////////
272
Robert Phillipsbb606772019-02-04 17:50:57 -0500273int GrContext::maxTextureSize() const { return this->caps()->maxTextureSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400274
Robert Phillipsbb606772019-02-04 17:50:57 -0500275int GrContext::maxRenderTargetSize() const { return this->caps()->maxRenderTargetSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400276
Brian Salomonbdecacf2018-02-02 20:32:49 -0500277bool GrContext::colorTypeSupportedAsImage(SkColorType colorType) const {
Greg Daniel7bfc9132019-08-14 14:23:53 -0400278 GrBackendFormat format =
279 this->caps()->getDefaultBackendFormat(SkColorTypeToGrColorType(colorType),
280 GrRenderable::kNo);
281 return format.isValid();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500282}
283
Brian Salomonbdecacf2018-02-02 20:32:49 -0500284////////////////////////////////////////////////////////////////////////////////
285
Greg Daniel414418d2020-07-08 11:44:25 -0400286bool GrContext::wait(int numSemaphores, const GrBackendSemaphore waitSemaphores[],
287 bool deleteSemaphoresAfterWait) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400288 if (!fGpu || fGpu->caps()->semaphoreSupport()) {
Greg Daniel06be0792019-04-22 15:53:23 -0400289 return false;
290 }
Greg Daniel414418d2020-07-08 11:44:25 -0400291 GrWrapOwnership ownership =
292 deleteSemaphoresAfterWait ? kAdopt_GrWrapOwnership : kBorrow_GrWrapOwnership;
Greg Daniel06be0792019-04-22 15:53:23 -0400293 for (int i = 0; i < numSemaphores; ++i) {
Greg Daniel301015c2019-11-18 14:06:46 -0500294 std::unique_ptr<GrSemaphore> sema = fResourceProvider->wrapBackendSemaphore(
Greg Daniel414418d2020-07-08 11:44:25 -0400295 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait, ownership);
Greg Daniel0106fcc2020-07-01 17:40:12 -0400296 // If we failed to wrap the semaphore it means the client didn't give us a valid semaphore
297 // to begin with. Therefore, it is fine to not wait on it.
298 if (sema) {
299 fGpu->waitSemaphore(sema.get());
300 }
Greg Daniel06be0792019-04-22 15:53:23 -0400301 }
302 return true;
303}
304
305////////////////////////////////////////////////////////////////////////////////
306
Greg Daniele8d3cca2020-06-10 10:04:48 -0400307GrSemaphoresSubmitted GrContext::flush(const GrFlushInfo& info) {
Greg Daniel51316782017-08-02 15:10:09 +0000308 ASSERT_SINGLE_OWNER
Robert Phillipsa9162df2019-02-11 14:12:03 -0500309 if (this->abandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400310 if (info.fFinishedProc) {
311 info.fFinishedProc(info.fFinishedContext);
312 }
313 if (info.fSubmittedProc) {
314 info.fSubmittedProc(info.fSubmittedContext, false);
315 }
Robert Phillipsa9162df2019-02-11 14:12:03 -0500316 return GrSemaphoresSubmitted::kNo;
317 }
Greg Daniel51316782017-08-02 15:10:09 +0000318
Greg Daniel04283f32020-05-20 13:16:00 -0400319 bool flushed = this->drawingManager()->flush(
Greg Daniel9efe3862020-06-11 11:51:06 -0400320 nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, info, nullptr);
Greg Danielfe159622020-04-10 17:43:51 +0000321
Greg Daniel04283f32020-05-20 13:16:00 -0400322 if (!flushed || (!this->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
Greg Danielfe159622020-04-10 17:43:51 +0000323 return GrSemaphoresSubmitted::kNo;
324 }
325 return GrSemaphoresSubmitted::kYes;
Greg Daniel51316782017-08-02 15:10:09 +0000326}
327
Greg Daniel04283f32020-05-20 13:16:00 -0400328bool GrContext::submit(bool syncCpu) {
329 ASSERT_SINGLE_OWNER
330 if (this->abandoned()) {
331 return false;
332 }
333
334 if (!fGpu) {
335 return false;
336 }
337
338 return fGpu->submitToGpu(syncCpu);
Greg Danielda50cb82020-05-13 14:07:40 -0400339}
340
Greg Daniela870b462019-01-08 15:49:46 -0500341////////////////////////////////////////////////////////////////////////////////
342
Brian Salomonb0d8b762019-05-06 16:58:22 -0400343void GrContext::checkAsyncWorkCompletion() {
344 if (fGpu) {
345 fGpu->checkFinishProcs();
346 }
347}
348
349////////////////////////////////////////////////////////////////////////////////
350
Greg Daniela870b462019-01-08 15:49:46 -0500351void GrContext::storeVkPipelineCacheData() {
352 if (fGpu) {
353 fGpu->storeVkPipelineCacheData();
354 }
355}
356
357////////////////////////////////////////////////////////////////////////////////
358
Khushal3e7548c2018-05-23 15:45:01 -0700359bool GrContext::supportsDistanceFieldText() const {
Robert Phillipsbb606772019-02-04 17:50:57 -0500360 return this->caps()->shaderCaps()->supportsDistanceFieldText();
Khushal3e7548c2018-05-23 15:45:01 -0700361}
362
bsalomon37f9a262015-02-02 13:00:10 -0800363//////////////////////////////////////////////////////////////////////////////
364
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500365void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800366 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500367 if (maxResources) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400368 *maxResources = -1;
bsalomon37f9a262015-02-02 13:00:10 -0800369 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500370 if (maxResourceBytes) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400371 *maxResourceBytes = this->getResourceCacheLimit();
bsalomon37f9a262015-02-02 13:00:10 -0800372 }
373}
374
Robert Phillipscf39f372019-09-03 10:29:20 -0400375size_t GrContext::getResourceCacheLimit() const {
joshualitt1de610a2016-01-06 08:26:09 -0800376 ASSERT_SINGLE_OWNER
Robert Phillipscf39f372019-09-03 10:29:20 -0400377 return fResourceCache->getMaxResourceBytes();
378}
379
380void GrContext::setResourceCacheLimits(int unused, size_t maxResourceBytes) {
381 ASSERT_SINGLE_OWNER
382 this->setResourceCacheLimit(maxResourceBytes);
383}
384
385void GrContext::setResourceCacheLimit(size_t maxResourceBytes) {
386 ASSERT_SINGLE_OWNER
387 fResourceCache->setLimit(maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800388}
389
ericrk0a5fa482015-09-15 14:16:10 -0700390//////////////////////////////////////////////////////////////////////////////
ericrk0a5fa482015-09-15 14:16:10 -0700391void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800392 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700393 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
Khushal71652e22018-10-29 13:05:36 -0700394 traceMemoryDump->dumpNumericValue("skia/gr_text_blob_cache", "size", "bytes",
Robert Phillips2184fb72019-02-21 16:11:41 -0500395 this->getTextBlobCache()->usedBytes());
ericrk0a5fa482015-09-15 14:16:10 -0700396}
Brian Osman71a18892017-08-10 10:23:25 -0400397
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400398//////////////////////////////////////////////////////////////////////////////
399GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400400 const GrBackendFormat& backendFormat,
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400401 GrMipMapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400402 GrRenderable renderable,
403 GrProtected isProtected) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400404 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400405 if (!this->asDirectContext()) {
406 return GrBackendTexture();
407 }
408
409 if (this->abandoned()) {
410 return GrBackendTexture();
411 }
412
Robert Phillips4277f012020-01-21 14:28:34 -0500413 return fGpu->createBackendTexture({width, height}, backendFormat, renderable,
Greg Daniel16032b32020-05-06 15:31:10 -0400414 mipMapped, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400415}
416
417GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400418 SkColorType skColorType,
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400419 GrMipMapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400420 GrRenderable renderable,
421 GrProtected isProtected) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400422 if (!this->asDirectContext()) {
423 return GrBackendTexture();
424 }
425
426 if (this->abandoned()) {
427 return GrBackendTexture();
428 }
429
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400430 const GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400431
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400432 return this->createBackendTexture(width, height, format, mipMapped, renderable, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400433}
434
Robert Phillips02dc0302019-07-02 17:58:27 -0400435GrBackendTexture GrContext::createBackendTexture(const SkSurfaceCharacterization& c) {
Robert Phillips02dc0302019-07-02 17:58:27 -0400436 if (!this->asDirectContext() || !c.isValid()) {
437 return GrBackendTexture();
438 }
439
440 if (this->abandoned()) {
441 return GrBackendTexture();
442 }
443
444 if (c.usesGLFBO0()) {
445 // If we are making the surface we will never use FBO0.
446 return GrBackendTexture();
447 }
448
449 if (c.vulkanSecondaryCBCompatible()) {
450 return {};
451 }
452
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400453 const GrBackendFormat format = this->defaultBackendFormat(c.colorType(), GrRenderable::kYes);
Robert Phillips02dc0302019-07-02 17:58:27 -0400454 if (!format.isValid()) {
455 return GrBackendTexture();
456 }
457
Robert Phillips02dc0302019-07-02 17:58:27 -0400458 GrBackendTexture result = this->createBackendTexture(c.width(), c.height(), format,
459 GrMipMapped(c.isMipMapped()),
460 GrRenderable::kYes,
Robert Phillips3cd54322019-07-10 09:28:59 -0400461 c.isProtected());
Robert Phillips02dc0302019-07-02 17:58:27 -0400462 SkASSERT(c.isCompatible(result));
463 return result;
464}
465
Greg Daniel25597782020-06-11 13:15:08 -0400466static GrBackendTexture create_and_update_backend_texture(
467 GrContext* context,
468 SkISize dimensions,
469 const GrBackendFormat& backendFormat,
470 GrMipMapped mipMapped,
471 GrRenderable renderable,
472 GrProtected isProtected,
473 sk_sp<GrRefCntedCallback> finishedCallback,
474 const GrGpu::BackendTextureData* data) {
Greg Daniel16032b32020-05-06 15:31:10 -0400475 GrGpu* gpu = context->priv().getGpu();
476
477 GrBackendTexture beTex = gpu->createBackendTexture(dimensions, backendFormat, renderable,
478 mipMapped, isProtected);
479 if (!beTex.isValid()) {
480 return {};
481 }
482
Greg Daniel25597782020-06-11 13:15:08 -0400483 if (!context->priv().getGpu()->updateBackendTexture(beTex, std::move(finishedCallback), data)) {
Greg Daniel16032b32020-05-06 15:31:10 -0400484 context->deleteBackendTexture(beTex);
485 return {};
486 }
487 return beTex;
488}
489
Greg Daniel25597782020-06-11 13:15:08 -0400490
491GrBackendTexture GrContext::createBackendTexture(const SkSurfaceCharacterization& c,
492 const SkColor4f& color,
493 GrGpuFinishedProc finishedProc,
494 GrGpuFinishedContext finishedContext) {
495 sk_sp<GrRefCntedCallback> finishedCallback;
496 if (finishedProc) {
497 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
498 }
499
500 if (!this->asDirectContext() || !c.isValid()) {
501 return {};
502 }
503
504 if (this->abandoned()) {
505 return {};
506 }
507
508 if (c.usesGLFBO0()) {
509 // If we are making the surface we will never use FBO0.
510 return {};
511 }
512
513 if (c.vulkanSecondaryCBCompatible()) {
514 return {};
515 }
516
517 const GrBackendFormat format = this->defaultBackendFormat(c.colorType(), GrRenderable::kYes);
518 if (!format.isValid()) {
519 return {};
520 }
521
522 GrGpu::BackendTextureData data(color);
523 GrBackendTexture result = create_and_update_backend_texture(
524 this, {c.width(), c.height()}, format, GrMipMapped(c.isMipMapped()), GrRenderable::kYes,
525 c.isProtected(), std::move(finishedCallback), &data);
526
527 SkASSERT(c.isCompatible(result));
528 return result;
529}
530
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400531GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400532 const GrBackendFormat& backendFormat,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400533 const SkColor4f& color,
534 GrMipMapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400535 GrRenderable renderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400536 GrProtected isProtected,
537 GrGpuFinishedProc finishedProc,
538 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400539 sk_sp<GrRefCntedCallback> finishedCallback;
540 if (finishedProc) {
541 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
542 }
543
Brian Salomonc42eb662019-06-24 17:13:00 -0400544 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400545 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400546 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400547 }
548
549 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400550 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400551 }
552
Brian Salomon85c3d682019-11-04 15:04:54 -0500553 GrGpu::BackendTextureData data(color);
Greg Daniel16032b32020-05-06 15:31:10 -0400554 return create_and_update_backend_texture(this, {width, height}, backendFormat, mipMapped,
Greg Daniel25597782020-06-11 13:15:08 -0400555 renderable, isProtected, std::move(finishedCallback),
Greg Daniel16032b32020-05-06 15:31:10 -0400556 &data);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400557}
558
559GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400560 SkColorType skColorType,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400561 const SkColor4f& color,
562 GrMipMapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400563 GrRenderable renderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400564 GrProtected isProtected,
565 GrGpuFinishedProc finishedProc,
566 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400567 sk_sp<GrRefCntedCallback> finishedCallback;
568 if (finishedProc) {
569 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
570 }
571
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400572 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400573 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400574 }
575
576 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400577 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400578 }
579
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400580 GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400581 if (!format.isValid()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400582 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400583 }
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400584
585 GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
Brian Salomon982f5462020-03-30 12:52:33 -0400586 SkColor4f swizzledColor = this->caps()->getWriteSwizzle(format, grColorType).applyTo(color);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400587
Greg Daniel25597782020-06-11 13:15:08 -0400588 GrGpu::BackendTextureData data(swizzledColor);
589 return create_and_update_backend_texture(this, {width, height}, format, mipMapped,
590 renderable, isProtected, std::move(finishedCallback),
591 &data);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400592}
593
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500594GrBackendTexture GrContext::createBackendTexture(const SkPixmap srcData[], int numProvidedLevels,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400595 GrRenderable renderable, GrProtected isProtected,
596 GrGpuFinishedProc finishedProc,
597 GrGpuFinishedContext finishedContext) {
Robert Phillips66944402019-09-30 13:21:25 -0400598 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
599
Greg Daniel25597782020-06-11 13:15:08 -0400600 sk_sp<GrRefCntedCallback> finishedCallback;
601 if (finishedProc) {
602 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
603 }
604
Robert Phillips66944402019-09-30 13:21:25 -0400605 if (!this->asDirectContext()) {
606 return {};
607 }
608
609 if (this->abandoned()) {
610 return {};
611 }
612
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500613 if (!srcData || numProvidedLevels <= 0) {
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) {
629 return {};
630 }
631
Robert Phillips66944402019-09-30 13:21:25 -0400632 GrBackendFormat backendFormat = this->defaultBackendFormat(colorType, renderable);
633
Brian Salomon85c3d682019-11-04 15:04:54 -0500634 GrGpu::BackendTextureData data(srcData);
Greg Daniel16032b32020-05-06 15:31:10 -0400635 return create_and_update_backend_texture(this, {baseWidth, baseHeight}, backendFormat,
Greg Daniel25597782020-06-11 13:15:08 -0400636 mipMapped, renderable, isProtected,
637 std::move(finishedCallback), &data);
Robert Phillips66944402019-09-30 13:21:25 -0400638}
639
Greg Danielb2365d82020-05-13 15:32:04 -0400640bool GrContext::updateBackendTexture(const GrBackendTexture& backendTexture,
641 const SkColor4f& color,
642 GrGpuFinishedProc finishedProc,
643 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400644 sk_sp<GrRefCntedCallback> finishedCallback;
645 if (finishedProc) {
646 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
647 }
648
Greg Danielb2365d82020-05-13 15:32:04 -0400649 if (!this->asDirectContext()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400650 return false;
651 }
652
653 if (this->abandoned()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400654 return false;
655 }
656
657 GrGpu::BackendTextureData data(color);
Greg Daniel25597782020-06-11 13:15:08 -0400658 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
Greg Danielb2365d82020-05-13 15:32:04 -0400659}
660
661bool GrContext::updateBackendTexture(const GrBackendTexture& backendTexture,
662 const SkPixmap srcData[],
663 int numLevels,
664 GrGpuFinishedProc finishedProc,
665 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400666 sk_sp<GrRefCntedCallback> finishedCallback;
667 if (finishedProc) {
668 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
669 }
670
Greg Danielb2365d82020-05-13 15:32:04 -0400671 if (!this->asDirectContext()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400672 return false;
673 }
674
675 if (this->abandoned()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400676 return false;
677 }
678
679 if (!srcData || numLevels <= 0) {
Greg Danielb2365d82020-05-13 15:32:04 -0400680 return false;
681 }
682
683 int numExpectedLevels = 1;
684 if (backendTexture.hasMipMaps()) {
685 numExpectedLevels = SkMipMap::ComputeLevelCount(backendTexture.width(),
686 backendTexture.height()) + 1;
687 }
688 if (numLevels != numExpectedLevels) {
Greg Danielb2365d82020-05-13 15:32:04 -0400689 return false;
690 }
691
692 GrGpu::BackendTextureData data(srcData);
Greg Daniel25597782020-06-11 13:15:08 -0400693 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
Greg Danielb2365d82020-05-13 15:32:04 -0400694}
695
Robert Phillipsb915c942019-12-17 14:44:37 -0500696//////////////////////////////////////////////////////////////////////////////
697
Greg Danielaaf738c2020-07-10 09:30:33 -0400698static GrBackendTexture create_and_update_compressed_backend_texture(
699 GrContext* context,
700 SkISize dimensions,
701 const GrBackendFormat& backendFormat,
702 GrMipMapped mipMapped,
703 GrProtected isProtected,
704 sk_sp<GrRefCntedCallback> finishedCallback,
705 const GrGpu::BackendTextureData* data) {
706 GrGpu* gpu = context->priv().getGpu();
707
708 GrBackendTexture beTex = gpu->createCompressedBackendTexture(dimensions, backendFormat,
709 mipMapped, isProtected);
710 if (!beTex.isValid()) {
711 return {};
712 }
713
714 if (!context->priv().getGpu()->updateCompressedBackendTexture(
715 beTex, std::move(finishedCallback), data)) {
716 context->deleteBackendTexture(beTex);
717 return {};
718 }
719 return beTex;
720}
721
Robert Phillipsb915c942019-12-17 14:44:37 -0500722GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
723 const GrBackendFormat& backendFormat,
724 const SkColor4f& color,
725 GrMipMapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400726 GrProtected isProtected,
727 GrGpuFinishedProc finishedProc,
728 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500729 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel25597782020-06-11 13:15:08 -0400730 sk_sp<GrRefCntedCallback> finishedCallback;
731 if (finishedProc) {
732 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
733 }
734
Robert Phillipsb915c942019-12-17 14:44:37 -0500735 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400736 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500737 }
738
739 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400740 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500741 }
742
743 GrGpu::BackendTextureData data(color);
Greg Danielaaf738c2020-07-10 09:30:33 -0400744 return create_and_update_compressed_backend_texture(this, {width, height}, backendFormat,
745 mipMapped, isProtected,
746 std::move(finishedCallback), &data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500747}
748
749GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
750 SkImage::CompressionType compression,
751 const SkColor4f& color,
752 GrMipMapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400753 GrProtected isProtected,
754 GrGpuFinishedProc finishedProc,
755 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500756 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillipsb915c942019-12-17 14:44:37 -0500757 GrBackendFormat format = this->compressedBackendFormat(compression);
758 return this->createCompressedBackendTexture(width, height, format, color,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400759 mipMapped, isProtected, finishedProc,
760 finishedContext);
Robert Phillipsb915c942019-12-17 14:44:37 -0500761}
762
763GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
764 const GrBackendFormat& backendFormat,
765 const void* compressedData,
766 size_t dataSize,
767 GrMipMapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400768 GrProtected isProtected,
769 GrGpuFinishedProc finishedProc,
770 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500771 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel25597782020-06-11 13:15:08 -0400772 sk_sp<GrRefCntedCallback> finishedCallback;
773 if (finishedProc) {
774 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
775 }
776
Robert Phillipsb915c942019-12-17 14:44:37 -0500777 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400778 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500779 }
780
781 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400782 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500783 }
784
785 GrGpu::BackendTextureData data(compressedData, dataSize);
Greg Danielaaf738c2020-07-10 09:30:33 -0400786 return create_and_update_compressed_backend_texture(this, {width, height}, backendFormat,
787 mipMapped, isProtected,
788 std::move(finishedCallback), &data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500789}
790
791GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
792 SkImage::CompressionType compression,
793 const void* data, size_t dataSize,
794 GrMipMapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400795 GrProtected isProtected,
796 GrGpuFinishedProc finishedProc,
797 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500798 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillipsb915c942019-12-17 14:44:37 -0500799 GrBackendFormat format = this->compressedBackendFormat(compression);
Greg Daniel25597782020-06-11 13:15:08 -0400800 return this->createCompressedBackendTexture(width, height, format, data, dataSize, mipMapped,
801 isProtected, finishedProc, finishedContext);
Robert Phillipsb915c942019-12-17 14:44:37 -0500802}
803
Greg Daniel1db8e792020-06-09 17:29:32 -0400804bool GrContext::setBackendTextureState(const GrBackendTexture& backendTexture,
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->setBackendTextureState(backendTexture, state, std::move(callback));
822}
823
824bool GrContext::setBackendRenderTargetState(const GrBackendRenderTarget& backendRenderTarget,
825 const GrBackendSurfaceMutableState& state,
826 GrGpuFinishedProc finishedProc,
827 GrGpuFinishedContext finishedContext) {
Greg Daniel1db8e792020-06-09 17:29:32 -0400828 sk_sp<GrRefCntedCallback> callback;
829 if (finishedProc) {
830 callback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
831 }
Greg Daniel25597782020-06-11 13:15:08 -0400832
833 if (!this->asDirectContext()) {
834 return false;
835 }
836
837 if (this->abandoned()) {
838 return false;
839 }
840
Greg Daniel1db8e792020-06-09 17:29:32 -0400841 return fGpu->setBackendRenderTargetState(backendRenderTarget, state, std::move(callback));
842}
843
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400844void GrContext::deleteBackendTexture(GrBackendTexture backendTex) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400845 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danielf0e04f02019-12-04 15:17:54 -0500846 // For the Vulkan backend we still must destroy the backend texture when the context is
847 // abandoned.
848 if ((this->abandoned() && this->backend() != GrBackendApi::kVulkan) || !backendTex.isValid()) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400849 return;
850 }
851
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400852 fGpu->deleteBackendTexture(backendTex);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400853}
854
Greg Daniel1db8e792020-06-09 17:29:32 -0400855//////////////////////////////////////////////////////////////////////////////
856
Brian Osmaned58e002019-09-06 14:42:43 -0400857bool GrContext::precompileShader(const SkData& key, const SkData& data) {
858 return fGpu->precompileShader(key, data);
859}
860
Brian Salomonec22b1a2019-08-09 09:41:48 -0400861#ifdef SK_ENABLE_DUMP_GPU
Michael Ludwigdd205452020-03-30 17:16:34 -0400862#include "include/core/SkString.h"
Brian Salomonec22b1a2019-08-09 09:41:48 -0400863#include "src/utils/SkJSONWriter.h"
864SkString GrContext::dump() const {
865 SkDynamicMemoryWStream stream;
866 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
867 writer.beginObject();
868
869 writer.appendString("backend", GrBackendApiToStr(this->backend()));
870
871 writer.appendName("caps");
872 this->caps()->dumpJSON(&writer);
873
874 writer.appendName("gpu");
875 this->fGpu->dumpJSON(&writer);
876
Robert Phillips273f1072020-05-05 13:03:07 -0400877 writer.appendName("context");
878 this->dumpJSON(&writer);
879
Brian Salomonec22b1a2019-08-09 09:41:48 -0400880 // Flush JSON to the memory stream
881 writer.endObject();
882 writer.flush();
883
884 // Null terminate the JSON data in the memory stream
885 stream.write8(0);
886
887 // Allocate a string big enough to hold all the data, then copy out of the stream
888 SkString result(stream.bytesWritten());
889 stream.copyToAndReset(result.writable_str());
890 return result;
891}
892#endif