blob: 9fa242ea04099608dd33d5accb38293cf68805ee [file] [log] [blame]
bsalomon@google.com27847de2011-02-22 20:59:41 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
bsalomon@google.com27847de2011-02-22 20:59:41 +00006 */
7
Brian Salomon9241a6d2019-10-03 13:26:54 -04008#include "include/gpu/GrContext.h"
9
Robert Phillips4d5594d2020-02-21 14:24:40 -050010#include "include/core/SkDeferredDisplayList.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkTraceMemoryDump.h"
12#include "include/gpu/GrBackendSemaphore.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/private/SkImageInfoPriv.h"
Brian Salomon85c3d682019-11-04 15:04:54 -050014#include "src/core/SkMipMap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/core/SkTaskGroup.h"
Brian Salomon9241a6d2019-10-03 13:26:54 -040016#include "src/gpu/GrClientMappedBufferManager.h"
Greg Daniel7fd7a8a2019-10-10 16:10:31 -040017#include "src/gpu/GrContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrDrawingManager.h"
19#include "src/gpu/GrGpu.h"
20#include "src/gpu/GrMemoryPool.h"
21#include "src/gpu/GrPathRendererChain.h"
22#include "src/gpu/GrProxyProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040023#include "src/gpu/GrRenderTargetProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrResourceCache.h"
25#include "src/gpu/GrResourceProvider.h"
26#include "src/gpu/GrSemaphore.h"
Brian Osman5e7fbfd2019-05-03 13:13:35 -040027#include "src/gpu/GrShaderUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/GrSoftwarePathRenderer.h"
29#include "src/gpu/GrTracing.h"
30#include "src/gpu/SkGr.h"
31#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
32#include "src/gpu/effects/GrSkSLFP.h"
Herb Derbya08bde62020-06-12 15:46:06 -040033#include "src/gpu/text/GrSDFTOptions.h"
Robert Phillips41bd97d2020-04-07 14:19:37 -040034#include "src/gpu/text/GrStrikeCache.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/gpu/text/GrTextBlobCache.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)
Adlai Holler33dbd652020-06-01 12:35:42 -040044#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(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
Adlai Hollere219d1c2020-06-02 11:23:16 -040051GrContext::GrContext(sk_sp<GrContextThreadSafeProxy> proxy) : INHERITED(std::move(proxy)) {
halcanary96fcdcc2015-08-27 07:41:13 -070052 fResourceCache = nullptr;
53 fResourceProvider = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000054}
55
Robert Phillips292a6b22019-02-14 14:49:02 -050056GrContext::~GrContext() {
57 ASSERT_SINGLE_OWNER
58
Adlai Holler6f1487f2020-06-17 18:33:08 -040059 this->destroyDrawingManager();
Greg Daniel5ed3c112020-06-18 15:59:17 -040060 fMappedBufferManager.reset();
Robert Phillips292a6b22019-02-14 14:49:02 -050061 delete fResourceProvider;
62 delete fResourceCache;
Robert Phillips292a6b22019-02-14 14:49:02 -050063}
64
Adlai Hollere219d1c2020-06-02 11:23:16 -040065bool GrContext::init() {
Greg Danielb76a72a2017-07-13 15:07:54 -040066 ASSERT_SINGLE_OWNER
Robert Phillipsa41c6852019-02-07 10:44:10 -050067 SkASSERT(this->proxyProvider());
Robert Phillips88260b52018-01-19 12:56:09 -050068
Adlai Hollere219d1c2020-06-02 11:23:16 -040069 if (!INHERITED::init()) {
Robert Phillipsbb606772019-02-04 17:50:57 -050070 return false;
71 }
72
Robert Phillips2184fb72019-02-21 16:11:41 -050073 SkASSERT(this->getTextBlobCache());
Robert Phillipsbb606772019-02-04 17:50:57 -050074
Robert Phillips88260b52018-01-19 12:56:09 -050075 if (fGpu) {
Robert Phillips4d932d12020-04-09 08:58:52 -040076 fStrikeCache.reset(new GrStrikeCache{});
Robert Phillipsa41c6852019-02-07 10:44:10 -050077 fResourceCache = new GrResourceCache(this->caps(), this->singleOwner(), this->contextID());
Robert Phillips12c46292019-04-23 07:36:17 -040078 fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, this->singleOwner());
Mike Kleinf46d5ca2019-12-11 10:45:01 -050079 fMappedBufferManager = std::make_unique<GrClientMappedBufferManager>(this->contextID());
Robert Phillips88260b52018-01-19 12:56:09 -050080 }
81
Robert Phillips88260b52018-01-19 12:56:09 -050082 if (fResourceCache) {
Robert Phillipsa41c6852019-02-07 10:44:10 -050083 fResourceCache->setProxyProvider(this->proxyProvider());
Robert Phillips88260b52018-01-19 12:56:09 -050084 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050085
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000086 fDidTestPMConversions = false;
87
Robert Phillipsfde6fa02018-03-02 08:53:14 -050088 // DDL TODO: we need to think through how the task group & persistent cache
89 // get passed on to/shared between all the DDLRecorders created with this context.
Robert Phillipsc1541ae2019-02-04 12:05:37 -050090 if (this->options().fExecutor) {
Mike Kleinf46d5ca2019-12-11 10:45:01 -050091 fTaskGroup = std::make_unique<SkTaskGroup>(*this->options().fExecutor);
Brian Osman51279982017-08-23 10:12:00 -040092 }
93
Robert Phillipsc1541ae2019-02-04 12:05:37 -050094 fPersistentCache = this->options().fPersistentCache;
Brian Osman5e7fbfd2019-05-03 13:13:35 -040095 fShaderErrorHandler = this->options().fShaderErrorHandler;
96 if (!fShaderErrorHandler) {
97 fShaderErrorHandler = GrShaderUtils::DefaultShaderErrorHandler();
98 }
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -040099
Brian Salomon91a3e522017-06-23 10:58:19 -0400100 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000101}
102
Robert Phillips4217ea72019-01-30 13:08:28 -0500103sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
Adlai Hollere219d1c2020-06-02 11:23:16 -0400104 return INHERITED::threadSafeProxy();
Robert Phillips4217ea72019-01-30 13:08:28 -0500105}
106
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400107//////////////////////////////////////////////////////////////////////////////
108
bsalomon2354f842014-07-28 13:48:36 -0700109void GrContext::abandonContext() {
Greg Daniel6e35a002020-04-01 13:29:59 -0400110 if (INHERITED::abandoned()) {
Robert Phillipsa9162df2019-02-11 14:12:03 -0500111 return;
112 }
joshualitt1de610a2016-01-06 08:26:09 -0800113
Robert Phillipsa9162df2019-02-11 14:12:03 -0500114 INHERITED::abandonContext();
115
Robert Phillips4d932d12020-04-09 08:58:52 -0400116 fStrikeCache->freeAll();
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
bsalomon@google.com205d4602011-04-25 12:43:45 +0000122 // abandon first to so destructors
123 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800124 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700125
bsalomon6e2aad42016-04-01 11:54:31 -0700126 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400127
128 fMappedBufferManager.reset();
Khushalc421ca12018-06-26 14:38:34 -0700129}
130
bsalomon6e2aad42016-04-01 11:54:31 -0700131void GrContext::releaseResourcesAndAbandonContext() {
Greg Daniel6e35a002020-04-01 13:29:59 -0400132 if (INHERITED::abandoned()) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500133 return;
134 }
Robert Phillipsa9162df2019-02-11 14:12:03 -0500135
136 INHERITED::abandonContext();
137
Brian Salomon9241a6d2019-10-03 13:26:54 -0400138 fMappedBufferManager.reset();
139
bsalomon6e2aad42016-04-01 11:54:31 -0700140 fResourceProvider->abandon();
141
bsalomon6e2aad42016-04-01 11:54:31 -0700142 // Release all resources in the backend 3D API.
143 fResourceCache->releaseAll();
144
145 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000146}
147
Greg Daniel6e35a002020-04-01 13:29:59 -0400148bool GrContext::abandoned() {
149 if (INHERITED::abandoned()) {
150 return true;
151 }
152
153 if (fGpu && fGpu->isDeviceLost()) {
154 this->abandonContext();
155 return true;
156 }
157 return false;
158}
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.
Robert Phillips4d932d12020-04-09 08:58:52 -0400177 fStrikeCache->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 Daniele8d3cca2020-06-10 10:04:48 -0400302GrSemaphoresSubmitted GrContext::flush(const GrFlushInfo& info) {
Greg Daniel51316782017-08-02 15:10:09 +0000303 ASSERT_SINGLE_OWNER
Robert Phillipsa9162df2019-02-11 14:12:03 -0500304 if (this->abandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400305 if (info.fFinishedProc) {
306 info.fFinishedProc(info.fFinishedContext);
307 }
308 if (info.fSubmittedProc) {
309 info.fSubmittedProc(info.fSubmittedContext, false);
310 }
Robert Phillipsa9162df2019-02-11 14:12:03 -0500311 return GrSemaphoresSubmitted::kNo;
312 }
Greg Daniel51316782017-08-02 15:10:09 +0000313
Greg Daniel04283f32020-05-20 13:16:00 -0400314 bool flushed = this->drawingManager()->flush(
Greg Daniel9efe3862020-06-11 11:51:06 -0400315 nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, info, nullptr);
Greg Danielfe159622020-04-10 17:43:51 +0000316
Greg Daniel04283f32020-05-20 13:16:00 -0400317 if (!flushed || (!this->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
Greg Danielfe159622020-04-10 17:43:51 +0000318 return GrSemaphoresSubmitted::kNo;
319 }
320 return GrSemaphoresSubmitted::kYes;
Greg Daniel51316782017-08-02 15:10:09 +0000321}
322
Greg Daniel04283f32020-05-20 13:16:00 -0400323bool GrContext::submit(bool syncCpu) {
324 ASSERT_SINGLE_OWNER
325 if (this->abandoned()) {
326 return false;
327 }
328
329 if (!fGpu) {
330 return false;
331 }
332
333 return fGpu->submitToGpu(syncCpu);
Greg Danielda50cb82020-05-13 14:07:40 -0400334}
335
Greg Daniela870b462019-01-08 15:49:46 -0500336////////////////////////////////////////////////////////////////////////////////
337
Brian Salomonb0d8b762019-05-06 16:58:22 -0400338void GrContext::checkAsyncWorkCompletion() {
339 if (fGpu) {
340 fGpu->checkFinishProcs();
341 }
342}
343
344////////////////////////////////////////////////////////////////////////////////
345
Greg Daniela870b462019-01-08 15:49:46 -0500346void GrContext::storeVkPipelineCacheData() {
347 if (fGpu) {
348 fGpu->storeVkPipelineCacheData();
349 }
350}
351
352////////////////////////////////////////////////////////////////////////////////
353
Khushal3e7548c2018-05-23 15:45:01 -0700354bool GrContext::supportsDistanceFieldText() const {
Robert Phillipsbb606772019-02-04 17:50:57 -0500355 return this->caps()->shaderCaps()->supportsDistanceFieldText();
Khushal3e7548c2018-05-23 15:45:01 -0700356}
357
bsalomon37f9a262015-02-02 13:00:10 -0800358//////////////////////////////////////////////////////////////////////////////
359
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500360void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800361 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500362 if (maxResources) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400363 *maxResources = -1;
bsalomon37f9a262015-02-02 13:00:10 -0800364 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500365 if (maxResourceBytes) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400366 *maxResourceBytes = this->getResourceCacheLimit();
bsalomon37f9a262015-02-02 13:00:10 -0800367 }
368}
369
Robert Phillipscf39f372019-09-03 10:29:20 -0400370size_t GrContext::getResourceCacheLimit() const {
joshualitt1de610a2016-01-06 08:26:09 -0800371 ASSERT_SINGLE_OWNER
Robert Phillipscf39f372019-09-03 10:29:20 -0400372 return fResourceCache->getMaxResourceBytes();
373}
374
375void GrContext::setResourceCacheLimits(int unused, size_t maxResourceBytes) {
376 ASSERT_SINGLE_OWNER
377 this->setResourceCacheLimit(maxResourceBytes);
378}
379
380void GrContext::setResourceCacheLimit(size_t maxResourceBytes) {
381 ASSERT_SINGLE_OWNER
382 fResourceCache->setLimit(maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800383}
384
ericrk0a5fa482015-09-15 14:16:10 -0700385//////////////////////////////////////////////////////////////////////////////
ericrk0a5fa482015-09-15 14:16:10 -0700386void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800387 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700388 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
Khushal71652e22018-10-29 13:05:36 -0700389 traceMemoryDump->dumpNumericValue("skia/gr_text_blob_cache", "size", "bytes",
Robert Phillips2184fb72019-02-21 16:11:41 -0500390 this->getTextBlobCache()->usedBytes());
ericrk0a5fa482015-09-15 14:16:10 -0700391}
Brian Osman71a18892017-08-10 10:23:25 -0400392
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400393//////////////////////////////////////////////////////////////////////////////
394GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400395 const GrBackendFormat& backendFormat,
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400396 GrMipMapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400397 GrRenderable renderable,
398 GrProtected isProtected) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400399 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400400 if (!this->asDirectContext()) {
401 return GrBackendTexture();
402 }
403
404 if (this->abandoned()) {
405 return GrBackendTexture();
406 }
407
Robert Phillips4277f012020-01-21 14:28:34 -0500408 return fGpu->createBackendTexture({width, height}, backendFormat, renderable,
Greg Daniel16032b32020-05-06 15:31:10 -0400409 mipMapped, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400410}
411
412GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400413 SkColorType skColorType,
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400414 GrMipMapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400415 GrRenderable renderable,
416 GrProtected isProtected) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400417 if (!this->asDirectContext()) {
418 return GrBackendTexture();
419 }
420
421 if (this->abandoned()) {
422 return GrBackendTexture();
423 }
424
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400425 const GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400426
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400427 return this->createBackendTexture(width, height, format, mipMapped, renderable, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400428}
429
Robert Phillips02dc0302019-07-02 17:58:27 -0400430GrBackendTexture GrContext::createBackendTexture(const SkSurfaceCharacterization& c) {
Robert Phillips02dc0302019-07-02 17:58:27 -0400431 if (!this->asDirectContext() || !c.isValid()) {
432 return GrBackendTexture();
433 }
434
435 if (this->abandoned()) {
436 return GrBackendTexture();
437 }
438
439 if (c.usesGLFBO0()) {
440 // If we are making the surface we will never use FBO0.
441 return GrBackendTexture();
442 }
443
444 if (c.vulkanSecondaryCBCompatible()) {
445 return {};
446 }
447
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400448 const GrBackendFormat format = this->defaultBackendFormat(c.colorType(), GrRenderable::kYes);
Robert Phillips02dc0302019-07-02 17:58:27 -0400449 if (!format.isValid()) {
450 return GrBackendTexture();
451 }
452
Robert Phillips02dc0302019-07-02 17:58:27 -0400453 GrBackendTexture result = this->createBackendTexture(c.width(), c.height(), format,
454 GrMipMapped(c.isMipMapped()),
455 GrRenderable::kYes,
Robert Phillips3cd54322019-07-10 09:28:59 -0400456 c.isProtected());
Robert Phillips02dc0302019-07-02 17:58:27 -0400457 SkASSERT(c.isCompatible(result));
458 return result;
459}
460
Greg Daniel25597782020-06-11 13:15:08 -0400461static GrBackendTexture create_and_update_backend_texture(
462 GrContext* context,
463 SkISize dimensions,
464 const GrBackendFormat& backendFormat,
465 GrMipMapped mipMapped,
466 GrRenderable renderable,
467 GrProtected isProtected,
468 sk_sp<GrRefCntedCallback> finishedCallback,
469 const GrGpu::BackendTextureData* data) {
Greg Daniel16032b32020-05-06 15:31:10 -0400470 GrGpu* gpu = context->priv().getGpu();
471
472 GrBackendTexture beTex = gpu->createBackendTexture(dimensions, backendFormat, renderable,
473 mipMapped, isProtected);
474 if (!beTex.isValid()) {
475 return {};
476 }
477
Greg Daniel25597782020-06-11 13:15:08 -0400478 if (!context->priv().getGpu()->updateBackendTexture(beTex, std::move(finishedCallback), data)) {
Greg Daniel16032b32020-05-06 15:31:10 -0400479 context->deleteBackendTexture(beTex);
480 return {};
481 }
482 return beTex;
483}
484
Greg Daniel25597782020-06-11 13:15:08 -0400485
486GrBackendTexture GrContext::createBackendTexture(const SkSurfaceCharacterization& c,
487 const SkColor4f& color,
488 GrGpuFinishedProc finishedProc,
489 GrGpuFinishedContext finishedContext) {
490 sk_sp<GrRefCntedCallback> finishedCallback;
491 if (finishedProc) {
492 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
493 }
494
495 if (!this->asDirectContext() || !c.isValid()) {
496 return {};
497 }
498
499 if (this->abandoned()) {
500 return {};
501 }
502
503 if (c.usesGLFBO0()) {
504 // If we are making the surface we will never use FBO0.
505 return {};
506 }
507
508 if (c.vulkanSecondaryCBCompatible()) {
509 return {};
510 }
511
512 const GrBackendFormat format = this->defaultBackendFormat(c.colorType(), GrRenderable::kYes);
513 if (!format.isValid()) {
514 return {};
515 }
516
517 GrGpu::BackendTextureData data(color);
518 GrBackendTexture result = create_and_update_backend_texture(
519 this, {c.width(), c.height()}, format, GrMipMapped(c.isMipMapped()), GrRenderable::kYes,
520 c.isProtected(), std::move(finishedCallback), &data);
521
522 SkASSERT(c.isCompatible(result));
523 return result;
524}
525
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400526GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400527 const GrBackendFormat& backendFormat,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400528 const SkColor4f& color,
529 GrMipMapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400530 GrRenderable renderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400531 GrProtected isProtected,
532 GrGpuFinishedProc finishedProc,
533 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400534 sk_sp<GrRefCntedCallback> finishedCallback;
535 if (finishedProc) {
536 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
537 }
538
Brian Salomonc42eb662019-06-24 17:13:00 -0400539 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400540 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400541 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400542 }
543
544 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400545 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400546 }
547
Brian Salomon85c3d682019-11-04 15:04:54 -0500548 GrGpu::BackendTextureData data(color);
Greg Daniel16032b32020-05-06 15:31:10 -0400549 return create_and_update_backend_texture(this, {width, height}, backendFormat, mipMapped,
Greg Daniel25597782020-06-11 13:15:08 -0400550 renderable, isProtected, std::move(finishedCallback),
Greg Daniel16032b32020-05-06 15:31:10 -0400551 &data);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400552}
553
554GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400555 SkColorType skColorType,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400556 const SkColor4f& color,
557 GrMipMapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400558 GrRenderable renderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400559 GrProtected isProtected,
560 GrGpuFinishedProc finishedProc,
561 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400562 sk_sp<GrRefCntedCallback> finishedCallback;
563 if (finishedProc) {
564 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
565 }
566
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400567 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400568 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400569 }
570
571 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400572 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400573 }
574
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400575 GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400576 if (!format.isValid()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400577 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400578 }
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400579
580 GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
Brian Salomon982f5462020-03-30 12:52:33 -0400581 SkColor4f swizzledColor = this->caps()->getWriteSwizzle(format, grColorType).applyTo(color);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400582
Greg Daniel25597782020-06-11 13:15:08 -0400583 GrGpu::BackendTextureData data(swizzledColor);
584 return create_and_update_backend_texture(this, {width, height}, format, mipMapped,
585 renderable, isProtected, std::move(finishedCallback),
586 &data);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400587}
588
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500589GrBackendTexture GrContext::createBackendTexture(const SkPixmap srcData[], int numProvidedLevels,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400590 GrRenderable renderable, GrProtected isProtected,
591 GrGpuFinishedProc finishedProc,
592 GrGpuFinishedContext finishedContext) {
Robert Phillips66944402019-09-30 13:21:25 -0400593 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
594
Greg Daniel25597782020-06-11 13:15:08 -0400595 sk_sp<GrRefCntedCallback> finishedCallback;
596 if (finishedProc) {
597 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
598 }
599
Robert Phillips66944402019-09-30 13:21:25 -0400600 if (!this->asDirectContext()) {
601 return {};
602 }
603
604 if (this->abandoned()) {
605 return {};
606 }
607
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500608 if (!srcData || numProvidedLevels <= 0) {
Robert Phillips66944402019-09-30 13:21:25 -0400609 return {};
610 }
611
612 int baseWidth = srcData[0].width();
613 int baseHeight = srcData[0].height();
614 SkColorType colorType = srcData[0].colorType();
615
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500616 GrMipMapped mipMapped = GrMipMapped::kNo;
617 int numExpectedLevels = 1;
618 if (numProvidedLevels > 1) {
619 numExpectedLevels = SkMipMap::ComputeLevelCount(baseWidth, baseHeight) + 1;
620 mipMapped = GrMipMapped::kYes;
621 }
622
623 if (numProvidedLevels != numExpectedLevels) {
624 return {};
625 }
626
Robert Phillips66944402019-09-30 13:21:25 -0400627 GrBackendFormat backendFormat = this->defaultBackendFormat(colorType, renderable);
628
Brian Salomon85c3d682019-11-04 15:04:54 -0500629 GrGpu::BackendTextureData data(srcData);
Greg Daniel16032b32020-05-06 15:31:10 -0400630 return create_and_update_backend_texture(this, {baseWidth, baseHeight}, backendFormat,
Greg Daniel25597782020-06-11 13:15:08 -0400631 mipMapped, renderable, isProtected,
632 std::move(finishedCallback), &data);
Robert Phillips66944402019-09-30 13:21:25 -0400633}
634
Greg Danielb2365d82020-05-13 15:32:04 -0400635bool GrContext::updateBackendTexture(const GrBackendTexture& backendTexture,
636 const SkColor4f& color,
637 GrGpuFinishedProc finishedProc,
638 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400639 sk_sp<GrRefCntedCallback> finishedCallback;
640 if (finishedProc) {
641 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
642 }
643
Greg Danielb2365d82020-05-13 15:32:04 -0400644 if (!this->asDirectContext()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400645 return false;
646 }
647
648 if (this->abandoned()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400649 return false;
650 }
651
652 GrGpu::BackendTextureData data(color);
Greg Daniel25597782020-06-11 13:15:08 -0400653 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
Greg Danielb2365d82020-05-13 15:32:04 -0400654}
655
656bool GrContext::updateBackendTexture(const GrBackendTexture& backendTexture,
657 const SkPixmap srcData[],
658 int numLevels,
659 GrGpuFinishedProc finishedProc,
660 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400661 sk_sp<GrRefCntedCallback> finishedCallback;
662 if (finishedProc) {
663 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
664 }
665
Greg Danielb2365d82020-05-13 15:32:04 -0400666 if (!this->asDirectContext()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400667 return false;
668 }
669
670 if (this->abandoned()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400671 return false;
672 }
673
674 if (!srcData || numLevels <= 0) {
Greg Danielb2365d82020-05-13 15:32:04 -0400675 return false;
676 }
677
678 int numExpectedLevels = 1;
679 if (backendTexture.hasMipMaps()) {
680 numExpectedLevels = SkMipMap::ComputeLevelCount(backendTexture.width(),
681 backendTexture.height()) + 1;
682 }
683 if (numLevels != numExpectedLevels) {
Greg Danielb2365d82020-05-13 15:32:04 -0400684 return false;
685 }
686
687 GrGpu::BackendTextureData data(srcData);
Greg Daniel25597782020-06-11 13:15:08 -0400688 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
Greg Danielb2365d82020-05-13 15:32:04 -0400689}
690
Robert Phillipsb915c942019-12-17 14:44:37 -0500691//////////////////////////////////////////////////////////////////////////////
692
693GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
694 const GrBackendFormat& backendFormat,
695 const SkColor4f& color,
696 GrMipMapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400697 GrProtected isProtected,
698 GrGpuFinishedProc finishedProc,
699 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500700 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel25597782020-06-11 13:15:08 -0400701 sk_sp<GrRefCntedCallback> finishedCallback;
702 if (finishedProc) {
703 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
704 }
705
Robert Phillipsb915c942019-12-17 14:44:37 -0500706 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400707 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500708 }
709
710 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400711 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500712 }
713
714 GrGpu::BackendTextureData data(color);
Robert Phillips4277f012020-01-21 14:28:34 -0500715 return fGpu->createCompressedBackendTexture({width, height}, backendFormat,
Greg Daniel25597782020-06-11 13:15:08 -0400716 mipMapped, isProtected, std::move(finishedCallback),
717 &data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500718}
719
720GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
721 SkImage::CompressionType compression,
722 const SkColor4f& color,
723 GrMipMapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400724 GrProtected isProtected,
725 GrGpuFinishedProc finishedProc,
726 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500727 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillipsb915c942019-12-17 14:44:37 -0500728 GrBackendFormat format = this->compressedBackendFormat(compression);
729 return this->createCompressedBackendTexture(width, height, format, color,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400730 mipMapped, isProtected, finishedProc,
731 finishedContext);
Robert Phillipsb915c942019-12-17 14:44:37 -0500732}
733
734GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
735 const GrBackendFormat& backendFormat,
736 const void* compressedData,
737 size_t dataSize,
738 GrMipMapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400739 GrProtected isProtected,
740 GrGpuFinishedProc finishedProc,
741 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500742 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel25597782020-06-11 13:15:08 -0400743 sk_sp<GrRefCntedCallback> finishedCallback;
744 if (finishedProc) {
745 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
746 }
747
Robert Phillipsb915c942019-12-17 14:44:37 -0500748 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400749 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500750 }
751
752 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400753 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500754 }
755
756 GrGpu::BackendTextureData data(compressedData, dataSize);
Robert Phillips4277f012020-01-21 14:28:34 -0500757 return fGpu->createCompressedBackendTexture({width, height}, backendFormat,
Greg Daniel25597782020-06-11 13:15:08 -0400758 mipMapped, isProtected, std::move(finishedCallback),
759 &data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500760}
761
762GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
763 SkImage::CompressionType compression,
764 const void* data, size_t dataSize,
765 GrMipMapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400766 GrProtected isProtected,
767 GrGpuFinishedProc finishedProc,
768 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500769 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillipsb915c942019-12-17 14:44:37 -0500770 GrBackendFormat format = this->compressedBackendFormat(compression);
Greg Daniel25597782020-06-11 13:15:08 -0400771 return this->createCompressedBackendTexture(width, height, format, data, dataSize, mipMapped,
772 isProtected, finishedProc, finishedContext);
Robert Phillipsb915c942019-12-17 14:44:37 -0500773}
774
Greg Daniel1db8e792020-06-09 17:29:32 -0400775bool GrContext::setBackendTextureState(const GrBackendTexture& backendTexture,
776 const GrBackendSurfaceMutableState& state,
777 GrGpuFinishedProc finishedProc,
778 GrGpuFinishedContext finishedContext) {
Greg Daniel1db8e792020-06-09 17:29:32 -0400779 sk_sp<GrRefCntedCallback> callback;
780 if (finishedProc) {
781 callback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
782 }
Greg Daniel25597782020-06-11 13:15:08 -0400783
784 if (!this->asDirectContext()) {
785 return false;
786 }
787
788 if (this->abandoned()) {
789 return false;
790 }
791
Greg Daniel1db8e792020-06-09 17:29:32 -0400792 return fGpu->setBackendTextureState(backendTexture, state, std::move(callback));
793}
794
795bool GrContext::setBackendRenderTargetState(const GrBackendRenderTarget& backendRenderTarget,
796 const GrBackendSurfaceMutableState& state,
797 GrGpuFinishedProc finishedProc,
798 GrGpuFinishedContext finishedContext) {
Greg Daniel1db8e792020-06-09 17:29:32 -0400799 sk_sp<GrRefCntedCallback> callback;
800 if (finishedProc) {
801 callback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
802 }
Greg Daniel25597782020-06-11 13:15:08 -0400803
804 if (!this->asDirectContext()) {
805 return false;
806 }
807
808 if (this->abandoned()) {
809 return false;
810 }
811
Greg Daniel1db8e792020-06-09 17:29:32 -0400812 return fGpu->setBackendRenderTargetState(backendRenderTarget, state, std::move(callback));
813}
814
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400815void GrContext::deleteBackendTexture(GrBackendTexture backendTex) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400816 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danielf0e04f02019-12-04 15:17:54 -0500817 // For the Vulkan backend we still must destroy the backend texture when the context is
818 // abandoned.
819 if ((this->abandoned() && this->backend() != GrBackendApi::kVulkan) || !backendTex.isValid()) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400820 return;
821 }
822
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400823 fGpu->deleteBackendTexture(backendTex);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400824}
825
Greg Daniel1db8e792020-06-09 17:29:32 -0400826//////////////////////////////////////////////////////////////////////////////
827
Brian Osmaned58e002019-09-06 14:42:43 -0400828bool GrContext::precompileShader(const SkData& key, const SkData& data) {
829 return fGpu->precompileShader(key, data);
830}
831
Brian Salomonec22b1a2019-08-09 09:41:48 -0400832#ifdef SK_ENABLE_DUMP_GPU
Michael Ludwigdd205452020-03-30 17:16:34 -0400833#include "include/core/SkString.h"
Brian Salomonec22b1a2019-08-09 09:41:48 -0400834#include "src/utils/SkJSONWriter.h"
835SkString GrContext::dump() const {
836 SkDynamicMemoryWStream stream;
837 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
838 writer.beginObject();
839
840 writer.appendString("backend", GrBackendApiToStr(this->backend()));
841
842 writer.appendName("caps");
843 this->caps()->dumpJSON(&writer);
844
845 writer.appendName("gpu");
846 this->fGpu->dumpJSON(&writer);
847
Robert Phillips273f1072020-05-05 13:03:07 -0400848 writer.appendName("context");
849 this->dumpJSON(&writer);
850
Brian Salomonec22b1a2019-08-09 09:41:48 -0400851 // Flush JSON to the memory stream
852 writer.endObject();
853 writer.flush();
854
855 // Null terminate the JSON data in the memory stream
856 stream.write8(0);
857
858 // Allocate a string big enough to hold all the data, then copy out of the stream
859 SkString result(stream.bytesWritten());
860 stream.copyToAndReset(result.writable_str());
861 return result;
862}
863#endif