blob: 343bb9d9cf649e444e80ca0d5f1588772efc9541 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/gpu/text/GrTextBlobCache.h"
34#include "src/gpu/text/GrTextContext.h"
Greg Daniel7fd7a8a2019-10-10 16:10:31 -040035#include "src/image/SkImage_GpuBase.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/image/SkSurface_Gpu.h"
Mike Klein0ec1c572018-12-04 11:52:51 -050037#include <atomic>
Greg Danielb76a72a2017-07-13 15:07:54 -040038
Robert Phillipse78b7252017-04-06 07:59:41 -040039#define ASSERT_OWNED_PROXY(P) \
Brian Salomonfd98c2c2018-07-31 17:25:29 -040040 SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == this)
Robert Phillips7ee385e2017-03-30 08:02:11 -040041
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000042#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt1de610a2016-01-06 08:26:09 -080043#define ASSERT_SINGLE_OWNER \
Robert Phillipsa41c6852019-02-07 10:44:10 -050044 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
Robert Phillipsa9162df2019-02-11 14:12:03 -050045#define RETURN_IF_ABANDONED if (this->abandoned()) { return; }
46#define RETURN_FALSE_IF_ABANDONED if (this->abandoned()) { return false; }
47#define RETURN_NULL_IF_ABANDONED if (this->abandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000048
robertphillipsea461502015-05-26 11:38:03 -070049////////////////////////////////////////////////////////////////////////////////
50
Robert Phillipsa41c6852019-02-07 10:44:10 -050051GrContext::GrContext(GrBackendApi backend, const GrContextOptions& options, int32_t contextID)
52 : INHERITED(backend, options, contextID) {
halcanary96fcdcc2015-08-27 07:41:13 -070053 fResourceCache = nullptr;
54 fResourceProvider = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000055}
56
Robert Phillips292a6b22019-02-14 14:49:02 -050057GrContext::~GrContext() {
58 ASSERT_SINGLE_OWNER
59
Robert Phillips6a6de562019-02-15 15:19:15 -050060 if (this->drawingManager()) {
61 this->drawingManager()->cleanup();
Robert Phillips292a6b22019-02-14 14:49:02 -050062 }
63 delete fResourceProvider;
64 delete fResourceCache;
Robert Phillips292a6b22019-02-14 14:49:02 -050065}
66
Brian Osman7b1678a2019-12-16 09:17:25 -050067bool GrContext::init(sk_sp<const GrCaps> caps) {
Greg Danielb76a72a2017-07-13 15:07:54 -040068 ASSERT_SINGLE_OWNER
Robert Phillipsfde6fa02018-03-02 08:53:14 -050069 SkASSERT(fThreadSafeProxy); // needs to have been initialized by derived classes
Robert Phillipsa41c6852019-02-07 10:44:10 -050070 SkASSERT(this->proxyProvider());
Robert Phillips88260b52018-01-19 12:56:09 -050071
Brian Osman7b1678a2019-12-16 09:17:25 -050072 if (!INHERITED::init(std::move(caps))) {
Robert Phillipsbb606772019-02-04 17:50:57 -050073 return false;
74 }
75
76 SkASSERT(this->caps());
Herb Derbya00da612019-03-04 17:10:01 -050077 SkASSERT(this->getGrStrikeCache());
Robert Phillips2184fb72019-02-21 16:11:41 -050078 SkASSERT(this->getTextBlobCache());
Robert Phillipsbb606772019-02-04 17:50:57 -050079
Robert Phillips88260b52018-01-19 12:56:09 -050080 if (fGpu) {
Robert Phillipsa41c6852019-02-07 10:44:10 -050081 fResourceCache = new GrResourceCache(this->caps(), this->singleOwner(), this->contextID());
Robert Phillips12c46292019-04-23 07:36:17 -040082 fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, this->singleOwner());
Mike Kleinf46d5ca2019-12-11 10:45:01 -050083 fMappedBufferManager = std::make_unique<GrClientMappedBufferManager>(this->contextID());
Robert Phillips88260b52018-01-19 12:56:09 -050084 }
85
Robert Phillips88260b52018-01-19 12:56:09 -050086 if (fResourceCache) {
Robert Phillipsa41c6852019-02-07 10:44:10 -050087 fResourceCache->setProxyProvider(this->proxyProvider());
Robert Phillips88260b52018-01-19 12:56:09 -050088 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050089
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000090 fDidTestPMConversions = false;
91
Robert Phillipsfde6fa02018-03-02 08:53:14 -050092 // DDL TODO: we need to think through how the task group & persistent cache
93 // get passed on to/shared between all the DDLRecorders created with this context.
Robert Phillipsc1541ae2019-02-04 12:05:37 -050094 if (this->options().fExecutor) {
Mike Kleinf46d5ca2019-12-11 10:45:01 -050095 fTaskGroup = std::make_unique<SkTaskGroup>(*this->options().fExecutor);
Brian Osman51279982017-08-23 10:12:00 -040096 }
97
Robert Phillipsc1541ae2019-02-04 12:05:37 -050098 fPersistentCache = this->options().fPersistentCache;
Brian Osman5e7fbfd2019-05-03 13:13:35 -040099 fShaderErrorHandler = this->options().fShaderErrorHandler;
100 if (!fShaderErrorHandler) {
101 fShaderErrorHandler = GrShaderUtils::DefaultShaderErrorHandler();
102 }
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400103
Brian Salomon91a3e522017-06-23 10:58:19 -0400104 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000105}
106
Robert Phillips4217ea72019-01-30 13:08:28 -0500107sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
108 return fThreadSafeProxy;
109}
110
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400111//////////////////////////////////////////////////////////////////////////////
112
bsalomon2354f842014-07-28 13:48:36 -0700113void GrContext::abandonContext() {
Greg Daniel6e35a002020-04-01 13:29:59 -0400114 if (INHERITED::abandoned()) {
Robert Phillipsa9162df2019-02-11 14:12:03 -0500115 return;
116 }
joshualitt1de610a2016-01-06 08:26:09 -0800117
Robert Phillipsa9162df2019-02-11 14:12:03 -0500118 INHERITED::abandonContext();
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
Robert Phillipsa9162df2019-02-11 14:12:03 -0500124 // Need to cleanup the drawing manager first so all the render targets
robertphillips0dfa62c2015-11-16 06:23:31 -0800125 // will be released/forgotten before they too are abandoned.
Robert Phillips6a6de562019-02-15 15:19:15 -0500126 this->drawingManager()->cleanup();
robertphillips0dfa62c2015-11-16 06:23:31 -0800127
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
Robert Phillipsa9162df2019-02-11 14:12:03 -0500148 // Need to cleanup the drawing manager first so all the render targets
bsalomon6e2aad42016-04-01 11:54:31 -0700149 // will be released/forgotten before they too are abandoned.
Robert Phillips6a6de562019-02-15 15:19:15 -0500150 this->drawingManager()->cleanup();
bsalomon6e2aad42016-04-01 11:54:31 -0700151
152 // 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 Salomon1f05d452019-02-08 12:33:08 -0500170void GrContext::resetGLTextureBindings() {
171 if (this->abandoned() || this->backend() != GrBackendApi::kOpenGL) {
172 return;
173 }
174 fGpu->resetTextureBindings();
175}
176
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000177void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800178 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000179 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000180}
181
182void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800183 ASSERT_SINGLE_OWNER
184
Robert Phillips2184fb72019-02-21 16:11:41 -0500185 // TODO: the glyph cache doesn't hold any GpuResources so this call should not be needed here.
186 // Some slack in the GrTextBlob's implementation requires it though. That could be fixed.
Herb Derbya00da612019-03-04 17:10:01 -0500187 this->getGrStrikeCache()->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700188
Robert Phillips6a6de562019-02-15 15:19:15 -0500189 this->drawingManager()->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700190
191 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000192}
193
Robert Phillips6eba0632018-03-28 12:25:42 -0400194void GrContext::purgeUnlockedResources(bool scratchResourcesOnly) {
195 ASSERT_SINGLE_OWNER
Robert Phillipsddc21482019-10-16 14:30:09 -0400196
197 if (this->abandoned()) {
198 return;
199 }
200
Robert Phillips6eba0632018-03-28 12:25:42 -0400201 fResourceCache->purgeUnlockedResources(scratchResourcesOnly);
202 fResourceCache->purgeAsNeeded();
Robert Phillips2184fb72019-02-21 16:11:41 -0500203
204 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
205 // place to purge stale blobs
206 this->getTextBlobCache()->purgeStaleBlobs();
Robert Phillips6eba0632018-03-28 12:25:42 -0400207}
208
Jim Van Verth76d917c2017-12-13 09:26:37 -0500209void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Yuqian Li40aa85f2019-07-02 13:45:00 -0700210 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
211
Brian Salomon5e150852017-03-22 14:53:13 -0400212 ASSERT_SINGLE_OWNER
Chris Dalton6c3879d2018-11-01 11:13:19 -0600213
Brian Salomon9241a6d2019-10-03 13:26:54 -0400214 if (this->abandoned()) {
215 return;
216 }
217
218 fMappedBufferManager->process();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600219 auto purgeTime = GrStdSteadyClock::now() - msNotUsed;
220
Jim Van Verth76d917c2017-12-13 09:26:37 -0500221 fResourceCache->purgeAsNeeded();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600222 fResourceCache->purgeResourcesNotUsedSince(purgeTime);
223
Robert Phillips6a6de562019-02-15 15:19:15 -0500224 if (auto ccpr = this->drawingManager()->getCoverageCountingPathRenderer()) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500225 ccpr->purgeCacheEntriesOlderThan(this->proxyProvider(), purgeTime);
Chris Dalton6c3879d2018-11-01 11:13:19 -0600226 }
Jim Van Verth76d917c2017-12-13 09:26:37 -0500227
Robert Phillips2184fb72019-02-21 16:11:41 -0500228 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
229 // place to purge stale blobs
230 this->getTextBlobCache()->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400231}
232
Derek Sollenberger5480a182017-05-25 16:43:59 -0400233void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
234 ASSERT_SINGLE_OWNER
Robert Phillipsddc21482019-10-16 14:30:09 -0400235
236 if (this->abandoned()) {
237 return;
238 }
239
Derek Sollenberger5480a182017-05-25 16:43:59 -0400240 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
241}
242
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000243void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800244 ASSERT_SINGLE_OWNER
245
bsalomon71cb0c22014-11-14 12:10:14 -0800246 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800247 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800248 }
249 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800250 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800251 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000252}
253
Derek Sollenbergeree479142017-05-24 11:41:33 -0400254size_t GrContext::getResourceCachePurgeableBytes() const {
255 ASSERT_SINGLE_OWNER
256 return fResourceCache->getPurgeableBytes();
257}
258
Greg Daniel8b666172019-10-09 12:38:22 -0400259size_t GrContext::ComputeImageSize(sk_sp<SkImage> image, GrMipMapped mipMapped, bool useNextPow2) {
260 if (!image->isTextureBacked()) {
261 return 0;
262 }
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400263 SkImage_GpuBase* gpuImage = static_cast<SkImage_GpuBase*>(as_IB(image.get()));
264 GrTextureProxy* proxy = gpuImage->peekProxy();
265 if (!proxy) {
266 return 0;
267 }
268
269 const GrCaps& caps = *gpuImage->context()->priv().caps();
Greg Daniel8b666172019-10-09 12:38:22 -0400270 int colorSamplesPerPixel = 1;
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400271 return GrSurface::ComputeSize(caps, proxy->backendFormat(), image->dimensions(),
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400272 colorSamplesPerPixel, mipMapped, useNextPow2);
Greg Daniel8b666172019-10-09 12:38:22 -0400273}
274
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000275////////////////////////////////////////////////////////////////////////////////
276
Robert Phillipsbb606772019-02-04 17:50:57 -0500277int GrContext::maxTextureSize() const { return this->caps()->maxTextureSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400278
Robert Phillipsbb606772019-02-04 17:50:57 -0500279int GrContext::maxRenderTargetSize() const { return this->caps()->maxRenderTargetSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400280
Brian Salomonbdecacf2018-02-02 20:32:49 -0500281bool GrContext::colorTypeSupportedAsImage(SkColorType colorType) const {
Greg Daniel7bfc9132019-08-14 14:23:53 -0400282 GrBackendFormat format =
283 this->caps()->getDefaultBackendFormat(SkColorTypeToGrColorType(colorType),
284 GrRenderable::kNo);
285 return format.isValid();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500286}
287
288int GrContext::maxSurfaceSampleCountForColorType(SkColorType colorType) const {
Greg Danieleadfac92019-08-02 09:03:53 -0400289 GrBackendFormat format =
290 this->caps()->getDefaultBackendFormat(SkColorTypeToGrColorType(colorType),
291 GrRenderable::kYes);
292 return this->caps()->maxRenderTargetSampleCount(format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500293}
294
295////////////////////////////////////////////////////////////////////////////////
296
Greg Daniel06be0792019-04-22 15:53:23 -0400297bool GrContext::wait(int numSemaphores, const GrBackendSemaphore waitSemaphores[]) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400298 if (!fGpu || fGpu->caps()->semaphoreSupport()) {
Greg Daniel06be0792019-04-22 15:53:23 -0400299 return false;
300 }
301 for (int i = 0; i < numSemaphores; ++i) {
Greg Daniel301015c2019-11-18 14:06:46 -0500302 std::unique_ptr<GrSemaphore> sema = fResourceProvider->wrapBackendSemaphore(
Greg Daniel06be0792019-04-22 15:53:23 -0400303 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
304 kAdopt_GrWrapOwnership);
Greg Daniel301015c2019-11-18 14:06:46 -0500305 fGpu->waitSemaphore(sema.get());
Greg Daniel06be0792019-04-22 15:53:23 -0400306 }
307 return true;
308}
309
310////////////////////////////////////////////////////////////////////////////////
311
Greg Daniel797efca2019-05-09 14:04:20 -0400312GrSemaphoresSubmitted GrContext::flush(const GrFlushInfo& info,
313 const GrPrepareForExternalIORequests& externalRequests) {
Greg Daniel51316782017-08-02 15:10:09 +0000314 ASSERT_SINGLE_OWNER
Robert Phillipsa9162df2019-02-11 14:12:03 -0500315 if (this->abandoned()) {
316 return GrSemaphoresSubmitted::kNo;
317 }
Greg Daniel51316782017-08-02 15:10:09 +0000318
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400319 return this->drawingManager()->flush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess,
Greg Daniel797efca2019-05-09 14:04:20 -0400320 info, externalRequests);
Greg Daniel51316782017-08-02 15:10:09 +0000321}
322
Greg Daniela870b462019-01-08 15:49:46 -0500323////////////////////////////////////////////////////////////////////////////////
324
Brian Salomonb0d8b762019-05-06 16:58:22 -0400325void GrContext::checkAsyncWorkCompletion() {
326 if (fGpu) {
327 fGpu->checkFinishProcs();
328 }
329}
330
331////////////////////////////////////////////////////////////////////////////////
332
Greg Daniela870b462019-01-08 15:49:46 -0500333void GrContext::storeVkPipelineCacheData() {
334 if (fGpu) {
335 fGpu->storeVkPipelineCacheData();
336 }
337}
338
339////////////////////////////////////////////////////////////////////////////////
340
Khushal3e7548c2018-05-23 15:45:01 -0700341bool GrContext::supportsDistanceFieldText() const {
Robert Phillipsbb606772019-02-04 17:50:57 -0500342 return this->caps()->shaderCaps()->supportsDistanceFieldText();
Khushal3e7548c2018-05-23 15:45:01 -0700343}
344
bsalomon37f9a262015-02-02 13:00:10 -0800345//////////////////////////////////////////////////////////////////////////////
346
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500347void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800348 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500349 if (maxResources) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400350 *maxResources = -1;
bsalomon37f9a262015-02-02 13:00:10 -0800351 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500352 if (maxResourceBytes) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400353 *maxResourceBytes = this->getResourceCacheLimit();
bsalomon37f9a262015-02-02 13:00:10 -0800354 }
355}
356
Robert Phillipscf39f372019-09-03 10:29:20 -0400357size_t GrContext::getResourceCacheLimit() const {
joshualitt1de610a2016-01-06 08:26:09 -0800358 ASSERT_SINGLE_OWNER
Robert Phillipscf39f372019-09-03 10:29:20 -0400359 return fResourceCache->getMaxResourceBytes();
360}
361
362void GrContext::setResourceCacheLimits(int unused, size_t maxResourceBytes) {
363 ASSERT_SINGLE_OWNER
364 this->setResourceCacheLimit(maxResourceBytes);
365}
366
367void GrContext::setResourceCacheLimit(size_t maxResourceBytes) {
368 ASSERT_SINGLE_OWNER
369 fResourceCache->setLimit(maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800370}
371
ericrk0a5fa482015-09-15 14:16:10 -0700372//////////////////////////////////////////////////////////////////////////////
ericrk0a5fa482015-09-15 14:16:10 -0700373void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800374 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700375 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
Khushal71652e22018-10-29 13:05:36 -0700376 traceMemoryDump->dumpNumericValue("skia/gr_text_blob_cache", "size", "bytes",
Robert Phillips2184fb72019-02-21 16:11:41 -0500377 this->getTextBlobCache()->usedBytes());
ericrk0a5fa482015-09-15 14:16:10 -0700378}
Brian Osman71a18892017-08-10 10:23:25 -0400379
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400380//////////////////////////////////////////////////////////////////////////////
381GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400382 const GrBackendFormat& backendFormat,
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400383 GrMipMapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400384 GrRenderable renderable,
385 GrProtected isProtected) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400386 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400387 if (!this->asDirectContext()) {
388 return GrBackendTexture();
389 }
390
391 if (this->abandoned()) {
392 return GrBackendTexture();
393 }
394
Robert Phillips4277f012020-01-21 14:28:34 -0500395 return fGpu->createBackendTexture({width, height}, backendFormat, renderable,
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500396 mipMapped, isProtected, nullptr);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400397}
398
399GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400400 SkColorType skColorType,
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400401 GrMipMapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400402 GrRenderable renderable,
403 GrProtected isProtected) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400404 if (!this->asDirectContext()) {
405 return GrBackendTexture();
406 }
407
408 if (this->abandoned()) {
409 return GrBackendTexture();
410 }
411
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400412 const GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400413
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400414 return this->createBackendTexture(width, height, format, mipMapped, renderable, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400415}
416
Robert Phillips02dc0302019-07-02 17:58:27 -0400417GrBackendTexture GrContext::createBackendTexture(const SkSurfaceCharacterization& c) {
Robert Phillips02dc0302019-07-02 17:58:27 -0400418 if (!this->asDirectContext() || !c.isValid()) {
419 return GrBackendTexture();
420 }
421
422 if (this->abandoned()) {
423 return GrBackendTexture();
424 }
425
426 if (c.usesGLFBO0()) {
427 // If we are making the surface we will never use FBO0.
428 return GrBackendTexture();
429 }
430
431 if (c.vulkanSecondaryCBCompatible()) {
432 return {};
433 }
434
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400435 const GrBackendFormat format = this->defaultBackendFormat(c.colorType(), GrRenderable::kYes);
Robert Phillips02dc0302019-07-02 17:58:27 -0400436 if (!format.isValid()) {
437 return GrBackendTexture();
438 }
439
Robert Phillips02dc0302019-07-02 17:58:27 -0400440 GrBackendTexture result = this->createBackendTexture(c.width(), c.height(), format,
441 GrMipMapped(c.isMipMapped()),
442 GrRenderable::kYes,
Robert Phillips3cd54322019-07-10 09:28:59 -0400443 c.isProtected());
Robert Phillips02dc0302019-07-02 17:58:27 -0400444 SkASSERT(c.isCompatible(result));
445 return result;
446}
447
448GrBackendTexture GrContext::createBackendTexture(const SkSurfaceCharacterization& c,
449 const SkColor4f& color) {
Robert Phillips02dc0302019-07-02 17:58:27 -0400450 if (!this->asDirectContext() || !c.isValid()) {
451 return GrBackendTexture();
452 }
453
454 if (this->abandoned()) {
455 return GrBackendTexture();
456 }
457
458 if (c.usesGLFBO0()) {
459 // If we are making the surface we will never use FBO0.
460 return GrBackendTexture();
461 }
462
463 if (c.vulkanSecondaryCBCompatible()) {
464 return {};
465 }
466
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400467 const GrBackendFormat format = this->defaultBackendFormat(c.colorType(), GrRenderable::kYes);
Robert Phillips02dc0302019-07-02 17:58:27 -0400468 if (!format.isValid()) {
469 return GrBackendTexture();
470 }
471
Robert Phillips02dc0302019-07-02 17:58:27 -0400472 GrBackendTexture result = this->createBackendTexture(c.width(), c.height(), format, color,
473 GrMipMapped(c.isMipMapped()),
474 GrRenderable::kYes,
Robert Phillips3cd54322019-07-10 09:28:59 -0400475 c.isProtected());
Robert Phillips02dc0302019-07-02 17:58:27 -0400476 SkASSERT(c.isCompatible(result));
477 return result;
478}
479
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400480GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400481 const GrBackendFormat& backendFormat,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400482 const SkColor4f& color,
483 GrMipMapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400484 GrRenderable renderable,
485 GrProtected isProtected) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400486 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400487 if (!this->asDirectContext()) {
488 return GrBackendTexture();
489 }
490
491 if (this->abandoned()) {
492 return GrBackendTexture();
493 }
494
Brian Salomon85c3d682019-11-04 15:04:54 -0500495 GrGpu::BackendTextureData data(color);
Robert Phillips4277f012020-01-21 14:28:34 -0500496 return fGpu->createBackendTexture({width, height}, backendFormat, renderable,
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500497 mipMapped, isProtected, &data);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400498}
499
500GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400501 SkColorType skColorType,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400502 const SkColor4f& color,
503 GrMipMapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400504 GrRenderable renderable,
505 GrProtected isProtected) {
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400506 if (!this->asDirectContext()) {
507 return GrBackendTexture();
508 }
509
510 if (this->abandoned()) {
511 return GrBackendTexture();
512 }
513
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400514 GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400515 if (!format.isValid()) {
516 return GrBackendTexture();
517 }
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400518
519 GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
Brian Salomon982f5462020-03-30 12:52:33 -0400520 SkColor4f swizzledColor = this->caps()->getWriteSwizzle(format, grColorType).applyTo(color);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400521
Brian Salomonb450f3b2019-07-09 09:36:51 -0400522 return this->createBackendTexture(width, height, format, swizzledColor, mipMapped, renderable,
523 isProtected);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400524}
525
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500526GrBackendTexture GrContext::createBackendTexture(const SkPixmap srcData[], int numProvidedLevels,
Robert Phillips66944402019-09-30 13:21:25 -0400527 GrRenderable renderable, GrProtected isProtected) {
528 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
529
530 if (!this->asDirectContext()) {
531 return {};
532 }
533
534 if (this->abandoned()) {
535 return {};
536 }
537
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500538 if (!srcData || numProvidedLevels <= 0) {
Robert Phillips66944402019-09-30 13:21:25 -0400539 return {};
540 }
541
542 int baseWidth = srcData[0].width();
543 int baseHeight = srcData[0].height();
544 SkColorType colorType = srcData[0].colorType();
545
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500546 GrMipMapped mipMapped = GrMipMapped::kNo;
547 int numExpectedLevels = 1;
548 if (numProvidedLevels > 1) {
549 numExpectedLevels = SkMipMap::ComputeLevelCount(baseWidth, baseHeight) + 1;
550 mipMapped = GrMipMapped::kYes;
551 }
552
553 if (numProvidedLevels != numExpectedLevels) {
554 return {};
555 }
556
Robert Phillips66944402019-09-30 13:21:25 -0400557 GrBackendFormat backendFormat = this->defaultBackendFormat(colorType, renderable);
558
Brian Salomon85c3d682019-11-04 15:04:54 -0500559 GrGpu::BackendTextureData data(srcData);
Robert Phillips4277f012020-01-21 14:28:34 -0500560 return fGpu->createBackendTexture({baseWidth, baseHeight}, backendFormat, renderable,
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500561 mipMapped, isProtected, &data);
Robert Phillips66944402019-09-30 13:21:25 -0400562}
563
Robert Phillipsb915c942019-12-17 14:44:37 -0500564//////////////////////////////////////////////////////////////////////////////
565
566GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
567 const GrBackendFormat& backendFormat,
568 const SkColor4f& color,
569 GrMipMapped mipMapped,
570 GrProtected isProtected) {
571 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
572 if (!this->asDirectContext()) {
573 return GrBackendTexture();
574 }
575
576 if (this->abandoned()) {
577 return GrBackendTexture();
578 }
579
580 GrGpu::BackendTextureData data(color);
Robert Phillips4277f012020-01-21 14:28:34 -0500581 return fGpu->createCompressedBackendTexture({width, height}, backendFormat,
582 mipMapped, isProtected, &data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500583}
584
585GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
586 SkImage::CompressionType compression,
587 const SkColor4f& color,
588 GrMipMapped mipMapped,
589 GrProtected isProtected) {
590 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
591 if (!this->asDirectContext()) {
592 return GrBackendTexture();
593 }
594
595 if (this->abandoned()) {
596 return GrBackendTexture();
597 }
598
599 GrBackendFormat format = this->compressedBackendFormat(compression);
600 return this->createCompressedBackendTexture(width, height, format, color,
601 mipMapped, isProtected);
602}
603
604GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
605 const GrBackendFormat& backendFormat,
606 const void* compressedData,
607 size_t dataSize,
608 GrMipMapped mipMapped,
609 GrProtected isProtected) {
610 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
611 if (!this->asDirectContext()) {
612 return GrBackendTexture();
613 }
614
615 if (this->abandoned()) {
616 return GrBackendTexture();
617 }
618
619 GrGpu::BackendTextureData data(compressedData, dataSize);
Robert Phillips4277f012020-01-21 14:28:34 -0500620 return fGpu->createCompressedBackendTexture({width, height}, backendFormat,
621 mipMapped, isProtected, &data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500622}
623
624GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
625 SkImage::CompressionType compression,
626 const void* data, size_t dataSize,
627 GrMipMapped mipMapped,
628 GrProtected isProtected) {
629 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
630 if (!this->asDirectContext()) {
631 return GrBackendTexture();
632 }
633
634 if (this->abandoned()) {
635 return GrBackendTexture();
636 }
637
638 GrBackendFormat format = this->compressedBackendFormat(compression);
639 return this->createCompressedBackendTexture(width, height, format, data, dataSize,
640 mipMapped, isProtected);
641}
642
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400643void GrContext::deleteBackendTexture(GrBackendTexture backendTex) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400644 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danielf0e04f02019-12-04 15:17:54 -0500645 // For the Vulkan backend we still must destroy the backend texture when the context is
646 // abandoned.
647 if ((this->abandoned() && this->backend() != GrBackendApi::kVulkan) || !backendTex.isValid()) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400648 return;
649 }
650
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400651 fGpu->deleteBackendTexture(backendTex);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400652}
653
Brian Osmaned58e002019-09-06 14:42:43 -0400654bool GrContext::precompileShader(const SkData& key, const SkData& data) {
655 return fGpu->precompileShader(key, data);
656}
657
Brian Salomonec22b1a2019-08-09 09:41:48 -0400658#ifdef SK_ENABLE_DUMP_GPU
Michael Ludwigdd205452020-03-30 17:16:34 -0400659#include "include/core/SkString.h"
Brian Salomonec22b1a2019-08-09 09:41:48 -0400660#include "src/utils/SkJSONWriter.h"
661SkString GrContext::dump() const {
662 SkDynamicMemoryWStream stream;
663 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
664 writer.beginObject();
665
666 writer.appendString("backend", GrBackendApiToStr(this->backend()));
667
668 writer.appendName("caps");
669 this->caps()->dumpJSON(&writer);
670
671 writer.appendName("gpu");
672 this->fGpu->dumpJSON(&writer);
673
674 // Flush JSON to the memory stream
675 writer.endObject();
676 writer.flush();
677
678 // Null terminate the JSON data in the memory stream
679 stream.write8(0);
680
681 // Allocate a string big enough to hold all the data, then copy out of the stream
682 SkString result(stream.bytesWritten());
683 stream.copyToAndReset(result.writable_str());
684 return result;
685}
686#endif