blob: d150ed4f34f2c758263aa2d8b9402e0fa3f02df4 [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"
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"
Robert Phillips7f11fb52019-12-03 13:35:19 -050028#include "src/gpu/GrSkSLFPFactoryCache.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/GrSoftwarePathRenderer.h"
30#include "src/gpu/GrTracing.h"
31#include "src/gpu/SkGr.h"
32#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
33#include "src/gpu/effects/GrSkSLFP.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/gpu/text/GrTextBlobCache.h"
35#include "src/gpu/text/GrTextContext.h"
Greg Daniel7fd7a8a2019-10-10 16:10:31 -040036#include "src/image/SkImage_GpuBase.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "src/image/SkSurface_Gpu.h"
Mike Klein0ec1c572018-12-04 11:52:51 -050038#include <atomic>
Greg Danielb76a72a2017-07-13 15:07:54 -040039
Robert Phillipse78b7252017-04-06 07:59:41 -040040#define ASSERT_OWNED_PROXY(P) \
Brian Salomonfd98c2c2018-07-31 17:25:29 -040041 SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == this)
Robert Phillips7ee385e2017-03-30 08:02:11 -040042
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000043#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt1de610a2016-01-06 08:26:09 -080044#define ASSERT_SINGLE_OWNER \
Robert Phillipsa41c6852019-02-07 10:44:10 -050045 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
Robert Phillipsa9162df2019-02-11 14:12:03 -050046#define RETURN_IF_ABANDONED if (this->abandoned()) { return; }
47#define RETURN_FALSE_IF_ABANDONED if (this->abandoned()) { return false; }
48#define RETURN_NULL_IF_ABANDONED if (this->abandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000049
robertphillipsea461502015-05-26 11:38:03 -070050////////////////////////////////////////////////////////////////////////////////
51
Robert Phillipsa41c6852019-02-07 10:44:10 -050052GrContext::GrContext(GrBackendApi backend, const GrContextOptions& options, int32_t contextID)
53 : INHERITED(backend, options, contextID) {
halcanary96fcdcc2015-08-27 07:41:13 -070054 fResourceCache = nullptr;
55 fResourceProvider = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000056}
57
Robert Phillips292a6b22019-02-14 14:49:02 -050058GrContext::~GrContext() {
59 ASSERT_SINGLE_OWNER
60
Robert Phillips6a6de562019-02-15 15:19:15 -050061 if (this->drawingManager()) {
62 this->drawingManager()->cleanup();
Robert Phillips292a6b22019-02-14 14:49:02 -050063 }
64 delete fResourceProvider;
65 delete fResourceCache;
Robert Phillips292a6b22019-02-14 14:49:02 -050066}
67
Robert Phillipsbb606772019-02-04 17:50:57 -050068bool GrContext::init(sk_sp<const GrCaps> caps, sk_sp<GrSkSLFPFactoryCache> FPFactoryCache) {
Greg Danielb76a72a2017-07-13 15:07:54 -040069 ASSERT_SINGLE_OWNER
Robert Phillipsfde6fa02018-03-02 08:53:14 -050070 SkASSERT(fThreadSafeProxy); // needs to have been initialized by derived classes
Robert Phillipsa41c6852019-02-07 10:44:10 -050071 SkASSERT(this->proxyProvider());
Robert Phillips88260b52018-01-19 12:56:09 -050072
Robert Phillipsbb606772019-02-04 17:50:57 -050073 if (!INHERITED::init(std::move(caps), std::move(FPFactoryCache))) {
74 return false;
75 }
76
77 SkASSERT(this->caps());
Herb Derbya00da612019-03-04 17:10:01 -050078 SkASSERT(this->getGrStrikeCache());
Robert Phillips2184fb72019-02-21 16:11:41 -050079 SkASSERT(this->getTextBlobCache());
Robert Phillipsbb606772019-02-04 17:50:57 -050080
Robert Phillips88260b52018-01-19 12:56:09 -050081 if (fGpu) {
Robert Phillipsa41c6852019-02-07 10:44:10 -050082 fResourceCache = new GrResourceCache(this->caps(), this->singleOwner(), this->contextID());
Robert Phillips12c46292019-04-23 07:36:17 -040083 fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, this->singleOwner());
Mike Kleinf46d5ca2019-12-11 10:45:01 -050084 fMappedBufferManager = std::make_unique<GrClientMappedBufferManager>(this->contextID());
Robert Phillips88260b52018-01-19 12:56:09 -050085 }
86
Robert Phillips88260b52018-01-19 12:56:09 -050087 if (fResourceCache) {
Robert Phillipsa41c6852019-02-07 10:44:10 -050088 fResourceCache->setProxyProvider(this->proxyProvider());
Robert Phillips88260b52018-01-19 12:56:09 -050089 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050090
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000091 fDidTestPMConversions = false;
92
Robert Phillipsfde6fa02018-03-02 08:53:14 -050093 // DDL TODO: we need to think through how the task group & persistent cache
94 // get passed on to/shared between all the DDLRecorders created with this context.
Robert Phillipsc1541ae2019-02-04 12:05:37 -050095 if (this->options().fExecutor) {
Mike Kleinf46d5ca2019-12-11 10:45:01 -050096 fTaskGroup = std::make_unique<SkTaskGroup>(*this->options().fExecutor);
Brian Osman51279982017-08-23 10:12:00 -040097 }
98
Robert Phillipsc1541ae2019-02-04 12:05:37 -050099 fPersistentCache = this->options().fPersistentCache;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400100 fShaderErrorHandler = this->options().fShaderErrorHandler;
101 if (!fShaderErrorHandler) {
102 fShaderErrorHandler = GrShaderUtils::DefaultShaderErrorHandler();
103 }
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400104
Brian Salomon91a3e522017-06-23 10:58:19 -0400105 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000106}
107
Robert Phillips4217ea72019-01-30 13:08:28 -0500108sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
109 return fThreadSafeProxy;
110}
111
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400112//////////////////////////////////////////////////////////////////////////////
113
bsalomon2354f842014-07-28 13:48:36 -0700114void GrContext::abandonContext() {
Robert Phillipsa9162df2019-02-11 14:12:03 -0500115 if (this->abandoned()) {
116 return;
117 }
joshualitt1de610a2016-01-06 08:26:09 -0800118
Robert Phillipsa9162df2019-02-11 14:12:03 -0500119 INHERITED::abandonContext();
120
Brian Salomon9241a6d2019-10-03 13:26:54 -0400121 fMappedBufferManager->abandon();
122
bsalomond309e7a2015-04-30 14:18:54 -0700123 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800124
Robert Phillipsa9162df2019-02-11 14:12:03 -0500125 // Need to cleanup the drawing manager first so all the render targets
robertphillips0dfa62c2015-11-16 06:23:31 -0800126 // will be released/forgotten before they too are abandoned.
Robert Phillips6a6de562019-02-15 15:19:15 -0500127 this->drawingManager()->cleanup();
robertphillips0dfa62c2015-11-16 06:23:31 -0800128
bsalomon@google.com205d4602011-04-25 12:43:45 +0000129 // abandon first to so destructors
130 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800131 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700132
bsalomon6e2aad42016-04-01 11:54:31 -0700133 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400134
135 fMappedBufferManager.reset();
Khushalc421ca12018-06-26 14:38:34 -0700136}
137
bsalomon6e2aad42016-04-01 11:54:31 -0700138void GrContext::releaseResourcesAndAbandonContext() {
Brian Salomon614c1a82018-12-19 15:42:06 -0500139 if (this->abandoned()) {
140 return;
141 }
Robert Phillipsa9162df2019-02-11 14:12:03 -0500142
143 INHERITED::abandonContext();
144
Brian Salomon9241a6d2019-10-03 13:26:54 -0400145 fMappedBufferManager.reset();
146
bsalomon6e2aad42016-04-01 11:54:31 -0700147 fResourceProvider->abandon();
148
Robert Phillipsa9162df2019-02-11 14:12:03 -0500149 // Need to cleanup the drawing manager first so all the render targets
bsalomon6e2aad42016-04-01 11:54:31 -0700150 // will be released/forgotten before they too are abandoned.
Robert Phillips6a6de562019-02-15 15:19:15 -0500151 this->drawingManager()->cleanup();
bsalomon6e2aad42016-04-01 11:54:31 -0700152
153 // Release all resources in the backend 3D API.
154 fResourceCache->releaseAll();
155
156 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000157}
158
Brian Salomon1f05d452019-02-08 12:33:08 -0500159void GrContext::resetGLTextureBindings() {
160 if (this->abandoned() || this->backend() != GrBackendApi::kOpenGL) {
161 return;
162 }
163 fGpu->resetTextureBindings();
164}
165
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000166void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800167 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000168 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000169}
170
171void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800172 ASSERT_SINGLE_OWNER
173
Robert Phillips2184fb72019-02-21 16:11:41 -0500174 // TODO: the glyph cache doesn't hold any GpuResources so this call should not be needed here.
175 // Some slack in the GrTextBlob's implementation requires it though. That could be fixed.
Herb Derbya00da612019-03-04 17:10:01 -0500176 this->getGrStrikeCache()->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700177
Robert Phillips6a6de562019-02-15 15:19:15 -0500178 this->drawingManager()->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700179
180 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000181}
182
Robert Phillips6eba0632018-03-28 12:25:42 -0400183void GrContext::purgeUnlockedResources(bool scratchResourcesOnly) {
184 ASSERT_SINGLE_OWNER
Robert Phillipsddc21482019-10-16 14:30:09 -0400185
186 if (this->abandoned()) {
187 return;
188 }
189
Robert Phillips6eba0632018-03-28 12:25:42 -0400190 fResourceCache->purgeUnlockedResources(scratchResourcesOnly);
191 fResourceCache->purgeAsNeeded();
Robert Phillips2184fb72019-02-21 16:11:41 -0500192
193 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
194 // place to purge stale blobs
195 this->getTextBlobCache()->purgeStaleBlobs();
Robert Phillips6eba0632018-03-28 12:25:42 -0400196}
197
Jim Van Verth76d917c2017-12-13 09:26:37 -0500198void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Yuqian Li40aa85f2019-07-02 13:45:00 -0700199 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
200
Brian Salomon5e150852017-03-22 14:53:13 -0400201 ASSERT_SINGLE_OWNER
Chris Dalton6c3879d2018-11-01 11:13:19 -0600202
Brian Salomon9241a6d2019-10-03 13:26:54 -0400203 if (this->abandoned()) {
204 return;
205 }
206
207 fMappedBufferManager->process();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600208 auto purgeTime = GrStdSteadyClock::now() - msNotUsed;
209
Jim Van Verth76d917c2017-12-13 09:26:37 -0500210 fResourceCache->purgeAsNeeded();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600211 fResourceCache->purgeResourcesNotUsedSince(purgeTime);
212
Robert Phillips6a6de562019-02-15 15:19:15 -0500213 if (auto ccpr = this->drawingManager()->getCoverageCountingPathRenderer()) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500214 ccpr->purgeCacheEntriesOlderThan(this->proxyProvider(), purgeTime);
Chris Dalton6c3879d2018-11-01 11:13:19 -0600215 }
Jim Van Verth76d917c2017-12-13 09:26:37 -0500216
Robert Phillips2184fb72019-02-21 16:11:41 -0500217 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
218 // place to purge stale blobs
219 this->getTextBlobCache()->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400220}
221
Derek Sollenberger5480a182017-05-25 16:43:59 -0400222void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
223 ASSERT_SINGLE_OWNER
Robert Phillipsddc21482019-10-16 14:30:09 -0400224
225 if (this->abandoned()) {
226 return;
227 }
228
Derek Sollenberger5480a182017-05-25 16:43:59 -0400229 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
230}
231
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000232void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800233 ASSERT_SINGLE_OWNER
234
bsalomon71cb0c22014-11-14 12:10:14 -0800235 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800236 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800237 }
238 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800239 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800240 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000241}
242
Derek Sollenbergeree479142017-05-24 11:41:33 -0400243size_t GrContext::getResourceCachePurgeableBytes() const {
244 ASSERT_SINGLE_OWNER
245 return fResourceCache->getPurgeableBytes();
246}
247
Greg Daniel8b666172019-10-09 12:38:22 -0400248size_t GrContext::ComputeImageSize(sk_sp<SkImage> image, GrMipMapped mipMapped, bool useNextPow2) {
249 if (!image->isTextureBacked()) {
250 return 0;
251 }
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400252 SkImage_GpuBase* gpuImage = static_cast<SkImage_GpuBase*>(as_IB(image.get()));
253 GrTextureProxy* proxy = gpuImage->peekProxy();
254 if (!proxy) {
255 return 0;
256 }
257
258 const GrCaps& caps = *gpuImage->context()->priv().caps();
Greg Daniel8b666172019-10-09 12:38:22 -0400259 int colorSamplesPerPixel = 1;
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400260 return GrSurface::ComputeSize(caps, proxy->backendFormat(), image->dimensions(),
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400261 colorSamplesPerPixel, mipMapped, useNextPow2);
Greg Daniel8b666172019-10-09 12:38:22 -0400262}
263
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000264////////////////////////////////////////////////////////////////////////////////
265
Robert Phillipsbb606772019-02-04 17:50:57 -0500266int GrContext::maxTextureSize() const { return this->caps()->maxTextureSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400267
Robert Phillipsbb606772019-02-04 17:50:57 -0500268int GrContext::maxRenderTargetSize() const { return this->caps()->maxRenderTargetSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400269
Brian Salomonbdecacf2018-02-02 20:32:49 -0500270bool GrContext::colorTypeSupportedAsImage(SkColorType colorType) const {
Greg Daniel7bfc9132019-08-14 14:23:53 -0400271 GrBackendFormat format =
272 this->caps()->getDefaultBackendFormat(SkColorTypeToGrColorType(colorType),
273 GrRenderable::kNo);
274 return format.isValid();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500275}
276
277int GrContext::maxSurfaceSampleCountForColorType(SkColorType colorType) const {
Greg Danieleadfac92019-08-02 09:03:53 -0400278 GrBackendFormat format =
279 this->caps()->getDefaultBackendFormat(SkColorTypeToGrColorType(colorType),
280 GrRenderable::kYes);
281 return this->caps()->maxRenderTargetSampleCount(format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500282}
283
284////////////////////////////////////////////////////////////////////////////////
285
Greg Daniel06be0792019-04-22 15:53:23 -0400286bool GrContext::wait(int numSemaphores, const GrBackendSemaphore waitSemaphores[]) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400287 if (!fGpu || fGpu->caps()->semaphoreSupport()) {
Greg Daniel06be0792019-04-22 15:53:23 -0400288 return false;
289 }
290 for (int i = 0; i < numSemaphores; ++i) {
Greg Daniel301015c2019-11-18 14:06:46 -0500291 std::unique_ptr<GrSemaphore> sema = fResourceProvider->wrapBackendSemaphore(
Greg Daniel06be0792019-04-22 15:53:23 -0400292 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
293 kAdopt_GrWrapOwnership);
Greg Daniel301015c2019-11-18 14:06:46 -0500294 fGpu->waitSemaphore(sema.get());
Greg Daniel06be0792019-04-22 15:53:23 -0400295 }
296 return true;
297}
298
299////////////////////////////////////////////////////////////////////////////////
300
Greg Daniel797efca2019-05-09 14:04:20 -0400301GrSemaphoresSubmitted GrContext::flush(const GrFlushInfo& info,
302 const GrPrepareForExternalIORequests& externalRequests) {
Greg Daniel51316782017-08-02 15:10:09 +0000303 ASSERT_SINGLE_OWNER
Robert Phillipsa9162df2019-02-11 14:12:03 -0500304 if (this->abandoned()) {
305 return GrSemaphoresSubmitted::kNo;
306 }
Greg Daniel51316782017-08-02 15:10:09 +0000307
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400308 return this->drawingManager()->flush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess,
Greg Daniel797efca2019-05-09 14:04:20 -0400309 info, externalRequests);
Greg Daniel51316782017-08-02 15:10:09 +0000310}
311
Greg Daniela870b462019-01-08 15:49:46 -0500312////////////////////////////////////////////////////////////////////////////////
313
Brian Salomonb0d8b762019-05-06 16:58:22 -0400314void GrContext::checkAsyncWorkCompletion() {
315 if (fGpu) {
316 fGpu->checkFinishProcs();
317 }
318}
319
320////////////////////////////////////////////////////////////////////////////////
321
Greg Daniela870b462019-01-08 15:49:46 -0500322void GrContext::storeVkPipelineCacheData() {
323 if (fGpu) {
324 fGpu->storeVkPipelineCacheData();
325 }
326}
327
328////////////////////////////////////////////////////////////////////////////////
329
Khushal3e7548c2018-05-23 15:45:01 -0700330bool GrContext::supportsDistanceFieldText() const {
Robert Phillipsbb606772019-02-04 17:50:57 -0500331 return this->caps()->shaderCaps()->supportsDistanceFieldText();
Khushal3e7548c2018-05-23 15:45:01 -0700332}
333
bsalomon37f9a262015-02-02 13:00:10 -0800334//////////////////////////////////////////////////////////////////////////////
335
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500336void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800337 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500338 if (maxResources) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400339 *maxResources = -1;
bsalomon37f9a262015-02-02 13:00:10 -0800340 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500341 if (maxResourceBytes) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400342 *maxResourceBytes = this->getResourceCacheLimit();
bsalomon37f9a262015-02-02 13:00:10 -0800343 }
344}
345
Robert Phillipscf39f372019-09-03 10:29:20 -0400346size_t GrContext::getResourceCacheLimit() const {
joshualitt1de610a2016-01-06 08:26:09 -0800347 ASSERT_SINGLE_OWNER
Robert Phillipscf39f372019-09-03 10:29:20 -0400348 return fResourceCache->getMaxResourceBytes();
349}
350
351void GrContext::setResourceCacheLimits(int unused, size_t maxResourceBytes) {
352 ASSERT_SINGLE_OWNER
353 this->setResourceCacheLimit(maxResourceBytes);
354}
355
356void GrContext::setResourceCacheLimit(size_t maxResourceBytes) {
357 ASSERT_SINGLE_OWNER
358 fResourceCache->setLimit(maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800359}
360
ericrk0a5fa482015-09-15 14:16:10 -0700361//////////////////////////////////////////////////////////////////////////////
ericrk0a5fa482015-09-15 14:16:10 -0700362void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800363 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700364 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
Khushal71652e22018-10-29 13:05:36 -0700365 traceMemoryDump->dumpNumericValue("skia/gr_text_blob_cache", "size", "bytes",
Robert Phillips2184fb72019-02-21 16:11:41 -0500366 this->getTextBlobCache()->usedBytes());
ericrk0a5fa482015-09-15 14:16:10 -0700367}
Brian Osman71a18892017-08-10 10:23:25 -0400368
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400369//////////////////////////////////////////////////////////////////////////////
370GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400371 const GrBackendFormat& backendFormat,
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400372 GrMipMapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400373 GrRenderable renderable,
374 GrProtected isProtected) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400375 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400376 if (!this->asDirectContext()) {
377 return GrBackendTexture();
378 }
379
380 if (this->abandoned()) {
381 return GrBackendTexture();
382 }
383
Brian Salomon85c3d682019-11-04 15:04:54 -0500384 int numMipLevels = 1;
385 if (mipMapped == GrMipMapped::kYes) {
386 numMipLevels = SkMipMap::ComputeLevelCount(width, height) + 1;
387 }
388
389 return fGpu->createBackendTexture({width, height}, backendFormat, renderable, nullptr,
390 numMipLevels, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400391}
392
393GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400394 SkColorType skColorType,
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400395 GrMipMapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400396 GrRenderable renderable,
397 GrProtected isProtected) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400398 if (!this->asDirectContext()) {
399 return GrBackendTexture();
400 }
401
402 if (this->abandoned()) {
403 return GrBackendTexture();
404 }
405
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400406 const GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400407
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400408 return this->createBackendTexture(width, height, format, mipMapped, renderable, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400409}
410
Robert Phillips02dc0302019-07-02 17:58:27 -0400411GrBackendTexture GrContext::createBackendTexture(const SkSurfaceCharacterization& c) {
Robert Phillips02dc0302019-07-02 17:58:27 -0400412 if (!this->asDirectContext() || !c.isValid()) {
413 return GrBackendTexture();
414 }
415
416 if (this->abandoned()) {
417 return GrBackendTexture();
418 }
419
420 if (c.usesGLFBO0()) {
421 // If we are making the surface we will never use FBO0.
422 return GrBackendTexture();
423 }
424
425 if (c.vulkanSecondaryCBCompatible()) {
426 return {};
427 }
428
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400429 const GrBackendFormat format = this->defaultBackendFormat(c.colorType(), GrRenderable::kYes);
Robert Phillips02dc0302019-07-02 17:58:27 -0400430 if (!format.isValid()) {
431 return GrBackendTexture();
432 }
433
Robert Phillips02dc0302019-07-02 17:58:27 -0400434 GrBackendTexture result = this->createBackendTexture(c.width(), c.height(), format,
435 GrMipMapped(c.isMipMapped()),
436 GrRenderable::kYes,
Robert Phillips3cd54322019-07-10 09:28:59 -0400437 c.isProtected());
Robert Phillips02dc0302019-07-02 17:58:27 -0400438 SkASSERT(c.isCompatible(result));
439 return result;
440}
441
442GrBackendTexture GrContext::createBackendTexture(const SkSurfaceCharacterization& c,
443 const SkColor4f& color) {
Robert Phillips02dc0302019-07-02 17:58:27 -0400444 if (!this->asDirectContext() || !c.isValid()) {
445 return GrBackendTexture();
446 }
447
448 if (this->abandoned()) {
449 return GrBackendTexture();
450 }
451
452 if (c.usesGLFBO0()) {
453 // If we are making the surface we will never use FBO0.
454 return GrBackendTexture();
455 }
456
457 if (c.vulkanSecondaryCBCompatible()) {
458 return {};
459 }
460
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400461 const GrBackendFormat format = this->defaultBackendFormat(c.colorType(), GrRenderable::kYes);
Robert Phillips02dc0302019-07-02 17:58:27 -0400462 if (!format.isValid()) {
463 return GrBackendTexture();
464 }
465
Robert Phillips02dc0302019-07-02 17:58:27 -0400466 GrBackendTexture result = this->createBackendTexture(c.width(), c.height(), format, color,
467 GrMipMapped(c.isMipMapped()),
468 GrRenderable::kYes,
Robert Phillips3cd54322019-07-10 09:28:59 -0400469 c.isProtected());
Robert Phillips02dc0302019-07-02 17:58:27 -0400470 SkASSERT(c.isCompatible(result));
471 return result;
472}
473
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400474GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400475 const GrBackendFormat& backendFormat,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400476 const SkColor4f& color,
477 GrMipMapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400478 GrRenderable renderable,
479 GrProtected isProtected) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400480 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400481 if (!this->asDirectContext()) {
482 return GrBackendTexture();
483 }
484
485 if (this->abandoned()) {
486 return GrBackendTexture();
487 }
488
Brian Salomon85c3d682019-11-04 15:04:54 -0500489 int numMipLevels = 1;
490 if (mipMapped == GrMipMapped::kYes) {
491 numMipLevels = SkMipMap::ComputeLevelCount(width, height) + 1;
492 }
493 GrGpu::BackendTextureData data(color);
494 return fGpu->createBackendTexture({width, height}, backendFormat, renderable, &data,
495 numMipLevels, isProtected);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400496}
497
498GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400499 SkColorType skColorType,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400500 const SkColor4f& color,
501 GrMipMapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400502 GrRenderable renderable,
503 GrProtected isProtected) {
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400504 if (!this->asDirectContext()) {
505 return GrBackendTexture();
506 }
507
508 if (this->abandoned()) {
509 return GrBackendTexture();
510 }
511
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400512 GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400513 if (!format.isValid()) {
514 return GrBackendTexture();
515 }
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400516
517 GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
518 SkColor4f swizzledColor = this->caps()->getOutputSwizzle(format, grColorType).applyTo(color);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400519
Brian Salomonb450f3b2019-07-09 09:36:51 -0400520 return this->createBackendTexture(width, height, format, swizzledColor, mipMapped, renderable,
521 isProtected);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400522}
523
Robert Phillips66944402019-09-30 13:21:25 -0400524GrBackendTexture GrContext::createBackendTexture(const SkPixmap srcData[], int numLevels,
525 GrRenderable renderable, GrProtected isProtected) {
526 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
527
528 if (!this->asDirectContext()) {
529 return {};
530 }
531
532 if (this->abandoned()) {
533 return {};
534 }
535
536 if (!srcData || !numLevels) {
537 return {};
538 }
539
540 int baseWidth = srcData[0].width();
541 int baseHeight = srcData[0].height();
542 SkColorType colorType = srcData[0].colorType();
543
544 GrBackendFormat backendFormat = this->defaultBackendFormat(colorType, renderable);
545
Brian Salomon85c3d682019-11-04 15:04:54 -0500546 GrGpu::BackendTextureData data(srcData);
547 return fGpu->createBackendTexture({baseWidth, baseHeight}, backendFormat, renderable, &data,
548 numLevels, isProtected);
Robert Phillips66944402019-09-30 13:21:25 -0400549}
550
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400551void GrContext::deleteBackendTexture(GrBackendTexture backendTex) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400552 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danielf0e04f02019-12-04 15:17:54 -0500553 // For the Vulkan backend we still must destroy the backend texture when the context is
554 // abandoned.
555 if ((this->abandoned() && this->backend() != GrBackendApi::kVulkan) || !backendTex.isValid()) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400556 return;
557 }
558
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400559 fGpu->deleteBackendTexture(backendTex);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400560}
561
Brian Osmaned58e002019-09-06 14:42:43 -0400562bool GrContext::precompileShader(const SkData& key, const SkData& data) {
563 return fGpu->precompileShader(key, data);
564}
565
Brian Salomonec22b1a2019-08-09 09:41:48 -0400566#ifdef SK_ENABLE_DUMP_GPU
567#include "src/utils/SkJSONWriter.h"
568SkString GrContext::dump() const {
569 SkDynamicMemoryWStream stream;
570 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
571 writer.beginObject();
572
573 writer.appendString("backend", GrBackendApiToStr(this->backend()));
574
575 writer.appendName("caps");
576 this->caps()->dumpJSON(&writer);
577
578 writer.appendName("gpu");
579 this->fGpu->dumpJSON(&writer);
580
581 // Flush JSON to the memory stream
582 writer.endObject();
583 writer.flush();
584
585 // Null terminate the JSON data in the memory stream
586 stream.write8(0);
587
588 // Allocate a string big enough to hold all the data, then copy out of the stream
589 SkString result(stream.bytesWritten());
590 stream.copyToAndReset(result.writable_str());
591 return result;
592}
593#endif