blob: f729d09331dd4720cf2f25556f51290be8a8191a [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrDrawingManager.h"
18#include "src/gpu/GrGpu.h"
19#include "src/gpu/GrMemoryPool.h"
20#include "src/gpu/GrPathRendererChain.h"
21#include "src/gpu/GrProxyProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040022#include "src/gpu/GrRenderTargetProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrResourceCache.h"
24#include "src/gpu/GrResourceProvider.h"
25#include "src/gpu/GrSemaphore.h"
Brian Osman5e7fbfd2019-05-03 13:13:35 -040026#include "src/gpu/GrShaderUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrSoftwarePathRenderer.h"
28#include "src/gpu/GrTracing.h"
29#include "src/gpu/SkGr.h"
30#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
31#include "src/gpu/effects/GrSkSLFP.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/gpu/text/GrTextBlobCache.h"
33#include "src/gpu/text/GrTextContext.h"
34#include "src/image/SkSurface_Gpu.h"
Mike Klein0ec1c572018-12-04 11:52:51 -050035#include <atomic>
Greg Danielb76a72a2017-07-13 15:07:54 -040036
Robert Phillipse78b7252017-04-06 07:59:41 -040037#define ASSERT_OWNED_PROXY(P) \
Brian Salomonfd98c2c2018-07-31 17:25:29 -040038 SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == this)
Robert Phillips7ee385e2017-03-30 08:02:11 -040039
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000040#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt1de610a2016-01-06 08:26:09 -080041#define ASSERT_SINGLE_OWNER \
Robert Phillipsa41c6852019-02-07 10:44:10 -050042 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
Robert Phillipsa9162df2019-02-11 14:12:03 -050043#define RETURN_IF_ABANDONED if (this->abandoned()) { return; }
44#define RETURN_FALSE_IF_ABANDONED if (this->abandoned()) { return false; }
45#define RETURN_NULL_IF_ABANDONED if (this->abandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000046
robertphillipsea461502015-05-26 11:38:03 -070047////////////////////////////////////////////////////////////////////////////////
48
Robert Phillipsa41c6852019-02-07 10:44:10 -050049GrContext::GrContext(GrBackendApi backend, const GrContextOptions& options, int32_t contextID)
50 : INHERITED(backend, options, contextID) {
halcanary96fcdcc2015-08-27 07:41:13 -070051 fResourceCache = nullptr;
52 fResourceProvider = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000053}
54
Robert Phillips292a6b22019-02-14 14:49:02 -050055GrContext::~GrContext() {
56 ASSERT_SINGLE_OWNER
57
Robert Phillips6a6de562019-02-15 15:19:15 -050058 if (this->drawingManager()) {
59 this->drawingManager()->cleanup();
Robert Phillips292a6b22019-02-14 14:49:02 -050060 }
61 delete fResourceProvider;
62 delete fResourceCache;
Robert Phillips292a6b22019-02-14 14:49:02 -050063}
64
Robert Phillipsbb606772019-02-04 17:50:57 -050065bool GrContext::init(sk_sp<const GrCaps> caps, sk_sp<GrSkSLFPFactoryCache> FPFactoryCache) {
Greg Danielb76a72a2017-07-13 15:07:54 -040066 ASSERT_SINGLE_OWNER
Robert Phillipsfde6fa02018-03-02 08:53:14 -050067 SkASSERT(fThreadSafeProxy); // needs to have been initialized by derived classes
Robert Phillipsa41c6852019-02-07 10:44:10 -050068 SkASSERT(this->proxyProvider());
Robert Phillips88260b52018-01-19 12:56:09 -050069
Robert Phillipsbb606772019-02-04 17:50:57 -050070 if (!INHERITED::init(std::move(caps), std::move(FPFactoryCache))) {
71 return false;
72 }
73
74 SkASSERT(this->caps());
Herb Derbya00da612019-03-04 17:10:01 -050075 SkASSERT(this->getGrStrikeCache());
Robert Phillips2184fb72019-02-21 16:11:41 -050076 SkASSERT(this->getTextBlobCache());
Robert Phillipsbb606772019-02-04 17:50:57 -050077
Robert Phillips88260b52018-01-19 12:56:09 -050078 if (fGpu) {
Robert Phillipsa41c6852019-02-07 10:44:10 -050079 fResourceCache = new GrResourceCache(this->caps(), this->singleOwner(), this->contextID());
Robert Phillips12c46292019-04-23 07:36:17 -040080 fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, this->singleOwner());
Brian Salomon9241a6d2019-10-03 13:26:54 -040081 fMappedBufferManager = skstd::make_unique<GrClientMappedBufferManager>(this->contextID());
Robert Phillips88260b52018-01-19 12:56:09 -050082 }
83
Robert Phillips88260b52018-01-19 12:56:09 -050084 if (fResourceCache) {
Robert Phillipsa41c6852019-02-07 10:44:10 -050085 fResourceCache->setProxyProvider(this->proxyProvider());
Robert Phillips88260b52018-01-19 12:56:09 -050086 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050087
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000088 fDidTestPMConversions = false;
89
Robert Phillipsfde6fa02018-03-02 08:53:14 -050090 // DDL TODO: we need to think through how the task group & persistent cache
91 // get passed on to/shared between all the DDLRecorders created with this context.
Robert Phillipsc1541ae2019-02-04 12:05:37 -050092 if (this->options().fExecutor) {
93 fTaskGroup = skstd::make_unique<SkTaskGroup>(*this->options().fExecutor);
Brian Osman51279982017-08-23 10:12:00 -040094 }
95
Robert Phillipsc1541ae2019-02-04 12:05:37 -050096 fPersistentCache = this->options().fPersistentCache;
Brian Osman5e7fbfd2019-05-03 13:13:35 -040097 fShaderErrorHandler = this->options().fShaderErrorHandler;
98 if (!fShaderErrorHandler) {
99 fShaderErrorHandler = GrShaderUtils::DefaultShaderErrorHandler();
100 }
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400101
Brian Salomon91a3e522017-06-23 10:58:19 -0400102 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000103}
104
Robert Phillips4217ea72019-01-30 13:08:28 -0500105sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
106 return fThreadSafeProxy;
107}
108
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400109//////////////////////////////////////////////////////////////////////////////
110
bsalomon2354f842014-07-28 13:48:36 -0700111void GrContext::abandonContext() {
Robert Phillipsa9162df2019-02-11 14:12:03 -0500112 if (this->abandoned()) {
113 return;
114 }
joshualitt1de610a2016-01-06 08:26:09 -0800115
Robert Phillipsa9162df2019-02-11 14:12:03 -0500116 INHERITED::abandonContext();
117
Brian Salomon9241a6d2019-10-03 13:26:54 -0400118 fMappedBufferManager->abandon();
119
bsalomond309e7a2015-04-30 14:18:54 -0700120 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800121
Robert Phillipsa9162df2019-02-11 14:12:03 -0500122 // Need to cleanup the drawing manager first so all the render targets
robertphillips0dfa62c2015-11-16 06:23:31 -0800123 // will be released/forgotten before they too are abandoned.
Robert Phillips6a6de562019-02-15 15:19:15 -0500124 this->drawingManager()->cleanup();
robertphillips0dfa62c2015-11-16 06:23:31 -0800125
bsalomon@google.com205d4602011-04-25 12:43:45 +0000126 // abandon first to so destructors
127 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800128 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700129
bsalomon6e2aad42016-04-01 11:54:31 -0700130 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400131
132 fMappedBufferManager.reset();
Khushalc421ca12018-06-26 14:38:34 -0700133}
134
bsalomon6e2aad42016-04-01 11:54:31 -0700135void GrContext::releaseResourcesAndAbandonContext() {
Brian Salomon614c1a82018-12-19 15:42:06 -0500136 if (this->abandoned()) {
137 return;
138 }
Robert Phillipsa9162df2019-02-11 14:12:03 -0500139
140 INHERITED::abandonContext();
141
Brian Salomon9241a6d2019-10-03 13:26:54 -0400142 fMappedBufferManager.reset();
143
bsalomon6e2aad42016-04-01 11:54:31 -0700144 fResourceProvider->abandon();
145
Robert Phillipsa9162df2019-02-11 14:12:03 -0500146 // Need to cleanup the drawing manager first so all the render targets
bsalomon6e2aad42016-04-01 11:54:31 -0700147 // will be released/forgotten before they too are abandoned.
Robert Phillips6a6de562019-02-15 15:19:15 -0500148 this->drawingManager()->cleanup();
bsalomon6e2aad42016-04-01 11:54:31 -0700149
150 // Release all resources in the backend 3D API.
151 fResourceCache->releaseAll();
152
153 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000154}
155
Brian Salomon1f05d452019-02-08 12:33:08 -0500156void GrContext::resetGLTextureBindings() {
157 if (this->abandoned() || this->backend() != GrBackendApi::kOpenGL) {
158 return;
159 }
160 fGpu->resetTextureBindings();
161}
162
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000163void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800164 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000165 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000166}
167
168void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800169 ASSERT_SINGLE_OWNER
170
Robert Phillips2184fb72019-02-21 16:11:41 -0500171 // TODO: the glyph cache doesn't hold any GpuResources so this call should not be needed here.
172 // Some slack in the GrTextBlob's implementation requires it though. That could be fixed.
Herb Derbya00da612019-03-04 17:10:01 -0500173 this->getGrStrikeCache()->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700174
Robert Phillips6a6de562019-02-15 15:19:15 -0500175 this->drawingManager()->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700176
177 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000178}
179
Robert Phillips6eba0632018-03-28 12:25:42 -0400180void GrContext::purgeUnlockedResources(bool scratchResourcesOnly) {
181 ASSERT_SINGLE_OWNER
182 fResourceCache->purgeUnlockedResources(scratchResourcesOnly);
183 fResourceCache->purgeAsNeeded();
Robert Phillips2184fb72019-02-21 16:11:41 -0500184
185 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
186 // place to purge stale blobs
187 this->getTextBlobCache()->purgeStaleBlobs();
Robert Phillips6eba0632018-03-28 12:25:42 -0400188}
189
Jim Van Verth76d917c2017-12-13 09:26:37 -0500190void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Yuqian Li40aa85f2019-07-02 13:45:00 -0700191 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
192
Brian Salomon5e150852017-03-22 14:53:13 -0400193 ASSERT_SINGLE_OWNER
Chris Dalton6c3879d2018-11-01 11:13:19 -0600194
Brian Salomon9241a6d2019-10-03 13:26:54 -0400195 if (this->abandoned()) {
196 return;
197 }
198
199 fMappedBufferManager->process();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600200 auto purgeTime = GrStdSteadyClock::now() - msNotUsed;
201
Jim Van Verth76d917c2017-12-13 09:26:37 -0500202 fResourceCache->purgeAsNeeded();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600203 fResourceCache->purgeResourcesNotUsedSince(purgeTime);
204
Robert Phillips6a6de562019-02-15 15:19:15 -0500205 if (auto ccpr = this->drawingManager()->getCoverageCountingPathRenderer()) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500206 ccpr->purgeCacheEntriesOlderThan(this->proxyProvider(), purgeTime);
Chris Dalton6c3879d2018-11-01 11:13:19 -0600207 }
Jim Van Verth76d917c2017-12-13 09:26:37 -0500208
Robert Phillips2184fb72019-02-21 16:11:41 -0500209 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
210 // place to purge stale blobs
211 this->getTextBlobCache()->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400212}
213
Derek Sollenberger5480a182017-05-25 16:43:59 -0400214void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
215 ASSERT_SINGLE_OWNER
216 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
217}
218
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000219void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800220 ASSERT_SINGLE_OWNER
221
bsalomon71cb0c22014-11-14 12:10:14 -0800222 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800223 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800224 }
225 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800226 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800227 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000228}
229
Derek Sollenbergeree479142017-05-24 11:41:33 -0400230size_t GrContext::getResourceCachePurgeableBytes() const {
231 ASSERT_SINGLE_OWNER
232 return fResourceCache->getPurgeableBytes();
233}
234
Adrienne Walker0f827972019-03-26 13:46:14 -0700235size_t GrContext::ComputeTextureSize(SkColorType type, int width, int height, GrMipMapped mipMapped,
236 bool useNextPow2) {
237 int colorSamplesPerPixel = 1;
238 return GrSurface::ComputeSize(SkColorType2GrPixelConfig(type), width, height,
239 colorSamplesPerPixel, mipMapped, useNextPow2);
240}
241
Greg Daniel8b666172019-10-09 12:38:22 -0400242size_t GrContext::ComputeImageSize(sk_sp<SkImage> image, GrMipMapped mipMapped, bool useNextPow2) {
243 if (!image->isTextureBacked()) {
244 return 0;
245 }
246 int colorSamplesPerPixel = 1;
247 return GrSurface::ComputeSize(SkColorType2GrPixelConfig(image->colorType()), image->width(),
248 image->height(), colorSamplesPerPixel, mipMapped, useNextPow2);
249}
250
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000251////////////////////////////////////////////////////////////////////////////////
252
Robert Phillipsbb606772019-02-04 17:50:57 -0500253int GrContext::maxTextureSize() const { return this->caps()->maxTextureSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400254
Robert Phillipsbb606772019-02-04 17:50:57 -0500255int GrContext::maxRenderTargetSize() const { return this->caps()->maxRenderTargetSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400256
Brian Salomonbdecacf2018-02-02 20:32:49 -0500257bool GrContext::colorTypeSupportedAsImage(SkColorType colorType) const {
Greg Daniel7bfc9132019-08-14 14:23:53 -0400258 GrBackendFormat format =
259 this->caps()->getDefaultBackendFormat(SkColorTypeToGrColorType(colorType),
260 GrRenderable::kNo);
261 return format.isValid();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500262}
263
264int GrContext::maxSurfaceSampleCountForColorType(SkColorType colorType) const {
Greg Danieleadfac92019-08-02 09:03:53 -0400265 GrBackendFormat format =
266 this->caps()->getDefaultBackendFormat(SkColorTypeToGrColorType(colorType),
267 GrRenderable::kYes);
268 return this->caps()->maxRenderTargetSampleCount(format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500269}
270
271////////////////////////////////////////////////////////////////////////////////
272
Greg Daniel06be0792019-04-22 15:53:23 -0400273bool GrContext::wait(int numSemaphores, const GrBackendSemaphore waitSemaphores[]) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400274 if (!fGpu || fGpu->caps()->semaphoreSupport()) {
Greg Daniel06be0792019-04-22 15:53:23 -0400275 return false;
276 }
277 for (int i = 0; i < numSemaphores; ++i) {
278 sk_sp<GrSemaphore> sema = fResourceProvider->wrapBackendSemaphore(
279 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
280 kAdopt_GrWrapOwnership);
281 fGpu->waitSemaphore(std::move(sema));
282 }
283 return true;
284}
285
286////////////////////////////////////////////////////////////////////////////////
287
Greg Daniel797efca2019-05-09 14:04:20 -0400288GrSemaphoresSubmitted GrContext::flush(const GrFlushInfo& info,
289 const GrPrepareForExternalIORequests& externalRequests) {
Greg Daniel51316782017-08-02 15:10:09 +0000290 ASSERT_SINGLE_OWNER
Robert Phillipsa9162df2019-02-11 14:12:03 -0500291 if (this->abandoned()) {
292 return GrSemaphoresSubmitted::kNo;
293 }
Greg Daniel51316782017-08-02 15:10:09 +0000294
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400295 return this->drawingManager()->flush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess,
Greg Daniel797efca2019-05-09 14:04:20 -0400296 info, externalRequests);
Greg Daniel51316782017-08-02 15:10:09 +0000297}
298
Greg Daniela870b462019-01-08 15:49:46 -0500299////////////////////////////////////////////////////////////////////////////////
300
Brian Salomonb0d8b762019-05-06 16:58:22 -0400301void GrContext::checkAsyncWorkCompletion() {
302 if (fGpu) {
303 fGpu->checkFinishProcs();
304 }
305}
306
307////////////////////////////////////////////////////////////////////////////////
308
Greg Daniela870b462019-01-08 15:49:46 -0500309void GrContext::storeVkPipelineCacheData() {
310 if (fGpu) {
311 fGpu->storeVkPipelineCacheData();
312 }
313}
314
315////////////////////////////////////////////////////////////////////////////////
316
Khushal3e7548c2018-05-23 15:45:01 -0700317bool GrContext::supportsDistanceFieldText() const {
Robert Phillipsbb606772019-02-04 17:50:57 -0500318 return this->caps()->shaderCaps()->supportsDistanceFieldText();
Khushal3e7548c2018-05-23 15:45:01 -0700319}
320
bsalomon37f9a262015-02-02 13:00:10 -0800321//////////////////////////////////////////////////////////////////////////////
322
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500323void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800324 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500325 if (maxResources) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400326 *maxResources = -1;
bsalomon37f9a262015-02-02 13:00:10 -0800327 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500328 if (maxResourceBytes) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400329 *maxResourceBytes = this->getResourceCacheLimit();
bsalomon37f9a262015-02-02 13:00:10 -0800330 }
331}
332
Robert Phillipscf39f372019-09-03 10:29:20 -0400333size_t GrContext::getResourceCacheLimit() const {
joshualitt1de610a2016-01-06 08:26:09 -0800334 ASSERT_SINGLE_OWNER
Robert Phillipscf39f372019-09-03 10:29:20 -0400335 return fResourceCache->getMaxResourceBytes();
336}
337
338void GrContext::setResourceCacheLimits(int unused, size_t maxResourceBytes) {
339 ASSERT_SINGLE_OWNER
340 this->setResourceCacheLimit(maxResourceBytes);
341}
342
343void GrContext::setResourceCacheLimit(size_t maxResourceBytes) {
344 ASSERT_SINGLE_OWNER
345 fResourceCache->setLimit(maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800346}
347
ericrk0a5fa482015-09-15 14:16:10 -0700348//////////////////////////////////////////////////////////////////////////////
ericrk0a5fa482015-09-15 14:16:10 -0700349void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800350 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700351 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
Khushal71652e22018-10-29 13:05:36 -0700352 traceMemoryDump->dumpNumericValue("skia/gr_text_blob_cache", "size", "bytes",
Robert Phillips2184fb72019-02-21 16:11:41 -0500353 this->getTextBlobCache()->usedBytes());
ericrk0a5fa482015-09-15 14:16:10 -0700354}
Brian Osman71a18892017-08-10 10:23:25 -0400355
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400356//////////////////////////////////////////////////////////////////////////////
357GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400358 const GrBackendFormat& backendFormat,
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400359 GrMipMapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400360 GrRenderable renderable,
361 GrProtected isProtected) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400362 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400363 if (!this->asDirectContext()) {
364 return GrBackendTexture();
365 }
366
367 if (this->abandoned()) {
368 return GrBackendTexture();
369 }
370
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400371 return fGpu->createBackendTexture(width, height, backendFormat,
372 mipMapped, renderable,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400373 nullptr, 0, nullptr, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400374}
375
376GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400377 SkColorType skColorType,
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400378 GrMipMapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400379 GrRenderable renderable,
380 GrProtected isProtected) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400381 if (!this->asDirectContext()) {
382 return GrBackendTexture();
383 }
384
385 if (this->abandoned()) {
386 return GrBackendTexture();
387 }
388
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400389 const GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400390
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400391 return this->createBackendTexture(width, height, format, mipMapped, renderable, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400392}
393
Robert Phillips02dc0302019-07-02 17:58:27 -0400394GrBackendTexture GrContext::createBackendTexture(const SkSurfaceCharacterization& c) {
Robert Phillips02dc0302019-07-02 17:58:27 -0400395 if (!this->asDirectContext() || !c.isValid()) {
396 return GrBackendTexture();
397 }
398
399 if (this->abandoned()) {
400 return GrBackendTexture();
401 }
402
403 if (c.usesGLFBO0()) {
404 // If we are making the surface we will never use FBO0.
405 return GrBackendTexture();
406 }
407
408 if (c.vulkanSecondaryCBCompatible()) {
409 return {};
410 }
411
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400412 const GrBackendFormat format = this->defaultBackendFormat(c.colorType(), GrRenderable::kYes);
Robert Phillips02dc0302019-07-02 17:58:27 -0400413 if (!format.isValid()) {
414 return GrBackendTexture();
415 }
416
Robert Phillips02dc0302019-07-02 17:58:27 -0400417 GrBackendTexture result = this->createBackendTexture(c.width(), c.height(), format,
418 GrMipMapped(c.isMipMapped()),
419 GrRenderable::kYes,
Robert Phillips3cd54322019-07-10 09:28:59 -0400420 c.isProtected());
Robert Phillips02dc0302019-07-02 17:58:27 -0400421 SkASSERT(c.isCompatible(result));
422 return result;
423}
424
425GrBackendTexture GrContext::createBackendTexture(const SkSurfaceCharacterization& c,
426 const SkColor4f& color) {
Robert Phillips02dc0302019-07-02 17:58:27 -0400427 if (!this->asDirectContext() || !c.isValid()) {
428 return GrBackendTexture();
429 }
430
431 if (this->abandoned()) {
432 return GrBackendTexture();
433 }
434
435 if (c.usesGLFBO0()) {
436 // If we are making the surface we will never use FBO0.
437 return GrBackendTexture();
438 }
439
440 if (c.vulkanSecondaryCBCompatible()) {
441 return {};
442 }
443
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400444 const GrBackendFormat format = this->defaultBackendFormat(c.colorType(), GrRenderable::kYes);
Robert Phillips02dc0302019-07-02 17:58:27 -0400445 if (!format.isValid()) {
446 return GrBackendTexture();
447 }
448
Robert Phillips02dc0302019-07-02 17:58:27 -0400449 GrBackendTexture result = this->createBackendTexture(c.width(), c.height(), format, color,
450 GrMipMapped(c.isMipMapped()),
451 GrRenderable::kYes,
Robert Phillips3cd54322019-07-10 09:28:59 -0400452 c.isProtected());
Robert Phillips02dc0302019-07-02 17:58:27 -0400453 SkASSERT(c.isCompatible(result));
454 return result;
455}
456
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400457GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400458 const GrBackendFormat& backendFormat,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400459 const SkColor4f& color,
460 GrMipMapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400461 GrRenderable renderable,
462 GrProtected isProtected) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400463 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400464 if (!this->asDirectContext()) {
465 return GrBackendTexture();
466 }
467
468 if (this->abandoned()) {
469 return GrBackendTexture();
470 }
471
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400472 return fGpu->createBackendTexture(width, height, backendFormat,
473 mipMapped, renderable,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400474 nullptr, 0, &color, isProtected);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400475}
476
477GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400478 SkColorType skColorType,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400479 const SkColor4f& color,
480 GrMipMapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400481 GrRenderable renderable,
482 GrProtected isProtected) {
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400483 if (!this->asDirectContext()) {
484 return GrBackendTexture();
485 }
486
487 if (this->abandoned()) {
488 return GrBackendTexture();
489 }
490
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400491 GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400492 if (!format.isValid()) {
493 return GrBackendTexture();
494 }
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400495
496 GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
497 SkColor4f swizzledColor = this->caps()->getOutputSwizzle(format, grColorType).applyTo(color);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400498
Brian Salomonb450f3b2019-07-09 09:36:51 -0400499 return this->createBackendTexture(width, height, format, swizzledColor, mipMapped, renderable,
500 isProtected);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400501}
502
Robert Phillips66944402019-09-30 13:21:25 -0400503GrBackendTexture GrContext::createBackendTexture(const SkPixmap srcData[], int numLevels,
504 GrRenderable renderable, GrProtected isProtected) {
505 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
506
507 if (!this->asDirectContext()) {
508 return {};
509 }
510
511 if (this->abandoned()) {
512 return {};
513 }
514
515 if (!srcData || !numLevels) {
516 return {};
517 }
518
519 int baseWidth = srcData[0].width();
520 int baseHeight = srcData[0].height();
521 SkColorType colorType = srcData[0].colorType();
522
523 GrBackendFormat backendFormat = this->defaultBackendFormat(colorType, renderable);
524
525 return fGpu->createBackendTexture(baseWidth, baseHeight, backendFormat,
526 numLevels > 1 ? GrMipMapped::kYes : GrMipMapped::kNo,
527 renderable, srcData, numLevels, nullptr, isProtected);
528}
529
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400530void GrContext::deleteBackendTexture(GrBackendTexture backendTex) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400531 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400532 if (this->abandoned() || !backendTex.isValid()) {
533 return;
534 }
535
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400536 fGpu->deleteBackendTexture(backendTex);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400537}
538
Brian Osmaned58e002019-09-06 14:42:43 -0400539bool GrContext::precompileShader(const SkData& key, const SkData& data) {
540 return fGpu->precompileShader(key, data);
541}
542
Brian Salomonec22b1a2019-08-09 09:41:48 -0400543#ifdef SK_ENABLE_DUMP_GPU
544#include "src/utils/SkJSONWriter.h"
545SkString GrContext::dump() const {
546 SkDynamicMemoryWStream stream;
547 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
548 writer.beginObject();
549
550 writer.appendString("backend", GrBackendApiToStr(this->backend()));
551
552 writer.appendName("caps");
553 this->caps()->dumpJSON(&writer);
554
555 writer.appendName("gpu");
556 this->fGpu->dumpJSON(&writer);
557
558 // Flush JSON to the memory stream
559 writer.endObject();
560 writer.flush();
561
562 // Null terminate the JSON data in the memory stream
563 stream.write8(0);
564
565 // Allocate a string big enough to hold all the data, then copy out of the stream
566 SkString result(stream.bytesWritten());
567 stream.copyToAndReset(result.writable_str());
568 return result;
569}
570#endif