blob: a41922de5f358f986110b31dfe080c4cbcf51a53 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkTraceMemoryDump.h"
9#include "include/gpu/GrBackendSemaphore.h"
Ravi Mistrycb550102019-10-03 09:06:25 +000010#include "include/gpu/GrContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/private/SkDeferredDisplayList.h"
12#include "include/private/SkImageInfoPriv.h"
13#include "src/core/SkMakeUnique.h"
14#include "src/core/SkTaskGroup.h"
15#include "src/gpu/GrDrawingManager.h"
16#include "src/gpu/GrGpu.h"
17#include "src/gpu/GrMemoryPool.h"
18#include "src/gpu/GrPathRendererChain.h"
19#include "src/gpu/GrProxyProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040020#include "src/gpu/GrRenderTargetProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrResourceCache.h"
22#include "src/gpu/GrResourceProvider.h"
23#include "src/gpu/GrSemaphore.h"
Brian Osman5e7fbfd2019-05-03 13:13:35 -040024#include "src/gpu/GrShaderUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrSoftwarePathRenderer.h"
26#include "src/gpu/GrTracing.h"
27#include "src/gpu/SkGr.h"
28#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
29#include "src/gpu/effects/GrSkSLFP.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/text/GrTextBlobCache.h"
31#include "src/gpu/text/GrTextContext.h"
32#include "src/image/SkSurface_Gpu.h"
Mike Klein0ec1c572018-12-04 11:52:51 -050033#include <atomic>
Ravi Mistrycb550102019-10-03 09:06:25 +000034#include <unordered_map>
Greg Danielb76a72a2017-07-13 15:07:54 -040035
Robert Phillipse78b7252017-04-06 07:59:41 -040036#define ASSERT_OWNED_PROXY(P) \
Brian Salomonfd98c2c2018-07-31 17:25:29 -040037 SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == this)
Robert Phillips7ee385e2017-03-30 08:02:11 -040038
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000039#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt1de610a2016-01-06 08:26:09 -080040#define ASSERT_SINGLE_OWNER \
Robert Phillipsa41c6852019-02-07 10:44:10 -050041 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
Robert Phillipsa9162df2019-02-11 14:12:03 -050042#define RETURN_IF_ABANDONED if (this->abandoned()) { return; }
43#define RETURN_FALSE_IF_ABANDONED if (this->abandoned()) { return false; }
44#define RETURN_NULL_IF_ABANDONED if (this->abandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000045
robertphillipsea461502015-05-26 11:38:03 -070046////////////////////////////////////////////////////////////////////////////////
47
Robert Phillipsa41c6852019-02-07 10:44:10 -050048GrContext::GrContext(GrBackendApi backend, const GrContextOptions& options, int32_t contextID)
49 : INHERITED(backend, options, contextID) {
halcanary96fcdcc2015-08-27 07:41:13 -070050 fResourceCache = nullptr;
51 fResourceProvider = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000052}
53
Robert Phillips292a6b22019-02-14 14:49:02 -050054GrContext::~GrContext() {
55 ASSERT_SINGLE_OWNER
56
Robert Phillips6a6de562019-02-15 15:19:15 -050057 if (this->drawingManager()) {
58 this->drawingManager()->cleanup();
Robert Phillips292a6b22019-02-14 14:49:02 -050059 }
60 delete fResourceProvider;
61 delete fResourceCache;
Robert Phillips292a6b22019-02-14 14:49:02 -050062}
63
Robert Phillipsbb606772019-02-04 17:50:57 -050064bool GrContext::init(sk_sp<const GrCaps> caps, sk_sp<GrSkSLFPFactoryCache> FPFactoryCache) {
Greg Danielb76a72a2017-07-13 15:07:54 -040065 ASSERT_SINGLE_OWNER
Robert Phillipsfde6fa02018-03-02 08:53:14 -050066 SkASSERT(fThreadSafeProxy); // needs to have been initialized by derived classes
Robert Phillipsa41c6852019-02-07 10:44:10 -050067 SkASSERT(this->proxyProvider());
Robert Phillips88260b52018-01-19 12:56:09 -050068
Robert Phillipsbb606772019-02-04 17:50:57 -050069 if (!INHERITED::init(std::move(caps), std::move(FPFactoryCache))) {
70 return false;
71 }
72
73 SkASSERT(this->caps());
Herb Derbya00da612019-03-04 17:10:01 -050074 SkASSERT(this->getGrStrikeCache());
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) {
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());
Robert Phillips88260b52018-01-19 12:56:09 -050080 }
81
Robert Phillips88260b52018-01-19 12:56:09 -050082 if (fResourceCache) {
Robert Phillipsa41c6852019-02-07 10:44:10 -050083 fResourceCache->setProxyProvider(this->proxyProvider());
Robert Phillips88260b52018-01-19 12:56:09 -050084 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050085
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000086 fDidTestPMConversions = false;
87
Robert Phillipsfde6fa02018-03-02 08:53:14 -050088 // DDL TODO: we need to think through how the task group & persistent cache
89 // get passed on to/shared between all the DDLRecorders created with this context.
Robert Phillipsc1541ae2019-02-04 12:05:37 -050090 if (this->options().fExecutor) {
91 fTaskGroup = skstd::make_unique<SkTaskGroup>(*this->options().fExecutor);
Brian Osman51279982017-08-23 10:12:00 -040092 }
93
Robert Phillipsc1541ae2019-02-04 12:05:37 -050094 fPersistentCache = this->options().fPersistentCache;
Brian Osman5e7fbfd2019-05-03 13:13:35 -040095 fShaderErrorHandler = this->options().fShaderErrorHandler;
96 if (!fShaderErrorHandler) {
97 fShaderErrorHandler = GrShaderUtils::DefaultShaderErrorHandler();
98 }
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -040099
Brian Salomon91a3e522017-06-23 10:58:19 -0400100 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000101}
102
Robert Phillips4217ea72019-01-30 13:08:28 -0500103sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
104 return fThreadSafeProxy;
105}
106
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400107//////////////////////////////////////////////////////////////////////////////
108
bsalomon2354f842014-07-28 13:48:36 -0700109void GrContext::abandonContext() {
Robert Phillipsa9162df2019-02-11 14:12:03 -0500110 if (this->abandoned()) {
111 return;
112 }
joshualitt1de610a2016-01-06 08:26:09 -0800113
Robert Phillipsa9162df2019-02-11 14:12:03 -0500114 INHERITED::abandonContext();
115
bsalomond309e7a2015-04-30 14:18:54 -0700116 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800117
Robert Phillipsa9162df2019-02-11 14:12:03 -0500118 // Need to cleanup the drawing manager first so all the render targets
robertphillips0dfa62c2015-11-16 06:23:31 -0800119 // will be released/forgotten before they too are abandoned.
Robert Phillips6a6de562019-02-15 15:19:15 -0500120 this->drawingManager()->cleanup();
robertphillips0dfa62c2015-11-16 06:23:31 -0800121
bsalomon@google.com205d4602011-04-25 12:43:45 +0000122 // abandon first to so destructors
123 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800124 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700125
bsalomon6e2aad42016-04-01 11:54:31 -0700126 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
Khushalc421ca12018-06-26 14:38:34 -0700127}
128
bsalomon6e2aad42016-04-01 11:54:31 -0700129void GrContext::releaseResourcesAndAbandonContext() {
Brian Salomon614c1a82018-12-19 15:42:06 -0500130 if (this->abandoned()) {
131 return;
132 }
Robert Phillipsa9162df2019-02-11 14:12:03 -0500133
134 INHERITED::abandonContext();
135
bsalomon6e2aad42016-04-01 11:54:31 -0700136 fResourceProvider->abandon();
137
Robert Phillipsa9162df2019-02-11 14:12:03 -0500138 // Need to cleanup the drawing manager first so all the render targets
bsalomon6e2aad42016-04-01 11:54:31 -0700139 // will be released/forgotten before they too are abandoned.
Robert Phillips6a6de562019-02-15 15:19:15 -0500140 this->drawingManager()->cleanup();
bsalomon6e2aad42016-04-01 11:54:31 -0700141
142 // Release all resources in the backend 3D API.
143 fResourceCache->releaseAll();
144
145 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000146}
147
Brian Salomon1f05d452019-02-08 12:33:08 -0500148void GrContext::resetGLTextureBindings() {
149 if (this->abandoned() || this->backend() != GrBackendApi::kOpenGL) {
150 return;
151 }
152 fGpu->resetTextureBindings();
153}
154
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000155void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800156 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000157 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000158}
159
160void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800161 ASSERT_SINGLE_OWNER
162
Robert Phillips2184fb72019-02-21 16:11:41 -0500163 // TODO: the glyph cache doesn't hold any GpuResources so this call should not be needed here.
164 // Some slack in the GrTextBlob's implementation requires it though. That could be fixed.
Herb Derbya00da612019-03-04 17:10:01 -0500165 this->getGrStrikeCache()->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700166
Robert Phillips6a6de562019-02-15 15:19:15 -0500167 this->drawingManager()->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700168
169 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000170}
171
Robert Phillips6eba0632018-03-28 12:25:42 -0400172void GrContext::purgeUnlockedResources(bool scratchResourcesOnly) {
173 ASSERT_SINGLE_OWNER
174 fResourceCache->purgeUnlockedResources(scratchResourcesOnly);
175 fResourceCache->purgeAsNeeded();
Robert Phillips2184fb72019-02-21 16:11:41 -0500176
177 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
178 // place to purge stale blobs
179 this->getTextBlobCache()->purgeStaleBlobs();
Robert Phillips6eba0632018-03-28 12:25:42 -0400180}
181
Jim Van Verth76d917c2017-12-13 09:26:37 -0500182void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Yuqian Li40aa85f2019-07-02 13:45:00 -0700183 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
184
Brian Salomon5e150852017-03-22 14:53:13 -0400185 ASSERT_SINGLE_OWNER
Chris Dalton6c3879d2018-11-01 11:13:19 -0600186
187 auto purgeTime = GrStdSteadyClock::now() - msNotUsed;
188
Jim Van Verth76d917c2017-12-13 09:26:37 -0500189 fResourceCache->purgeAsNeeded();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600190 fResourceCache->purgeResourcesNotUsedSince(purgeTime);
191
Robert Phillips6a6de562019-02-15 15:19:15 -0500192 if (auto ccpr = this->drawingManager()->getCoverageCountingPathRenderer()) {
Robert Phillipsa41c6852019-02-07 10:44:10 -0500193 ccpr->purgeCacheEntriesOlderThan(this->proxyProvider(), purgeTime);
Chris Dalton6c3879d2018-11-01 11:13:19 -0600194 }
Jim Van Verth76d917c2017-12-13 09:26:37 -0500195
Robert Phillips2184fb72019-02-21 16:11:41 -0500196 // The textBlob Cache doesn't actually hold any GPU resource but this is a convenient
197 // place to purge stale blobs
198 this->getTextBlobCache()->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400199}
200
Derek Sollenberger5480a182017-05-25 16:43:59 -0400201void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
202 ASSERT_SINGLE_OWNER
203 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
204}
205
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000206void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800207 ASSERT_SINGLE_OWNER
208
bsalomon71cb0c22014-11-14 12:10:14 -0800209 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800210 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800211 }
212 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800213 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800214 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000215}
216
Derek Sollenbergeree479142017-05-24 11:41:33 -0400217size_t GrContext::getResourceCachePurgeableBytes() const {
218 ASSERT_SINGLE_OWNER
219 return fResourceCache->getPurgeableBytes();
220}
221
Adrienne Walker0f827972019-03-26 13:46:14 -0700222size_t GrContext::ComputeTextureSize(SkColorType type, int width, int height, GrMipMapped mipMapped,
223 bool useNextPow2) {
224 int colorSamplesPerPixel = 1;
225 return GrSurface::ComputeSize(SkColorType2GrPixelConfig(type), width, height,
226 colorSamplesPerPixel, mipMapped, useNextPow2);
227}
228
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000229////////////////////////////////////////////////////////////////////////////////
230
Robert Phillipsbb606772019-02-04 17:50:57 -0500231int GrContext::maxTextureSize() const { return this->caps()->maxTextureSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400232
Robert Phillipsbb606772019-02-04 17:50:57 -0500233int GrContext::maxRenderTargetSize() const { return this->caps()->maxRenderTargetSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400234
Brian Salomonbdecacf2018-02-02 20:32:49 -0500235bool GrContext::colorTypeSupportedAsImage(SkColorType colorType) const {
Greg Daniel7bfc9132019-08-14 14:23:53 -0400236 GrBackendFormat format =
237 this->caps()->getDefaultBackendFormat(SkColorTypeToGrColorType(colorType),
238 GrRenderable::kNo);
239 return format.isValid();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500240}
241
242int GrContext::maxSurfaceSampleCountForColorType(SkColorType colorType) const {
Greg Danieleadfac92019-08-02 09:03:53 -0400243 GrBackendFormat format =
244 this->caps()->getDefaultBackendFormat(SkColorTypeToGrColorType(colorType),
245 GrRenderable::kYes);
246 return this->caps()->maxRenderTargetSampleCount(format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500247}
248
249////////////////////////////////////////////////////////////////////////////////
250
Greg Daniel06be0792019-04-22 15:53:23 -0400251bool GrContext::wait(int numSemaphores, const GrBackendSemaphore waitSemaphores[]) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400252 if (!fGpu || fGpu->caps()->semaphoreSupport()) {
Greg Daniel06be0792019-04-22 15:53:23 -0400253 return false;
254 }
255 for (int i = 0; i < numSemaphores; ++i) {
256 sk_sp<GrSemaphore> sema = fResourceProvider->wrapBackendSemaphore(
257 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
258 kAdopt_GrWrapOwnership);
259 fGpu->waitSemaphore(std::move(sema));
260 }
261 return true;
262}
263
264////////////////////////////////////////////////////////////////////////////////
265
Greg Daniel797efca2019-05-09 14:04:20 -0400266GrSemaphoresSubmitted GrContext::flush(const GrFlushInfo& info,
267 const GrPrepareForExternalIORequests& externalRequests) {
Greg Daniel51316782017-08-02 15:10:09 +0000268 ASSERT_SINGLE_OWNER
Robert Phillipsa9162df2019-02-11 14:12:03 -0500269 if (this->abandoned()) {
270 return GrSemaphoresSubmitted::kNo;
271 }
Greg Daniel51316782017-08-02 15:10:09 +0000272
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400273 return this->drawingManager()->flush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess,
Greg Daniel797efca2019-05-09 14:04:20 -0400274 info, externalRequests);
Greg Daniel51316782017-08-02 15:10:09 +0000275}
276
Greg Daniela870b462019-01-08 15:49:46 -0500277////////////////////////////////////////////////////////////////////////////////
278
Brian Salomonb0d8b762019-05-06 16:58:22 -0400279void GrContext::checkAsyncWorkCompletion() {
280 if (fGpu) {
281 fGpu->checkFinishProcs();
282 }
283}
284
285////////////////////////////////////////////////////////////////////////////////
286
Greg Daniela870b462019-01-08 15:49:46 -0500287void GrContext::storeVkPipelineCacheData() {
288 if (fGpu) {
289 fGpu->storeVkPipelineCacheData();
290 }
291}
292
293////////////////////////////////////////////////////////////////////////////////
294
Khushal3e7548c2018-05-23 15:45:01 -0700295bool GrContext::supportsDistanceFieldText() const {
Robert Phillipsbb606772019-02-04 17:50:57 -0500296 return this->caps()->shaderCaps()->supportsDistanceFieldText();
Khushal3e7548c2018-05-23 15:45:01 -0700297}
298
bsalomon37f9a262015-02-02 13:00:10 -0800299//////////////////////////////////////////////////////////////////////////////
300
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500301void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800302 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500303 if (maxResources) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400304 *maxResources = -1;
bsalomon37f9a262015-02-02 13:00:10 -0800305 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -0500306 if (maxResourceBytes) {
Robert Phillipscf39f372019-09-03 10:29:20 -0400307 *maxResourceBytes = this->getResourceCacheLimit();
bsalomon37f9a262015-02-02 13:00:10 -0800308 }
309}
310
Robert Phillipscf39f372019-09-03 10:29:20 -0400311size_t GrContext::getResourceCacheLimit() const {
joshualitt1de610a2016-01-06 08:26:09 -0800312 ASSERT_SINGLE_OWNER
Robert Phillipscf39f372019-09-03 10:29:20 -0400313 return fResourceCache->getMaxResourceBytes();
314}
315
316void GrContext::setResourceCacheLimits(int unused, size_t maxResourceBytes) {
317 ASSERT_SINGLE_OWNER
318 this->setResourceCacheLimit(maxResourceBytes);
319}
320
321void GrContext::setResourceCacheLimit(size_t maxResourceBytes) {
322 ASSERT_SINGLE_OWNER
323 fResourceCache->setLimit(maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800324}
325
ericrk0a5fa482015-09-15 14:16:10 -0700326//////////////////////////////////////////////////////////////////////////////
ericrk0a5fa482015-09-15 14:16:10 -0700327void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800328 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700329 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
Khushal71652e22018-10-29 13:05:36 -0700330 traceMemoryDump->dumpNumericValue("skia/gr_text_blob_cache", "size", "bytes",
Robert Phillips2184fb72019-02-21 16:11:41 -0500331 this->getTextBlobCache()->usedBytes());
ericrk0a5fa482015-09-15 14:16:10 -0700332}
Brian Osman71a18892017-08-10 10:23:25 -0400333
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400334//////////////////////////////////////////////////////////////////////////////
335GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400336 const GrBackendFormat& backendFormat,
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400337 GrMipMapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400338 GrRenderable renderable,
339 GrProtected isProtected) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400340 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400341 if (!this->asDirectContext()) {
342 return GrBackendTexture();
343 }
344
345 if (this->abandoned()) {
346 return GrBackendTexture();
347 }
348
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400349 return fGpu->createBackendTexture(width, height, backendFormat,
350 mipMapped, renderable,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400351 nullptr, 0, nullptr, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400352}
353
354GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400355 SkColorType skColorType,
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400356 GrMipMapped mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400357 GrRenderable renderable,
358 GrProtected isProtected) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400359 if (!this->asDirectContext()) {
360 return GrBackendTexture();
361 }
362
363 if (this->abandoned()) {
364 return GrBackendTexture();
365 }
366
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400367 const GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400368
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400369 return this->createBackendTexture(width, height, format, mipMapped, renderable, isProtected);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400370}
371
Robert Phillips02dc0302019-07-02 17:58:27 -0400372GrBackendTexture GrContext::createBackendTexture(const SkSurfaceCharacterization& c) {
Robert Phillips02dc0302019-07-02 17:58:27 -0400373 if (!this->asDirectContext() || !c.isValid()) {
374 return GrBackendTexture();
375 }
376
377 if (this->abandoned()) {
378 return GrBackendTexture();
379 }
380
381 if (c.usesGLFBO0()) {
382 // If we are making the surface we will never use FBO0.
383 return GrBackendTexture();
384 }
385
386 if (c.vulkanSecondaryCBCompatible()) {
387 return {};
388 }
389
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400390 const GrBackendFormat format = this->defaultBackendFormat(c.colorType(), GrRenderable::kYes);
Robert Phillips02dc0302019-07-02 17:58:27 -0400391 if (!format.isValid()) {
392 return GrBackendTexture();
393 }
394
Robert Phillips02dc0302019-07-02 17:58:27 -0400395 GrBackendTexture result = this->createBackendTexture(c.width(), c.height(), format,
396 GrMipMapped(c.isMipMapped()),
397 GrRenderable::kYes,
Robert Phillips3cd54322019-07-10 09:28:59 -0400398 c.isProtected());
Robert Phillips02dc0302019-07-02 17:58:27 -0400399 SkASSERT(c.isCompatible(result));
400 return result;
401}
402
403GrBackendTexture GrContext::createBackendTexture(const SkSurfaceCharacterization& c,
404 const SkColor4f& color) {
Robert Phillips02dc0302019-07-02 17:58:27 -0400405 if (!this->asDirectContext() || !c.isValid()) {
406 return GrBackendTexture();
407 }
408
409 if (this->abandoned()) {
410 return GrBackendTexture();
411 }
412
413 if (c.usesGLFBO0()) {
414 // If we are making the surface we will never use FBO0.
415 return GrBackendTexture();
416 }
417
418 if (c.vulkanSecondaryCBCompatible()) {
419 return {};
420 }
421
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400422 const GrBackendFormat format = this->defaultBackendFormat(c.colorType(), GrRenderable::kYes);
Robert Phillips02dc0302019-07-02 17:58:27 -0400423 if (!format.isValid()) {
424 return GrBackendTexture();
425 }
426
Robert Phillips02dc0302019-07-02 17:58:27 -0400427 GrBackendTexture result = this->createBackendTexture(c.width(), c.height(), format, color,
428 GrMipMapped(c.isMipMapped()),
429 GrRenderable::kYes,
Robert Phillips3cd54322019-07-10 09:28:59 -0400430 c.isProtected());
Robert Phillips02dc0302019-07-02 17:58:27 -0400431 SkASSERT(c.isCompatible(result));
432 return result;
433}
434
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400435GrBackendTexture GrContext::createBackendTexture(int width, int height,
Greg Danielf91aeb22019-06-18 09:58:02 -0400436 const GrBackendFormat& backendFormat,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400437 const SkColor4f& color,
438 GrMipMapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400439 GrRenderable renderable,
440 GrProtected isProtected) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400441 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400442 if (!this->asDirectContext()) {
443 return GrBackendTexture();
444 }
445
446 if (this->abandoned()) {
447 return GrBackendTexture();
448 }
449
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400450 return fGpu->createBackendTexture(width, height, backendFormat,
451 mipMapped, renderable,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400452 nullptr, 0, &color, isProtected);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400453}
454
455GrBackendTexture GrContext::createBackendTexture(int width, int height,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400456 SkColorType skColorType,
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400457 const SkColor4f& color,
458 GrMipMapped mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400459 GrRenderable renderable,
460 GrProtected isProtected) {
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400461 if (!this->asDirectContext()) {
462 return GrBackendTexture();
463 }
464
465 if (this->abandoned()) {
466 return GrBackendTexture();
467 }
468
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400469 GrBackendFormat format = this->defaultBackendFormat(skColorType, renderable);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400470 if (!format.isValid()) {
471 return GrBackendTexture();
472 }
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400473
474 GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
475 SkColor4f swizzledColor = this->caps()->getOutputSwizzle(format, grColorType).applyTo(color);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400476
Brian Salomonb450f3b2019-07-09 09:36:51 -0400477 return this->createBackendTexture(width, height, format, swizzledColor, mipMapped, renderable,
478 isProtected);
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400479}
480
Robert Phillips66944402019-09-30 13:21:25 -0400481GrBackendTexture GrContext::createBackendTexture(const SkPixmap srcData[], int numLevels,
482 GrRenderable renderable, GrProtected isProtected) {
483 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
484
485 if (!this->asDirectContext()) {
486 return {};
487 }
488
489 if (this->abandoned()) {
490 return {};
491 }
492
493 if (!srcData || !numLevels) {
494 return {};
495 }
496
497 int baseWidth = srcData[0].width();
498 int baseHeight = srcData[0].height();
499 SkColorType colorType = srcData[0].colorType();
500
501 GrBackendFormat backendFormat = this->defaultBackendFormat(colorType, renderable);
502
503 return fGpu->createBackendTexture(baseWidth, baseHeight, backendFormat,
504 numLevels > 1 ? GrMipMapped::kYes : GrMipMapped::kNo,
505 renderable, srcData, numLevels, nullptr, isProtected);
506}
507
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400508void GrContext::deleteBackendTexture(GrBackendTexture backendTex) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400509 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400510 if (this->abandoned() || !backendTex.isValid()) {
511 return;
512 }
513
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400514 fGpu->deleteBackendTexture(backendTex);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400515}
516
Brian Osmaned58e002019-09-06 14:42:43 -0400517bool GrContext::precompileShader(const SkData& key, const SkData& data) {
518 return fGpu->precompileShader(key, data);
519}
520
Brian Salomonec22b1a2019-08-09 09:41:48 -0400521#ifdef SK_ENABLE_DUMP_GPU
522#include "src/utils/SkJSONWriter.h"
523SkString GrContext::dump() const {
524 SkDynamicMemoryWStream stream;
525 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
526 writer.beginObject();
527
528 writer.appendString("backend", GrBackendApiToStr(this->backend()));
529
530 writer.appendName("caps");
531 this->caps()->dumpJSON(&writer);
532
533 writer.appendName("gpu");
534 this->fGpu->dumpJSON(&writer);
535
536 // Flush JSON to the memory stream
537 writer.endObject();
538 writer.flush();
539
540 // Null terminate the JSON data in the memory stream
541 stream.write8(0);
542
543 // Allocate a string big enough to hold all the data, then copy out of the stream
544 SkString result(stream.bytesWritten());
545 stream.copyToAndReset(result.writable_str());
546 return result;
547}
548#endif