blob: d962bbd8d25217d643cd705a53ae8f85d779bf07 [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"
Khushal71652e22018-10-29 13:05:36 -07009#include <unordered_map>
Brian Salomon5f33a8c2018-02-26 14:32:39 -050010#include "GrBackendSemaphore.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050011#include "GrClip.h"
bsalomon682c2692015-05-22 14:01:46 -070012#include "GrContextOptions.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050013#include "GrContextPriv.h"
robertphillips77a2e522015-10-17 07:43:27 -070014#include "GrDrawingManager.h"
Robert Phillips646e4292017-06-13 12:44:56 -040015#include "GrGpu.h"
Robert Phillipsc994a932018-06-19 13:09:54 -040016#include "GrMemoryPool.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050017#include "GrProxyProvider.h"
Brian Osman11052242016-10-27 14:47:55 -040018#include "GrRenderTargetContext.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050019#include "GrRenderTargetProxy.h"
bsalomon0ea80f42015-02-11 10:49:59 -080020#include "GrResourceCache.h"
bsalomond309e7a2015-04-30 14:18:54 -070021#include "GrResourceProvider.h"
Greg Danield85f97d2017-03-07 13:37:21 -050022#include "GrSemaphore.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000023#include "GrSoftwarePathRenderer.h"
Brian Osman45580d32016-11-23 09:37:01 -050024#include "GrSurfaceContext.h"
bsalomonafbf2d62014-09-30 12:18:44 -070025#include "GrSurfacePriv.h"
Robert Phillips757914d2017-01-25 15:48:30 -050026#include "GrSurfaceProxyPriv.h"
Robert Phillips646e4292017-06-13 12:44:56 -040027#include "GrTexture.h"
Brian Osman45580d32016-11-23 09:37:01 -050028#include "GrTextureContext.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040029#include "GrTracing.h"
Brian Salomon19eaf2d2018-03-19 16:06:44 -040030#include "SkAutoPixmapStorage.h"
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -050031#include "SkDeferredDisplayList.h"
Brian Osman3b655982017-03-07 16:58:08 -050032#include "SkGr.h"
Mike Reed7fcfb622018-02-09 13:26:46 -050033#include "SkImageInfoPriv.h"
Brian Osman51279982017-08-23 10:12:00 -040034#include "SkMakeUnique.h"
Robert Phillips6b6fcc72018-03-30 13:57:00 -040035#include "SkSurface_Gpu.h"
Brian Osman51279982017-08-23 10:12:00 -040036#include "SkTaskGroup.h"
Khushal71652e22018-10-29 13:05:36 -070037#include "SkTraceMemoryDump.h"
joshualitt5478d422014-11-14 16:00:38 -080038#include "effects/GrConfigConversionEffect.h"
Ethan Nicholas00543112018-07-31 09:44:36 -040039#include "effects/GrSkSLFP.h"
Chris Dalton6c3879d2018-11-01 11:13:19 -060040#include "ccpr/GrCoverageCountingPathRenderer.h"
Brian Salomon5f33a8c2018-02-26 14:32:39 -050041#include "text/GrTextBlobCache.h"
Greg Danielb76a72a2017-07-13 15:07:54 -040042
Robert Phillipse78b7252017-04-06 07:59:41 -040043#define ASSERT_OWNED_PROXY(P) \
Brian Salomonfd98c2c2018-07-31 17:25:29 -040044 SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == this)
Robert Phillips7ee385e2017-03-30 08:02:11 -040045#define ASSERT_OWNED_PROXY_PRIV(P) \
Brian Salomonfd98c2c2018-07-31 17:25:29 -040046 SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == fContext)
Robert Phillips7ee385e2017-03-30 08:02:11 -040047
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000048#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt1de610a2016-01-06 08:26:09 -080049#define ASSERT_SINGLE_OWNER \
50 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fSingleOwner);)
robertphillips4fd74ae2016-08-03 14:26:53 -070051#define ASSERT_SINGLE_OWNER_PRIV \
52 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fContext->fSingleOwner);)
robertphillips7761d612016-05-16 09:14:53 -070053#define RETURN_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return; }
Robert Phillips7ee385e2017-03-30 08:02:11 -040054#define RETURN_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return; }
robertphillips7761d612016-05-16 09:14:53 -070055#define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return false; }
Robert Phillipse78b7252017-04-06 07:59:41 -040056#define RETURN_FALSE_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return false; }
robertphillips7761d612016-05-16 09:14:53 -070057#define RETURN_NULL_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000058
robertphillipsea461502015-05-26 11:38:03 -070059////////////////////////////////////////////////////////////////////////////////
60
joshualitt0acd0d32015-05-07 08:23:19 -070061static int32_t gNextID = 1;
62static int32_t next_id() {
63 int32_t id;
64 do {
65 id = sk_atomic_inc(&gNextID);
66 } while (id == SK_InvalidGenID);
67 return id;
68}
69
Greg Danielbdf12ad2018-10-12 09:31:11 -040070GrContext::GrContext(GrBackendApi backend, int32_t id)
Robert Phillipsfde6fa02018-03-02 08:53:14 -050071 : fBackend(backend)
72 , fUniqueID(SK_InvalidGenID == id ? next_id() : id) {
halcanary96fcdcc2015-08-27 07:41:13 -070073 fResourceCache = nullptr;
74 fResourceProvider = nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050075 fProxyProvider = nullptr;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050076 fGlyphCache = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000077}
78
Robert Phillipsfde6fa02018-03-02 08:53:14 -050079bool GrContext::initCommon(const GrContextOptions& options) {
Greg Danielb76a72a2017-07-13 15:07:54 -040080 ASSERT_SINGLE_OWNER
Robert Phillipsfde6fa02018-03-02 08:53:14 -050081 SkASSERT(fCaps); // needs to have been initialized by derived classes
82 SkASSERT(fThreadSafeProxy); // needs to have been initialized by derived classes
Robert Phillips88260b52018-01-19 12:56:09 -050083
84 if (fGpu) {
85 fCaps = fGpu->refCaps();
Brian Salomon8f8995a2018-10-15 14:32:15 -040086 fResourceCache = new GrResourceCache(fCaps.get(), &fSingleOwner, fUniqueID);
Robert Phillips4150eea2018-02-07 17:08:21 -050087 fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, &fSingleOwner,
88 options.fExplicitlyAllocateGPUResources);
Brian Salomon238069b2018-07-11 15:58:57 -040089 fProxyProvider =
90 new GrProxyProvider(fResourceProvider, fResourceCache, fCaps, &fSingleOwner);
91 } else {
92 fProxyProvider = new GrProxyProvider(this->uniqueID(), fCaps, &fSingleOwner);
Robert Phillips88260b52018-01-19 12:56:09 -050093 }
94
Robert Phillips88260b52018-01-19 12:56:09 -050095 if (fResourceCache) {
96 fResourceCache->setProxyProvider(fProxyProvider);
97 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050098
Brian Osman46da1cc2017-02-14 14:15:48 -050099 fDisableGpuYUVConversion = options.fDisableGpuYUVConversion;
Brian Osman8a83ca42018-02-12 14:32:17 -0500100 fSharpenMipmappedTextures = options.fSharpenMipmappedTextures;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000101 fDidTestPMConversions = false;
102
bsalomon6b2552f2016-09-15 13:50:26 -0700103 GrPathRendererChain::Options prcOptions;
bsalomon39ef7fb2016-09-21 11:16:05 -0700104 prcOptions.fAllowPathMaskCaching = options.fAllowPathMaskCaching;
Brian Osman195c05b2017-08-30 15:14:04 -0400105#if GR_TEST_UTILS
csmartdalton008b9d82017-02-22 12:00:42 -0700106 prcOptions.fGpuPathRenderers = options.fGpuPathRenderers;
Brian Osman195c05b2017-08-30 15:14:04 -0400107#endif
Chris Daltonef125092018-06-26 18:16:47 -0600108 if (options.fDisableCoverageCountingPaths) {
109 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kCoverageCounting;
110 }
Brian Osmanb350ae22017-08-29 16:15:39 -0400111 if (options.fDisableDistanceFieldPaths) {
Brian Osman195c05b2017-08-30 15:14:04 -0400112 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
Brian Osmanb350ae22017-08-29 16:15:39 -0400113 }
Brian Salomonaf597482017-11-07 16:23:34 -0500114
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500115 if (!fResourceCache) {
116 // DDL TODO: remove this crippling of the path renderer chain
117 // Disable the small path renderer bc of the proxies in the atlas. They need to be
118 // unified when the opLists are added back to the destination drawing manager.
119 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
Robert Phillips5ffcb4d2018-07-03 16:39:40 -0400120 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kStencilAndCover;
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500121 }
122
Herb Derby26cbe512018-05-24 14:39:01 -0400123 GrTextContext::Options textContextOptions;
124 textContextOptions.fMaxDistanceFieldFontSize = options.fGlyphsAsPathsFontSize;
125 textContextOptions.fMinDistanceFieldFontSize = options.fMinDistanceFieldFontSize;
126 textContextOptions.fDistanceFieldVerticesAlwaysHaveW = false;
Brian Salomonb5086962017-12-13 10:59:33 -0500127#if SK_SUPPORT_ATLAS_TEXT
128 if (GrContextOptions::Enable::kYes == options.fDistanceFieldGlyphVerticesAlwaysHaveW) {
Herb Derby26cbe512018-05-24 14:39:01 -0400129 textContextOptions.fDistanceFieldVerticesAlwaysHaveW = true;
Brian Salomonb5086962017-12-13 10:59:33 -0500130 }
131#endif
Brian Salomonaf597482017-11-07 16:23:34 -0500132
Robert Phillips64ecdce2018-04-02 10:26:39 -0400133 bool explicitlyAllocatingResources = fResourceProvider
134 ? fResourceProvider->explicitlyAllocateGPUResources()
135 : false;
Herb Derby26cbe512018-05-24 14:39:01 -0400136 fDrawingManager.reset(new GrDrawingManager(this, prcOptions, textContextOptions,
Robert Phillips64ecdce2018-04-02 10:26:39 -0400137 &fSingleOwner, explicitlyAllocatingResources,
Robert Phillips46acf9d2018-10-09 09:31:40 -0400138 options.fSortRenderTargets,
139 options.fReduceOpListSplitting));
joshualitt7c3a2f82015-03-31 13:32:05 -0700140
Robert Phillipsaf4adef2018-03-07 11:59:37 -0500141 fGlyphCache = new GrGlyphCache(fCaps.get(), options.fGlyphCacheTextureMaximumBytes);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500142
Robert Phillips303cd582018-02-14 18:54:01 -0500143 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB,
Herb Derbyb12175f2018-05-23 16:38:09 -0400144 this, this->uniqueID()));
Brian Salomon91a3e522017-06-23 10:58:19 -0400145
Robert Phillipsfde6fa02018-03-02 08:53:14 -0500146 // DDL TODO: we need to think through how the task group & persistent cache
147 // get passed on to/shared between all the DDLRecorders created with this context.
Brian Osman51279982017-08-23 10:12:00 -0400148 if (options.fExecutor) {
149 fTaskGroup = skstd::make_unique<SkTaskGroup>(*options.fExecutor);
150 }
151
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400152 fPersistentCache = options.fPersistentCache;
153
Brian Salomon91a3e522017-06-23 10:58:19 -0400154 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000155}
156
bsalomon@google.com27847de2011-02-22 20:59:41 +0000157GrContext::~GrContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800158 ASSERT_SINGLE_OWNER
159
Robert Phillips2e6feed2018-01-22 15:27:20 -0500160 if (fDrawingManager) {
161 fDrawingManager->cleanup();
162 }
halcanary385fe4d2015-08-26 13:07:48 -0700163 delete fResourceProvider;
164 delete fResourceCache;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500165 delete fProxyProvider;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500166 delete fGlyphCache;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000167}
168
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400169//////////////////////////////////////////////////////////////////////////////
170
171GrContextThreadSafeProxy::GrContextThreadSafeProxy(sk_sp<const GrCaps> caps, uint32_t uniqueID,
Greg Danielbdf12ad2018-10-12 09:31:11 -0400172 GrBackendApi backend,
Ethan Nicholas00543112018-07-31 09:44:36 -0400173 const GrContextOptions& options,
174 sk_sp<GrSkSLFPFactoryCache> cache)
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400175 : fCaps(std::move(caps))
176 , fContextUniqueID(uniqueID)
177 , fBackend(backend)
Ethan Nicholas00543112018-07-31 09:44:36 -0400178 , fOptions(options)
179 , fFPFactoryCache(std::move(cache)) {}
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400180
Brian Salomon103d6f62018-05-11 11:19:22 -0400181GrContextThreadSafeProxy::~GrContextThreadSafeProxy() = default;
182
bungeman6bd52842016-10-27 09:30:08 -0700183sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
bungeman6bd52842016-10-27 09:30:08 -0700184 return fThreadSafeProxy;
bsalomon41b952c2016-03-11 06:46:33 -0800185}
186
Robert Phillipsfc711a22018-02-13 17:03:00 -0500187SkSurfaceCharacterization GrContextThreadSafeProxy::createCharacterization(
188 size_t cacheMaxResourceBytes,
189 const SkImageInfo& ii, const GrBackendFormat& backendFormat,
190 int sampleCnt, GrSurfaceOrigin origin,
191 const SkSurfaceProps& surfaceProps,
Greg Daniela070ed72018-04-26 16:31:38 -0400192 bool isMipMapped, bool willUseGLFBO0) {
Robert Phillipsfc711a22018-02-13 17:03:00 -0500193 if (!backendFormat.isValid()) {
194 return SkSurfaceCharacterization(); // return an invalid characterization
195 }
196
Greg Danielbdf12ad2018-10-12 09:31:11 -0400197 if (GrBackendApi::kOpenGL != backendFormat.backend() && willUseGLFBO0) {
Greg Daniela070ed72018-04-26 16:31:38 -0400198 // The willUseGLFBO0 flags can only be used for a GL backend.
199 return SkSurfaceCharacterization(); // return an invalid characterization
200 }
201
Robert Phillipsfc711a22018-02-13 17:03:00 -0500202 if (!fCaps->mipMapSupport()) {
203 isMipMapped = false;
204 }
205
206 GrPixelConfig config = kUnknown_GrPixelConfig;
207 if (!fCaps->getConfigFromBackendFormat(backendFormat, ii.colorType(), &config)) {
208 return SkSurfaceCharacterization(); // return an invalid characterization
209 }
210
Robert Phillipsbe77a022018-04-03 17:17:05 -0400211 if (!SkSurface_Gpu::Valid(fCaps.get(), config, ii.colorSpace())) {
212 return SkSurfaceCharacterization(); // return an invalid characterization
213 }
214
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400215 sampleCnt = fCaps->getRenderTargetSampleCount(sampleCnt, config);
216 if (!sampleCnt) {
217 return SkSurfaceCharacterization(); // return an invalid characterization
218 }
219
220 GrFSAAType FSAAType = GrFSAAType::kNone;
221 if (sampleCnt > 1) {
222 FSAAType = fCaps->usesMixedSamples() ? GrFSAAType::kMixedSamples : GrFSAAType::kUnifiedMSAA;
223 }
224
Robert Phillipsfc711a22018-02-13 17:03:00 -0500225 // This surface characterization factory assumes that the resulting characterization is
226 // textureable.
227 if (!fCaps->isConfigTexturable(config)) {
228 return SkSurfaceCharacterization(); // return an invalid characterization
229 }
230
231 return SkSurfaceCharacterization(sk_ref_sp<GrContextThreadSafeProxy>(this),
Robert Phillipsbe77a022018-04-03 17:17:05 -0400232 cacheMaxResourceBytes, ii,
233 origin, config, FSAAType, sampleCnt,
Robert Phillipsfc711a22018-02-13 17:03:00 -0500234 SkSurfaceCharacterization::Textureable(true),
235 SkSurfaceCharacterization::MipMapped(isMipMapped),
Greg Daniela070ed72018-04-26 16:31:38 -0400236 SkSurfaceCharacterization::UsesGLFBO0(willUseGLFBO0),
Robert Phillipsbe77a022018-04-03 17:17:05 -0400237 surfaceProps);
Robert Phillipsfc711a22018-02-13 17:03:00 -0500238}
239
bsalomon2354f842014-07-28 13:48:36 -0700240void GrContext::abandonContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800241 ASSERT_SINGLE_OWNER
242
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500243 fProxyProvider->abandon();
bsalomond309e7a2015-04-30 14:18:54 -0700244 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800245
246 // Need to abandon the drawing manager first so all the render targets
247 // will be released/forgotten before they too are abandoned.
248 fDrawingManager->abandon();
249
bsalomon@google.com205d4602011-04-25 12:43:45 +0000250 // abandon first to so destructors
251 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800252 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700253
bsalomon6e2aad42016-04-01 11:54:31 -0700254 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
255
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500256 fGlyphCache->freeAll();
bsalomon6e2aad42016-04-01 11:54:31 -0700257 fTextBlobCache->freeAll();
258}
259
Khushalc421ca12018-06-26 14:38:34 -0700260bool GrContext::abandoned() const {
261 ASSERT_SINGLE_OWNER
262 return fDrawingManager->wasAbandoned();
263}
264
bsalomon6e2aad42016-04-01 11:54:31 -0700265void GrContext::releaseResourcesAndAbandonContext() {
266 ASSERT_SINGLE_OWNER
267
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500268 fProxyProvider->abandon();
bsalomon6e2aad42016-04-01 11:54:31 -0700269 fResourceProvider->abandon();
270
271 // Need to abandon the drawing manager first so all the render targets
272 // will be released/forgotten before they too are abandoned.
273 fDrawingManager->abandon();
274
275 // Release all resources in the backend 3D API.
276 fResourceCache->releaseAll();
277
278 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000279
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500280 fGlyphCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700281 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000282}
283
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000284void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800285 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000286 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000287}
288
289void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800290 ASSERT_SINGLE_OWNER
291
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500292 fGlyphCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700293
294 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700295
296 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000297}
298
Robert Phillips6eba0632018-03-28 12:25:42 -0400299void GrContext::purgeUnlockedResources(bool scratchResourcesOnly) {
300 ASSERT_SINGLE_OWNER
301 fResourceCache->purgeUnlockedResources(scratchResourcesOnly);
302 fResourceCache->purgeAsNeeded();
303 fTextBlobCache->purgeStaleBlobs();
304}
305
Jim Van Verth76d917c2017-12-13 09:26:37 -0500306void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Brian Salomon5e150852017-03-22 14:53:13 -0400307 ASSERT_SINGLE_OWNER
Chris Dalton6c3879d2018-11-01 11:13:19 -0600308
309 auto purgeTime = GrStdSteadyClock::now() - msNotUsed;
310
Jim Van Verth76d917c2017-12-13 09:26:37 -0500311 fResourceCache->purgeAsNeeded();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600312 fResourceCache->purgeResourcesNotUsedSince(purgeTime);
313
314 if (auto ccpr = fDrawingManager->getCoverageCountingPathRenderer()) {
315 ccpr->purgeCacheEntriesOlderThan(purgeTime);
316 }
Jim Van Verth76d917c2017-12-13 09:26:37 -0500317
318 fTextBlobCache->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400319}
320
Derek Sollenberger5480a182017-05-25 16:43:59 -0400321void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
322 ASSERT_SINGLE_OWNER
323 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
324}
325
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000326void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800327 ASSERT_SINGLE_OWNER
328
bsalomon71cb0c22014-11-14 12:10:14 -0800329 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800330 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800331 }
332 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800333 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800334 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000335}
336
Derek Sollenbergeree479142017-05-24 11:41:33 -0400337size_t GrContext::getResourceCachePurgeableBytes() const {
338 ASSERT_SINGLE_OWNER
339 return fResourceCache->getPurgeableBytes();
340}
341
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000342////////////////////////////////////////////////////////////////////////////////
343
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400344int GrContext::maxTextureSize() const { return fCaps->maxTextureSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400345
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400346int GrContext::maxRenderTargetSize() const { return fCaps->maxRenderTargetSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400347
Brian Salomonbdecacf2018-02-02 20:32:49 -0500348bool GrContext::colorTypeSupportedAsImage(SkColorType colorType) const {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400349 GrPixelConfig config = SkColorType2GrPixelConfig(colorType);
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400350 return fCaps->isConfigTexturable(config);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500351}
352
353int GrContext::maxSurfaceSampleCountForColorType(SkColorType colorType) const {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400354 GrPixelConfig config = SkColorType2GrPixelConfig(colorType);
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400355 return fCaps->maxRenderTargetSampleCount(config);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500356}
357
358////////////////////////////////////////////////////////////////////////////////
359
joshualitt0db6dfa2015-04-10 07:01:30 -0700360void GrContext::TextBlobCacheOverBudgetCB(void* data) {
361 SkASSERT(data);
Brian Osman11052242016-10-27 14:47:55 -0400362 // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on
363 // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls
364 // to below the GrContext level, but this is not trivial because they call drawPath on
365 // SkGpuDevice.
joshualitt0db6dfa2015-04-10 07:01:30 -0700366 GrContext* context = reinterpret_cast<GrContext*>(data);
367 context->flush();
368}
369
bsalomon@google.com27847de2011-02-22 20:59:41 +0000370////////////////////////////////////////////////////////////////////////////////
371
bsalomonb77a9072016-09-07 10:02:04 -0700372void GrContext::flush() {
joshualitt1de610a2016-01-06 08:26:09 -0800373 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700374 RETURN_IF_ABANDONED
Robert Phillips7ee385e2017-03-30 08:02:11 -0400375
376 fDrawingManager->flush(nullptr);
377}
378
Greg Daniel51316782017-08-02 15:10:09 +0000379GrSemaphoresSubmitted GrContext::flushAndSignalSemaphores(int numSemaphores,
380 GrBackendSemaphore signalSemaphores[]) {
381 ASSERT_SINGLE_OWNER
382 if (fDrawingManager->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; }
383
384 return fDrawingManager->flush(nullptr, numSemaphores, signalSemaphores);
385}
386
Robert Phillips7ee385e2017-03-30 08:02:11 -0400387void GrContextPriv::flush(GrSurfaceProxy* proxy) {
388 ASSERT_SINGLE_OWNER_PRIV
389 RETURN_IF_ABANDONED_PRIV
390 ASSERT_OWNED_PROXY_PRIV(proxy);
391
392 fContext->fDrawingManager->flush(proxy);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000393}
394
Brian Salomonc320b152018-02-20 14:05:36 -0500395// TODO: This will be removed when GrSurfaceContexts are aware of their color types.
396// (skbug.com/6718)
Brian Osmand2ca59a2017-04-13 14:03:57 -0400397static bool valid_premul_config(GrPixelConfig config) {
Brian Salomonc320b152018-02-20 14:05:36 -0500398 switch (config) {
399 case kUnknown_GrPixelConfig: return false;
400 case kAlpha_8_GrPixelConfig: return false;
401 case kGray_8_GrPixelConfig: return false;
402 case kRGB_565_GrPixelConfig: return false;
403 case kRGBA_4444_GrPixelConfig: return true;
404 case kRGBA_8888_GrPixelConfig: return true;
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400405 case kRGB_888_GrPixelConfig: return false;
Brian Salomonc320b152018-02-20 14:05:36 -0500406 case kBGRA_8888_GrPixelConfig: return true;
407 case kSRGBA_8888_GrPixelConfig: return true;
408 case kSBGRA_8888_GrPixelConfig: return true;
Brian Osman10fc6fd2018-03-02 11:01:10 -0500409 case kRGBA_1010102_GrPixelConfig: return true;
Brian Salomonc320b152018-02-20 14:05:36 -0500410 case kRGBA_float_GrPixelConfig: return true;
411 case kRG_float_GrPixelConfig: return false;
412 case kAlpha_half_GrPixelConfig: return false;
413 case kRGBA_half_GrPixelConfig: return true;
414 case kAlpha_8_as_Alpha_GrPixelConfig: return false;
415 case kAlpha_8_as_Red_GrPixelConfig: return false;
416 case kAlpha_half_as_Red_GrPixelConfig: return false;
417 case kGray_8_as_Lum_GrPixelConfig: return false;
418 case kGray_8_as_Red_GrPixelConfig: return false;
419 }
420 SK_ABORT("Invalid GrPixelConfig");
421 return false;
Brian Osmance425512017-03-22 14:37:50 -0400422}
423
Brian Salomonc320b152018-02-20 14:05:36 -0500424static bool valid_premul_color_type(GrColorType ct) {
425 switch (ct) {
Brian Osman10fc6fd2018-03-02 11:01:10 -0500426 case GrColorType::kUnknown: return false;
427 case GrColorType::kAlpha_8: return false;
428 case GrColorType::kRGB_565: return false;
429 case GrColorType::kABGR_4444: return true;
430 case GrColorType::kRGBA_8888: return true;
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400431 case GrColorType::kRGB_888x: return false;
Brian Osman10fc6fd2018-03-02 11:01:10 -0500432 case GrColorType::kBGRA_8888: return true;
433 case GrColorType::kRGBA_1010102: return true;
434 case GrColorType::kGray_8: return false;
435 case GrColorType::kAlpha_F16: return false;
436 case GrColorType::kRGBA_F16: return true;
437 case GrColorType::kRG_F32: return false;
438 case GrColorType::kRGBA_F32: return true;
Brian Salomonc320b152018-02-20 14:05:36 -0500439 }
440 SK_ABORT("Invalid GrColorType");
441 return false;
442}
443
444static bool valid_pixel_conversion(GrColorType cpuColorType, GrPixelConfig gpuConfig,
Brian Osmand2ca59a2017-04-13 14:03:57 -0400445 bool premulConversion) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400446 // We only allow premul <-> unpremul conversions for some formats
Brian Salomonc320b152018-02-20 14:05:36 -0500447 if (premulConversion &&
448 (!valid_premul_color_type(cpuColorType) || !valid_premul_config(gpuConfig))) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400449 return false;
450 }
Brian Osmand2ca59a2017-04-13 14:03:57 -0400451 return true;
452}
453
Brian Salomonc320b152018-02-20 14:05:36 -0500454bool GrContextPriv::writeSurfacePixels(GrSurfaceContext* dst, int left, int top, int width,
455 int height, GrColorType srcColorType,
456 SkColorSpace* srcColorSpace, const void* buffer,
457 size_t rowBytes, uint32_t pixelOpsFlags) {
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500458 ASSERT_SINGLE_OWNER_PRIV
459 RETURN_FALSE_IF_ABANDONED_PRIV
460 SkASSERT(dst);
461 SkASSERT(buffer);
462 ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy());
Brian Salomond494f6e2018-06-01 14:13:43 -0400463 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels", fContext);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500464
465 if (GrColorType::kUnknown == srcColorType) {
466 return false;
467 }
468
469 if (!dst->asSurfaceProxy()->instantiate(this->resourceProvider())) {
470 return false;
471 }
472
473 GrSurfaceProxy* dstProxy = dst->asSurfaceProxy();
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400474 GrSurface* dstSurface = dstProxy->peekSurface();
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500475
476 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
477 GrColorTypeBytesPerPixel(srcColorType), &left, &top,
478 &width, &height, &buffer, &rowBytes)) {
479 return false;
480 }
481
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400482 // TODO: Make GrSurfaceContext know its alpha type and pass src buffer's alpha type.
483 bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
484
485 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
486 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon817847c2018-05-07 13:33:50 -0400487 bool canvas2DFastPath =
Greg Daniel4374e962018-09-28 15:09:47 -0400488 !fContext->contextPriv().caps()->avoidWritePixelsFastPath() &&
Brian Salomon817847c2018-05-07 13:33:50 -0400489 premul &&
Brian Osman34ec3742018-07-03 10:40:57 -0400490 !dst->colorSpaceInfo().colorSpace() &&
Brian Salomon817847c2018-05-07 13:33:50 -0400491 (srcColorType == GrColorType::kRGBA_8888 || srcColorType == GrColorType::kBGRA_8888) &&
492 SkToBool(dst->asRenderTargetContext()) &&
493 (dstProxy->config() == kRGBA_8888_GrPixelConfig ||
494 dstProxy->config() == kBGRA_8888_GrPixelConfig) &&
495 !(pixelOpsFlags & kDontFlush_PixelOpsFlag) &&
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400496 fContext->contextPriv().caps()->isConfigTexturable(kRGBA_8888_GrPixelConfig) &&
Brian Salomon817847c2018-05-07 13:33:50 -0400497 fContext->validPMUPMConversionExists();
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400498
Greg Daniel4065d452018-11-16 15:43:41 -0500499 const GrCaps* caps = this->caps();
500 if (!caps->surfaceSupportsWritePixels(dstSurface) ||
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400501 canvas2DFastPath) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400502 // We don't expect callers that are skipping flushes to require an intermediate draw.
503 SkASSERT(!(pixelOpsFlags & kDontFlush_PixelOpsFlag));
504 if (pixelOpsFlags & kDontFlush_PixelOpsFlag) {
505 return false;
506 }
507
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500508 GrSurfaceDesc desc;
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500509 desc.fWidth = width;
510 desc.fHeight = height;
511 desc.fSampleCnt = 1;
Greg Daniel4065d452018-11-16 15:43:41 -0500512
513 GrBackendFormat format;
514 if (canvas2DFastPath) {
515 desc.fConfig = kRGBA_8888_GrPixelConfig;
516 format =
517 fContext->contextPriv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
518 } else {
519 desc.fConfig = dstProxy->config();
520 format = dstProxy->backendFormat().makeTexture2D();
521 if (!format.isValid()) {
522 return false;
523 }
524 }
525
Brian Salomon2a4f9832018-03-03 22:43:43 -0500526 auto tempProxy = this->proxyProvider()->createProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500527 format, desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kApprox, SkBudgeted::kYes);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500528 if (!tempProxy) {
529 return false;
530 }
531 auto tempCtx = this->drawingManager()->makeTextureContext(
532 tempProxy, dst->colorSpaceInfo().refColorSpace());
533 if (!tempCtx) {
534 return false;
535 }
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400536 uint32_t flags = canvas2DFastPath ? 0 : pixelOpsFlags;
Brian Salomon817847c2018-05-07 13:33:50 -0400537 // In the fast path we always write the srcData to the temp context as though it were RGBA.
538 // When the data is really BGRA the write will cause the R and B channels to be swapped in
539 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
540 // dst.
541 auto tmpColorType = canvas2DFastPath ? GrColorType::kRGBA_8888 : srcColorType;
Brian Salomond494f6e2018-06-01 14:13:43 -0400542 if (!this->writeSurfacePixels(tempCtx.get(), 0, 0, width, height, tmpColorType,
543 srcColorSpace, buffer, rowBytes, flags)) {
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500544 return false;
545 }
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400546 if (canvas2DFastPath) {
547 GrPaint paint;
548 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomon817847c2018-05-07 13:33:50 -0400549 auto fp = fContext->createUPMToPMEffect(
Brian Osman5ea96bf2018-10-02 14:58:05 -0400550 GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I()));
Brian Salomon817847c2018-05-07 13:33:50 -0400551 if (srcColorType == GrColorType::kBGRA_8888) {
552 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
553 }
554 if (!fp) {
555 return false;
556 }
557 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400558 dst->asRenderTargetContext()->fillRectToRect(
559 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
560 SkRect::MakeXYWH(left, top, width, height), SkRect::MakeWH(width, height));
561 return true;
562 } else {
563 return dst->copy(tempProxy.get(), SkIRect::MakeWH(width, height), {left, top});
564 }
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500565 }
566
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500567 bool convert = premul;
568
569 if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) {
570 return false;
571 }
572
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400573 GrColorType allowedColorType = fContext->contextPriv().caps()->supportedWritePixelsColorType(
574 dstProxy->config(), srcColorType);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500575 convert = convert || (srcColorType != allowedColorType);
576
577 if (!dst->colorSpaceInfo().colorSpace()) {
578 // "Legacy" mode - no color space conversions.
579 srcColorSpace = nullptr;
580 }
581 convert = convert || !SkColorSpace::Equals(srcColorSpace, dst->colorSpaceInfo().colorSpace());
582
583 std::unique_ptr<char[]> tempBuffer;
584 if (convert) {
585 auto srcSkColorType = GrColorTypeToSkColorType(srcColorType);
586 auto dstSkColorType = GrColorTypeToSkColorType(allowedColorType);
587 if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) {
588 return false;
589 }
Brian Osman2091fbb2018-10-11 11:05:37 -0400590 auto srcAlphaType = SkColorTypeIsAlwaysOpaque(srcSkColorType)
591 ? kOpaque_SkAlphaType
592 : (premul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500593 SkPixmap src(SkImageInfo::Make(width, height, srcSkColorType, srcAlphaType,
594 sk_ref_sp(srcColorSpace)),
595 buffer, rowBytes);
596 auto tempSrcII = SkImageInfo::Make(width, height, dstSkColorType, kPremul_SkAlphaType,
597 dst->colorSpaceInfo().refColorSpace());
598 auto size = tempSrcII.computeMinByteSize();
599 if (!size) {
600 return false;
601 }
602 tempBuffer.reset(new char[size]);
603 SkPixmap tempSrc(tempSrcII, tempBuffer.get(), tempSrcII.minRowBytes());
604 if (!src.readPixels(tempSrc)) {
605 return false;
606 }
607 srcColorType = allowedColorType;
608 buffer = tempSrc.addr();
609 rowBytes = tempSrc.rowBytes();
610 if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
611 std::unique_ptr<char[]> row(new char[rowBytes]);
612 for (int y = 0; y < height / 2; ++y) {
613 memcpy(row.get(), tempSrc.addr(0, y), rowBytes);
614 memcpy(tempSrc.writable_addr(0, y), tempSrc.addr(0, height - 1 - y), rowBytes);
615 memcpy(tempSrc.writable_addr(0, height - 1 - y), row.get(), rowBytes);
616 }
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400617 top = dstSurface->height() - top - height;
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500618 }
619 } else if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
620 size_t trimRowBytes = GrColorTypeBytesPerPixel(srcColorType) * width;
621 tempBuffer.reset(new char[trimRowBytes * height]);
622 char* dst = reinterpret_cast<char*>(tempBuffer.get()) + trimRowBytes * (height - 1);
623 const char* src = reinterpret_cast<const char*>(buffer);
624 for (int i = 0; i < height; ++i, src += rowBytes, dst -= trimRowBytes) {
625 memcpy(dst, src, trimRowBytes);
626 }
627 buffer = tempBuffer.get();
628 rowBytes = trimRowBytes;
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400629 top = dstSurface->height() - top - height;
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500630 }
631
632 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) {
633 this->flush(nullptr); // MDB TODO: tighten this
634 }
635
636 return this->getGpu()->writePixels(dstSurface, left, top, width, height, srcColorType, buffer,
637 rowBytes);
638}
639
Brian Salomond494f6e2018-06-01 14:13:43 -0400640bool GrContextPriv::readSurfacePixels(GrSurfaceContext* src, int left, int top, int width,
641 int height, GrColorType dstColorType,
642 SkColorSpace* dstColorSpace, void* buffer, size_t rowBytes,
643 uint32_t pixelOpsFlags) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400644 ASSERT_SINGLE_OWNER_PRIV
645 RETURN_FALSE_IF_ABANDONED_PRIV
646 SkASSERT(src);
647 SkASSERT(buffer);
648 ASSERT_OWNED_PROXY_PRIV(src->asSurfaceProxy());
Brian Salomond494f6e2018-06-01 14:13:43 -0400649 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "readSurfacePixels", fContext);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400650
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400651 SkASSERT(!(pixelOpsFlags & kDontFlush_PixelOpsFlag));
652 if (pixelOpsFlags & kDontFlush_PixelOpsFlag) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400653 return false;
654 }
655
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400656 // MDB TODO: delay this instantiation until later in the method
657 if (!src->asSurfaceProxy()->instantiate(this->resourceProvider())) {
658 return false;
659 }
660
661 GrSurfaceProxy* srcProxy = src->asSurfaceProxy();
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400662 GrSurface* srcSurface = srcProxy->peekSurface();
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400663
664 if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(),
665 GrColorTypeBytesPerPixel(dstColorType), &left, &top,
666 &width, &height, &buffer, &rowBytes)) {
667 return false;
668 }
669
670 // TODO: Make GrSurfaceContext know its alpha type and pass dst buffer's alpha type.
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400671 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400672
673 if (!valid_pixel_conversion(dstColorType, srcProxy->config(), unpremul)) {
674 return false;
675 }
676
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400677 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
678 // care so much about getImageData performance. However, in order to ensure putImageData/
679 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
Brian Salomond494f6e2018-06-01 14:13:43 -0400680 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400681 // fContext->vaildaPMUPMConversionExists()).
Brian Salomon817847c2018-05-07 13:33:50 -0400682 bool canvas2DFastPath =
683 unpremul &&
Brian Osman34ec3742018-07-03 10:40:57 -0400684 !src->colorSpaceInfo().colorSpace() &&
Brian Salomon817847c2018-05-07 13:33:50 -0400685 (GrColorType::kRGBA_8888 == dstColorType || GrColorType::kBGRA_8888 == dstColorType) &&
686 SkToBool(srcProxy->asTextureProxy()) &&
687 (srcProxy->config() == kRGBA_8888_GrPixelConfig ||
688 srcProxy->config() == kBGRA_8888_GrPixelConfig) &&
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400689 fContext->contextPriv().caps()->isConfigRenderable(kRGBA_8888_GrPixelConfig) &&
Brian Salomon817847c2018-05-07 13:33:50 -0400690 fContext->validPMUPMConversionExists();
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400691
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400692 if (!fContext->contextPriv().caps()->surfaceSupportsReadPixels(srcSurface) ||
693 canvas2DFastPath) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400694 GrSurfaceDesc desc;
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400695 desc.fFlags = canvas2DFastPath ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
696 desc.fConfig = canvas2DFastPath ? kRGBA_8888_GrPixelConfig : srcProxy->config();
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400697 desc.fWidth = width;
698 desc.fHeight = height;
699 desc.fSampleCnt = 1;
Greg Daniel4065d452018-11-16 15:43:41 -0500700
701 GrBackendFormat format;
702 if (canvas2DFastPath) {
703 desc.fFlags = kRenderTarget_GrSurfaceFlag;
704 desc.fConfig = kRGBA_8888_GrPixelConfig;
705 format = this->caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
706 } else {
707 desc.fFlags = kNone_GrSurfaceFlags;
708 desc.fConfig = srcProxy->config();
709 format = srcProxy->backendFormat().makeTexture2D();
710 if (!format.isValid()) {
711 return false;
712 }
713 }
714
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400715 auto tempProxy = this->proxyProvider()->createProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500716 format, desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kApprox, SkBudgeted::kYes);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400717 if (!tempProxy) {
718 return false;
719 }
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400720 sk_sp<GrSurfaceContext> tempCtx;
721 if (canvas2DFastPath) {
722 tempCtx = this->drawingManager()->makeRenderTargetContext(std::move(tempProxy), nullptr,
723 nullptr);
Greg Daniel42314012018-04-23 10:57:37 -0400724 SkASSERT(tempCtx->asRenderTargetContext());
725 tempCtx->asRenderTargetContext()->discard();
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400726 } else {
727 tempCtx = this->drawingManager()->makeTextureContext(
728 std::move(tempProxy), src->colorSpaceInfo().refColorSpace());
729 }
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400730 if (!tempCtx) {
731 return false;
732 }
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400733 if (canvas2DFastPath) {
734 GrPaint paint;
735 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomon817847c2018-05-07 13:33:50 -0400736 auto fp = fContext->createPMToUPMEffect(
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400737 GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()),
Brian Osman5ea96bf2018-10-02 14:58:05 -0400738 SkMatrix::I()));
Brian Salomon817847c2018-05-07 13:33:50 -0400739 if (dstColorType == GrColorType::kBGRA_8888) {
740 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
741 dstColorType = GrColorType::kRGBA_8888;
742 }
743 if (!fp) {
744 return false;
745 }
746 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400747 tempCtx->asRenderTargetContext()->fillRectToRect(
748 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
749 SkRect::MakeWH(width, height), SkRect::MakeXYWH(left, top, width, height));
750 } else if (!tempCtx->copy(srcProxy, SkIRect::MakeXYWH(left, top, width, height), {0, 0})) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400751 return false;
752 }
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400753 uint32_t flags = canvas2DFastPath ? 0 : pixelOpsFlags;
Brian Salomond494f6e2018-06-01 14:13:43 -0400754 return this->readSurfacePixels(tempCtx.get(), 0, 0, width, height, dstColorType,
755 dstColorSpace, buffer, rowBytes, flags);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400756 }
757
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400758 bool convert = unpremul;
759
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400760 bool flip = srcProxy->origin() == kBottomLeft_GrSurfaceOrigin;
761 if (flip) {
762 top = srcSurface->height() - top - height;
763 }
764
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400765 GrColorType allowedColorType = fContext->contextPriv().caps()->supportedReadPixelsColorType(
766 srcProxy->config(), dstColorType);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400767 convert = convert || (dstColorType != allowedColorType);
768
769 if (!src->colorSpaceInfo().colorSpace()) {
770 // "Legacy" mode - no color space conversions.
771 dstColorSpace = nullptr;
772 }
773 convert = convert || !SkColorSpace::Equals(dstColorSpace, src->colorSpaceInfo().colorSpace());
774
775 SkAutoPixmapStorage tempPixmap;
776 SkPixmap finalPixmap;
777 if (convert) {
778 SkColorType srcSkColorType = GrColorTypeToSkColorType(allowedColorType);
779 SkColorType dstSkColorType = GrColorTypeToSkColorType(dstColorType);
Brian Osman2091fbb2018-10-11 11:05:37 -0400780 bool srcAlwaysOpaque = SkColorTypeIsAlwaysOpaque(srcSkColorType);
781 bool dstAlwaysOpaque = SkColorTypeIsAlwaysOpaque(dstSkColorType);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400782 if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) {
783 return false;
784 }
Brian Osman2091fbb2018-10-11 11:05:37 -0400785 auto tempAT = srcAlwaysOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400786 auto tempII = SkImageInfo::Make(width, height, srcSkColorType, tempAT,
787 src->colorSpaceInfo().refColorSpace());
Brian Osman2091fbb2018-10-11 11:05:37 -0400788 SkASSERT(!unpremul || !dstAlwaysOpaque);
789 auto finalAT = (srcAlwaysOpaque || dstAlwaysOpaque)
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400790 ? kOpaque_SkAlphaType
791 : unpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
792 auto finalII =
793 SkImageInfo::Make(width, height, dstSkColorType, finalAT, sk_ref_sp(dstColorSpace));
794 if (!SkImageInfoValidConversion(finalII, tempII)) {
795 return false;
796 }
Brian Salomon6196fc12018-07-12 16:39:23 -0400797 if (!tempPixmap.tryAlloc(tempII)) {
798 return false;
799 }
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400800 finalPixmap.reset(finalII, buffer, rowBytes);
801 buffer = tempPixmap.writable_addr();
802 rowBytes = tempPixmap.rowBytes();
Brian Salomon3d945e82018-05-14 14:54:06 -0400803 // Chrome msan bots require this.
804 sk_bzero(buffer, tempPixmap.computeByteSize());
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400805 }
806
807 if (srcSurface->surfacePriv().hasPendingWrite()) {
808 this->flush(nullptr); // MDB TODO: tighten this
809 }
810
811 if (!fContext->fGpu->readPixels(srcSurface, left, top, width, height, allowedColorType, buffer,
812 rowBytes)) {
813 return false;
814 }
815
816 if (flip) {
817 size_t trimRowBytes = GrColorTypeBytesPerPixel(allowedColorType) * width;
818 std::unique_ptr<char[]> row(new char[trimRowBytes]);
819 char* upper = reinterpret_cast<char*>(buffer);
820 char* lower = reinterpret_cast<char*>(buffer) + (height - 1) * rowBytes;
821 for (int y = 0; y < height / 2; ++y, upper += rowBytes, lower -= rowBytes) {
822 memcpy(row.get(), upper, trimRowBytes);
823 memcpy(upper, lower, trimRowBytes);
824 memcpy(lower, row.get(), trimRowBytes);
825 }
826 }
827 if (convert) {
828 if (!tempPixmap.readPixels(finalPixmap)) {
829 return false;
830 }
831 }
832 return true;
833}
834
Robert Phillips7ee385e2017-03-30 08:02:11 -0400835void GrContextPriv::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) {
836 ASSERT_SINGLE_OWNER_PRIV
837 RETURN_IF_ABANDONED_PRIV
838 SkASSERT(proxy);
839 ASSERT_OWNED_PROXY_PRIV(proxy);
Greg Daniel51316782017-08-02 15:10:09 +0000840 fContext->fDrawingManager->prepareSurfaceForExternalIO(proxy, 0, nullptr);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000841}
842
Robert Phillips7ee385e2017-03-30 08:02:11 -0400843void GrContextPriv::flushSurfaceWrites(GrSurfaceProxy* proxy) {
844 ASSERT_SINGLE_OWNER_PRIV
845 RETURN_IF_ABANDONED_PRIV
846 SkASSERT(proxy);
847 ASSERT_OWNED_PROXY_PRIV(proxy);
848 if (proxy->priv().hasPendingWrite()) {
849 this->flush(proxy);
bsalomonf80bfed2014-10-07 05:56:02 -0700850 }
851}
852
Robert Phillips7ee385e2017-03-30 08:02:11 -0400853void GrContextPriv::flushSurfaceIO(GrSurfaceProxy* proxy) {
854 ASSERT_SINGLE_OWNER_PRIV
855 RETURN_IF_ABANDONED_PRIV
856 SkASSERT(proxy);
857 ASSERT_OWNED_PROXY_PRIV(proxy);
858 if (proxy->priv().hasPendingIO()) {
859 this->flush(proxy);
ajuma95243eb2016-08-24 08:19:02 -0700860 }
861}
862
bsalomon@google.com27847de2011-02-22 20:59:41 +0000863////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000864
Robert Phillipsc994a932018-06-19 13:09:54 -0400865sk_sp<GrOpMemoryPool> GrContextPriv::refOpMemoryPool() {
866 if (!fContext->fOpMemoryPool) {
867 // DDL TODO: should the size of the memory pool be decreased in DDL mode? CPU-side memory
868 // consumed in DDL mode vs. normal mode for a single skp might be a good metric of wasted
869 // memory.
870 fContext->fOpMemoryPool = sk_sp<GrOpMemoryPool>(new GrOpMemoryPool(16384, 16384));
871 }
872
873 SkASSERT(fContext->fOpMemoryPool);
874 return fContext->fOpMemoryPool;
875}
876
877GrOpMemoryPool* GrContextPriv::opMemoryPool() {
878 return this->refOpMemoryPool().get();
879}
880
Robert Phillips2c862492017-01-18 10:08:39 -0500881sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy,
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500882 sk_sp<SkColorSpace> colorSpace,
883 const SkSurfaceProps* props) {
Brian Osman45580d32016-11-23 09:37:01 -0500884 ASSERT_SINGLE_OWNER_PRIV
885
Brian Osman45580d32016-11-23 09:37:01 -0500886 if (proxy->asRenderTargetProxy()) {
Robert Phillips2c862492017-01-18 10:08:39 -0500887 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500888 std::move(colorSpace), props);
Brian Osman45580d32016-11-23 09:37:01 -0500889 } else {
890 SkASSERT(proxy->asTextureProxy());
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500891 SkASSERT(!props);
Robert Phillips2c862492017-01-18 10:08:39 -0500892 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Brian Osman45580d32016-11-23 09:37:01 -0500893 }
894}
895
Greg Daniel4065d452018-11-16 15:43:41 -0500896sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrBackendFormat& format,
897 const GrSurfaceDesc& dstDesc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500898 GrSurfaceOrigin origin,
Greg Daniel65c7f662017-10-30 13:39:09 -0400899 GrMipMapped mipMapped,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500900 SkBackingFit fit,
Brian Salomon366093f2018-02-13 09:25:22 -0500901 SkBudgeted isDstBudgeted,
902 sk_sp<SkColorSpace> colorSpace,
903 const SkSurfaceProps* props) {
Greg Daniel65c7f662017-10-30 13:39:09 -0400904 sk_sp<GrTextureProxy> proxy;
905 if (GrMipMapped::kNo == mipMapped) {
Greg Daniel4065d452018-11-16 15:43:41 -0500906 proxy = this->proxyProvider()->createProxy(format, dstDesc, origin, fit, isDstBudgeted);
Greg Daniel65c7f662017-10-30 13:39:09 -0400907 } else {
908 SkASSERT(SkBackingFit::kExact == fit);
Greg Daniel4065d452018-11-16 15:43:41 -0500909 proxy = this->proxyProvider()->createMipMapProxy(format, dstDesc, origin, isDstBudgeted);
Greg Daniel65c7f662017-10-30 13:39:09 -0400910 }
Robert Phillips77b3f322017-01-31 18:24:12 -0500911 if (!proxy) {
912 return nullptr;
913 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500914
Greg Daniel55fa6472018-03-16 16:13:10 -0400915 sk_sp<GrSurfaceContext> sContext = this->makeWrappedSurfaceContext(std::move(proxy),
916 std::move(colorSpace),
917 props);
918 if (sContext && sContext->asRenderTargetContext()) {
919 sContext->asRenderTargetContext()->discard();
920 }
921
922 return sContext;
Robert Phillipse2f7d182016-12-15 09:23:05 -0500923}
924
Brian Salomond17f6582017-07-19 18:28:58 -0400925sk_sp<GrTextureContext> GrContextPriv::makeBackendTextureContext(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000926 GrSurfaceOrigin origin,
Brian Osmanc1e37052017-03-09 14:19:20 -0500927 sk_sp<SkColorSpace> colorSpace) {
Robert Phillips26caf892017-01-27 10:58:31 -0500928 ASSERT_SINGLE_OWNER_PRIV
929
Brian Salomon7578f3e2018-03-07 14:39:54 -0500930 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->wrapBackendTexture(tex, origin);
Robert Phillips77b3f322017-01-31 18:24:12 -0500931 if (!proxy) {
932 return nullptr;
933 }
Robert Phillips26caf892017-01-27 10:58:31 -0500934
Brian Salomond17f6582017-07-19 18:28:58 -0400935 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Robert Phillips26caf892017-01-27 10:58:31 -0500936}
937
Brian Osman11052242016-10-27 14:47:55 -0400938sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000939 const GrBackendTexture& tex,
940 GrSurfaceOrigin origin,
941 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -0400942 sk_sp<SkColorSpace> colorSpace,
Brian Osmanc1e37052017-03-09 14:19:20 -0500943 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700944 ASSERT_SINGLE_OWNER_PRIV
Brian Salomonbdecacf2018-02-02 20:32:49 -0500945 SkASSERT(sampleCnt > 0);
robertphillips4fd74ae2016-08-03 14:26:53 -0700946
Brian Salomon7578f3e2018-03-07 14:39:54 -0500947 sk_sp<GrTextureProxy> proxy(
948 this->proxyProvider()->wrapRenderableBackendTexture(tex, origin, sampleCnt));
Robert Phillips77b3f322017-01-31 18:24:12 -0500949 if (!proxy) {
950 return nullptr;
951 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400952
Robert Phillips37430132016-11-09 06:50:43 -0500953 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Brian Osman11052242016-10-27 14:47:55 -0400954 std::move(colorSpace), props);
robertphillips4fd74ae2016-08-03 14:26:53 -0700955}
956
Brian Osman11052242016-10-27 14:47:55 -0400957sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +0000958 const GrBackendRenderTarget& backendRT,
959 GrSurfaceOrigin origin,
robertphillips4fd74ae2016-08-03 14:26:53 -0700960 sk_sp<SkColorSpace> colorSpace,
961 const SkSurfaceProps* surfaceProps) {
962 ASSERT_SINGLE_OWNER_PRIV
963
Brian Salomon7578f3e2018-03-07 14:39:54 -0500964 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->wrapBackendRenderTarget(backendRT, origin);
Robert Phillips77b3f322017-01-31 18:24:12 -0500965 if (!proxy) {
966 return nullptr;
967 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400968
Robert Phillips37430132016-11-09 06:50:43 -0500969 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400970 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -0400971 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700972}
973
Brian Osman11052242016-10-27 14:47:55 -0400974sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000975 const GrBackendTexture& tex,
976 GrSurfaceOrigin origin,
977 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -0700978 sk_sp<SkColorSpace> colorSpace,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500979 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700980 ASSERT_SINGLE_OWNER_PRIV
Brian Salomonbdecacf2018-02-02 20:32:49 -0500981 SkASSERT(sampleCnt > 0);
Brian Salomon7578f3e2018-03-07 14:39:54 -0500982 sk_sp<GrSurfaceProxy> proxy(
983 this->proxyProvider()->wrapBackendTextureAsRenderTarget(tex, origin, sampleCnt));
Robert Phillips77b3f322017-01-31 18:24:12 -0500984 if (!proxy) {
985 return nullptr;
986 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400987
Robert Phillips37430132016-11-09 06:50:43 -0500988 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400989 std::move(colorSpace),
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500990 props);
robertphillips77a2e522015-10-17 07:43:27 -0700991}
992
Chris Daltonfe199b72017-05-05 11:26:15 -0400993void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
994 fContext->fDrawingManager->addOnFlushCallbackObject(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400995}
996
Robert Phillips62000362018-02-01 09:10:04 -0500997void GrContextPriv::moveOpListsToDDL(SkDeferredDisplayList* ddl) {
998 fContext->fDrawingManager->moveOpListsToDDL(ddl);
999}
1000
1001void GrContextPriv::copyOpListsFromDDL(const SkDeferredDisplayList* ddl,
1002 GrRenderTargetProxy* newDest) {
1003 fContext->fDrawingManager->copyOpListsFromDDL(ddl, newDest);
1004}
1005
robertphillips48fde9c2016-09-06 05:20:20 -07001006static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
Brian Osman78f20e02017-01-12 10:28:01 -05001007 switch (config) {
1008 case kAlpha_8_GrPixelConfig:
Greg Daniel93b4ddd2018-04-16 16:16:47 -04001009 case kAlpha_8_as_Alpha_GrPixelConfig:
1010 case kAlpha_8_as_Red_GrPixelConfig:
Brian Osman78f20e02017-01-12 10:28:01 -05001011 case kRGB_565_GrPixelConfig:
1012 case kRGBA_4444_GrPixelConfig:
1013 case kBGRA_8888_GrPixelConfig:
Brian Osman10fc6fd2018-03-02 11:01:10 -05001014 case kRGBA_1010102_GrPixelConfig:
Brian Osman9c111352018-10-16 10:18:26 -04001015 case kRGBA_half_GrPixelConfig:
Brian Osman78f20e02017-01-12 10:28:01 -05001016 return kRGBA_8888_GrPixelConfig;
1017 case kSBGRA_8888_GrPixelConfig:
1018 return kSRGBA_8888_GrPixelConfig;
1019 case kAlpha_half_GrPixelConfig:
Greg Daniel93b4ddd2018-04-16 16:16:47 -04001020 case kAlpha_half_as_Red_GrPixelConfig:
Brian Osman78f20e02017-01-12 10:28:01 -05001021 return kRGBA_half_GrPixelConfig;
Greg Daniel93b4ddd2018-04-16 16:16:47 -04001022 case kGray_8_GrPixelConfig:
1023 case kGray_8_as_Lum_GrPixelConfig:
1024 case kGray_8_as_Red_GrPixelConfig:
1025 return kRGB_888_GrPixelConfig;
Brian Osman78f20e02017-01-12 10:28:01 -05001026 default:
1027 return kUnknown_GrPixelConfig;
1028 }
robertphillips48fde9c2016-09-06 05:20:20 -07001029}
1030
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001031sk_sp<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContextWithFallback(
Greg Daniel4065d452018-11-16 15:43:41 -05001032 const GrBackendFormat& format,
robertphillipsd728f0c2016-11-21 11:05:03 -08001033 SkBackingFit fit,
1034 int width, int height,
1035 GrPixelConfig config,
1036 sk_sp<SkColorSpace> colorSpace,
1037 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -04001038 GrMipMapped mipMapped,
robertphillipsd728f0c2016-11-21 11:05:03 -08001039 GrSurfaceOrigin origin,
1040 const SkSurfaceProps* surfaceProps,
1041 SkBudgeted budgeted) {
Greg Daniel4065d452018-11-16 15:43:41 -05001042 GrBackendFormat localFormat = format;
Brian Salomonbdecacf2018-02-02 20:32:49 -05001043 SkASSERT(sampleCnt > 0);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001044 if (0 == fContext->contextPriv().caps()->getRenderTargetSampleCount(sampleCnt, config)) {
robertphillipsd728f0c2016-11-21 11:05:03 -08001045 config = GrPixelConfigFallback(config);
Greg Daniel4065d452018-11-16 15:43:41 -05001046 // TODO: First we should be checking the getRenderTargetSampleCount from the GrBackendFormat
1047 // and not GrPixelConfig. Besides that, we should implement the fallback in the caps, but
1048 // for now we just convert the fallback pixel config to an SkColorType and then get the
1049 // GrBackendFormat from that.
1050 SkColorType colorType;
1051 if (!GrPixelConfigToColorType(config, &colorType)) {
1052 return nullptr;
1053 }
1054 localFormat = fContext->fCaps->getBackendFormatFromColorType(colorType);
robertphillipsd728f0c2016-11-21 11:05:03 -08001055 }
1056
Greg Daniel4065d452018-11-16 15:43:41 -05001057 return this->makeDeferredRenderTargetContext(localFormat, fit, width, height, config,
1058 std::move(colorSpace), sampleCnt, mipMapped,
1059 origin, surfaceProps, budgeted);
robertphillipsd728f0c2016-11-21 11:05:03 -08001060}
1061
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001062sk_sp<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -05001063 const GrBackendFormat& format,
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001064 SkBackingFit fit,
1065 int width, int height,
1066 GrPixelConfig config,
1067 sk_sp<SkColorSpace> colorSpace,
1068 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -04001069 GrMipMapped mipMapped,
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001070 GrSurfaceOrigin origin,
1071 const SkSurfaceProps* surfaceProps,
1072 SkBudgeted budgeted) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05001073 SkASSERT(sampleCnt > 0);
Khushalc421ca12018-06-26 14:38:34 -07001074 if (fContext->abandoned()) {
Brian Salomon79e4d1b2017-07-13 11:17:11 -04001075 return nullptr;
1076 }
1077
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001078 GrSurfaceDesc desc;
1079 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001080 desc.fWidth = width;
1081 desc.fHeight = height;
1082 desc.fConfig = config;
1083 desc.fSampleCnt = sampleCnt;
1084
Greg Daniele1da1d92017-10-06 15:59:27 -04001085 sk_sp<GrTextureProxy> rtp;
Greg Daniel45d63032017-10-30 13:41:26 -04001086 if (GrMipMapped::kNo == mipMapped) {
Greg Daniel4065d452018-11-16 15:43:41 -05001087 rtp = fContext->fProxyProvider->createProxy(format, desc, origin, fit, budgeted);
Greg Daniele1da1d92017-10-06 15:59:27 -04001088 } else {
Greg Daniel4065d452018-11-16 15:43:41 -05001089 rtp = fContext->fProxyProvider->createMipMapProxy(format, desc, origin, budgeted);
Greg Daniele1da1d92017-10-06 15:59:27 -04001090 }
Robert Phillips08c5ec72017-01-30 12:26:47 -05001091 if (!rtp) {
1092 return nullptr;
1093 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001094
Robert Phillips1119dc32017-04-11 12:54:57 -04001095 sk_sp<GrRenderTargetContext> renderTargetContext(
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001096 fContext->fDrawingManager->makeRenderTargetContext(std::move(rtp),
1097 std::move(colorSpace),
1098 surfaceProps));
Robert Phillips1119dc32017-04-11 12:54:57 -04001099 if (!renderTargetContext) {
1100 return nullptr;
1101 }
1102
1103 renderTargetContext->discard();
1104
1105 return renderTargetContext;
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001106}
1107
Brian Salomonaff329b2017-08-11 09:40:37 -04001108std::unique_ptr<GrFragmentProcessor> GrContext::createPMToUPMEffect(
Brian Osman5ea96bf2018-10-02 14:58:05 -04001109 std::unique_ptr<GrFragmentProcessor> fp) {
Robert Phillips757914d2017-01-25 15:48:30 -05001110 ASSERT_SINGLE_OWNER
Brian Osman5ea96bf2018-10-02 14:58:05 -04001111 // We should have already called this->validPMUPMConversionExists() in this case
1112 SkASSERT(fDidTestPMConversions);
1113 // ...and it should have succeeded
1114 SkASSERT(this->validPMUPMConversionExists());
Brian Osman409e74f2017-04-17 11:48:28 -04001115
Brian Osman5ea96bf2018-10-02 14:58:05 -04001116 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
Robert Phillips757914d2017-01-25 15:48:30 -05001117}
1118
Brian Salomonaff329b2017-08-11 09:40:37 -04001119std::unique_ptr<GrFragmentProcessor> GrContext::createUPMToPMEffect(
Brian Osman5ea96bf2018-10-02 14:58:05 -04001120 std::unique_ptr<GrFragmentProcessor> fp) {
joshualitt1de610a2016-01-06 08:26:09 -08001121 ASSERT_SINGLE_OWNER
Brian Osman5ea96bf2018-10-02 14:58:05 -04001122 // We should have already called this->validPMUPMConversionExists() in this case
1123 SkASSERT(fDidTestPMConversions);
1124 // ...and it should have succeeded
1125 SkASSERT(this->validPMUPMConversionExists());
Brian Osman409e74f2017-04-17 11:48:28 -04001126
Brian Osman5ea96bf2018-10-02 14:58:05 -04001127 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001128}
1129
Brian Osman409e74f2017-04-17 11:48:28 -04001130bool GrContext::validPMUPMConversionExists() {
joshualitt1de610a2016-01-06 08:26:09 -08001131 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -04001132 if (!fDidTestPMConversions) {
Brian Osman28804f32017-04-20 10:24:36 -04001133 fPMUPMConversionsRoundTrip = GrConfigConversionEffect::TestForPreservingPMConversions(this);
Brian Osman409e74f2017-04-17 11:48:28 -04001134 fDidTestPMConversions = true;
1135 }
1136
bsalomon636e8022015-07-29 06:08:46 -07001137 // The PM<->UPM tests fail or succeed together so we only need to check one.
Brian Osman28804f32017-04-20 10:24:36 -04001138 return fPMUPMConversionsRoundTrip;
bsalomon636e8022015-07-29 06:08:46 -07001139}
1140
Khushal3e7548c2018-05-23 15:45:01 -07001141bool GrContext::supportsDistanceFieldText() const {
1142 return fCaps->shaderCaps()->supportsDistanceFieldText();
1143}
1144
bsalomon37f9a262015-02-02 13:00:10 -08001145//////////////////////////////////////////////////////////////////////////////
1146
Robert Phillipsfc711a22018-02-13 17:03:00 -05001147// DDL TODO: remove 'maxResources'
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001148void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -08001149 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001150 if (maxResources) {
1151 *maxResources = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -08001152 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001153 if (maxResourceBytes) {
1154 *maxResourceBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -08001155 }
1156}
1157
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001158void GrContext::setResourceCacheLimits(int maxResources, size_t maxResourceBytes) {
joshualitt1de610a2016-01-06 08:26:09 -08001159 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001160 fResourceCache->setLimits(maxResources, maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -08001161}
1162
ericrk0a5fa482015-09-15 14:16:10 -07001163//////////////////////////////////////////////////////////////////////////////
ericrk0a5fa482015-09-15 14:16:10 -07001164void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -08001165 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -07001166 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
Khushal71652e22018-10-29 13:05:36 -07001167 traceMemoryDump->dumpNumericValue("skia/gr_text_blob_cache", "size", "bytes",
1168 fTextBlobCache->usedBytes());
ericrk0a5fa482015-09-15 14:16:10 -07001169}
Brian Osman71a18892017-08-10 10:23:25 -04001170
1171//////////////////////////////////////////////////////////////////////////////
Kevin Lubickf4def342018-10-04 12:52:50 -04001172#ifdef SK_ENABLE_DUMP_GPU
1173#include "SkJSONWriter.h"
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001174SkString GrContextPriv::dump() const {
Brian Osman71a18892017-08-10 10:23:25 -04001175 SkDynamicMemoryWStream stream;
1176 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
1177 writer.beginObject();
1178
1179 static const char* kBackendStr[] = {
1180 "Metal",
1181 "OpenGL",
1182 "Vulkan",
1183 "Mock",
1184 };
Greg Danielbdf12ad2018-10-12 09:31:11 -04001185 GR_STATIC_ASSERT(0 == (unsigned)GrBackendApi::kMetal);
1186 GR_STATIC_ASSERT(1 == (unsigned)GrBackendApi::kOpenGL);
1187 GR_STATIC_ASSERT(2 == (unsigned)GrBackendApi::kVulkan);
1188 GR_STATIC_ASSERT(3 == (unsigned)GrBackendApi::kMock);
1189 writer.appendString("backend", kBackendStr[(unsigned)fContext->fBackend]);
Brian Osman71a18892017-08-10 10:23:25 -04001190
1191 writer.appendName("caps");
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001192 fContext->fCaps->dumpJSON(&writer);
Brian Osman71a18892017-08-10 10:23:25 -04001193
1194 writer.appendName("gpu");
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001195 fContext->fGpu->dumpJSON(&writer);
Brian Osman71a18892017-08-10 10:23:25 -04001196
1197 // Flush JSON to the memory stream
1198 writer.endObject();
1199 writer.flush();
1200
1201 // Null terminate the JSON data in the memory stream
1202 stream.write8(0);
1203
1204 // Allocate a string big enough to hold all the data, then copy out of the stream
1205 SkString result(stream.bytesWritten());
1206 stream.copyToAndReset(result.writable_str());
1207 return result;
1208}
Kevin Lubickf4def342018-10-04 12:52:50 -04001209#endif