blob: 96edd6fba2844c86fa07b2e2c7f4c05240bef1a7 [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 Phillips12d06a32020-09-16 12:31:34 -040030#include "src/gpu/GrThreadSafeUniquelyKeyedProxyViewCache.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 Phillips12d06a32020-09-16 12:31:34 -040077 SkASSERT(this->threadSafeViewCache());
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 Phillips12d06a32020-09-16 12:31:34 -040088 fResourceCache->setThreadSafeViewCache(this->threadSafeViewCache());
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
271 const GrCaps& caps = *gpuImage->context()->priv().caps();
Greg Daniel8b666172019-10-09 12:38:22 -0400272 int colorSamplesPerPixel = 1;
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400273 return GrSurface::ComputeSize(caps, proxy->backendFormat(), image->dimensions(),
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400274 colorSamplesPerPixel, mipMapped, useNextPow2);
Greg Daniel8b666172019-10-09 12:38:22 -0400275}
276
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000277////////////////////////////////////////////////////////////////////////////////
Greg Daniel414418d2020-07-08 11:44:25 -0400278bool GrContext::wait(int numSemaphores, const GrBackendSemaphore waitSemaphores[],
279 bool deleteSemaphoresAfterWait) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400280 if (!fGpu || fGpu->caps()->semaphoreSupport()) {
Greg Daniel06be0792019-04-22 15:53:23 -0400281 return false;
282 }
Greg Daniel414418d2020-07-08 11:44:25 -0400283 GrWrapOwnership ownership =
284 deleteSemaphoresAfterWait ? kAdopt_GrWrapOwnership : kBorrow_GrWrapOwnership;
Greg Daniel06be0792019-04-22 15:53:23 -0400285 for (int i = 0; i < numSemaphores; ++i) {
Greg Daniel301015c2019-11-18 14:06:46 -0500286 std::unique_ptr<GrSemaphore> sema = fResourceProvider->wrapBackendSemaphore(
Greg Daniel414418d2020-07-08 11:44:25 -0400287 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait, ownership);
Greg Daniel0106fcc2020-07-01 17:40:12 -0400288 // If we failed to wrap the semaphore it means the client didn't give us a valid semaphore
289 // to begin with. Therefore, it is fine to not wait on it.
290 if (sema) {
291 fGpu->waitSemaphore(sema.get());
292 }
Greg Daniel06be0792019-04-22 15:53:23 -0400293 }
294 return true;
295}
296
297////////////////////////////////////////////////////////////////////////////////
298
Greg Daniele8d3cca2020-06-10 10:04:48 -0400299GrSemaphoresSubmitted GrContext::flush(const GrFlushInfo& info) {
Greg Daniel51316782017-08-02 15:10:09 +0000300 ASSERT_SINGLE_OWNER
Robert Phillipsa9162df2019-02-11 14:12:03 -0500301 if (this->abandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400302 if (info.fFinishedProc) {
303 info.fFinishedProc(info.fFinishedContext);
304 }
305 if (info.fSubmittedProc) {
306 info.fSubmittedProc(info.fSubmittedContext, false);
307 }
Robert Phillipsa9162df2019-02-11 14:12:03 -0500308 return GrSemaphoresSubmitted::kNo;
309 }
Greg Daniel51316782017-08-02 15:10:09 +0000310
Greg Daniel04283f32020-05-20 13:16:00 -0400311 bool flushed = this->drawingManager()->flush(
Greg Daniel9efe3862020-06-11 11:51:06 -0400312 nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, info, nullptr);
Greg Danielfe159622020-04-10 17:43:51 +0000313
Greg Daniel04283f32020-05-20 13:16:00 -0400314 if (!flushed || (!this->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
Greg Danielfe159622020-04-10 17:43:51 +0000315 return GrSemaphoresSubmitted::kNo;
316 }
317 return GrSemaphoresSubmitted::kYes;
Greg Daniel51316782017-08-02 15:10:09 +0000318}
319
Greg Daniel04283f32020-05-20 13:16:00 -0400320bool GrContext::submit(bool syncCpu) {
321 ASSERT_SINGLE_OWNER
322 if (this->abandoned()) {
323 return false;
324 }
325
326 if (!fGpu) {
327 return false;
328 }
329
330 return fGpu->submitToGpu(syncCpu);
Greg Danielda50cb82020-05-13 14:07:40 -0400331}
332
Greg Daniela870b462019-01-08 15:49:46 -0500333////////////////////////////////////////////////////////////////////////////////
334
Brian Salomonb0d8b762019-05-06 16:58:22 -0400335void GrContext::checkAsyncWorkCompletion() {
336 if (fGpu) {
337 fGpu->checkFinishProcs();
338 }
339}
340
341////////////////////////////////////////////////////////////////////////////////
342
Greg Daniela870b462019-01-08 15:49:46 -0500343void GrContext::storeVkPipelineCacheData() {
344 if (fGpu) {
345 fGpu->storeVkPipelineCacheData();
346 }
347}
348
349////////////////////////////////////////////////////////////////////////////////
350
Khushal3e7548c2018-05-23 15:45:01 -0700351bool GrContext::supportsDistanceFieldText() const {
Robert Phillipsbb606772019-02-04 17:50:57 -0500352 return this->caps()->shaderCaps()->supportsDistanceFieldText();
Khushal3e7548c2018-05-23 15:45:01 -0700353}
354
bsalomon37f9a262015-02-02 13:00:10 -0800355//////////////////////////////////////////////////////////////////////////////
356
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500357void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800358 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500359 if (maxResources) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400360 *maxResources = -1;
bsalomon37f9a262015-02-02 13:00:10 -0800361 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500362 if (maxResourceBytes) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400363 *maxResourceBytes = this->getResourceCacheLimit();
bsalomon37f9a262015-02-02 13:00:10 -0800364 }
365}
366
Robert Phillipscf39f372019-09-03 10:29:20 -0400367size_t GrContext::getResourceCacheLimit() const {
joshualitt1de610a2016-01-06 08:26:09 -0800368 ASSERT_SINGLE_OWNER
Robert Phillipscf39f372019-09-03 10:29:20 -0400369 return fResourceCache->getMaxResourceBytes();
370}
371
372void GrContext::setResourceCacheLimits(int unused, size_t maxResourceBytes) {
373 ASSERT_SINGLE_OWNER
374 this->setResourceCacheLimit(maxResourceBytes);
375}
376
377void GrContext::setResourceCacheLimit(size_t maxResourceBytes) {
378 ASSERT_SINGLE_OWNER
379 fResourceCache->setLimit(maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800380}
381
ericrk0a5fa482015-09-15 14:16:10 -0700382//////////////////////////////////////////////////////////////////////////////
ericrk0a5fa482015-09-15 14:16:10 -0700383void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800384 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700385 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
Khushal71652e22018-10-29 13:05:36 -0700386 traceMemoryDump->dumpNumericValue("skia/gr_text_blob_cache", "size", "bytes",
Robert Phillips2184fb72019-02-21 16:11:41 -0500387 this->getTextBlobCache()->usedBytes());
ericrk0a5fa482015-09-15 14:16:10 -0700388}
Brian Osman71a18892017-08-10 10:23:25 -0400389
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400390//////////////////////////////////////////////////////////////////////////////
391GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400392 const GrBackendFormat& backendFormat,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400393 GrMipmapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400394 GrRenderable renderable,
395 GrProtected isProtected) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400396 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400397 if (!this->asDirectContext()) {
398 return GrBackendTexture();
399 }
400
401 if (this->abandoned()) {
402 return GrBackendTexture();
403 }
404
Robert Phillips4277f012020-01-21 14:28:34 -0500405 return fGpu->createBackendTexture({width, height}, backendFormat, renderable,
Greg Daniel16032b32020-05-06 15:31:10 -0400406 mipMapped, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400407}
408
409GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400410 SkColorType skColorType,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400411 GrMipmapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400412 GrRenderable renderable,
413 GrProtected isProtected) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400414 if (!this->asDirectContext()) {
415 return GrBackendTexture();
416 }
417
418 if (this->abandoned()) {
419 return GrBackendTexture();
420 }
421
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400422 const GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400423
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400424 return this->createBackendTexture(width, height, format, mipMapped, renderable, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400425}
426
Greg Daniel25597782020-06-11 13:15:08 -0400427static GrBackendTexture create_and_update_backend_texture(
Robert Phillips4e105e22020-07-16 09:18:50 -0400428 GrDirectContext* context,
Greg Daniel25597782020-06-11 13:15:08 -0400429 SkISize dimensions,
430 const GrBackendFormat& backendFormat,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400431 GrMipmapped mipMapped,
Greg Daniel25597782020-06-11 13:15:08 -0400432 GrRenderable renderable,
433 GrProtected isProtected,
434 sk_sp<GrRefCntedCallback> finishedCallback,
435 const GrGpu::BackendTextureData* data) {
Greg Daniel16032b32020-05-06 15:31:10 -0400436 GrGpu* gpu = context->priv().getGpu();
437
438 GrBackendTexture beTex = gpu->createBackendTexture(dimensions, backendFormat, renderable,
439 mipMapped, isProtected);
440 if (!beTex.isValid()) {
441 return {};
442 }
443
Greg Daniel25597782020-06-11 13:15:08 -0400444 if (!context->priv().getGpu()->updateBackendTexture(beTex, std::move(finishedCallback), data)) {
Greg Daniel16032b32020-05-06 15:31:10 -0400445 context->deleteBackendTexture(beTex);
446 return {};
447 }
448 return beTex;
449}
450
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400451GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400452 const GrBackendFormat& backendFormat,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400453 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400454 GrMipmapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400455 GrRenderable renderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400456 GrProtected isProtected,
457 GrGpuFinishedProc finishedProc,
458 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400459 sk_sp<GrRefCntedCallback> finishedCallback;
460 if (finishedProc) {
461 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
462 }
463
Brian Salomonc42eb662019-06-24 17:13:00 -0400464 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400465 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400466 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400467 }
468
469 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400470 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400471 }
472
Brian Salomon85c3d682019-11-04 15:04:54 -0500473 GrGpu::BackendTextureData data(color);
Robert Phillips4e105e22020-07-16 09:18:50 -0400474 return create_and_update_backend_texture(this->asDirectContext(), {width, height},
475 backendFormat, mipMapped, renderable, isProtected,
476 std::move(finishedCallback), &data);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400477}
478
479GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400480 SkColorType skColorType,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400481 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400482 GrMipmapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400483 GrRenderable renderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400484 GrProtected isProtected,
485 GrGpuFinishedProc finishedProc,
486 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400487 sk_sp<GrRefCntedCallback> finishedCallback;
488 if (finishedProc) {
489 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
490 }
491
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400492 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400493 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400494 }
495
496 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400497 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400498 }
499
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400500 GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400501 if (!format.isValid()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400502 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400503 }
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400504
505 GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
Brian Salomon982f5462020-03-30 12:52:33 -0400506 SkColor4f swizzledColor = this->caps()->getWriteSwizzle(format, grColorType).applyTo(color);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400507
Greg Daniel25597782020-06-11 13:15:08 -0400508 GrGpu::BackendTextureData data(swizzledColor);
Robert Phillips4e105e22020-07-16 09:18:50 -0400509 return create_and_update_backend_texture(this->asDirectContext(), {width, height}, format,
510 mipMapped, renderable, isProtected,
511 std::move(finishedCallback), &data);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400512}
513
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500514GrBackendTexture GrContext::createBackendTexture(const SkPixmap srcData[], int numProvidedLevels,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400515 GrRenderable renderable, GrProtected isProtected,
516 GrGpuFinishedProc finishedProc,
517 GrGpuFinishedContext finishedContext) {
Robert Phillips66944402019-09-30 13:21:25 -0400518 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
519
Greg Daniel25597782020-06-11 13:15:08 -0400520 sk_sp<GrRefCntedCallback> finishedCallback;
521 if (finishedProc) {
522 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
523 }
524
Robert Phillips66944402019-09-30 13:21:25 -0400525 if (!this->asDirectContext()) {
526 return {};
527 }
528
529 if (this->abandoned()) {
530 return {};
531 }
532
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500533 if (!srcData || numProvidedLevels <= 0) {
Robert Phillips66944402019-09-30 13:21:25 -0400534 return {};
535 }
536
537 int baseWidth = srcData[0].width();
538 int baseHeight = srcData[0].height();
539 SkColorType colorType = srcData[0].colorType();
540
Brian Salomon7e67dca2020-07-21 09:27:25 -0400541 GrMipmapped mipMapped = GrMipmapped::kNo;
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500542 int numExpectedLevels = 1;
543 if (numProvidedLevels > 1) {
Mike Reed13711eb2020-07-14 17:16:32 -0400544 numExpectedLevels = SkMipmap::ComputeLevelCount(baseWidth, baseHeight) + 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400545 mipMapped = GrMipmapped::kYes;
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500546 }
547
548 if (numProvidedLevels != numExpectedLevels) {
549 return {};
550 }
551
Robert Phillips66944402019-09-30 13:21:25 -0400552 GrBackendFormat backendFormat = this->defaultBackendFormat(colorType, renderable);
553
Brian Salomon85c3d682019-11-04 15:04:54 -0500554 GrGpu::BackendTextureData data(srcData);
Robert Phillips4e105e22020-07-16 09:18:50 -0400555 return create_and_update_backend_texture(this->asDirectContext(), {baseWidth, baseHeight},
556 backendFormat, mipMapped, renderable, isProtected,
Greg Daniel25597782020-06-11 13:15:08 -0400557 std::move(finishedCallback), &data);
Robert Phillips66944402019-09-30 13:21:25 -0400558}
559
Greg Danielb2365d82020-05-13 15:32:04 -0400560bool GrContext::updateBackendTexture(const GrBackendTexture& backendTexture,
561 const SkColor4f& color,
562 GrGpuFinishedProc finishedProc,
563 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400564 sk_sp<GrRefCntedCallback> finishedCallback;
565 if (finishedProc) {
566 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
567 }
568
Greg Danielb2365d82020-05-13 15:32:04 -0400569 if (!this->asDirectContext()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400570 return false;
571 }
572
573 if (this->abandoned()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400574 return false;
575 }
576
577 GrGpu::BackendTextureData data(color);
Greg Daniel25597782020-06-11 13:15:08 -0400578 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
Greg Danielb2365d82020-05-13 15:32:04 -0400579}
580
581bool GrContext::updateBackendTexture(const GrBackendTexture& backendTexture,
Greg Daniel373d7dd2020-07-21 10:41:50 -0400582 SkColorType skColorType,
583 const SkColor4f& color,
584 GrGpuFinishedProc finishedProc,
585 GrGpuFinishedContext finishedContext) {
586 sk_sp<GrRefCntedCallback> finishedCallback;
587 if (finishedProc) {
588 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
589 }
590
591 if (!this->asDirectContext()) {
592 return false;
593 }
594
595 if (this->abandoned()) {
596 return false;
597 }
598
599 GrBackendFormat format = backendTexture.getBackendFormat();
600 GrColorType grColorType = SkColorTypeAndFormatToGrColorType(this->caps(), skColorType, format);
601
602 if (!this->caps()->areColorTypeAndFormatCompatible(grColorType, format)) {
603 return false;
604 }
605
606 GrSwizzle swizzle = this->caps()->getWriteSwizzle(format, grColorType);
607 GrGpu::BackendTextureData data(swizzle.applyTo(color));
608
609 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
610}
611
612bool GrContext::updateBackendTexture(const GrBackendTexture& backendTexture,
Greg Danielb2365d82020-05-13 15:32:04 -0400613 const SkPixmap srcData[],
614 int numLevels,
615 GrGpuFinishedProc finishedProc,
616 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400617 sk_sp<GrRefCntedCallback> finishedCallback;
618 if (finishedProc) {
619 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
620 }
621
Greg Danielb2365d82020-05-13 15:32:04 -0400622 if (!this->asDirectContext()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400623 return false;
624 }
625
626 if (this->abandoned()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400627 return false;
628 }
629
630 if (!srcData || numLevels <= 0) {
Greg Danielb2365d82020-05-13 15:32:04 -0400631 return false;
632 }
633
634 int numExpectedLevels = 1;
Brian Salomon40a40622020-07-21 10:32:07 -0400635 if (backendTexture.hasMipmaps()) {
Mike Reed13711eb2020-07-14 17:16:32 -0400636 numExpectedLevels = SkMipmap::ComputeLevelCount(backendTexture.width(),
Greg Danielb2365d82020-05-13 15:32:04 -0400637 backendTexture.height()) + 1;
638 }
639 if (numLevels != numExpectedLevels) {
Greg Danielb2365d82020-05-13 15:32:04 -0400640 return false;
641 }
642
643 GrGpu::BackendTextureData data(srcData);
Greg Daniel25597782020-06-11 13:15:08 -0400644 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
Greg Danielb2365d82020-05-13 15:32:04 -0400645}
646
Robert Phillipsb915c942019-12-17 14:44:37 -0500647//////////////////////////////////////////////////////////////////////////////
648
Greg Danielaaf738c2020-07-10 09:30:33 -0400649static GrBackendTexture create_and_update_compressed_backend_texture(
Robert Phillips4e105e22020-07-16 09:18:50 -0400650 GrDirectContext* context,
Greg Danielaaf738c2020-07-10 09:30:33 -0400651 SkISize dimensions,
652 const GrBackendFormat& backendFormat,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400653 GrMipmapped mipMapped,
Greg Danielaaf738c2020-07-10 09:30:33 -0400654 GrProtected isProtected,
655 sk_sp<GrRefCntedCallback> finishedCallback,
656 const GrGpu::BackendTextureData* data) {
657 GrGpu* gpu = context->priv().getGpu();
658
659 GrBackendTexture beTex = gpu->createCompressedBackendTexture(dimensions, backendFormat,
660 mipMapped, isProtected);
661 if (!beTex.isValid()) {
662 return {};
663 }
664
665 if (!context->priv().getGpu()->updateCompressedBackendTexture(
666 beTex, std::move(finishedCallback), data)) {
667 context->deleteBackendTexture(beTex);
668 return {};
669 }
670 return beTex;
671}
672
Robert Phillipsb915c942019-12-17 14:44:37 -0500673GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
674 const GrBackendFormat& backendFormat,
675 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400676 GrMipmapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400677 GrProtected isProtected,
678 GrGpuFinishedProc finishedProc,
679 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500680 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel25597782020-06-11 13:15:08 -0400681 sk_sp<GrRefCntedCallback> finishedCallback;
682 if (finishedProc) {
683 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
684 }
685
Robert Phillipsb915c942019-12-17 14:44:37 -0500686 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400687 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500688 }
689
690 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400691 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500692 }
693
694 GrGpu::BackendTextureData data(color);
Robert Phillips4e105e22020-07-16 09:18:50 -0400695 return create_and_update_compressed_backend_texture(this->asDirectContext(), {width, height},
696 backendFormat, mipMapped, isProtected,
Greg Danielaaf738c2020-07-10 09:30:33 -0400697 std::move(finishedCallback), &data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500698}
699
700GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
701 SkImage::CompressionType compression,
702 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400703 GrMipmapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400704 GrProtected isProtected,
705 GrGpuFinishedProc finishedProc,
706 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500707 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillipsb915c942019-12-17 14:44:37 -0500708 GrBackendFormat format = this->compressedBackendFormat(compression);
709 return this->createCompressedBackendTexture(width, height, format, color,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400710 mipMapped, isProtected, finishedProc,
711 finishedContext);
Robert Phillipsb915c942019-12-17 14:44:37 -0500712}
713
714GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
715 const GrBackendFormat& backendFormat,
716 const void* compressedData,
717 size_t dataSize,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400718 GrMipmapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400719 GrProtected isProtected,
720 GrGpuFinishedProc finishedProc,
721 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500722 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel25597782020-06-11 13:15:08 -0400723 sk_sp<GrRefCntedCallback> finishedCallback;
724 if (finishedProc) {
725 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
726 }
727
Robert Phillipsb915c942019-12-17 14:44:37 -0500728 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400729 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500730 }
731
732 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400733 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500734 }
735
736 GrGpu::BackendTextureData data(compressedData, dataSize);
Robert Phillips4e105e22020-07-16 09:18:50 -0400737 return create_and_update_compressed_backend_texture(this->asDirectContext(), {width, height},
738 backendFormat, mipMapped, isProtected,
Greg Danielaaf738c2020-07-10 09:30:33 -0400739 std::move(finishedCallback), &data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500740}
741
742GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
743 SkImage::CompressionType compression,
744 const void* data, size_t dataSize,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400745 GrMipmapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400746 GrProtected isProtected,
747 GrGpuFinishedProc finishedProc,
748 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500749 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillipsb915c942019-12-17 14:44:37 -0500750 GrBackendFormat format = this->compressedBackendFormat(compression);
Greg Daniel25597782020-06-11 13:15:08 -0400751 return this->createCompressedBackendTexture(width, height, format, data, dataSize, mipMapped,
752 isProtected, finishedProc, finishedContext);
Robert Phillipsb915c942019-12-17 14:44:37 -0500753}
754
Greg Daniel1db8e792020-06-09 17:29:32 -0400755bool GrContext::setBackendTextureState(const GrBackendTexture& backendTexture,
756 const GrBackendSurfaceMutableState& state,
Greg Daniel1d3c8c12020-09-23 14:23:36 -0400757 GrBackendSurfaceMutableState* previousState,
Greg Daniel1db8e792020-06-09 17:29:32 -0400758 GrGpuFinishedProc finishedProc,
759 GrGpuFinishedContext finishedContext) {
Greg Daniel1db8e792020-06-09 17:29:32 -0400760 sk_sp<GrRefCntedCallback> callback;
761 if (finishedProc) {
762 callback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
763 }
Greg Daniel25597782020-06-11 13:15:08 -0400764
765 if (!this->asDirectContext()) {
766 return false;
767 }
768
769 if (this->abandoned()) {
770 return false;
771 }
772
Greg Daniel1d3c8c12020-09-23 14:23:36 -0400773 return fGpu->setBackendTextureState(backendTexture, state, previousState, std::move(callback));
Greg Daniel1db8e792020-06-09 17:29:32 -0400774}
775
Greg Daniel95afafb2020-07-22 12:09:26 -0400776bool GrContext::updateCompressedBackendTexture(const GrBackendTexture& backendTexture,
777 const SkColor4f& color,
778 GrGpuFinishedProc finishedProc,
779 GrGpuFinishedContext finishedContext) {
780 sk_sp<GrRefCntedCallback> finishedCallback;
781 if (finishedProc) {
782 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
783 }
784
785 if (!this->asDirectContext()) {
786 return false;
787 }
788
789 if (this->abandoned()) {
790 return false;
791 }
792
793 GrGpu::BackendTextureData data(color);
794 return fGpu->updateCompressedBackendTexture(backendTexture, std::move(finishedCallback), &data);
795}
796
797bool GrContext::updateCompressedBackendTexture(const GrBackendTexture& backendTexture,
798 const void* compressedData,
799 size_t dataSize,
800 GrGpuFinishedProc finishedProc,
801 GrGpuFinishedContext finishedContext) {
802 sk_sp<GrRefCntedCallback> finishedCallback;
803 if (finishedProc) {
804 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
805 }
806
807 if (!this->asDirectContext()) {
808 return false;
809 }
810
811 if (this->abandoned()) {
812 return false;
813 }
814
815 if (!compressedData) {
816 return false;
817 }
818
819 GrGpu::BackendTextureData data(compressedData, dataSize);
820
821 return fGpu->updateCompressedBackendTexture(backendTexture, std::move(finishedCallback), &data);
822}
823
824//////////////////////////////////////////////////////////////////////////////
825
Greg Daniel1db8e792020-06-09 17:29:32 -0400826bool GrContext::setBackendRenderTargetState(const GrBackendRenderTarget& backendRenderTarget,
827 const GrBackendSurfaceMutableState& state,
Greg Daniel1d3c8c12020-09-23 14:23:36 -0400828 GrBackendSurfaceMutableState* previousState,
Greg Daniel1db8e792020-06-09 17:29:32 -0400829 GrGpuFinishedProc finishedProc,
830 GrGpuFinishedContext finishedContext) {
Greg Daniel1db8e792020-06-09 17:29:32 -0400831 sk_sp<GrRefCntedCallback> callback;
832 if (finishedProc) {
833 callback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
834 }
Greg Daniel25597782020-06-11 13:15:08 -0400835
836 if (!this->asDirectContext()) {
837 return false;
838 }
839
840 if (this->abandoned()) {
841 return false;
842 }
843
Greg Daniel1d3c8c12020-09-23 14:23:36 -0400844 return fGpu->setBackendRenderTargetState(backendRenderTarget, state, previousState,
845 std::move(callback));
Greg Daniel1db8e792020-06-09 17:29:32 -0400846}
847
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400848void GrContext::deleteBackendTexture(GrBackendTexture backendTex) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400849 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danielf0e04f02019-12-04 15:17:54 -0500850 // For the Vulkan backend we still must destroy the backend texture when the context is
851 // abandoned.
852 if ((this->abandoned() && this->backend() != GrBackendApi::kVulkan) || !backendTex.isValid()) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400853 return;
854 }
855
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400856 fGpu->deleteBackendTexture(backendTex);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400857}
858
Greg Daniel1db8e792020-06-09 17:29:32 -0400859//////////////////////////////////////////////////////////////////////////////
860
Brian Osmaned58e002019-09-06 14:42:43 -0400861bool GrContext::precompileShader(const SkData& key, const SkData& data) {
862 return fGpu->precompileShader(key, data);
863}
864
Brian Salomonec22b1a2019-08-09 09:41:48 -0400865#ifdef SK_ENABLE_DUMP_GPU
Michael Ludwigdd205452020-03-30 17:16:34 -0400866#include "include/core/SkString.h"
Brian Salomonec22b1a2019-08-09 09:41:48 -0400867#include "src/utils/SkJSONWriter.h"
868SkString GrContext::dump() const {
869 SkDynamicMemoryWStream stream;
870 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
871 writer.beginObject();
872
873 writer.appendString("backend", GrBackendApiToStr(this->backend()));
874
875 writer.appendName("caps");
876 this->caps()->dumpJSON(&writer);
877
878 writer.appendName("gpu");
879 this->fGpu->dumpJSON(&writer);
880
Robert Phillips273f1072020-05-05 13:03:07 -0400881 writer.appendName("context");
882 this->dumpJSON(&writer);
883
Brian Salomonec22b1a2019-08-09 09:41:48 -0400884 // Flush JSON to the memory stream
885 writer.endObject();
886 writer.flush();
887
888 // Null terminate the JSON data in the memory stream
889 stream.write8(0);
890
891 // Allocate a string big enough to hold all the data, then copy out of the stream
892 SkString result(stream.bytesWritten());
893 stream.copyToAndReset(result.writable_str());
894 return result;
895}
896#endif