blob: ee7fa89f3fade66306b40b504a77aac8cd5c982f [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
bsalomon@google.com1fadb202011-12-12 16:10:08 +00008#include "GrContext.h"
Brian Salomon5f33a8c2018-02-26 14:32:39 -05009#include "GrBackendSemaphore.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050010#include "GrClip.h"
bsalomon682c2692015-05-22 14:01:46 -070011#include "GrContextOptions.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050012#include "GrContextPriv.h"
robertphillips77a2e522015-10-17 07:43:27 -070013#include "GrDrawingManager.h"
Robert Phillips646e4292017-06-13 12:44:56 -040014#include "GrGpu.h"
Robert Phillipsc994a932018-06-19 13:09:54 -040015#include "GrMemoryPool.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050016#include "GrProxyProvider.h"
Brian Osman11052242016-10-27 14:47:55 -040017#include "GrRenderTargetContext.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050018#include "GrRenderTargetProxy.h"
bsalomon0ea80f42015-02-11 10:49:59 -080019#include "GrResourceCache.h"
bsalomond309e7a2015-04-30 14:18:54 -070020#include "GrResourceProvider.h"
Greg Danield85f97d2017-03-07 13:37:21 -050021#include "GrSemaphore.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000022#include "GrSoftwarePathRenderer.h"
Brian Osman45580d32016-11-23 09:37:01 -050023#include "GrSurfaceContext.h"
bsalomonafbf2d62014-09-30 12:18:44 -070024#include "GrSurfacePriv.h"
Robert Phillips757914d2017-01-25 15:48:30 -050025#include "GrSurfaceProxyPriv.h"
Robert Phillips646e4292017-06-13 12:44:56 -040026#include "GrTexture.h"
Brian Osman45580d32016-11-23 09:37:01 -050027#include "GrTextureContext.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040028#include "GrTracing.h"
Brian Salomon19eaf2d2018-03-19 16:06:44 -040029#include "SkAutoPixmapStorage.h"
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -050030#include "SkDeferredDisplayList.h"
Brian Osman3b655982017-03-07 16:58:08 -050031#include "SkGr.h"
Mike Reed7fcfb622018-02-09 13:26:46 -050032#include "SkImageInfoPriv.h"
Brian Osman51279982017-08-23 10:12:00 -040033#include "SkMakeUnique.h"
Robert Phillips6b6fcc72018-03-30 13:57:00 -040034#include "SkSurface_Gpu.h"
Brian Osman51279982017-08-23 10:12:00 -040035#include "SkTaskGroup.h"
Khushal71652e22018-10-29 13:05:36 -070036#include "SkTraceMemoryDump.h"
joshualitt5478d422014-11-14 16:00:38 -080037#include "effects/GrConfigConversionEffect.h"
Ethan Nicholas00543112018-07-31 09:44:36 -040038#include "effects/GrSkSLFP.h"
Chris Dalton6c3879d2018-11-01 11:13:19 -060039#include "ccpr/GrCoverageCountingPathRenderer.h"
Brian Salomon5f33a8c2018-02-26 14:32:39 -050040#include "text/GrTextBlobCache.h"
Mike Klein0ec1c572018-12-04 11:52:51 -050041#include <atomic>
42#include <unordered_map>
Greg Danielb76a72a2017-07-13 15:07:54 -040043
Robert Phillipse78b7252017-04-06 07:59:41 -040044#define ASSERT_OWNED_PROXY(P) \
Brian Salomonfd98c2c2018-07-31 17:25:29 -040045 SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == this)
Robert Phillips7ee385e2017-03-30 08:02:11 -040046#define ASSERT_OWNED_PROXY_PRIV(P) \
Brian Salomonfd98c2c2018-07-31 17:25:29 -040047 SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == fContext)
Robert Phillips7ee385e2017-03-30 08:02:11 -040048
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000049#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt1de610a2016-01-06 08:26:09 -080050#define ASSERT_SINGLE_OWNER \
51 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fSingleOwner);)
robertphillips4fd74ae2016-08-03 14:26:53 -070052#define ASSERT_SINGLE_OWNER_PRIV \
53 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fContext->fSingleOwner);)
robertphillips7761d612016-05-16 09:14:53 -070054#define RETURN_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return; }
Robert Phillips7ee385e2017-03-30 08:02:11 -040055#define RETURN_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return; }
robertphillips7761d612016-05-16 09:14:53 -070056#define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return false; }
Robert Phillipse78b7252017-04-06 07:59:41 -040057#define RETURN_FALSE_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return false; }
robertphillips7761d612016-05-16 09:14:53 -070058#define RETURN_NULL_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000059
robertphillipsea461502015-05-26 11:38:03 -070060////////////////////////////////////////////////////////////////////////////////
61
joshualitt0acd0d32015-05-07 08:23:19 -070062static int32_t next_id() {
Mike Klein0ec1c572018-12-04 11:52:51 -050063 static std::atomic<int32_t> nextID{1};
joshualitt0acd0d32015-05-07 08:23:19 -070064 int32_t id;
65 do {
Mike Klein0ec1c572018-12-04 11:52:51 -050066 id = nextID++;
joshualitt0acd0d32015-05-07 08:23:19 -070067 } while (id == SK_InvalidGenID);
68 return id;
69}
70
Greg Danielbdf12ad2018-10-12 09:31:11 -040071GrContext::GrContext(GrBackendApi backend, int32_t id)
Robert Phillipsfde6fa02018-03-02 08:53:14 -050072 : fBackend(backend)
73 , fUniqueID(SK_InvalidGenID == id ? next_id() : id) {
halcanary96fcdcc2015-08-27 07:41:13 -070074 fResourceCache = nullptr;
75 fResourceProvider = nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050076 fProxyProvider = nullptr;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050077 fGlyphCache = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000078}
79
Robert Phillipsfde6fa02018-03-02 08:53:14 -050080bool GrContext::initCommon(const GrContextOptions& options) {
Greg Danielb76a72a2017-07-13 15:07:54 -040081 ASSERT_SINGLE_OWNER
Robert Phillipsfde6fa02018-03-02 08:53:14 -050082 SkASSERT(fCaps); // needs to have been initialized by derived classes
83 SkASSERT(fThreadSafeProxy); // needs to have been initialized by derived classes
Robert Phillips88260b52018-01-19 12:56:09 -050084
85 if (fGpu) {
86 fCaps = fGpu->refCaps();
Brian Salomon8f8995a2018-10-15 14:32:15 -040087 fResourceCache = new GrResourceCache(fCaps.get(), &fSingleOwner, fUniqueID);
Robert Phillips4150eea2018-02-07 17:08:21 -050088 fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, &fSingleOwner,
89 options.fExplicitlyAllocateGPUResources);
Brian Salomon238069b2018-07-11 15:58:57 -040090 fProxyProvider =
91 new GrProxyProvider(fResourceProvider, fResourceCache, fCaps, &fSingleOwner);
92 } else {
93 fProxyProvider = new GrProxyProvider(this->uniqueID(), fCaps, &fSingleOwner);
Robert Phillips88260b52018-01-19 12:56:09 -050094 }
95
Robert Phillips88260b52018-01-19 12:56:09 -050096 if (fResourceCache) {
97 fResourceCache->setProxyProvider(fProxyProvider);
98 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050099
Brian Osman46da1cc2017-02-14 14:15:48 -0500100 fDisableGpuYUVConversion = options.fDisableGpuYUVConversion;
Brian Osman8a83ca42018-02-12 14:32:17 -0500101 fSharpenMipmappedTextures = options.fSharpenMipmappedTextures;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000102 fDidTestPMConversions = false;
103
bsalomon6b2552f2016-09-15 13:50:26 -0700104 GrPathRendererChain::Options prcOptions;
bsalomon39ef7fb2016-09-21 11:16:05 -0700105 prcOptions.fAllowPathMaskCaching = options.fAllowPathMaskCaching;
Brian Osman195c05b2017-08-30 15:14:04 -0400106#if GR_TEST_UTILS
csmartdalton008b9d82017-02-22 12:00:42 -0700107 prcOptions.fGpuPathRenderers = options.fGpuPathRenderers;
Brian Osman195c05b2017-08-30 15:14:04 -0400108#endif
Chris Daltonef125092018-06-26 18:16:47 -0600109 if (options.fDisableCoverageCountingPaths) {
110 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kCoverageCounting;
111 }
Brian Osmanb350ae22017-08-29 16:15:39 -0400112 if (options.fDisableDistanceFieldPaths) {
Brian Osman195c05b2017-08-30 15:14:04 -0400113 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
Brian Osmanb350ae22017-08-29 16:15:39 -0400114 }
Brian Salomonaf597482017-11-07 16:23:34 -0500115
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500116 if (!fResourceCache) {
117 // DDL TODO: remove this crippling of the path renderer chain
118 // Disable the small path renderer bc of the proxies in the atlas. They need to be
119 // unified when the opLists are added back to the destination drawing manager.
120 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
Robert Phillips5ffcb4d2018-07-03 16:39:40 -0400121 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kStencilAndCover;
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500122 }
123
Herb Derby26cbe512018-05-24 14:39:01 -0400124 GrTextContext::Options textContextOptions;
125 textContextOptions.fMaxDistanceFieldFontSize = options.fGlyphsAsPathsFontSize;
126 textContextOptions.fMinDistanceFieldFontSize = options.fMinDistanceFieldFontSize;
127 textContextOptions.fDistanceFieldVerticesAlwaysHaveW = false;
Brian Salomonb5086962017-12-13 10:59:33 -0500128#if SK_SUPPORT_ATLAS_TEXT
129 if (GrContextOptions::Enable::kYes == options.fDistanceFieldGlyphVerticesAlwaysHaveW) {
Herb Derby26cbe512018-05-24 14:39:01 -0400130 textContextOptions.fDistanceFieldVerticesAlwaysHaveW = true;
Brian Salomonb5086962017-12-13 10:59:33 -0500131 }
132#endif
Brian Salomonaf597482017-11-07 16:23:34 -0500133
Robert Phillips64ecdce2018-04-02 10:26:39 -0400134 bool explicitlyAllocatingResources = fResourceProvider
135 ? fResourceProvider->explicitlyAllocateGPUResources()
136 : false;
Herb Derby26cbe512018-05-24 14:39:01 -0400137 fDrawingManager.reset(new GrDrawingManager(this, prcOptions, textContextOptions,
Robert Phillips64ecdce2018-04-02 10:26:39 -0400138 &fSingleOwner, explicitlyAllocatingResources,
Robert Phillips46acf9d2018-10-09 09:31:40 -0400139 options.fSortRenderTargets,
140 options.fReduceOpListSplitting));
joshualitt7c3a2f82015-03-31 13:32:05 -0700141
Robert Phillipsaf4adef2018-03-07 11:59:37 -0500142 fGlyphCache = new GrGlyphCache(fCaps.get(), options.fGlyphCacheTextureMaximumBytes);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500143
Robert Phillips303cd582018-02-14 18:54:01 -0500144 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB,
Herb Derbyb12175f2018-05-23 16:38:09 -0400145 this, this->uniqueID()));
Brian Salomon91a3e522017-06-23 10:58:19 -0400146
Robert Phillipsfde6fa02018-03-02 08:53:14 -0500147 // DDL TODO: we need to think through how the task group & persistent cache
148 // get passed on to/shared between all the DDLRecorders created with this context.
Brian Osman51279982017-08-23 10:12:00 -0400149 if (options.fExecutor) {
150 fTaskGroup = skstd::make_unique<SkTaskGroup>(*options.fExecutor);
151 }
152
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400153 fPersistentCache = options.fPersistentCache;
154
Brian Salomon91a3e522017-06-23 10:58:19 -0400155 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000156}
157
bsalomon@google.com27847de2011-02-22 20:59:41 +0000158GrContext::~GrContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800159 ASSERT_SINGLE_OWNER
160
Robert Phillips2e6feed2018-01-22 15:27:20 -0500161 if (fDrawingManager) {
162 fDrawingManager->cleanup();
163 }
halcanary385fe4d2015-08-26 13:07:48 -0700164 delete fResourceProvider;
165 delete fResourceCache;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500166 delete fProxyProvider;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500167 delete fGlyphCache;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000168}
169
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400170//////////////////////////////////////////////////////////////////////////////
171
172GrContextThreadSafeProxy::GrContextThreadSafeProxy(sk_sp<const GrCaps> caps, uint32_t uniqueID,
Greg Danielbdf12ad2018-10-12 09:31:11 -0400173 GrBackendApi backend,
Ethan Nicholas00543112018-07-31 09:44:36 -0400174 const GrContextOptions& options,
175 sk_sp<GrSkSLFPFactoryCache> cache)
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400176 : fCaps(std::move(caps))
177 , fContextUniqueID(uniqueID)
178 , fBackend(backend)
Ethan Nicholas00543112018-07-31 09:44:36 -0400179 , fOptions(options)
180 , fFPFactoryCache(std::move(cache)) {}
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400181
Brian Salomon103d6f62018-05-11 11:19:22 -0400182GrContextThreadSafeProxy::~GrContextThreadSafeProxy() = default;
183
bungeman6bd52842016-10-27 09:30:08 -0700184sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
bungeman6bd52842016-10-27 09:30:08 -0700185 return fThreadSafeProxy;
bsalomon41b952c2016-03-11 06:46:33 -0800186}
187
Robert Phillipsfc711a22018-02-13 17:03:00 -0500188SkSurfaceCharacterization GrContextThreadSafeProxy::createCharacterization(
189 size_t cacheMaxResourceBytes,
190 const SkImageInfo& ii, const GrBackendFormat& backendFormat,
191 int sampleCnt, GrSurfaceOrigin origin,
192 const SkSurfaceProps& surfaceProps,
Greg Daniela070ed72018-04-26 16:31:38 -0400193 bool isMipMapped, bool willUseGLFBO0) {
Robert Phillipsfc711a22018-02-13 17:03:00 -0500194 if (!backendFormat.isValid()) {
195 return SkSurfaceCharacterization(); // return an invalid characterization
196 }
197
Greg Danielbdf12ad2018-10-12 09:31:11 -0400198 if (GrBackendApi::kOpenGL != backendFormat.backend() && willUseGLFBO0) {
Greg Daniela070ed72018-04-26 16:31:38 -0400199 // The willUseGLFBO0 flags can only be used for a GL backend.
200 return SkSurfaceCharacterization(); // return an invalid characterization
201 }
202
Robert Phillipsfc711a22018-02-13 17:03:00 -0500203 if (!fCaps->mipMapSupport()) {
204 isMipMapped = false;
205 }
206
Brian Salomonf391d0f2018-12-14 09:18:50 -0500207 GrPixelConfig config = fCaps->getConfigFromBackendFormat(backendFormat, ii.colorType());
208 if (config == kUnknown_GrPixelConfig) {
Robert Phillipsfc711a22018-02-13 17:03:00 -0500209 return SkSurfaceCharacterization(); // return an invalid characterization
210 }
211
Robert Phillipsbe77a022018-04-03 17:17:05 -0400212 if (!SkSurface_Gpu::Valid(fCaps.get(), config, ii.colorSpace())) {
213 return SkSurfaceCharacterization(); // return an invalid characterization
214 }
215
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400216 sampleCnt = fCaps->getRenderTargetSampleCount(sampleCnt, config);
217 if (!sampleCnt) {
218 return SkSurfaceCharacterization(); // return an invalid characterization
219 }
220
221 GrFSAAType FSAAType = GrFSAAType::kNone;
222 if (sampleCnt > 1) {
223 FSAAType = fCaps->usesMixedSamples() ? GrFSAAType::kMixedSamples : GrFSAAType::kUnifiedMSAA;
224 }
225
Robert Phillipsfc711a22018-02-13 17:03:00 -0500226 // This surface characterization factory assumes that the resulting characterization is
227 // textureable.
228 if (!fCaps->isConfigTexturable(config)) {
229 return SkSurfaceCharacterization(); // return an invalid characterization
230 }
231
232 return SkSurfaceCharacterization(sk_ref_sp<GrContextThreadSafeProxy>(this),
Robert Phillipsbe77a022018-04-03 17:17:05 -0400233 cacheMaxResourceBytes, ii,
234 origin, config, FSAAType, sampleCnt,
Robert Phillipsfc711a22018-02-13 17:03:00 -0500235 SkSurfaceCharacterization::Textureable(true),
236 SkSurfaceCharacterization::MipMapped(isMipMapped),
Greg Daniela070ed72018-04-26 16:31:38 -0400237 SkSurfaceCharacterization::UsesGLFBO0(willUseGLFBO0),
Robert Phillipsbe77a022018-04-03 17:17:05 -0400238 surfaceProps);
Robert Phillipsfc711a22018-02-13 17:03:00 -0500239}
240
bsalomon2354f842014-07-28 13:48:36 -0700241void GrContext::abandonContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800242 ASSERT_SINGLE_OWNER
243
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500244 fProxyProvider->abandon();
bsalomond309e7a2015-04-30 14:18:54 -0700245 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800246
247 // Need to abandon the drawing manager first so all the render targets
248 // will be released/forgotten before they too are abandoned.
249 fDrawingManager->abandon();
250
bsalomon@google.com205d4602011-04-25 12:43:45 +0000251 // abandon first to so destructors
252 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800253 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700254
bsalomon6e2aad42016-04-01 11:54:31 -0700255 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
256
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500257 fGlyphCache->freeAll();
bsalomon6e2aad42016-04-01 11:54:31 -0700258 fTextBlobCache->freeAll();
259}
260
Khushalc421ca12018-06-26 14:38:34 -0700261bool GrContext::abandoned() const {
262 ASSERT_SINGLE_OWNER
263 return fDrawingManager->wasAbandoned();
264}
265
bsalomon6e2aad42016-04-01 11:54:31 -0700266void GrContext::releaseResourcesAndAbandonContext() {
267 ASSERT_SINGLE_OWNER
268
Brian Salomon614c1a82018-12-19 15:42:06 -0500269 if (this->abandoned()) {
270 return;
271 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500272 fProxyProvider->abandon();
bsalomon6e2aad42016-04-01 11:54:31 -0700273 fResourceProvider->abandon();
274
275 // Need to abandon the drawing manager first so all the render targets
276 // will be released/forgotten before they too are abandoned.
277 fDrawingManager->abandon();
278
279 // Release all resources in the backend 3D API.
280 fResourceCache->releaseAll();
281
282 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000283
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500284 fGlyphCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700285 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000286}
287
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000288void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800289 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000290 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000291}
292
293void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800294 ASSERT_SINGLE_OWNER
295
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500296 fGlyphCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700297
298 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700299
300 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000301}
302
Robert Phillips6eba0632018-03-28 12:25:42 -0400303void GrContext::purgeUnlockedResources(bool scratchResourcesOnly) {
304 ASSERT_SINGLE_OWNER
305 fResourceCache->purgeUnlockedResources(scratchResourcesOnly);
306 fResourceCache->purgeAsNeeded();
307 fTextBlobCache->purgeStaleBlobs();
308}
309
Jim Van Verth76d917c2017-12-13 09:26:37 -0500310void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Brian Salomon5e150852017-03-22 14:53:13 -0400311 ASSERT_SINGLE_OWNER
Chris Dalton6c3879d2018-11-01 11:13:19 -0600312
313 auto purgeTime = GrStdSteadyClock::now() - msNotUsed;
314
Jim Van Verth76d917c2017-12-13 09:26:37 -0500315 fResourceCache->purgeAsNeeded();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600316 fResourceCache->purgeResourcesNotUsedSince(purgeTime);
317
318 if (auto ccpr = fDrawingManager->getCoverageCountingPathRenderer()) {
319 ccpr->purgeCacheEntriesOlderThan(purgeTime);
320 }
Jim Van Verth76d917c2017-12-13 09:26:37 -0500321
322 fTextBlobCache->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400323}
324
Derek Sollenberger5480a182017-05-25 16:43:59 -0400325void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
326 ASSERT_SINGLE_OWNER
327 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
328}
329
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000330void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800331 ASSERT_SINGLE_OWNER
332
bsalomon71cb0c22014-11-14 12:10:14 -0800333 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800334 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800335 }
336 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800337 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800338 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000339}
340
Derek Sollenbergeree479142017-05-24 11:41:33 -0400341size_t GrContext::getResourceCachePurgeableBytes() const {
342 ASSERT_SINGLE_OWNER
343 return fResourceCache->getPurgeableBytes();
344}
345
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000346////////////////////////////////////////////////////////////////////////////////
347
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400348int GrContext::maxTextureSize() const { return fCaps->maxTextureSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400349
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400350int GrContext::maxRenderTargetSize() const { return fCaps->maxRenderTargetSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400351
Brian Salomonbdecacf2018-02-02 20:32:49 -0500352bool GrContext::colorTypeSupportedAsImage(SkColorType colorType) const {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400353 GrPixelConfig config = SkColorType2GrPixelConfig(colorType);
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400354 return fCaps->isConfigTexturable(config);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500355}
356
357int GrContext::maxSurfaceSampleCountForColorType(SkColorType colorType) const {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400358 GrPixelConfig config = SkColorType2GrPixelConfig(colorType);
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400359 return fCaps->maxRenderTargetSampleCount(config);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500360}
361
362////////////////////////////////////////////////////////////////////////////////
363
joshualitt0db6dfa2015-04-10 07:01:30 -0700364void GrContext::TextBlobCacheOverBudgetCB(void* data) {
365 SkASSERT(data);
Brian Osman11052242016-10-27 14:47:55 -0400366 // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on
367 // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls
368 // to below the GrContext level, but this is not trivial because they call drawPath on
369 // SkGpuDevice.
joshualitt0db6dfa2015-04-10 07:01:30 -0700370 GrContext* context = reinterpret_cast<GrContext*>(data);
371 context->flush();
372}
373
bsalomon@google.com27847de2011-02-22 20:59:41 +0000374////////////////////////////////////////////////////////////////////////////////
375
bsalomonb77a9072016-09-07 10:02:04 -0700376void GrContext::flush() {
joshualitt1de610a2016-01-06 08:26:09 -0800377 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700378 RETURN_IF_ABANDONED
Robert Phillips7ee385e2017-03-30 08:02:11 -0400379
380 fDrawingManager->flush(nullptr);
381}
382
Greg Daniel51316782017-08-02 15:10:09 +0000383GrSemaphoresSubmitted GrContext::flushAndSignalSemaphores(int numSemaphores,
384 GrBackendSemaphore signalSemaphores[]) {
385 ASSERT_SINGLE_OWNER
386 if (fDrawingManager->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; }
387
388 return fDrawingManager->flush(nullptr, numSemaphores, signalSemaphores);
389}
390
Robert Phillips7ee385e2017-03-30 08:02:11 -0400391void GrContextPriv::flush(GrSurfaceProxy* proxy) {
392 ASSERT_SINGLE_OWNER_PRIV
393 RETURN_IF_ABANDONED_PRIV
394 ASSERT_OWNED_PROXY_PRIV(proxy);
395
396 fContext->fDrawingManager->flush(proxy);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000397}
398
Brian Salomonc320b152018-02-20 14:05:36 -0500399// TODO: This will be removed when GrSurfaceContexts are aware of their color types.
400// (skbug.com/6718)
Brian Osmand2ca59a2017-04-13 14:03:57 -0400401static bool valid_premul_config(GrPixelConfig config) {
Brian Salomonc320b152018-02-20 14:05:36 -0500402 switch (config) {
403 case kUnknown_GrPixelConfig: return false;
404 case kAlpha_8_GrPixelConfig: return false;
405 case kGray_8_GrPixelConfig: return false;
406 case kRGB_565_GrPixelConfig: return false;
407 case kRGBA_4444_GrPixelConfig: return true;
408 case kRGBA_8888_GrPixelConfig: return true;
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400409 case kRGB_888_GrPixelConfig: return false;
Jim Van Verth69e57852018-12-05 13:38:59 -0500410 case kRG_88_GrPixelConfig: return false;
Brian Salomonc320b152018-02-20 14:05:36 -0500411 case kBGRA_8888_GrPixelConfig: return true;
412 case kSRGBA_8888_GrPixelConfig: return true;
413 case kSBGRA_8888_GrPixelConfig: return true;
Brian Osman10fc6fd2018-03-02 11:01:10 -0500414 case kRGBA_1010102_GrPixelConfig: return true;
Brian Salomonc320b152018-02-20 14:05:36 -0500415 case kRGBA_float_GrPixelConfig: return true;
416 case kRG_float_GrPixelConfig: return false;
417 case kAlpha_half_GrPixelConfig: return false;
418 case kRGBA_half_GrPixelConfig: return true;
419 case kAlpha_8_as_Alpha_GrPixelConfig: return false;
420 case kAlpha_8_as_Red_GrPixelConfig: return false;
421 case kAlpha_half_as_Red_GrPixelConfig: return false;
422 case kGray_8_as_Lum_GrPixelConfig: return false;
423 case kGray_8_as_Red_GrPixelConfig: return false;
424 }
425 SK_ABORT("Invalid GrPixelConfig");
426 return false;
Brian Osmance425512017-03-22 14:37:50 -0400427}
428
Brian Salomonc320b152018-02-20 14:05:36 -0500429static bool valid_premul_color_type(GrColorType ct) {
430 switch (ct) {
Brian Osman10fc6fd2018-03-02 11:01:10 -0500431 case GrColorType::kUnknown: return false;
432 case GrColorType::kAlpha_8: return false;
433 case GrColorType::kRGB_565: return false;
434 case GrColorType::kABGR_4444: return true;
435 case GrColorType::kRGBA_8888: return true;
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400436 case GrColorType::kRGB_888x: return false;
Jim Van Verth69e57852018-12-05 13:38:59 -0500437 case GrColorType::kRG_88: return false;
Brian Osman10fc6fd2018-03-02 11:01:10 -0500438 case GrColorType::kBGRA_8888: return true;
439 case GrColorType::kRGBA_1010102: return true;
440 case GrColorType::kGray_8: return false;
441 case GrColorType::kAlpha_F16: return false;
442 case GrColorType::kRGBA_F16: return true;
443 case GrColorType::kRG_F32: return false;
444 case GrColorType::kRGBA_F32: return true;
Brian Salomonc320b152018-02-20 14:05:36 -0500445 }
446 SK_ABORT("Invalid GrColorType");
447 return false;
448}
449
450static bool valid_pixel_conversion(GrColorType cpuColorType, GrPixelConfig gpuConfig,
Brian Osmand2ca59a2017-04-13 14:03:57 -0400451 bool premulConversion) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400452 // We only allow premul <-> unpremul conversions for some formats
Brian Salomonc320b152018-02-20 14:05:36 -0500453 if (premulConversion &&
454 (!valid_premul_color_type(cpuColorType) || !valid_premul_config(gpuConfig))) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400455 return false;
456 }
Brian Osmand2ca59a2017-04-13 14:03:57 -0400457 return true;
458}
459
Brian Salomonc320b152018-02-20 14:05:36 -0500460bool GrContextPriv::writeSurfacePixels(GrSurfaceContext* dst, int left, int top, int width,
461 int height, GrColorType srcColorType,
462 SkColorSpace* srcColorSpace, const void* buffer,
463 size_t rowBytes, uint32_t pixelOpsFlags) {
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500464 ASSERT_SINGLE_OWNER_PRIV
465 RETURN_FALSE_IF_ABANDONED_PRIV
466 SkASSERT(dst);
467 SkASSERT(buffer);
468 ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy());
Brian Salomond494f6e2018-06-01 14:13:43 -0400469 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels", fContext);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500470
471 if (GrColorType::kUnknown == srcColorType) {
472 return false;
473 }
474
475 if (!dst->asSurfaceProxy()->instantiate(this->resourceProvider())) {
476 return false;
477 }
478
479 GrSurfaceProxy* dstProxy = dst->asSurfaceProxy();
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400480 GrSurface* dstSurface = dstProxy->peekSurface();
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500481
482 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
483 GrColorTypeBytesPerPixel(srcColorType), &left, &top,
484 &width, &height, &buffer, &rowBytes)) {
485 return false;
486 }
487
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400488 // TODO: Make GrSurfaceContext know its alpha type and pass src buffer's alpha type.
489 bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
490
491 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
492 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon817847c2018-05-07 13:33:50 -0400493 bool canvas2DFastPath =
Greg Daniel4374e962018-09-28 15:09:47 -0400494 !fContext->contextPriv().caps()->avoidWritePixelsFastPath() &&
Brian Salomon817847c2018-05-07 13:33:50 -0400495 premul &&
Brian Osman34ec3742018-07-03 10:40:57 -0400496 !dst->colorSpaceInfo().colorSpace() &&
Brian Salomon817847c2018-05-07 13:33:50 -0400497 (srcColorType == GrColorType::kRGBA_8888 || srcColorType == GrColorType::kBGRA_8888) &&
498 SkToBool(dst->asRenderTargetContext()) &&
499 (dstProxy->config() == kRGBA_8888_GrPixelConfig ||
500 dstProxy->config() == kBGRA_8888_GrPixelConfig) &&
501 !(pixelOpsFlags & kDontFlush_PixelOpsFlag) &&
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400502 fContext->contextPriv().caps()->isConfigTexturable(kRGBA_8888_GrPixelConfig) &&
Brian Salomon817847c2018-05-07 13:33:50 -0400503 fContext->validPMUPMConversionExists();
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400504
Greg Daniel4065d452018-11-16 15:43:41 -0500505 const GrCaps* caps = this->caps();
506 if (!caps->surfaceSupportsWritePixels(dstSurface) ||
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400507 canvas2DFastPath) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400508 // We don't expect callers that are skipping flushes to require an intermediate draw.
509 SkASSERT(!(pixelOpsFlags & kDontFlush_PixelOpsFlag));
510 if (pixelOpsFlags & kDontFlush_PixelOpsFlag) {
511 return false;
512 }
513
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500514 GrSurfaceDesc desc;
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500515 desc.fWidth = width;
516 desc.fHeight = height;
517 desc.fSampleCnt = 1;
Greg Daniel4065d452018-11-16 15:43:41 -0500518
519 GrBackendFormat format;
520 if (canvas2DFastPath) {
521 desc.fConfig = kRGBA_8888_GrPixelConfig;
522 format =
523 fContext->contextPriv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
524 } else {
525 desc.fConfig = dstProxy->config();
526 format = dstProxy->backendFormat().makeTexture2D();
527 if (!format.isValid()) {
528 return false;
529 }
530 }
531
Brian Salomon2a4f9832018-03-03 22:43:43 -0500532 auto tempProxy = this->proxyProvider()->createProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500533 format, desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kApprox, SkBudgeted::kYes);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500534 if (!tempProxy) {
535 return false;
536 }
537 auto tempCtx = this->drawingManager()->makeTextureContext(
538 tempProxy, dst->colorSpaceInfo().refColorSpace());
539 if (!tempCtx) {
540 return false;
541 }
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400542 uint32_t flags = canvas2DFastPath ? 0 : pixelOpsFlags;
Brian Salomon817847c2018-05-07 13:33:50 -0400543 // In the fast path we always write the srcData to the temp context as though it were RGBA.
544 // When the data is really BGRA the write will cause the R and B channels to be swapped in
545 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
546 // dst.
547 auto tmpColorType = canvas2DFastPath ? GrColorType::kRGBA_8888 : srcColorType;
Brian Salomond494f6e2018-06-01 14:13:43 -0400548 if (!this->writeSurfacePixels(tempCtx.get(), 0, 0, width, height, tmpColorType,
549 srcColorSpace, buffer, rowBytes, flags)) {
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500550 return false;
551 }
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400552 if (canvas2DFastPath) {
553 GrPaint paint;
554 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomon817847c2018-05-07 13:33:50 -0400555 auto fp = fContext->createUPMToPMEffect(
Brian Osman5ea96bf2018-10-02 14:58:05 -0400556 GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I()));
Brian Salomon817847c2018-05-07 13:33:50 -0400557 if (srcColorType == GrColorType::kBGRA_8888) {
558 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
559 }
560 if (!fp) {
561 return false;
562 }
563 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400564 dst->asRenderTargetContext()->fillRectToRect(
565 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
566 SkRect::MakeXYWH(left, top, width, height), SkRect::MakeWH(width, height));
567 return true;
568 } else {
569 return dst->copy(tempProxy.get(), SkIRect::MakeWH(width, height), {left, top});
570 }
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500571 }
572
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500573 bool convert = premul;
574
575 if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) {
576 return false;
577 }
578
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400579 GrColorType allowedColorType = fContext->contextPriv().caps()->supportedWritePixelsColorType(
580 dstProxy->config(), srcColorType);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500581 convert = convert || (srcColorType != allowedColorType);
582
583 if (!dst->colorSpaceInfo().colorSpace()) {
584 // "Legacy" mode - no color space conversions.
585 srcColorSpace = nullptr;
586 }
587 convert = convert || !SkColorSpace::Equals(srcColorSpace, dst->colorSpaceInfo().colorSpace());
588
589 std::unique_ptr<char[]> tempBuffer;
590 if (convert) {
591 auto srcSkColorType = GrColorTypeToSkColorType(srcColorType);
592 auto dstSkColorType = GrColorTypeToSkColorType(allowedColorType);
593 if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) {
594 return false;
595 }
Brian Osman2091fbb2018-10-11 11:05:37 -0400596 auto srcAlphaType = SkColorTypeIsAlwaysOpaque(srcSkColorType)
597 ? kOpaque_SkAlphaType
598 : (premul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500599 SkPixmap src(SkImageInfo::Make(width, height, srcSkColorType, srcAlphaType,
600 sk_ref_sp(srcColorSpace)),
601 buffer, rowBytes);
602 auto tempSrcII = SkImageInfo::Make(width, height, dstSkColorType, kPremul_SkAlphaType,
603 dst->colorSpaceInfo().refColorSpace());
604 auto size = tempSrcII.computeMinByteSize();
605 if (!size) {
606 return false;
607 }
608 tempBuffer.reset(new char[size]);
609 SkPixmap tempSrc(tempSrcII, tempBuffer.get(), tempSrcII.minRowBytes());
610 if (!src.readPixels(tempSrc)) {
611 return false;
612 }
613 srcColorType = allowedColorType;
614 buffer = tempSrc.addr();
615 rowBytes = tempSrc.rowBytes();
616 if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
617 std::unique_ptr<char[]> row(new char[rowBytes]);
618 for (int y = 0; y < height / 2; ++y) {
619 memcpy(row.get(), tempSrc.addr(0, y), rowBytes);
620 memcpy(tempSrc.writable_addr(0, y), tempSrc.addr(0, height - 1 - y), rowBytes);
621 memcpy(tempSrc.writable_addr(0, height - 1 - y), row.get(), rowBytes);
622 }
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400623 top = dstSurface->height() - top - height;
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500624 }
625 } else if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
626 size_t trimRowBytes = GrColorTypeBytesPerPixel(srcColorType) * width;
627 tempBuffer.reset(new char[trimRowBytes * height]);
628 char* dst = reinterpret_cast<char*>(tempBuffer.get()) + trimRowBytes * (height - 1);
629 const char* src = reinterpret_cast<const char*>(buffer);
630 for (int i = 0; i < height; ++i, src += rowBytes, dst -= trimRowBytes) {
631 memcpy(dst, src, trimRowBytes);
632 }
633 buffer = tempBuffer.get();
634 rowBytes = trimRowBytes;
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400635 top = dstSurface->height() - top - height;
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500636 }
637
638 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) {
639 this->flush(nullptr); // MDB TODO: tighten this
640 }
641
642 return this->getGpu()->writePixels(dstSurface, left, top, width, height, srcColorType, buffer,
643 rowBytes);
644}
645
Brian Salomond494f6e2018-06-01 14:13:43 -0400646bool GrContextPriv::readSurfacePixels(GrSurfaceContext* src, int left, int top, int width,
647 int height, GrColorType dstColorType,
648 SkColorSpace* dstColorSpace, void* buffer, size_t rowBytes,
649 uint32_t pixelOpsFlags) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400650 ASSERT_SINGLE_OWNER_PRIV
651 RETURN_FALSE_IF_ABANDONED_PRIV
652 SkASSERT(src);
653 SkASSERT(buffer);
654 ASSERT_OWNED_PROXY_PRIV(src->asSurfaceProxy());
Brian Salomond494f6e2018-06-01 14:13:43 -0400655 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "readSurfacePixels", fContext);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400656
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400657 SkASSERT(!(pixelOpsFlags & kDontFlush_PixelOpsFlag));
658 if (pixelOpsFlags & kDontFlush_PixelOpsFlag) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400659 return false;
660 }
661
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400662 // MDB TODO: delay this instantiation until later in the method
663 if (!src->asSurfaceProxy()->instantiate(this->resourceProvider())) {
664 return false;
665 }
666
667 GrSurfaceProxy* srcProxy = src->asSurfaceProxy();
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400668 GrSurface* srcSurface = srcProxy->peekSurface();
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400669
670 if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(),
671 GrColorTypeBytesPerPixel(dstColorType), &left, &top,
672 &width, &height, &buffer, &rowBytes)) {
673 return false;
674 }
675
676 // TODO: Make GrSurfaceContext know its alpha type and pass dst buffer's alpha type.
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400677 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400678
679 if (!valid_pixel_conversion(dstColorType, srcProxy->config(), unpremul)) {
680 return false;
681 }
682
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400683 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
684 // care so much about getImageData performance. However, in order to ensure putImageData/
685 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
Brian Salomond494f6e2018-06-01 14:13:43 -0400686 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400687 // fContext->vaildaPMUPMConversionExists()).
Brian Salomon817847c2018-05-07 13:33:50 -0400688 bool canvas2DFastPath =
689 unpremul &&
Brian Osman34ec3742018-07-03 10:40:57 -0400690 !src->colorSpaceInfo().colorSpace() &&
Brian Salomon817847c2018-05-07 13:33:50 -0400691 (GrColorType::kRGBA_8888 == dstColorType || GrColorType::kBGRA_8888 == dstColorType) &&
692 SkToBool(srcProxy->asTextureProxy()) &&
693 (srcProxy->config() == kRGBA_8888_GrPixelConfig ||
694 srcProxy->config() == kBGRA_8888_GrPixelConfig) &&
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400695 fContext->contextPriv().caps()->isConfigRenderable(kRGBA_8888_GrPixelConfig) &&
Brian Salomon817847c2018-05-07 13:33:50 -0400696 fContext->validPMUPMConversionExists();
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400697
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400698 if (!fContext->contextPriv().caps()->surfaceSupportsReadPixels(srcSurface) ||
699 canvas2DFastPath) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400700 GrSurfaceDesc desc;
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400701 desc.fFlags = canvas2DFastPath ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
702 desc.fConfig = canvas2DFastPath ? kRGBA_8888_GrPixelConfig : srcProxy->config();
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400703 desc.fWidth = width;
704 desc.fHeight = height;
705 desc.fSampleCnt = 1;
Greg Daniel4065d452018-11-16 15:43:41 -0500706
707 GrBackendFormat format;
708 if (canvas2DFastPath) {
709 desc.fFlags = kRenderTarget_GrSurfaceFlag;
710 desc.fConfig = kRGBA_8888_GrPixelConfig;
711 format = this->caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
712 } else {
713 desc.fFlags = kNone_GrSurfaceFlags;
714 desc.fConfig = srcProxy->config();
715 format = srcProxy->backendFormat().makeTexture2D();
716 if (!format.isValid()) {
717 return false;
718 }
719 }
720
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400721 auto tempProxy = this->proxyProvider()->createProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500722 format, desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kApprox, SkBudgeted::kYes);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400723 if (!tempProxy) {
724 return false;
725 }
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400726 sk_sp<GrSurfaceContext> tempCtx;
727 if (canvas2DFastPath) {
728 tempCtx = this->drawingManager()->makeRenderTargetContext(std::move(tempProxy), nullptr,
729 nullptr);
Greg Daniel42314012018-04-23 10:57:37 -0400730 SkASSERT(tempCtx->asRenderTargetContext());
731 tempCtx->asRenderTargetContext()->discard();
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400732 } else {
733 tempCtx = this->drawingManager()->makeTextureContext(
734 std::move(tempProxy), src->colorSpaceInfo().refColorSpace());
735 }
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400736 if (!tempCtx) {
737 return false;
738 }
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400739 if (canvas2DFastPath) {
740 GrPaint paint;
741 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomon817847c2018-05-07 13:33:50 -0400742 auto fp = fContext->createPMToUPMEffect(
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400743 GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()),
Brian Osman5ea96bf2018-10-02 14:58:05 -0400744 SkMatrix::I()));
Brian Salomon817847c2018-05-07 13:33:50 -0400745 if (dstColorType == GrColorType::kBGRA_8888) {
746 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
747 dstColorType = GrColorType::kRGBA_8888;
748 }
749 if (!fp) {
750 return false;
751 }
752 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400753 tempCtx->asRenderTargetContext()->fillRectToRect(
754 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
755 SkRect::MakeWH(width, height), SkRect::MakeXYWH(left, top, width, height));
756 } else if (!tempCtx->copy(srcProxy, SkIRect::MakeXYWH(left, top, width, height), {0, 0})) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400757 return false;
758 }
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400759 uint32_t flags = canvas2DFastPath ? 0 : pixelOpsFlags;
Brian Salomond494f6e2018-06-01 14:13:43 -0400760 return this->readSurfacePixels(tempCtx.get(), 0, 0, width, height, dstColorType,
761 dstColorSpace, buffer, rowBytes, flags);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400762 }
763
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400764 bool convert = unpremul;
765
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400766 bool flip = srcProxy->origin() == kBottomLeft_GrSurfaceOrigin;
767 if (flip) {
768 top = srcSurface->height() - top - height;
769 }
770
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400771 GrColorType allowedColorType = fContext->contextPriv().caps()->supportedReadPixelsColorType(
772 srcProxy->config(), dstColorType);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400773 convert = convert || (dstColorType != allowedColorType);
774
775 if (!src->colorSpaceInfo().colorSpace()) {
776 // "Legacy" mode - no color space conversions.
777 dstColorSpace = nullptr;
778 }
779 convert = convert || !SkColorSpace::Equals(dstColorSpace, src->colorSpaceInfo().colorSpace());
780
781 SkAutoPixmapStorage tempPixmap;
782 SkPixmap finalPixmap;
783 if (convert) {
784 SkColorType srcSkColorType = GrColorTypeToSkColorType(allowedColorType);
785 SkColorType dstSkColorType = GrColorTypeToSkColorType(dstColorType);
Brian Osman2091fbb2018-10-11 11:05:37 -0400786 bool srcAlwaysOpaque = SkColorTypeIsAlwaysOpaque(srcSkColorType);
787 bool dstAlwaysOpaque = SkColorTypeIsAlwaysOpaque(dstSkColorType);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400788 if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) {
789 return false;
790 }
Brian Osman2091fbb2018-10-11 11:05:37 -0400791 auto tempAT = srcAlwaysOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400792 auto tempII = SkImageInfo::Make(width, height, srcSkColorType, tempAT,
793 src->colorSpaceInfo().refColorSpace());
Brian Osman2091fbb2018-10-11 11:05:37 -0400794 SkASSERT(!unpremul || !dstAlwaysOpaque);
795 auto finalAT = (srcAlwaysOpaque || dstAlwaysOpaque)
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400796 ? kOpaque_SkAlphaType
797 : unpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
798 auto finalII =
799 SkImageInfo::Make(width, height, dstSkColorType, finalAT, sk_ref_sp(dstColorSpace));
800 if (!SkImageInfoValidConversion(finalII, tempII)) {
801 return false;
802 }
Brian Salomon6196fc12018-07-12 16:39:23 -0400803 if (!tempPixmap.tryAlloc(tempII)) {
804 return false;
805 }
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400806 finalPixmap.reset(finalII, buffer, rowBytes);
807 buffer = tempPixmap.writable_addr();
808 rowBytes = tempPixmap.rowBytes();
Brian Salomon3d945e82018-05-14 14:54:06 -0400809 // Chrome msan bots require this.
810 sk_bzero(buffer, tempPixmap.computeByteSize());
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400811 }
812
813 if (srcSurface->surfacePriv().hasPendingWrite()) {
814 this->flush(nullptr); // MDB TODO: tighten this
815 }
816
817 if (!fContext->fGpu->readPixels(srcSurface, left, top, width, height, allowedColorType, buffer,
818 rowBytes)) {
819 return false;
820 }
821
822 if (flip) {
823 size_t trimRowBytes = GrColorTypeBytesPerPixel(allowedColorType) * width;
824 std::unique_ptr<char[]> row(new char[trimRowBytes]);
825 char* upper = reinterpret_cast<char*>(buffer);
826 char* lower = reinterpret_cast<char*>(buffer) + (height - 1) * rowBytes;
827 for (int y = 0; y < height / 2; ++y, upper += rowBytes, lower -= rowBytes) {
828 memcpy(row.get(), upper, trimRowBytes);
829 memcpy(upper, lower, trimRowBytes);
830 memcpy(lower, row.get(), trimRowBytes);
831 }
832 }
833 if (convert) {
834 if (!tempPixmap.readPixels(finalPixmap)) {
835 return false;
836 }
837 }
838 return true;
839}
840
Robert Phillips7ee385e2017-03-30 08:02:11 -0400841void GrContextPriv::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) {
842 ASSERT_SINGLE_OWNER_PRIV
843 RETURN_IF_ABANDONED_PRIV
844 SkASSERT(proxy);
845 ASSERT_OWNED_PROXY_PRIV(proxy);
Greg Daniel51316782017-08-02 15:10:09 +0000846 fContext->fDrawingManager->prepareSurfaceForExternalIO(proxy, 0, nullptr);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000847}
848
Robert Phillips7ee385e2017-03-30 08:02:11 -0400849void GrContextPriv::flushSurfaceWrites(GrSurfaceProxy* proxy) {
850 ASSERT_SINGLE_OWNER_PRIV
851 RETURN_IF_ABANDONED_PRIV
852 SkASSERT(proxy);
853 ASSERT_OWNED_PROXY_PRIV(proxy);
854 if (proxy->priv().hasPendingWrite()) {
855 this->flush(proxy);
bsalomonf80bfed2014-10-07 05:56:02 -0700856 }
857}
858
Robert Phillips7ee385e2017-03-30 08:02:11 -0400859void GrContextPriv::flushSurfaceIO(GrSurfaceProxy* proxy) {
860 ASSERT_SINGLE_OWNER_PRIV
861 RETURN_IF_ABANDONED_PRIV
862 SkASSERT(proxy);
863 ASSERT_OWNED_PROXY_PRIV(proxy);
864 if (proxy->priv().hasPendingIO()) {
865 this->flush(proxy);
ajuma95243eb2016-08-24 08:19:02 -0700866 }
867}
868
bsalomon@google.com27847de2011-02-22 20:59:41 +0000869////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000870
Robert Phillipsc994a932018-06-19 13:09:54 -0400871sk_sp<GrOpMemoryPool> GrContextPriv::refOpMemoryPool() {
872 if (!fContext->fOpMemoryPool) {
873 // DDL TODO: should the size of the memory pool be decreased in DDL mode? CPU-side memory
874 // consumed in DDL mode vs. normal mode for a single skp might be a good metric of wasted
875 // memory.
876 fContext->fOpMemoryPool = sk_sp<GrOpMemoryPool>(new GrOpMemoryPool(16384, 16384));
877 }
878
879 SkASSERT(fContext->fOpMemoryPool);
880 return fContext->fOpMemoryPool;
881}
882
883GrOpMemoryPool* GrContextPriv::opMemoryPool() {
884 return this->refOpMemoryPool().get();
885}
886
Robert Phillips2c862492017-01-18 10:08:39 -0500887sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy,
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500888 sk_sp<SkColorSpace> colorSpace,
889 const SkSurfaceProps* props) {
Brian Osman45580d32016-11-23 09:37:01 -0500890 ASSERT_SINGLE_OWNER_PRIV
891
Brian Osman45580d32016-11-23 09:37:01 -0500892 if (proxy->asRenderTargetProxy()) {
Robert Phillips2c862492017-01-18 10:08:39 -0500893 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500894 std::move(colorSpace), props);
Brian Osman45580d32016-11-23 09:37:01 -0500895 } else {
896 SkASSERT(proxy->asTextureProxy());
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500897 SkASSERT(!props);
Robert Phillips2c862492017-01-18 10:08:39 -0500898 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Brian Osman45580d32016-11-23 09:37:01 -0500899 }
900}
901
Greg Daniel4065d452018-11-16 15:43:41 -0500902sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrBackendFormat& format,
903 const GrSurfaceDesc& dstDesc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500904 GrSurfaceOrigin origin,
Greg Daniel65c7f662017-10-30 13:39:09 -0400905 GrMipMapped mipMapped,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500906 SkBackingFit fit,
Brian Salomon366093f2018-02-13 09:25:22 -0500907 SkBudgeted isDstBudgeted,
908 sk_sp<SkColorSpace> colorSpace,
909 const SkSurfaceProps* props) {
Greg Daniel65c7f662017-10-30 13:39:09 -0400910 sk_sp<GrTextureProxy> proxy;
911 if (GrMipMapped::kNo == mipMapped) {
Greg Daniel4065d452018-11-16 15:43:41 -0500912 proxy = this->proxyProvider()->createProxy(format, dstDesc, origin, fit, isDstBudgeted);
Greg Daniel65c7f662017-10-30 13:39:09 -0400913 } else {
914 SkASSERT(SkBackingFit::kExact == fit);
Greg Daniel4065d452018-11-16 15:43:41 -0500915 proxy = this->proxyProvider()->createMipMapProxy(format, dstDesc, origin, isDstBudgeted);
Greg Daniel65c7f662017-10-30 13:39:09 -0400916 }
Robert Phillips77b3f322017-01-31 18:24:12 -0500917 if (!proxy) {
918 return nullptr;
919 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500920
Greg Daniel55fa6472018-03-16 16:13:10 -0400921 sk_sp<GrSurfaceContext> sContext = this->makeWrappedSurfaceContext(std::move(proxy),
922 std::move(colorSpace),
923 props);
924 if (sContext && sContext->asRenderTargetContext()) {
925 sContext->asRenderTargetContext()->discard();
926 }
927
928 return sContext;
Robert Phillipse2f7d182016-12-15 09:23:05 -0500929}
930
Brian Salomond17f6582017-07-19 18:28:58 -0400931sk_sp<GrTextureContext> GrContextPriv::makeBackendTextureContext(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000932 GrSurfaceOrigin origin,
Brian Osmanc1e37052017-03-09 14:19:20 -0500933 sk_sp<SkColorSpace> colorSpace) {
Robert Phillips26caf892017-01-27 10:58:31 -0500934 ASSERT_SINGLE_OWNER_PRIV
935
Brian Salomonc67c31c2018-12-06 10:00:03 -0500936 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->wrapBackendTexture(
937 tex, origin, kBorrow_GrWrapOwnership, kRW_GrIOType);
Robert Phillips77b3f322017-01-31 18:24:12 -0500938 if (!proxy) {
939 return nullptr;
940 }
Robert Phillips26caf892017-01-27 10:58:31 -0500941
Brian Salomond17f6582017-07-19 18:28:58 -0400942 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Robert Phillips26caf892017-01-27 10:58:31 -0500943}
944
Brian Osman11052242016-10-27 14:47:55 -0400945sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000946 const GrBackendTexture& tex,
947 GrSurfaceOrigin origin,
948 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -0400949 sk_sp<SkColorSpace> colorSpace,
Brian Osmanc1e37052017-03-09 14:19:20 -0500950 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700951 ASSERT_SINGLE_OWNER_PRIV
Brian Salomonbdecacf2018-02-02 20:32:49 -0500952 SkASSERT(sampleCnt > 0);
robertphillips4fd74ae2016-08-03 14:26:53 -0700953
Brian Salomon7578f3e2018-03-07 14:39:54 -0500954 sk_sp<GrTextureProxy> proxy(
955 this->proxyProvider()->wrapRenderableBackendTexture(tex, origin, sampleCnt));
Robert Phillips77b3f322017-01-31 18:24:12 -0500956 if (!proxy) {
957 return nullptr;
958 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400959
Robert Phillips37430132016-11-09 06:50:43 -0500960 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Brian Osman11052242016-10-27 14:47:55 -0400961 std::move(colorSpace), props);
robertphillips4fd74ae2016-08-03 14:26:53 -0700962}
963
Brian Osman11052242016-10-27 14:47:55 -0400964sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +0000965 const GrBackendRenderTarget& backendRT,
966 GrSurfaceOrigin origin,
robertphillips4fd74ae2016-08-03 14:26:53 -0700967 sk_sp<SkColorSpace> colorSpace,
968 const SkSurfaceProps* surfaceProps) {
969 ASSERT_SINGLE_OWNER_PRIV
970
Brian Salomon7578f3e2018-03-07 14:39:54 -0500971 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->wrapBackendRenderTarget(backendRT, origin);
Robert Phillips77b3f322017-01-31 18:24:12 -0500972 if (!proxy) {
973 return nullptr;
974 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400975
Robert Phillips37430132016-11-09 06:50:43 -0500976 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400977 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -0400978 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700979}
980
Brian Osman11052242016-10-27 14:47:55 -0400981sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000982 const GrBackendTexture& tex,
983 GrSurfaceOrigin origin,
984 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -0700985 sk_sp<SkColorSpace> colorSpace,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500986 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700987 ASSERT_SINGLE_OWNER_PRIV
Brian Salomonbdecacf2018-02-02 20:32:49 -0500988 SkASSERT(sampleCnt > 0);
Brian Salomon7578f3e2018-03-07 14:39:54 -0500989 sk_sp<GrSurfaceProxy> proxy(
990 this->proxyProvider()->wrapBackendTextureAsRenderTarget(tex, origin, sampleCnt));
Robert Phillips77b3f322017-01-31 18:24:12 -0500991 if (!proxy) {
992 return nullptr;
993 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400994
Robert Phillips37430132016-11-09 06:50:43 -0500995 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400996 std::move(colorSpace),
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500997 props);
robertphillips77a2e522015-10-17 07:43:27 -0700998}
999
Chris Daltonfe199b72017-05-05 11:26:15 -04001000void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
1001 fContext->fDrawingManager->addOnFlushCallbackObject(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -04001002}
1003
Robert Phillips62000362018-02-01 09:10:04 -05001004void GrContextPriv::moveOpListsToDDL(SkDeferredDisplayList* ddl) {
1005 fContext->fDrawingManager->moveOpListsToDDL(ddl);
1006}
1007
1008void GrContextPriv::copyOpListsFromDDL(const SkDeferredDisplayList* ddl,
1009 GrRenderTargetProxy* newDest) {
1010 fContext->fDrawingManager->copyOpListsFromDDL(ddl, newDest);
1011}
1012
robertphillips48fde9c2016-09-06 05:20:20 -07001013static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
Brian Osman78f20e02017-01-12 10:28:01 -05001014 switch (config) {
1015 case kAlpha_8_GrPixelConfig:
Greg Daniel93b4ddd2018-04-16 16:16:47 -04001016 case kAlpha_8_as_Alpha_GrPixelConfig:
1017 case kAlpha_8_as_Red_GrPixelConfig:
Brian Osman78f20e02017-01-12 10:28:01 -05001018 case kRGB_565_GrPixelConfig:
1019 case kRGBA_4444_GrPixelConfig:
1020 case kBGRA_8888_GrPixelConfig:
Brian Osman10fc6fd2018-03-02 11:01:10 -05001021 case kRGBA_1010102_GrPixelConfig:
Brian Osman9c111352018-10-16 10:18:26 -04001022 case kRGBA_half_GrPixelConfig:
Brian Osman78f20e02017-01-12 10:28:01 -05001023 return kRGBA_8888_GrPixelConfig;
1024 case kSBGRA_8888_GrPixelConfig:
1025 return kSRGBA_8888_GrPixelConfig;
1026 case kAlpha_half_GrPixelConfig:
Greg Daniel93b4ddd2018-04-16 16:16:47 -04001027 case kAlpha_half_as_Red_GrPixelConfig:
Brian Osman78f20e02017-01-12 10:28:01 -05001028 return kRGBA_half_GrPixelConfig;
Greg Daniel93b4ddd2018-04-16 16:16:47 -04001029 case kGray_8_GrPixelConfig:
1030 case kGray_8_as_Lum_GrPixelConfig:
1031 case kGray_8_as_Red_GrPixelConfig:
1032 return kRGB_888_GrPixelConfig;
Brian Osman78f20e02017-01-12 10:28:01 -05001033 default:
1034 return kUnknown_GrPixelConfig;
1035 }
robertphillips48fde9c2016-09-06 05:20:20 -07001036}
1037
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001038sk_sp<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContextWithFallback(
Greg Daniel4065d452018-11-16 15:43:41 -05001039 const GrBackendFormat& format,
robertphillipsd728f0c2016-11-21 11:05:03 -08001040 SkBackingFit fit,
1041 int width, int height,
1042 GrPixelConfig config,
1043 sk_sp<SkColorSpace> colorSpace,
1044 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -04001045 GrMipMapped mipMapped,
robertphillipsd728f0c2016-11-21 11:05:03 -08001046 GrSurfaceOrigin origin,
1047 const SkSurfaceProps* surfaceProps,
1048 SkBudgeted budgeted) {
Greg Daniel4065d452018-11-16 15:43:41 -05001049 GrBackendFormat localFormat = format;
Brian Salomonbdecacf2018-02-02 20:32:49 -05001050 SkASSERT(sampleCnt > 0);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001051 if (0 == fContext->contextPriv().caps()->getRenderTargetSampleCount(sampleCnt, config)) {
robertphillipsd728f0c2016-11-21 11:05:03 -08001052 config = GrPixelConfigFallback(config);
Greg Daniel4065d452018-11-16 15:43:41 -05001053 // TODO: First we should be checking the getRenderTargetSampleCount from the GrBackendFormat
1054 // and not GrPixelConfig. Besides that, we should implement the fallback in the caps, but
1055 // for now we just convert the fallback pixel config to an SkColorType and then get the
1056 // GrBackendFormat from that.
1057 SkColorType colorType;
1058 if (!GrPixelConfigToColorType(config, &colorType)) {
1059 return nullptr;
1060 }
1061 localFormat = fContext->fCaps->getBackendFormatFromColorType(colorType);
robertphillipsd728f0c2016-11-21 11:05:03 -08001062 }
1063
Greg Daniel4065d452018-11-16 15:43:41 -05001064 return this->makeDeferredRenderTargetContext(localFormat, fit, width, height, config,
1065 std::move(colorSpace), sampleCnt, mipMapped,
1066 origin, surfaceProps, budgeted);
robertphillipsd728f0c2016-11-21 11:05:03 -08001067}
1068
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001069sk_sp<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -05001070 const GrBackendFormat& format,
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001071 SkBackingFit fit,
1072 int width, int height,
1073 GrPixelConfig config,
1074 sk_sp<SkColorSpace> colorSpace,
1075 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -04001076 GrMipMapped mipMapped,
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001077 GrSurfaceOrigin origin,
1078 const SkSurfaceProps* surfaceProps,
1079 SkBudgeted budgeted) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05001080 SkASSERT(sampleCnt > 0);
Khushalc421ca12018-06-26 14:38:34 -07001081 if (fContext->abandoned()) {
Brian Salomon79e4d1b2017-07-13 11:17:11 -04001082 return nullptr;
1083 }
1084
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001085 GrSurfaceDesc desc;
1086 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001087 desc.fWidth = width;
1088 desc.fHeight = height;
1089 desc.fConfig = config;
1090 desc.fSampleCnt = sampleCnt;
1091
Greg Daniele1da1d92017-10-06 15:59:27 -04001092 sk_sp<GrTextureProxy> rtp;
Greg Daniel45d63032017-10-30 13:41:26 -04001093 if (GrMipMapped::kNo == mipMapped) {
Greg Daniel4065d452018-11-16 15:43:41 -05001094 rtp = fContext->fProxyProvider->createProxy(format, desc, origin, fit, budgeted);
Greg Daniele1da1d92017-10-06 15:59:27 -04001095 } else {
Greg Daniel4065d452018-11-16 15:43:41 -05001096 rtp = fContext->fProxyProvider->createMipMapProxy(format, desc, origin, budgeted);
Greg Daniele1da1d92017-10-06 15:59:27 -04001097 }
Robert Phillips08c5ec72017-01-30 12:26:47 -05001098 if (!rtp) {
1099 return nullptr;
1100 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001101
Robert Phillips1119dc32017-04-11 12:54:57 -04001102 sk_sp<GrRenderTargetContext> renderTargetContext(
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001103 fContext->fDrawingManager->makeRenderTargetContext(std::move(rtp),
1104 std::move(colorSpace),
1105 surfaceProps));
Robert Phillips1119dc32017-04-11 12:54:57 -04001106 if (!renderTargetContext) {
1107 return nullptr;
1108 }
1109
1110 renderTargetContext->discard();
1111
1112 return renderTargetContext;
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001113}
1114
Brian Salomonaff329b2017-08-11 09:40:37 -04001115std::unique_ptr<GrFragmentProcessor> GrContext::createPMToUPMEffect(
Brian Osman5ea96bf2018-10-02 14:58:05 -04001116 std::unique_ptr<GrFragmentProcessor> fp) {
Robert Phillips757914d2017-01-25 15:48:30 -05001117 ASSERT_SINGLE_OWNER
Brian Osman5ea96bf2018-10-02 14:58:05 -04001118 // We should have already called this->validPMUPMConversionExists() in this case
1119 SkASSERT(fDidTestPMConversions);
1120 // ...and it should have succeeded
1121 SkASSERT(this->validPMUPMConversionExists());
Brian Osman409e74f2017-04-17 11:48:28 -04001122
Brian Osman5ea96bf2018-10-02 14:58:05 -04001123 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
Robert Phillips757914d2017-01-25 15:48:30 -05001124}
1125
Brian Salomonaff329b2017-08-11 09:40:37 -04001126std::unique_ptr<GrFragmentProcessor> GrContext::createUPMToPMEffect(
Brian Osman5ea96bf2018-10-02 14:58:05 -04001127 std::unique_ptr<GrFragmentProcessor> fp) {
joshualitt1de610a2016-01-06 08:26:09 -08001128 ASSERT_SINGLE_OWNER
Brian Osman5ea96bf2018-10-02 14:58:05 -04001129 // We should have already called this->validPMUPMConversionExists() in this case
1130 SkASSERT(fDidTestPMConversions);
1131 // ...and it should have succeeded
1132 SkASSERT(this->validPMUPMConversionExists());
Brian Osman409e74f2017-04-17 11:48:28 -04001133
Brian Osman5ea96bf2018-10-02 14:58:05 -04001134 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001135}
1136
Brian Osman409e74f2017-04-17 11:48:28 -04001137bool GrContext::validPMUPMConversionExists() {
joshualitt1de610a2016-01-06 08:26:09 -08001138 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -04001139 if (!fDidTestPMConversions) {
Brian Osman28804f32017-04-20 10:24:36 -04001140 fPMUPMConversionsRoundTrip = GrConfigConversionEffect::TestForPreservingPMConversions(this);
Brian Osman409e74f2017-04-17 11:48:28 -04001141 fDidTestPMConversions = true;
1142 }
1143
bsalomon636e8022015-07-29 06:08:46 -07001144 // The PM<->UPM tests fail or succeed together so we only need to check one.
Brian Osman28804f32017-04-20 10:24:36 -04001145 return fPMUPMConversionsRoundTrip;
bsalomon636e8022015-07-29 06:08:46 -07001146}
1147
Khushal3e7548c2018-05-23 15:45:01 -07001148bool GrContext::supportsDistanceFieldText() const {
1149 return fCaps->shaderCaps()->supportsDistanceFieldText();
1150}
1151
bsalomon37f9a262015-02-02 13:00:10 -08001152//////////////////////////////////////////////////////////////////////////////
1153
Robert Phillipsfc711a22018-02-13 17:03:00 -05001154// DDL TODO: remove 'maxResources'
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001155void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -08001156 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001157 if (maxResources) {
1158 *maxResources = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -08001159 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001160 if (maxResourceBytes) {
1161 *maxResourceBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -08001162 }
1163}
1164
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001165void GrContext::setResourceCacheLimits(int maxResources, size_t maxResourceBytes) {
joshualitt1de610a2016-01-06 08:26:09 -08001166 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001167 fResourceCache->setLimits(maxResources, maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -08001168}
1169
ericrk0a5fa482015-09-15 14:16:10 -07001170//////////////////////////////////////////////////////////////////////////////
ericrk0a5fa482015-09-15 14:16:10 -07001171void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -08001172 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -07001173 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
Khushal71652e22018-10-29 13:05:36 -07001174 traceMemoryDump->dumpNumericValue("skia/gr_text_blob_cache", "size", "bytes",
1175 fTextBlobCache->usedBytes());
ericrk0a5fa482015-09-15 14:16:10 -07001176}
Brian Osman71a18892017-08-10 10:23:25 -04001177
1178//////////////////////////////////////////////////////////////////////////////
Kevin Lubickf4def342018-10-04 12:52:50 -04001179#ifdef SK_ENABLE_DUMP_GPU
1180#include "SkJSONWriter.h"
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001181SkString GrContextPriv::dump() const {
Brian Osman71a18892017-08-10 10:23:25 -04001182 SkDynamicMemoryWStream stream;
1183 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
1184 writer.beginObject();
1185
1186 static const char* kBackendStr[] = {
1187 "Metal",
1188 "OpenGL",
1189 "Vulkan",
1190 "Mock",
1191 };
Greg Danielbdf12ad2018-10-12 09:31:11 -04001192 GR_STATIC_ASSERT(0 == (unsigned)GrBackendApi::kMetal);
1193 GR_STATIC_ASSERT(1 == (unsigned)GrBackendApi::kOpenGL);
1194 GR_STATIC_ASSERT(2 == (unsigned)GrBackendApi::kVulkan);
1195 GR_STATIC_ASSERT(3 == (unsigned)GrBackendApi::kMock);
1196 writer.appendString("backend", kBackendStr[(unsigned)fContext->fBackend]);
Brian Osman71a18892017-08-10 10:23:25 -04001197
1198 writer.appendName("caps");
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001199 fContext->fCaps->dumpJSON(&writer);
Brian Osman71a18892017-08-10 10:23:25 -04001200
1201 writer.appendName("gpu");
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001202 fContext->fGpu->dumpJSON(&writer);
Brian Osman71a18892017-08-10 10:23:25 -04001203
1204 // Flush JSON to the memory stream
1205 writer.endObject();
1206 writer.flush();
1207
1208 // Null terminate the JSON data in the memory stream
1209 stream.write8(0);
1210
1211 // Allocate a string big enough to hold all the data, then copy out of the stream
1212 SkString result(stream.bytesWritten());
1213 stream.copyToAndReset(result.writable_str());
1214 return result;
1215}
Kevin Lubickf4def342018-10-04 12:52:50 -04001216#endif