blob: f09936149d0a4416da19be768ee32f0af7f1a404 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkTraceMemoryDump.h"
11#include "include/gpu/GrBackendSemaphore.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/private/SkDeferredDisplayList.h"
13#include "include/private/SkImageInfoPriv.h"
14#include "src/core/SkMakeUnique.h"
15#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
Robert Phillipsbb606772019-02-04 17:50:57 -050067bool GrContext::init(sk_sp<const GrCaps> caps, sk_sp<GrSkSLFPFactoryCache> FPFactoryCache) {
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
Robert Phillipsbb606772019-02-04 17:50:57 -050072 if (!INHERITED::init(std::move(caps), std::move(FPFactoryCache))) {
73 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());
Brian Salomon9241a6d2019-10-03 13:26:54 -040083 fMappedBufferManager = skstd::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) {
95 fTaskGroup = skstd::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() {
Robert Phillipsa9162df2019-02-11 14:12:03 -0500114 if (this->abandoned()) {
115 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() {
Brian Salomon614c1a82018-12-19 15:42:06 -0500138 if (this->abandoned()) {
139 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
Brian Salomon1f05d452019-02-08 12:33:08 -0500158void GrContext::resetGLTextureBindings() {
159 if (this->abandoned() || this->backend() != GrBackendApi::kOpenGL) {
160 return;
161 }
162 fGpu->resetTextureBindings();
163}
164
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000165void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800166 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000167 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000168}
169
170void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800171 ASSERT_SINGLE_OWNER
172
Robert Phillips2184fb72019-02-21 16:11:41 -0500173 // TODO: the glyph cache doesn't hold any GpuResources so this call should not be needed here.
174 // Some slack in the GrTextBlob's implementation requires it though. That could be fixed.
Herb Derbya00da612019-03-04 17:10:01 -0500175 this->getGrStrikeCache()->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700176
Robert Phillips6a6de562019-02-15 15:19:15 -0500177 this->drawingManager()->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700178
179 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000180}
181
Robert Phillips6eba0632018-03-28 12:25:42 -0400182void GrContext::purgeUnlockedResources(bool scratchResourcesOnly) {
183 ASSERT_SINGLE_OWNER
Robert Phillipsddc21482019-10-16 14:30:09 -0400184
185 if (this->abandoned()) {
186 return;
187 }
188
Robert Phillips6eba0632018-03-28 12:25:42 -0400189 fResourceCache->purgeUnlockedResources(scratchResourcesOnly);
190 fResourceCache->purgeAsNeeded();
Robert Phillips2184fb72019-02-21 16:11:41 -0500191
192 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
193 // place to purge stale blobs
194 this->getTextBlobCache()->purgeStaleBlobs();
Robert Phillips6eba0632018-03-28 12:25:42 -0400195}
196
Jim Van Verth76d917c2017-12-13 09:26:37 -0500197void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Yuqian Li40aa85f2019-07-02 13:45:00 -0700198 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
199
Brian Salomon5e150852017-03-22 14:53:13 -0400200 ASSERT_SINGLE_OWNER
Chris Dalton6c3879d2018-11-01 11:13:19 -0600201
Brian Salomon9241a6d2019-10-03 13:26:54 -0400202 if (this->abandoned()) {
203 return;
204 }
205
206 fMappedBufferManager->process();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600207 auto purgeTime = GrStdSteadyClock::now() - msNotUsed;
208
Jim Van Verth76d917c2017-12-13 09:26:37 -0500209 fResourceCache->purgeAsNeeded();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600210 fResourceCache->purgeResourcesNotUsedSince(purgeTime);
211
Robert Phillips6a6de562019-02-15 15:19:15 -0500212 if (auto ccpr = this->drawingManager()->getCoverageCountingPathRenderer()) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500213 ccpr->purgeCacheEntriesOlderThan(this->proxyProvider(), purgeTime);
Chris Dalton6c3879d2018-11-01 11:13:19 -0600214 }
Jim Van Verth76d917c2017-12-13 09:26:37 -0500215
Robert Phillips2184fb72019-02-21 16:11:41 -0500216 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
217 // place to purge stale blobs
218 this->getTextBlobCache()->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400219}
220
Derek Sollenberger5480a182017-05-25 16:43:59 -0400221void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
222 ASSERT_SINGLE_OWNER
Robert Phillipsddc21482019-10-16 14:30:09 -0400223
224 if (this->abandoned()) {
225 return;
226 }
227
Derek Sollenberger5480a182017-05-25 16:43:59 -0400228 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
229}
230
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000231void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800232 ASSERT_SINGLE_OWNER
233
bsalomon71cb0c22014-11-14 12:10:14 -0800234 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800235 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800236 }
237 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800238 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800239 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000240}
241
Derek Sollenbergeree479142017-05-24 11:41:33 -0400242size_t GrContext::getResourceCachePurgeableBytes() const {
243 ASSERT_SINGLE_OWNER
244 return fResourceCache->getPurgeableBytes();
245}
246
Greg Daniel8b666172019-10-09 12:38:22 -0400247size_t GrContext::ComputeImageSize(sk_sp<SkImage> image, GrMipMapped mipMapped, bool useNextPow2) {
248 if (!image->isTextureBacked()) {
249 return 0;
250 }
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400251 SkImage_GpuBase* gpuImage = static_cast<SkImage_GpuBase*>(as_IB(image.get()));
252 GrTextureProxy* proxy = gpuImage->peekProxy();
253 if (!proxy) {
254 return 0;
255 }
256
257 const GrCaps& caps = *gpuImage->context()->priv().caps();
Greg Daniel8b666172019-10-09 12:38:22 -0400258 int colorSamplesPerPixel = 1;
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400259 return GrSurface::ComputeSize(caps, proxy->backendFormat(), image->dimensions(),
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400260 colorSamplesPerPixel, mipMapped, useNextPow2);
Greg Daniel8b666172019-10-09 12:38:22 -0400261}
262
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000263////////////////////////////////////////////////////////////////////////////////
264
Robert Phillipsbb606772019-02-04 17:50:57 -0500265int GrContext::maxTextureSize() const { return this->caps()->maxTextureSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400266
Robert Phillipsbb606772019-02-04 17:50:57 -0500267int GrContext::maxRenderTargetSize() const { return this->caps()->maxRenderTargetSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400268
Brian Salomonbdecacf2018-02-02 20:32:49 -0500269bool GrContext::colorTypeSupportedAsImage(SkColorType colorType) const {
Greg Daniel7bfc9132019-08-14 14:23:53 -0400270 GrBackendFormat format =
271 this->caps()->getDefaultBackendFormat(SkColorTypeToGrColorType(colorType),
272 GrRenderable::kNo);
273 return format.isValid();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500274}
275
276int GrContext::maxSurfaceSampleCountForColorType(SkColorType colorType) const {
Greg Danieleadfac92019-08-02 09:03:53 -0400277 GrBackendFormat format =
278 this->caps()->getDefaultBackendFormat(SkColorTypeToGrColorType(colorType),
279 GrRenderable::kYes);
280 return this->caps()->maxRenderTargetSampleCount(format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500281}
282
283////////////////////////////////////////////////////////////////////////////////
284
Greg Daniel06be0792019-04-22 15:53:23 -0400285bool GrContext::wait(int numSemaphores, const GrBackendSemaphore waitSemaphores[]) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400286 if (!fGpu || fGpu->caps()->semaphoreSupport()) {
Greg Daniel06be0792019-04-22 15:53:23 -0400287 return false;
288 }
289 for (int i = 0; i < numSemaphores; ++i) {
290 sk_sp<GrSemaphore> sema = fResourceProvider->wrapBackendSemaphore(
291 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
292 kAdopt_GrWrapOwnership);
293 fGpu->waitSemaphore(std::move(sema));
294 }
295 return true;
296}
297
298////////////////////////////////////////////////////////////////////////////////
299
Greg Daniel797efca2019-05-09 14:04:20 -0400300GrSemaphoresSubmitted GrContext::flush(const GrFlushInfo& info,
301 const GrPrepareForExternalIORequests& externalRequests) {
Greg Daniel51316782017-08-02 15:10:09 +0000302 ASSERT_SINGLE_OWNER
Robert Phillipsa9162df2019-02-11 14:12:03 -0500303 if (this->abandoned()) {
304 return GrSemaphoresSubmitted::kNo;
305 }
Greg Daniel51316782017-08-02 15:10:09 +0000306
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400307 return this->drawingManager()->flush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess,
Greg Daniel797efca2019-05-09 14:04:20 -0400308 info, externalRequests);
Greg Daniel51316782017-08-02 15:10:09 +0000309}
310
Greg Daniela870b462019-01-08 15:49:46 -0500311////////////////////////////////////////////////////////////////////////////////
312
Brian Salomonb0d8b762019-05-06 16:58:22 -0400313void GrContext::checkAsyncWorkCompletion() {
314 if (fGpu) {
315 fGpu->checkFinishProcs();
316 }
317}
318
319////////////////////////////////////////////////////////////////////////////////
320
Greg Daniela870b462019-01-08 15:49:46 -0500321void GrContext::storeVkPipelineCacheData() {
322 if (fGpu) {
323 fGpu->storeVkPipelineCacheData();
324 }
325}
326
327////////////////////////////////////////////////////////////////////////////////
328
Khushal3e7548c2018-05-23 15:45:01 -0700329bool GrContext::supportsDistanceFieldText() const {
Robert Phillipsbb606772019-02-04 17:50:57 -0500330 return this->caps()->shaderCaps()->supportsDistanceFieldText();
Khushal3e7548c2018-05-23 15:45:01 -0700331}
332
bsalomon37f9a262015-02-02 13:00:10 -0800333//////////////////////////////////////////////////////////////////////////////
334
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500335void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800336 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500337 if (maxResources) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400338 *maxResources = -1;
bsalomon37f9a262015-02-02 13:00:10 -0800339 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500340 if (maxResourceBytes) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400341 *maxResourceBytes = this->getResourceCacheLimit();
bsalomon37f9a262015-02-02 13:00:10 -0800342 }
343}
344
Robert Phillipscf39f372019-09-03 10:29:20 -0400345size_t GrContext::getResourceCacheLimit() const {
joshualitt1de610a2016-01-06 08:26:09 -0800346 ASSERT_SINGLE_OWNER
Robert Phillipscf39f372019-09-03 10:29:20 -0400347 return fResourceCache->getMaxResourceBytes();
348}
349
350void GrContext::setResourceCacheLimits(int unused, size_t maxResourceBytes) {
351 ASSERT_SINGLE_OWNER
352 this->setResourceCacheLimit(maxResourceBytes);
353}
354
355void GrContext::setResourceCacheLimit(size_t maxResourceBytes) {
356 ASSERT_SINGLE_OWNER
357 fResourceCache->setLimit(maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800358}
359
ericrk0a5fa482015-09-15 14:16:10 -0700360//////////////////////////////////////////////////////////////////////////////
ericrk0a5fa482015-09-15 14:16:10 -0700361void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800362 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700363 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
Khushal71652e22018-10-29 13:05:36 -0700364 traceMemoryDump->dumpNumericValue("skia/gr_text_blob_cache", "size", "bytes",
Robert Phillips2184fb72019-02-21 16:11:41 -0500365 this->getTextBlobCache()->usedBytes());
ericrk0a5fa482015-09-15 14:16:10 -0700366}
Brian Osman71a18892017-08-10 10:23:25 -0400367
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400368//////////////////////////////////////////////////////////////////////////////
369GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400370 const GrBackendFormat& backendFormat,
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400371 GrMipMapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400372 GrRenderable renderable,
373 GrProtected isProtected) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400374 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400375 if (!this->asDirectContext()) {
376 return GrBackendTexture();
377 }
378
379 if (this->abandoned()) {
380 return GrBackendTexture();
381 }
382
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400383 return fGpu->createBackendTexture(width, height, backendFormat,
384 mipMapped, renderable,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400385 nullptr, 0, nullptr, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400386}
387
388GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400389 SkColorType skColorType,
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400390 GrMipMapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400391 GrRenderable renderable,
392 GrProtected isProtected) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400393 if (!this->asDirectContext()) {
394 return GrBackendTexture();
395 }
396
397 if (this->abandoned()) {
398 return GrBackendTexture();
399 }
400
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400401 const GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400402
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400403 return this->createBackendTexture(width, height, format, mipMapped, renderable, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400404}
405
Robert Phillips02dc0302019-07-02 17:58:27 -0400406GrBackendTexture GrContext::createBackendTexture(const SkSurfaceCharacterization& c) {
Robert Phillips02dc0302019-07-02 17:58:27 -0400407 if (!this->asDirectContext() || !c.isValid()) {
408 return GrBackendTexture();
409 }
410
411 if (this->abandoned()) {
412 return GrBackendTexture();
413 }
414
415 if (c.usesGLFBO0()) {
416 // If we are making the surface we will never use FBO0.
417 return GrBackendTexture();
418 }
419
420 if (c.vulkanSecondaryCBCompatible()) {
421 return {};
422 }
423
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400424 const GrBackendFormat format = this->defaultBackendFormat(c.colorType(), GrRenderable::kYes);
Robert Phillips02dc0302019-07-02 17:58:27 -0400425 if (!format.isValid()) {
426 return GrBackendTexture();
427 }
428
Robert Phillips02dc0302019-07-02 17:58:27 -0400429 GrBackendTexture result = this->createBackendTexture(c.width(), c.height(), format,
430 GrMipMapped(c.isMipMapped()),
431 GrRenderable::kYes,
Robert Phillips3cd54322019-07-10 09:28:59 -0400432 c.isProtected());
Robert Phillips02dc0302019-07-02 17:58:27 -0400433 SkASSERT(c.isCompatible(result));
434 return result;
435}
436
437GrBackendTexture GrContext::createBackendTexture(const SkSurfaceCharacterization& c,
438 const SkColor4f& color) {
Robert Phillips02dc0302019-07-02 17:58:27 -0400439 if (!this->asDirectContext() || !c.isValid()) {
440 return GrBackendTexture();
441 }
442
443 if (this->abandoned()) {
444 return GrBackendTexture();
445 }
446
447 if (c.usesGLFBO0()) {
448 // If we are making the surface we will never use FBO0.
449 return GrBackendTexture();
450 }
451
452 if (c.vulkanSecondaryCBCompatible()) {
453 return {};
454 }
455
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400456 const GrBackendFormat format = this->defaultBackendFormat(c.colorType(), GrRenderable::kYes);
Robert Phillips02dc0302019-07-02 17:58:27 -0400457 if (!format.isValid()) {
458 return GrBackendTexture();
459 }
460
Robert Phillips02dc0302019-07-02 17:58:27 -0400461 GrBackendTexture result = this->createBackendTexture(c.width(), c.height(), format, color,
462 GrMipMapped(c.isMipMapped()),
463 GrRenderable::kYes,
Robert Phillips3cd54322019-07-10 09:28:59 -0400464 c.isProtected());
Robert Phillips02dc0302019-07-02 17:58:27 -0400465 SkASSERT(c.isCompatible(result));
466 return result;
467}
468
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400469GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400470 const GrBackendFormat& backendFormat,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400471 const SkColor4f& color,
472 GrMipMapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400473 GrRenderable renderable,
474 GrProtected isProtected) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400475 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400476 if (!this->asDirectContext()) {
477 return GrBackendTexture();
478 }
479
480 if (this->abandoned()) {
481 return GrBackendTexture();
482 }
483
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400484 return fGpu->createBackendTexture(width, height, backendFormat,
485 mipMapped, renderable,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400486 nullptr, 0, &color, isProtected);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400487}
488
489GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400490 SkColorType skColorType,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400491 const SkColor4f& color,
492 GrMipMapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400493 GrRenderable renderable,
494 GrProtected isProtected) {
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400495 if (!this->asDirectContext()) {
496 return GrBackendTexture();
497 }
498
499 if (this->abandoned()) {
500 return GrBackendTexture();
501 }
502
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400503 GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400504 if (!format.isValid()) {
505 return GrBackendTexture();
506 }
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400507
508 GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
509 SkColor4f swizzledColor = this->caps()->getOutputSwizzle(format, grColorType).applyTo(color);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400510
Brian Salomonb450f3b2019-07-09 09:36:51 -0400511 return this->createBackendTexture(width, height, format, swizzledColor, mipMapped, renderable,
512 isProtected);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400513}
514
Robert Phillips66944402019-09-30 13:21:25 -0400515GrBackendTexture GrContext::createBackendTexture(const SkPixmap srcData[], int numLevels,
516 GrRenderable renderable, GrProtected isProtected) {
517 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
518
519 if (!this->asDirectContext()) {
520 return {};
521 }
522
523 if (this->abandoned()) {
524 return {};
525 }
526
527 if (!srcData || !numLevels) {
528 return {};
529 }
530
531 int baseWidth = srcData[0].width();
532 int baseHeight = srcData[0].height();
533 SkColorType colorType = srcData[0].colorType();
534
535 GrBackendFormat backendFormat = this->defaultBackendFormat(colorType, renderable);
536
537 return fGpu->createBackendTexture(baseWidth, baseHeight, backendFormat,
538 numLevels > 1 ? GrMipMapped::kYes : GrMipMapped::kNo,
539 renderable, srcData, numLevels, nullptr, isProtected);
540}
541
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400542void GrContext::deleteBackendTexture(GrBackendTexture backendTex) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400543 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400544 if (this->abandoned() || !backendTex.isValid()) {
545 return;
546 }
547
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400548 fGpu->deleteBackendTexture(backendTex);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400549}
550
Brian Osmaned58e002019-09-06 14:42:43 -0400551bool GrContext::precompileShader(const SkData& key, const SkData& data) {
552 return fGpu->precompileShader(key, data);
553}
554
Brian Salomonec22b1a2019-08-09 09:41:48 -0400555#ifdef SK_ENABLE_DUMP_GPU
556#include "src/utils/SkJSONWriter.h"
557SkString GrContext::dump() const {
558 SkDynamicMemoryWStream stream;
559 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
560 writer.beginObject();
561
562 writer.appendString("backend", GrBackendApiToStr(this->backend()));
563
564 writer.appendName("caps");
565 this->caps()->dumpJSON(&writer);
566
567 writer.appendName("gpu");
568 this->fGpu->dumpJSON(&writer);
569
570 // Flush JSON to the memory stream
571 writer.endObject();
572 writer.flush();
573
574 // Null terminate the JSON data in the memory stream
575 stream.write8(0);
576
577 // Allocate a string big enough to hold all the data, then copy out of the stream
578 SkString result(stream.bytesWritten());
579 stream.copyToAndReset(result.writable_str());
580 return result;
581}
582#endif