blob: 71359d1619bc43bc347dd185de28b9991973bc8e [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();
Robert Phillips292a6b22019-02-14 14:49:02 -050060 delete fResourceProvider;
61 delete fResourceCache;
Robert Phillips292a6b22019-02-14 14:49:02 -050062}
63
Adlai Hollere219d1c2020-06-02 11:23:16 -040064bool GrContext::init() {
Greg Danielb76a72a2017-07-13 15:07:54 -040065 ASSERT_SINGLE_OWNER
Robert Phillipsa41c6852019-02-07 10:44:10 -050066 SkASSERT(this->proxyProvider());
Robert Phillips88260b52018-01-19 12:56:09 -050067
Adlai Hollere219d1c2020-06-02 11:23:16 -040068 if (!INHERITED::init()) {
Robert Phillipsbb606772019-02-04 17:50:57 -050069 return false;
70 }
71
Robert Phillips2184fb72019-02-21 16:11:41 -050072 SkASSERT(this->getTextBlobCache());
Robert Phillipsbb606772019-02-04 17:50:57 -050073
Robert Phillips88260b52018-01-19 12:56:09 -050074 if (fGpu) {
Robert Phillips4d932d12020-04-09 08:58:52 -040075 fStrikeCache.reset(new GrStrikeCache{});
Robert Phillipsa41c6852019-02-07 10:44:10 -050076 fResourceCache = new GrResourceCache(this->caps(), this->singleOwner(), this->contextID());
Robert Phillips12c46292019-04-23 07:36:17 -040077 fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, this->singleOwner());
Mike Kleinf46d5ca2019-12-11 10:45:01 -050078 fMappedBufferManager = std::make_unique<GrClientMappedBufferManager>(this->contextID());
Robert Phillips88260b52018-01-19 12:56:09 -050079 }
80
Robert Phillips88260b52018-01-19 12:56:09 -050081 if (fResourceCache) {
Robert Phillipsa41c6852019-02-07 10:44:10 -050082 fResourceCache->setProxyProvider(this->proxyProvider());
Robert Phillips88260b52018-01-19 12:56:09 -050083 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050084
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000085 fDidTestPMConversions = false;
86
Robert Phillipsfde6fa02018-03-02 08:53:14 -050087 // DDL TODO: we need to think through how the task group & persistent cache
88 // get passed on to/shared between all the DDLRecorders created with this context.
Robert Phillipsc1541ae2019-02-04 12:05:37 -050089 if (this->options().fExecutor) {
Mike Kleinf46d5ca2019-12-11 10:45:01 -050090 fTaskGroup = std::make_unique<SkTaskGroup>(*this->options().fExecutor);
Brian Osman51279982017-08-23 10:12:00 -040091 }
92
Robert Phillipsc1541ae2019-02-04 12:05:37 -050093 fPersistentCache = this->options().fPersistentCache;
Brian Osman5e7fbfd2019-05-03 13:13:35 -040094 fShaderErrorHandler = this->options().fShaderErrorHandler;
95 if (!fShaderErrorHandler) {
96 fShaderErrorHandler = GrShaderUtils::DefaultShaderErrorHandler();
97 }
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -040098
Brian Salomon91a3e522017-06-23 10:58:19 -040099 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000100}
101
Robert Phillips4217ea72019-01-30 13:08:28 -0500102sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
Adlai Hollere219d1c2020-06-02 11:23:16 -0400103 return INHERITED::threadSafeProxy();
Robert Phillips4217ea72019-01-30 13:08:28 -0500104}
105
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400106//////////////////////////////////////////////////////////////////////////////
107
bsalomon2354f842014-07-28 13:48:36 -0700108void GrContext::abandonContext() {
Greg Daniel6e35a002020-04-01 13:29:59 -0400109 if (INHERITED::abandoned()) {
Robert Phillipsa9162df2019-02-11 14:12:03 -0500110 return;
111 }
joshualitt1de610a2016-01-06 08:26:09 -0800112
Robert Phillipsa9162df2019-02-11 14:12:03 -0500113 INHERITED::abandonContext();
114
Robert Phillips4d932d12020-04-09 08:58:52 -0400115 fStrikeCache->freeAll();
116
Brian Salomon9241a6d2019-10-03 13:26:54 -0400117 fMappedBufferManager->abandon();
118
bsalomond309e7a2015-04-30 14:18:54 -0700119 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800120
bsalomon@google.com205d4602011-04-25 12:43:45 +0000121 // abandon first to so destructors
122 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800123 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700124
bsalomon6e2aad42016-04-01 11:54:31 -0700125 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400126
127 fMappedBufferManager.reset();
Khushalc421ca12018-06-26 14:38:34 -0700128}
129
bsalomon6e2aad42016-04-01 11:54:31 -0700130void GrContext::releaseResourcesAndAbandonContext() {
Greg Daniel6e35a002020-04-01 13:29:59 -0400131 if (INHERITED::abandoned()) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500132 return;
133 }
Robert Phillipsa9162df2019-02-11 14:12:03 -0500134
135 INHERITED::abandonContext();
136
Brian Salomon9241a6d2019-10-03 13:26:54 -0400137 fMappedBufferManager.reset();
138
bsalomon6e2aad42016-04-01 11:54:31 -0700139 fResourceProvider->abandon();
140
bsalomon6e2aad42016-04-01 11:54:31 -0700141 // Release all resources in the backend 3D API.
142 fResourceCache->releaseAll();
143
144 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000145}
146
Greg Daniel6e35a002020-04-01 13:29:59 -0400147bool GrContext::abandoned() {
148 if (INHERITED::abandoned()) {
149 return true;
150 }
151
152 if (fGpu && fGpu->isDeviceLost()) {
153 this->abandonContext();
154 return true;
155 }
156 return false;
157}
158
Brian Salomon1f05d452019-02-08 12:33:08 -0500159void GrContext::resetGLTextureBindings() {
160 if (this->abandoned() || this->backend() != GrBackendApi::kOpenGL) {
161 return;
162 }
163 fGpu->resetTextureBindings();
164}
165
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000166void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800167 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000168 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000169}
170
171void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800172 ASSERT_SINGLE_OWNER
173
Robert Phillips2184fb72019-02-21 16:11:41 -0500174 // TODO: the glyph cache doesn't hold any GpuResources so this call should not be needed here.
175 // Some slack in the GrTextBlob's implementation requires it though. That could be fixed.
Robert Phillips4d932d12020-04-09 08:58:52 -0400176 fStrikeCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700177
Robert Phillips6a6de562019-02-15 15:19:15 -0500178 this->drawingManager()->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700179
180 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000181}
182
Robert Phillips6eba0632018-03-28 12:25:42 -0400183void GrContext::purgeUnlockedResources(bool scratchResourcesOnly) {
184 ASSERT_SINGLE_OWNER
Robert Phillipsddc21482019-10-16 14:30:09 -0400185
186 if (this->abandoned()) {
187 return;
188 }
189
Robert Phillips6eba0632018-03-28 12:25:42 -0400190 fResourceCache->purgeUnlockedResources(scratchResourcesOnly);
191 fResourceCache->purgeAsNeeded();
Robert Phillips2184fb72019-02-21 16:11:41 -0500192
193 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
194 // place to purge stale blobs
195 this->getTextBlobCache()->purgeStaleBlobs();
Robert Phillips6eba0632018-03-28 12:25:42 -0400196}
197
Jim Van Verth76d917c2017-12-13 09:26:37 -0500198void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Yuqian Li40aa85f2019-07-02 13:45:00 -0700199 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
200
Brian Salomon5e150852017-03-22 14:53:13 -0400201 ASSERT_SINGLE_OWNER
Chris Dalton6c3879d2018-11-01 11:13:19 -0600202
Brian Salomon9241a6d2019-10-03 13:26:54 -0400203 if (this->abandoned()) {
204 return;
205 }
206
207 fMappedBufferManager->process();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600208 auto purgeTime = GrStdSteadyClock::now() - msNotUsed;
209
Jim Van Verth76d917c2017-12-13 09:26:37 -0500210 fResourceCache->purgeAsNeeded();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600211 fResourceCache->purgeResourcesNotUsedSince(purgeTime);
212
Robert Phillips6a6de562019-02-15 15:19:15 -0500213 if (auto ccpr = this->drawingManager()->getCoverageCountingPathRenderer()) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500214 ccpr->purgeCacheEntriesOlderThan(this->proxyProvider(), purgeTime);
Chris Dalton6c3879d2018-11-01 11:13:19 -0600215 }
Jim Van Verth76d917c2017-12-13 09:26:37 -0500216
Robert Phillips2184fb72019-02-21 16:11:41 -0500217 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
218 // place to purge stale blobs
219 this->getTextBlobCache()->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400220}
221
Derek Sollenberger5480a182017-05-25 16:43:59 -0400222void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
223 ASSERT_SINGLE_OWNER
Robert Phillipsddc21482019-10-16 14:30:09 -0400224
225 if (this->abandoned()) {
226 return;
227 }
228
Derek Sollenberger5480a182017-05-25 16:43:59 -0400229 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
230}
231
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000232void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800233 ASSERT_SINGLE_OWNER
234
bsalomon71cb0c22014-11-14 12:10:14 -0800235 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800236 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800237 }
238 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800239 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800240 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000241}
242
Derek Sollenbergeree479142017-05-24 11:41:33 -0400243size_t GrContext::getResourceCachePurgeableBytes() const {
244 ASSERT_SINGLE_OWNER
245 return fResourceCache->getPurgeableBytes();
246}
247
Greg Daniel8b666172019-10-09 12:38:22 -0400248size_t GrContext::ComputeImageSize(sk_sp<SkImage> image, GrMipMapped mipMapped, bool useNextPow2) {
249 if (!image->isTextureBacked()) {
250 return 0;
251 }
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400252 SkImage_GpuBase* gpuImage = static_cast<SkImage_GpuBase*>(as_IB(image.get()));
253 GrTextureProxy* proxy = gpuImage->peekProxy();
254 if (!proxy) {
255 return 0;
256 }
257
258 const GrCaps& caps = *gpuImage->context()->priv().caps();
Greg Daniel8b666172019-10-09 12:38:22 -0400259 int colorSamplesPerPixel = 1;
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400260 return GrSurface::ComputeSize(caps, proxy->backendFormat(), image->dimensions(),
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400261 colorSamplesPerPixel, mipMapped, useNextPow2);
Greg Daniel8b666172019-10-09 12:38:22 -0400262}
263
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000264////////////////////////////////////////////////////////////////////////////////
265
Robert Phillipsbb606772019-02-04 17:50:57 -0500266int GrContext::maxTextureSize() const { return this->caps()->maxTextureSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400267
Robert Phillipsbb606772019-02-04 17:50:57 -0500268int GrContext::maxRenderTargetSize() const { return this->caps()->maxRenderTargetSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400269
Brian Salomonbdecacf2018-02-02 20:32:49 -0500270bool GrContext::colorTypeSupportedAsImage(SkColorType colorType) const {
Greg Daniel7bfc9132019-08-14 14:23:53 -0400271 GrBackendFormat format =
272 this->caps()->getDefaultBackendFormat(SkColorTypeToGrColorType(colorType),
273 GrRenderable::kNo);
274 return format.isValid();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500275}
276
277int GrContext::maxSurfaceSampleCountForColorType(SkColorType colorType) const {
Greg Danieleadfac92019-08-02 09:03:53 -0400278 GrBackendFormat format =
279 this->caps()->getDefaultBackendFormat(SkColorTypeToGrColorType(colorType),
280 GrRenderable::kYes);
281 return this->caps()->maxRenderTargetSampleCount(format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500282}
283
284////////////////////////////////////////////////////////////////////////////////
285
Greg Daniel06be0792019-04-22 15:53:23 -0400286bool GrContext::wait(int numSemaphores, const GrBackendSemaphore waitSemaphores[]) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400287 if (!fGpu || fGpu->caps()->semaphoreSupport()) {
Greg Daniel06be0792019-04-22 15:53:23 -0400288 return false;
289 }
290 for (int i = 0; i < numSemaphores; ++i) {
Greg Daniel301015c2019-11-18 14:06:46 -0500291 std::unique_ptr<GrSemaphore> sema = fResourceProvider->wrapBackendSemaphore(
Greg Daniel06be0792019-04-22 15:53:23 -0400292 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
293 kAdopt_GrWrapOwnership);
Greg Daniel301015c2019-11-18 14:06:46 -0500294 fGpu->waitSemaphore(sema.get());
Greg Daniel06be0792019-04-22 15:53:23 -0400295 }
296 return true;
297}
298
299////////////////////////////////////////////////////////////////////////////////
300
Greg Daniele8d3cca2020-06-10 10:04:48 -0400301GrSemaphoresSubmitted GrContext::flush(const GrFlushInfo& info) {
Greg Daniel51316782017-08-02 15:10:09 +0000302 ASSERT_SINGLE_OWNER
Robert Phillipsa9162df2019-02-11 14:12:03 -0500303 if (this->abandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400304 if (info.fFinishedProc) {
305 info.fFinishedProc(info.fFinishedContext);
306 }
307 if (info.fSubmittedProc) {
308 info.fSubmittedProc(info.fSubmittedContext, false);
309 }
Robert Phillipsa9162df2019-02-11 14:12:03 -0500310 return GrSemaphoresSubmitted::kNo;
311 }
Greg Daniel51316782017-08-02 15:10:09 +0000312
Greg Daniel04283f32020-05-20 13:16:00 -0400313 bool flushed = this->drawingManager()->flush(
Greg Daniel9efe3862020-06-11 11:51:06 -0400314 nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, info, nullptr);
Greg Danielfe159622020-04-10 17:43:51 +0000315
Greg Daniel04283f32020-05-20 13:16:00 -0400316 if (!flushed || (!this->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
Greg Danielfe159622020-04-10 17:43:51 +0000317 return GrSemaphoresSubmitted::kNo;
318 }
319 return GrSemaphoresSubmitted::kYes;
Greg Daniel51316782017-08-02 15:10:09 +0000320}
321
Greg Daniel04283f32020-05-20 13:16:00 -0400322bool GrContext::submit(bool syncCpu) {
323 ASSERT_SINGLE_OWNER
324 if (this->abandoned()) {
325 return false;
326 }
327
328 if (!fGpu) {
329 return false;
330 }
331
332 return fGpu->submitToGpu(syncCpu);
Greg Danielda50cb82020-05-13 14:07:40 -0400333}
334
Greg Daniela870b462019-01-08 15:49:46 -0500335////////////////////////////////////////////////////////////////////////////////
336
Brian Salomonb0d8b762019-05-06 16:58:22 -0400337void GrContext::checkAsyncWorkCompletion() {
338 if (fGpu) {
339 fGpu->checkFinishProcs();
340 }
341}
342
343////////////////////////////////////////////////////////////////////////////////
344
Greg Daniela870b462019-01-08 15:49:46 -0500345void GrContext::storeVkPipelineCacheData() {
346 if (fGpu) {
347 fGpu->storeVkPipelineCacheData();
348 }
349}
350
351////////////////////////////////////////////////////////////////////////////////
352
Khushal3e7548c2018-05-23 15:45:01 -0700353bool GrContext::supportsDistanceFieldText() const {
Robert Phillipsbb606772019-02-04 17:50:57 -0500354 return this->caps()->shaderCaps()->supportsDistanceFieldText();
Khushal3e7548c2018-05-23 15:45:01 -0700355}
356
bsalomon37f9a262015-02-02 13:00:10 -0800357//////////////////////////////////////////////////////////////////////////////
358
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500359void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800360 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500361 if (maxResources) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400362 *maxResources = -1;
bsalomon37f9a262015-02-02 13:00:10 -0800363 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500364 if (maxResourceBytes) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400365 *maxResourceBytes = this->getResourceCacheLimit();
bsalomon37f9a262015-02-02 13:00:10 -0800366 }
367}
368
Robert Phillipscf39f372019-09-03 10:29:20 -0400369size_t GrContext::getResourceCacheLimit() const {
joshualitt1de610a2016-01-06 08:26:09 -0800370 ASSERT_SINGLE_OWNER
Robert Phillipscf39f372019-09-03 10:29:20 -0400371 return fResourceCache->getMaxResourceBytes();
372}
373
374void GrContext::setResourceCacheLimits(int unused, size_t maxResourceBytes) {
375 ASSERT_SINGLE_OWNER
376 this->setResourceCacheLimit(maxResourceBytes);
377}
378
379void GrContext::setResourceCacheLimit(size_t maxResourceBytes) {
380 ASSERT_SINGLE_OWNER
381 fResourceCache->setLimit(maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800382}
383
ericrk0a5fa482015-09-15 14:16:10 -0700384//////////////////////////////////////////////////////////////////////////////
ericrk0a5fa482015-09-15 14:16:10 -0700385void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800386 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700387 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
Khushal71652e22018-10-29 13:05:36 -0700388 traceMemoryDump->dumpNumericValue("skia/gr_text_blob_cache", "size", "bytes",
Robert Phillips2184fb72019-02-21 16:11:41 -0500389 this->getTextBlobCache()->usedBytes());
ericrk0a5fa482015-09-15 14:16:10 -0700390}
Brian Osman71a18892017-08-10 10:23:25 -0400391
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400392//////////////////////////////////////////////////////////////////////////////
393GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400394 const GrBackendFormat& backendFormat,
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400395 GrMipMapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400396 GrRenderable renderable,
397 GrProtected isProtected) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400398 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
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 Phillips4277f012020-01-21 14:28:34 -0500407 return fGpu->createBackendTexture({width, height}, backendFormat, renderable,
Greg Daniel16032b32020-05-06 15:31:10 -0400408 mipMapped, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400409}
410
411GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400412 SkColorType skColorType,
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400413 GrMipMapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400414 GrRenderable renderable,
415 GrProtected isProtected) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400416 if (!this->asDirectContext()) {
417 return GrBackendTexture();
418 }
419
420 if (this->abandoned()) {
421 return GrBackendTexture();
422 }
423
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400424 const GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400425
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400426 return this->createBackendTexture(width, height, format, mipMapped, renderable, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400427}
428
Robert Phillips02dc0302019-07-02 17:58:27 -0400429GrBackendTexture GrContext::createBackendTexture(const SkSurfaceCharacterization& c) {
Robert Phillips02dc0302019-07-02 17:58:27 -0400430 if (!this->asDirectContext() || !c.isValid()) {
431 return GrBackendTexture();
432 }
433
434 if (this->abandoned()) {
435 return GrBackendTexture();
436 }
437
438 if (c.usesGLFBO0()) {
439 // If we are making the surface we will never use FBO0.
440 return GrBackendTexture();
441 }
442
443 if (c.vulkanSecondaryCBCompatible()) {
444 return {};
445 }
446
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400447 const GrBackendFormat format = this->defaultBackendFormat(c.colorType(), GrRenderable::kYes);
Robert Phillips02dc0302019-07-02 17:58:27 -0400448 if (!format.isValid()) {
449 return GrBackendTexture();
450 }
451
Robert Phillips02dc0302019-07-02 17:58:27 -0400452 GrBackendTexture result = this->createBackendTexture(c.width(), c.height(), format,
453 GrMipMapped(c.isMipMapped()),
454 GrRenderable::kYes,
Robert Phillips3cd54322019-07-10 09:28:59 -0400455 c.isProtected());
Robert Phillips02dc0302019-07-02 17:58:27 -0400456 SkASSERT(c.isCompatible(result));
457 return result;
458}
459
Greg Daniel25597782020-06-11 13:15:08 -0400460static GrBackendTexture create_and_update_backend_texture(
461 GrContext* context,
462 SkISize dimensions,
463 const GrBackendFormat& backendFormat,
464 GrMipMapped mipMapped,
465 GrRenderable renderable,
466 GrProtected isProtected,
467 sk_sp<GrRefCntedCallback> finishedCallback,
468 const GrGpu::BackendTextureData* data) {
Greg Daniel16032b32020-05-06 15:31:10 -0400469 GrGpu* gpu = context->priv().getGpu();
470
471 GrBackendTexture beTex = gpu->createBackendTexture(dimensions, backendFormat, renderable,
472 mipMapped, isProtected);
473 if (!beTex.isValid()) {
474 return {};
475 }
476
Greg Daniel25597782020-06-11 13:15:08 -0400477 if (!context->priv().getGpu()->updateBackendTexture(beTex, std::move(finishedCallback), data)) {
Greg Daniel16032b32020-05-06 15:31:10 -0400478 context->deleteBackendTexture(beTex);
479 return {};
480 }
481 return beTex;
482}
483
Greg Daniel25597782020-06-11 13:15:08 -0400484
485GrBackendTexture GrContext::createBackendTexture(const SkSurfaceCharacterization& c,
486 const SkColor4f& color,
487 GrGpuFinishedProc finishedProc,
488 GrGpuFinishedContext finishedContext) {
489 sk_sp<GrRefCntedCallback> finishedCallback;
490 if (finishedProc) {
491 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
492 }
493
494 if (!this->asDirectContext() || !c.isValid()) {
495 return {};
496 }
497
498 if (this->abandoned()) {
499 return {};
500 }
501
502 if (c.usesGLFBO0()) {
503 // If we are making the surface we will never use FBO0.
504 return {};
505 }
506
507 if (c.vulkanSecondaryCBCompatible()) {
508 return {};
509 }
510
511 const GrBackendFormat format = this->defaultBackendFormat(c.colorType(), GrRenderable::kYes);
512 if (!format.isValid()) {
513 return {};
514 }
515
516 GrGpu::BackendTextureData data(color);
517 GrBackendTexture result = create_and_update_backend_texture(
518 this, {c.width(), c.height()}, format, GrMipMapped(c.isMipMapped()), GrRenderable::kYes,
519 c.isProtected(), std::move(finishedCallback), &data);
520
521 SkASSERT(c.isCompatible(result));
522 return result;
523}
524
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400525GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400526 const GrBackendFormat& backendFormat,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400527 const SkColor4f& color,
528 GrMipMapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400529 GrRenderable renderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400530 GrProtected isProtected,
531 GrGpuFinishedProc finishedProc,
532 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400533 sk_sp<GrRefCntedCallback> finishedCallback;
534 if (finishedProc) {
535 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
536 }
537
Brian Salomonc42eb662019-06-24 17:13:00 -0400538 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400539 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400540 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400541 }
542
543 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400544 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400545 }
546
Brian Salomon85c3d682019-11-04 15:04:54 -0500547 GrGpu::BackendTextureData data(color);
Greg Daniel16032b32020-05-06 15:31:10 -0400548 return create_and_update_backend_texture(this, {width, height}, backendFormat, mipMapped,
Greg Daniel25597782020-06-11 13:15:08 -0400549 renderable, isProtected, std::move(finishedCallback),
Greg Daniel16032b32020-05-06 15:31:10 -0400550 &data);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400551}
552
553GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400554 SkColorType skColorType,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400555 const SkColor4f& color,
556 GrMipMapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400557 GrRenderable renderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400558 GrProtected isProtected,
559 GrGpuFinishedProc finishedProc,
560 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400561 sk_sp<GrRefCntedCallback> finishedCallback;
562 if (finishedProc) {
563 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
564 }
565
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400566 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400567 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400568 }
569
570 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400571 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400572 }
573
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400574 GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400575 if (!format.isValid()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400576 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400577 }
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400578
579 GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
Brian Salomon982f5462020-03-30 12:52:33 -0400580 SkColor4f swizzledColor = this->caps()->getWriteSwizzle(format, grColorType).applyTo(color);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400581
Greg Daniel25597782020-06-11 13:15:08 -0400582 GrGpu::BackendTextureData data(swizzledColor);
583 return create_and_update_backend_texture(this, {width, height}, format, mipMapped,
584 renderable, isProtected, std::move(finishedCallback),
585 &data);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400586}
587
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500588GrBackendTexture GrContext::createBackendTexture(const SkPixmap srcData[], int numProvidedLevels,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400589 GrRenderable renderable, GrProtected isProtected,
590 GrGpuFinishedProc finishedProc,
591 GrGpuFinishedContext finishedContext) {
Robert Phillips66944402019-09-30 13:21:25 -0400592 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
593
Greg Daniel25597782020-06-11 13:15:08 -0400594 sk_sp<GrRefCntedCallback> finishedCallback;
595 if (finishedProc) {
596 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
597 }
598
Robert Phillips66944402019-09-30 13:21:25 -0400599 if (!this->asDirectContext()) {
600 return {};
601 }
602
603 if (this->abandoned()) {
604 return {};
605 }
606
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500607 if (!srcData || numProvidedLevels <= 0) {
Robert Phillips66944402019-09-30 13:21:25 -0400608 return {};
609 }
610
611 int baseWidth = srcData[0].width();
612 int baseHeight = srcData[0].height();
613 SkColorType colorType = srcData[0].colorType();
614
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500615 GrMipMapped mipMapped = GrMipMapped::kNo;
616 int numExpectedLevels = 1;
617 if (numProvidedLevels > 1) {
618 numExpectedLevels = SkMipMap::ComputeLevelCount(baseWidth, baseHeight) + 1;
619 mipMapped = GrMipMapped::kYes;
620 }
621
622 if (numProvidedLevels != numExpectedLevels) {
623 return {};
624 }
625
Robert Phillips66944402019-09-30 13:21:25 -0400626 GrBackendFormat backendFormat = this->defaultBackendFormat(colorType, renderable);
627
Brian Salomon85c3d682019-11-04 15:04:54 -0500628 GrGpu::BackendTextureData data(srcData);
Greg Daniel16032b32020-05-06 15:31:10 -0400629 return create_and_update_backend_texture(this, {baseWidth, baseHeight}, backendFormat,
Greg Daniel25597782020-06-11 13:15:08 -0400630 mipMapped, renderable, isProtected,
631 std::move(finishedCallback), &data);
Robert Phillips66944402019-09-30 13:21:25 -0400632}
633
Greg Danielb2365d82020-05-13 15:32:04 -0400634bool GrContext::updateBackendTexture(const GrBackendTexture& backendTexture,
635 const SkColor4f& color,
636 GrGpuFinishedProc finishedProc,
637 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400638 sk_sp<GrRefCntedCallback> finishedCallback;
639 if (finishedProc) {
640 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
641 }
642
Greg Danielb2365d82020-05-13 15:32:04 -0400643 if (!this->asDirectContext()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400644 return false;
645 }
646
647 if (this->abandoned()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400648 return false;
649 }
650
651 GrGpu::BackendTextureData data(color);
Greg Daniel25597782020-06-11 13:15:08 -0400652 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
Greg Danielb2365d82020-05-13 15:32:04 -0400653}
654
655bool GrContext::updateBackendTexture(const GrBackendTexture& backendTexture,
656 const SkPixmap srcData[],
657 int numLevels,
658 GrGpuFinishedProc finishedProc,
659 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400660 sk_sp<GrRefCntedCallback> finishedCallback;
661 if (finishedProc) {
662 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
663 }
664
Greg Danielb2365d82020-05-13 15:32:04 -0400665 if (!this->asDirectContext()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400666 return false;
667 }
668
669 if (this->abandoned()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400670 return false;
671 }
672
673 if (!srcData || numLevels <= 0) {
Greg Danielb2365d82020-05-13 15:32:04 -0400674 return false;
675 }
676
677 int numExpectedLevels = 1;
678 if (backendTexture.hasMipMaps()) {
679 numExpectedLevels = SkMipMap::ComputeLevelCount(backendTexture.width(),
680 backendTexture.height()) + 1;
681 }
682 if (numLevels != numExpectedLevels) {
Greg Danielb2365d82020-05-13 15:32:04 -0400683 return false;
684 }
685
686 GrGpu::BackendTextureData data(srcData);
Greg Daniel25597782020-06-11 13:15:08 -0400687 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
Greg Danielb2365d82020-05-13 15:32:04 -0400688}
689
Robert Phillipsb915c942019-12-17 14:44:37 -0500690//////////////////////////////////////////////////////////////////////////////
691
692GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
693 const GrBackendFormat& backendFormat,
694 const SkColor4f& color,
695 GrMipMapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400696 GrProtected isProtected,
697 GrGpuFinishedProc finishedProc,
698 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500699 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel25597782020-06-11 13:15:08 -0400700 sk_sp<GrRefCntedCallback> finishedCallback;
701 if (finishedProc) {
702 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
703 }
704
Robert Phillipsb915c942019-12-17 14:44:37 -0500705 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400706 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500707 }
708
709 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400710 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500711 }
712
713 GrGpu::BackendTextureData data(color);
Robert Phillips4277f012020-01-21 14:28:34 -0500714 return fGpu->createCompressedBackendTexture({width, height}, backendFormat,
Greg Daniel25597782020-06-11 13:15:08 -0400715 mipMapped, isProtected, std::move(finishedCallback),
716 &data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500717}
718
719GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
720 SkImage::CompressionType compression,
721 const SkColor4f& color,
722 GrMipMapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400723 GrProtected isProtected,
724 GrGpuFinishedProc finishedProc,
725 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500726 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillipsb915c942019-12-17 14:44:37 -0500727 GrBackendFormat format = this->compressedBackendFormat(compression);
728 return this->createCompressedBackendTexture(width, height, format, color,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400729 mipMapped, isProtected, finishedProc,
730 finishedContext);
Robert Phillipsb915c942019-12-17 14:44:37 -0500731}
732
733GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
734 const GrBackendFormat& backendFormat,
735 const void* compressedData,
736 size_t dataSize,
737 GrMipMapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400738 GrProtected isProtected,
739 GrGpuFinishedProc finishedProc,
740 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500741 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel25597782020-06-11 13:15:08 -0400742 sk_sp<GrRefCntedCallback> finishedCallback;
743 if (finishedProc) {
744 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
745 }
746
Robert Phillipsb915c942019-12-17 14:44:37 -0500747 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400748 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500749 }
750
751 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400752 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500753 }
754
755 GrGpu::BackendTextureData data(compressedData, dataSize);
Robert Phillips4277f012020-01-21 14:28:34 -0500756 return fGpu->createCompressedBackendTexture({width, height}, backendFormat,
Greg Daniel25597782020-06-11 13:15:08 -0400757 mipMapped, isProtected, std::move(finishedCallback),
758 &data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500759}
760
761GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
762 SkImage::CompressionType compression,
763 const void* data, size_t dataSize,
764 GrMipMapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400765 GrProtected isProtected,
766 GrGpuFinishedProc finishedProc,
767 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500768 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillipsb915c942019-12-17 14:44:37 -0500769 GrBackendFormat format = this->compressedBackendFormat(compression);
Greg Daniel25597782020-06-11 13:15:08 -0400770 return this->createCompressedBackendTexture(width, height, format, data, dataSize, mipMapped,
771 isProtected, finishedProc, finishedContext);
Robert Phillipsb915c942019-12-17 14:44:37 -0500772}
773
Greg Daniel1db8e792020-06-09 17:29:32 -0400774bool GrContext::setBackendTextureState(const GrBackendTexture& backendTexture,
775 const GrBackendSurfaceMutableState& state,
776 GrGpuFinishedProc finishedProc,
777 GrGpuFinishedContext finishedContext) {
Greg Daniel1db8e792020-06-09 17:29:32 -0400778 sk_sp<GrRefCntedCallback> callback;
779 if (finishedProc) {
780 callback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
781 }
Greg Daniel25597782020-06-11 13:15:08 -0400782
783 if (!this->asDirectContext()) {
784 return false;
785 }
786
787 if (this->abandoned()) {
788 return false;
789 }
790
Greg Daniel1db8e792020-06-09 17:29:32 -0400791 return fGpu->setBackendTextureState(backendTexture, state, std::move(callback));
792}
793
794bool GrContext::setBackendRenderTargetState(const GrBackendRenderTarget& backendRenderTarget,
795 const GrBackendSurfaceMutableState& state,
796 GrGpuFinishedProc finishedProc,
797 GrGpuFinishedContext finishedContext) {
Greg Daniel1db8e792020-06-09 17:29:32 -0400798 sk_sp<GrRefCntedCallback> callback;
799 if (finishedProc) {
800 callback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
801 }
Greg Daniel25597782020-06-11 13:15:08 -0400802
803 if (!this->asDirectContext()) {
804 return false;
805 }
806
807 if (this->abandoned()) {
808 return false;
809 }
810
Greg Daniel1db8e792020-06-09 17:29:32 -0400811 return fGpu->setBackendRenderTargetState(backendRenderTarget, state, std::move(callback));
812}
813
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400814void GrContext::deleteBackendTexture(GrBackendTexture backendTex) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400815 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danielf0e04f02019-12-04 15:17:54 -0500816 // For the Vulkan backend we still must destroy the backend texture when the context is
817 // abandoned.
818 if ((this->abandoned() && this->backend() != GrBackendApi::kVulkan) || !backendTex.isValid()) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400819 return;
820 }
821
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400822 fGpu->deleteBackendTexture(backendTex);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400823}
824
Greg Daniel1db8e792020-06-09 17:29:32 -0400825//////////////////////////////////////////////////////////////////////////////
826
Brian Osmaned58e002019-09-06 14:42:43 -0400827bool GrContext::precompileShader(const SkData& key, const SkData& data) {
828 return fGpu->precompileShader(key, data);
829}
830
Brian Salomonec22b1a2019-08-09 09:41:48 -0400831#ifdef SK_ENABLE_DUMP_GPU
Michael Ludwigdd205452020-03-30 17:16:34 -0400832#include "include/core/SkString.h"
Brian Salomonec22b1a2019-08-09 09:41:48 -0400833#include "src/utils/SkJSONWriter.h"
834SkString GrContext::dump() const {
835 SkDynamicMemoryWStream stream;
836 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
837 writer.beginObject();
838
839 writer.appendString("backend", GrBackendApiToStr(this->backend()));
840
841 writer.appendName("caps");
842 this->caps()->dumpJSON(&writer);
843
844 writer.appendName("gpu");
845 this->fGpu->dumpJSON(&writer);
846
Robert Phillips273f1072020-05-05 13:03:07 -0400847 writer.appendName("context");
848 this->dumpJSON(&writer);
849
Brian Salomonec22b1a2019-08-09 09:41:48 -0400850 // Flush JSON to the memory stream
851 writer.endObject();
852 writer.flush();
853
854 // Null terminate the JSON data in the memory stream
855 stream.write8(0);
856
857 // Allocate a string big enough to hold all the data, then copy out of the stream
858 SkString result(stream.bytesWritten());
859 stream.copyToAndReset(result.writable_str());
860 return result;
861}
862#endif