blob: 7b6837e08bede3f0485645998eb4cb39a0f3876b [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
Robert Phillips4e105e22020-07-16 09:18:50 -04008#include "include/gpu/GrDirectContext.h"
Brian Salomon9241a6d2019-10-03 13:26:54 -04009
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"
Mike Reed13711eb2020-07-14 17:16:32 -040014#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>
John Stilesfbd050b2020-08-03 13:21:46 -040039#include <memory>
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)
Adlai Holler33dbd652020-06-01 12:35:42 -040045#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner())
Robert Phillipsa9162df2019-02-11 14:12:03 -050046#define RETURN_IF_ABANDONED if (this->abandoned()) { return; }
47#define RETURN_FALSE_IF_ABANDONED if (this->abandoned()) { return false; }
48#define RETURN_NULL_IF_ABANDONED if (this->abandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000049
robertphillipsea461502015-05-26 11:38:03 -070050////////////////////////////////////////////////////////////////////////////////
51
Adlai Hollere219d1c2020-06-02 11:23:16 -040052GrContext::GrContext(sk_sp<GrContextThreadSafeProxy> proxy) : INHERITED(std::move(proxy)) {
halcanary96fcdcc2015-08-27 07:41:13 -070053 fResourceCache = nullptr;
54 fResourceProvider = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000055}
56
Robert Phillips292a6b22019-02-14 14:49:02 -050057GrContext::~GrContext() {
58 ASSERT_SINGLE_OWNER
59
Adlai Holler96ead542020-06-26 08:50:14 -040060 this->destroyDrawingManager();
Greg Daniel5ed3c112020-06-18 15:59:17 -040061 fMappedBufferManager.reset();
Robert Phillips292a6b22019-02-14 14:49:02 -050062 delete fResourceProvider;
63 delete fResourceCache;
Robert Phillips292a6b22019-02-14 14:49:02 -050064}
65
Adlai Hollere219d1c2020-06-02 11:23:16 -040066bool GrContext::init() {
Greg Danielb76a72a2017-07-13 15:07:54 -040067 ASSERT_SINGLE_OWNER
Robert Phillipsa41c6852019-02-07 10:44:10 -050068 SkASSERT(this->proxyProvider());
Robert Phillips88260b52018-01-19 12:56:09 -050069
Adlai Hollere219d1c2020-06-02 11:23:16 -040070 if (!INHERITED::init()) {
Robert Phillipsbb606772019-02-04 17:50:57 -050071 return false;
72 }
73
Robert Phillips2184fb72019-02-21 16:11:41 -050074 SkASSERT(this->getTextBlobCache());
Robert Phillipsbb606772019-02-04 17:50:57 -050075
Robert Phillips88260b52018-01-19 12:56:09 -050076 if (fGpu) {
John Stilesfbd050b2020-08-03 13:21:46 -040077 fStrikeCache = std::make_unique<GrStrikeCache>();
Robert Phillipsa41c6852019-02-07 10:44:10 -050078 fResourceCache = new GrResourceCache(this->caps(), this->singleOwner(), this->contextID());
Robert Phillips12c46292019-04-23 07:36:17 -040079 fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, this->singleOwner());
Mike Kleinf46d5ca2019-12-11 10:45:01 -050080 fMappedBufferManager = std::make_unique<GrClientMappedBufferManager>(this->contextID());
Robert Phillips88260b52018-01-19 12:56:09 -050081 }
82
Robert Phillips88260b52018-01-19 12:56:09 -050083 if (fResourceCache) {
Robert Phillipsa41c6852019-02-07 10:44:10 -050084 fResourceCache->setProxyProvider(this->proxyProvider());
Robert Phillips88260b52018-01-19 12:56:09 -050085 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050086
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000087 fDidTestPMConversions = false;
88
Robert Phillipsfde6fa02018-03-02 08:53:14 -050089 // DDL TODO: we need to think through how the task group & persistent cache
90 // get passed on to/shared between all the DDLRecorders created with this context.
Robert Phillipsc1541ae2019-02-04 12:05:37 -050091 if (this->options().fExecutor) {
Mike Kleinf46d5ca2019-12-11 10:45:01 -050092 fTaskGroup = std::make_unique<SkTaskGroup>(*this->options().fExecutor);
Brian Osman51279982017-08-23 10:12:00 -040093 }
94
Robert Phillipsc1541ae2019-02-04 12:05:37 -050095 fPersistentCache = this->options().fPersistentCache;
Brian Osman5e7fbfd2019-05-03 13:13:35 -040096 fShaderErrorHandler = this->options().fShaderErrorHandler;
97 if (!fShaderErrorHandler) {
98 fShaderErrorHandler = GrShaderUtils::DefaultShaderErrorHandler();
99 }
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400100
Brian Salomon91a3e522017-06-23 10:58:19 -0400101 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000102}
103
Robert Phillips4217ea72019-01-30 13:08:28 -0500104sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
Adlai Hollere219d1c2020-06-02 11:23:16 -0400105 return INHERITED::threadSafeProxy();
Robert Phillips4217ea72019-01-30 13:08:28 -0500106}
107
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400108//////////////////////////////////////////////////////////////////////////////
109
bsalomon2354f842014-07-28 13:48:36 -0700110void GrContext::abandonContext() {
Greg Daniel6e35a002020-04-01 13:29:59 -0400111 if (INHERITED::abandoned()) {
Robert Phillipsa9162df2019-02-11 14:12:03 -0500112 return;
113 }
joshualitt1de610a2016-01-06 08:26:09 -0800114
Robert Phillipsa9162df2019-02-11 14:12:03 -0500115 INHERITED::abandonContext();
116
Robert Phillips4d932d12020-04-09 08:58:52 -0400117 fStrikeCache->freeAll();
118
Brian Salomon9241a6d2019-10-03 13:26:54 -0400119 fMappedBufferManager->abandon();
120
bsalomond309e7a2015-04-30 14:18:54 -0700121 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800122
bsalomon@google.com205d4602011-04-25 12:43:45 +0000123 // abandon first to so destructors
124 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800125 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700126
bsalomon6e2aad42016-04-01 11:54:31 -0700127 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400128
129 fMappedBufferManager.reset();
Khushalc421ca12018-06-26 14:38:34 -0700130}
131
bsalomon6e2aad42016-04-01 11:54:31 -0700132void GrContext::releaseResourcesAndAbandonContext() {
Greg Daniel6e35a002020-04-01 13:29:59 -0400133 if (INHERITED::abandoned()) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500134 return;
135 }
Robert Phillipsa9162df2019-02-11 14:12:03 -0500136
137 INHERITED::abandonContext();
138
Brian Salomon9241a6d2019-10-03 13:26:54 -0400139 fMappedBufferManager.reset();
140
bsalomon6e2aad42016-04-01 11:54:31 -0700141 fResourceProvider->abandon();
142
bsalomon6e2aad42016-04-01 11:54:31 -0700143 // Release all resources in the backend 3D API.
144 fResourceCache->releaseAll();
145
146 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000147}
148
Greg Daniel6e35a002020-04-01 13:29:59 -0400149bool GrContext::abandoned() {
150 if (INHERITED::abandoned()) {
151 return true;
152 }
153
154 if (fGpu && fGpu->isDeviceLost()) {
155 this->abandonContext();
156 return true;
157 }
158 return false;
159}
160
Brian Salomon24069eb2020-06-24 10:19:52 -0400161bool GrContext::oomed() { return fGpu ? fGpu->checkAndResetOOMed() : false; }
162
Brian Salomon1f05d452019-02-08 12:33:08 -0500163void GrContext::resetGLTextureBindings() {
164 if (this->abandoned() || this->backend() != GrBackendApi::kOpenGL) {
165 return;
166 }
167 fGpu->resetTextureBindings();
168}
169
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000170void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800171 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000172 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000173}
174
175void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800176 ASSERT_SINGLE_OWNER
177
Adlai Holler9d8a41c2020-06-25 14:01:58 +0000178 if (this->abandoned()) {
179 return;
180 }
181
Robert Phillips2184fb72019-02-21 16:11:41 -0500182 // TODO: the glyph cache doesn't hold any GpuResources so this call should not be needed here.
183 // Some slack in the GrTextBlob's implementation requires it though. That could be fixed.
Robert Phillips4d932d12020-04-09 08:58:52 -0400184 fStrikeCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700185
Robert Phillips6a6de562019-02-15 15:19:15 -0500186 this->drawingManager()->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700187
188 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000189}
190
Robert Phillips6eba0632018-03-28 12:25:42 -0400191void GrContext::purgeUnlockedResources(bool scratchResourcesOnly) {
192 ASSERT_SINGLE_OWNER
Robert Phillipsddc21482019-10-16 14:30:09 -0400193
194 if (this->abandoned()) {
195 return;
196 }
197
Robert Phillips6eba0632018-03-28 12:25:42 -0400198 fResourceCache->purgeUnlockedResources(scratchResourcesOnly);
199 fResourceCache->purgeAsNeeded();
Robert Phillips2184fb72019-02-21 16:11:41 -0500200
201 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
202 // place to purge stale blobs
203 this->getTextBlobCache()->purgeStaleBlobs();
Robert Phillips6eba0632018-03-28 12:25:42 -0400204}
205
Jim Van Verth76d917c2017-12-13 09:26:37 -0500206void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Yuqian Li40aa85f2019-07-02 13:45:00 -0700207 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
208
Brian Salomon5e150852017-03-22 14:53:13 -0400209 ASSERT_SINGLE_OWNER
Chris Dalton6c3879d2018-11-01 11:13:19 -0600210
Brian Salomon9241a6d2019-10-03 13:26:54 -0400211 if (this->abandoned()) {
212 return;
213 }
214
Jim Van Verthe0da3792020-08-14 16:50:19 -0400215 this->checkAsyncWorkCompletion();
Brian Salomon9241a6d2019-10-03 13:26:54 -0400216 fMappedBufferManager->process();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600217 auto purgeTime = GrStdSteadyClock::now() - msNotUsed;
218
Jim Van Verth76d917c2017-12-13 09:26:37 -0500219 fResourceCache->purgeAsNeeded();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600220 fResourceCache->purgeResourcesNotUsedSince(purgeTime);
221
Robert Phillips6a6de562019-02-15 15:19:15 -0500222 if (auto ccpr = this->drawingManager()->getCoverageCountingPathRenderer()) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500223 ccpr->purgeCacheEntriesOlderThan(this->proxyProvider(), purgeTime);
Chris Dalton6c3879d2018-11-01 11:13:19 -0600224 }
Jim Van Verth76d917c2017-12-13 09:26:37 -0500225
Robert Phillips2184fb72019-02-21 16:11:41 -0500226 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
227 // place to purge stale blobs
228 this->getTextBlobCache()->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400229}
230
Derek Sollenberger5480a182017-05-25 16:43:59 -0400231void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
232 ASSERT_SINGLE_OWNER
Robert Phillipsddc21482019-10-16 14:30:09 -0400233
234 if (this->abandoned()) {
235 return;
236 }
237
Derek Sollenberger5480a182017-05-25 16:43:59 -0400238 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
239}
240
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000241void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800242 ASSERT_SINGLE_OWNER
243
bsalomon71cb0c22014-11-14 12:10:14 -0800244 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800245 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800246 }
247 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800248 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800249 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000250}
251
Derek Sollenbergeree479142017-05-24 11:41:33 -0400252size_t GrContext::getResourceCachePurgeableBytes() const {
253 ASSERT_SINGLE_OWNER
254 return fResourceCache->getPurgeableBytes();
255}
256
Brian Salomon7e67dca2020-07-21 09:27:25 -0400257size_t GrContext::ComputeImageSize(sk_sp<SkImage> image, GrMipmapped mipMapped, bool useNextPow2) {
Greg Daniel8b666172019-10-09 12:38:22 -0400258 if (!image->isTextureBacked()) {
259 return 0;
260 }
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400261 SkImage_GpuBase* gpuImage = static_cast<SkImage_GpuBase*>(as_IB(image.get()));
262 GrTextureProxy* proxy = gpuImage->peekProxy();
263 if (!proxy) {
264 return 0;
265 }
266
267 const GrCaps& caps = *gpuImage->context()->priv().caps();
Greg Daniel8b666172019-10-09 12:38:22 -0400268 int colorSamplesPerPixel = 1;
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400269 return GrSurface::ComputeSize(caps, proxy->backendFormat(), image->dimensions(),
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400270 colorSamplesPerPixel, mipMapped, useNextPow2);
Greg Daniel8b666172019-10-09 12:38:22 -0400271}
272
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000273////////////////////////////////////////////////////////////////////////////////
274
Robert Phillipsbb606772019-02-04 17:50:57 -0500275int GrContext::maxTextureSize() const { return this->caps()->maxTextureSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400276
Robert Phillipsbb606772019-02-04 17:50:57 -0500277int GrContext::maxRenderTargetSize() const { return this->caps()->maxRenderTargetSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400278
Brian Salomonbdecacf2018-02-02 20:32:49 -0500279bool GrContext::colorTypeSupportedAsImage(SkColorType colorType) const {
Greg Daniel7bfc9132019-08-14 14:23:53 -0400280 GrBackendFormat format =
281 this->caps()->getDefaultBackendFormat(SkColorTypeToGrColorType(colorType),
282 GrRenderable::kNo);
283 return format.isValid();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500284}
285
Brian Salomonbdecacf2018-02-02 20:32:49 -0500286////////////////////////////////////////////////////////////////////////////////
287
Greg Daniel414418d2020-07-08 11:44:25 -0400288bool GrContext::wait(int numSemaphores, const GrBackendSemaphore waitSemaphores[],
289 bool deleteSemaphoresAfterWait) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400290 if (!fGpu || fGpu->caps()->semaphoreSupport()) {
Greg Daniel06be0792019-04-22 15:53:23 -0400291 return false;
292 }
Greg Daniel414418d2020-07-08 11:44:25 -0400293 GrWrapOwnership ownership =
294 deleteSemaphoresAfterWait ? kAdopt_GrWrapOwnership : kBorrow_GrWrapOwnership;
Greg Daniel06be0792019-04-22 15:53:23 -0400295 for (int i = 0; i < numSemaphores; ++i) {
Greg Daniel301015c2019-11-18 14:06:46 -0500296 std::unique_ptr<GrSemaphore> sema = fResourceProvider->wrapBackendSemaphore(
Greg Daniel414418d2020-07-08 11:44:25 -0400297 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait, ownership);
Greg Daniel0106fcc2020-07-01 17:40:12 -0400298 // If we failed to wrap the semaphore it means the client didn't give us a valid semaphore
299 // to begin with. Therefore, it is fine to not wait on it.
300 if (sema) {
301 fGpu->waitSemaphore(sema.get());
302 }
Greg Daniel06be0792019-04-22 15:53:23 -0400303 }
304 return true;
305}
306
307////////////////////////////////////////////////////////////////////////////////
308
Greg Daniele8d3cca2020-06-10 10:04:48 -0400309GrSemaphoresSubmitted GrContext::flush(const GrFlushInfo& info) {
Greg Daniel51316782017-08-02 15:10:09 +0000310 ASSERT_SINGLE_OWNER
Robert Phillipsa9162df2019-02-11 14:12:03 -0500311 if (this->abandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400312 if (info.fFinishedProc) {
313 info.fFinishedProc(info.fFinishedContext);
314 }
315 if (info.fSubmittedProc) {
316 info.fSubmittedProc(info.fSubmittedContext, false);
317 }
Robert Phillipsa9162df2019-02-11 14:12:03 -0500318 return GrSemaphoresSubmitted::kNo;
319 }
Greg Daniel51316782017-08-02 15:10:09 +0000320
Greg Daniel04283f32020-05-20 13:16:00 -0400321 bool flushed = this->drawingManager()->flush(
Greg Daniel9efe3862020-06-11 11:51:06 -0400322 nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, info, nullptr);
Greg Danielfe159622020-04-10 17:43:51 +0000323
Greg Daniel04283f32020-05-20 13:16:00 -0400324 if (!flushed || (!this->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
Greg Danielfe159622020-04-10 17:43:51 +0000325 return GrSemaphoresSubmitted::kNo;
326 }
327 return GrSemaphoresSubmitted::kYes;
Greg Daniel51316782017-08-02 15:10:09 +0000328}
329
Greg Daniel04283f32020-05-20 13:16:00 -0400330bool GrContext::submit(bool syncCpu) {
331 ASSERT_SINGLE_OWNER
332 if (this->abandoned()) {
333 return false;
334 }
335
336 if (!fGpu) {
337 return false;
338 }
339
340 return fGpu->submitToGpu(syncCpu);
Greg Danielda50cb82020-05-13 14:07:40 -0400341}
342
Greg Daniela870b462019-01-08 15:49:46 -0500343////////////////////////////////////////////////////////////////////////////////
344
Brian Salomonb0d8b762019-05-06 16:58:22 -0400345void GrContext::checkAsyncWorkCompletion() {
346 if (fGpu) {
347 fGpu->checkFinishProcs();
348 }
349}
350
351////////////////////////////////////////////////////////////////////////////////
352
Greg Daniela870b462019-01-08 15:49:46 -0500353void GrContext::storeVkPipelineCacheData() {
354 if (fGpu) {
355 fGpu->storeVkPipelineCacheData();
356 }
357}
358
359////////////////////////////////////////////////////////////////////////////////
360
Khushal3e7548c2018-05-23 15:45:01 -0700361bool GrContext::supportsDistanceFieldText() const {
Robert Phillipsbb606772019-02-04 17:50:57 -0500362 return this->caps()->shaderCaps()->supportsDistanceFieldText();
Khushal3e7548c2018-05-23 15:45:01 -0700363}
364
bsalomon37f9a262015-02-02 13:00:10 -0800365//////////////////////////////////////////////////////////////////////////////
366
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500367void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800368 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500369 if (maxResources) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400370 *maxResources = -1;
bsalomon37f9a262015-02-02 13:00:10 -0800371 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500372 if (maxResourceBytes) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400373 *maxResourceBytes = this->getResourceCacheLimit();
bsalomon37f9a262015-02-02 13:00:10 -0800374 }
375}
376
Robert Phillipscf39f372019-09-03 10:29:20 -0400377size_t GrContext::getResourceCacheLimit() const {
joshualitt1de610a2016-01-06 08:26:09 -0800378 ASSERT_SINGLE_OWNER
Robert Phillipscf39f372019-09-03 10:29:20 -0400379 return fResourceCache->getMaxResourceBytes();
380}
381
382void GrContext::setResourceCacheLimits(int unused, size_t maxResourceBytes) {
383 ASSERT_SINGLE_OWNER
384 this->setResourceCacheLimit(maxResourceBytes);
385}
386
387void GrContext::setResourceCacheLimit(size_t maxResourceBytes) {
388 ASSERT_SINGLE_OWNER
389 fResourceCache->setLimit(maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800390}
391
ericrk0a5fa482015-09-15 14:16:10 -0700392//////////////////////////////////////////////////////////////////////////////
ericrk0a5fa482015-09-15 14:16:10 -0700393void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800394 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700395 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
Khushal71652e22018-10-29 13:05:36 -0700396 traceMemoryDump->dumpNumericValue("skia/gr_text_blob_cache", "size", "bytes",
Robert Phillips2184fb72019-02-21 16:11:41 -0500397 this->getTextBlobCache()->usedBytes());
ericrk0a5fa482015-09-15 14:16:10 -0700398}
Brian Osman71a18892017-08-10 10:23:25 -0400399
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400400//////////////////////////////////////////////////////////////////////////////
401GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400402 const GrBackendFormat& backendFormat,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400403 GrMipmapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400404 GrRenderable renderable,
405 GrProtected isProtected) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400406 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400407 if (!this->asDirectContext()) {
408 return GrBackendTexture();
409 }
410
411 if (this->abandoned()) {
412 return GrBackendTexture();
413 }
414
Robert Phillips4277f012020-01-21 14:28:34 -0500415 return fGpu->createBackendTexture({width, height}, backendFormat, renderable,
Greg Daniel16032b32020-05-06 15:31:10 -0400416 mipMapped, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400417}
418
419GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400420 SkColorType skColorType,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400421 GrMipmapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400422 GrRenderable renderable,
423 GrProtected isProtected) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400424 if (!this->asDirectContext()) {
425 return GrBackendTexture();
426 }
427
428 if (this->abandoned()) {
429 return GrBackendTexture();
430 }
431
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400432 const GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400433
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400434 return this->createBackendTexture(width, height, format, mipMapped, renderable, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400435}
436
Robert Phillips02dc0302019-07-02 17:58:27 -0400437GrBackendTexture GrContext::createBackendTexture(const SkSurfaceCharacterization& c) {
Robert Phillips02dc0302019-07-02 17:58:27 -0400438 if (!this->asDirectContext() || !c.isValid()) {
439 return GrBackendTexture();
440 }
441
442 if (this->abandoned()) {
443 return GrBackendTexture();
444 }
445
446 if (c.usesGLFBO0()) {
447 // If we are making the surface we will never use FBO0.
448 return GrBackendTexture();
449 }
450
451 if (c.vulkanSecondaryCBCompatible()) {
452 return {};
453 }
454
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400455 const GrBackendFormat format = this->defaultBackendFormat(c.colorType(), GrRenderable::kYes);
Robert Phillips02dc0302019-07-02 17:58:27 -0400456 if (!format.isValid()) {
457 return GrBackendTexture();
458 }
459
Robert Phillips02dc0302019-07-02 17:58:27 -0400460 GrBackendTexture result = this->createBackendTexture(c.width(), c.height(), format,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400461 GrMipmapped(c.isMipMapped()),
Robert Phillips02dc0302019-07-02 17:58:27 -0400462 GrRenderable::kYes,
Robert Phillips3cd54322019-07-10 09:28:59 -0400463 c.isProtected());
Robert Phillips02dc0302019-07-02 17:58:27 -0400464 SkASSERT(c.isCompatible(result));
465 return result;
466}
467
Greg Daniel25597782020-06-11 13:15:08 -0400468static GrBackendTexture create_and_update_backend_texture(
Robert Phillips4e105e22020-07-16 09:18:50 -0400469 GrDirectContext* context,
Greg Daniel25597782020-06-11 13:15:08 -0400470 SkISize dimensions,
471 const GrBackendFormat& backendFormat,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400472 GrMipmapped mipMapped,
Greg Daniel25597782020-06-11 13:15:08 -0400473 GrRenderable renderable,
474 GrProtected isProtected,
475 sk_sp<GrRefCntedCallback> finishedCallback,
476 const GrGpu::BackendTextureData* data) {
Greg Daniel16032b32020-05-06 15:31:10 -0400477 GrGpu* gpu = context->priv().getGpu();
478
479 GrBackendTexture beTex = gpu->createBackendTexture(dimensions, backendFormat, renderable,
480 mipMapped, isProtected);
481 if (!beTex.isValid()) {
482 return {};
483 }
484
Greg Daniel25597782020-06-11 13:15:08 -0400485 if (!context->priv().getGpu()->updateBackendTexture(beTex, std::move(finishedCallback), data)) {
Greg Daniel16032b32020-05-06 15:31:10 -0400486 context->deleteBackendTexture(beTex);
487 return {};
488 }
489 return beTex;
490}
491
Greg Daniel25597782020-06-11 13:15:08 -0400492
493GrBackendTexture GrContext::createBackendTexture(const SkSurfaceCharacterization& c,
494 const SkColor4f& color,
495 GrGpuFinishedProc finishedProc,
496 GrGpuFinishedContext finishedContext) {
497 sk_sp<GrRefCntedCallback> finishedCallback;
498 if (finishedProc) {
499 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
500 }
501
502 if (!this->asDirectContext() || !c.isValid()) {
503 return {};
504 }
505
506 if (this->abandoned()) {
507 return {};
508 }
509
510 if (c.usesGLFBO0()) {
511 // If we are making the surface we will never use FBO0.
512 return {};
513 }
514
515 if (c.vulkanSecondaryCBCompatible()) {
516 return {};
517 }
518
519 const GrBackendFormat format = this->defaultBackendFormat(c.colorType(), GrRenderable::kYes);
520 if (!format.isValid()) {
521 return {};
522 }
523
524 GrGpu::BackendTextureData data(color);
525 GrBackendTexture result = create_and_update_backend_texture(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400526 this->asDirectContext(), {c.width(), c.height()}, format, GrMipmapped(c.isMipMapped()),
Robert Phillips4e105e22020-07-16 09:18:50 -0400527 GrRenderable::kYes, c.isProtected(), std::move(finishedCallback), &data);
Greg Daniel25597782020-06-11 13:15:08 -0400528
529 SkASSERT(c.isCompatible(result));
530 return result;
531}
532
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400533GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400534 const GrBackendFormat& backendFormat,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400535 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400536 GrMipmapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400537 GrRenderable renderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400538 GrProtected isProtected,
539 GrGpuFinishedProc finishedProc,
540 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400541 sk_sp<GrRefCntedCallback> finishedCallback;
542 if (finishedProc) {
543 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
544 }
545
Brian Salomonc42eb662019-06-24 17:13:00 -0400546 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400547 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400548 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400549 }
550
551 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400552 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400553 }
554
Brian Salomon85c3d682019-11-04 15:04:54 -0500555 GrGpu::BackendTextureData data(color);
Robert Phillips4e105e22020-07-16 09:18:50 -0400556 return create_and_update_backend_texture(this->asDirectContext(), {width, height},
557 backendFormat, mipMapped, renderable, isProtected,
558 std::move(finishedCallback), &data);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400559}
560
561GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400562 SkColorType skColorType,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400563 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400564 GrMipmapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400565 GrRenderable renderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400566 GrProtected isProtected,
567 GrGpuFinishedProc finishedProc,
568 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400569 sk_sp<GrRefCntedCallback> finishedCallback;
570 if (finishedProc) {
571 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
572 }
573
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400574 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400575 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400576 }
577
578 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400579 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400580 }
581
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400582 GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400583 if (!format.isValid()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400584 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400585 }
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400586
587 GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
Brian Salomon982f5462020-03-30 12:52:33 -0400588 SkColor4f swizzledColor = this->caps()->getWriteSwizzle(format, grColorType).applyTo(color);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400589
Greg Daniel25597782020-06-11 13:15:08 -0400590 GrGpu::BackendTextureData data(swizzledColor);
Robert Phillips4e105e22020-07-16 09:18:50 -0400591 return create_and_update_backend_texture(this->asDirectContext(), {width, height}, format,
592 mipMapped, renderable, isProtected,
593 std::move(finishedCallback), &data);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400594}
595
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500596GrBackendTexture GrContext::createBackendTexture(const SkPixmap srcData[], int numProvidedLevels,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400597 GrRenderable renderable, GrProtected isProtected,
598 GrGpuFinishedProc finishedProc,
599 GrGpuFinishedContext finishedContext) {
Robert Phillips66944402019-09-30 13:21:25 -0400600 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
601
Greg Daniel25597782020-06-11 13:15:08 -0400602 sk_sp<GrRefCntedCallback> finishedCallback;
603 if (finishedProc) {
604 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
605 }
606
Robert Phillips66944402019-09-30 13:21:25 -0400607 if (!this->asDirectContext()) {
608 return {};
609 }
610
611 if (this->abandoned()) {
612 return {};
613 }
614
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500615 if (!srcData || numProvidedLevels <= 0) {
Robert Phillips66944402019-09-30 13:21:25 -0400616 return {};
617 }
618
619 int baseWidth = srcData[0].width();
620 int baseHeight = srcData[0].height();
621 SkColorType colorType = srcData[0].colorType();
622
Brian Salomon7e67dca2020-07-21 09:27:25 -0400623 GrMipmapped mipMapped = GrMipmapped::kNo;
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500624 int numExpectedLevels = 1;
625 if (numProvidedLevels > 1) {
Mike Reed13711eb2020-07-14 17:16:32 -0400626 numExpectedLevels = SkMipmap::ComputeLevelCount(baseWidth, baseHeight) + 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400627 mipMapped = GrMipmapped::kYes;
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500628 }
629
630 if (numProvidedLevels != numExpectedLevels) {
631 return {};
632 }
633
Robert Phillips66944402019-09-30 13:21:25 -0400634 GrBackendFormat backendFormat = this->defaultBackendFormat(colorType, renderable);
635
Brian Salomon85c3d682019-11-04 15:04:54 -0500636 GrGpu::BackendTextureData data(srcData);
Robert Phillips4e105e22020-07-16 09:18:50 -0400637 return create_and_update_backend_texture(this->asDirectContext(), {baseWidth, baseHeight},
638 backendFormat, mipMapped, renderable, isProtected,
Greg Daniel25597782020-06-11 13:15:08 -0400639 std::move(finishedCallback), &data);
Robert Phillips66944402019-09-30 13:21:25 -0400640}
641
Greg Danielb2365d82020-05-13 15:32:04 -0400642bool GrContext::updateBackendTexture(const GrBackendTexture& backendTexture,
643 const SkColor4f& color,
644 GrGpuFinishedProc finishedProc,
645 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400646 sk_sp<GrRefCntedCallback> finishedCallback;
647 if (finishedProc) {
648 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
649 }
650
Greg Danielb2365d82020-05-13 15:32:04 -0400651 if (!this->asDirectContext()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400652 return false;
653 }
654
655 if (this->abandoned()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400656 return false;
657 }
658
659 GrGpu::BackendTextureData data(color);
Greg Daniel25597782020-06-11 13:15:08 -0400660 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
Greg Danielb2365d82020-05-13 15:32:04 -0400661}
662
663bool GrContext::updateBackendTexture(const GrBackendTexture& backendTexture,
Greg Daniel373d7dd2020-07-21 10:41:50 -0400664 SkColorType skColorType,
665 const SkColor4f& color,
666 GrGpuFinishedProc finishedProc,
667 GrGpuFinishedContext finishedContext) {
668 sk_sp<GrRefCntedCallback> finishedCallback;
669 if (finishedProc) {
670 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
671 }
672
673 if (!this->asDirectContext()) {
674 return false;
675 }
676
677 if (this->abandoned()) {
678 return false;
679 }
680
681 GrBackendFormat format = backendTexture.getBackendFormat();
682 GrColorType grColorType = SkColorTypeAndFormatToGrColorType(this->caps(), skColorType, format);
683
684 if (!this->caps()->areColorTypeAndFormatCompatible(grColorType, format)) {
685 return false;
686 }
687
688 GrSwizzle swizzle = this->caps()->getWriteSwizzle(format, grColorType);
689 GrGpu::BackendTextureData data(swizzle.applyTo(color));
690
691 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
692}
693
694bool GrContext::updateBackendTexture(const GrBackendTexture& backendTexture,
Greg Danielb2365d82020-05-13 15:32:04 -0400695 const SkPixmap srcData[],
696 int numLevels,
697 GrGpuFinishedProc finishedProc,
698 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400699 sk_sp<GrRefCntedCallback> finishedCallback;
700 if (finishedProc) {
701 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
702 }
703
Greg Danielb2365d82020-05-13 15:32:04 -0400704 if (!this->asDirectContext()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400705 return false;
706 }
707
708 if (this->abandoned()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400709 return false;
710 }
711
712 if (!srcData || numLevels <= 0) {
Greg Danielb2365d82020-05-13 15:32:04 -0400713 return false;
714 }
715
716 int numExpectedLevels = 1;
Brian Salomon40a40622020-07-21 10:32:07 -0400717 if (backendTexture.hasMipmaps()) {
Mike Reed13711eb2020-07-14 17:16:32 -0400718 numExpectedLevels = SkMipmap::ComputeLevelCount(backendTexture.width(),
Greg Danielb2365d82020-05-13 15:32:04 -0400719 backendTexture.height()) + 1;
720 }
721 if (numLevels != numExpectedLevels) {
Greg Danielb2365d82020-05-13 15:32:04 -0400722 return false;
723 }
724
725 GrGpu::BackendTextureData data(srcData);
Greg Daniel25597782020-06-11 13:15:08 -0400726 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
Greg Danielb2365d82020-05-13 15:32:04 -0400727}
728
Robert Phillipsb915c942019-12-17 14:44:37 -0500729//////////////////////////////////////////////////////////////////////////////
730
Greg Danielaaf738c2020-07-10 09:30:33 -0400731static GrBackendTexture create_and_update_compressed_backend_texture(
Robert Phillips4e105e22020-07-16 09:18:50 -0400732 GrDirectContext* context,
Greg Danielaaf738c2020-07-10 09:30:33 -0400733 SkISize dimensions,
734 const GrBackendFormat& backendFormat,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400735 GrMipmapped mipMapped,
Greg Danielaaf738c2020-07-10 09:30:33 -0400736 GrProtected isProtected,
737 sk_sp<GrRefCntedCallback> finishedCallback,
738 const GrGpu::BackendTextureData* data) {
739 GrGpu* gpu = context->priv().getGpu();
740
741 GrBackendTexture beTex = gpu->createCompressedBackendTexture(dimensions, backendFormat,
742 mipMapped, isProtected);
743 if (!beTex.isValid()) {
744 return {};
745 }
746
747 if (!context->priv().getGpu()->updateCompressedBackendTexture(
748 beTex, std::move(finishedCallback), data)) {
749 context->deleteBackendTexture(beTex);
750 return {};
751 }
752 return beTex;
753}
754
Robert Phillipsb915c942019-12-17 14:44:37 -0500755GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
756 const GrBackendFormat& backendFormat,
757 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400758 GrMipmapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400759 GrProtected isProtected,
760 GrGpuFinishedProc finishedProc,
761 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500762 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel25597782020-06-11 13:15:08 -0400763 sk_sp<GrRefCntedCallback> finishedCallback;
764 if (finishedProc) {
765 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
766 }
767
Robert Phillipsb915c942019-12-17 14:44:37 -0500768 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400769 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500770 }
771
772 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400773 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500774 }
775
776 GrGpu::BackendTextureData data(color);
Robert Phillips4e105e22020-07-16 09:18:50 -0400777 return create_and_update_compressed_backend_texture(this->asDirectContext(), {width, height},
778 backendFormat, mipMapped, isProtected,
Greg Danielaaf738c2020-07-10 09:30:33 -0400779 std::move(finishedCallback), &data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500780}
781
782GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
783 SkImage::CompressionType compression,
784 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400785 GrMipmapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400786 GrProtected isProtected,
787 GrGpuFinishedProc finishedProc,
788 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500789 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillipsb915c942019-12-17 14:44:37 -0500790 GrBackendFormat format = this->compressedBackendFormat(compression);
791 return this->createCompressedBackendTexture(width, height, format, color,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400792 mipMapped, isProtected, finishedProc,
793 finishedContext);
Robert Phillipsb915c942019-12-17 14:44:37 -0500794}
795
796GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
797 const GrBackendFormat& backendFormat,
798 const void* compressedData,
799 size_t dataSize,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400800 GrMipmapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400801 GrProtected isProtected,
802 GrGpuFinishedProc finishedProc,
803 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500804 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel25597782020-06-11 13:15:08 -0400805 sk_sp<GrRefCntedCallback> finishedCallback;
806 if (finishedProc) {
807 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
808 }
809
Robert Phillipsb915c942019-12-17 14:44:37 -0500810 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400811 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500812 }
813
814 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400815 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500816 }
817
818 GrGpu::BackendTextureData data(compressedData, dataSize);
Robert Phillips4e105e22020-07-16 09:18:50 -0400819 return create_and_update_compressed_backend_texture(this->asDirectContext(), {width, height},
820 backendFormat, mipMapped, isProtected,
Greg Danielaaf738c2020-07-10 09:30:33 -0400821 std::move(finishedCallback), &data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500822}
823
824GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
825 SkImage::CompressionType compression,
826 const void* data, size_t dataSize,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400827 GrMipmapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400828 GrProtected isProtected,
829 GrGpuFinishedProc finishedProc,
830 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500831 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillipsb915c942019-12-17 14:44:37 -0500832 GrBackendFormat format = this->compressedBackendFormat(compression);
Greg Daniel25597782020-06-11 13:15:08 -0400833 return this->createCompressedBackendTexture(width, height, format, data, dataSize, mipMapped,
834 isProtected, finishedProc, finishedContext);
Robert Phillipsb915c942019-12-17 14:44:37 -0500835}
836
Greg Daniel1db8e792020-06-09 17:29:32 -0400837bool GrContext::setBackendTextureState(const GrBackendTexture& backendTexture,
838 const GrBackendSurfaceMutableState& state,
839 GrGpuFinishedProc finishedProc,
840 GrGpuFinishedContext finishedContext) {
Greg Daniel1db8e792020-06-09 17:29:32 -0400841 sk_sp<GrRefCntedCallback> callback;
842 if (finishedProc) {
843 callback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
844 }
Greg Daniel25597782020-06-11 13:15:08 -0400845
846 if (!this->asDirectContext()) {
847 return false;
848 }
849
850 if (this->abandoned()) {
851 return false;
852 }
853
Greg Daniel1db8e792020-06-09 17:29:32 -0400854 return fGpu->setBackendTextureState(backendTexture, state, std::move(callback));
855}
856
Greg Daniel95afafb2020-07-22 12:09:26 -0400857bool GrContext::updateCompressedBackendTexture(const GrBackendTexture& backendTexture,
858 const SkColor4f& color,
859 GrGpuFinishedProc finishedProc,
860 GrGpuFinishedContext finishedContext) {
861 sk_sp<GrRefCntedCallback> finishedCallback;
862 if (finishedProc) {
863 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
864 }
865
866 if (!this->asDirectContext()) {
867 return false;
868 }
869
870 if (this->abandoned()) {
871 return false;
872 }
873
874 GrGpu::BackendTextureData data(color);
875 return fGpu->updateCompressedBackendTexture(backendTexture, std::move(finishedCallback), &data);
876}
877
878bool GrContext::updateCompressedBackendTexture(const GrBackendTexture& backendTexture,
879 const void* compressedData,
880 size_t dataSize,
881 GrGpuFinishedProc finishedProc,
882 GrGpuFinishedContext finishedContext) {
883 sk_sp<GrRefCntedCallback> finishedCallback;
884 if (finishedProc) {
885 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
886 }
887
888 if (!this->asDirectContext()) {
889 return false;
890 }
891
892 if (this->abandoned()) {
893 return false;
894 }
895
896 if (!compressedData) {
897 return false;
898 }
899
900 GrGpu::BackendTextureData data(compressedData, dataSize);
901
902 return fGpu->updateCompressedBackendTexture(backendTexture, std::move(finishedCallback), &data);
903}
904
905//////////////////////////////////////////////////////////////////////////////
906
Greg Daniel1db8e792020-06-09 17:29:32 -0400907bool GrContext::setBackendRenderTargetState(const GrBackendRenderTarget& backendRenderTarget,
908 const GrBackendSurfaceMutableState& state,
909 GrGpuFinishedProc finishedProc,
910 GrGpuFinishedContext finishedContext) {
Greg Daniel1db8e792020-06-09 17:29:32 -0400911 sk_sp<GrRefCntedCallback> callback;
912 if (finishedProc) {
913 callback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
914 }
Greg Daniel25597782020-06-11 13:15:08 -0400915
916 if (!this->asDirectContext()) {
917 return false;
918 }
919
920 if (this->abandoned()) {
921 return false;
922 }
923
Greg Daniel1db8e792020-06-09 17:29:32 -0400924 return fGpu->setBackendRenderTargetState(backendRenderTarget, state, std::move(callback));
925}
926
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400927void GrContext::deleteBackendTexture(GrBackendTexture backendTex) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400928 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danielf0e04f02019-12-04 15:17:54 -0500929 // For the Vulkan backend we still must destroy the backend texture when the context is
930 // abandoned.
931 if ((this->abandoned() && this->backend() != GrBackendApi::kVulkan) || !backendTex.isValid()) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400932 return;
933 }
934
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400935 fGpu->deleteBackendTexture(backendTex);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400936}
937
Greg Daniel1db8e792020-06-09 17:29:32 -0400938//////////////////////////////////////////////////////////////////////////////
939
Brian Osmaned58e002019-09-06 14:42:43 -0400940bool GrContext::precompileShader(const SkData& key, const SkData& data) {
941 return fGpu->precompileShader(key, data);
942}
943
Brian Salomonec22b1a2019-08-09 09:41:48 -0400944#ifdef SK_ENABLE_DUMP_GPU
Michael Ludwigdd205452020-03-30 17:16:34 -0400945#include "include/core/SkString.h"
Brian Salomonec22b1a2019-08-09 09:41:48 -0400946#include "src/utils/SkJSONWriter.h"
947SkString GrContext::dump() const {
948 SkDynamicMemoryWStream stream;
949 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
950 writer.beginObject();
951
952 writer.appendString("backend", GrBackendApiToStr(this->backend()));
953
954 writer.appendName("caps");
955 this->caps()->dumpJSON(&writer);
956
957 writer.appendName("gpu");
958 this->fGpu->dumpJSON(&writer);
959
Robert Phillips273f1072020-05-05 13:03:07 -0400960 writer.appendName("context");
961 this->dumpJSON(&writer);
962
Brian Salomonec22b1a2019-08-09 09:41:48 -0400963 // Flush JSON to the memory stream
964 writer.endObject();
965 writer.flush();
966
967 // Null terminate the JSON data in the memory stream
968 stream.write8(0);
969
970 // Allocate a string big enough to hold all the data, then copy out of the stream
971 SkString result(stream.bytesWritten());
972 stream.copyToAndReset(result.writable_str());
973 return result;
974}
975#endif