blob: b29454cd2f926f4089d7874b2d4754f446b3a2cd [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"
Matt Sarettc7b29082017-02-09 16:22:39 -050036#include "SkUnPreMultiplyPriv.h"
joshualitt5478d422014-11-14 16:00:38 -080037#include "effects/GrConfigConversionEffect.h"
Ethan Nicholas00543112018-07-31 09:44:36 -040038#include "effects/GrSkSLFP.h"
Brian Salomon5f33a8c2018-02-26 14:32:39 -050039#include "text/GrTextBlobCache.h"
Ethan Nicholas00543112018-07-31 09:44:36 -040040#include <unordered_map>
Greg Danielb76a72a2017-07-13 15:07:54 -040041
Robert Phillipse78b7252017-04-06 07:59:41 -040042#define ASSERT_OWNED_PROXY(P) \
Brian Salomonfd98c2c2018-07-31 17:25:29 -040043 SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == this)
Robert Phillips7ee385e2017-03-30 08:02:11 -040044#define ASSERT_OWNED_PROXY_PRIV(P) \
Brian Salomonfd98c2c2018-07-31 17:25:29 -040045 SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == fContext)
Robert Phillips7ee385e2017-03-30 08:02:11 -040046
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000047#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt1de610a2016-01-06 08:26:09 -080048#define ASSERT_SINGLE_OWNER \
49 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fSingleOwner);)
robertphillips4fd74ae2016-08-03 14:26:53 -070050#define ASSERT_SINGLE_OWNER_PRIV \
51 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fContext->fSingleOwner);)
robertphillips7761d612016-05-16 09:14:53 -070052#define RETURN_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return; }
Robert Phillips7ee385e2017-03-30 08:02:11 -040053#define RETURN_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return; }
robertphillips7761d612016-05-16 09:14:53 -070054#define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return false; }
Robert Phillipse78b7252017-04-06 07:59:41 -040055#define RETURN_FALSE_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return false; }
robertphillips7761d612016-05-16 09:14:53 -070056#define RETURN_NULL_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000057
robertphillipsea461502015-05-26 11:38:03 -070058////////////////////////////////////////////////////////////////////////////////
59
joshualitt0acd0d32015-05-07 08:23:19 -070060static int32_t gNextID = 1;
61static int32_t next_id() {
62 int32_t id;
63 do {
64 id = sk_atomic_inc(&gNextID);
65 } while (id == SK_InvalidGenID);
66 return id;
67}
68
Robert Phillipsfde6fa02018-03-02 08:53:14 -050069GrContext::GrContext(GrBackend backend, int32_t id)
70 : fBackend(backend)
71 , fUniqueID(SK_InvalidGenID == id ? next_id() : id) {
halcanary96fcdcc2015-08-27 07:41:13 -070072 fResourceCache = nullptr;
73 fResourceProvider = nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050074 fProxyProvider = nullptr;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050075 fGlyphCache = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000076}
77
Robert Phillipsfde6fa02018-03-02 08:53:14 -050078bool GrContext::initCommon(const GrContextOptions& options) {
Greg Danielb76a72a2017-07-13 15:07:54 -040079 ASSERT_SINGLE_OWNER
Robert Phillipsfde6fa02018-03-02 08:53:14 -050080 SkASSERT(fCaps); // needs to have been initialized by derived classes
81 SkASSERT(fThreadSafeProxy); // needs to have been initialized by derived classes
Robert Phillips88260b52018-01-19 12:56:09 -050082
83 if (fGpu) {
84 fCaps = fGpu->refCaps();
85 fResourceCache = new GrResourceCache(fCaps.get(), fUniqueID);
Robert Phillips4150eea2018-02-07 17:08:21 -050086 fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, &fSingleOwner,
87 options.fExplicitlyAllocateGPUResources);
Brian Salomon238069b2018-07-11 15:58:57 -040088 fProxyProvider =
89 new GrProxyProvider(fResourceProvider, fResourceCache, fCaps, &fSingleOwner);
90 } else {
91 fProxyProvider = new GrProxyProvider(this->uniqueID(), fCaps, &fSingleOwner);
Robert Phillips88260b52018-01-19 12:56:09 -050092 }
93
Robert Phillips88260b52018-01-19 12:56:09 -050094 if (fResourceCache) {
95 fResourceCache->setProxyProvider(fProxyProvider);
96 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050097
Brian Osman46da1cc2017-02-14 14:15:48 -050098 fDisableGpuYUVConversion = options.fDisableGpuYUVConversion;
Brian Osman8a83ca42018-02-12 14:32:17 -050099 fSharpenMipmappedTextures = options.fSharpenMipmappedTextures;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000100 fDidTestPMConversions = false;
101
bsalomon6b2552f2016-09-15 13:50:26 -0700102 GrPathRendererChain::Options prcOptions;
bsalomon39ef7fb2016-09-21 11:16:05 -0700103 prcOptions.fAllowPathMaskCaching = options.fAllowPathMaskCaching;
Brian Osman195c05b2017-08-30 15:14:04 -0400104#if GR_TEST_UTILS
csmartdalton008b9d82017-02-22 12:00:42 -0700105 prcOptions.fGpuPathRenderers = options.fGpuPathRenderers;
Brian Osman195c05b2017-08-30 15:14:04 -0400106#endif
Chris Daltonef125092018-06-26 18:16:47 -0600107 if (options.fDisableCoverageCountingPaths) {
108 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kCoverageCounting;
109 }
Brian Osmanb350ae22017-08-29 16:15:39 -0400110 if (options.fDisableDistanceFieldPaths) {
Brian Osman195c05b2017-08-30 15:14:04 -0400111 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
Brian Osmanb350ae22017-08-29 16:15:39 -0400112 }
Brian Salomonaf597482017-11-07 16:23:34 -0500113
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500114 if (!fResourceCache) {
115 // DDL TODO: remove this crippling of the path renderer chain
116 // Disable the small path renderer bc of the proxies in the atlas. They need to be
117 // unified when the opLists are added back to the destination drawing manager.
118 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
Robert Phillips5ffcb4d2018-07-03 16:39:40 -0400119 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kStencilAndCover;
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500120 }
121
Herb Derby26cbe512018-05-24 14:39:01 -0400122 GrTextContext::Options textContextOptions;
123 textContextOptions.fMaxDistanceFieldFontSize = options.fGlyphsAsPathsFontSize;
124 textContextOptions.fMinDistanceFieldFontSize = options.fMinDistanceFieldFontSize;
125 textContextOptions.fDistanceFieldVerticesAlwaysHaveW = false;
Brian Salomonb5086962017-12-13 10:59:33 -0500126#if SK_SUPPORT_ATLAS_TEXT
127 if (GrContextOptions::Enable::kYes == options.fDistanceFieldGlyphVerticesAlwaysHaveW) {
Herb Derby26cbe512018-05-24 14:39:01 -0400128 textContextOptions.fDistanceFieldVerticesAlwaysHaveW = true;
Brian Salomonb5086962017-12-13 10:59:33 -0500129 }
130#endif
Brian Salomonaf597482017-11-07 16:23:34 -0500131
Robert Phillips64ecdce2018-04-02 10:26:39 -0400132 bool explicitlyAllocatingResources = fResourceProvider
133 ? fResourceProvider->explicitlyAllocateGPUResources()
134 : false;
Herb Derby26cbe512018-05-24 14:39:01 -0400135 fDrawingManager.reset(new GrDrawingManager(this, prcOptions, textContextOptions,
Robert Phillips64ecdce2018-04-02 10:26:39 -0400136 &fSingleOwner, explicitlyAllocatingResources,
Robert Phillips46acf9d2018-10-09 09:31:40 -0400137 options.fSortRenderTargets,
138 options.fReduceOpListSplitting));
joshualitt7c3a2f82015-03-31 13:32:05 -0700139
Robert Phillipsaf4adef2018-03-07 11:59:37 -0500140 fGlyphCache = new GrGlyphCache(fCaps.get(), options.fGlyphCacheTextureMaximumBytes);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500141
Robert Phillips303cd582018-02-14 18:54:01 -0500142 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB,
Herb Derbyb12175f2018-05-23 16:38:09 -0400143 this, this->uniqueID()));
Brian Salomon91a3e522017-06-23 10:58:19 -0400144
Robert Phillipsfde6fa02018-03-02 08:53:14 -0500145 // DDL TODO: we need to think through how the task group & persistent cache
146 // get passed on to/shared between all the DDLRecorders created with this context.
Brian Osman51279982017-08-23 10:12:00 -0400147 if (options.fExecutor) {
148 fTaskGroup = skstd::make_unique<SkTaskGroup>(*options.fExecutor);
149 }
150
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400151 fPersistentCache = options.fPersistentCache;
152
Brian Salomon91a3e522017-06-23 10:58:19 -0400153 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000154}
155
bsalomon@google.com27847de2011-02-22 20:59:41 +0000156GrContext::~GrContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800157 ASSERT_SINGLE_OWNER
158
Robert Phillips2e6feed2018-01-22 15:27:20 -0500159 if (fDrawingManager) {
160 fDrawingManager->cleanup();
161 }
halcanary385fe4d2015-08-26 13:07:48 -0700162 delete fResourceProvider;
163 delete fResourceCache;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500164 delete fProxyProvider;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500165 delete fGlyphCache;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000166}
167
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400168//////////////////////////////////////////////////////////////////////////////
169
170GrContextThreadSafeProxy::GrContextThreadSafeProxy(sk_sp<const GrCaps> caps, uint32_t uniqueID,
171 GrBackend backend,
Ethan Nicholas00543112018-07-31 09:44:36 -0400172 const GrContextOptions& options,
173 sk_sp<GrSkSLFPFactoryCache> cache)
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400174 : fCaps(std::move(caps))
175 , fContextUniqueID(uniqueID)
176 , fBackend(backend)
Ethan Nicholas00543112018-07-31 09:44:36 -0400177 , fOptions(options)
178 , fFPFactoryCache(std::move(cache)) {}
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400179
Brian Salomon103d6f62018-05-11 11:19:22 -0400180GrContextThreadSafeProxy::~GrContextThreadSafeProxy() = default;
181
bungeman6bd52842016-10-27 09:30:08 -0700182sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
bungeman6bd52842016-10-27 09:30:08 -0700183 return fThreadSafeProxy;
bsalomon41b952c2016-03-11 06:46:33 -0800184}
185
Robert Phillipsfc711a22018-02-13 17:03:00 -0500186SkSurfaceCharacterization GrContextThreadSafeProxy::createCharacterization(
187 size_t cacheMaxResourceBytes,
188 const SkImageInfo& ii, const GrBackendFormat& backendFormat,
189 int sampleCnt, GrSurfaceOrigin origin,
190 const SkSurfaceProps& surfaceProps,
Greg Daniela070ed72018-04-26 16:31:38 -0400191 bool isMipMapped, bool willUseGLFBO0) {
Robert Phillipsfc711a22018-02-13 17:03:00 -0500192 if (!backendFormat.isValid()) {
193 return SkSurfaceCharacterization(); // return an invalid characterization
194 }
195
Greg Daniela070ed72018-04-26 16:31:38 -0400196 if (kOpenGL_GrBackend != backendFormat.backend() && willUseGLFBO0) {
197 // The willUseGLFBO0 flags can only be used for a GL backend.
198 return SkSurfaceCharacterization(); // return an invalid characterization
199 }
200
Robert Phillipsfc711a22018-02-13 17:03:00 -0500201 if (!fCaps->mipMapSupport()) {
202 isMipMapped = false;
203 }
204
205 GrPixelConfig config = kUnknown_GrPixelConfig;
206 if (!fCaps->getConfigFromBackendFormat(backendFormat, ii.colorType(), &config)) {
207 return SkSurfaceCharacterization(); // return an invalid characterization
208 }
209
Robert Phillipsbe77a022018-04-03 17:17:05 -0400210 if (!SkSurface_Gpu::Valid(fCaps.get(), config, ii.colorSpace())) {
211 return SkSurfaceCharacterization(); // return an invalid characterization
212 }
213
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400214 sampleCnt = fCaps->getRenderTargetSampleCount(sampleCnt, config);
215 if (!sampleCnt) {
216 return SkSurfaceCharacterization(); // return an invalid characterization
217 }
218
219 GrFSAAType FSAAType = GrFSAAType::kNone;
220 if (sampleCnt > 1) {
221 FSAAType = fCaps->usesMixedSamples() ? GrFSAAType::kMixedSamples : GrFSAAType::kUnifiedMSAA;
222 }
223
Robert Phillipsfc711a22018-02-13 17:03:00 -0500224 // This surface characterization factory assumes that the resulting characterization is
225 // textureable.
226 if (!fCaps->isConfigTexturable(config)) {
227 return SkSurfaceCharacterization(); // return an invalid characterization
228 }
229
230 return SkSurfaceCharacterization(sk_ref_sp<GrContextThreadSafeProxy>(this),
Robert Phillipsbe77a022018-04-03 17:17:05 -0400231 cacheMaxResourceBytes, ii,
232 origin, config, FSAAType, sampleCnt,
Robert Phillipsfc711a22018-02-13 17:03:00 -0500233 SkSurfaceCharacterization::Textureable(true),
234 SkSurfaceCharacterization::MipMapped(isMipMapped),
Greg Daniela070ed72018-04-26 16:31:38 -0400235 SkSurfaceCharacterization::UsesGLFBO0(willUseGLFBO0),
Robert Phillipsbe77a022018-04-03 17:17:05 -0400236 surfaceProps);
Robert Phillipsfc711a22018-02-13 17:03:00 -0500237}
238
bsalomon2354f842014-07-28 13:48:36 -0700239void GrContext::abandonContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800240 ASSERT_SINGLE_OWNER
241
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500242 fProxyProvider->abandon();
bsalomond309e7a2015-04-30 14:18:54 -0700243 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800244
245 // Need to abandon the drawing manager first so all the render targets
246 // will be released/forgotten before they too are abandoned.
247 fDrawingManager->abandon();
248
bsalomon@google.com205d4602011-04-25 12:43:45 +0000249 // abandon first to so destructors
250 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800251 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700252
bsalomon6e2aad42016-04-01 11:54:31 -0700253 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
254
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500255 fGlyphCache->freeAll();
bsalomon6e2aad42016-04-01 11:54:31 -0700256 fTextBlobCache->freeAll();
257}
258
Khushalc421ca12018-06-26 14:38:34 -0700259bool GrContext::abandoned() const {
260 ASSERT_SINGLE_OWNER
261 return fDrawingManager->wasAbandoned();
262}
263
bsalomon6e2aad42016-04-01 11:54:31 -0700264void GrContext::releaseResourcesAndAbandonContext() {
265 ASSERT_SINGLE_OWNER
266
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500267 fProxyProvider->abandon();
bsalomon6e2aad42016-04-01 11:54:31 -0700268 fResourceProvider->abandon();
269
270 // Need to abandon the drawing manager first so all the render targets
271 // will be released/forgotten before they too are abandoned.
272 fDrawingManager->abandon();
273
274 // Release all resources in the backend 3D API.
275 fResourceCache->releaseAll();
276
277 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000278
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500279 fGlyphCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700280 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000281}
282
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000283void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800284 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000285 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000286}
287
288void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800289 ASSERT_SINGLE_OWNER
290
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500291 fGlyphCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700292
293 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700294
295 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000296}
297
Robert Phillips6eba0632018-03-28 12:25:42 -0400298void GrContext::purgeUnlockedResources(bool scratchResourcesOnly) {
299 ASSERT_SINGLE_OWNER
300 fResourceCache->purgeUnlockedResources(scratchResourcesOnly);
301 fResourceCache->purgeAsNeeded();
302 fTextBlobCache->purgeStaleBlobs();
303}
304
Jim Van Verth76d917c2017-12-13 09:26:37 -0500305void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Brian Salomon5e150852017-03-22 14:53:13 -0400306 ASSERT_SINGLE_OWNER
Jim Van Verth76d917c2017-12-13 09:26:37 -0500307 fResourceCache->purgeAsNeeded();
308 fResourceCache->purgeResourcesNotUsedSince(GrStdSteadyClock::now() - msNotUsed);
309
310 fTextBlobCache->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400311}
312
Derek Sollenberger5480a182017-05-25 16:43:59 -0400313void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
314 ASSERT_SINGLE_OWNER
315 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
316}
317
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000318void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800319 ASSERT_SINGLE_OWNER
320
bsalomon71cb0c22014-11-14 12:10:14 -0800321 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800322 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800323 }
324 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800325 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800326 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000327}
328
Derek Sollenbergeree479142017-05-24 11:41:33 -0400329size_t GrContext::getResourceCachePurgeableBytes() const {
330 ASSERT_SINGLE_OWNER
331 return fResourceCache->getPurgeableBytes();
332}
333
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000334////////////////////////////////////////////////////////////////////////////////
335
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400336int GrContext::maxTextureSize() const { return fCaps->maxTextureSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400337
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400338int GrContext::maxRenderTargetSize() const { return fCaps->maxRenderTargetSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400339
Brian Salomonbdecacf2018-02-02 20:32:49 -0500340bool GrContext::colorTypeSupportedAsImage(SkColorType colorType) const {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400341 GrPixelConfig config = SkColorType2GrPixelConfig(colorType);
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400342 return fCaps->isConfigTexturable(config);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500343}
344
345int GrContext::maxSurfaceSampleCountForColorType(SkColorType colorType) const {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400346 GrPixelConfig config = SkColorType2GrPixelConfig(colorType);
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400347 return fCaps->maxRenderTargetSampleCount(config);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500348}
349
350////////////////////////////////////////////////////////////////////////////////
351
joshualitt0db6dfa2015-04-10 07:01:30 -0700352void GrContext::TextBlobCacheOverBudgetCB(void* data) {
353 SkASSERT(data);
Brian Osman11052242016-10-27 14:47:55 -0400354 // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on
355 // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls
356 // to below the GrContext level, but this is not trivial because they call drawPath on
357 // SkGpuDevice.
joshualitt0db6dfa2015-04-10 07:01:30 -0700358 GrContext* context = reinterpret_cast<GrContext*>(data);
359 context->flush();
360}
361
bsalomon@google.com27847de2011-02-22 20:59:41 +0000362////////////////////////////////////////////////////////////////////////////////
363
bsalomonb77a9072016-09-07 10:02:04 -0700364void GrContext::flush() {
joshualitt1de610a2016-01-06 08:26:09 -0800365 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700366 RETURN_IF_ABANDONED
Robert Phillips7ee385e2017-03-30 08:02:11 -0400367
368 fDrawingManager->flush(nullptr);
369}
370
Greg Daniel51316782017-08-02 15:10:09 +0000371GrSemaphoresSubmitted GrContext::flushAndSignalSemaphores(int numSemaphores,
372 GrBackendSemaphore signalSemaphores[]) {
373 ASSERT_SINGLE_OWNER
374 if (fDrawingManager->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; }
375
376 return fDrawingManager->flush(nullptr, numSemaphores, signalSemaphores);
377}
378
Robert Phillips7ee385e2017-03-30 08:02:11 -0400379void GrContextPriv::flush(GrSurfaceProxy* proxy) {
380 ASSERT_SINGLE_OWNER_PRIV
381 RETURN_IF_ABANDONED_PRIV
382 ASSERT_OWNED_PROXY_PRIV(proxy);
383
384 fContext->fDrawingManager->flush(proxy);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000385}
386
Brian Salomonc320b152018-02-20 14:05:36 -0500387// TODO: This will be removed when GrSurfaceContexts are aware of their color types.
388// (skbug.com/6718)
Brian Osmand2ca59a2017-04-13 14:03:57 -0400389static bool valid_premul_config(GrPixelConfig config) {
Brian Salomonc320b152018-02-20 14:05:36 -0500390 switch (config) {
391 case kUnknown_GrPixelConfig: return false;
392 case kAlpha_8_GrPixelConfig: return false;
393 case kGray_8_GrPixelConfig: return false;
394 case kRGB_565_GrPixelConfig: return false;
395 case kRGBA_4444_GrPixelConfig: return true;
396 case kRGBA_8888_GrPixelConfig: return true;
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400397 case kRGB_888_GrPixelConfig: return false;
Brian Salomonc320b152018-02-20 14:05:36 -0500398 case kBGRA_8888_GrPixelConfig: return true;
399 case kSRGBA_8888_GrPixelConfig: return true;
400 case kSBGRA_8888_GrPixelConfig: return true;
Brian Osman10fc6fd2018-03-02 11:01:10 -0500401 case kRGBA_1010102_GrPixelConfig: return true;
Brian Salomonc320b152018-02-20 14:05:36 -0500402 case kRGBA_float_GrPixelConfig: return true;
403 case kRG_float_GrPixelConfig: return false;
404 case kAlpha_half_GrPixelConfig: return false;
405 case kRGBA_half_GrPixelConfig: return true;
406 case kAlpha_8_as_Alpha_GrPixelConfig: return false;
407 case kAlpha_8_as_Red_GrPixelConfig: return false;
408 case kAlpha_half_as_Red_GrPixelConfig: return false;
409 case kGray_8_as_Lum_GrPixelConfig: return false;
410 case kGray_8_as_Red_GrPixelConfig: return false;
411 }
412 SK_ABORT("Invalid GrPixelConfig");
413 return false;
Brian Osmance425512017-03-22 14:37:50 -0400414}
415
Brian Salomonc320b152018-02-20 14:05:36 -0500416static bool valid_premul_color_type(GrColorType ct) {
417 switch (ct) {
Brian Osman10fc6fd2018-03-02 11:01:10 -0500418 case GrColorType::kUnknown: return false;
419 case GrColorType::kAlpha_8: return false;
420 case GrColorType::kRGB_565: return false;
421 case GrColorType::kABGR_4444: return true;
422 case GrColorType::kRGBA_8888: return true;
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400423 case GrColorType::kRGB_888x: return false;
Brian Osman10fc6fd2018-03-02 11:01:10 -0500424 case GrColorType::kBGRA_8888: return true;
425 case GrColorType::kRGBA_1010102: return true;
426 case GrColorType::kGray_8: return false;
427 case GrColorType::kAlpha_F16: return false;
428 case GrColorType::kRGBA_F16: return true;
429 case GrColorType::kRG_F32: return false;
430 case GrColorType::kRGBA_F32: return true;
Brian Salomonc320b152018-02-20 14:05:36 -0500431 }
432 SK_ABORT("Invalid GrColorType");
433 return false;
434}
435
436static bool valid_pixel_conversion(GrColorType cpuColorType, GrPixelConfig gpuConfig,
Brian Osmand2ca59a2017-04-13 14:03:57 -0400437 bool premulConversion) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400438 // We only allow premul <-> unpremul conversions for some formats
Brian Salomonc320b152018-02-20 14:05:36 -0500439 if (premulConversion &&
440 (!valid_premul_color_type(cpuColorType) || !valid_premul_config(gpuConfig))) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400441 return false;
442 }
Brian Osmand2ca59a2017-04-13 14:03:57 -0400443 return true;
444}
445
Brian Salomonc320b152018-02-20 14:05:36 -0500446bool GrContextPriv::writeSurfacePixels(GrSurfaceContext* dst, int left, int top, int width,
447 int height, GrColorType srcColorType,
448 SkColorSpace* srcColorSpace, const void* buffer,
449 size_t rowBytes, uint32_t pixelOpsFlags) {
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500450 ASSERT_SINGLE_OWNER_PRIV
451 RETURN_FALSE_IF_ABANDONED_PRIV
452 SkASSERT(dst);
453 SkASSERT(buffer);
454 ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy());
Brian Salomond494f6e2018-06-01 14:13:43 -0400455 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels", fContext);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500456
457 if (GrColorType::kUnknown == srcColorType) {
458 return false;
459 }
460
461 if (!dst->asSurfaceProxy()->instantiate(this->resourceProvider())) {
462 return false;
463 }
464
465 GrSurfaceProxy* dstProxy = dst->asSurfaceProxy();
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400466 GrSurface* dstSurface = dstProxy->peekSurface();
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500467
468 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
469 GrColorTypeBytesPerPixel(srcColorType), &left, &top,
470 &width, &height, &buffer, &rowBytes)) {
471 return false;
472 }
473
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400474 // TODO: Make GrSurfaceContext know its alpha type and pass src buffer's alpha type.
475 bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
476
477 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
478 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon817847c2018-05-07 13:33:50 -0400479 bool canvas2DFastPath =
Greg Daniel4374e962018-09-28 15:09:47 -0400480 !fContext->contextPriv().caps()->avoidWritePixelsFastPath() &&
Brian Salomon817847c2018-05-07 13:33:50 -0400481 premul &&
Brian Osman34ec3742018-07-03 10:40:57 -0400482 !dst->colorSpaceInfo().colorSpace() &&
Brian Salomon817847c2018-05-07 13:33:50 -0400483 (srcColorType == GrColorType::kRGBA_8888 || srcColorType == GrColorType::kBGRA_8888) &&
484 SkToBool(dst->asRenderTargetContext()) &&
485 (dstProxy->config() == kRGBA_8888_GrPixelConfig ||
486 dstProxy->config() == kBGRA_8888_GrPixelConfig) &&
487 !(pixelOpsFlags & kDontFlush_PixelOpsFlag) &&
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400488 fContext->contextPriv().caps()->isConfigTexturable(kRGBA_8888_GrPixelConfig) &&
Brian Salomon817847c2018-05-07 13:33:50 -0400489 fContext->validPMUPMConversionExists();
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400490
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400491 if (!fContext->contextPriv().caps()->surfaceSupportsWritePixels(dstSurface) ||
492 canvas2DFastPath) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400493 // We don't expect callers that are skipping flushes to require an intermediate draw.
494 SkASSERT(!(pixelOpsFlags & kDontFlush_PixelOpsFlag));
495 if (pixelOpsFlags & kDontFlush_PixelOpsFlag) {
496 return false;
497 }
498
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500499 GrSurfaceDesc desc;
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400500 desc.fConfig = canvas2DFastPath ? kRGBA_8888_GrPixelConfig : dstProxy->config();
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500501 desc.fWidth = width;
502 desc.fHeight = height;
503 desc.fSampleCnt = 1;
Brian Salomon2a4f9832018-03-03 22:43:43 -0500504 auto tempProxy = this->proxyProvider()->createProxy(
505 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kApprox, SkBudgeted::kYes);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500506 if (!tempProxy) {
507 return false;
508 }
509 auto tempCtx = this->drawingManager()->makeTextureContext(
510 tempProxy, dst->colorSpaceInfo().refColorSpace());
511 if (!tempCtx) {
512 return false;
513 }
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400514 uint32_t flags = canvas2DFastPath ? 0 : pixelOpsFlags;
Brian Salomon817847c2018-05-07 13:33:50 -0400515 // In the fast path we always write the srcData to the temp context as though it were RGBA.
516 // When the data is really BGRA the write will cause the R and B channels to be swapped in
517 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
518 // dst.
519 auto tmpColorType = canvas2DFastPath ? GrColorType::kRGBA_8888 : srcColorType;
Brian Salomond494f6e2018-06-01 14:13:43 -0400520 if (!this->writeSurfacePixels(tempCtx.get(), 0, 0, width, height, tmpColorType,
521 srcColorSpace, buffer, rowBytes, flags)) {
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500522 return false;
523 }
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400524 if (canvas2DFastPath) {
525 GrPaint paint;
526 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomon817847c2018-05-07 13:33:50 -0400527 auto fp = fContext->createUPMToPMEffect(
Brian Osman5ea96bf2018-10-02 14:58:05 -0400528 GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I()));
Brian Salomon817847c2018-05-07 13:33:50 -0400529 if (srcColorType == GrColorType::kBGRA_8888) {
530 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
531 }
532 if (!fp) {
533 return false;
534 }
535 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400536 dst->asRenderTargetContext()->fillRectToRect(
537 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
538 SkRect::MakeXYWH(left, top, width, height), SkRect::MakeWH(width, height));
539 return true;
540 } else {
541 return dst->copy(tempProxy.get(), SkIRect::MakeWH(width, height), {left, top});
542 }
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500543 }
544
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500545 bool convert = premul;
546
547 if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) {
548 return false;
549 }
550
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400551 GrColorType allowedColorType = fContext->contextPriv().caps()->supportedWritePixelsColorType(
552 dstProxy->config(), srcColorType);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500553 convert = convert || (srcColorType != allowedColorType);
554
555 if (!dst->colorSpaceInfo().colorSpace()) {
556 // "Legacy" mode - no color space conversions.
557 srcColorSpace = nullptr;
558 }
559 convert = convert || !SkColorSpace::Equals(srcColorSpace, dst->colorSpaceInfo().colorSpace());
560
561 std::unique_ptr<char[]> tempBuffer;
562 if (convert) {
563 auto srcSkColorType = GrColorTypeToSkColorType(srcColorType);
564 auto dstSkColorType = GrColorTypeToSkColorType(allowedColorType);
565 if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) {
566 return false;
567 }
Brian Osman9c37cd92018-10-11 12:57:44 +0000568 auto srcAlphaType = premul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500569 SkPixmap src(SkImageInfo::Make(width, height, srcSkColorType, srcAlphaType,
570 sk_ref_sp(srcColorSpace)),
571 buffer, rowBytes);
572 auto tempSrcII = SkImageInfo::Make(width, height, dstSkColorType, kPremul_SkAlphaType,
573 dst->colorSpaceInfo().refColorSpace());
574 auto size = tempSrcII.computeMinByteSize();
575 if (!size) {
576 return false;
577 }
578 tempBuffer.reset(new char[size]);
579 SkPixmap tempSrc(tempSrcII, tempBuffer.get(), tempSrcII.minRowBytes());
580 if (!src.readPixels(tempSrc)) {
581 return false;
582 }
583 srcColorType = allowedColorType;
584 buffer = tempSrc.addr();
585 rowBytes = tempSrc.rowBytes();
586 if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
587 std::unique_ptr<char[]> row(new char[rowBytes]);
588 for (int y = 0; y < height / 2; ++y) {
589 memcpy(row.get(), tempSrc.addr(0, y), rowBytes);
590 memcpy(tempSrc.writable_addr(0, y), tempSrc.addr(0, height - 1 - y), rowBytes);
591 memcpy(tempSrc.writable_addr(0, height - 1 - y), row.get(), rowBytes);
592 }
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400593 top = dstSurface->height() - top - height;
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500594 }
595 } else if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
596 size_t trimRowBytes = GrColorTypeBytesPerPixel(srcColorType) * width;
597 tempBuffer.reset(new char[trimRowBytes * height]);
598 char* dst = reinterpret_cast<char*>(tempBuffer.get()) + trimRowBytes * (height - 1);
599 const char* src = reinterpret_cast<const char*>(buffer);
600 for (int i = 0; i < height; ++i, src += rowBytes, dst -= trimRowBytes) {
601 memcpy(dst, src, trimRowBytes);
602 }
603 buffer = tempBuffer.get();
604 rowBytes = trimRowBytes;
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400605 top = dstSurface->height() - top - height;
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500606 }
607
608 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) {
609 this->flush(nullptr); // MDB TODO: tighten this
610 }
611
612 return this->getGpu()->writePixels(dstSurface, left, top, width, height, srcColorType, buffer,
613 rowBytes);
614}
615
Brian Salomond494f6e2018-06-01 14:13:43 -0400616bool GrContextPriv::readSurfacePixels(GrSurfaceContext* src, int left, int top, int width,
617 int height, GrColorType dstColorType,
618 SkColorSpace* dstColorSpace, void* buffer, size_t rowBytes,
619 uint32_t pixelOpsFlags) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400620 ASSERT_SINGLE_OWNER_PRIV
621 RETURN_FALSE_IF_ABANDONED_PRIV
622 SkASSERT(src);
623 SkASSERT(buffer);
624 ASSERT_OWNED_PROXY_PRIV(src->asSurfaceProxy());
Brian Salomond494f6e2018-06-01 14:13:43 -0400625 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "readSurfacePixels", fContext);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400626
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400627 SkASSERT(!(pixelOpsFlags & kDontFlush_PixelOpsFlag));
628 if (pixelOpsFlags & kDontFlush_PixelOpsFlag) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400629 return false;
630 }
631
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400632 // MDB TODO: delay this instantiation until later in the method
633 if (!src->asSurfaceProxy()->instantiate(this->resourceProvider())) {
634 return false;
635 }
636
637 GrSurfaceProxy* srcProxy = src->asSurfaceProxy();
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400638 GrSurface* srcSurface = srcProxy->peekSurface();
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400639
640 if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(),
641 GrColorTypeBytesPerPixel(dstColorType), &left, &top,
642 &width, &height, &buffer, &rowBytes)) {
643 return false;
644 }
645
646 // TODO: Make GrSurfaceContext know its alpha type and pass dst buffer's alpha type.
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400647 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400648
649 if (!valid_pixel_conversion(dstColorType, srcProxy->config(), unpremul)) {
650 return false;
651 }
652
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400653 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
654 // care so much about getImageData performance. However, in order to ensure putImageData/
655 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
Brian Salomond494f6e2018-06-01 14:13:43 -0400656 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400657 // fContext->vaildaPMUPMConversionExists()).
Brian Salomon817847c2018-05-07 13:33:50 -0400658 bool canvas2DFastPath =
659 unpremul &&
Brian Osman34ec3742018-07-03 10:40:57 -0400660 !src->colorSpaceInfo().colorSpace() &&
Brian Salomon817847c2018-05-07 13:33:50 -0400661 (GrColorType::kRGBA_8888 == dstColorType || GrColorType::kBGRA_8888 == dstColorType) &&
662 SkToBool(srcProxy->asTextureProxy()) &&
663 (srcProxy->config() == kRGBA_8888_GrPixelConfig ||
664 srcProxy->config() == kBGRA_8888_GrPixelConfig) &&
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400665 fContext->contextPriv().caps()->isConfigRenderable(kRGBA_8888_GrPixelConfig) &&
Brian Salomon817847c2018-05-07 13:33:50 -0400666 fContext->validPMUPMConversionExists();
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400667
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400668 if (!fContext->contextPriv().caps()->surfaceSupportsReadPixels(srcSurface) ||
669 canvas2DFastPath) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400670 GrSurfaceDesc desc;
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400671 desc.fFlags = canvas2DFastPath ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
672 desc.fConfig = canvas2DFastPath ? kRGBA_8888_GrPixelConfig : srcProxy->config();
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400673 desc.fWidth = width;
674 desc.fHeight = height;
675 desc.fSampleCnt = 1;
676 auto tempProxy = this->proxyProvider()->createProxy(
677 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kApprox, SkBudgeted::kYes);
678 if (!tempProxy) {
679 return false;
680 }
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400681 sk_sp<GrSurfaceContext> tempCtx;
682 if (canvas2DFastPath) {
683 tempCtx = this->drawingManager()->makeRenderTargetContext(std::move(tempProxy), nullptr,
684 nullptr);
Greg Daniel42314012018-04-23 10:57:37 -0400685 SkASSERT(tempCtx->asRenderTargetContext());
686 tempCtx->asRenderTargetContext()->discard();
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400687 } else {
688 tempCtx = this->drawingManager()->makeTextureContext(
689 std::move(tempProxy), src->colorSpaceInfo().refColorSpace());
690 }
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400691 if (!tempCtx) {
692 return false;
693 }
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400694 if (canvas2DFastPath) {
695 GrPaint paint;
696 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomon817847c2018-05-07 13:33:50 -0400697 auto fp = fContext->createPMToUPMEffect(
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400698 GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()),
Brian Osman5ea96bf2018-10-02 14:58:05 -0400699 SkMatrix::I()));
Brian Salomon817847c2018-05-07 13:33:50 -0400700 if (dstColorType == GrColorType::kBGRA_8888) {
701 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
702 dstColorType = GrColorType::kRGBA_8888;
703 }
704 if (!fp) {
705 return false;
706 }
707 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400708 tempCtx->asRenderTargetContext()->fillRectToRect(
709 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
710 SkRect::MakeWH(width, height), SkRect::MakeXYWH(left, top, width, height));
711 } else if (!tempCtx->copy(srcProxy, SkIRect::MakeXYWH(left, top, width, height), {0, 0})) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400712 return false;
713 }
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400714 uint32_t flags = canvas2DFastPath ? 0 : pixelOpsFlags;
Brian Salomond494f6e2018-06-01 14:13:43 -0400715 return this->readSurfacePixels(tempCtx.get(), 0, 0, width, height, dstColorType,
716 dstColorSpace, buffer, rowBytes, flags);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400717 }
718
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400719 bool convert = unpremul;
720
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400721 bool flip = srcProxy->origin() == kBottomLeft_GrSurfaceOrigin;
722 if (flip) {
723 top = srcSurface->height() - top - height;
724 }
725
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400726 GrColorType allowedColorType = fContext->contextPriv().caps()->supportedReadPixelsColorType(
727 srcProxy->config(), dstColorType);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400728 convert = convert || (dstColorType != allowedColorType);
729
730 if (!src->colorSpaceInfo().colorSpace()) {
731 // "Legacy" mode - no color space conversions.
732 dstColorSpace = nullptr;
733 }
734 convert = convert || !SkColorSpace::Equals(dstColorSpace, src->colorSpaceInfo().colorSpace());
735
736 SkAutoPixmapStorage tempPixmap;
737 SkPixmap finalPixmap;
738 if (convert) {
739 SkColorType srcSkColorType = GrColorTypeToSkColorType(allowedColorType);
740 SkColorType dstSkColorType = GrColorTypeToSkColorType(dstColorType);
741 if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) {
742 return false;
743 }
744 auto tempAT = SkColorTypeIsAlwaysOpaque(srcSkColorType) ? kOpaque_SkAlphaType
745 : kPremul_SkAlphaType;
746 auto tempII = SkImageInfo::Make(width, height, srcSkColorType, tempAT,
747 src->colorSpaceInfo().refColorSpace());
748 SkASSERT(!unpremul || !SkColorTypeIsAlwaysOpaque(dstSkColorType));
Brian Osman9c37cd92018-10-11 12:57:44 +0000749 auto finalAT = SkColorTypeIsAlwaysOpaque(srcSkColorType)
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400750 ? kOpaque_SkAlphaType
751 : unpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
752 auto finalII =
753 SkImageInfo::Make(width, height, dstSkColorType, finalAT, sk_ref_sp(dstColorSpace));
754 if (!SkImageInfoValidConversion(finalII, tempII)) {
755 return false;
756 }
Brian Salomon6196fc12018-07-12 16:39:23 -0400757 if (!tempPixmap.tryAlloc(tempII)) {
758 return false;
759 }
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400760 finalPixmap.reset(finalII, buffer, rowBytes);
761 buffer = tempPixmap.writable_addr();
762 rowBytes = tempPixmap.rowBytes();
Brian Salomon3d945e82018-05-14 14:54:06 -0400763 // Chrome msan bots require this.
764 sk_bzero(buffer, tempPixmap.computeByteSize());
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400765 }
766
767 if (srcSurface->surfacePriv().hasPendingWrite()) {
768 this->flush(nullptr); // MDB TODO: tighten this
769 }
770
771 if (!fContext->fGpu->readPixels(srcSurface, left, top, width, height, allowedColorType, buffer,
772 rowBytes)) {
773 return false;
774 }
775
776 if (flip) {
777 size_t trimRowBytes = GrColorTypeBytesPerPixel(allowedColorType) * width;
778 std::unique_ptr<char[]> row(new char[trimRowBytes]);
779 char* upper = reinterpret_cast<char*>(buffer);
780 char* lower = reinterpret_cast<char*>(buffer) + (height - 1) * rowBytes;
781 for (int y = 0; y < height / 2; ++y, upper += rowBytes, lower -= rowBytes) {
782 memcpy(row.get(), upper, trimRowBytes);
783 memcpy(upper, lower, trimRowBytes);
784 memcpy(lower, row.get(), trimRowBytes);
785 }
786 }
787 if (convert) {
788 if (!tempPixmap.readPixels(finalPixmap)) {
789 return false;
790 }
791 }
792 return true;
793}
794
Robert Phillips7ee385e2017-03-30 08:02:11 -0400795void GrContextPriv::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) {
796 ASSERT_SINGLE_OWNER_PRIV
797 RETURN_IF_ABANDONED_PRIV
798 SkASSERT(proxy);
799 ASSERT_OWNED_PROXY_PRIV(proxy);
Greg Daniel51316782017-08-02 15:10:09 +0000800 fContext->fDrawingManager->prepareSurfaceForExternalIO(proxy, 0, nullptr);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000801}
802
Robert Phillips7ee385e2017-03-30 08:02:11 -0400803void GrContextPriv::flushSurfaceWrites(GrSurfaceProxy* proxy) {
804 ASSERT_SINGLE_OWNER_PRIV
805 RETURN_IF_ABANDONED_PRIV
806 SkASSERT(proxy);
807 ASSERT_OWNED_PROXY_PRIV(proxy);
808 if (proxy->priv().hasPendingWrite()) {
809 this->flush(proxy);
bsalomonf80bfed2014-10-07 05:56:02 -0700810 }
811}
812
Robert Phillips7ee385e2017-03-30 08:02:11 -0400813void GrContextPriv::flushSurfaceIO(GrSurfaceProxy* proxy) {
814 ASSERT_SINGLE_OWNER_PRIV
815 RETURN_IF_ABANDONED_PRIV
816 SkASSERT(proxy);
817 ASSERT_OWNED_PROXY_PRIV(proxy);
818 if (proxy->priv().hasPendingIO()) {
819 this->flush(proxy);
ajuma95243eb2016-08-24 08:19:02 -0700820 }
821}
822
bsalomon@google.com27847de2011-02-22 20:59:41 +0000823////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000824
Robert Phillipsc994a932018-06-19 13:09:54 -0400825sk_sp<GrOpMemoryPool> GrContextPriv::refOpMemoryPool() {
826 if (!fContext->fOpMemoryPool) {
827 // DDL TODO: should the size of the memory pool be decreased in DDL mode? CPU-side memory
828 // consumed in DDL mode vs. normal mode for a single skp might be a good metric of wasted
829 // memory.
830 fContext->fOpMemoryPool = sk_sp<GrOpMemoryPool>(new GrOpMemoryPool(16384, 16384));
831 }
832
833 SkASSERT(fContext->fOpMemoryPool);
834 return fContext->fOpMemoryPool;
835}
836
837GrOpMemoryPool* GrContextPriv::opMemoryPool() {
838 return this->refOpMemoryPool().get();
839}
840
Robert Phillips2c862492017-01-18 10:08:39 -0500841sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy,
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500842 sk_sp<SkColorSpace> colorSpace,
843 const SkSurfaceProps* props) {
Brian Osman45580d32016-11-23 09:37:01 -0500844 ASSERT_SINGLE_OWNER_PRIV
845
Brian Osman45580d32016-11-23 09:37:01 -0500846 if (proxy->asRenderTargetProxy()) {
Robert Phillips2c862492017-01-18 10:08:39 -0500847 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500848 std::move(colorSpace), props);
Brian Osman45580d32016-11-23 09:37:01 -0500849 } else {
850 SkASSERT(proxy->asTextureProxy());
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500851 SkASSERT(!props);
Robert Phillips2c862492017-01-18 10:08:39 -0500852 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Brian Osman45580d32016-11-23 09:37:01 -0500853 }
854}
855
Robert Phillipse2f7d182016-12-15 09:23:05 -0500856sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrSurfaceDesc& dstDesc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500857 GrSurfaceOrigin origin,
Greg Daniel65c7f662017-10-30 13:39:09 -0400858 GrMipMapped mipMapped,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500859 SkBackingFit fit,
Brian Salomon366093f2018-02-13 09:25:22 -0500860 SkBudgeted isDstBudgeted,
861 sk_sp<SkColorSpace> colorSpace,
862 const SkSurfaceProps* props) {
Greg Daniel65c7f662017-10-30 13:39:09 -0400863 sk_sp<GrTextureProxy> proxy;
864 if (GrMipMapped::kNo == mipMapped) {
Brian Salomon2a4f9832018-03-03 22:43:43 -0500865 proxy = this->proxyProvider()->createProxy(dstDesc, origin, fit, isDstBudgeted);
Greg Daniel65c7f662017-10-30 13:39:09 -0400866 } else {
867 SkASSERT(SkBackingFit::kExact == fit);
Brian Salomon2a4f9832018-03-03 22:43:43 -0500868 proxy = this->proxyProvider()->createMipMapProxy(dstDesc, origin, isDstBudgeted);
Greg Daniel65c7f662017-10-30 13:39:09 -0400869 }
Robert Phillips77b3f322017-01-31 18:24:12 -0500870 if (!proxy) {
871 return nullptr;
872 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500873
Greg Daniel55fa6472018-03-16 16:13:10 -0400874 sk_sp<GrSurfaceContext> sContext = this->makeWrappedSurfaceContext(std::move(proxy),
875 std::move(colorSpace),
876 props);
877 if (sContext && sContext->asRenderTargetContext()) {
878 sContext->asRenderTargetContext()->discard();
879 }
880
881 return sContext;
Robert Phillipse2f7d182016-12-15 09:23:05 -0500882}
883
Brian Salomond17f6582017-07-19 18:28:58 -0400884sk_sp<GrTextureContext> GrContextPriv::makeBackendTextureContext(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000885 GrSurfaceOrigin origin,
Brian Osmanc1e37052017-03-09 14:19:20 -0500886 sk_sp<SkColorSpace> colorSpace) {
Robert Phillips26caf892017-01-27 10:58:31 -0500887 ASSERT_SINGLE_OWNER_PRIV
888
Brian Salomon7578f3e2018-03-07 14:39:54 -0500889 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->wrapBackendTexture(tex, origin);
Robert Phillips77b3f322017-01-31 18:24:12 -0500890 if (!proxy) {
891 return nullptr;
892 }
Robert Phillips26caf892017-01-27 10:58:31 -0500893
Brian Salomond17f6582017-07-19 18:28:58 -0400894 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Robert Phillips26caf892017-01-27 10:58:31 -0500895}
896
Brian Osman11052242016-10-27 14:47:55 -0400897sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000898 const GrBackendTexture& tex,
899 GrSurfaceOrigin origin,
900 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -0400901 sk_sp<SkColorSpace> colorSpace,
Brian Osmanc1e37052017-03-09 14:19:20 -0500902 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700903 ASSERT_SINGLE_OWNER_PRIV
Brian Salomonbdecacf2018-02-02 20:32:49 -0500904 SkASSERT(sampleCnt > 0);
robertphillips4fd74ae2016-08-03 14:26:53 -0700905
Brian Salomon7578f3e2018-03-07 14:39:54 -0500906 sk_sp<GrTextureProxy> proxy(
907 this->proxyProvider()->wrapRenderableBackendTexture(tex, origin, sampleCnt));
Robert Phillips77b3f322017-01-31 18:24:12 -0500908 if (!proxy) {
909 return nullptr;
910 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400911
Robert Phillips37430132016-11-09 06:50:43 -0500912 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Brian Osman11052242016-10-27 14:47:55 -0400913 std::move(colorSpace), props);
robertphillips4fd74ae2016-08-03 14:26:53 -0700914}
915
Brian Osman11052242016-10-27 14:47:55 -0400916sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +0000917 const GrBackendRenderTarget& backendRT,
918 GrSurfaceOrigin origin,
robertphillips4fd74ae2016-08-03 14:26:53 -0700919 sk_sp<SkColorSpace> colorSpace,
920 const SkSurfaceProps* surfaceProps) {
921 ASSERT_SINGLE_OWNER_PRIV
922
Brian Salomon7578f3e2018-03-07 14:39:54 -0500923 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->wrapBackendRenderTarget(backendRT, origin);
Robert Phillips77b3f322017-01-31 18:24:12 -0500924 if (!proxy) {
925 return nullptr;
926 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400927
Robert Phillips37430132016-11-09 06:50:43 -0500928 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400929 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -0400930 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700931}
932
Brian Osman11052242016-10-27 14:47:55 -0400933sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000934 const GrBackendTexture& tex,
935 GrSurfaceOrigin origin,
936 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -0700937 sk_sp<SkColorSpace> colorSpace,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500938 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700939 ASSERT_SINGLE_OWNER_PRIV
Brian Salomonbdecacf2018-02-02 20:32:49 -0500940 SkASSERT(sampleCnt > 0);
Brian Salomon7578f3e2018-03-07 14:39:54 -0500941 sk_sp<GrSurfaceProxy> proxy(
942 this->proxyProvider()->wrapBackendTextureAsRenderTarget(tex, origin, sampleCnt));
Robert Phillips77b3f322017-01-31 18:24:12 -0500943 if (!proxy) {
944 return nullptr;
945 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400946
Robert Phillips37430132016-11-09 06:50:43 -0500947 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400948 std::move(colorSpace),
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500949 props);
robertphillips77a2e522015-10-17 07:43:27 -0700950}
951
Chris Daltonfe199b72017-05-05 11:26:15 -0400952void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
953 fContext->fDrawingManager->addOnFlushCallbackObject(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400954}
955
Robert Phillips62000362018-02-01 09:10:04 -0500956void GrContextPriv::moveOpListsToDDL(SkDeferredDisplayList* ddl) {
957 fContext->fDrawingManager->moveOpListsToDDL(ddl);
958}
959
960void GrContextPriv::copyOpListsFromDDL(const SkDeferredDisplayList* ddl,
961 GrRenderTargetProxy* newDest) {
962 fContext->fDrawingManager->copyOpListsFromDDL(ddl, newDest);
963}
964
robertphillips48fde9c2016-09-06 05:20:20 -0700965static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
Brian Osman78f20e02017-01-12 10:28:01 -0500966 switch (config) {
967 case kAlpha_8_GrPixelConfig:
Greg Daniel93b4ddd2018-04-16 16:16:47 -0400968 case kAlpha_8_as_Alpha_GrPixelConfig:
969 case kAlpha_8_as_Red_GrPixelConfig:
Brian Osman78f20e02017-01-12 10:28:01 -0500970 case kRGB_565_GrPixelConfig:
971 case kRGBA_4444_GrPixelConfig:
972 case kBGRA_8888_GrPixelConfig:
Brian Osman10fc6fd2018-03-02 11:01:10 -0500973 case kRGBA_1010102_GrPixelConfig:
Brian Osman78f20e02017-01-12 10:28:01 -0500974 return kRGBA_8888_GrPixelConfig;
975 case kSBGRA_8888_GrPixelConfig:
976 return kSRGBA_8888_GrPixelConfig;
977 case kAlpha_half_GrPixelConfig:
Greg Daniel93b4ddd2018-04-16 16:16:47 -0400978 case kAlpha_half_as_Red_GrPixelConfig:
Brian Osman78f20e02017-01-12 10:28:01 -0500979 return kRGBA_half_GrPixelConfig;
Greg Daniel93b4ddd2018-04-16 16:16:47 -0400980 case kGray_8_GrPixelConfig:
981 case kGray_8_as_Lum_GrPixelConfig:
982 case kGray_8_as_Red_GrPixelConfig:
983 return kRGB_888_GrPixelConfig;
Brian Osman78f20e02017-01-12 10:28:01 -0500984 default:
985 return kUnknown_GrPixelConfig;
986 }
robertphillips48fde9c2016-09-06 05:20:20 -0700987}
988
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500989sk_sp<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContextWithFallback(
robertphillipsd728f0c2016-11-21 11:05:03 -0800990 SkBackingFit fit,
991 int width, int height,
992 GrPixelConfig config,
993 sk_sp<SkColorSpace> colorSpace,
994 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -0400995 GrMipMapped mipMapped,
robertphillipsd728f0c2016-11-21 11:05:03 -0800996 GrSurfaceOrigin origin,
997 const SkSurfaceProps* surfaceProps,
998 SkBudgeted budgeted) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500999 SkASSERT(sampleCnt > 0);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001000 if (0 == fContext->contextPriv().caps()->getRenderTargetSampleCount(sampleCnt, config)) {
robertphillipsd728f0c2016-11-21 11:05:03 -08001001 config = GrPixelConfigFallback(config);
1002 }
1003
1004 return this->makeDeferredRenderTargetContext(fit, width, height, config, std::move(colorSpace),
Greg Daniel45d63032017-10-30 13:41:26 -04001005 sampleCnt, mipMapped, origin, surfaceProps,
Greg Daniele1da1d92017-10-06 15:59:27 -04001006 budgeted);
robertphillipsd728f0c2016-11-21 11:05:03 -08001007}
1008
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001009sk_sp<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContext(
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001010 SkBackingFit fit,
1011 int width, int height,
1012 GrPixelConfig config,
1013 sk_sp<SkColorSpace> colorSpace,
1014 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -04001015 GrMipMapped mipMapped,
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001016 GrSurfaceOrigin origin,
1017 const SkSurfaceProps* surfaceProps,
1018 SkBudgeted budgeted) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05001019 SkASSERT(sampleCnt > 0);
Khushalc421ca12018-06-26 14:38:34 -07001020 if (fContext->abandoned()) {
Brian Salomon79e4d1b2017-07-13 11:17:11 -04001021 return nullptr;
1022 }
1023
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001024 GrSurfaceDesc desc;
1025 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001026 desc.fWidth = width;
1027 desc.fHeight = height;
1028 desc.fConfig = config;
1029 desc.fSampleCnt = sampleCnt;
1030
Greg Daniele1da1d92017-10-06 15:59:27 -04001031 sk_sp<GrTextureProxy> rtp;
Greg Daniel45d63032017-10-30 13:41:26 -04001032 if (GrMipMapped::kNo == mipMapped) {
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001033 rtp = fContext->fProxyProvider->createProxy(desc, origin, fit, budgeted);
Greg Daniele1da1d92017-10-06 15:59:27 -04001034 } else {
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001035 rtp = fContext->fProxyProvider->createMipMapProxy(desc, origin, budgeted);
Greg Daniele1da1d92017-10-06 15:59:27 -04001036 }
Robert Phillips08c5ec72017-01-30 12:26:47 -05001037 if (!rtp) {
1038 return nullptr;
1039 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001040
Robert Phillips1119dc32017-04-11 12:54:57 -04001041 sk_sp<GrRenderTargetContext> renderTargetContext(
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001042 fContext->fDrawingManager->makeRenderTargetContext(std::move(rtp),
1043 std::move(colorSpace),
1044 surfaceProps));
Robert Phillips1119dc32017-04-11 12:54:57 -04001045 if (!renderTargetContext) {
1046 return nullptr;
1047 }
1048
1049 renderTargetContext->discard();
1050
1051 return renderTargetContext;
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001052}
1053
Brian Salomonaff329b2017-08-11 09:40:37 -04001054std::unique_ptr<GrFragmentProcessor> GrContext::createPMToUPMEffect(
Brian Osman5ea96bf2018-10-02 14:58:05 -04001055 std::unique_ptr<GrFragmentProcessor> fp) {
Robert Phillips757914d2017-01-25 15:48:30 -05001056 ASSERT_SINGLE_OWNER
Brian Osman5ea96bf2018-10-02 14:58:05 -04001057 // We should have already called this->validPMUPMConversionExists() in this case
1058 SkASSERT(fDidTestPMConversions);
1059 // ...and it should have succeeded
1060 SkASSERT(this->validPMUPMConversionExists());
Brian Osman409e74f2017-04-17 11:48:28 -04001061
Brian Osman5ea96bf2018-10-02 14:58:05 -04001062 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
Robert Phillips757914d2017-01-25 15:48:30 -05001063}
1064
Brian Salomonaff329b2017-08-11 09:40:37 -04001065std::unique_ptr<GrFragmentProcessor> GrContext::createUPMToPMEffect(
Brian Osman5ea96bf2018-10-02 14:58:05 -04001066 std::unique_ptr<GrFragmentProcessor> fp) {
joshualitt1de610a2016-01-06 08:26:09 -08001067 ASSERT_SINGLE_OWNER
Brian Osman5ea96bf2018-10-02 14:58:05 -04001068 // We should have already called this->validPMUPMConversionExists() in this case
1069 SkASSERT(fDidTestPMConversions);
1070 // ...and it should have succeeded
1071 SkASSERT(this->validPMUPMConversionExists());
Brian Osman409e74f2017-04-17 11:48:28 -04001072
Brian Osman5ea96bf2018-10-02 14:58:05 -04001073 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001074}
1075
Brian Osman409e74f2017-04-17 11:48:28 -04001076bool GrContext::validPMUPMConversionExists() {
joshualitt1de610a2016-01-06 08:26:09 -08001077 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -04001078 if (!fDidTestPMConversions) {
Brian Osman28804f32017-04-20 10:24:36 -04001079 fPMUPMConversionsRoundTrip = GrConfigConversionEffect::TestForPreservingPMConversions(this);
Brian Osman409e74f2017-04-17 11:48:28 -04001080 fDidTestPMConversions = true;
1081 }
1082
bsalomon636e8022015-07-29 06:08:46 -07001083 // The PM<->UPM tests fail or succeed together so we only need to check one.
Brian Osman28804f32017-04-20 10:24:36 -04001084 return fPMUPMConversionsRoundTrip;
bsalomon636e8022015-07-29 06:08:46 -07001085}
1086
Khushal3e7548c2018-05-23 15:45:01 -07001087bool GrContext::supportsDistanceFieldText() const {
1088 return fCaps->shaderCaps()->supportsDistanceFieldText();
1089}
1090
bsalomon37f9a262015-02-02 13:00:10 -08001091//////////////////////////////////////////////////////////////////////////////
1092
Robert Phillipsfc711a22018-02-13 17:03:00 -05001093// DDL TODO: remove 'maxResources'
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001094void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -08001095 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001096 if (maxResources) {
1097 *maxResources = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -08001098 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001099 if (maxResourceBytes) {
1100 *maxResourceBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -08001101 }
1102}
1103
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001104void GrContext::setResourceCacheLimits(int maxResources, size_t maxResourceBytes) {
joshualitt1de610a2016-01-06 08:26:09 -08001105 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001106 fResourceCache->setLimits(maxResources, maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -08001107}
1108
ericrk0a5fa482015-09-15 14:16:10 -07001109//////////////////////////////////////////////////////////////////////////////
ericrk0a5fa482015-09-15 14:16:10 -07001110void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -08001111 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -07001112 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
1113}
Brian Osman71a18892017-08-10 10:23:25 -04001114
1115//////////////////////////////////////////////////////////////////////////////
Kevin Lubickf4def342018-10-04 12:52:50 -04001116#ifdef SK_ENABLE_DUMP_GPU
1117#include "SkJSONWriter.h"
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001118SkString GrContextPriv::dump() const {
Brian Osman71a18892017-08-10 10:23:25 -04001119 SkDynamicMemoryWStream stream;
1120 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
1121 writer.beginObject();
1122
1123 static const char* kBackendStr[] = {
1124 "Metal",
1125 "OpenGL",
1126 "Vulkan",
1127 "Mock",
1128 };
1129 GR_STATIC_ASSERT(0 == kMetal_GrBackend);
1130 GR_STATIC_ASSERT(1 == kOpenGL_GrBackend);
1131 GR_STATIC_ASSERT(2 == kVulkan_GrBackend);
1132 GR_STATIC_ASSERT(3 == kMock_GrBackend);
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001133 writer.appendString("backend", kBackendStr[fContext->fBackend]);
Brian Osman71a18892017-08-10 10:23:25 -04001134
1135 writer.appendName("caps");
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001136 fContext->fCaps->dumpJSON(&writer);
Brian Osman71a18892017-08-10 10:23:25 -04001137
1138 writer.appendName("gpu");
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001139 fContext->fGpu->dumpJSON(&writer);
Brian Osman71a18892017-08-10 10:23:25 -04001140
1141 // Flush JSON to the memory stream
1142 writer.endObject();
1143 writer.flush();
1144
1145 // Null terminate the JSON data in the memory stream
1146 stream.write8(0);
1147
1148 // Allocate a string big enough to hold all the data, then copy out of the stream
1149 SkString result(stream.bytesWritten());
1150 stream.copyToAndReset(result.writable_str());
1151 return result;
1152}
Kevin Lubickf4def342018-10-04 12:52:50 -04001153#endif