blob: d82192a96cf480cd7bd4038a6f737231d8e13096 [file] [log] [blame]
bsalomon@google.com27847de2011-02-22 20:59:41 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
bsalomon@google.com27847de2011-02-22 20:59:41 +00006 */
7
Robert Phillips4e105e22020-07-16 09:18:50 -04008#include "include/gpu/GrDirectContext.h"
Brian Salomon9241a6d2019-10-03 13:26:54 -04009
Robert Phillips4d5594d2020-02-21 14:24:40 -050010#include "include/core/SkDeferredDisplayList.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkTraceMemoryDump.h"
12#include "include/gpu/GrBackendSemaphore.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/private/SkImageInfoPriv.h"
Mike Reed13711eb2020-07-14 17:16:32 -040014#include "src/core/SkMipmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/core/SkTaskGroup.h"
Brian Salomon9241a6d2019-10-03 13:26:54 -040016#include "src/gpu/GrClientMappedBufferManager.h"
Greg Daniel7fd7a8a2019-10-10 16:10:31 -040017#include "src/gpu/GrContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrDrawingManager.h"
19#include "src/gpu/GrGpu.h"
20#include "src/gpu/GrMemoryPool.h"
21#include "src/gpu/GrPathRendererChain.h"
22#include "src/gpu/GrProxyProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040023#include "src/gpu/GrRenderTargetProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrResourceCache.h"
25#include "src/gpu/GrResourceProvider.h"
26#include "src/gpu/GrSemaphore.h"
Brian Osman5e7fbfd2019-05-03 13:13:35 -040027#include "src/gpu/GrShaderUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/GrSoftwarePathRenderer.h"
29#include "src/gpu/GrTracing.h"
30#include "src/gpu/SkGr.h"
31#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
32#include "src/gpu/effects/GrSkSLFP.h"
Herb Derbya08bde62020-06-12 15:46:06 -040033#include "src/gpu/text/GrSDFTOptions.h"
Robert Phillips41bd97d2020-04-07 14:19:37 -040034#include "src/gpu/text/GrStrikeCache.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/gpu/text/GrTextBlobCache.h"
Greg Daniel7fd7a8a2019-10-10 16:10:31 -040036#include "src/image/SkImage_GpuBase.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "src/image/SkSurface_Gpu.h"
Mike Klein0ec1c572018-12-04 11:52:51 -050038#include <atomic>
John Stilesfbd050b2020-08-03 13:21:46 -040039#include <memory>
Greg Danielb76a72a2017-07-13 15:07:54 -040040
Robert Phillipse78b7252017-04-06 07:59:41 -040041#define ASSERT_OWNED_PROXY(P) \
Brian Salomonfd98c2c2018-07-31 17:25:29 -040042 SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == this)
Robert Phillips7ee385e2017-03-30 08:02:11 -040043
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000044#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
Adlai Holler33dbd652020-06-01 12:35:42 -040045#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner())
Robert Phillipsa9162df2019-02-11 14:12:03 -050046#define RETURN_IF_ABANDONED if (this->abandoned()) { return; }
47#define RETURN_FALSE_IF_ABANDONED if (this->abandoned()) { return false; }
48#define RETURN_NULL_IF_ABANDONED if (this->abandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000049
robertphillipsea461502015-05-26 11:38:03 -070050////////////////////////////////////////////////////////////////////////////////
51
Adlai Hollere219d1c2020-06-02 11:23:16 -040052GrContext::GrContext(sk_sp<GrContextThreadSafeProxy> proxy) : INHERITED(std::move(proxy)) {
halcanary96fcdcc2015-08-27 07:41:13 -070053 fResourceCache = nullptr;
54 fResourceProvider = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000055}
56
Robert Phillips292a6b22019-02-14 14:49:02 -050057GrContext::~GrContext() {
58 ASSERT_SINGLE_OWNER
59
Adlai Holler96ead542020-06-26 08:50:14 -040060 this->destroyDrawingManager();
Greg Daniel5ed3c112020-06-18 15:59:17 -040061 fMappedBufferManager.reset();
Robert Phillips292a6b22019-02-14 14:49:02 -050062 delete fResourceProvider;
63 delete fResourceCache;
Robert Phillips292a6b22019-02-14 14:49:02 -050064}
65
Adlai Hollere219d1c2020-06-02 11:23:16 -040066bool GrContext::init() {
Greg Danielb76a72a2017-07-13 15:07:54 -040067 ASSERT_SINGLE_OWNER
Robert Phillipsa41c6852019-02-07 10:44:10 -050068 SkASSERT(this->proxyProvider());
Robert Phillips88260b52018-01-19 12:56:09 -050069
Adlai Hollere219d1c2020-06-02 11:23:16 -040070 if (!INHERITED::init()) {
Robert Phillipsbb606772019-02-04 17:50:57 -050071 return false;
72 }
73
Robert Phillips2184fb72019-02-21 16:11:41 -050074 SkASSERT(this->getTextBlobCache());
Robert Phillipsbb606772019-02-04 17:50:57 -050075
Robert Phillips88260b52018-01-19 12:56:09 -050076 if (fGpu) {
John Stilesfbd050b2020-08-03 13:21:46 -040077 fStrikeCache = std::make_unique<GrStrikeCache>();
Robert Phillipsa41c6852019-02-07 10:44:10 -050078 fResourceCache = new GrResourceCache(this->caps(), this->singleOwner(), this->contextID());
Robert Phillips12c46292019-04-23 07:36:17 -040079 fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, this->singleOwner());
Mike Kleinf46d5ca2019-12-11 10:45:01 -050080 fMappedBufferManager = std::make_unique<GrClientMappedBufferManager>(this->contextID());
Robert Phillips88260b52018-01-19 12:56:09 -050081 }
82
Robert Phillips88260b52018-01-19 12:56:09 -050083 if (fResourceCache) {
Robert Phillipsa41c6852019-02-07 10:44:10 -050084 fResourceCache->setProxyProvider(this->proxyProvider());
Robert Phillips88260b52018-01-19 12:56:09 -050085 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050086
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000087 fDidTestPMConversions = false;
88
Robert Phillipsfde6fa02018-03-02 08:53:14 -050089 // DDL TODO: we need to think through how the task group & persistent cache
90 // get passed on to/shared between all the DDLRecorders created with this context.
Robert Phillipsc1541ae2019-02-04 12:05:37 -050091 if (this->options().fExecutor) {
Mike Kleinf46d5ca2019-12-11 10:45:01 -050092 fTaskGroup = std::make_unique<SkTaskGroup>(*this->options().fExecutor);
Brian Osman51279982017-08-23 10:12:00 -040093 }
94
Robert Phillipsc1541ae2019-02-04 12:05:37 -050095 fPersistentCache = this->options().fPersistentCache;
Brian Osman5e7fbfd2019-05-03 13:13:35 -040096 fShaderErrorHandler = this->options().fShaderErrorHandler;
97 if (!fShaderErrorHandler) {
98 fShaderErrorHandler = GrShaderUtils::DefaultShaderErrorHandler();
99 }
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400100
Brian Salomon91a3e522017-06-23 10:58:19 -0400101 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000102}
103
Robert Phillips4217ea72019-01-30 13:08:28 -0500104sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
Adlai Hollere219d1c2020-06-02 11:23:16 -0400105 return INHERITED::threadSafeProxy();
Robert Phillips4217ea72019-01-30 13:08:28 -0500106}
107
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400108//////////////////////////////////////////////////////////////////////////////
109
bsalomon2354f842014-07-28 13:48:36 -0700110void GrContext::abandonContext() {
Greg Daniel6e35a002020-04-01 13:29:59 -0400111 if (INHERITED::abandoned()) {
Robert Phillipsa9162df2019-02-11 14:12:03 -0500112 return;
113 }
joshualitt1de610a2016-01-06 08:26:09 -0800114
Robert Phillipsa9162df2019-02-11 14:12:03 -0500115 INHERITED::abandonContext();
116
Robert Phillips4d932d12020-04-09 08:58:52 -0400117 fStrikeCache->freeAll();
118
Brian Salomon9241a6d2019-10-03 13:26:54 -0400119 fMappedBufferManager->abandon();
120
bsalomond309e7a2015-04-30 14:18:54 -0700121 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800122
bsalomon@google.com205d4602011-04-25 12:43:45 +0000123 // abandon first to so destructors
124 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800125 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700126
bsalomon6e2aad42016-04-01 11:54:31 -0700127 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400128
129 fMappedBufferManager.reset();
Khushalc421ca12018-06-26 14:38:34 -0700130}
131
bsalomon6e2aad42016-04-01 11:54:31 -0700132void GrContext::releaseResourcesAndAbandonContext() {
Greg Daniel6e35a002020-04-01 13:29:59 -0400133 if (INHERITED::abandoned()) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500134 return;
135 }
Robert Phillipsa9162df2019-02-11 14:12:03 -0500136
137 INHERITED::abandonContext();
138
Brian Salomon9241a6d2019-10-03 13:26:54 -0400139 fMappedBufferManager.reset();
140
bsalomon6e2aad42016-04-01 11:54:31 -0700141 fResourceProvider->abandon();
142
bsalomon6e2aad42016-04-01 11:54:31 -0700143 // Release all resources in the backend 3D API.
144 fResourceCache->releaseAll();
145
146 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000147}
148
Greg Daniel6e35a002020-04-01 13:29:59 -0400149bool GrContext::abandoned() {
150 if (INHERITED::abandoned()) {
151 return true;
152 }
153
154 if (fGpu && fGpu->isDeviceLost()) {
155 this->abandonContext();
156 return true;
157 }
158 return false;
159}
160
Brian Salomon24069eb2020-06-24 10:19:52 -0400161bool GrContext::oomed() { return fGpu ? fGpu->checkAndResetOOMed() : false; }
162
Brian Salomon1f05d452019-02-08 12:33:08 -0500163void GrContext::resetGLTextureBindings() {
164 if (this->abandoned() || this->backend() != GrBackendApi::kOpenGL) {
165 return;
166 }
167 fGpu->resetTextureBindings();
168}
169
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000170void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800171 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000172 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000173}
174
175void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800176 ASSERT_SINGLE_OWNER
177
Adlai Holler9d8a41c2020-06-25 14:01:58 +0000178 if (this->abandoned()) {
179 return;
180 }
181
Robert Phillips2184fb72019-02-21 16:11:41 -0500182 // TODO: the glyph cache doesn't hold any GpuResources so this call should not be needed here.
183 // Some slack in the GrTextBlob's implementation requires it though. That could be fixed.
Robert Phillips4d932d12020-04-09 08:58:52 -0400184 fStrikeCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700185
Robert Phillips6a6de562019-02-15 15:19:15 -0500186 this->drawingManager()->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700187
188 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000189}
190
Robert Phillips6eba0632018-03-28 12:25:42 -0400191void GrContext::purgeUnlockedResources(bool scratchResourcesOnly) {
192 ASSERT_SINGLE_OWNER
Robert Phillipsddc21482019-10-16 14:30:09 -0400193
194 if (this->abandoned()) {
195 return;
196 }
197
Robert Phillips6eba0632018-03-28 12:25:42 -0400198 fResourceCache->purgeUnlockedResources(scratchResourcesOnly);
199 fResourceCache->purgeAsNeeded();
Robert Phillips2184fb72019-02-21 16:11:41 -0500200
201 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
202 // place to purge stale blobs
203 this->getTextBlobCache()->purgeStaleBlobs();
Robert Phillips6eba0632018-03-28 12:25:42 -0400204}
205
Jim Van Verth76d917c2017-12-13 09:26:37 -0500206void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Yuqian Li40aa85f2019-07-02 13:45:00 -0700207 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
208
Brian Salomon5e150852017-03-22 14:53:13 -0400209 ASSERT_SINGLE_OWNER
Chris Dalton6c3879d2018-11-01 11:13:19 -0600210
Brian Salomon9241a6d2019-10-03 13:26:54 -0400211 if (this->abandoned()) {
212 return;
213 }
214
Jim Van Verthe0da3792020-08-14 16:50:19 -0400215 this->checkAsyncWorkCompletion();
Brian Salomon9241a6d2019-10-03 13:26:54 -0400216 fMappedBufferManager->process();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600217 auto purgeTime = GrStdSteadyClock::now() - msNotUsed;
218
Jim Van Verth76d917c2017-12-13 09:26:37 -0500219 fResourceCache->purgeAsNeeded();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600220 fResourceCache->purgeResourcesNotUsedSince(purgeTime);
221
Robert Phillips6a6de562019-02-15 15:19:15 -0500222 if (auto ccpr = this->drawingManager()->getCoverageCountingPathRenderer()) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500223 ccpr->purgeCacheEntriesOlderThan(this->proxyProvider(), purgeTime);
Chris Dalton6c3879d2018-11-01 11:13:19 -0600224 }
Jim Van Verth76d917c2017-12-13 09:26:37 -0500225
Robert Phillips2184fb72019-02-21 16:11:41 -0500226 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
227 // place to purge stale blobs
228 this->getTextBlobCache()->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400229}
230
Derek Sollenberger5480a182017-05-25 16:43:59 -0400231void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
232 ASSERT_SINGLE_OWNER
Robert Phillipsddc21482019-10-16 14:30:09 -0400233
234 if (this->abandoned()) {
235 return;
236 }
237
Derek Sollenberger5480a182017-05-25 16:43:59 -0400238 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
239}
240
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000241void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800242 ASSERT_SINGLE_OWNER
243
bsalomon71cb0c22014-11-14 12:10:14 -0800244 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800245 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800246 }
247 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800248 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800249 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000250}
251
Derek Sollenbergeree479142017-05-24 11:41:33 -0400252size_t GrContext::getResourceCachePurgeableBytes() const {
253 ASSERT_SINGLE_OWNER
254 return fResourceCache->getPurgeableBytes();
255}
256
Brian Salomon7e67dca2020-07-21 09:27:25 -0400257size_t GrContext::ComputeImageSize(sk_sp<SkImage> image, GrMipmapped mipMapped, bool useNextPow2) {
Greg Daniel8b666172019-10-09 12:38:22 -0400258 if (!image->isTextureBacked()) {
259 return 0;
260 }
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400261 SkImage_GpuBase* gpuImage = static_cast<SkImage_GpuBase*>(as_IB(image.get()));
262 GrTextureProxy* proxy = gpuImage->peekProxy();
263 if (!proxy) {
264 return 0;
265 }
266
267 const GrCaps& caps = *gpuImage->context()->priv().caps();
Greg Daniel8b666172019-10-09 12:38:22 -0400268 int colorSamplesPerPixel = 1;
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400269 return GrSurface::ComputeSize(caps, proxy->backendFormat(), image->dimensions(),
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400270 colorSamplesPerPixel, mipMapped, useNextPow2);
Greg Daniel8b666172019-10-09 12:38:22 -0400271}
272
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000273////////////////////////////////////////////////////////////////////////////////
274
Greg Daniel414418d2020-07-08 11:44:25 -0400275bool GrContext::wait(int numSemaphores, const GrBackendSemaphore waitSemaphores[],
276 bool deleteSemaphoresAfterWait) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400277 if (!fGpu || fGpu->caps()->semaphoreSupport()) {
Greg Daniel06be0792019-04-22 15:53:23 -0400278 return false;
279 }
Greg Daniel414418d2020-07-08 11:44:25 -0400280 GrWrapOwnership ownership =
281 deleteSemaphoresAfterWait ? kAdopt_GrWrapOwnership : kBorrow_GrWrapOwnership;
Greg Daniel06be0792019-04-22 15:53:23 -0400282 for (int i = 0; i < numSemaphores; ++i) {
Greg Daniel301015c2019-11-18 14:06:46 -0500283 std::unique_ptr<GrSemaphore> sema = fResourceProvider->wrapBackendSemaphore(
Greg Daniel414418d2020-07-08 11:44:25 -0400284 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait, ownership);
Greg Daniel0106fcc2020-07-01 17:40:12 -0400285 // If we failed to wrap the semaphore it means the client didn't give us a valid semaphore
286 // to begin with. Therefore, it is fine to not wait on it.
287 if (sema) {
288 fGpu->waitSemaphore(sema.get());
289 }
Greg Daniel06be0792019-04-22 15:53:23 -0400290 }
291 return true;
292}
293
294////////////////////////////////////////////////////////////////////////////////
295
Greg Daniele8d3cca2020-06-10 10:04:48 -0400296GrSemaphoresSubmitted GrContext::flush(const GrFlushInfo& info) {
Greg Daniel51316782017-08-02 15:10:09 +0000297 ASSERT_SINGLE_OWNER
Robert Phillipsa9162df2019-02-11 14:12:03 -0500298 if (this->abandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400299 if (info.fFinishedProc) {
300 info.fFinishedProc(info.fFinishedContext);
301 }
302 if (info.fSubmittedProc) {
303 info.fSubmittedProc(info.fSubmittedContext, false);
304 }
Robert Phillipsa9162df2019-02-11 14:12:03 -0500305 return GrSemaphoresSubmitted::kNo;
306 }
Greg Daniel51316782017-08-02 15:10:09 +0000307
Greg Daniel04283f32020-05-20 13:16:00 -0400308 bool flushed = this->drawingManager()->flush(
Greg Daniel9efe3862020-06-11 11:51:06 -0400309 nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, info, nullptr);
Greg Danielfe159622020-04-10 17:43:51 +0000310
Greg Daniel04283f32020-05-20 13:16:00 -0400311 if (!flushed || (!this->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
Greg Danielfe159622020-04-10 17:43:51 +0000312 return GrSemaphoresSubmitted::kNo;
313 }
314 return GrSemaphoresSubmitted::kYes;
Greg Daniel51316782017-08-02 15:10:09 +0000315}
316
Greg Daniel04283f32020-05-20 13:16:00 -0400317bool GrContext::submit(bool syncCpu) {
318 ASSERT_SINGLE_OWNER
319 if (this->abandoned()) {
320 return false;
321 }
322
323 if (!fGpu) {
324 return false;
325 }
326
327 return fGpu->submitToGpu(syncCpu);
Greg Danielda50cb82020-05-13 14:07:40 -0400328}
329
Greg Daniela870b462019-01-08 15:49:46 -0500330////////////////////////////////////////////////////////////////////////////////
331
Brian Salomonb0d8b762019-05-06 16:58:22 -0400332void GrContext::checkAsyncWorkCompletion() {
333 if (fGpu) {
334 fGpu->checkFinishProcs();
335 }
336}
337
338////////////////////////////////////////////////////////////////////////////////
339
Greg Daniela870b462019-01-08 15:49:46 -0500340void GrContext::storeVkPipelineCacheData() {
341 if (fGpu) {
342 fGpu->storeVkPipelineCacheData();
343 }
344}
345
346////////////////////////////////////////////////////////////////////////////////
347
Khushal3e7548c2018-05-23 15:45:01 -0700348bool GrContext::supportsDistanceFieldText() const {
Robert Phillipsbb606772019-02-04 17:50:57 -0500349 return this->caps()->shaderCaps()->supportsDistanceFieldText();
Khushal3e7548c2018-05-23 15:45:01 -0700350}
351
bsalomon37f9a262015-02-02 13:00:10 -0800352//////////////////////////////////////////////////////////////////////////////
353
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500354void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800355 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500356 if (maxResources) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400357 *maxResources = -1;
bsalomon37f9a262015-02-02 13:00:10 -0800358 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500359 if (maxResourceBytes) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400360 *maxResourceBytes = this->getResourceCacheLimit();
bsalomon37f9a262015-02-02 13:00:10 -0800361 }
362}
363
Robert Phillipscf39f372019-09-03 10:29:20 -0400364size_t GrContext::getResourceCacheLimit() const {
joshualitt1de610a2016-01-06 08:26:09 -0800365 ASSERT_SINGLE_OWNER
Robert Phillipscf39f372019-09-03 10:29:20 -0400366 return fResourceCache->getMaxResourceBytes();
367}
368
369void GrContext::setResourceCacheLimits(int unused, size_t maxResourceBytes) {
370 ASSERT_SINGLE_OWNER
371 this->setResourceCacheLimit(maxResourceBytes);
372}
373
374void GrContext::setResourceCacheLimit(size_t maxResourceBytes) {
375 ASSERT_SINGLE_OWNER
376 fResourceCache->setLimit(maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800377}
378
ericrk0a5fa482015-09-15 14:16:10 -0700379//////////////////////////////////////////////////////////////////////////////
ericrk0a5fa482015-09-15 14:16:10 -0700380void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800381 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700382 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
Khushal71652e22018-10-29 13:05:36 -0700383 traceMemoryDump->dumpNumericValue("skia/gr_text_blob_cache", "size", "bytes",
Robert Phillips2184fb72019-02-21 16:11:41 -0500384 this->getTextBlobCache()->usedBytes());
ericrk0a5fa482015-09-15 14:16:10 -0700385}
Brian Osman71a18892017-08-10 10:23:25 -0400386
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400387//////////////////////////////////////////////////////////////////////////////
388GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400389 const GrBackendFormat& backendFormat,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400390 GrMipmapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400391 GrRenderable renderable,
392 GrProtected isProtected) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400393 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400394 if (!this->asDirectContext()) {
395 return GrBackendTexture();
396 }
397
398 if (this->abandoned()) {
399 return GrBackendTexture();
400 }
401
Robert Phillips4277f012020-01-21 14:28:34 -0500402 return fGpu->createBackendTexture({width, height}, backendFormat, renderable,
Greg Daniel16032b32020-05-06 15:31:10 -0400403 mipMapped, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400404}
405
406GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400407 SkColorType skColorType,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400408 GrMipmapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400409 GrRenderable renderable,
410 GrProtected isProtected) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400411 if (!this->asDirectContext()) {
412 return GrBackendTexture();
413 }
414
415 if (this->abandoned()) {
416 return GrBackendTexture();
417 }
418
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400419 const GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400420
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400421 return this->createBackendTexture(width, height, format, mipMapped, renderable, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400422}
423
Greg Daniel25597782020-06-11 13:15:08 -0400424static GrBackendTexture create_and_update_backend_texture(
Robert Phillips4e105e22020-07-16 09:18:50 -0400425 GrDirectContext* context,
Greg Daniel25597782020-06-11 13:15:08 -0400426 SkISize dimensions,
427 const GrBackendFormat& backendFormat,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400428 GrMipmapped mipMapped,
Greg Daniel25597782020-06-11 13:15:08 -0400429 GrRenderable renderable,
430 GrProtected isProtected,
431 sk_sp<GrRefCntedCallback> finishedCallback,
432 const GrGpu::BackendTextureData* data) {
Greg Daniel16032b32020-05-06 15:31:10 -0400433 GrGpu* gpu = context->priv().getGpu();
434
435 GrBackendTexture beTex = gpu->createBackendTexture(dimensions, backendFormat, renderable,
436 mipMapped, isProtected);
437 if (!beTex.isValid()) {
438 return {};
439 }
440
Greg Daniel25597782020-06-11 13:15:08 -0400441 if (!context->priv().getGpu()->updateBackendTexture(beTex, std::move(finishedCallback), data)) {
Greg Daniel16032b32020-05-06 15:31:10 -0400442 context->deleteBackendTexture(beTex);
443 return {};
444 }
445 return beTex;
446}
447
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400448GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400449 const GrBackendFormat& backendFormat,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400450 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400451 GrMipmapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400452 GrRenderable renderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400453 GrProtected isProtected,
454 GrGpuFinishedProc finishedProc,
455 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400456 sk_sp<GrRefCntedCallback> finishedCallback;
457 if (finishedProc) {
458 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
459 }
460
Brian Salomonc42eb662019-06-24 17:13:00 -0400461 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400462 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400463 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400464 }
465
466 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400467 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400468 }
469
Brian Salomon85c3d682019-11-04 15:04:54 -0500470 GrGpu::BackendTextureData data(color);
Robert Phillips4e105e22020-07-16 09:18:50 -0400471 return create_and_update_backend_texture(this->asDirectContext(), {width, height},
472 backendFormat, mipMapped, renderable, isProtected,
473 std::move(finishedCallback), &data);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400474}
475
476GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400477 SkColorType skColorType,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400478 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400479 GrMipmapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400480 GrRenderable renderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400481 GrProtected isProtected,
482 GrGpuFinishedProc finishedProc,
483 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400484 sk_sp<GrRefCntedCallback> finishedCallback;
485 if (finishedProc) {
486 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
487 }
488
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400489 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400490 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400491 }
492
493 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400494 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400495 }
496
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400497 GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400498 if (!format.isValid()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400499 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400500 }
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400501
502 GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
Brian Salomon982f5462020-03-30 12:52:33 -0400503 SkColor4f swizzledColor = this->caps()->getWriteSwizzle(format, grColorType).applyTo(color);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400504
Greg Daniel25597782020-06-11 13:15:08 -0400505 GrGpu::BackendTextureData data(swizzledColor);
Robert Phillips4e105e22020-07-16 09:18:50 -0400506 return create_and_update_backend_texture(this->asDirectContext(), {width, height}, format,
507 mipMapped, renderable, isProtected,
508 std::move(finishedCallback), &data);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400509}
510
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500511GrBackendTexture GrContext::createBackendTexture(const SkPixmap srcData[], int numProvidedLevels,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400512 GrRenderable renderable, GrProtected isProtected,
513 GrGpuFinishedProc finishedProc,
514 GrGpuFinishedContext finishedContext) {
Robert Phillips66944402019-09-30 13:21:25 -0400515 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
516
Greg Daniel25597782020-06-11 13:15:08 -0400517 sk_sp<GrRefCntedCallback> finishedCallback;
518 if (finishedProc) {
519 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
520 }
521
Robert Phillips66944402019-09-30 13:21:25 -0400522 if (!this->asDirectContext()) {
523 return {};
524 }
525
526 if (this->abandoned()) {
527 return {};
528 }
529
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500530 if (!srcData || numProvidedLevels <= 0) {
Robert Phillips66944402019-09-30 13:21:25 -0400531 return {};
532 }
533
534 int baseWidth = srcData[0].width();
535 int baseHeight = srcData[0].height();
536 SkColorType colorType = srcData[0].colorType();
537
Brian Salomon7e67dca2020-07-21 09:27:25 -0400538 GrMipmapped mipMapped = GrMipmapped::kNo;
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500539 int numExpectedLevels = 1;
540 if (numProvidedLevels > 1) {
Mike Reed13711eb2020-07-14 17:16:32 -0400541 numExpectedLevels = SkMipmap::ComputeLevelCount(baseWidth, baseHeight) + 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400542 mipMapped = GrMipmapped::kYes;
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500543 }
544
545 if (numProvidedLevels != numExpectedLevels) {
546 return {};
547 }
548
Robert Phillips66944402019-09-30 13:21:25 -0400549 GrBackendFormat backendFormat = this->defaultBackendFormat(colorType, renderable);
550
Brian Salomon85c3d682019-11-04 15:04:54 -0500551 GrGpu::BackendTextureData data(srcData);
Robert Phillips4e105e22020-07-16 09:18:50 -0400552 return create_and_update_backend_texture(this->asDirectContext(), {baseWidth, baseHeight},
553 backendFormat, mipMapped, renderable, isProtected,
Greg Daniel25597782020-06-11 13:15:08 -0400554 std::move(finishedCallback), &data);
Robert Phillips66944402019-09-30 13:21:25 -0400555}
556
Greg Danielb2365d82020-05-13 15:32:04 -0400557bool GrContext::updateBackendTexture(const GrBackendTexture& backendTexture,
558 const SkColor4f& color,
559 GrGpuFinishedProc finishedProc,
560 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400561 sk_sp<GrRefCntedCallback> finishedCallback;
562 if (finishedProc) {
563 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
564 }
565
Greg Danielb2365d82020-05-13 15:32:04 -0400566 if (!this->asDirectContext()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400567 return false;
568 }
569
570 if (this->abandoned()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400571 return false;
572 }
573
574 GrGpu::BackendTextureData data(color);
Greg Daniel25597782020-06-11 13:15:08 -0400575 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
Greg Danielb2365d82020-05-13 15:32:04 -0400576}
577
578bool GrContext::updateBackendTexture(const GrBackendTexture& backendTexture,
Greg Daniel373d7dd2020-07-21 10:41:50 -0400579 SkColorType skColorType,
580 const SkColor4f& color,
581 GrGpuFinishedProc finishedProc,
582 GrGpuFinishedContext finishedContext) {
583 sk_sp<GrRefCntedCallback> finishedCallback;
584 if (finishedProc) {
585 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
586 }
587
588 if (!this->asDirectContext()) {
589 return false;
590 }
591
592 if (this->abandoned()) {
593 return false;
594 }
595
596 GrBackendFormat format = backendTexture.getBackendFormat();
597 GrColorType grColorType = SkColorTypeAndFormatToGrColorType(this->caps(), skColorType, format);
598
599 if (!this->caps()->areColorTypeAndFormatCompatible(grColorType, format)) {
600 return false;
601 }
602
603 GrSwizzle swizzle = this->caps()->getWriteSwizzle(format, grColorType);
604 GrGpu::BackendTextureData data(swizzle.applyTo(color));
605
606 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
607}
608
609bool GrContext::updateBackendTexture(const GrBackendTexture& backendTexture,
Greg Danielb2365d82020-05-13 15:32:04 -0400610 const SkPixmap srcData[],
611 int numLevels,
612 GrGpuFinishedProc finishedProc,
613 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400614 sk_sp<GrRefCntedCallback> finishedCallback;
615 if (finishedProc) {
616 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
617 }
618
Greg Danielb2365d82020-05-13 15:32:04 -0400619 if (!this->asDirectContext()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400620 return false;
621 }
622
623 if (this->abandoned()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400624 return false;
625 }
626
627 if (!srcData || numLevels <= 0) {
Greg Danielb2365d82020-05-13 15:32:04 -0400628 return false;
629 }
630
631 int numExpectedLevels = 1;
Brian Salomon40a40622020-07-21 10:32:07 -0400632 if (backendTexture.hasMipmaps()) {
Mike Reed13711eb2020-07-14 17:16:32 -0400633 numExpectedLevels = SkMipmap::ComputeLevelCount(backendTexture.width(),
Greg Danielb2365d82020-05-13 15:32:04 -0400634 backendTexture.height()) + 1;
635 }
636 if (numLevels != numExpectedLevels) {
Greg Danielb2365d82020-05-13 15:32:04 -0400637 return false;
638 }
639
640 GrGpu::BackendTextureData data(srcData);
Greg Daniel25597782020-06-11 13:15:08 -0400641 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
Greg Danielb2365d82020-05-13 15:32:04 -0400642}
643
Robert Phillipsb915c942019-12-17 14:44:37 -0500644//////////////////////////////////////////////////////////////////////////////
645
Greg Danielaaf738c2020-07-10 09:30:33 -0400646static GrBackendTexture create_and_update_compressed_backend_texture(
Robert Phillips4e105e22020-07-16 09:18:50 -0400647 GrDirectContext* context,
Greg Danielaaf738c2020-07-10 09:30:33 -0400648 SkISize dimensions,
649 const GrBackendFormat& backendFormat,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400650 GrMipmapped mipMapped,
Greg Danielaaf738c2020-07-10 09:30:33 -0400651 GrProtected isProtected,
652 sk_sp<GrRefCntedCallback> finishedCallback,
653 const GrGpu::BackendTextureData* data) {
654 GrGpu* gpu = context->priv().getGpu();
655
656 GrBackendTexture beTex = gpu->createCompressedBackendTexture(dimensions, backendFormat,
657 mipMapped, isProtected);
658 if (!beTex.isValid()) {
659 return {};
660 }
661
662 if (!context->priv().getGpu()->updateCompressedBackendTexture(
663 beTex, std::move(finishedCallback), data)) {
664 context->deleteBackendTexture(beTex);
665 return {};
666 }
667 return beTex;
668}
669
Robert Phillipsb915c942019-12-17 14:44:37 -0500670GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
671 const GrBackendFormat& backendFormat,
672 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400673 GrMipmapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400674 GrProtected isProtected,
675 GrGpuFinishedProc finishedProc,
676 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500677 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel25597782020-06-11 13:15:08 -0400678 sk_sp<GrRefCntedCallback> finishedCallback;
679 if (finishedProc) {
680 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
681 }
682
Robert Phillipsb915c942019-12-17 14:44:37 -0500683 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400684 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500685 }
686
687 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400688 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500689 }
690
691 GrGpu::BackendTextureData data(color);
Robert Phillips4e105e22020-07-16 09:18:50 -0400692 return create_and_update_compressed_backend_texture(this->asDirectContext(), {width, height},
693 backendFormat, mipMapped, isProtected,
Greg Danielaaf738c2020-07-10 09:30:33 -0400694 std::move(finishedCallback), &data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500695}
696
697GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
698 SkImage::CompressionType compression,
699 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400700 GrMipmapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400701 GrProtected isProtected,
702 GrGpuFinishedProc finishedProc,
703 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500704 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillipsb915c942019-12-17 14:44:37 -0500705 GrBackendFormat format = this->compressedBackendFormat(compression);
706 return this->createCompressedBackendTexture(width, height, format, color,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400707 mipMapped, isProtected, finishedProc,
708 finishedContext);
Robert Phillipsb915c942019-12-17 14:44:37 -0500709}
710
711GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
712 const GrBackendFormat& backendFormat,
713 const void* compressedData,
714 size_t dataSize,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400715 GrMipmapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400716 GrProtected isProtected,
717 GrGpuFinishedProc finishedProc,
718 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500719 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel25597782020-06-11 13:15:08 -0400720 sk_sp<GrRefCntedCallback> finishedCallback;
721 if (finishedProc) {
722 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
723 }
724
Robert Phillipsb915c942019-12-17 14:44:37 -0500725 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400726 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500727 }
728
729 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400730 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500731 }
732
733 GrGpu::BackendTextureData data(compressedData, dataSize);
Robert Phillips4e105e22020-07-16 09:18:50 -0400734 return create_and_update_compressed_backend_texture(this->asDirectContext(), {width, height},
735 backendFormat, mipMapped, isProtected,
Greg Danielaaf738c2020-07-10 09:30:33 -0400736 std::move(finishedCallback), &data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500737}
738
739GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
740 SkImage::CompressionType compression,
741 const void* data, size_t dataSize,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400742 GrMipmapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400743 GrProtected isProtected,
744 GrGpuFinishedProc finishedProc,
745 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500746 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillipsb915c942019-12-17 14:44:37 -0500747 GrBackendFormat format = this->compressedBackendFormat(compression);
Greg Daniel25597782020-06-11 13:15:08 -0400748 return this->createCompressedBackendTexture(width, height, format, data, dataSize, mipMapped,
749 isProtected, finishedProc, finishedContext);
Robert Phillipsb915c942019-12-17 14:44:37 -0500750}
751
Greg Daniel1db8e792020-06-09 17:29:32 -0400752bool GrContext::setBackendTextureState(const GrBackendTexture& backendTexture,
753 const GrBackendSurfaceMutableState& state,
754 GrGpuFinishedProc finishedProc,
755 GrGpuFinishedContext finishedContext) {
Greg Daniel1db8e792020-06-09 17:29:32 -0400756 sk_sp<GrRefCntedCallback> callback;
757 if (finishedProc) {
758 callback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
759 }
Greg Daniel25597782020-06-11 13:15:08 -0400760
761 if (!this->asDirectContext()) {
762 return false;
763 }
764
765 if (this->abandoned()) {
766 return false;
767 }
768
Greg Daniel1db8e792020-06-09 17:29:32 -0400769 return fGpu->setBackendTextureState(backendTexture, state, std::move(callback));
770}
771
Greg Daniel95afafb2020-07-22 12:09:26 -0400772bool GrContext::updateCompressedBackendTexture(const GrBackendTexture& backendTexture,
773 const SkColor4f& color,
774 GrGpuFinishedProc finishedProc,
775 GrGpuFinishedContext finishedContext) {
776 sk_sp<GrRefCntedCallback> finishedCallback;
777 if (finishedProc) {
778 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
779 }
780
781 if (!this->asDirectContext()) {
782 return false;
783 }
784
785 if (this->abandoned()) {
786 return false;
787 }
788
789 GrGpu::BackendTextureData data(color);
790 return fGpu->updateCompressedBackendTexture(backendTexture, std::move(finishedCallback), &data);
791}
792
793bool GrContext::updateCompressedBackendTexture(const GrBackendTexture& backendTexture,
794 const void* compressedData,
795 size_t dataSize,
796 GrGpuFinishedProc finishedProc,
797 GrGpuFinishedContext finishedContext) {
798 sk_sp<GrRefCntedCallback> finishedCallback;
799 if (finishedProc) {
800 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
801 }
802
803 if (!this->asDirectContext()) {
804 return false;
805 }
806
807 if (this->abandoned()) {
808 return false;
809 }
810
811 if (!compressedData) {
812 return false;
813 }
814
815 GrGpu::BackendTextureData data(compressedData, dataSize);
816
817 return fGpu->updateCompressedBackendTexture(backendTexture, std::move(finishedCallback), &data);
818}
819
820//////////////////////////////////////////////////////////////////////////////
821
Greg Daniel1db8e792020-06-09 17:29:32 -0400822bool GrContext::setBackendRenderTargetState(const GrBackendRenderTarget& backendRenderTarget,
823 const GrBackendSurfaceMutableState& state,
824 GrGpuFinishedProc finishedProc,
825 GrGpuFinishedContext finishedContext) {
Greg Daniel1db8e792020-06-09 17:29:32 -0400826 sk_sp<GrRefCntedCallback> callback;
827 if (finishedProc) {
828 callback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
829 }
Greg Daniel25597782020-06-11 13:15:08 -0400830
831 if (!this->asDirectContext()) {
832 return false;
833 }
834
835 if (this->abandoned()) {
836 return false;
837 }
838
Greg Daniel1db8e792020-06-09 17:29:32 -0400839 return fGpu->setBackendRenderTargetState(backendRenderTarget, state, std::move(callback));
840}
841
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400842void GrContext::deleteBackendTexture(GrBackendTexture backendTex) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400843 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danielf0e04f02019-12-04 15:17:54 -0500844 // For the Vulkan backend we still must destroy the backend texture when the context is
845 // abandoned.
846 if ((this->abandoned() && this->backend() != GrBackendApi::kVulkan) || !backendTex.isValid()) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400847 return;
848 }
849
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400850 fGpu->deleteBackendTexture(backendTex);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400851}
852
Greg Daniel1db8e792020-06-09 17:29:32 -0400853//////////////////////////////////////////////////////////////////////////////
854
Brian Osmaned58e002019-09-06 14:42:43 -0400855bool GrContext::precompileShader(const SkData& key, const SkData& data) {
856 return fGpu->precompileShader(key, data);
857}
858
Brian Salomonec22b1a2019-08-09 09:41:48 -0400859#ifdef SK_ENABLE_DUMP_GPU
Michael Ludwigdd205452020-03-30 17:16:34 -0400860#include "include/core/SkString.h"
Brian Salomonec22b1a2019-08-09 09:41:48 -0400861#include "src/utils/SkJSONWriter.h"
862SkString GrContext::dump() const {
863 SkDynamicMemoryWStream stream;
864 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
865 writer.beginObject();
866
867 writer.appendString("backend", GrBackendApiToStr(this->backend()));
868
869 writer.appendName("caps");
870 this->caps()->dumpJSON(&writer);
871
872 writer.appendName("gpu");
873 this->fGpu->dumpJSON(&writer);
874
Robert Phillips273f1072020-05-05 13:03:07 -0400875 writer.appendName("context");
876 this->dumpJSON(&writer);
877
Brian Salomonec22b1a2019-08-09 09:41:48 -0400878 // Flush JSON to the memory stream
879 writer.endObject();
880 writer.flush();
881
882 // Null terminate the JSON data in the memory stream
883 stream.write8(0);
884
885 // Allocate a string big enough to hold all the data, then copy out of the stream
886 SkString result(stream.bytesWritten());
887 stream.copyToAndReset(result.writable_str());
888 return result;
889}
890#endif