blob: 4e1b27ee3c744e01cfade88be94caf39874d8555 [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
215 fMappedBufferManager->process();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600216 auto purgeTime = GrStdSteadyClock::now() - msNotUsed;
217
Jim Van Verth76d917c2017-12-13 09:26:37 -0500218 fResourceCache->purgeAsNeeded();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600219 fResourceCache->purgeResourcesNotUsedSince(purgeTime);
220
Robert Phillips6a6de562019-02-15 15:19:15 -0500221 if (auto ccpr = this->drawingManager()->getCoverageCountingPathRenderer()) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500222 ccpr->purgeCacheEntriesOlderThan(this->proxyProvider(), purgeTime);
Chris Dalton6c3879d2018-11-01 11:13:19 -0600223 }
Jim Van Verth76d917c2017-12-13 09:26:37 -0500224
Robert Phillips2184fb72019-02-21 16:11:41 -0500225 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
226 // place to purge stale blobs
227 this->getTextBlobCache()->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400228}
229
Derek Sollenberger5480a182017-05-25 16:43:59 -0400230void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
231 ASSERT_SINGLE_OWNER
Robert Phillipsddc21482019-10-16 14:30:09 -0400232
233 if (this->abandoned()) {
234 return;
235 }
236
Derek Sollenberger5480a182017-05-25 16:43:59 -0400237 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
238}
239
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000240void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800241 ASSERT_SINGLE_OWNER
242
bsalomon71cb0c22014-11-14 12:10:14 -0800243 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800244 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800245 }
246 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800247 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800248 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000249}
250
Derek Sollenbergeree479142017-05-24 11:41:33 -0400251size_t GrContext::getResourceCachePurgeableBytes() const {
252 ASSERT_SINGLE_OWNER
253 return fResourceCache->getPurgeableBytes();
254}
255
Brian Salomon7e67dca2020-07-21 09:27:25 -0400256size_t GrContext::ComputeImageSize(sk_sp<SkImage> image, GrMipmapped mipMapped, bool useNextPow2) {
Greg Daniel8b666172019-10-09 12:38:22 -0400257 if (!image->isTextureBacked()) {
258 return 0;
259 }
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400260 SkImage_GpuBase* gpuImage = static_cast<SkImage_GpuBase*>(as_IB(image.get()));
261 GrTextureProxy* proxy = gpuImage->peekProxy();
262 if (!proxy) {
263 return 0;
264 }
265
266 const GrCaps& caps = *gpuImage->context()->priv().caps();
Greg Daniel8b666172019-10-09 12:38:22 -0400267 int colorSamplesPerPixel = 1;
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400268 return GrSurface::ComputeSize(caps, proxy->backendFormat(), image->dimensions(),
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400269 colorSamplesPerPixel, mipMapped, useNextPow2);
Greg Daniel8b666172019-10-09 12:38:22 -0400270}
271
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000272////////////////////////////////////////////////////////////////////////////////
273
Robert Phillipsbb606772019-02-04 17:50:57 -0500274int GrContext::maxTextureSize() const { return this->caps()->maxTextureSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400275
Robert Phillipsbb606772019-02-04 17:50:57 -0500276int GrContext::maxRenderTargetSize() const { return this->caps()->maxRenderTargetSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400277
Brian Salomonbdecacf2018-02-02 20:32:49 -0500278bool GrContext::colorTypeSupportedAsImage(SkColorType colorType) const {
Greg Daniel7bfc9132019-08-14 14:23:53 -0400279 GrBackendFormat format =
280 this->caps()->getDefaultBackendFormat(SkColorTypeToGrColorType(colorType),
281 GrRenderable::kNo);
282 return format.isValid();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500283}
284
Brian Salomonbdecacf2018-02-02 20:32:49 -0500285////////////////////////////////////////////////////////////////////////////////
286
Greg Daniel414418d2020-07-08 11:44:25 -0400287bool GrContext::wait(int numSemaphores, const GrBackendSemaphore waitSemaphores[],
288 bool deleteSemaphoresAfterWait) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400289 if (!fGpu || fGpu->caps()->semaphoreSupport()) {
Greg Daniel06be0792019-04-22 15:53:23 -0400290 return false;
291 }
Greg Daniel414418d2020-07-08 11:44:25 -0400292 GrWrapOwnership ownership =
293 deleteSemaphoresAfterWait ? kAdopt_GrWrapOwnership : kBorrow_GrWrapOwnership;
Greg Daniel06be0792019-04-22 15:53:23 -0400294 for (int i = 0; i < numSemaphores; ++i) {
Greg Daniel301015c2019-11-18 14:06:46 -0500295 std::unique_ptr<GrSemaphore> sema = fResourceProvider->wrapBackendSemaphore(
Greg Daniel414418d2020-07-08 11:44:25 -0400296 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait, ownership);
Greg Daniel0106fcc2020-07-01 17:40:12 -0400297 // If we failed to wrap the semaphore it means the client didn't give us a valid semaphore
298 // to begin with. Therefore, it is fine to not wait on it.
299 if (sema) {
300 fGpu->waitSemaphore(sema.get());
301 }
Greg Daniel06be0792019-04-22 15:53:23 -0400302 }
303 return true;
304}
305
306////////////////////////////////////////////////////////////////////////////////
307
Greg Daniele8d3cca2020-06-10 10:04:48 -0400308GrSemaphoresSubmitted GrContext::flush(const GrFlushInfo& info) {
Greg Daniel51316782017-08-02 15:10:09 +0000309 ASSERT_SINGLE_OWNER
Robert Phillipsa9162df2019-02-11 14:12:03 -0500310 if (this->abandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400311 if (info.fFinishedProc) {
312 info.fFinishedProc(info.fFinishedContext);
313 }
314 if (info.fSubmittedProc) {
315 info.fSubmittedProc(info.fSubmittedContext, false);
316 }
Robert Phillipsa9162df2019-02-11 14:12:03 -0500317 return GrSemaphoresSubmitted::kNo;
318 }
Greg Daniel51316782017-08-02 15:10:09 +0000319
Greg Daniel04283f32020-05-20 13:16:00 -0400320 bool flushed = this->drawingManager()->flush(
Greg Daniel9efe3862020-06-11 11:51:06 -0400321 nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, info, nullptr);
Greg Danielfe159622020-04-10 17:43:51 +0000322
Greg Daniel04283f32020-05-20 13:16:00 -0400323 if (!flushed || (!this->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
Greg Danielfe159622020-04-10 17:43:51 +0000324 return GrSemaphoresSubmitted::kNo;
325 }
326 return GrSemaphoresSubmitted::kYes;
Greg Daniel51316782017-08-02 15:10:09 +0000327}
328
Greg Daniel04283f32020-05-20 13:16:00 -0400329bool GrContext::submit(bool syncCpu) {
330 ASSERT_SINGLE_OWNER
331 if (this->abandoned()) {
332 return false;
333 }
334
335 if (!fGpu) {
336 return false;
337 }
338
339 return fGpu->submitToGpu(syncCpu);
Greg Danielda50cb82020-05-13 14:07:40 -0400340}
341
Greg Daniela870b462019-01-08 15:49:46 -0500342////////////////////////////////////////////////////////////////////////////////
343
Brian Salomonb0d8b762019-05-06 16:58:22 -0400344void GrContext::checkAsyncWorkCompletion() {
345 if (fGpu) {
346 fGpu->checkFinishProcs();
347 }
348}
349
350////////////////////////////////////////////////////////////////////////////////
351
Greg Daniela870b462019-01-08 15:49:46 -0500352void GrContext::storeVkPipelineCacheData() {
353 if (fGpu) {
354 fGpu->storeVkPipelineCacheData();
355 }
356}
357
358////////////////////////////////////////////////////////////////////////////////
359
Khushal3e7548c2018-05-23 15:45:01 -0700360bool GrContext::supportsDistanceFieldText() const {
Robert Phillipsbb606772019-02-04 17:50:57 -0500361 return this->caps()->shaderCaps()->supportsDistanceFieldText();
Khushal3e7548c2018-05-23 15:45:01 -0700362}
363
bsalomon37f9a262015-02-02 13:00:10 -0800364//////////////////////////////////////////////////////////////////////////////
365
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500366void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800367 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500368 if (maxResources) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400369 *maxResources = -1;
bsalomon37f9a262015-02-02 13:00:10 -0800370 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500371 if (maxResourceBytes) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400372 *maxResourceBytes = this->getResourceCacheLimit();
bsalomon37f9a262015-02-02 13:00:10 -0800373 }
374}
375
Robert Phillipscf39f372019-09-03 10:29:20 -0400376size_t GrContext::getResourceCacheLimit() const {
joshualitt1de610a2016-01-06 08:26:09 -0800377 ASSERT_SINGLE_OWNER
Robert Phillipscf39f372019-09-03 10:29:20 -0400378 return fResourceCache->getMaxResourceBytes();
379}
380
381void GrContext::setResourceCacheLimits(int unused, size_t maxResourceBytes) {
382 ASSERT_SINGLE_OWNER
383 this->setResourceCacheLimit(maxResourceBytes);
384}
385
386void GrContext::setResourceCacheLimit(size_t maxResourceBytes) {
387 ASSERT_SINGLE_OWNER
388 fResourceCache->setLimit(maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800389}
390
ericrk0a5fa482015-09-15 14:16:10 -0700391//////////////////////////////////////////////////////////////////////////////
ericrk0a5fa482015-09-15 14:16:10 -0700392void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800393 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700394 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
Khushal71652e22018-10-29 13:05:36 -0700395 traceMemoryDump->dumpNumericValue("skia/gr_text_blob_cache", "size", "bytes",
Robert Phillips2184fb72019-02-21 16:11:41 -0500396 this->getTextBlobCache()->usedBytes());
ericrk0a5fa482015-09-15 14:16:10 -0700397}
Brian Osman71a18892017-08-10 10:23:25 -0400398
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400399//////////////////////////////////////////////////////////////////////////////
400GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400401 const GrBackendFormat& backendFormat,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400402 GrMipmapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400403 GrRenderable renderable,
404 GrProtected isProtected) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400405 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400406 if (!this->asDirectContext()) {
407 return GrBackendTexture();
408 }
409
410 if (this->abandoned()) {
411 return GrBackendTexture();
412 }
413
Robert Phillips4277f012020-01-21 14:28:34 -0500414 return fGpu->createBackendTexture({width, height}, backendFormat, renderable,
Greg Daniel16032b32020-05-06 15:31:10 -0400415 mipMapped, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400416}
417
418GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400419 SkColorType skColorType,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400420 GrMipmapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400421 GrRenderable renderable,
422 GrProtected isProtected) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400423 if (!this->asDirectContext()) {
424 return GrBackendTexture();
425 }
426
427 if (this->abandoned()) {
428 return GrBackendTexture();
429 }
430
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400431 const GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400432
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400433 return this->createBackendTexture(width, height, format, mipMapped, renderable, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400434}
435
Robert Phillips02dc0302019-07-02 17:58:27 -0400436GrBackendTexture GrContext::createBackendTexture(const SkSurfaceCharacterization& c) {
Robert Phillips02dc0302019-07-02 17:58:27 -0400437 if (!this->asDirectContext() || !c.isValid()) {
438 return GrBackendTexture();
439 }
440
441 if (this->abandoned()) {
442 return GrBackendTexture();
443 }
444
445 if (c.usesGLFBO0()) {
446 // If we are making the surface we will never use FBO0.
447 return GrBackendTexture();
448 }
449
450 if (c.vulkanSecondaryCBCompatible()) {
451 return {};
452 }
453
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400454 const GrBackendFormat format = this->defaultBackendFormat(c.colorType(), GrRenderable::kYes);
Robert Phillips02dc0302019-07-02 17:58:27 -0400455 if (!format.isValid()) {
456 return GrBackendTexture();
457 }
458
Robert Phillips02dc0302019-07-02 17:58:27 -0400459 GrBackendTexture result = this->createBackendTexture(c.width(), c.height(), format,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400460 GrMipmapped(c.isMipMapped()),
Robert Phillips02dc0302019-07-02 17:58:27 -0400461 GrRenderable::kYes,
Robert Phillips3cd54322019-07-10 09:28:59 -0400462 c.isProtected());
Robert Phillips02dc0302019-07-02 17:58:27 -0400463 SkASSERT(c.isCompatible(result));
464 return result;
465}
466
Greg Daniel25597782020-06-11 13:15:08 -0400467static GrBackendTexture create_and_update_backend_texture(
Robert Phillips4e105e22020-07-16 09:18:50 -0400468 GrDirectContext* context,
Greg Daniel25597782020-06-11 13:15:08 -0400469 SkISize dimensions,
470 const GrBackendFormat& backendFormat,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400471 GrMipmapped mipMapped,
Greg Daniel25597782020-06-11 13:15:08 -0400472 GrRenderable renderable,
473 GrProtected isProtected,
474 sk_sp<GrRefCntedCallback> finishedCallback,
475 const GrGpu::BackendTextureData* data) {
Greg Daniel16032b32020-05-06 15:31:10 -0400476 GrGpu* gpu = context->priv().getGpu();
477
478 GrBackendTexture beTex = gpu->createBackendTexture(dimensions, backendFormat, renderable,
479 mipMapped, isProtected);
480 if (!beTex.isValid()) {
481 return {};
482 }
483
Greg Daniel25597782020-06-11 13:15:08 -0400484 if (!context->priv().getGpu()->updateBackendTexture(beTex, std::move(finishedCallback), data)) {
Greg Daniel16032b32020-05-06 15:31:10 -0400485 context->deleteBackendTexture(beTex);
486 return {};
487 }
488 return beTex;
489}
490
Greg Daniel25597782020-06-11 13:15:08 -0400491
492GrBackendTexture GrContext::createBackendTexture(const SkSurfaceCharacterization& c,
493 const SkColor4f& color,
494 GrGpuFinishedProc finishedProc,
495 GrGpuFinishedContext finishedContext) {
496 sk_sp<GrRefCntedCallback> finishedCallback;
497 if (finishedProc) {
498 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
499 }
500
501 if (!this->asDirectContext() || !c.isValid()) {
502 return {};
503 }
504
505 if (this->abandoned()) {
506 return {};
507 }
508
509 if (c.usesGLFBO0()) {
510 // If we are making the surface we will never use FBO0.
511 return {};
512 }
513
514 if (c.vulkanSecondaryCBCompatible()) {
515 return {};
516 }
517
518 const GrBackendFormat format = this->defaultBackendFormat(c.colorType(), GrRenderable::kYes);
519 if (!format.isValid()) {
520 return {};
521 }
522
523 GrGpu::BackendTextureData data(color);
524 GrBackendTexture result = create_and_update_backend_texture(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400525 this->asDirectContext(), {c.width(), c.height()}, format, GrMipmapped(c.isMipMapped()),
Robert Phillips4e105e22020-07-16 09:18:50 -0400526 GrRenderable::kYes, c.isProtected(), std::move(finishedCallback), &data);
Greg Daniel25597782020-06-11 13:15:08 -0400527
528 SkASSERT(c.isCompatible(result));
529 return result;
530}
531
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400532GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400533 const GrBackendFormat& backendFormat,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400534 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400535 GrMipmapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400536 GrRenderable renderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400537 GrProtected isProtected,
538 GrGpuFinishedProc finishedProc,
539 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400540 sk_sp<GrRefCntedCallback> finishedCallback;
541 if (finishedProc) {
542 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
543 }
544
Brian Salomonc42eb662019-06-24 17:13:00 -0400545 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400546 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400547 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400548 }
549
550 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400551 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400552 }
553
Brian Salomon85c3d682019-11-04 15:04:54 -0500554 GrGpu::BackendTextureData data(color);
Robert Phillips4e105e22020-07-16 09:18:50 -0400555 return create_and_update_backend_texture(this->asDirectContext(), {width, height},
556 backendFormat, mipMapped, renderable, isProtected,
557 std::move(finishedCallback), &data);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400558}
559
560GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400561 SkColorType skColorType,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400562 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400563 GrMipmapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400564 GrRenderable renderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400565 GrProtected isProtected,
566 GrGpuFinishedProc finishedProc,
567 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400568 sk_sp<GrRefCntedCallback> finishedCallback;
569 if (finishedProc) {
570 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
571 }
572
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400573 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400574 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400575 }
576
577 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400578 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400579 }
580
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400581 GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400582 if (!format.isValid()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400583 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400584 }
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400585
586 GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
Brian Salomon982f5462020-03-30 12:52:33 -0400587 SkColor4f swizzledColor = this->caps()->getWriteSwizzle(format, grColorType).applyTo(color);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400588
Greg Daniel25597782020-06-11 13:15:08 -0400589 GrGpu::BackendTextureData data(swizzledColor);
Robert Phillips4e105e22020-07-16 09:18:50 -0400590 return create_and_update_backend_texture(this->asDirectContext(), {width, height}, format,
591 mipMapped, renderable, isProtected,
592 std::move(finishedCallback), &data);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400593}
594
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500595GrBackendTexture GrContext::createBackendTexture(const SkPixmap srcData[], int numProvidedLevels,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400596 GrRenderable renderable, GrProtected isProtected,
597 GrGpuFinishedProc finishedProc,
598 GrGpuFinishedContext finishedContext) {
Robert Phillips66944402019-09-30 13:21:25 -0400599 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
600
Greg Daniel25597782020-06-11 13:15:08 -0400601 sk_sp<GrRefCntedCallback> finishedCallback;
602 if (finishedProc) {
603 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
604 }
605
Robert Phillips66944402019-09-30 13:21:25 -0400606 if (!this->asDirectContext()) {
607 return {};
608 }
609
610 if (this->abandoned()) {
611 return {};
612 }
613
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500614 if (!srcData || numProvidedLevels <= 0) {
Robert Phillips66944402019-09-30 13:21:25 -0400615 return {};
616 }
617
618 int baseWidth = srcData[0].width();
619 int baseHeight = srcData[0].height();
620 SkColorType colorType = srcData[0].colorType();
621
Brian Salomon7e67dca2020-07-21 09:27:25 -0400622 GrMipmapped mipMapped = GrMipmapped::kNo;
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500623 int numExpectedLevels = 1;
624 if (numProvidedLevels > 1) {
Mike Reed13711eb2020-07-14 17:16:32 -0400625 numExpectedLevels = SkMipmap::ComputeLevelCount(baseWidth, baseHeight) + 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400626 mipMapped = GrMipmapped::kYes;
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500627 }
628
629 if (numProvidedLevels != numExpectedLevels) {
630 return {};
631 }
632
Robert Phillips66944402019-09-30 13:21:25 -0400633 GrBackendFormat backendFormat = this->defaultBackendFormat(colorType, renderable);
634
Brian Salomon85c3d682019-11-04 15:04:54 -0500635 GrGpu::BackendTextureData data(srcData);
Robert Phillips4e105e22020-07-16 09:18:50 -0400636 return create_and_update_backend_texture(this->asDirectContext(), {baseWidth, baseHeight},
637 backendFormat, mipMapped, renderable, isProtected,
Greg Daniel25597782020-06-11 13:15:08 -0400638 std::move(finishedCallback), &data);
Robert Phillips66944402019-09-30 13:21:25 -0400639}
640
Greg Danielb2365d82020-05-13 15:32:04 -0400641bool GrContext::updateBackendTexture(const GrBackendTexture& backendTexture,
642 const SkColor4f& color,
643 GrGpuFinishedProc finishedProc,
644 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400645 sk_sp<GrRefCntedCallback> finishedCallback;
646 if (finishedProc) {
647 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
648 }
649
Greg Danielb2365d82020-05-13 15:32:04 -0400650 if (!this->asDirectContext()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400651 return false;
652 }
653
654 if (this->abandoned()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400655 return false;
656 }
657
658 GrGpu::BackendTextureData data(color);
Greg Daniel25597782020-06-11 13:15:08 -0400659 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
Greg Danielb2365d82020-05-13 15:32:04 -0400660}
661
662bool GrContext::updateBackendTexture(const GrBackendTexture& backendTexture,
Greg Daniel373d7dd2020-07-21 10:41:50 -0400663 SkColorType skColorType,
664 const SkColor4f& color,
665 GrGpuFinishedProc finishedProc,
666 GrGpuFinishedContext finishedContext) {
667 sk_sp<GrRefCntedCallback> finishedCallback;
668 if (finishedProc) {
669 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
670 }
671
672 if (!this->asDirectContext()) {
673 return false;
674 }
675
676 if (this->abandoned()) {
677 return false;
678 }
679
680 GrBackendFormat format = backendTexture.getBackendFormat();
681 GrColorType grColorType = SkColorTypeAndFormatToGrColorType(this->caps(), skColorType, format);
682
683 if (!this->caps()->areColorTypeAndFormatCompatible(grColorType, format)) {
684 return false;
685 }
686
687 GrSwizzle swizzle = this->caps()->getWriteSwizzle(format, grColorType);
688 GrGpu::BackendTextureData data(swizzle.applyTo(color));
689
690 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
691}
692
693bool GrContext::updateBackendTexture(const GrBackendTexture& backendTexture,
Greg Danielb2365d82020-05-13 15:32:04 -0400694 const SkPixmap srcData[],
695 int numLevels,
696 GrGpuFinishedProc finishedProc,
697 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400698 sk_sp<GrRefCntedCallback> finishedCallback;
699 if (finishedProc) {
700 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
701 }
702
Greg Danielb2365d82020-05-13 15:32:04 -0400703 if (!this->asDirectContext()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400704 return false;
705 }
706
707 if (this->abandoned()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400708 return false;
709 }
710
711 if (!srcData || numLevels <= 0) {
Greg Danielb2365d82020-05-13 15:32:04 -0400712 return false;
713 }
714
715 int numExpectedLevels = 1;
Brian Salomon40a40622020-07-21 10:32:07 -0400716 if (backendTexture.hasMipmaps()) {
Mike Reed13711eb2020-07-14 17:16:32 -0400717 numExpectedLevels = SkMipmap::ComputeLevelCount(backendTexture.width(),
Greg Danielb2365d82020-05-13 15:32:04 -0400718 backendTexture.height()) + 1;
719 }
720 if (numLevels != numExpectedLevels) {
Greg Danielb2365d82020-05-13 15:32:04 -0400721 return false;
722 }
723
724 GrGpu::BackendTextureData data(srcData);
Greg Daniel25597782020-06-11 13:15:08 -0400725 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
Greg Danielb2365d82020-05-13 15:32:04 -0400726}
727
Robert Phillipsb915c942019-12-17 14:44:37 -0500728//////////////////////////////////////////////////////////////////////////////
729
Greg Danielaaf738c2020-07-10 09:30:33 -0400730static GrBackendTexture create_and_update_compressed_backend_texture(
Robert Phillips4e105e22020-07-16 09:18:50 -0400731 GrDirectContext* context,
Greg Danielaaf738c2020-07-10 09:30:33 -0400732 SkISize dimensions,
733 const GrBackendFormat& backendFormat,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400734 GrMipmapped mipMapped,
Greg Danielaaf738c2020-07-10 09:30:33 -0400735 GrProtected isProtected,
736 sk_sp<GrRefCntedCallback> finishedCallback,
737 const GrGpu::BackendTextureData* data) {
738 GrGpu* gpu = context->priv().getGpu();
739
740 GrBackendTexture beTex = gpu->createCompressedBackendTexture(dimensions, backendFormat,
741 mipMapped, isProtected);
742 if (!beTex.isValid()) {
743 return {};
744 }
745
746 if (!context->priv().getGpu()->updateCompressedBackendTexture(
747 beTex, std::move(finishedCallback), data)) {
748 context->deleteBackendTexture(beTex);
749 return {};
750 }
751 return beTex;
752}
753
Robert Phillipsb915c942019-12-17 14:44:37 -0500754GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
755 const GrBackendFormat& backendFormat,
756 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400757 GrMipmapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400758 GrProtected isProtected,
759 GrGpuFinishedProc finishedProc,
760 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500761 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel25597782020-06-11 13:15:08 -0400762 sk_sp<GrRefCntedCallback> finishedCallback;
763 if (finishedProc) {
764 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
765 }
766
Robert Phillipsb915c942019-12-17 14:44:37 -0500767 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400768 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500769 }
770
771 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400772 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500773 }
774
775 GrGpu::BackendTextureData data(color);
Robert Phillips4e105e22020-07-16 09:18:50 -0400776 return create_and_update_compressed_backend_texture(this->asDirectContext(), {width, height},
777 backendFormat, mipMapped, isProtected,
Greg Danielaaf738c2020-07-10 09:30:33 -0400778 std::move(finishedCallback), &data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500779}
780
781GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
782 SkImage::CompressionType compression,
783 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400784 GrMipmapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400785 GrProtected isProtected,
786 GrGpuFinishedProc finishedProc,
787 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500788 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillipsb915c942019-12-17 14:44:37 -0500789 GrBackendFormat format = this->compressedBackendFormat(compression);
790 return this->createCompressedBackendTexture(width, height, format, color,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400791 mipMapped, isProtected, finishedProc,
792 finishedContext);
Robert Phillipsb915c942019-12-17 14:44:37 -0500793}
794
795GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
796 const GrBackendFormat& backendFormat,
797 const void* compressedData,
798 size_t dataSize,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400799 GrMipmapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400800 GrProtected isProtected,
801 GrGpuFinishedProc finishedProc,
802 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500803 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel25597782020-06-11 13:15:08 -0400804 sk_sp<GrRefCntedCallback> finishedCallback;
805 if (finishedProc) {
806 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
807 }
808
Robert Phillipsb915c942019-12-17 14:44:37 -0500809 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400810 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500811 }
812
813 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400814 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500815 }
816
817 GrGpu::BackendTextureData data(compressedData, dataSize);
Robert Phillips4e105e22020-07-16 09:18:50 -0400818 return create_and_update_compressed_backend_texture(this->asDirectContext(), {width, height},
819 backendFormat, mipMapped, isProtected,
Greg Danielaaf738c2020-07-10 09:30:33 -0400820 std::move(finishedCallback), &data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500821}
822
823GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
824 SkImage::CompressionType compression,
825 const void* data, size_t dataSize,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400826 GrMipmapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400827 GrProtected isProtected,
828 GrGpuFinishedProc finishedProc,
829 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500830 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillipsb915c942019-12-17 14:44:37 -0500831 GrBackendFormat format = this->compressedBackendFormat(compression);
Greg Daniel25597782020-06-11 13:15:08 -0400832 return this->createCompressedBackendTexture(width, height, format, data, dataSize, mipMapped,
833 isProtected, finishedProc, finishedContext);
Robert Phillipsb915c942019-12-17 14:44:37 -0500834}
835
Greg Daniel1db8e792020-06-09 17:29:32 -0400836bool GrContext::setBackendTextureState(const GrBackendTexture& backendTexture,
837 const GrBackendSurfaceMutableState& state,
838 GrGpuFinishedProc finishedProc,
839 GrGpuFinishedContext finishedContext) {
Greg Daniel1db8e792020-06-09 17:29:32 -0400840 sk_sp<GrRefCntedCallback> callback;
841 if (finishedProc) {
842 callback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
843 }
Greg Daniel25597782020-06-11 13:15:08 -0400844
845 if (!this->asDirectContext()) {
846 return false;
847 }
848
849 if (this->abandoned()) {
850 return false;
851 }
852
Greg Daniel1db8e792020-06-09 17:29:32 -0400853 return fGpu->setBackendTextureState(backendTexture, state, std::move(callback));
854}
855
Greg Daniel95afafb2020-07-22 12:09:26 -0400856bool GrContext::updateCompressedBackendTexture(const GrBackendTexture& backendTexture,
857 const SkColor4f& color,
858 GrGpuFinishedProc finishedProc,
859 GrGpuFinishedContext finishedContext) {
860 sk_sp<GrRefCntedCallback> finishedCallback;
861 if (finishedProc) {
862 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
863 }
864
865 if (!this->asDirectContext()) {
866 return false;
867 }
868
869 if (this->abandoned()) {
870 return false;
871 }
872
873 GrGpu::BackendTextureData data(color);
874 return fGpu->updateCompressedBackendTexture(backendTexture, std::move(finishedCallback), &data);
875}
876
877bool GrContext::updateCompressedBackendTexture(const GrBackendTexture& backendTexture,
878 const void* compressedData,
879 size_t dataSize,
880 GrGpuFinishedProc finishedProc,
881 GrGpuFinishedContext finishedContext) {
882 sk_sp<GrRefCntedCallback> finishedCallback;
883 if (finishedProc) {
884 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
885 }
886
887 if (!this->asDirectContext()) {
888 return false;
889 }
890
891 if (this->abandoned()) {
892 return false;
893 }
894
895 if (!compressedData) {
896 return false;
897 }
898
899 GrGpu::BackendTextureData data(compressedData, dataSize);
900
901 return fGpu->updateCompressedBackendTexture(backendTexture, std::move(finishedCallback), &data);
902}
903
904//////////////////////////////////////////////////////////////////////////////
905
Greg Daniel1db8e792020-06-09 17:29:32 -0400906bool GrContext::setBackendRenderTargetState(const GrBackendRenderTarget& backendRenderTarget,
907 const GrBackendSurfaceMutableState& state,
908 GrGpuFinishedProc finishedProc,
909 GrGpuFinishedContext finishedContext) {
Greg Daniel1db8e792020-06-09 17:29:32 -0400910 sk_sp<GrRefCntedCallback> callback;
911 if (finishedProc) {
912 callback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
913 }
Greg Daniel25597782020-06-11 13:15:08 -0400914
915 if (!this->asDirectContext()) {
916 return false;
917 }
918
919 if (this->abandoned()) {
920 return false;
921 }
922
Greg Daniel1db8e792020-06-09 17:29:32 -0400923 return fGpu->setBackendRenderTargetState(backendRenderTarget, state, std::move(callback));
924}
925
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400926void GrContext::deleteBackendTexture(GrBackendTexture backendTex) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400927 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danielf0e04f02019-12-04 15:17:54 -0500928 // For the Vulkan backend we still must destroy the backend texture when the context is
929 // abandoned.
930 if ((this->abandoned() && this->backend() != GrBackendApi::kVulkan) || !backendTex.isValid()) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400931 return;
932 }
933
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400934 fGpu->deleteBackendTexture(backendTex);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400935}
936
Greg Daniel1db8e792020-06-09 17:29:32 -0400937//////////////////////////////////////////////////////////////////////////////
938
Brian Osmaned58e002019-09-06 14:42:43 -0400939bool GrContext::precompileShader(const SkData& key, const SkData& data) {
940 return fGpu->precompileShader(key, data);
941}
942
Brian Salomonec22b1a2019-08-09 09:41:48 -0400943#ifdef SK_ENABLE_DUMP_GPU
Michael Ludwigdd205452020-03-30 17:16:34 -0400944#include "include/core/SkString.h"
Brian Salomonec22b1a2019-08-09 09:41:48 -0400945#include "src/utils/SkJSONWriter.h"
946SkString GrContext::dump() const {
947 SkDynamicMemoryWStream stream;
948 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
949 writer.beginObject();
950
951 writer.appendString("backend", GrBackendApiToStr(this->backend()));
952
953 writer.appendName("caps");
954 this->caps()->dumpJSON(&writer);
955
956 writer.appendName("gpu");
957 this->fGpu->dumpJSON(&writer);
958
Robert Phillips273f1072020-05-05 13:03:07 -0400959 writer.appendName("context");
960 this->dumpJSON(&writer);
961
Brian Salomonec22b1a2019-08-09 09:41:48 -0400962 // Flush JSON to the memory stream
963 writer.endObject();
964 writer.flush();
965
966 // Null terminate the JSON data in the memory stream
967 stream.write8(0);
968
969 // Allocate a string big enough to hold all the data, then copy out of the stream
970 SkString result(stream.bytesWritten());
971 stream.copyToAndReset(result.writable_str());
972 return result;
973}
974#endif