blob: 0ab948cc864db90ae19d72b222942c9ca36b0603 [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"
Brian Salomon85c3d682019-11-04 15:04:54 -050015#include "src/core/SkMipMap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/core/SkTaskGroup.h"
Brian Salomon9241a6d2019-10-03 13:26:54 -040017#include "src/gpu/GrClientMappedBufferManager.h"
Greg Daniel7fd7a8a2019-10-10 16:10:31 -040018#include "src/gpu/GrContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrDrawingManager.h"
20#include "src/gpu/GrGpu.h"
21#include "src/gpu/GrMemoryPool.h"
22#include "src/gpu/GrPathRendererChain.h"
23#include "src/gpu/GrProxyProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040024#include "src/gpu/GrRenderTargetProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrResourceCache.h"
26#include "src/gpu/GrResourceProvider.h"
27#include "src/gpu/GrSemaphore.h"
Brian Osman5e7fbfd2019-05-03 13:13:35 -040028#include "src/gpu/GrShaderUtils.h"
Robert Phillips7f11fb52019-12-03 13:35:19 -050029#include "src/gpu/GrSkSLFPFactoryCache.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/GrSoftwarePathRenderer.h"
31#include "src/gpu/GrTracing.h"
32#include "src/gpu/SkGr.h"
33#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
34#include "src/gpu/effects/GrSkSLFP.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/gpu/text/GrTextBlobCache.h"
36#include "src/gpu/text/GrTextContext.h"
Greg Daniel7fd7a8a2019-10-10 16:10:31 -040037#include "src/image/SkImage_GpuBase.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "src/image/SkSurface_Gpu.h"
Mike Klein0ec1c572018-12-04 11:52:51 -050039#include <atomic>
Greg Danielb76a72a2017-07-13 15:07:54 -040040
Robert Phillipse78b7252017-04-06 07:59:41 -040041#define ASSERT_OWNED_PROXY(P) \
Brian Salomonfd98c2c2018-07-31 17:25:29 -040042 SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == this)
Robert Phillips7ee385e2017-03-30 08:02:11 -040043
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000044#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt1de610a2016-01-06 08:26:09 -080045#define ASSERT_SINGLE_OWNER \
Robert Phillipsa41c6852019-02-07 10:44:10 -050046 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
Robert Phillipsa9162df2019-02-11 14:12:03 -050047#define RETURN_IF_ABANDONED if (this->abandoned()) { return; }
48#define RETURN_FALSE_IF_ABANDONED if (this->abandoned()) { return false; }
49#define RETURN_NULL_IF_ABANDONED if (this->abandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000050
robertphillipsea461502015-05-26 11:38:03 -070051////////////////////////////////////////////////////////////////////////////////
52
Robert Phillipsa41c6852019-02-07 10:44:10 -050053GrContext::GrContext(GrBackendApi backend, const GrContextOptions& options, int32_t contextID)
54 : INHERITED(backend, options, contextID) {
halcanary96fcdcc2015-08-27 07:41:13 -070055 fResourceCache = nullptr;
56 fResourceProvider = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000057}
58
Robert Phillips292a6b22019-02-14 14:49:02 -050059GrContext::~GrContext() {
60 ASSERT_SINGLE_OWNER
61
Robert Phillips6a6de562019-02-15 15:19:15 -050062 if (this->drawingManager()) {
63 this->drawingManager()->cleanup();
Robert Phillips292a6b22019-02-14 14:49:02 -050064 }
65 delete fResourceProvider;
66 delete fResourceCache;
Robert Phillips292a6b22019-02-14 14:49:02 -050067}
68
Robert Phillipsbb606772019-02-04 17:50:57 -050069bool GrContext::init(sk_sp<const GrCaps> caps, sk_sp<GrSkSLFPFactoryCache> FPFactoryCache) {
Greg Danielb76a72a2017-07-13 15:07:54 -040070 ASSERT_SINGLE_OWNER
Robert Phillipsfde6fa02018-03-02 08:53:14 -050071 SkASSERT(fThreadSafeProxy); // needs to have been initialized by derived classes
Robert Phillipsa41c6852019-02-07 10:44:10 -050072 SkASSERT(this->proxyProvider());
Robert Phillips88260b52018-01-19 12:56:09 -050073
Robert Phillipsbb606772019-02-04 17:50:57 -050074 if (!INHERITED::init(std::move(caps), std::move(FPFactoryCache))) {
75 return false;
76 }
77
78 SkASSERT(this->caps());
Herb Derbya00da612019-03-04 17:10:01 -050079 SkASSERT(this->getGrStrikeCache());
Robert Phillips2184fb72019-02-21 16:11:41 -050080 SkASSERT(this->getTextBlobCache());
Robert Phillipsbb606772019-02-04 17:50:57 -050081
Robert Phillips88260b52018-01-19 12:56:09 -050082 if (fGpu) {
Robert Phillipsa41c6852019-02-07 10:44:10 -050083 fResourceCache = new GrResourceCache(this->caps(), this->singleOwner(), this->contextID());
Robert Phillips12c46292019-04-23 07:36:17 -040084 fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, this->singleOwner());
Brian Salomon9241a6d2019-10-03 13:26:54 -040085 fMappedBufferManager = skstd::make_unique<GrClientMappedBufferManager>(this->contextID());
Robert Phillips88260b52018-01-19 12:56:09 -050086 }
87
Robert Phillips88260b52018-01-19 12:56:09 -050088 if (fResourceCache) {
Robert Phillipsa41c6852019-02-07 10:44:10 -050089 fResourceCache->setProxyProvider(this->proxyProvider());
Robert Phillips88260b52018-01-19 12:56:09 -050090 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050091
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000092 fDidTestPMConversions = false;
93
Robert Phillipsfde6fa02018-03-02 08:53:14 -050094 // DDL TODO: we need to think through how the task group & persistent cache
95 // get passed on to/shared between all the DDLRecorders created with this context.
Robert Phillipsc1541ae2019-02-04 12:05:37 -050096 if (this->options().fExecutor) {
97 fTaskGroup = skstd::make_unique<SkTaskGroup>(*this->options().fExecutor);
Brian Osman51279982017-08-23 10:12:00 -040098 }
99
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500100 fPersistentCache = this->options().fPersistentCache;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400101 fShaderErrorHandler = this->options().fShaderErrorHandler;
102 if (!fShaderErrorHandler) {
103 fShaderErrorHandler = GrShaderUtils::DefaultShaderErrorHandler();
104 }
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400105
Brian Salomon91a3e522017-06-23 10:58:19 -0400106 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000107}
108
Robert Phillips4217ea72019-01-30 13:08:28 -0500109sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
110 return fThreadSafeProxy;
111}
112
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400113//////////////////////////////////////////////////////////////////////////////
114
bsalomon2354f842014-07-28 13:48:36 -0700115void GrContext::abandonContext() {
Robert Phillipsa9162df2019-02-11 14:12:03 -0500116 if (this->abandoned()) {
117 return;
118 }
joshualitt1de610a2016-01-06 08:26:09 -0800119
Robert Phillipsa9162df2019-02-11 14:12:03 -0500120 INHERITED::abandonContext();
121
Brian Salomon9241a6d2019-10-03 13:26:54 -0400122 fMappedBufferManager->abandon();
123
bsalomond309e7a2015-04-30 14:18:54 -0700124 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800125
Robert Phillipsa9162df2019-02-11 14:12:03 -0500126 // Need to cleanup the drawing manager first so all the render targets
robertphillips0dfa62c2015-11-16 06:23:31 -0800127 // will be released/forgotten before they too are abandoned.
Robert Phillips6a6de562019-02-15 15:19:15 -0500128 this->drawingManager()->cleanup();
robertphillips0dfa62c2015-11-16 06:23:31 -0800129
bsalomon@google.com205d4602011-04-25 12:43:45 +0000130 // abandon first to so destructors
131 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800132 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700133
bsalomon6e2aad42016-04-01 11:54:31 -0700134 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400135
136 fMappedBufferManager.reset();
Khushalc421ca12018-06-26 14:38:34 -0700137}
138
bsalomon6e2aad42016-04-01 11:54:31 -0700139void GrContext::releaseResourcesAndAbandonContext() {
Brian Salomon614c1a82018-12-19 15:42:06 -0500140 if (this->abandoned()) {
141 return;
142 }
Robert Phillipsa9162df2019-02-11 14:12:03 -0500143
144 INHERITED::abandonContext();
145
Brian Salomon9241a6d2019-10-03 13:26:54 -0400146 fMappedBufferManager.reset();
147
bsalomon6e2aad42016-04-01 11:54:31 -0700148 fResourceProvider->abandon();
149
Robert Phillipsa9162df2019-02-11 14:12:03 -0500150 // Need to cleanup the drawing manager first so all the render targets
bsalomon6e2aad42016-04-01 11:54:31 -0700151 // will be released/forgotten before they too are abandoned.
Robert Phillips6a6de562019-02-15 15:19:15 -0500152 this->drawingManager()->cleanup();
bsalomon6e2aad42016-04-01 11:54:31 -0700153
154 // Release all resources in the backend 3D API.
155 fResourceCache->releaseAll();
156
157 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000158}
159
Brian Salomon1f05d452019-02-08 12:33:08 -0500160void GrContext::resetGLTextureBindings() {
161 if (this->abandoned() || this->backend() != GrBackendApi::kOpenGL) {
162 return;
163 }
164 fGpu->resetTextureBindings();
165}
166
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000167void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800168 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000169 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000170}
171
172void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800173 ASSERT_SINGLE_OWNER
174
Robert Phillips2184fb72019-02-21 16:11:41 -0500175 // TODO: the glyph cache doesn't hold any GpuResources so this call should not be needed here.
176 // Some slack in the GrTextBlob's implementation requires it though. That could be fixed.
Herb Derbya00da612019-03-04 17:10:01 -0500177 this->getGrStrikeCache()->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700178
Robert Phillips6a6de562019-02-15 15:19:15 -0500179 this->drawingManager()->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700180
181 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000182}
183
Robert Phillips6eba0632018-03-28 12:25:42 -0400184void GrContext::purgeUnlockedResources(bool scratchResourcesOnly) {
185 ASSERT_SINGLE_OWNER
Robert Phillipsddc21482019-10-16 14:30:09 -0400186
187 if (this->abandoned()) {
188 return;
189 }
190
Robert Phillips6eba0632018-03-28 12:25:42 -0400191 fResourceCache->purgeUnlockedResources(scratchResourcesOnly);
192 fResourceCache->purgeAsNeeded();
Robert Phillips2184fb72019-02-21 16:11:41 -0500193
194 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
195 // place to purge stale blobs
196 this->getTextBlobCache()->purgeStaleBlobs();
Robert Phillips6eba0632018-03-28 12:25:42 -0400197}
198
Jim Van Verth76d917c2017-12-13 09:26:37 -0500199void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Yuqian Li40aa85f2019-07-02 13:45:00 -0700200 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
201
Brian Salomon5e150852017-03-22 14:53:13 -0400202 ASSERT_SINGLE_OWNER
Chris Dalton6c3879d2018-11-01 11:13:19 -0600203
Brian Salomon9241a6d2019-10-03 13:26:54 -0400204 if (this->abandoned()) {
205 return;
206 }
207
208 fMappedBufferManager->process();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600209 auto purgeTime = GrStdSteadyClock::now() - msNotUsed;
210
Jim Van Verth76d917c2017-12-13 09:26:37 -0500211 fResourceCache->purgeAsNeeded();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600212 fResourceCache->purgeResourcesNotUsedSince(purgeTime);
213
Robert Phillips6a6de562019-02-15 15:19:15 -0500214 if (auto ccpr = this->drawingManager()->getCoverageCountingPathRenderer()) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500215 ccpr->purgeCacheEntriesOlderThan(this->proxyProvider(), purgeTime);
Chris Dalton6c3879d2018-11-01 11:13:19 -0600216 }
Jim Van Verth76d917c2017-12-13 09:26:37 -0500217
Robert Phillips2184fb72019-02-21 16:11:41 -0500218 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
219 // place to purge stale blobs
220 this->getTextBlobCache()->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400221}
222
Derek Sollenberger5480a182017-05-25 16:43:59 -0400223void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
224 ASSERT_SINGLE_OWNER
Robert Phillipsddc21482019-10-16 14:30:09 -0400225
226 if (this->abandoned()) {
227 return;
228 }
229
Derek Sollenberger5480a182017-05-25 16:43:59 -0400230 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
231}
232
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000233void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800234 ASSERT_SINGLE_OWNER
235
bsalomon71cb0c22014-11-14 12:10:14 -0800236 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800237 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800238 }
239 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800240 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800241 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000242}
243
Derek Sollenbergeree479142017-05-24 11:41:33 -0400244size_t GrContext::getResourceCachePurgeableBytes() const {
245 ASSERT_SINGLE_OWNER
246 return fResourceCache->getPurgeableBytes();
247}
248
Greg Daniel8b666172019-10-09 12:38:22 -0400249size_t GrContext::ComputeImageSize(sk_sp<SkImage> image, GrMipMapped mipMapped, bool useNextPow2) {
250 if (!image->isTextureBacked()) {
251 return 0;
252 }
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400253 SkImage_GpuBase* gpuImage = static_cast<SkImage_GpuBase*>(as_IB(image.get()));
254 GrTextureProxy* proxy = gpuImage->peekProxy();
255 if (!proxy) {
256 return 0;
257 }
258
259 const GrCaps& caps = *gpuImage->context()->priv().caps();
Greg Daniel8b666172019-10-09 12:38:22 -0400260 int colorSamplesPerPixel = 1;
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400261 return GrSurface::ComputeSize(caps, proxy->backendFormat(), image->dimensions(),
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400262 colorSamplesPerPixel, mipMapped, useNextPow2);
Greg Daniel8b666172019-10-09 12:38:22 -0400263}
264
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000265////////////////////////////////////////////////////////////////////////////////
266
Robert Phillipsbb606772019-02-04 17:50:57 -0500267int GrContext::maxTextureSize() const { return this->caps()->maxTextureSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400268
Robert Phillipsbb606772019-02-04 17:50:57 -0500269int GrContext::maxRenderTargetSize() const { return this->caps()->maxRenderTargetSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400270
Brian Salomonbdecacf2018-02-02 20:32:49 -0500271bool GrContext::colorTypeSupportedAsImage(SkColorType colorType) const {
Greg Daniel7bfc9132019-08-14 14:23:53 -0400272 GrBackendFormat format =
273 this->caps()->getDefaultBackendFormat(SkColorTypeToGrColorType(colorType),
274 GrRenderable::kNo);
275 return format.isValid();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500276}
277
278int GrContext::maxSurfaceSampleCountForColorType(SkColorType colorType) const {
Greg Danieleadfac92019-08-02 09:03:53 -0400279 GrBackendFormat format =
280 this->caps()->getDefaultBackendFormat(SkColorTypeToGrColorType(colorType),
281 GrRenderable::kYes);
282 return this->caps()->maxRenderTargetSampleCount(format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500283}
284
285////////////////////////////////////////////////////////////////////////////////
286
Greg Daniel06be0792019-04-22 15:53:23 -0400287bool GrContext::wait(int numSemaphores, const GrBackendSemaphore waitSemaphores[]) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400288 if (!fGpu || fGpu->caps()->semaphoreSupport()) {
Greg Daniel06be0792019-04-22 15:53:23 -0400289 return false;
290 }
291 for (int i = 0; i < numSemaphores; ++i) {
Greg Daniel301015c2019-11-18 14:06:46 -0500292 std::unique_ptr<GrSemaphore> sema = fResourceProvider->wrapBackendSemaphore(
Greg Daniel06be0792019-04-22 15:53:23 -0400293 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
294 kAdopt_GrWrapOwnership);
Greg Daniel301015c2019-11-18 14:06:46 -0500295 fGpu->waitSemaphore(sema.get());
Greg Daniel06be0792019-04-22 15:53:23 -0400296 }
297 return true;
298}
299
300////////////////////////////////////////////////////////////////////////////////
301
Greg Daniel797efca2019-05-09 14:04:20 -0400302GrSemaphoresSubmitted GrContext::flush(const GrFlushInfo& info,
303 const GrPrepareForExternalIORequests& externalRequests) {
Greg Daniel51316782017-08-02 15:10:09 +0000304 ASSERT_SINGLE_OWNER
Robert Phillipsa9162df2019-02-11 14:12:03 -0500305 if (this->abandoned()) {
306 return GrSemaphoresSubmitted::kNo;
307 }
Greg Daniel51316782017-08-02 15:10:09 +0000308
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400309 return this->drawingManager()->flush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess,
Greg Daniel797efca2019-05-09 14:04:20 -0400310 info, externalRequests);
Greg Daniel51316782017-08-02 15:10:09 +0000311}
312
Greg Daniela870b462019-01-08 15:49:46 -0500313////////////////////////////////////////////////////////////////////////////////
314
Brian Salomonb0d8b762019-05-06 16:58:22 -0400315void GrContext::checkAsyncWorkCompletion() {
316 if (fGpu) {
317 fGpu->checkFinishProcs();
318 }
319}
320
321////////////////////////////////////////////////////////////////////////////////
322
Greg Daniela870b462019-01-08 15:49:46 -0500323void GrContext::storeVkPipelineCacheData() {
324 if (fGpu) {
325 fGpu->storeVkPipelineCacheData();
326 }
327}
328
329////////////////////////////////////////////////////////////////////////////////
330
Khushal3e7548c2018-05-23 15:45:01 -0700331bool GrContext::supportsDistanceFieldText() const {
Robert Phillipsbb606772019-02-04 17:50:57 -0500332 return this->caps()->shaderCaps()->supportsDistanceFieldText();
Khushal3e7548c2018-05-23 15:45:01 -0700333}
334
bsalomon37f9a262015-02-02 13:00:10 -0800335//////////////////////////////////////////////////////////////////////////////
336
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500337void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800338 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500339 if (maxResources) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400340 *maxResources = -1;
bsalomon37f9a262015-02-02 13:00:10 -0800341 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500342 if (maxResourceBytes) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400343 *maxResourceBytes = this->getResourceCacheLimit();
bsalomon37f9a262015-02-02 13:00:10 -0800344 }
345}
346
Robert Phillipscf39f372019-09-03 10:29:20 -0400347size_t GrContext::getResourceCacheLimit() const {
joshualitt1de610a2016-01-06 08:26:09 -0800348 ASSERT_SINGLE_OWNER
Robert Phillipscf39f372019-09-03 10:29:20 -0400349 return fResourceCache->getMaxResourceBytes();
350}
351
352void GrContext::setResourceCacheLimits(int unused, size_t maxResourceBytes) {
353 ASSERT_SINGLE_OWNER
354 this->setResourceCacheLimit(maxResourceBytes);
355}
356
357void GrContext::setResourceCacheLimit(size_t maxResourceBytes) {
358 ASSERT_SINGLE_OWNER
359 fResourceCache->setLimit(maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800360}
361
ericrk0a5fa482015-09-15 14:16:10 -0700362//////////////////////////////////////////////////////////////////////////////
ericrk0a5fa482015-09-15 14:16:10 -0700363void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800364 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700365 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
Khushal71652e22018-10-29 13:05:36 -0700366 traceMemoryDump->dumpNumericValue("skia/gr_text_blob_cache", "size", "bytes",
Robert Phillips2184fb72019-02-21 16:11:41 -0500367 this->getTextBlobCache()->usedBytes());
ericrk0a5fa482015-09-15 14:16:10 -0700368}
Brian Osman71a18892017-08-10 10:23:25 -0400369
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400370//////////////////////////////////////////////////////////////////////////////
371GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400372 const GrBackendFormat& backendFormat,
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400373 GrMipMapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400374 GrRenderable renderable,
375 GrProtected isProtected) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400376 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400377 if (!this->asDirectContext()) {
378 return GrBackendTexture();
379 }
380
381 if (this->abandoned()) {
382 return GrBackendTexture();
383 }
384
Brian Salomon85c3d682019-11-04 15:04:54 -0500385 int numMipLevels = 1;
386 if (mipMapped == GrMipMapped::kYes) {
387 numMipLevels = SkMipMap::ComputeLevelCount(width, height) + 1;
388 }
389
390 return fGpu->createBackendTexture({width, height}, backendFormat, renderable, nullptr,
391 numMipLevels, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400392}
393
394GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400395 SkColorType skColorType,
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400396 GrMipMapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400397 GrRenderable renderable,
398 GrProtected isProtected) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400399 if (!this->asDirectContext()) {
400 return GrBackendTexture();
401 }
402
403 if (this->abandoned()) {
404 return GrBackendTexture();
405 }
406
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400407 const GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400408
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400409 return this->createBackendTexture(width, height, format, mipMapped, renderable, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400410}
411
Robert Phillips02dc0302019-07-02 17:58:27 -0400412GrBackendTexture GrContext::createBackendTexture(const SkSurfaceCharacterization& c) {
Robert Phillips02dc0302019-07-02 17:58:27 -0400413 if (!this->asDirectContext() || !c.isValid()) {
414 return GrBackendTexture();
415 }
416
417 if (this->abandoned()) {
418 return GrBackendTexture();
419 }
420
421 if (c.usesGLFBO0()) {
422 // If we are making the surface we will never use FBO0.
423 return GrBackendTexture();
424 }
425
426 if (c.vulkanSecondaryCBCompatible()) {
427 return {};
428 }
429
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400430 const GrBackendFormat format = this->defaultBackendFormat(c.colorType(), GrRenderable::kYes);
Robert Phillips02dc0302019-07-02 17:58:27 -0400431 if (!format.isValid()) {
432 return GrBackendTexture();
433 }
434
Robert Phillips02dc0302019-07-02 17:58:27 -0400435 GrBackendTexture result = this->createBackendTexture(c.width(), c.height(), format,
436 GrMipMapped(c.isMipMapped()),
437 GrRenderable::kYes,
Robert Phillips3cd54322019-07-10 09:28:59 -0400438 c.isProtected());
Robert Phillips02dc0302019-07-02 17:58:27 -0400439 SkASSERT(c.isCompatible(result));
440 return result;
441}
442
443GrBackendTexture GrContext::createBackendTexture(const SkSurfaceCharacterization& c,
444 const SkColor4f& color) {
Robert Phillips02dc0302019-07-02 17:58:27 -0400445 if (!this->asDirectContext() || !c.isValid()) {
446 return GrBackendTexture();
447 }
448
449 if (this->abandoned()) {
450 return GrBackendTexture();
451 }
452
453 if (c.usesGLFBO0()) {
454 // If we are making the surface we will never use FBO0.
455 return GrBackendTexture();
456 }
457
458 if (c.vulkanSecondaryCBCompatible()) {
459 return {};
460 }
461
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400462 const GrBackendFormat format = this->defaultBackendFormat(c.colorType(), GrRenderable::kYes);
Robert Phillips02dc0302019-07-02 17:58:27 -0400463 if (!format.isValid()) {
464 return GrBackendTexture();
465 }
466
Robert Phillips02dc0302019-07-02 17:58:27 -0400467 GrBackendTexture result = this->createBackendTexture(c.width(), c.height(), format, color,
468 GrMipMapped(c.isMipMapped()),
469 GrRenderable::kYes,
Robert Phillips3cd54322019-07-10 09:28:59 -0400470 c.isProtected());
Robert Phillips02dc0302019-07-02 17:58:27 -0400471 SkASSERT(c.isCompatible(result));
472 return result;
473}
474
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400475GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400476 const GrBackendFormat& backendFormat,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400477 const SkColor4f& color,
478 GrMipMapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400479 GrRenderable renderable,
480 GrProtected isProtected) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400481 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400482 if (!this->asDirectContext()) {
483 return GrBackendTexture();
484 }
485
486 if (this->abandoned()) {
487 return GrBackendTexture();
488 }
489
Brian Salomon85c3d682019-11-04 15:04:54 -0500490 int numMipLevels = 1;
491 if (mipMapped == GrMipMapped::kYes) {
492 numMipLevels = SkMipMap::ComputeLevelCount(width, height) + 1;
493 }
494 GrGpu::BackendTextureData data(color);
495 return fGpu->createBackendTexture({width, height}, backendFormat, renderable, &data,
496 numMipLevels, isProtected);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400497}
498
499GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400500 SkColorType skColorType,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400501 const SkColor4f& color,
502 GrMipMapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400503 GrRenderable renderable,
504 GrProtected isProtected) {
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400505 if (!this->asDirectContext()) {
506 return GrBackendTexture();
507 }
508
509 if (this->abandoned()) {
510 return GrBackendTexture();
511 }
512
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400513 GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400514 if (!format.isValid()) {
515 return GrBackendTexture();
516 }
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400517
518 GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
519 SkColor4f swizzledColor = this->caps()->getOutputSwizzle(format, grColorType).applyTo(color);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400520
Brian Salomonb450f3b2019-07-09 09:36:51 -0400521 return this->createBackendTexture(width, height, format, swizzledColor, mipMapped, renderable,
522 isProtected);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400523}
524
Robert Phillips66944402019-09-30 13:21:25 -0400525GrBackendTexture GrContext::createBackendTexture(const SkPixmap srcData[], int numLevels,
526 GrRenderable renderable, GrProtected isProtected) {
527 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
528
529 if (!this->asDirectContext()) {
530 return {};
531 }
532
533 if (this->abandoned()) {
534 return {};
535 }
536
537 if (!srcData || !numLevels) {
538 return {};
539 }
540
541 int baseWidth = srcData[0].width();
542 int baseHeight = srcData[0].height();
543 SkColorType colorType = srcData[0].colorType();
544
545 GrBackendFormat backendFormat = this->defaultBackendFormat(colorType, renderable);
546
Brian Salomon85c3d682019-11-04 15:04:54 -0500547 GrGpu::BackendTextureData data(srcData);
548 return fGpu->createBackendTexture({baseWidth, baseHeight}, backendFormat, renderable, &data,
549 numLevels, isProtected);
Robert Phillips66944402019-09-30 13:21:25 -0400550}
551
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400552void GrContext::deleteBackendTexture(GrBackendTexture backendTex) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400553 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danielf0e04f02019-12-04 15:17:54 -0500554 // For the Vulkan backend we still must destroy the backend texture when the context is
555 // abandoned.
556 if ((this->abandoned() && this->backend() != GrBackendApi::kVulkan) || !backendTex.isValid()) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400557 return;
558 }
559
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400560 fGpu->deleteBackendTexture(backendTex);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400561}
562
Brian Osmaned58e002019-09-06 14:42:43 -0400563bool GrContext::precompileShader(const SkData& key, const SkData& data) {
564 return fGpu->precompileShader(key, data);
565}
566
Brian Salomonec22b1a2019-08-09 09:41:48 -0400567#ifdef SK_ENABLE_DUMP_GPU
568#include "src/utils/SkJSONWriter.h"
569SkString GrContext::dump() const {
570 SkDynamicMemoryWStream stream;
571 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
572 writer.beginObject();
573
574 writer.appendString("backend", GrBackendApiToStr(this->backend()));
575
576 writer.appendName("caps");
577 this->caps()->dumpJSON(&writer);
578
579 writer.appendName("gpu");
580 this->fGpu->dumpJSON(&writer);
581
582 // Flush JSON to the memory stream
583 writer.endObject();
584 writer.flush();
585
586 // Null terminate the JSON data in the memory stream
587 stream.write8(0);
588
589 // Allocate a string big enough to hold all the data, then copy out of the stream
590 SkString result(stream.bytesWritten());
591 stream.copyToAndReset(result.writable_str());
592 return result;
593}
594#endif