blob: b5c96d1eafad66e189254e05d9c26c92c6cf0439 [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
Brian Salomon9241a6d2019-10-03 13:26:54 -04008#include "include/gpu/GrContext.h"
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkTraceMemoryDump.h"
11#include "include/gpu/GrBackendSemaphore.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/private/SkDeferredDisplayList.h"
13#include "include/private/SkImageInfoPriv.h"
14#include "src/core/SkMakeUnique.h"
15#include "src/core/SkTaskGroup.h"
Brian Salomon9241a6d2019-10-03 13:26:54 -040016#include "src/gpu/GrClientMappedBufferManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrDrawingManager.h"
18#include "src/gpu/GrGpu.h"
19#include "src/gpu/GrMemoryPool.h"
20#include "src/gpu/GrPathRendererChain.h"
21#include "src/gpu/GrProxyProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040022#include "src/gpu/GrRenderTargetProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrResourceCache.h"
24#include "src/gpu/GrResourceProvider.h"
25#include "src/gpu/GrSemaphore.h"
Brian Osman5e7fbfd2019-05-03 13:13:35 -040026#include "src/gpu/GrShaderUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrSoftwarePathRenderer.h"
28#include "src/gpu/GrTracing.h"
29#include "src/gpu/SkGr.h"
30#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
31#include "src/gpu/effects/GrSkSLFP.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/gpu/text/GrTextBlobCache.h"
33#include "src/gpu/text/GrTextContext.h"
34#include "src/image/SkSurface_Gpu.h"
Mike Klein0ec1c572018-12-04 11:52:51 -050035#include <atomic>
Greg Danielb76a72a2017-07-13 15:07:54 -040036
Robert Phillipse78b7252017-04-06 07:59:41 -040037#define ASSERT_OWNED_PROXY(P) \
Brian Salomonfd98c2c2018-07-31 17:25:29 -040038 SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == this)
Robert Phillips7ee385e2017-03-30 08:02:11 -040039
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000040#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt1de610a2016-01-06 08:26:09 -080041#define ASSERT_SINGLE_OWNER \
Robert Phillipsa41c6852019-02-07 10:44:10 -050042 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
Robert Phillipsa9162df2019-02-11 14:12:03 -050043#define RETURN_IF_ABANDONED if (this->abandoned()) { return; }
44#define RETURN_FALSE_IF_ABANDONED if (this->abandoned()) { return false; }
45#define RETURN_NULL_IF_ABANDONED if (this->abandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000046
robertphillipsea461502015-05-26 11:38:03 -070047////////////////////////////////////////////////////////////////////////////////
48
Robert Phillipsa41c6852019-02-07 10:44:10 -050049GrContext::GrContext(GrBackendApi backend, const GrContextOptions& options, int32_t contextID)
50 : INHERITED(backend, options, contextID) {
halcanary96fcdcc2015-08-27 07:41:13 -070051 fResourceCache = nullptr;
52 fResourceProvider = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000053}
54
Robert Phillips292a6b22019-02-14 14:49:02 -050055GrContext::~GrContext() {
56 ASSERT_SINGLE_OWNER
57
Robert Phillips6a6de562019-02-15 15:19:15 -050058 if (this->drawingManager()) {
59 this->drawingManager()->cleanup();
Robert Phillips292a6b22019-02-14 14:49:02 -050060 }
61 delete fResourceProvider;
62 delete fResourceCache;
Robert Phillips292a6b22019-02-14 14:49:02 -050063}
64
Robert Phillipsbb606772019-02-04 17:50:57 -050065bool GrContext::init(sk_sp<const GrCaps> caps, sk_sp<GrSkSLFPFactoryCache> FPFactoryCache) {
Greg Danielb76a72a2017-07-13 15:07:54 -040066 ASSERT_SINGLE_OWNER
Robert Phillipsfde6fa02018-03-02 08:53:14 -050067 SkASSERT(fThreadSafeProxy); // needs to have been initialized by derived classes
Robert Phillipsa41c6852019-02-07 10:44:10 -050068 SkASSERT(this->proxyProvider());
Robert Phillips88260b52018-01-19 12:56:09 -050069
Robert Phillipsbb606772019-02-04 17:50:57 -050070 if (!INHERITED::init(std::move(caps), std::move(FPFactoryCache))) {
71 return false;
72 }
73
74 SkASSERT(this->caps());
Herb Derbya00da612019-03-04 17:10:01 -050075 SkASSERT(this->getGrStrikeCache());
Robert Phillips2184fb72019-02-21 16:11:41 -050076 SkASSERT(this->getTextBlobCache());
Robert Phillipsbb606772019-02-04 17:50:57 -050077
Robert Phillips88260b52018-01-19 12:56:09 -050078 if (fGpu) {
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());
Brian Salomon9241a6d2019-10-03 13:26:54 -040081 fMappedBufferManager = skstd::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) {
93 fTaskGroup = skstd::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() {
106 return fThreadSafeProxy;
107}
108
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400109//////////////////////////////////////////////////////////////////////////////
110
bsalomon2354f842014-07-28 13:48:36 -0700111void GrContext::abandonContext() {
Robert Phillipsa9162df2019-02-11 14:12:03 -0500112 if (this->abandoned()) {
113 return;
114 }
joshualitt1de610a2016-01-06 08:26:09 -0800115
Robert Phillipsa9162df2019-02-11 14:12:03 -0500116 INHERITED::abandonContext();
117
Brian Salomon9241a6d2019-10-03 13:26:54 -0400118 fMappedBufferManager->abandon();
119
bsalomond309e7a2015-04-30 14:18:54 -0700120 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800121
Robert Phillipsa9162df2019-02-11 14:12:03 -0500122 // Need to cleanup the drawing manager first so all the render targets
robertphillips0dfa62c2015-11-16 06:23:31 -0800123 // will be released/forgotten before they too are abandoned.
Robert Phillips6a6de562019-02-15 15:19:15 -0500124 this->drawingManager()->cleanup();
robertphillips0dfa62c2015-11-16 06:23:31 -0800125
bsalomon@google.com205d4602011-04-25 12:43:45 +0000126 // abandon first to so destructors
127 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800128 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700129
bsalomon6e2aad42016-04-01 11:54:31 -0700130 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
Brian Salomon9241a6d2019-10-03 13:26:54 -0400131
132 fMappedBufferManager.reset();
Khushalc421ca12018-06-26 14:38:34 -0700133}
134
bsalomon6e2aad42016-04-01 11:54:31 -0700135void GrContext::releaseResourcesAndAbandonContext() {
Brian Salomon614c1a82018-12-19 15:42:06 -0500136 if (this->abandoned()) {
137 return;
138 }
Robert Phillipsa9162df2019-02-11 14:12:03 -0500139
140 INHERITED::abandonContext();
141
Brian Salomon9241a6d2019-10-03 13:26:54 -0400142 fMappedBufferManager.reset();
143
bsalomon6e2aad42016-04-01 11:54:31 -0700144 fResourceProvider->abandon();
145
Robert Phillipsa9162df2019-02-11 14:12:03 -0500146 // Need to cleanup the drawing manager first so all the render targets
bsalomon6e2aad42016-04-01 11:54:31 -0700147 // will be released/forgotten before they too are abandoned.
Robert Phillips6a6de562019-02-15 15:19:15 -0500148 this->drawingManager()->cleanup();
bsalomon6e2aad42016-04-01 11:54:31 -0700149
150 // Release all resources in the backend 3D API.
151 fResourceCache->releaseAll();
152
153 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000154}
155
Brian Salomon1f05d452019-02-08 12:33:08 -0500156void GrContext::resetGLTextureBindings() {
157 if (this->abandoned() || this->backend() != GrBackendApi::kOpenGL) {
158 return;
159 }
160 fGpu->resetTextureBindings();
161}
162
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000163void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800164 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000165 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000166}
167
168void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800169 ASSERT_SINGLE_OWNER
170
Robert Phillips2184fb72019-02-21 16:11:41 -0500171 // TODO: the glyph cache doesn't hold any GpuResources so this call should not be needed here.
172 // Some slack in the GrTextBlob's implementation requires it though. That could be fixed.
Herb Derbya00da612019-03-04 17:10:01 -0500173 this->getGrStrikeCache()->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700174
Robert Phillips6a6de562019-02-15 15:19:15 -0500175 this->drawingManager()->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700176
177 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000178}
179
Robert Phillips6eba0632018-03-28 12:25:42 -0400180void GrContext::purgeUnlockedResources(bool scratchResourcesOnly) {
181 ASSERT_SINGLE_OWNER
182 fResourceCache->purgeUnlockedResources(scratchResourcesOnly);
183 fResourceCache->purgeAsNeeded();
Robert Phillips2184fb72019-02-21 16:11:41 -0500184
185 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
186 // place to purge stale blobs
187 this->getTextBlobCache()->purgeStaleBlobs();
Robert Phillips6eba0632018-03-28 12:25:42 -0400188}
189
Jim Van Verth76d917c2017-12-13 09:26:37 -0500190void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Yuqian Li40aa85f2019-07-02 13:45:00 -0700191 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
192
Brian Salomon5e150852017-03-22 14:53:13 -0400193 ASSERT_SINGLE_OWNER
Chris Dalton6c3879d2018-11-01 11:13:19 -0600194
Brian Salomon9241a6d2019-10-03 13:26:54 -0400195 if (this->abandoned()) {
196 return;
197 }
198
199 fMappedBufferManager->process();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600200 auto purgeTime = GrStdSteadyClock::now() - msNotUsed;
201
Jim Van Verth76d917c2017-12-13 09:26:37 -0500202 fResourceCache->purgeAsNeeded();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600203 fResourceCache->purgeResourcesNotUsedSince(purgeTime);
204
Robert Phillips6a6de562019-02-15 15:19:15 -0500205 if (auto ccpr = this->drawingManager()->getCoverageCountingPathRenderer()) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500206 ccpr->purgeCacheEntriesOlderThan(this->proxyProvider(), purgeTime);
Chris Dalton6c3879d2018-11-01 11:13:19 -0600207 }
Jim Van Verth76d917c2017-12-13 09:26:37 -0500208
Robert Phillips2184fb72019-02-21 16:11:41 -0500209 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
210 // place to purge stale blobs
211 this->getTextBlobCache()->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400212}
213
Derek Sollenberger5480a182017-05-25 16:43:59 -0400214void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
215 ASSERT_SINGLE_OWNER
216 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
217}
218
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000219void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800220 ASSERT_SINGLE_OWNER
221
bsalomon71cb0c22014-11-14 12:10:14 -0800222 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800223 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800224 }
225 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800226 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800227 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000228}
229
Derek Sollenbergeree479142017-05-24 11:41:33 -0400230size_t GrContext::getResourceCachePurgeableBytes() const {
231 ASSERT_SINGLE_OWNER
232 return fResourceCache->getPurgeableBytes();
233}
234
Adrienne Walker0f827972019-03-26 13:46:14 -0700235size_t GrContext::ComputeTextureSize(SkColorType type, int width, int height, GrMipMapped mipMapped,
236 bool useNextPow2) {
237 int colorSamplesPerPixel = 1;
238 return GrSurface::ComputeSize(SkColorType2GrPixelConfig(type), width, height,
239 colorSamplesPerPixel, mipMapped, useNextPow2);
240}
241
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000242////////////////////////////////////////////////////////////////////////////////
243
Robert Phillipsbb606772019-02-04 17:50:57 -0500244int GrContext::maxTextureSize() const { return this->caps()->maxTextureSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400245
Robert Phillipsbb606772019-02-04 17:50:57 -0500246int GrContext::maxRenderTargetSize() const { return this->caps()->maxRenderTargetSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400247
Brian Salomonbdecacf2018-02-02 20:32:49 -0500248bool GrContext::colorTypeSupportedAsImage(SkColorType colorType) const {
Greg Daniel7bfc9132019-08-14 14:23:53 -0400249 GrBackendFormat format =
250 this->caps()->getDefaultBackendFormat(SkColorTypeToGrColorType(colorType),
251 GrRenderable::kNo);
252 return format.isValid();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500253}
254
255int GrContext::maxSurfaceSampleCountForColorType(SkColorType colorType) const {
Greg Danieleadfac92019-08-02 09:03:53 -0400256 GrBackendFormat format =
257 this->caps()->getDefaultBackendFormat(SkColorTypeToGrColorType(colorType),
258 GrRenderable::kYes);
259 return this->caps()->maxRenderTargetSampleCount(format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500260}
261
262////////////////////////////////////////////////////////////////////////////////
263
Greg Daniel06be0792019-04-22 15:53:23 -0400264bool GrContext::wait(int numSemaphores, const GrBackendSemaphore waitSemaphores[]) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400265 if (!fGpu || fGpu->caps()->semaphoreSupport()) {
Greg Daniel06be0792019-04-22 15:53:23 -0400266 return false;
267 }
268 for (int i = 0; i < numSemaphores; ++i) {
269 sk_sp<GrSemaphore> sema = fResourceProvider->wrapBackendSemaphore(
270 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
271 kAdopt_GrWrapOwnership);
272 fGpu->waitSemaphore(std::move(sema));
273 }
274 return true;
275}
276
277////////////////////////////////////////////////////////////////////////////////
278
Greg Daniel797efca2019-05-09 14:04:20 -0400279GrSemaphoresSubmitted GrContext::flush(const GrFlushInfo& info,
280 const GrPrepareForExternalIORequests& externalRequests) {
Greg Daniel51316782017-08-02 15:10:09 +0000281 ASSERT_SINGLE_OWNER
Robert Phillipsa9162df2019-02-11 14:12:03 -0500282 if (this->abandoned()) {
283 return GrSemaphoresSubmitted::kNo;
284 }
Greg Daniel51316782017-08-02 15:10:09 +0000285
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400286 return this->drawingManager()->flush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess,
Greg Daniel797efca2019-05-09 14:04:20 -0400287 info, externalRequests);
Greg Daniel51316782017-08-02 15:10:09 +0000288}
289
Greg Daniela870b462019-01-08 15:49:46 -0500290////////////////////////////////////////////////////////////////////////////////
291
Brian Salomonb0d8b762019-05-06 16:58:22 -0400292void GrContext::checkAsyncWorkCompletion() {
293 if (fGpu) {
294 fGpu->checkFinishProcs();
295 }
296}
297
298////////////////////////////////////////////////////////////////////////////////
299
Greg Daniela870b462019-01-08 15:49:46 -0500300void GrContext::storeVkPipelineCacheData() {
301 if (fGpu) {
302 fGpu->storeVkPipelineCacheData();
303 }
304}
305
306////////////////////////////////////////////////////////////////////////////////
307
Khushal3e7548c2018-05-23 15:45:01 -0700308bool GrContext::supportsDistanceFieldText() const {
Robert Phillipsbb606772019-02-04 17:50:57 -0500309 return this->caps()->shaderCaps()->supportsDistanceFieldText();
Khushal3e7548c2018-05-23 15:45:01 -0700310}
311
bsalomon37f9a262015-02-02 13:00:10 -0800312//////////////////////////////////////////////////////////////////////////////
313
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500314void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800315 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500316 if (maxResources) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400317 *maxResources = -1;
bsalomon37f9a262015-02-02 13:00:10 -0800318 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500319 if (maxResourceBytes) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400320 *maxResourceBytes = this->getResourceCacheLimit();
bsalomon37f9a262015-02-02 13:00:10 -0800321 }
322}
323
Robert Phillipscf39f372019-09-03 10:29:20 -0400324size_t GrContext::getResourceCacheLimit() const {
joshualitt1de610a2016-01-06 08:26:09 -0800325 ASSERT_SINGLE_OWNER
Robert Phillipscf39f372019-09-03 10:29:20 -0400326 return fResourceCache->getMaxResourceBytes();
327}
328
329void GrContext::setResourceCacheLimits(int unused, size_t maxResourceBytes) {
330 ASSERT_SINGLE_OWNER
331 this->setResourceCacheLimit(maxResourceBytes);
332}
333
334void GrContext::setResourceCacheLimit(size_t maxResourceBytes) {
335 ASSERT_SINGLE_OWNER
336 fResourceCache->setLimit(maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800337}
338
ericrk0a5fa482015-09-15 14:16:10 -0700339//////////////////////////////////////////////////////////////////////////////
ericrk0a5fa482015-09-15 14:16:10 -0700340void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800341 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700342 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
Khushal71652e22018-10-29 13:05:36 -0700343 traceMemoryDump->dumpNumericValue("skia/gr_text_blob_cache", "size", "bytes",
Robert Phillips2184fb72019-02-21 16:11:41 -0500344 this->getTextBlobCache()->usedBytes());
ericrk0a5fa482015-09-15 14:16:10 -0700345}
Brian Osman71a18892017-08-10 10:23:25 -0400346
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400347//////////////////////////////////////////////////////////////////////////////
348GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400349 const GrBackendFormat& backendFormat,
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400350 GrMipMapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400351 GrRenderable renderable,
352 GrProtected isProtected) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400353 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400354 if (!this->asDirectContext()) {
355 return GrBackendTexture();
356 }
357
358 if (this->abandoned()) {
359 return GrBackendTexture();
360 }
361
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400362 return fGpu->createBackendTexture(width, height, backendFormat,
363 mipMapped, renderable,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400364 nullptr, 0, nullptr, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400365}
366
367GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400368 SkColorType skColorType,
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400369 GrMipMapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400370 GrRenderable renderable,
371 GrProtected isProtected) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400372 if (!this->asDirectContext()) {
373 return GrBackendTexture();
374 }
375
376 if (this->abandoned()) {
377 return GrBackendTexture();
378 }
379
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400380 const GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400381
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400382 return this->createBackendTexture(width, height, format, mipMapped, renderable, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400383}
384
Robert Phillips02dc0302019-07-02 17:58:27 -0400385GrBackendTexture GrContext::createBackendTexture(const SkSurfaceCharacterization& c) {
Robert Phillips02dc0302019-07-02 17:58:27 -0400386 if (!this->asDirectContext() || !c.isValid()) {
387 return GrBackendTexture();
388 }
389
390 if (this->abandoned()) {
391 return GrBackendTexture();
392 }
393
394 if (c.usesGLFBO0()) {
395 // If we are making the surface we will never use FBO0.
396 return GrBackendTexture();
397 }
398
399 if (c.vulkanSecondaryCBCompatible()) {
400 return {};
401 }
402
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400403 const GrBackendFormat format = this->defaultBackendFormat(c.colorType(), GrRenderable::kYes);
Robert Phillips02dc0302019-07-02 17:58:27 -0400404 if (!format.isValid()) {
405 return GrBackendTexture();
406 }
407
Robert Phillips02dc0302019-07-02 17:58:27 -0400408 GrBackendTexture result = this->createBackendTexture(c.width(), c.height(), format,
409 GrMipMapped(c.isMipMapped()),
410 GrRenderable::kYes,
Robert Phillips3cd54322019-07-10 09:28:59 -0400411 c.isProtected());
Robert Phillips02dc0302019-07-02 17:58:27 -0400412 SkASSERT(c.isCompatible(result));
413 return result;
414}
415
416GrBackendTexture GrContext::createBackendTexture(const SkSurfaceCharacterization& c,
417 const SkColor4f& color) {
Robert Phillips02dc0302019-07-02 17:58:27 -0400418 if (!this->asDirectContext() || !c.isValid()) {
419 return GrBackendTexture();
420 }
421
422 if (this->abandoned()) {
423 return GrBackendTexture();
424 }
425
426 if (c.usesGLFBO0()) {
427 // If we are making the surface we will never use FBO0.
428 return GrBackendTexture();
429 }
430
431 if (c.vulkanSecondaryCBCompatible()) {
432 return {};
433 }
434
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400435 const GrBackendFormat format = this->defaultBackendFormat(c.colorType(), GrRenderable::kYes);
Robert Phillips02dc0302019-07-02 17:58:27 -0400436 if (!format.isValid()) {
437 return GrBackendTexture();
438 }
439
Robert Phillips02dc0302019-07-02 17:58:27 -0400440 GrBackendTexture result = this->createBackendTexture(c.width(), c.height(), format, color,
441 GrMipMapped(c.isMipMapped()),
442 GrRenderable::kYes,
Robert Phillips3cd54322019-07-10 09:28:59 -0400443 c.isProtected());
Robert Phillips02dc0302019-07-02 17:58:27 -0400444 SkASSERT(c.isCompatible(result));
445 return result;
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,
451 GrMipMapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400452 GrRenderable renderable,
453 GrProtected isProtected) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400454 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400455 if (!this->asDirectContext()) {
456 return GrBackendTexture();
457 }
458
459 if (this->abandoned()) {
460 return GrBackendTexture();
461 }
462
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400463 return fGpu->createBackendTexture(width, height, backendFormat,
464 mipMapped, renderable,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400465 nullptr, 0, &color, isProtected);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400466}
467
468GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400469 SkColorType skColorType,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400470 const SkColor4f& color,
471 GrMipMapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400472 GrRenderable renderable,
473 GrProtected isProtected) {
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400474 if (!this->asDirectContext()) {
475 return GrBackendTexture();
476 }
477
478 if (this->abandoned()) {
479 return GrBackendTexture();
480 }
481
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400482 GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400483 if (!format.isValid()) {
484 return GrBackendTexture();
485 }
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400486
487 GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
488 SkColor4f swizzledColor = this->caps()->getOutputSwizzle(format, grColorType).applyTo(color);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400489
Brian Salomonb450f3b2019-07-09 09:36:51 -0400490 return this->createBackendTexture(width, height, format, swizzledColor, mipMapped, renderable,
491 isProtected);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400492}
493
Robert Phillips66944402019-09-30 13:21:25 -0400494GrBackendTexture GrContext::createBackendTexture(const SkPixmap srcData[], int numLevels,
495 GrRenderable renderable, GrProtected isProtected) {
496 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
497
498 if (!this->asDirectContext()) {
499 return {};
500 }
501
502 if (this->abandoned()) {
503 return {};
504 }
505
506 if (!srcData || !numLevels) {
507 return {};
508 }
509
510 int baseWidth = srcData[0].width();
511 int baseHeight = srcData[0].height();
512 SkColorType colorType = srcData[0].colorType();
513
514 GrBackendFormat backendFormat = this->defaultBackendFormat(colorType, renderable);
515
516 return fGpu->createBackendTexture(baseWidth, baseHeight, backendFormat,
517 numLevels > 1 ? GrMipMapped::kYes : GrMipMapped::kNo,
518 renderable, srcData, numLevels, nullptr, isProtected);
519}
520
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400521void GrContext::deleteBackendTexture(GrBackendTexture backendTex) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400522 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400523 if (this->abandoned() || !backendTex.isValid()) {
524 return;
525 }
526
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400527 fGpu->deleteBackendTexture(backendTex);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400528}
529
Brian Osmaned58e002019-09-06 14:42:43 -0400530bool GrContext::precompileShader(const SkData& key, const SkData& data) {
531 return fGpu->precompileShader(key, data);
532}
533
Brian Salomonec22b1a2019-08-09 09:41:48 -0400534#ifdef SK_ENABLE_DUMP_GPU
535#include "src/utils/SkJSONWriter.h"
536SkString GrContext::dump() const {
537 SkDynamicMemoryWStream stream;
538 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
539 writer.beginObject();
540
541 writer.appendString("backend", GrBackendApiToStr(this->backend()));
542
543 writer.appendName("caps");
544 this->caps()->dumpJSON(&writer);
545
546 writer.appendName("gpu");
547 this->fGpu->dumpJSON(&writer);
548
549 // Flush JSON to the memory stream
550 writer.endObject();
551 writer.flush();
552
553 // Null terminate the JSON data in the memory stream
554 stream.write8(0);
555
556 // Allocate a string big enough to hold all the data, then copy out of the stream
557 SkString result(stream.bytesWritten());
558 stream.copyToAndReset(result.writable_str());
559 return result;
560}
561#endif