blob: 2739138b044d3d0f2578153d4f2bc34d0c66f8c2 [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"
30#include "src/gpu/GrTracing.h"
31#include "src/gpu/SkGr.h"
32#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
33#include "src/gpu/effects/GrSkSLFP.h"
Herb Derbya08bde62020-06-12 15:46:06 -040034#include "src/gpu/text/GrSDFTOptions.h"
Robert Phillips41bd97d2020-04-07 14:19:37 -040035#include "src/gpu/text/GrStrikeCache.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/gpu/text/GrTextBlobCache.h"
Greg Daniel7fd7a8a2019-10-10 16:10:31 -040037#include "src/image/SkImage_GpuBase.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "src/image/SkSurface_Gpu.h"
Mike Klein0ec1c572018-12-04 11:52:51 -050039#include <atomic>
John Stilesfbd050b2020-08-03 13:21:46 -040040#include <memory>
Greg Danielb76a72a2017-07-13 15:07:54 -040041
Robert Phillipse78b7252017-04-06 07:59:41 -040042#define ASSERT_OWNED_PROXY(P) \
Brian Salomonfd98c2c2018-07-31 17:25:29 -040043 SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == this)
Robert Phillips7ee385e2017-03-30 08:02:11 -040044
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000045#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
Adlai Holler33dbd652020-06-01 12:35:42 -040046#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner())
Robert Phillipsa9162df2019-02-11 14:12:03 -050047#define RETURN_IF_ABANDONED if (this->abandoned()) { return; }
48#define RETURN_FALSE_IF_ABANDONED if (this->abandoned()) { return false; }
49#define RETURN_NULL_IF_ABANDONED if (this->abandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000050
robertphillipsea461502015-05-26 11:38:03 -070051////////////////////////////////////////////////////////////////////////////////
52
Adlai Hollere219d1c2020-06-02 11:23:16 -040053GrContext::GrContext(sk_sp<GrContextThreadSafeProxy> proxy) : INHERITED(std::move(proxy)) {
halcanary96fcdcc2015-08-27 07:41:13 -070054 fResourceCache = nullptr;
55 fResourceProvider = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000056}
57
Robert Phillips292a6b22019-02-14 14:49:02 -050058GrContext::~GrContext() {
59 ASSERT_SINGLE_OWNER
60
Adlai Holler96ead542020-06-26 08:50:14 -040061 this->destroyDrawingManager();
Greg Daniel5ed3c112020-06-18 15:59:17 -040062 fMappedBufferManager.reset();
Robert Phillips292a6b22019-02-14 14:49:02 -050063 delete fResourceProvider;
64 delete fResourceCache;
Robert Phillips292a6b22019-02-14 14:49:02 -050065}
66
Adlai Hollere219d1c2020-06-02 11:23:16 -040067bool GrContext::init() {
Greg Danielb76a72a2017-07-13 15:07:54 -040068 ASSERT_SINGLE_OWNER
Robert Phillipsa41c6852019-02-07 10:44:10 -050069 SkASSERT(this->proxyProvider());
Robert Phillips88260b52018-01-19 12:56:09 -050070
Adlai Hollere219d1c2020-06-02 11:23:16 -040071 if (!INHERITED::init()) {
Robert Phillipsbb606772019-02-04 17:50:57 -050072 return false;
73 }
74
Robert Phillips2184fb72019-02-21 16:11:41 -050075 SkASSERT(this->getTextBlobCache());
Robert Phillipsbb606772019-02-04 17:50:57 -050076
Robert Phillips88260b52018-01-19 12:56:09 -050077 if (fGpu) {
John Stilesfbd050b2020-08-03 13:21:46 -040078 fStrikeCache = std::make_unique<GrStrikeCache>();
Robert Phillipsa41c6852019-02-07 10:44:10 -050079 fResourceCache = new GrResourceCache(this->caps(), this->singleOwner(), this->contextID());
Robert Phillips12c46292019-04-23 07:36:17 -040080 fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, this->singleOwner());
Mike Kleinf46d5ca2019-12-11 10:45:01 -050081 fMappedBufferManager = std::make_unique<GrClientMappedBufferManager>(this->contextID());
Robert Phillips88260b52018-01-19 12:56:09 -050082 }
83
Robert Phillips88260b52018-01-19 12:56:09 -050084 if (fResourceCache) {
Robert Phillipsa41c6852019-02-07 10:44:10 -050085 fResourceCache->setProxyProvider(this->proxyProvider());
Robert Phillips88260b52018-01-19 12:56:09 -050086 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050087
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000088 fDidTestPMConversions = false;
89
Robert Phillipsfde6fa02018-03-02 08:53:14 -050090 // DDL TODO: we need to think through how the task group & persistent cache
91 // get passed on to/shared between all the DDLRecorders created with this context.
Robert Phillipsc1541ae2019-02-04 12:05:37 -050092 if (this->options().fExecutor) {
Mike Kleinf46d5ca2019-12-11 10:45:01 -050093 fTaskGroup = std::make_unique<SkTaskGroup>(*this->options().fExecutor);
Brian Osman51279982017-08-23 10:12:00 -040094 }
95
Robert Phillipsc1541ae2019-02-04 12:05:37 -050096 fPersistentCache = this->options().fPersistentCache;
Brian Osman5e7fbfd2019-05-03 13:13:35 -040097 fShaderErrorHandler = this->options().fShaderErrorHandler;
98 if (!fShaderErrorHandler) {
99 fShaderErrorHandler = GrShaderUtils::DefaultShaderErrorHandler();
100 }
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400101
Brian Salomon91a3e522017-06-23 10:58:19 -0400102 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000103}
104
Robert Phillips4217ea72019-01-30 13:08:28 -0500105sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
Adlai Hollere219d1c2020-06-02 11:23:16 -0400106 return INHERITED::threadSafeProxy();
Robert Phillips4217ea72019-01-30 13:08:28 -0500107}
108
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400109//////////////////////////////////////////////////////////////////////////////
110
bsalomon2354f842014-07-28 13:48:36 -0700111void GrContext::abandonContext() {
Greg Daniel6e35a002020-04-01 13:29:59 -0400112 if (INHERITED::abandoned()) {
Robert Phillipsa9162df2019-02-11 14:12:03 -0500113 return;
114 }
joshualitt1de610a2016-01-06 08:26:09 -0800115
Robert Phillipsa9162df2019-02-11 14:12:03 -0500116 INHERITED::abandonContext();
117
Robert Phillips4d932d12020-04-09 08:58:52 -0400118 fStrikeCache->freeAll();
119
Brian Salomon9241a6d2019-10-03 13:26:54 -0400120 fMappedBufferManager->abandon();
121
bsalomond309e7a2015-04-30 14:18:54 -0700122 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800123
bsalomon@google.com205d4602011-04-25 12:43:45 +0000124 // abandon first to so destructors
125 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800126 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700127
bsalomon6e2aad42016-04-01 11:54:31 -0700128 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400129
130 fMappedBufferManager.reset();
Khushalc421ca12018-06-26 14:38:34 -0700131}
132
bsalomon6e2aad42016-04-01 11:54:31 -0700133void GrContext::releaseResourcesAndAbandonContext() {
Greg Daniel6e35a002020-04-01 13:29:59 -0400134 if (INHERITED::abandoned()) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500135 return;
136 }
Robert Phillipsa9162df2019-02-11 14:12:03 -0500137
138 INHERITED::abandonContext();
139
Brian Salomon9241a6d2019-10-03 13:26:54 -0400140 fMappedBufferManager.reset();
141
bsalomon6e2aad42016-04-01 11:54:31 -0700142 fResourceProvider->abandon();
143
bsalomon6e2aad42016-04-01 11:54:31 -0700144 // Release all resources in the backend 3D API.
145 fResourceCache->releaseAll();
146
147 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000148}
149
Greg Daniel6e35a002020-04-01 13:29:59 -0400150bool GrContext::abandoned() {
151 if (INHERITED::abandoned()) {
152 return true;
153 }
154
155 if (fGpu && fGpu->isDeviceLost()) {
156 this->abandonContext();
157 return true;
158 }
159 return false;
160}
161
Brian Salomon24069eb2020-06-24 10:19:52 -0400162bool GrContext::oomed() { return fGpu ? fGpu->checkAndResetOOMed() : false; }
163
Brian Salomon1f05d452019-02-08 12:33:08 -0500164void GrContext::resetGLTextureBindings() {
165 if (this->abandoned() || this->backend() != GrBackendApi::kOpenGL) {
166 return;
167 }
168 fGpu->resetTextureBindings();
169}
170
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000171void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800172 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000173 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000174}
175
176void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800177 ASSERT_SINGLE_OWNER
178
Adlai Holler9d8a41c2020-06-25 14:01:58 +0000179 if (this->abandoned()) {
180 return;
181 }
182
Robert Phillips2184fb72019-02-21 16:11:41 -0500183 // TODO: the glyph cache doesn't hold any GpuResources so this call should not be needed here.
184 // Some slack in the GrTextBlob's implementation requires it though. That could be fixed.
Robert Phillips4d932d12020-04-09 08:58:52 -0400185 fStrikeCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700186
Robert Phillips6a6de562019-02-15 15:19:15 -0500187 this->drawingManager()->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700188
189 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000190}
191
Robert Phillips6eba0632018-03-28 12:25:42 -0400192void GrContext::purgeUnlockedResources(bool scratchResourcesOnly) {
193 ASSERT_SINGLE_OWNER
Robert Phillipsddc21482019-10-16 14:30:09 -0400194
195 if (this->abandoned()) {
196 return;
197 }
198
Robert Phillips6eba0632018-03-28 12:25:42 -0400199 fResourceCache->purgeUnlockedResources(scratchResourcesOnly);
200 fResourceCache->purgeAsNeeded();
Robert Phillips2184fb72019-02-21 16:11:41 -0500201
202 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
203 // place to purge stale blobs
204 this->getTextBlobCache()->purgeStaleBlobs();
Robert Phillips6eba0632018-03-28 12:25:42 -0400205}
206
Jim Van Verth76d917c2017-12-13 09:26:37 -0500207void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Yuqian Li40aa85f2019-07-02 13:45:00 -0700208 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
209
Brian Salomon5e150852017-03-22 14:53:13 -0400210 ASSERT_SINGLE_OWNER
Chris Dalton6c3879d2018-11-01 11:13:19 -0600211
Brian Salomon9241a6d2019-10-03 13:26:54 -0400212 if (this->abandoned()) {
213 return;
214 }
215
Jim Van Verthe0da3792020-08-14 16:50:19 -0400216 this->checkAsyncWorkCompletion();
Brian Salomon9241a6d2019-10-03 13:26:54 -0400217 fMappedBufferManager->process();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600218 auto purgeTime = GrStdSteadyClock::now() - msNotUsed;
219
Jim Van Verth76d917c2017-12-13 09:26:37 -0500220 fResourceCache->purgeAsNeeded();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600221 fResourceCache->purgeResourcesNotUsedSince(purgeTime);
222
Robert Phillips6a6de562019-02-15 15:19:15 -0500223 if (auto ccpr = this->drawingManager()->getCoverageCountingPathRenderer()) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500224 ccpr->purgeCacheEntriesOlderThan(this->proxyProvider(), purgeTime);
Chris Dalton6c3879d2018-11-01 11:13:19 -0600225 }
Jim Van Verth76d917c2017-12-13 09:26:37 -0500226
Robert Phillips2184fb72019-02-21 16:11:41 -0500227 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
228 // place to purge stale blobs
229 this->getTextBlobCache()->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400230}
231
Derek Sollenberger5480a182017-05-25 16:43:59 -0400232void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
233 ASSERT_SINGLE_OWNER
Robert Phillipsddc21482019-10-16 14:30:09 -0400234
235 if (this->abandoned()) {
236 return;
237 }
238
Derek Sollenberger5480a182017-05-25 16:43:59 -0400239 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
240}
241
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000242void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800243 ASSERT_SINGLE_OWNER
244
bsalomon71cb0c22014-11-14 12:10:14 -0800245 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800246 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800247 }
248 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800249 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800250 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000251}
252
Derek Sollenbergeree479142017-05-24 11:41:33 -0400253size_t GrContext::getResourceCachePurgeableBytes() const {
254 ASSERT_SINGLE_OWNER
255 return fResourceCache->getPurgeableBytes();
256}
257
Brian Salomon7e67dca2020-07-21 09:27:25 -0400258size_t GrContext::ComputeImageSize(sk_sp<SkImage> image, GrMipmapped mipMapped, bool useNextPow2) {
Greg Daniel8b666172019-10-09 12:38:22 -0400259 if (!image->isTextureBacked()) {
260 return 0;
261 }
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400262 SkImage_GpuBase* gpuImage = static_cast<SkImage_GpuBase*>(as_IB(image.get()));
263 GrTextureProxy* proxy = gpuImage->peekProxy();
264 if (!proxy) {
265 return 0;
266 }
267
268 const GrCaps& caps = *gpuImage->context()->priv().caps();
Greg Daniel8b666172019-10-09 12:38:22 -0400269 int colorSamplesPerPixel = 1;
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400270 return GrSurface::ComputeSize(caps, proxy->backendFormat(), image->dimensions(),
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400271 colorSamplesPerPixel, mipMapped, useNextPow2);
Greg Daniel8b666172019-10-09 12:38:22 -0400272}
273
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000274////////////////////////////////////////////////////////////////////////////////
275
Greg Daniel414418d2020-07-08 11:44:25 -0400276bool GrContext::wait(int numSemaphores, const GrBackendSemaphore waitSemaphores[],
277 bool deleteSemaphoresAfterWait) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400278 if (!fGpu || fGpu->caps()->semaphoreSupport()) {
Greg Daniel06be0792019-04-22 15:53:23 -0400279 return false;
280 }
Greg Daniel414418d2020-07-08 11:44:25 -0400281 GrWrapOwnership ownership =
282 deleteSemaphoresAfterWait ? kAdopt_GrWrapOwnership : kBorrow_GrWrapOwnership;
Greg Daniel06be0792019-04-22 15:53:23 -0400283 for (int i = 0; i < numSemaphores; ++i) {
Greg Daniel301015c2019-11-18 14:06:46 -0500284 std::unique_ptr<GrSemaphore> sema = fResourceProvider->wrapBackendSemaphore(
Greg Daniel414418d2020-07-08 11:44:25 -0400285 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait, ownership);
Greg Daniel0106fcc2020-07-01 17:40:12 -0400286 // If we failed to wrap the semaphore it means the client didn't give us a valid semaphore
287 // to begin with. Therefore, it is fine to not wait on it.
288 if (sema) {
289 fGpu->waitSemaphore(sema.get());
290 }
Greg Daniel06be0792019-04-22 15:53:23 -0400291 }
292 return true;
293}
294
295////////////////////////////////////////////////////////////////////////////////
296
Greg Daniele8d3cca2020-06-10 10:04:48 -0400297GrSemaphoresSubmitted GrContext::flush(const GrFlushInfo& info) {
Greg Daniel51316782017-08-02 15:10:09 +0000298 ASSERT_SINGLE_OWNER
Robert Phillipsa9162df2019-02-11 14:12:03 -0500299 if (this->abandoned()) {
Greg Daniel55822f12020-05-26 11:26:45 -0400300 if (info.fFinishedProc) {
301 info.fFinishedProc(info.fFinishedContext);
302 }
303 if (info.fSubmittedProc) {
304 info.fSubmittedProc(info.fSubmittedContext, false);
305 }
Robert Phillipsa9162df2019-02-11 14:12:03 -0500306 return GrSemaphoresSubmitted::kNo;
307 }
Greg Daniel51316782017-08-02 15:10:09 +0000308
Greg Daniel04283f32020-05-20 13:16:00 -0400309 bool flushed = this->drawingManager()->flush(
Greg Daniel9efe3862020-06-11 11:51:06 -0400310 nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, info, nullptr);
Greg Danielfe159622020-04-10 17:43:51 +0000311
Greg Daniel04283f32020-05-20 13:16:00 -0400312 if (!flushed || (!this->priv().caps()->semaphoreSupport() && info.fNumSemaphores)) {
Greg Danielfe159622020-04-10 17:43:51 +0000313 return GrSemaphoresSubmitted::kNo;
314 }
315 return GrSemaphoresSubmitted::kYes;
Greg Daniel51316782017-08-02 15:10:09 +0000316}
317
Greg Daniel04283f32020-05-20 13:16:00 -0400318bool GrContext::submit(bool syncCpu) {
319 ASSERT_SINGLE_OWNER
320 if (this->abandoned()) {
321 return false;
322 }
323
324 if (!fGpu) {
325 return false;
326 }
327
328 return fGpu->submitToGpu(syncCpu);
Greg Danielda50cb82020-05-13 14:07:40 -0400329}
330
Greg Daniela870b462019-01-08 15:49:46 -0500331////////////////////////////////////////////////////////////////////////////////
332
Brian Salomonb0d8b762019-05-06 16:58:22 -0400333void GrContext::checkAsyncWorkCompletion() {
334 if (fGpu) {
335 fGpu->checkFinishProcs();
336 }
337}
338
339////////////////////////////////////////////////////////////////////////////////
340
Greg Daniela870b462019-01-08 15:49:46 -0500341void GrContext::storeVkPipelineCacheData() {
342 if (fGpu) {
343 fGpu->storeVkPipelineCacheData();
344 }
345}
346
347////////////////////////////////////////////////////////////////////////////////
348
Khushal3e7548c2018-05-23 15:45:01 -0700349bool GrContext::supportsDistanceFieldText() const {
Robert Phillipsbb606772019-02-04 17:50:57 -0500350 return this->caps()->shaderCaps()->supportsDistanceFieldText();
Khushal3e7548c2018-05-23 15:45:01 -0700351}
352
bsalomon37f9a262015-02-02 13:00:10 -0800353//////////////////////////////////////////////////////////////////////////////
354
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500355void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800356 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500357 if (maxResources) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400358 *maxResources = -1;
bsalomon37f9a262015-02-02 13:00:10 -0800359 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500360 if (maxResourceBytes) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400361 *maxResourceBytes = this->getResourceCacheLimit();
bsalomon37f9a262015-02-02 13:00:10 -0800362 }
363}
364
Robert Phillipscf39f372019-09-03 10:29:20 -0400365size_t GrContext::getResourceCacheLimit() const {
joshualitt1de610a2016-01-06 08:26:09 -0800366 ASSERT_SINGLE_OWNER
Robert Phillipscf39f372019-09-03 10:29:20 -0400367 return fResourceCache->getMaxResourceBytes();
368}
369
370void GrContext::setResourceCacheLimits(int unused, size_t maxResourceBytes) {
371 ASSERT_SINGLE_OWNER
372 this->setResourceCacheLimit(maxResourceBytes);
373}
374
375void GrContext::setResourceCacheLimit(size_t maxResourceBytes) {
376 ASSERT_SINGLE_OWNER
377 fResourceCache->setLimit(maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800378}
379
ericrk0a5fa482015-09-15 14:16:10 -0700380//////////////////////////////////////////////////////////////////////////////
ericrk0a5fa482015-09-15 14:16:10 -0700381void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800382 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700383 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
Khushal71652e22018-10-29 13:05:36 -0700384 traceMemoryDump->dumpNumericValue("skia/gr_text_blob_cache", "size", "bytes",
Robert Phillips2184fb72019-02-21 16:11:41 -0500385 this->getTextBlobCache()->usedBytes());
ericrk0a5fa482015-09-15 14:16:10 -0700386}
Brian Osman71a18892017-08-10 10:23:25 -0400387
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400388//////////////////////////////////////////////////////////////////////////////
389GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400390 const GrBackendFormat& backendFormat,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400391 GrMipmapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400392 GrRenderable renderable,
393 GrProtected isProtected) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400394 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400395 if (!this->asDirectContext()) {
396 return GrBackendTexture();
397 }
398
399 if (this->abandoned()) {
400 return GrBackendTexture();
401 }
402
Robert Phillips4277f012020-01-21 14:28:34 -0500403 return fGpu->createBackendTexture({width, height}, backendFormat, renderable,
Greg Daniel16032b32020-05-06 15:31:10 -0400404 mipMapped, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400405}
406
407GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400408 SkColorType skColorType,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400409 GrMipmapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400410 GrRenderable renderable,
411 GrProtected isProtected) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400412 if (!this->asDirectContext()) {
413 return GrBackendTexture();
414 }
415
416 if (this->abandoned()) {
417 return GrBackendTexture();
418 }
419
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400420 const GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400421
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400422 return this->createBackendTexture(width, height, format, mipMapped, renderable, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400423}
424
Greg Daniel25597782020-06-11 13:15:08 -0400425static GrBackendTexture create_and_update_backend_texture(
Robert Phillips4e105e22020-07-16 09:18:50 -0400426 GrDirectContext* context,
Greg Daniel25597782020-06-11 13:15:08 -0400427 SkISize dimensions,
428 const GrBackendFormat& backendFormat,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400429 GrMipmapped mipMapped,
Greg Daniel25597782020-06-11 13:15:08 -0400430 GrRenderable renderable,
431 GrProtected isProtected,
432 sk_sp<GrRefCntedCallback> finishedCallback,
433 const GrGpu::BackendTextureData* data) {
Greg Daniel16032b32020-05-06 15:31:10 -0400434 GrGpu* gpu = context->priv().getGpu();
435
436 GrBackendTexture beTex = gpu->createBackendTexture(dimensions, backendFormat, renderable,
437 mipMapped, isProtected);
438 if (!beTex.isValid()) {
439 return {};
440 }
441
Greg Daniel25597782020-06-11 13:15:08 -0400442 if (!context->priv().getGpu()->updateBackendTexture(beTex, std::move(finishedCallback), data)) {
Greg Daniel16032b32020-05-06 15:31:10 -0400443 context->deleteBackendTexture(beTex);
444 return {};
445 }
446 return beTex;
447}
448
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400449GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400450 const GrBackendFormat& backendFormat,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400451 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400452 GrMipmapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400453 GrRenderable renderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400454 GrProtected isProtected,
455 GrGpuFinishedProc finishedProc,
456 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400457 sk_sp<GrRefCntedCallback> finishedCallback;
458 if (finishedProc) {
459 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
460 }
461
Brian Salomonc42eb662019-06-24 17:13:00 -0400462 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400463 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400464 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400465 }
466
467 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400468 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400469 }
470
Brian Salomon85c3d682019-11-04 15:04:54 -0500471 GrGpu::BackendTextureData data(color);
Robert Phillips4e105e22020-07-16 09:18:50 -0400472 return create_and_update_backend_texture(this->asDirectContext(), {width, height},
473 backendFormat, mipMapped, renderable, isProtected,
474 std::move(finishedCallback), &data);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400475}
476
477GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400478 SkColorType skColorType,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400479 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400480 GrMipmapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400481 GrRenderable renderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400482 GrProtected isProtected,
483 GrGpuFinishedProc finishedProc,
484 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400485 sk_sp<GrRefCntedCallback> finishedCallback;
486 if (finishedProc) {
487 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
488 }
489
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400490 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400491 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400492 }
493
494 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400495 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400496 }
497
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400498 GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400499 if (!format.isValid()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400500 return {};
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400501 }
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400502
503 GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
Brian Salomon982f5462020-03-30 12:52:33 -0400504 SkColor4f swizzledColor = this->caps()->getWriteSwizzle(format, grColorType).applyTo(color);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400505
Greg Daniel25597782020-06-11 13:15:08 -0400506 GrGpu::BackendTextureData data(swizzledColor);
Robert Phillips4e105e22020-07-16 09:18:50 -0400507 return create_and_update_backend_texture(this->asDirectContext(), {width, height}, format,
508 mipMapped, renderable, isProtected,
509 std::move(finishedCallback), &data);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400510}
511
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500512GrBackendTexture GrContext::createBackendTexture(const SkPixmap srcData[], int numProvidedLevels,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400513 GrRenderable renderable, GrProtected isProtected,
514 GrGpuFinishedProc finishedProc,
515 GrGpuFinishedContext finishedContext) {
Robert Phillips66944402019-09-30 13:21:25 -0400516 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
517
Greg Daniel25597782020-06-11 13:15:08 -0400518 sk_sp<GrRefCntedCallback> finishedCallback;
519 if (finishedProc) {
520 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
521 }
522
Robert Phillips66944402019-09-30 13:21:25 -0400523 if (!this->asDirectContext()) {
524 return {};
525 }
526
527 if (this->abandoned()) {
528 return {};
529 }
530
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500531 if (!srcData || numProvidedLevels <= 0) {
Robert Phillips66944402019-09-30 13:21:25 -0400532 return {};
533 }
534
535 int baseWidth = srcData[0].width();
536 int baseHeight = srcData[0].height();
537 SkColorType colorType = srcData[0].colorType();
538
Brian Salomon7e67dca2020-07-21 09:27:25 -0400539 GrMipmapped mipMapped = GrMipmapped::kNo;
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500540 int numExpectedLevels = 1;
541 if (numProvidedLevels > 1) {
Mike Reed13711eb2020-07-14 17:16:32 -0400542 numExpectedLevels = SkMipmap::ComputeLevelCount(baseWidth, baseHeight) + 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400543 mipMapped = GrMipmapped::kYes;
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500544 }
545
546 if (numProvidedLevels != numExpectedLevels) {
547 return {};
548 }
549
Robert Phillips66944402019-09-30 13:21:25 -0400550 GrBackendFormat backendFormat = this->defaultBackendFormat(colorType, renderable);
551
Brian Salomon85c3d682019-11-04 15:04:54 -0500552 GrGpu::BackendTextureData data(srcData);
Robert Phillips4e105e22020-07-16 09:18:50 -0400553 return create_and_update_backend_texture(this->asDirectContext(), {baseWidth, baseHeight},
554 backendFormat, mipMapped, renderable, isProtected,
Greg Daniel25597782020-06-11 13:15:08 -0400555 std::move(finishedCallback), &data);
Robert Phillips66944402019-09-30 13:21:25 -0400556}
557
Greg Danielb2365d82020-05-13 15:32:04 -0400558bool GrContext::updateBackendTexture(const GrBackendTexture& backendTexture,
559 const SkColor4f& color,
560 GrGpuFinishedProc finishedProc,
561 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400562 sk_sp<GrRefCntedCallback> finishedCallback;
563 if (finishedProc) {
564 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
565 }
566
Greg Danielb2365d82020-05-13 15:32:04 -0400567 if (!this->asDirectContext()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400568 return false;
569 }
570
571 if (this->abandoned()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400572 return false;
573 }
574
575 GrGpu::BackendTextureData data(color);
Greg Daniel25597782020-06-11 13:15:08 -0400576 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
Greg Danielb2365d82020-05-13 15:32:04 -0400577}
578
579bool GrContext::updateBackendTexture(const GrBackendTexture& backendTexture,
Greg Daniel373d7dd2020-07-21 10:41:50 -0400580 SkColorType skColorType,
581 const SkColor4f& color,
582 GrGpuFinishedProc finishedProc,
583 GrGpuFinishedContext finishedContext) {
584 sk_sp<GrRefCntedCallback> finishedCallback;
585 if (finishedProc) {
586 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
587 }
588
589 if (!this->asDirectContext()) {
590 return false;
591 }
592
593 if (this->abandoned()) {
594 return false;
595 }
596
597 GrBackendFormat format = backendTexture.getBackendFormat();
598 GrColorType grColorType = SkColorTypeAndFormatToGrColorType(this->caps(), skColorType, format);
599
600 if (!this->caps()->areColorTypeAndFormatCompatible(grColorType, format)) {
601 return false;
602 }
603
604 GrSwizzle swizzle = this->caps()->getWriteSwizzle(format, grColorType);
605 GrGpu::BackendTextureData data(swizzle.applyTo(color));
606
607 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
608}
609
610bool GrContext::updateBackendTexture(const GrBackendTexture& backendTexture,
Greg Danielb2365d82020-05-13 15:32:04 -0400611 const SkPixmap srcData[],
612 int numLevels,
613 GrGpuFinishedProc finishedProc,
614 GrGpuFinishedContext finishedContext) {
Greg Daniel25597782020-06-11 13:15:08 -0400615 sk_sp<GrRefCntedCallback> finishedCallback;
616 if (finishedProc) {
617 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
618 }
619
Greg Danielb2365d82020-05-13 15:32:04 -0400620 if (!this->asDirectContext()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400621 return false;
622 }
623
624 if (this->abandoned()) {
Greg Danielb2365d82020-05-13 15:32:04 -0400625 return false;
626 }
627
628 if (!srcData || numLevels <= 0) {
Greg Danielb2365d82020-05-13 15:32:04 -0400629 return false;
630 }
631
632 int numExpectedLevels = 1;
Brian Salomon40a40622020-07-21 10:32:07 -0400633 if (backendTexture.hasMipmaps()) {
Mike Reed13711eb2020-07-14 17:16:32 -0400634 numExpectedLevels = SkMipmap::ComputeLevelCount(backendTexture.width(),
Greg Danielb2365d82020-05-13 15:32:04 -0400635 backendTexture.height()) + 1;
636 }
637 if (numLevels != numExpectedLevels) {
Greg Danielb2365d82020-05-13 15:32:04 -0400638 return false;
639 }
640
641 GrGpu::BackendTextureData data(srcData);
Greg Daniel25597782020-06-11 13:15:08 -0400642 return fGpu->updateBackendTexture(backendTexture, std::move(finishedCallback), &data);
Greg Danielb2365d82020-05-13 15:32:04 -0400643}
644
Robert Phillipsb915c942019-12-17 14:44:37 -0500645//////////////////////////////////////////////////////////////////////////////
646
Greg Danielaaf738c2020-07-10 09:30:33 -0400647static GrBackendTexture create_and_update_compressed_backend_texture(
Robert Phillips4e105e22020-07-16 09:18:50 -0400648 GrDirectContext* context,
Greg Danielaaf738c2020-07-10 09:30:33 -0400649 SkISize dimensions,
650 const GrBackendFormat& backendFormat,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400651 GrMipmapped mipMapped,
Greg Danielaaf738c2020-07-10 09:30:33 -0400652 GrProtected isProtected,
653 sk_sp<GrRefCntedCallback> finishedCallback,
654 const GrGpu::BackendTextureData* data) {
655 GrGpu* gpu = context->priv().getGpu();
656
657 GrBackendTexture beTex = gpu->createCompressedBackendTexture(dimensions, backendFormat,
658 mipMapped, isProtected);
659 if (!beTex.isValid()) {
660 return {};
661 }
662
663 if (!context->priv().getGpu()->updateCompressedBackendTexture(
664 beTex, std::move(finishedCallback), data)) {
665 context->deleteBackendTexture(beTex);
666 return {};
667 }
668 return beTex;
669}
670
Robert Phillipsb915c942019-12-17 14:44:37 -0500671GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
672 const GrBackendFormat& backendFormat,
673 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400674 GrMipmapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400675 GrProtected isProtected,
676 GrGpuFinishedProc finishedProc,
677 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500678 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel25597782020-06-11 13:15:08 -0400679 sk_sp<GrRefCntedCallback> finishedCallback;
680 if (finishedProc) {
681 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
682 }
683
Robert Phillipsb915c942019-12-17 14:44:37 -0500684 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400685 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500686 }
687
688 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400689 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500690 }
691
692 GrGpu::BackendTextureData data(color);
Robert Phillips4e105e22020-07-16 09:18:50 -0400693 return create_and_update_compressed_backend_texture(this->asDirectContext(), {width, height},
694 backendFormat, mipMapped, isProtected,
Greg Danielaaf738c2020-07-10 09:30:33 -0400695 std::move(finishedCallback), &data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500696}
697
698GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
699 SkImage::CompressionType compression,
700 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400701 GrMipmapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400702 GrProtected isProtected,
703 GrGpuFinishedProc finishedProc,
704 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500705 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillipsb915c942019-12-17 14:44:37 -0500706 GrBackendFormat format = this->compressedBackendFormat(compression);
707 return this->createCompressedBackendTexture(width, height, format, color,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400708 mipMapped, isProtected, finishedProc,
709 finishedContext);
Robert Phillipsb915c942019-12-17 14:44:37 -0500710}
711
712GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
713 const GrBackendFormat& backendFormat,
714 const void* compressedData,
715 size_t dataSize,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400716 GrMipmapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400717 GrProtected isProtected,
718 GrGpuFinishedProc finishedProc,
719 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500720 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel25597782020-06-11 13:15:08 -0400721 sk_sp<GrRefCntedCallback> finishedCallback;
722 if (finishedProc) {
723 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
724 }
725
Robert Phillipsb915c942019-12-17 14:44:37 -0500726 if (!this->asDirectContext()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400727 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500728 }
729
730 if (this->abandoned()) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400731 return {};
Robert Phillipsb915c942019-12-17 14:44:37 -0500732 }
733
734 GrGpu::BackendTextureData data(compressedData, dataSize);
Robert Phillips4e105e22020-07-16 09:18:50 -0400735 return create_and_update_compressed_backend_texture(this->asDirectContext(), {width, height},
736 backendFormat, mipMapped, isProtected,
Greg Danielaaf738c2020-07-10 09:30:33 -0400737 std::move(finishedCallback), &data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500738}
739
740GrBackendTexture GrContext::createCompressedBackendTexture(int width, int height,
741 SkImage::CompressionType compression,
742 const void* data, size_t dataSize,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400743 GrMipmapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400744 GrProtected isProtected,
745 GrGpuFinishedProc finishedProc,
746 GrGpuFinishedContext finishedContext) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500747 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillipsb915c942019-12-17 14:44:37 -0500748 GrBackendFormat format = this->compressedBackendFormat(compression);
Greg Daniel25597782020-06-11 13:15:08 -0400749 return this->createCompressedBackendTexture(width, height, format, data, dataSize, mipMapped,
750 isProtected, finishedProc, finishedContext);
Robert Phillipsb915c942019-12-17 14:44:37 -0500751}
752
Greg Daniel1db8e792020-06-09 17:29:32 -0400753bool GrContext::setBackendTextureState(const GrBackendTexture& backendTexture,
754 const GrBackendSurfaceMutableState& state,
755 GrGpuFinishedProc finishedProc,
756 GrGpuFinishedContext finishedContext) {
Greg Daniel1db8e792020-06-09 17:29:32 -0400757 sk_sp<GrRefCntedCallback> callback;
758 if (finishedProc) {
759 callback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
760 }
Greg Daniel25597782020-06-11 13:15:08 -0400761
762 if (!this->asDirectContext()) {
763 return false;
764 }
765
766 if (this->abandoned()) {
767 return false;
768 }
769
Greg Daniel1db8e792020-06-09 17:29:32 -0400770 return fGpu->setBackendTextureState(backendTexture, state, std::move(callback));
771}
772
Greg Daniel95afafb2020-07-22 12:09:26 -0400773bool GrContext::updateCompressedBackendTexture(const GrBackendTexture& backendTexture,
774 const SkColor4f& color,
775 GrGpuFinishedProc finishedProc,
776 GrGpuFinishedContext finishedContext) {
777 sk_sp<GrRefCntedCallback> finishedCallback;
778 if (finishedProc) {
779 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
780 }
781
782 if (!this->asDirectContext()) {
783 return false;
784 }
785
786 if (this->abandoned()) {
787 return false;
788 }
789
790 GrGpu::BackendTextureData data(color);
791 return fGpu->updateCompressedBackendTexture(backendTexture, std::move(finishedCallback), &data);
792}
793
794bool GrContext::updateCompressedBackendTexture(const GrBackendTexture& backendTexture,
795 const void* compressedData,
796 size_t dataSize,
797 GrGpuFinishedProc finishedProc,
798 GrGpuFinishedContext finishedContext) {
799 sk_sp<GrRefCntedCallback> finishedCallback;
800 if (finishedProc) {
801 finishedCallback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
802 }
803
804 if (!this->asDirectContext()) {
805 return false;
806 }
807
808 if (this->abandoned()) {
809 return false;
810 }
811
812 if (!compressedData) {
813 return false;
814 }
815
816 GrGpu::BackendTextureData data(compressedData, dataSize);
817
818 return fGpu->updateCompressedBackendTexture(backendTexture, std::move(finishedCallback), &data);
819}
820
821//////////////////////////////////////////////////////////////////////////////
822
Greg Daniel1db8e792020-06-09 17:29:32 -0400823bool GrContext::setBackendRenderTargetState(const GrBackendRenderTarget& backendRenderTarget,
824 const GrBackendSurfaceMutableState& state,
825 GrGpuFinishedProc finishedProc,
826 GrGpuFinishedContext finishedContext) {
Greg Daniel1db8e792020-06-09 17:29:32 -0400827 sk_sp<GrRefCntedCallback> callback;
828 if (finishedProc) {
829 callback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
830 }
Greg Daniel25597782020-06-11 13:15:08 -0400831
832 if (!this->asDirectContext()) {
833 return false;
834 }
835
836 if (this->abandoned()) {
837 return false;
838 }
839
Greg Daniel1db8e792020-06-09 17:29:32 -0400840 return fGpu->setBackendRenderTargetState(backendRenderTarget, state, std::move(callback));
841}
842
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400843void GrContext::deleteBackendTexture(GrBackendTexture backendTex) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400844 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danielf0e04f02019-12-04 15:17:54 -0500845 // For the Vulkan backend we still must destroy the backend texture when the context is
846 // abandoned.
847 if ((this->abandoned() && this->backend() != GrBackendApi::kVulkan) || !backendTex.isValid()) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400848 return;
849 }
850
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400851 fGpu->deleteBackendTexture(backendTex);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400852}
853
Greg Daniel1db8e792020-06-09 17:29:32 -0400854//////////////////////////////////////////////////////////////////////////////
855
Brian Osmaned58e002019-09-06 14:42:43 -0400856bool GrContext::precompileShader(const SkData& key, const SkData& data) {
857 return fGpu->precompileShader(key, data);
858}
859
Brian Salomonec22b1a2019-08-09 09:41:48 -0400860#ifdef SK_ENABLE_DUMP_GPU
Michael Ludwigdd205452020-03-30 17:16:34 -0400861#include "include/core/SkString.h"
Brian Salomonec22b1a2019-08-09 09:41:48 -0400862#include "src/utils/SkJSONWriter.h"
863SkString GrContext::dump() const {
864 SkDynamicMemoryWStream stream;
865 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
866 writer.beginObject();
867
868 writer.appendString("backend", GrBackendApiToStr(this->backend()));
869
870 writer.appendName("caps");
871 this->caps()->dumpJSON(&writer);
872
873 writer.appendName("gpu");
874 this->fGpu->dumpJSON(&writer);
875
Robert Phillips273f1072020-05-05 13:03:07 -0400876 writer.appendName("context");
877 this->dumpJSON(&writer);
878
Brian Salomonec22b1a2019-08-09 09:41:48 -0400879 // Flush JSON to the memory stream
880 writer.endObject();
881 writer.flush();
882
883 // Null terminate the JSON data in the memory stream
884 stream.write8(0);
885
886 // Allocate a string big enough to hold all the data, then copy out of the stream
887 SkString result(stream.bytesWritten());
888 stream.copyToAndReset(result.writable_str());
889 return result;
890}
891#endif