blob: 91f328fbaafec9fce099090d0e69fb5090484e51 [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"
Adlai Holler302e8fb2020-09-14 11:58:06 -040020#include "src/gpu/GrImageContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrMemoryPool.h"
22#include "src/gpu/GrPathRendererChain.h"
23#include "src/gpu/GrProxyProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040024#include "src/gpu/GrRenderTargetProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrResourceCache.h"
26#include "src/gpu/GrResourceProvider.h"
27#include "src/gpu/GrSemaphore.h"
Brian Osman5e7fbfd2019-05-03 13:13:35 -040028#include "src/gpu/GrShaderUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/GrSoftwarePathRenderer.h"
Robert Phillipsd464feb2020-10-08 11:00:02 -040030#include "src/gpu/GrThreadSafeCache.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/GrTracing.h"
32#include "src/gpu/SkGr.h"
33#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
34#include "src/gpu/effects/GrSkSLFP.h"
Herb Derbya08bde62020-06-12 15:46:06 -040035#include "src/gpu/text/GrSDFTOptions.h"
Robert Phillips41bd97d2020-04-07 14:19:37 -040036#include "src/gpu/text/GrStrikeCache.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "src/gpu/text/GrTextBlobCache.h"
Greg Daniel7fd7a8a2019-10-10 16:10:31 -040038#include "src/image/SkImage_GpuBase.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050039#include "src/image/SkSurface_Gpu.h"
Mike Klein0ec1c572018-12-04 11:52:51 -050040#include <atomic>
John Stilesfbd050b2020-08-03 13:21:46 -040041#include <memory>
Greg Danielb76a72a2017-07-13 15:07:54 -040042
Robert Phillipse78b7252017-04-06 07:59:41 -040043#define ASSERT_OWNED_PROXY(P) \
Brian Salomonfd98c2c2018-07-31 17:25:29 -040044 SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == this)
Robert Phillips7ee385e2017-03-30 08:02:11 -040045
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000046#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
Adlai Holler33dbd652020-06-01 12:35:42 -040047#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner())
Robert Phillipsa9162df2019-02-11 14:12:03 -050048#define RETURN_IF_ABANDONED if (this->abandoned()) { return; }
49#define RETURN_FALSE_IF_ABANDONED if (this->abandoned()) { return false; }
50#define RETURN_NULL_IF_ABANDONED if (this->abandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000051
robertphillipsea461502015-05-26 11:38:03 -070052////////////////////////////////////////////////////////////////////////////////
53
Adlai Hollere219d1c2020-06-02 11:23:16 -040054GrContext::GrContext(sk_sp<GrContextThreadSafeProxy> proxy) : INHERITED(std::move(proxy)) {
halcanary96fcdcc2015-08-27 07:41:13 -070055 fResourceCache = nullptr;
56 fResourceProvider = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000057}
58
Robert Phillips292a6b22019-02-14 14:49:02 -050059GrContext::~GrContext() {
60 ASSERT_SINGLE_OWNER
61
Adlai Holler96ead542020-06-26 08:50:14 -040062 this->destroyDrawingManager();
Greg Daniel5ed3c112020-06-18 15:59:17 -040063 fMappedBufferManager.reset();
Robert Phillips292a6b22019-02-14 14:49:02 -050064 delete fResourceProvider;
65 delete fResourceCache;
Robert Phillips292a6b22019-02-14 14:49:02 -050066}
67
Adlai Hollere219d1c2020-06-02 11:23:16 -040068bool GrContext::init() {
Greg Danielb76a72a2017-07-13 15:07:54 -040069 ASSERT_SINGLE_OWNER
Robert Phillipsa41c6852019-02-07 10:44:10 -050070 SkASSERT(this->proxyProvider());
Robert Phillips88260b52018-01-19 12:56:09 -050071
Adlai Hollere219d1c2020-06-02 11:23:16 -040072 if (!INHERITED::init()) {
Robert Phillipsbb606772019-02-04 17:50:57 -050073 return false;
74 }
75
Robert Phillips2184fb72019-02-21 16:11:41 -050076 SkASSERT(this->getTextBlobCache());
Robert Phillipsd464feb2020-10-08 11:00:02 -040077 SkASSERT(this->threadSafeCache());
Robert Phillipsbb606772019-02-04 17:50:57 -050078
Robert Phillips88260b52018-01-19 12:56:09 -050079 if (fGpu) {
John Stilesfbd050b2020-08-03 13:21:46 -040080 fStrikeCache = std::make_unique<GrStrikeCache>();
Robert Phillipsa41c6852019-02-07 10:44:10 -050081 fResourceCache = new GrResourceCache(this->caps(), this->singleOwner(), this->contextID());
Robert Phillips12c46292019-04-23 07:36:17 -040082 fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, this->singleOwner());
Mike Kleinf46d5ca2019-12-11 10:45:01 -050083 fMappedBufferManager = std::make_unique<GrClientMappedBufferManager>(this->contextID());
Robert Phillips88260b52018-01-19 12:56:09 -050084 }
85
Robert Phillips88260b52018-01-19 12:56:09 -050086 if (fResourceCache) {
Robert Phillipsa41c6852019-02-07 10:44:10 -050087 fResourceCache->setProxyProvider(this->proxyProvider());
Robert Phillipsd464feb2020-10-08 11:00:02 -040088 fResourceCache->setThreadSafeCache(this->threadSafeCache());
Robert Phillips88260b52018-01-19 12:56:09 -050089 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050090
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000091 fDidTestPMConversions = false;
92
Robert Phillipsfde6fa02018-03-02 08:53:14 -050093 // DDL TODO: we need to think through how the task group & persistent cache
94 // get passed on to/shared between all the DDLRecorders created with this context.
Robert Phillipsc1541ae2019-02-04 12:05:37 -050095 if (this->options().fExecutor) {
Mike Kleinf46d5ca2019-12-11 10:45:01 -050096 fTaskGroup = std::make_unique<SkTaskGroup>(*this->options().fExecutor);
Brian Osman51279982017-08-23 10:12:00 -040097 }
98
Robert Phillipsc1541ae2019-02-04 12:05:37 -050099 fPersistentCache = this->options().fPersistentCache;
Brian Osman5e7fbfd2019-05-03 13:13:35 -0400100 fShaderErrorHandler = this->options().fShaderErrorHandler;
101 if (!fShaderErrorHandler) {
102 fShaderErrorHandler = GrShaderUtils::DefaultShaderErrorHandler();
103 }
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400104
Brian Salomon91a3e522017-06-23 10:58:19 -0400105 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000106}
107
Robert Phillips4217ea72019-01-30 13:08:28 -0500108sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
Adlai Hollere219d1c2020-06-02 11:23:16 -0400109 return INHERITED::threadSafeProxy();
Robert Phillips4217ea72019-01-30 13:08:28 -0500110}
111
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400112//////////////////////////////////////////////////////////////////////////////
113
bsalomon2354f842014-07-28 13:48:36 -0700114void GrContext::abandonContext() {
Greg Daniel6e35a002020-04-01 13:29:59 -0400115 if (INHERITED::abandoned()) {
Robert Phillipsa9162df2019-02-11 14:12:03 -0500116 return;
117 }
joshualitt1de610a2016-01-06 08:26:09 -0800118
Robert Phillipsa9162df2019-02-11 14:12:03 -0500119 INHERITED::abandonContext();
120
Robert Phillips4d932d12020-04-09 08:58:52 -0400121 fStrikeCache->freeAll();
122
Brian Salomon9241a6d2019-10-03 13:26:54 -0400123 fMappedBufferManager->abandon();
124
bsalomond309e7a2015-04-30 14:18:54 -0700125 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800126
bsalomon@google.com205d4602011-04-25 12:43:45 +0000127 // abandon first to so destructors
128 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800129 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700130
bsalomon6e2aad42016-04-01 11:54:31 -0700131 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400132
133 fMappedBufferManager.reset();
Khushalc421ca12018-06-26 14:38:34 -0700134}
135
bsalomon6e2aad42016-04-01 11:54:31 -0700136void GrContext::releaseResourcesAndAbandonContext() {
Greg Daniel6e35a002020-04-01 13:29:59 -0400137 if (INHERITED::abandoned()) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500138 return;
139 }
Robert Phillipsa9162df2019-02-11 14:12:03 -0500140
141 INHERITED::abandonContext();
142
Brian Salomon9241a6d2019-10-03 13:26:54 -0400143 fMappedBufferManager.reset();
144
bsalomon6e2aad42016-04-01 11:54:31 -0700145 fResourceProvider->abandon();
146
bsalomon6e2aad42016-04-01 11:54:31 -0700147 // Release all resources in the backend 3D API.
148 fResourceCache->releaseAll();
149
150 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000151}
152
Greg Daniel6e35a002020-04-01 13:29:59 -0400153bool GrContext::abandoned() {
154 if (INHERITED::abandoned()) {
155 return true;
156 }
157
158 if (fGpu && fGpu->isDeviceLost()) {
159 this->abandonContext();
160 return true;
161 }
162 return false;
163}
164
Brian Salomon24069eb2020-06-24 10:19:52 -0400165bool GrContext::oomed() { return fGpu ? fGpu->checkAndResetOOMed() : false; }
166
Brian Salomon1f05d452019-02-08 12:33:08 -0500167void GrContext::resetGLTextureBindings() {
168 if (this->abandoned() || this->backend() != GrBackendApi::kOpenGL) {
169 return;
170 }
171 fGpu->resetTextureBindings();
172}
173
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000174void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800175 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000176 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000177}
178
179void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800180 ASSERT_SINGLE_OWNER
181
Adlai Holler9d8a41c2020-06-25 14:01:58 +0000182 if (this->abandoned()) {
183 return;
184 }
185
Robert Phillips2184fb72019-02-21 16:11:41 -0500186 // TODO: the glyph cache doesn't hold any GpuResources so this call should not be needed here.
187 // Some slack in the GrTextBlob's implementation requires it though. That could be fixed.
Robert Phillips4d932d12020-04-09 08:58:52 -0400188 fStrikeCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700189
Robert Phillips6a6de562019-02-15 15:19:15 -0500190 this->drawingManager()->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700191
192 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000193}
194
Robert Phillips6eba0632018-03-28 12:25:42 -0400195void GrContext::purgeUnlockedResources(bool scratchResourcesOnly) {
196 ASSERT_SINGLE_OWNER
Robert Phillipsddc21482019-10-16 14:30:09 -0400197
198 if (this->abandoned()) {
199 return;
200 }
201
Robert Phillips6eba0632018-03-28 12:25:42 -0400202 fResourceCache->purgeUnlockedResources(scratchResourcesOnly);
203 fResourceCache->purgeAsNeeded();
Robert Phillips2184fb72019-02-21 16:11:41 -0500204
205 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
206 // place to purge stale blobs
207 this->getTextBlobCache()->purgeStaleBlobs();
Robert Phillips6eba0632018-03-28 12:25:42 -0400208}
209
Jim Van Verth76d917c2017-12-13 09:26:37 -0500210void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Yuqian Li40aa85f2019-07-02 13:45:00 -0700211 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
212
Brian Salomon5e150852017-03-22 14:53:13 -0400213 ASSERT_SINGLE_OWNER
Chris Dalton6c3879d2018-11-01 11:13:19 -0600214
Brian Salomon9241a6d2019-10-03 13:26:54 -0400215 if (this->abandoned()) {
216 return;
217 }
218
Jim Van Verthe0da3792020-08-14 16:50:19 -0400219 this->checkAsyncWorkCompletion();
Brian Salomon9241a6d2019-10-03 13:26:54 -0400220 fMappedBufferManager->process();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600221 auto purgeTime = GrStdSteadyClock::now() - msNotUsed;
222
Jim Van Verth76d917c2017-12-13 09:26:37 -0500223 fResourceCache->purgeAsNeeded();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600224 fResourceCache->purgeResourcesNotUsedSince(purgeTime);
225
Robert Phillips6a6de562019-02-15 15:19:15 -0500226 if (auto ccpr = this->drawingManager()->getCoverageCountingPathRenderer()) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500227 ccpr->purgeCacheEntriesOlderThan(this->proxyProvider(), purgeTime);
Chris Dalton6c3879d2018-11-01 11:13:19 -0600228 }
Jim Van Verth76d917c2017-12-13 09:26:37 -0500229
Robert Phillips2184fb72019-02-21 16:11:41 -0500230 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
231 // place to purge stale blobs
232 this->getTextBlobCache()->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400233}
234
Derek Sollenberger5480a182017-05-25 16:43:59 -0400235void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
236 ASSERT_SINGLE_OWNER
Robert Phillipsddc21482019-10-16 14:30:09 -0400237
238 if (this->abandoned()) {
239 return;
240 }
241
Derek Sollenberger5480a182017-05-25 16:43:59 -0400242 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
243}
244
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000245void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800246 ASSERT_SINGLE_OWNER
247
bsalomon71cb0c22014-11-14 12:10:14 -0800248 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800249 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800250 }
251 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800252 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800253 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000254}
255
Derek Sollenbergeree479142017-05-24 11:41:33 -0400256size_t GrContext::getResourceCachePurgeableBytes() const {
257 ASSERT_SINGLE_OWNER
258 return fResourceCache->getPurgeableBytes();
259}
260
Brian Salomon7e67dca2020-07-21 09:27:25 -0400261size_t GrContext::ComputeImageSize(sk_sp<SkImage> image, GrMipmapped mipMapped, bool useNextPow2) {
Greg Daniel8b666172019-10-09 12:38:22 -0400262 if (!image->isTextureBacked()) {
263 return 0;
264 }
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400265 SkImage_GpuBase* gpuImage = static_cast<SkImage_GpuBase*>(as_IB(image.get()));
266 GrTextureProxy* proxy = gpuImage->peekProxy();
267 if (!proxy) {
268 return 0;
269 }
270
Greg Daniel8b666172019-10-09 12:38:22 -0400271 int colorSamplesPerPixel = 1;
Greg Daniel0eca74c2020-10-01 13:46:00 -0400272 return GrSurface::ComputeSize(proxy->backendFormat(), image->dimensions(),
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400273 colorSamplesPerPixel, mipMapped, useNextPow2);
Greg Daniel8b666172019-10-09 12:38:22 -0400274}
275
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000276////////////////////////////////////////////////////////////////////////////////
Greg Daniel414418d2020-07-08 11:44:25 -0400277bool GrContext::wait(int numSemaphores, const GrBackendSemaphore waitSemaphores[],
278 bool deleteSemaphoresAfterWait) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400279 if (!fGpu || fGpu->caps()->semaphoreSupport()) {
Greg Daniel06be0792019-04-22 15:53:23 -0400280 return false;
281 }
Greg Daniel414418d2020-07-08 11:44:25 -0400282 GrWrapOwnership ownership =
283 deleteSemaphoresAfterWait ? kAdopt_GrWrapOwnership : kBorrow_GrWrapOwnership;
Greg Daniel06be0792019-04-22 15:53:23 -0400284 for (int i = 0; i < numSemaphores; ++i) {
Greg Daniel301015c2019-11-18 14:06:46 -0500285 std::unique_ptr<GrSemaphore> sema = fResourceProvider->wrapBackendSemaphore(
Greg Daniel414418d2020-07-08 11:44:25 -0400286 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait, ownership);
Greg Daniel0106fcc2020-07-01 17:40:12 -0400287 // If we failed to wrap the semaphore it means the client didn't give us a valid semaphore
288 // to begin with. Therefore, it is fine to not wait on it.
289 if (sema) {
290 fGpu->waitSemaphore(sema.get());
291 }
Greg Daniel06be0792019-04-22 15:53:23 -0400292 }
293 return true;
294}
295
296////////////////////////////////////////////////////////////////////////////////
297
Greg Daniele8d3cca2020-06-10 10:04:48 -0400298GrSemaphoresSubmitted GrContext::flush(const GrFlushInfo& info) {
Greg Daniel51316782017-08-02 15:10:09 +0000299 ASSERT_SINGLE_OWNER
Robert Phillipsa9162df2019-02-11 14:12:03 -0500300 if (this->abandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400301 if (info.fFinishedProc) {
302 info.fFinishedProc(info.fFinishedContext);
303 }
304 if (info.fSubmittedProc) {
305 info.fSubmittedProc(info.fSubmittedContext, false);
306 }
Robert Phillipsa9162df2019-02-11 14:12:03 -0500307 return GrSemaphoresSubmitted::kNo;
308 }
Greg Daniel51316782017-08-02 15:10:09 +0000309
Greg Daniel04283f32020-05-20 13:16:00 -0400310 bool flushed = this->drawingManager()->flush(
Greg Daniel9efe3862020-06-11 11:51:06 -0400311 nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, info, nullptr);
Greg Danielfe159622020-04-10 17:43:51 +0000312
Greg Daniel04283f32020-05-20 13:16:00 -0400313 if (!flushed || (!this->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
Greg Danielfe159622020-04-10 17:43:51 +0000314 return GrSemaphoresSubmitted::kNo;
315 }
316 return GrSemaphoresSubmitted::kYes;
Greg Daniel51316782017-08-02 15:10:09 +0000317}
318
Greg Daniel04283f32020-05-20 13:16:00 -0400319bool GrContext::submit(bool syncCpu) {
320 ASSERT_SINGLE_OWNER
321 if (this->abandoned()) {
322 return false;
323 }
324
325 if (!fGpu) {
326 return false;
327 }
328
329 return fGpu->submitToGpu(syncCpu);
Greg Danielda50cb82020-05-13 14:07:40 -0400330}
331
Greg Daniela870b462019-01-08 15:49:46 -0500332////////////////////////////////////////////////////////////////////////////////
333
Brian Salomonb0d8b762019-05-06 16:58:22 -0400334void GrContext::checkAsyncWorkCompletion() {
335 if (fGpu) {
336 fGpu->checkFinishProcs();
337 }
338}
339
340////////////////////////////////////////////////////////////////////////////////
341
Greg Daniela870b462019-01-08 15:49:46 -0500342void GrContext::storeVkPipelineCacheData() {
343 if (fGpu) {
344 fGpu->storeVkPipelineCacheData();
345 }
346}
347
348////////////////////////////////////////////////////////////////////////////////
349
Khushal3e7548c2018-05-23 15:45:01 -0700350bool GrContext::supportsDistanceFieldText() const {
Robert Phillipsbb606772019-02-04 17:50:57 -0500351 return this->caps()->shaderCaps()->supportsDistanceFieldText();
Khushal3e7548c2018-05-23 15:45:01 -0700352}
353
bsalomon37f9a262015-02-02 13:00:10 -0800354//////////////////////////////////////////////////////////////////////////////
355
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500356void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800357 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500358 if (maxResources) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400359 *maxResources = -1;
bsalomon37f9a262015-02-02 13:00:10 -0800360 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500361 if (maxResourceBytes) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400362 *maxResourceBytes = this->getResourceCacheLimit();
bsalomon37f9a262015-02-02 13:00:10 -0800363 }
364}
365
Robert Phillipscf39f372019-09-03 10:29:20 -0400366size_t GrContext::getResourceCacheLimit() const {
joshualitt1de610a2016-01-06 08:26:09 -0800367 ASSERT_SINGLE_OWNER
Robert Phillipscf39f372019-09-03 10:29:20 -0400368 return fResourceCache->getMaxResourceBytes();
369}
370
371void GrContext::setResourceCacheLimits(int unused, size_t maxResourceBytes) {
372 ASSERT_SINGLE_OWNER
373 this->setResourceCacheLimit(maxResourceBytes);
374}
375
376void GrContext::setResourceCacheLimit(size_t maxResourceBytes) {
377 ASSERT_SINGLE_OWNER
378 fResourceCache->setLimit(maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800379}
380
ericrk0a5fa482015-09-15 14:16:10 -0700381//////////////////////////////////////////////////////////////////////////////
ericrk0a5fa482015-09-15 14:16:10 -0700382void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800383 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700384 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
Khushal71652e22018-10-29 13:05:36 -0700385 traceMemoryDump->dumpNumericValue("skia/gr_text_blob_cache", "size", "bytes",
Robert Phillips2184fb72019-02-21 16:11:41 -0500386 this->getTextBlobCache()->usedBytes());
ericrk0a5fa482015-09-15 14:16:10 -0700387}
Brian Osman71a18892017-08-10 10:23:25 -0400388
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400389//////////////////////////////////////////////////////////////////////////////
390GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400391 const GrBackendFormat& backendFormat,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400392 GrMipmapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400393 GrRenderable renderable,
394 GrProtected isProtected) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400395 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400396 if (!this->asDirectContext()) {
397 return GrBackendTexture();
398 }
399
400 if (this->abandoned()) {
401 return GrBackendTexture();
402 }
403
Robert Phillips4277f012020-01-21 14:28:34 -0500404 return fGpu->createBackendTexture({width, height}, backendFormat, renderable,
Greg Daniel16032b32020-05-06 15:31:10 -0400405 mipMapped, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400406}
407
408GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400409 SkColorType skColorType,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400410 GrMipmapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400411 GrRenderable renderable,
412 GrProtected isProtected) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400413 if (!this->asDirectContext()) {
414 return GrBackendTexture();
415 }
416
417 if (this->abandoned()) {
418 return GrBackendTexture();
419 }
420
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400421 const GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400422
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400423 return this->createBackendTexture(width, height, format, mipMapped, renderable, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400424}
425
Greg Daniel25597782020-06-11 13:15:08 -0400426static GrBackendTexture create_and_update_backend_texture(
Robert Phillips4e105e22020-07-16 09:18:50 -0400427 GrDirectContext* context,
Greg Daniel25597782020-06-11 13:15:08 -0400428 SkISize dimensions,
429 const GrBackendFormat& backendFormat,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400430 GrMipmapped mipMapped,
Greg Daniel25597782020-06-11 13:15:08 -0400431 GrRenderable renderable,
432 GrProtected isProtected,
433 sk_sp<GrRefCntedCallback> finishedCallback,
434 const GrGpu::BackendTextureData* data) {
Greg Daniel16032b32020-05-06 15:31:10 -0400435 GrGpu* gpu = context->priv().getGpu();
436
437 GrBackendTexture beTex = gpu->createBackendTexture(dimensions, backendFormat, renderable,
438 mipMapped, isProtected);
439 if (!beTex.isValid()) {
440 return {};
441 }
442
Greg Daniel25597782020-06-11 13:15:08 -0400443 if (!context->priv().getGpu()->updateBackendTexture(beTex, std::move(finishedCallback), data)) {
Greg Daniel16032b32020-05-06 15:31:10 -0400444 context->deleteBackendTexture(beTex);
445 return {};
446 }
447 return beTex;
448}
449
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400450GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400451 const GrBackendFormat& backendFormat,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400452 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400453 GrMipmapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400454 GrRenderable renderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400455 GrProtected isProtected,
456 GrGpuFinishedProc finishedProc,
457 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400458 sk_sp<GrRefCntedCallback> finishedCallback;
459 if (finishedProc) {
460 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
461 }
462
Brian Salomonc42eb662019-06-24 17:13:00 -0400463 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400464 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400465 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400466 }
467
468 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400469 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400470 }
471
Brian Salomon85c3d682019-11-04 15:04:54 -0500472 GrGpu::BackendTextureData data(color);
Robert Phillips4e105e22020-07-16 09:18:50 -0400473 return create_and_update_backend_texture(this->asDirectContext(), {width, height},
474 backendFormat, mipMapped, renderable, isProtected,
475 std::move(finishedCallback), &data);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400476}
477
478GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400479 SkColorType skColorType,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400480 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400481 GrMipmapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400482 GrRenderable renderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400483 GrProtected isProtected,
484 GrGpuFinishedProc finishedProc,
485 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400486 sk_sp<GrRefCntedCallback> finishedCallback;
487 if (finishedProc) {
488 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
489 }
490
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400491 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400492 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400493 }
494
495 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400496 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400497 }
498
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400499 GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400500 if (!format.isValid()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400501 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400502 }
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400503
504 GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
Brian Salomon982f5462020-03-30 12:52:33 -0400505 SkColor4f swizzledColor = this->caps()->getWriteSwizzle(format, grColorType).applyTo(color);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400506
Greg Daniel25597782020-06-11 13:15:08 -0400507 GrGpu::BackendTextureData data(swizzledColor);
Robert Phillips4e105e22020-07-16 09:18:50 -0400508 return create_and_update_backend_texture(this->asDirectContext(), {width, height}, format,
509 mipMapped, renderable, isProtected,
510 std::move(finishedCallback), &data);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400511}
512
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500513GrBackendTexture GrContext::createBackendTexture(const SkPixmap srcData[], int numProvidedLevels,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400514 GrRenderable renderable, GrProtected isProtected,
515 GrGpuFinishedProc finishedProc,
516 GrGpuFinishedContext finishedContext) {
Robert Phillips66944402019-09-30 13:21:25 -0400517 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
518
Greg Daniel25597782020-06-11 13:15:08 -0400519 sk_sp<GrRefCntedCallback> finishedCallback;
520 if (finishedProc) {
521 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
522 }
523
Robert Phillips66944402019-09-30 13:21:25 -0400524 if (!this->asDirectContext()) {
525 return {};
526 }
527
528 if (this->abandoned()) {
529 return {};
530 }
531
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500532 if (!srcData || numProvidedLevels <= 0) {
Robert Phillips66944402019-09-30 13:21:25 -0400533 return {};
534 }
535
536 int baseWidth = srcData[0].width();
537 int baseHeight = srcData[0].height();
538 SkColorType colorType = srcData[0].colorType();
539
Brian Salomon7e67dca2020-07-21 09:27:25 -0400540 GrMipmapped mipMapped = GrMipmapped::kNo;
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500541 int numExpectedLevels = 1;
542 if (numProvidedLevels > 1) {
Mike Reed13711eb2020-07-14 17:16:32 -0400543 numExpectedLevels = SkMipmap::ComputeLevelCount(baseWidth, baseHeight) + 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400544 mipMapped = GrMipmapped::kYes;
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500545 }
546
547 if (numProvidedLevels != numExpectedLevels) {
548 return {};
549 }
550
Robert Phillips66944402019-09-30 13:21:25 -0400551 GrBackendFormat backendFormat = this->defaultBackendFormat(colorType, renderable);
552
Brian Salomon85c3d682019-11-04 15:04:54 -0500553 GrGpu::BackendTextureData data(srcData);
Robert Phillips4e105e22020-07-16 09:18:50 -0400554 return create_and_update_backend_texture(this->asDirectContext(), {baseWidth, baseHeight},
555 backendFormat, mipMapped, renderable, isProtected,
Greg Daniel25597782020-06-11 13:15:08 -0400556 std::move(finishedCallback), &data);
Robert Phillips66944402019-09-30 13:21:25 -0400557}
558
Greg Danielb2365d82020-05-13 15:32:04 -0400559bool GrContext::updateBackendTexture(const GrBackendTexture& backendTexture,
560 const SkColor4f& color,
561 GrGpuFinishedProc finishedProc,
562 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400563 sk_sp<GrRefCntedCallback> finishedCallback;
564 if (finishedProc) {
565 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
566 }
567
Greg Danielb2365d82020-05-13 15:32:04 -0400568 if (!this->asDirectContext()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400569 return false;
570 }
571
572 if (this->abandoned()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400573 return false;
574 }
575
576 GrGpu::BackendTextureData data(color);
Greg Daniel25597782020-06-11 13:15:08 -0400577 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
Greg Danielb2365d82020-05-13 15:32:04 -0400578}
579
580bool GrContext::updateBackendTexture(const GrBackendTexture& backendTexture,
Greg Daniel373d7dd2020-07-21 10:41:50 -0400581 SkColorType skColorType,
582 const SkColor4f& color,
583 GrGpuFinishedProc finishedProc,
584 GrGpuFinishedContext finishedContext) {
585 sk_sp<GrRefCntedCallback> finishedCallback;
586 if (finishedProc) {
587 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
588 }
589
590 if (!this->asDirectContext()) {
591 return false;
592 }
593
594 if (this->abandoned()) {
595 return false;
596 }
597
598 GrBackendFormat format = backendTexture.getBackendFormat();
599 GrColorType grColorType = SkColorTypeAndFormatToGrColorType(this->caps(), skColorType, format);
600
601 if (!this->caps()->areColorTypeAndFormatCompatible(grColorType, format)) {
602 return false;
603 }
604
605 GrSwizzle swizzle = this->caps()->getWriteSwizzle(format, grColorType);
606 GrGpu::BackendTextureData data(swizzle.applyTo(color));
607
608 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
609}
610
611bool GrContext::updateBackendTexture(const GrBackendTexture& backendTexture,
Greg Danielb2365d82020-05-13 15:32:04 -0400612 const SkPixmap srcData[],
613 int numLevels,
614 GrGpuFinishedProc finishedProc,
615 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400616 sk_sp<GrRefCntedCallback> finishedCallback;
617 if (finishedProc) {
618 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
619 }
620
Greg Danielb2365d82020-05-13 15:32:04 -0400621 if (!this->asDirectContext()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400622 return false;
623 }
624
625 if (this->abandoned()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400626 return false;
627 }
628
629 if (!srcData || numLevels <= 0) {
Greg Danielb2365d82020-05-13 15:32:04 -0400630 return false;
631 }
632
633 int numExpectedLevels = 1;
Brian Salomon40a40622020-07-21 10:32:07 -0400634 if (backendTexture.hasMipmaps()) {
Mike Reed13711eb2020-07-14 17:16:32 -0400635 numExpectedLevels = SkMipmap::ComputeLevelCount(backendTexture.width(),
Greg Danielb2365d82020-05-13 15:32:04 -0400636 backendTexture.height()) + 1;
637 }
638 if (numLevels != numExpectedLevels) {
Greg Danielb2365d82020-05-13 15:32:04 -0400639 return false;
640 }
641
642 GrGpu::BackendTextureData data(srcData);
Greg Daniel25597782020-06-11 13:15:08 -0400643 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
Greg Danielb2365d82020-05-13 15:32:04 -0400644}
645
Robert Phillipsb915c942019-12-17 14:44:37 -0500646//////////////////////////////////////////////////////////////////////////////
647
Greg Danielaaf738c2020-07-10 09:30:33 -0400648static GrBackendTexture create_and_update_compressed_backend_texture(
Robert Phillips4e105e22020-07-16 09:18:50 -0400649 GrDirectContext* context,
Greg Danielaaf738c2020-07-10 09:30:33 -0400650 SkISize dimensions,
651 const GrBackendFormat& backendFormat,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400652 GrMipmapped mipMapped,
Greg Danielaaf738c2020-07-10 09:30:33 -0400653 GrProtected isProtected,
654 sk_sp<GrRefCntedCallback> finishedCallback,
655 const GrGpu::BackendTextureData* data) {
656 GrGpu* gpu = context->priv().getGpu();
657
658 GrBackendTexture beTex = gpu->createCompressedBackendTexture(dimensions, backendFormat,
659 mipMapped, isProtected);
660 if (!beTex.isValid()) {
661 return {};
662 }
663
664 if (!context->priv().getGpu()->updateCompressedBackendTexture(
665 beTex, std::move(finishedCallback), data)) {
666 context->deleteBackendTexture(beTex);
667 return {};
668 }
669 return beTex;
670}
671
Robert Phillipsb915c942019-12-17 14:44:37 -0500672GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
673 const GrBackendFormat& backendFormat,
674 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400675 GrMipmapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400676 GrProtected isProtected,
677 GrGpuFinishedProc finishedProc,
678 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500679 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel25597782020-06-11 13:15:08 -0400680 sk_sp<GrRefCntedCallback> finishedCallback;
681 if (finishedProc) {
682 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
683 }
684
Robert Phillipsb915c942019-12-17 14:44:37 -0500685 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400686 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500687 }
688
689 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400690 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500691 }
692
693 GrGpu::BackendTextureData data(color);
Robert Phillips4e105e22020-07-16 09:18:50 -0400694 return create_and_update_compressed_backend_texture(this->asDirectContext(), {width, height},
695 backendFormat, mipMapped, isProtected,
Greg Danielaaf738c2020-07-10 09:30:33 -0400696 std::move(finishedCallback), &data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500697}
698
699GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
700 SkImage::CompressionType compression,
701 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400702 GrMipmapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400703 GrProtected isProtected,
704 GrGpuFinishedProc finishedProc,
705 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500706 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillipsb915c942019-12-17 14:44:37 -0500707 GrBackendFormat format = this->compressedBackendFormat(compression);
708 return this->createCompressedBackendTexture(width, height, format, color,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400709 mipMapped, isProtected, finishedProc,
710 finishedContext);
Robert Phillipsb915c942019-12-17 14:44:37 -0500711}
712
713GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
714 const GrBackendFormat& backendFormat,
715 const void* compressedData,
716 size_t dataSize,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400717 GrMipmapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400718 GrProtected isProtected,
719 GrGpuFinishedProc finishedProc,
720 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500721 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel25597782020-06-11 13:15:08 -0400722 sk_sp<GrRefCntedCallback> finishedCallback;
723 if (finishedProc) {
724 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
725 }
726
Robert Phillipsb915c942019-12-17 14:44:37 -0500727 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400728 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500729 }
730
731 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400732 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500733 }
734
735 GrGpu::BackendTextureData data(compressedData, dataSize);
Robert Phillips4e105e22020-07-16 09:18:50 -0400736 return create_and_update_compressed_backend_texture(this->asDirectContext(), {width, height},
737 backendFormat, mipMapped, isProtected,
Greg Danielaaf738c2020-07-10 09:30:33 -0400738 std::move(finishedCallback), &data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500739}
740
741GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
742 SkImage::CompressionType compression,
743 const void* data, size_t dataSize,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400744 GrMipmapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400745 GrProtected isProtected,
746 GrGpuFinishedProc finishedProc,
747 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500748 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillipsb915c942019-12-17 14:44:37 -0500749 GrBackendFormat format = this->compressedBackendFormat(compression);
Greg Daniel25597782020-06-11 13:15:08 -0400750 return this->createCompressedBackendTexture(width, height, format, data, dataSize, mipMapped,
751 isProtected, finishedProc, finishedContext);
Robert Phillipsb915c942019-12-17 14:44:37 -0500752}
753
Greg Daniel1db8e792020-06-09 17:29:32 -0400754bool GrContext::setBackendTextureState(const GrBackendTexture& backendTexture,
755 const GrBackendSurfaceMutableState& state,
Greg Daniel1d3c8c12020-09-23 14:23:36 -0400756 GrBackendSurfaceMutableState* previousState,
Greg Daniel1db8e792020-06-09 17:29:32 -0400757 GrGpuFinishedProc finishedProc,
758 GrGpuFinishedContext finishedContext) {
Greg Daniel1db8e792020-06-09 17:29:32 -0400759 sk_sp<GrRefCntedCallback> callback;
760 if (finishedProc) {
761 callback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
762 }
Greg Daniel25597782020-06-11 13:15:08 -0400763
764 if (!this->asDirectContext()) {
765 return false;
766 }
767
768 if (this->abandoned()) {
769 return false;
770 }
771
Greg Daniel1d3c8c12020-09-23 14:23:36 -0400772 return fGpu->setBackendTextureState(backendTexture, state, previousState, std::move(callback));
Greg Daniel1db8e792020-06-09 17:29:32 -0400773}
774
Greg Daniel95afafb2020-07-22 12:09:26 -0400775bool GrContext::updateCompressedBackendTexture(const GrBackendTexture& backendTexture,
776 const SkColor4f& color,
777 GrGpuFinishedProc finishedProc,
778 GrGpuFinishedContext finishedContext) {
779 sk_sp<GrRefCntedCallback> finishedCallback;
780 if (finishedProc) {
781 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
782 }
783
784 if (!this->asDirectContext()) {
785 return false;
786 }
787
788 if (this->abandoned()) {
789 return false;
790 }
791
792 GrGpu::BackendTextureData data(color);
793 return fGpu->updateCompressedBackendTexture(backendTexture, std::move(finishedCallback), &data);
794}
795
796bool GrContext::updateCompressedBackendTexture(const GrBackendTexture& backendTexture,
797 const void* compressedData,
798 size_t dataSize,
799 GrGpuFinishedProc finishedProc,
800 GrGpuFinishedContext finishedContext) {
801 sk_sp<GrRefCntedCallback> finishedCallback;
802 if (finishedProc) {
803 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
804 }
805
806 if (!this->asDirectContext()) {
807 return false;
808 }
809
810 if (this->abandoned()) {
811 return false;
812 }
813
814 if (!compressedData) {
815 return false;
816 }
817
818 GrGpu::BackendTextureData data(compressedData, dataSize);
819
820 return fGpu->updateCompressedBackendTexture(backendTexture, std::move(finishedCallback), &data);
821}
822
823//////////////////////////////////////////////////////////////////////////////
824
Greg Daniel1db8e792020-06-09 17:29:32 -0400825bool GrContext::setBackendRenderTargetState(const GrBackendRenderTarget& backendRenderTarget,
826 const GrBackendSurfaceMutableState& state,
Greg Daniel1d3c8c12020-09-23 14:23:36 -0400827 GrBackendSurfaceMutableState* previousState,
Greg Daniel1db8e792020-06-09 17:29:32 -0400828 GrGpuFinishedProc finishedProc,
829 GrGpuFinishedContext finishedContext) {
Greg Daniel1db8e792020-06-09 17:29:32 -0400830 sk_sp<GrRefCntedCallback> callback;
831 if (finishedProc) {
832 callback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
833 }
Greg Daniel25597782020-06-11 13:15:08 -0400834
835 if (!this->asDirectContext()) {
836 return false;
837 }
838
839 if (this->abandoned()) {
840 return false;
841 }
842
Greg Daniel1d3c8c12020-09-23 14:23:36 -0400843 return fGpu->setBackendRenderTargetState(backendRenderTarget, state, previousState,
844 std::move(callback));
Greg Daniel1db8e792020-06-09 17:29:32 -0400845}
846
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400847void GrContext::deleteBackendTexture(GrBackendTexture backendTex) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400848 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danielf0e04f02019-12-04 15:17:54 -0500849 // For the Vulkan backend we still must destroy the backend texture when the context is
850 // abandoned.
851 if ((this->abandoned() && this->backend() != GrBackendApi::kVulkan) || !backendTex.isValid()) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400852 return;
853 }
854
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400855 fGpu->deleteBackendTexture(backendTex);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400856}
857
Greg Daniel1db8e792020-06-09 17:29:32 -0400858//////////////////////////////////////////////////////////////////////////////
859
Brian Osmaned58e002019-09-06 14:42:43 -0400860bool GrContext::precompileShader(const SkData& key, const SkData& data) {
861 return fGpu->precompileShader(key, data);
862}
863
Brian Salomonec22b1a2019-08-09 09:41:48 -0400864#ifdef SK_ENABLE_DUMP_GPU
Michael Ludwigdd205452020-03-30 17:16:34 -0400865#include "include/core/SkString.h"
Brian Salomonec22b1a2019-08-09 09:41:48 -0400866#include "src/utils/SkJSONWriter.h"
867SkString GrContext::dump() const {
868 SkDynamicMemoryWStream stream;
869 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
870 writer.beginObject();
871
872 writer.appendString("backend", GrBackendApiToStr(this->backend()));
873
874 writer.appendName("caps");
875 this->caps()->dumpJSON(&writer);
876
877 writer.appendName("gpu");
878 this->fGpu->dumpJSON(&writer);
879
Robert Phillips273f1072020-05-05 13:03:07 -0400880 writer.appendName("context");
881 this->dumpJSON(&writer);
882
Brian Salomonec22b1a2019-08-09 09:41:48 -0400883 // Flush JSON to the memory stream
884 writer.endObject();
885 writer.flush();
886
887 // Null terminate the JSON data in the memory stream
888 stream.write8(0);
889
890 // Allocate a string big enough to hold all the data, then copy out of the stream
891 SkString result(stream.bytesWritten());
892 stream.copyToAndReset(result.writable_str());
893 return result;
894}
895#endif