blob: 613a05b22e090bb671ade935621dfc9dc41e9ba3 [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 Phillips1afd4cd2018-01-08 13:40:32 -050015#include "GrProxyProvider.h"
Brian Osman11052242016-10-27 14:47:55 -040016#include "GrRenderTargetContext.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050017#include "GrRenderTargetProxy.h"
bsalomon0ea80f42015-02-11 10:49:59 -080018#include "GrResourceCache.h"
bsalomond309e7a2015-04-30 14:18:54 -070019#include "GrResourceProvider.h"
Greg Danield85f97d2017-03-07 13:37:21 -050020#include "GrSemaphore.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000021#include "GrSoftwarePathRenderer.h"
Brian Osman45580d32016-11-23 09:37:01 -050022#include "GrSurfaceContext.h"
bsalomonafbf2d62014-09-30 12:18:44 -070023#include "GrSurfacePriv.h"
Robert Phillips757914d2017-01-25 15:48:30 -050024#include "GrSurfaceProxyPriv.h"
Robert Phillips646e4292017-06-13 12:44:56 -040025#include "GrTexture.h"
Brian Osman45580d32016-11-23 09:37:01 -050026#include "GrTextureContext.h"
Robert Phillips41a3b872018-03-09 12:00:34 -050027#include "GrTextureStripAtlas.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040028#include "GrTracing.h"
Brian Salomon19eaf2d2018-03-19 16:06:44 -040029#include "SkAutoPixmapStorage.h"
Matt Sarett485c4992017-02-14 14:18:27 -050030#include "SkConvertPixels.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 Osman71a18892017-08-10 10:23:25 -040034#include "SkJSONWriter.h"
Brian Osman51279982017-08-23 10:12:00 -040035#include "SkMakeUnique.h"
Robert Phillips6b6fcc72018-03-30 13:57:00 -040036#include "SkSurface_Gpu.h"
Brian Osman51279982017-08-23 10:12:00 -040037#include "SkTaskGroup.h"
Matt Sarettc7b29082017-02-09 16:22:39 -050038#include "SkUnPreMultiplyPriv.h"
joshualitt5478d422014-11-14 16:00:38 -080039#include "effects/GrConfigConversionEffect.h"
Brian Salomon5f33a8c2018-02-26 14:32:39 -050040#include "text/GrTextBlobCache.h"
Greg Danielb76a72a2017-07-13 15:07:54 -040041
Robert Phillipse78b7252017-04-06 07:59:41 -040042#define ASSERT_OWNED_PROXY(P) \
43SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == this)
Robert Phillips7ee385e2017-03-30 08:02:11 -040044#define ASSERT_OWNED_PROXY_PRIV(P) \
45SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == fContext)
46
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);
Robert Phillips88260b52018-01-19 12:56:09 -050088 }
89
Robert Phillips1afd4cd2018-01-08 13:40:32 -050090 fProxyProvider = new GrProxyProvider(fResourceProvider, fResourceCache, fCaps, &fSingleOwner);
Robert Phillips88260b52018-01-19 12:56:09 -050091
92 if (fResourceCache) {
93 fResourceCache->setProxyProvider(fProxyProvider);
94 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050095
Robert Phillips41a3b872018-03-09 12:00:34 -050096 fTextureStripAtlasManager.reset(new GrTextureStripAtlasManager);
97
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
Brian Osmanb350ae22017-08-29 16:15:39 -0400107 if (options.fDisableDistanceFieldPaths) {
Brian Osman195c05b2017-08-30 15:14:04 -0400108 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
Brian Osmanb350ae22017-08-29 16:15:39 -0400109 }
Brian Salomonaf597482017-11-07 16:23:34 -0500110
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500111 if (!fResourceCache) {
112 // DDL TODO: remove this crippling of the path renderer chain
113 // Disable the small path renderer bc of the proxies in the atlas. They need to be
114 // unified when the opLists are added back to the destination drawing manager.
115 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
Robert Phillipsf733a722018-03-15 13:59:35 -0400116 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kCoverageCounting;
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500117 }
118
Brian Salomonaf597482017-11-07 16:23:34 -0500119 GrAtlasTextContext::Options atlasTextContextOptions;
120 atlasTextContextOptions.fMaxDistanceFieldFontSize = options.fGlyphsAsPathsFontSize;
121 atlasTextContextOptions.fMinDistanceFieldFontSize = options.fMinDistanceFieldFontSize;
Brian Salomonb5086962017-12-13 10:59:33 -0500122 atlasTextContextOptions.fDistanceFieldVerticesAlwaysHaveW = false;
123#if SK_SUPPORT_ATLAS_TEXT
124 if (GrContextOptions::Enable::kYes == options.fDistanceFieldGlyphVerticesAlwaysHaveW) {
125 atlasTextContextOptions.fDistanceFieldVerticesAlwaysHaveW = true;
126 }
127#endif
Brian Salomonaf597482017-11-07 16:23:34 -0500128
Robert Phillips64ecdce2018-04-02 10:26:39 -0400129 bool explicitlyAllocatingResources = fResourceProvider
130 ? fResourceProvider->explicitlyAllocateGPUResources()
131 : false;
Robert Phillips4150eea2018-02-07 17:08:21 -0500132 fDrawingManager.reset(new GrDrawingManager(this, prcOptions, atlasTextContextOptions,
Robert Phillips64ecdce2018-04-02 10:26:39 -0400133 &fSingleOwner, explicitlyAllocatingResources,
134 options.fSortRenderTargets));
joshualitt7c3a2f82015-03-31 13:32:05 -0700135
Robert Phillipsaf4adef2018-03-07 11:59:37 -0500136 fGlyphCache = new GrGlyphCache(fCaps.get(), options.fGlyphCacheTextureMaximumBytes);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500137
Robert Phillips303cd582018-02-14 18:54:01 -0500138 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB,
139 this, this->uniqueID(), SkToBool(fGpu)));
Brian Salomon91a3e522017-06-23 10:58:19 -0400140
Robert Phillipsfde6fa02018-03-02 08:53:14 -0500141 // DDL TODO: we need to think through how the task group & persistent cache
142 // get passed on to/shared between all the DDLRecorders created with this context.
Brian Osman51279982017-08-23 10:12:00 -0400143 if (options.fExecutor) {
144 fTaskGroup = skstd::make_unique<SkTaskGroup>(*options.fExecutor);
145 }
146
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400147 fPersistentCache = options.fPersistentCache;
148
Brian Salomon91a3e522017-06-23 10:58:19 -0400149 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000150}
151
bsalomon@google.com27847de2011-02-22 20:59:41 +0000152GrContext::~GrContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800153 ASSERT_SINGLE_OWNER
154
Robert Phillips2e6feed2018-01-22 15:27:20 -0500155 if (fDrawingManager) {
156 fDrawingManager->cleanup();
157 }
robertphillips2334fb62015-06-17 05:43:33 -0700158
Robert Phillips41a3b872018-03-09 12:00:34 -0500159 fTextureStripAtlasManager = nullptr;
halcanary385fe4d2015-08-26 13:07:48 -0700160 delete fResourceProvider;
161 delete fResourceCache;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500162 delete fProxyProvider;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500163 delete fGlyphCache;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000164}
165
bungeman6bd52842016-10-27 09:30:08 -0700166sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
bungeman6bd52842016-10-27 09:30:08 -0700167 return fThreadSafeProxy;
bsalomon41b952c2016-03-11 06:46:33 -0800168}
169
Robert Phillipsfc711a22018-02-13 17:03:00 -0500170SkSurfaceCharacterization GrContextThreadSafeProxy::createCharacterization(
171 size_t cacheMaxResourceBytes,
172 const SkImageInfo& ii, const GrBackendFormat& backendFormat,
173 int sampleCnt, GrSurfaceOrigin origin,
174 const SkSurfaceProps& surfaceProps,
175 bool isMipMapped) {
176 if (!backendFormat.isValid()) {
177 return SkSurfaceCharacterization(); // return an invalid characterization
178 }
179
Robert Phillipsfc711a22018-02-13 17:03:00 -0500180 if (!fCaps->mipMapSupport()) {
181 isMipMapped = false;
182 }
183
184 GrPixelConfig config = kUnknown_GrPixelConfig;
185 if (!fCaps->getConfigFromBackendFormat(backendFormat, ii.colorType(), &config)) {
186 return SkSurfaceCharacterization(); // return an invalid characterization
187 }
188
Robert Phillipsbe77a022018-04-03 17:17:05 -0400189 if (!SkSurface_Gpu::Valid(fCaps.get(), config, ii.colorSpace())) {
190 return SkSurfaceCharacterization(); // return an invalid characterization
191 }
192
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400193 sampleCnt = fCaps->getRenderTargetSampleCount(sampleCnt, config);
194 if (!sampleCnt) {
195 return SkSurfaceCharacterization(); // return an invalid characterization
196 }
197
198 GrFSAAType FSAAType = GrFSAAType::kNone;
199 if (sampleCnt > 1) {
200 FSAAType = fCaps->usesMixedSamples() ? GrFSAAType::kMixedSamples : GrFSAAType::kUnifiedMSAA;
201 }
202
Robert Phillipsfc711a22018-02-13 17:03:00 -0500203 // This surface characterization factory assumes that the resulting characterization is
204 // textureable.
205 if (!fCaps->isConfigTexturable(config)) {
206 return SkSurfaceCharacterization(); // return an invalid characterization
207 }
208
209 return SkSurfaceCharacterization(sk_ref_sp<GrContextThreadSafeProxy>(this),
Robert Phillipsbe77a022018-04-03 17:17:05 -0400210 cacheMaxResourceBytes, ii,
211 origin, config, FSAAType, sampleCnt,
Robert Phillipsfc711a22018-02-13 17:03:00 -0500212 SkSurfaceCharacterization::Textureable(true),
213 SkSurfaceCharacterization::MipMapped(isMipMapped),
Robert Phillipsbe77a022018-04-03 17:17:05 -0400214 surfaceProps);
Robert Phillipsfc711a22018-02-13 17:03:00 -0500215}
216
bsalomon2354f842014-07-28 13:48:36 -0700217void GrContext::abandonContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800218 ASSERT_SINGLE_OWNER
219
Robert Phillips41a3b872018-03-09 12:00:34 -0500220 fTextureStripAtlasManager->abandon();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500221 fProxyProvider->abandon();
bsalomond309e7a2015-04-30 14:18:54 -0700222 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800223
224 // Need to abandon the drawing manager first so all the render targets
225 // will be released/forgotten before they too are abandoned.
226 fDrawingManager->abandon();
227
bsalomon@google.com205d4602011-04-25 12:43:45 +0000228 // abandon first to so destructors
229 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800230 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700231
bsalomon6e2aad42016-04-01 11:54:31 -0700232 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
233
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500234 fGlyphCache->freeAll();
bsalomon6e2aad42016-04-01 11:54:31 -0700235 fTextBlobCache->freeAll();
236}
237
238void GrContext::releaseResourcesAndAbandonContext() {
239 ASSERT_SINGLE_OWNER
240
Robert Phillips41a3b872018-03-09 12:00:34 -0500241 fTextureStripAtlasManager->abandon();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500242 fProxyProvider->abandon();
bsalomon6e2aad42016-04-01 11:54:31 -0700243 fResourceProvider->abandon();
244
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
249 // Release all resources in the backend 3D API.
250 fResourceCache->releaseAll();
251
252 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000253
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500254 fGlyphCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700255 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000256}
257
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000258void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800259 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000260 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000261}
262
263void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800264 ASSERT_SINGLE_OWNER
265
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500266 fGlyphCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700267
268 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700269
270 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000271}
272
Robert Phillips6eba0632018-03-28 12:25:42 -0400273void GrContext::purgeUnlockedResources(bool scratchResourcesOnly) {
274 ASSERT_SINGLE_OWNER
275 fResourceCache->purgeUnlockedResources(scratchResourcesOnly);
276 fResourceCache->purgeAsNeeded();
277 fTextBlobCache->purgeStaleBlobs();
278}
279
Jim Van Verth76d917c2017-12-13 09:26:37 -0500280void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Brian Salomon5e150852017-03-22 14:53:13 -0400281 ASSERT_SINGLE_OWNER
Jim Van Verth76d917c2017-12-13 09:26:37 -0500282 fResourceCache->purgeAsNeeded();
283 fResourceCache->purgeResourcesNotUsedSince(GrStdSteadyClock::now() - msNotUsed);
284
285 fTextBlobCache->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400286}
287
Derek Sollenberger5480a182017-05-25 16:43:59 -0400288void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
289 ASSERT_SINGLE_OWNER
290 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
291}
292
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000293void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800294 ASSERT_SINGLE_OWNER
295
bsalomon71cb0c22014-11-14 12:10:14 -0800296 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800297 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800298 }
299 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800300 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800301 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000302}
303
Derek Sollenbergeree479142017-05-24 11:41:33 -0400304size_t GrContext::getResourceCachePurgeableBytes() const {
305 ASSERT_SINGLE_OWNER
306 return fResourceCache->getPurgeableBytes();
307}
308
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000309////////////////////////////////////////////////////////////////////////////////
310
Brian Salomonf932a632018-04-05 12:46:09 -0400311int GrContext::maxTextureSize() const { return this->caps()->maxTextureSize(); }
312
313int GrContext::maxRenderTargetSize() const { return this->caps()->maxRenderTargetSize(); }
314
Brian Salomonbdecacf2018-02-02 20:32:49 -0500315bool GrContext::colorTypeSupportedAsImage(SkColorType colorType) const {
316 GrPixelConfig config = SkImageInfo2GrPixelConfig(colorType, nullptr, *this->caps());
317 return this->caps()->isConfigTexturable(config);
318}
319
320int GrContext::maxSurfaceSampleCountForColorType(SkColorType colorType) const {
321 GrPixelConfig config = SkImageInfo2GrPixelConfig(colorType, nullptr, *this->caps());
322 return this->caps()->maxRenderTargetSampleCount(config);
323}
324
325////////////////////////////////////////////////////////////////////////////////
326
joshualitt0db6dfa2015-04-10 07:01:30 -0700327void GrContext::TextBlobCacheOverBudgetCB(void* data) {
328 SkASSERT(data);
Brian Osman11052242016-10-27 14:47:55 -0400329 // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on
330 // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls
331 // to below the GrContext level, but this is not trivial because they call drawPath on
332 // SkGpuDevice.
joshualitt0db6dfa2015-04-10 07:01:30 -0700333 GrContext* context = reinterpret_cast<GrContext*>(data);
334 context->flush();
335}
336
bsalomon@google.com27847de2011-02-22 20:59:41 +0000337////////////////////////////////////////////////////////////////////////////////
338
bsalomonb77a9072016-09-07 10:02:04 -0700339void GrContext::flush() {
joshualitt1de610a2016-01-06 08:26:09 -0800340 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700341 RETURN_IF_ABANDONED
Robert Phillips7ee385e2017-03-30 08:02:11 -0400342
343 fDrawingManager->flush(nullptr);
344}
345
Greg Daniel51316782017-08-02 15:10:09 +0000346GrSemaphoresSubmitted GrContext::flushAndSignalSemaphores(int numSemaphores,
347 GrBackendSemaphore signalSemaphores[]) {
348 ASSERT_SINGLE_OWNER
349 if (fDrawingManager->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; }
350
351 return fDrawingManager->flush(nullptr, numSemaphores, signalSemaphores);
352}
353
Robert Phillips7ee385e2017-03-30 08:02:11 -0400354void GrContextPriv::flush(GrSurfaceProxy* proxy) {
355 ASSERT_SINGLE_OWNER_PRIV
356 RETURN_IF_ABANDONED_PRIV
357 ASSERT_OWNED_PROXY_PRIV(proxy);
358
359 fContext->fDrawingManager->flush(proxy);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000360}
361
Brian Salomonc320b152018-02-20 14:05:36 -0500362bool sw_convert_to_premul(GrColorType srcColorType, int width, int height, size_t inRowBytes,
bsalomon81beccc2014-10-13 12:32:55 -0700363 const void* inPixels, size_t outRowBytes, void* outPixels) {
Brian Salomonc320b152018-02-20 14:05:36 -0500364 SkColorType colorType = GrColorTypeToSkColorType(srcColorType);
365 if (kUnknown_SkColorType == colorType || 4 != SkColorTypeBytesPerPixel(colorType)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000366 return false;
367 }
bsalomon81beccc2014-10-13 12:32:55 -0700368
Matt Sarettc7b29082017-02-09 16:22:39 -0500369 for (int y = 0; y < height; y++) {
370 SkOpts::RGBA_to_rgbA((uint32_t*) outPixels, inPixels, width);
371 outPixels = SkTAddOffset<void>(outPixels, outRowBytes);
372 inPixels = SkTAddOffset<const void>(inPixels, inRowBytes);
373 }
bsalomon81beccc2014-10-13 12:32:55 -0700374
Matt Sarettc7b29082017-02-09 16:22:39 -0500375 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000376}
377
Brian Salomonc320b152018-02-20 14:05:36 -0500378// TODO: This will be removed when GrSurfaceContexts are aware of their color types.
379// (skbug.com/6718)
Brian Osmand2ca59a2017-04-13 14:03:57 -0400380static bool valid_premul_config(GrPixelConfig config) {
Brian Salomonc320b152018-02-20 14:05:36 -0500381 switch (config) {
382 case kUnknown_GrPixelConfig: return false;
383 case kAlpha_8_GrPixelConfig: return false;
384 case kGray_8_GrPixelConfig: return false;
385 case kRGB_565_GrPixelConfig: return false;
386 case kRGBA_4444_GrPixelConfig: return true;
387 case kRGBA_8888_GrPixelConfig: return true;
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400388 case kRGB_888_GrPixelConfig: return false;
Brian Salomonc320b152018-02-20 14:05:36 -0500389 case kBGRA_8888_GrPixelConfig: return true;
390 case kSRGBA_8888_GrPixelConfig: return true;
391 case kSBGRA_8888_GrPixelConfig: return true;
Brian Osman10fc6fd2018-03-02 11:01:10 -0500392 case kRGBA_1010102_GrPixelConfig: return true;
Brian Salomonc320b152018-02-20 14:05:36 -0500393 case kRGBA_float_GrPixelConfig: return true;
394 case kRG_float_GrPixelConfig: return false;
395 case kAlpha_half_GrPixelConfig: return false;
396 case kRGBA_half_GrPixelConfig: return true;
397 case kAlpha_8_as_Alpha_GrPixelConfig: return false;
398 case kAlpha_8_as_Red_GrPixelConfig: return false;
399 case kAlpha_half_as_Red_GrPixelConfig: return false;
400 case kGray_8_as_Lum_GrPixelConfig: return false;
401 case kGray_8_as_Red_GrPixelConfig: return false;
402 }
403 SK_ABORT("Invalid GrPixelConfig");
404 return false;
Brian Osmance425512017-03-22 14:37:50 -0400405}
406
Brian Salomonc320b152018-02-20 14:05:36 -0500407static bool valid_premul_color_type(GrColorType ct) {
408 switch (ct) {
Brian Osman10fc6fd2018-03-02 11:01:10 -0500409 case GrColorType::kUnknown: return false;
410 case GrColorType::kAlpha_8: return false;
411 case GrColorType::kRGB_565: return false;
412 case GrColorType::kABGR_4444: return true;
413 case GrColorType::kRGBA_8888: return true;
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400414 case GrColorType::kRGB_888x: return false;
Brian Osman10fc6fd2018-03-02 11:01:10 -0500415 case GrColorType::kBGRA_8888: return true;
416 case GrColorType::kRGBA_1010102: return true;
417 case GrColorType::kGray_8: return false;
418 case GrColorType::kAlpha_F16: return false;
419 case GrColorType::kRGBA_F16: return true;
420 case GrColorType::kRG_F32: return false;
421 case GrColorType::kRGBA_F32: return true;
Brian Salomonc320b152018-02-20 14:05:36 -0500422 }
423 SK_ABORT("Invalid GrColorType");
424 return false;
425}
426
427static bool valid_pixel_conversion(GrColorType cpuColorType, GrPixelConfig gpuConfig,
Brian Osmand2ca59a2017-04-13 14:03:57 -0400428 bool premulConversion) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400429 // We only allow premul <-> unpremul conversions for some formats
Brian Salomonc320b152018-02-20 14:05:36 -0500430 if (premulConversion &&
431 (!valid_premul_color_type(cpuColorType) || !valid_premul_config(gpuConfig))) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400432 return false;
433 }
Brian Osmand2ca59a2017-04-13 14:03:57 -0400434 return true;
435}
436
Brian Salomonc320b152018-02-20 14:05:36 -0500437static bool pm_upm_must_round_trip(GrColorType cpuColorType, const SkColorSpace* cpuColorSpace) {
438 return !cpuColorSpace &&
439 (GrColorType::kRGBA_8888 == cpuColorType || GrColorType::kBGRA_8888 == cpuColorType);
Brian Osman409e74f2017-04-17 11:48:28 -0400440}
441
Brian Salomonc320b152018-02-20 14:05:36 -0500442// TODO: This will be removed when GrSurfaceContexts are aware of their color types.
443// (skbug.com/6718)
444static bool pm_upm_must_round_trip(GrPixelConfig surfaceConfig,
445 const SkColorSpace* surfaceColorSpace) {
446 return !surfaceColorSpace &&
447 (kRGBA_8888_GrPixelConfig == surfaceConfig || kBGRA_8888_GrPixelConfig == surfaceConfig);
448}
449
450static GrSRGBConversion determine_write_pixels_srgb_conversion(GrColorType srcColorType,
451 const SkColorSpace* srcColorSpace,
452 GrSRGBEncoded dstSRGBEncoded,
453 const SkColorSpace* dstColorSpace,
454 const GrCaps& caps) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500455 // No support for sRGB-encoded alpha.
Brian Salomonc320b152018-02-20 14:05:36 -0500456 if (GrColorTypeIsAlphaOnly(srcColorType)) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500457 return GrSRGBConversion::kNone;
458 }
Brian Salomonc320b152018-02-20 14:05:36 -0500459 // No conversions without GPU support for sRGB. (Legacy mode)
460 if (!caps.srgbSupport()) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500461 return GrSRGBConversion::kNone;
462 }
Brian Salomonc320b152018-02-20 14:05:36 -0500463 // If the GrSurfaceContext has no color space then it is in legacy mode.
464 if (!dstColorSpace) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500465 return GrSRGBConversion::kNone;
466 }
467
468 bool srcColorSpaceIsSRGB = srcColorSpace && srcColorSpace->gammaCloseToSRGB();
469 bool dstColorSpaceIsSRGB = dstColorSpace->gammaCloseToSRGB();
470
471 // For now we are assuming that if color space of the dst does not have sRGB gamma then the
472 // texture format is not sRGB encoded and vice versa. Note that we already checked for "legacy"
Brian Salomonc320b152018-02-20 14:05:36 -0500473 // mode being forced on by caps above. This may change in the future. We will then have to
474 // perform shader based conversions.
Brian Salomon9b009bb2018-02-14 13:53:55 -0500475 SkASSERT(dstColorSpaceIsSRGB == (GrSRGBEncoded::kYes == dstSRGBEncoded));
476
Brian Salomon9b009bb2018-02-14 13:53:55 -0500477 if (srcColorSpaceIsSRGB == dstColorSpaceIsSRGB) {
478 return GrSRGBConversion::kNone;
479 }
480 return srcColorSpaceIsSRGB ? GrSRGBConversion::kSRGBToLinear : GrSRGBConversion::kLinearToSRGB;
481}
482
Brian Salomonc320b152018-02-20 14:05:36 -0500483static GrSRGBConversion determine_read_pixels_srgb_conversion(GrSRGBEncoded srcSRGBEncoded,
484 const SkColorSpace* srcColorSpace,
485 GrColorType dstColorType,
486 const SkColorSpace* dstColorSpace,
487 const GrCaps& caps) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500488 // This is symmetrical with the write version.
Brian Salomonc320b152018-02-20 14:05:36 -0500489 switch (determine_write_pixels_srgb_conversion(dstColorType, dstColorSpace, srcSRGBEncoded,
490 srcColorSpace, caps)) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500491 case GrSRGBConversion::kNone: return GrSRGBConversion::kNone;
492 case GrSRGBConversion::kLinearToSRGB: return GrSRGBConversion::kSRGBToLinear;
493 case GrSRGBConversion::kSRGBToLinear: return GrSRGBConversion::kLinearToSRGB;
494 }
495 return GrSRGBConversion::kNone;
496}
497
Brian Salomonc320b152018-02-20 14:05:36 -0500498bool GrContextPriv::writeSurfacePixels(GrSurfaceContext* dst, int left, int top, int width,
499 int height, GrColorType srcColorType,
500 SkColorSpace* srcColorSpace, const void* buffer,
501 size_t rowBytes, uint32_t pixelOpsFlags) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400502 bool useLegacyPath = false;
503#ifdef SK_LEGACY_GPU_PIXEL_OPS
504 useLegacyPath = true;
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500505#endif
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400506 // Newly added color types/configs are only supported by the new code path.
507 if (srcColorType == GrColorType::kRGB_888x ||
508 dst->asSurfaceProxy()->config() == kRGB_888_GrPixelConfig) {
509 useLegacyPath = false;
510 }
511
512 if (!useLegacyPath) {
513 return this->writeSurfacePixels2(dst, left, top, width, height, srcColorType, srcColorSpace,
514 buffer, rowBytes, pixelOpsFlags);
515 }
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500516
Brian Osmanb62ea222016-12-22 11:12:16 -0500517 // TODO: Color space conversion
518
Robert Phillipse78b7252017-04-06 07:59:41 -0400519 ASSERT_SINGLE_OWNER_PRIV
520 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400521 SkASSERT(dst);
522 ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy());
Brian Salomondcbb9d92017-07-19 10:53:20 -0400523 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels", fContext);
bsalomon6c6f6582015-09-10 08:12:46 -0700524
Robert Phillips6be756b2018-01-16 15:07:54 -0500525 if (!dst->asSurfaceProxy()->instantiate(this->resourceProvider())) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400526 return false;
527 }
528
Robert Phillips16d8ec62017-07-27 16:16:25 -0400529 GrSurfaceProxy* dstProxy = dst->asSurfaceProxy();
530 GrSurface* dstSurface = dstProxy->priv().peekSurface();
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400531
Brian Osmand2ca59a2017-04-13 14:03:57 -0400532 // The src is unpremul but the dst is premul -> premul the src before or as part of the write
Brian Osman409e74f2017-04-17 11:48:28 -0400533 const bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
Brian Salomonc320b152018-02-20 14:05:36 -0500534
535 if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400536 return false;
537 }
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400538 // There is no way to store alpha values in the dst.
539 if (GrColorTypeHasAlpha(srcColorType) && GrPixelConfigIsOpaque(dstProxy->config())) {
540 return false;
541 }
542 // The source has no alpha value and the dst is only alpha
543 if (!GrColorTypeHasAlpha(srcColorType) && GrPixelConfigIsAlphaOnly(dstProxy->config())) {
544 return false;
545 }
Brian Osmand2ca59a2017-04-13 14:03:57 -0400546
Brian Osman409e74f2017-04-17 11:48:28 -0400547 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
548 // without any color spaces attached, and the caller wants us to premul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400549 bool useConfigConversionEffect =
Brian Salomonc320b152018-02-20 14:05:36 -0500550 premul && pm_upm_must_round_trip(srcColorType, srcColorSpace) &&
Brian Salomonf3569f02017-10-24 12:52:33 -0400551 pm_upm_must_round_trip(dstProxy->config(), dst->colorSpaceInfo().colorSpace());
Brian Osman409e74f2017-04-17 11:48:28 -0400552
553 // Are we going to try to premul as part of a draw? For the non-legacy case, we always allow
554 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
555 bool premulOnGpu = premul &&
556 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon81beccc2014-10-13 12:32:55 -0700557
bsalomone8d21e82015-07-16 08:23:13 -0700558 // Trim the params here so that if we wind up making a temporary surface it can be as small as
bsalomonf0674512015-07-28 13:26:15 -0700559 // necessary and because GrGpu::getWritePixelsInfo requires it.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400560 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
Brian Salomonc320b152018-02-20 14:05:36 -0500561 GrColorTypeBytesPerPixel(srcColorType), &left, &top,
562 &width, &height, &buffer, &rowBytes)) {
bsalomone8d21e82015-07-16 08:23:13 -0700563 return false;
564 }
565
Brian Osman409e74f2017-04-17 11:48:28 -0400566 GrGpu::DrawPreference drawPreference = premulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
567 : GrGpu::kNoDraw_DrawPreference;
bsalomonf0674512015-07-28 13:26:15 -0700568 GrGpu::WritePixelTempDrawInfo tempDrawInfo;
Brian Salomon9b009bb2018-02-14 13:53:55 -0500569 GrSRGBConversion srgbConversion = determine_write_pixels_srgb_conversion(
Brian Salomonc320b152018-02-20 14:05:36 -0500570 srcColorType, srcColorSpace, GrPixelConfigIsSRGBEncoded(dstProxy->config()),
571 dst->colorSpaceInfo().colorSpace(), *fContext->caps());
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400572 if (!fContext->fGpu->getWritePixelsInfo(dstSurface, dstProxy->origin(), width, height,
Brian Salomonc320b152018-02-20 14:05:36 -0500573 srcColorType, srgbConversion, &drawPreference,
Brian Salomon9b009bb2018-02-14 13:53:55 -0500574 &tempDrawInfo)) {
bsalomonf0674512015-07-28 13:26:15 -0700575 return false;
576 }
577
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400578 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400579 this->flush(nullptr); // MDB TODO: tighten this
bsalomonf0674512015-07-28 13:26:15 -0700580 }
581
Robert Phillips2f493142017-03-02 18:18:38 -0500582 sk_sp<GrTextureProxy> tempProxy;
bsalomonf0674512015-07-28 13:26:15 -0700583 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500584 tempProxy = this->proxyProvider()->createProxy(tempDrawInfo.fTempSurfaceDesc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500585 kTopLeft_GrSurfaceOrigin,
586 SkBackingFit::kApprox, SkBudgeted::kYes);
Robert Phillips2f493142017-03-02 18:18:38 -0500587 if (!tempProxy && GrGpu::kRequireDraw_DrawPreference == drawPreference) {
bsalomonf0674512015-07-28 13:26:15 -0700588 return false;
589 }
590 }
591
592 // temp buffer for doing sw premul conversion, if needed.
593 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
Brian Osman409e74f2017-04-17 11:48:28 -0400594 // We need to do sw premul if we were unable to create a RT for drawing, or if we can't do the
595 // premul on the GPU
596 if (premul && (!tempProxy || !premulOnGpu)) {
597 size_t tmpRowBytes = 4 * width;
598 tmpPixels.reset(width * height);
Brian Salomonc320b152018-02-20 14:05:36 -0500599 if (!sw_convert_to_premul(srcColorType, width, height, rowBytes, buffer, tmpRowBytes,
Brian Osman409e74f2017-04-17 11:48:28 -0400600 tmpPixels.get())) {
601 return false;
bsalomonf0674512015-07-28 13:26:15 -0700602 }
Brian Osman409e74f2017-04-17 11:48:28 -0400603 rowBytes = tmpRowBytes;
604 buffer = tmpPixels.get();
bsalomonf0674512015-07-28 13:26:15 -0700605 }
Brian Osman409e74f2017-04-17 11:48:28 -0400606
607 if (tempProxy) {
Brian Osman2240be92017-10-18 13:15:13 -0400608 auto fp = GrSimpleTextureEffect::Make(tempProxy, SkMatrix::I());
Brian Osman409e74f2017-04-17 11:48:28 -0400609 if (premulOnGpu) {
610 fp = fContext->createUPMToPMEffect(std::move(fp), useConfigConversionEffect);
bsalomon81beccc2014-10-13 12:32:55 -0700611 }
Brian Osman409e74f2017-04-17 11:48:28 -0400612 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400613 if (!fp) {
614 return false;
615 }
Brian Osman409e74f2017-04-17 11:48:28 -0400616
Robert Phillips6be756b2018-01-16 15:07:54 -0500617 if (!tempProxy->instantiate(this->resourceProvider())) {
Brian Osman409e74f2017-04-17 11:48:28 -0400618 return false;
619 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400620 GrTexture* texture = tempProxy->priv().peekTexture();
Robert Phillips7bbbf622017-10-17 07:36:59 -0400621
622 if (tempProxy->priv().hasPendingIO()) {
623 this->flush(tempProxy.get());
624 }
625
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400626 if (!fContext->fGpu->writePixels(texture, tempProxy->origin(), 0, 0, width, height,
Brian Salomonc320b152018-02-20 14:05:36 -0500627 tempDrawInfo.fWriteColorType, buffer, rowBytes)) {
Brian Osman409e74f2017-04-17 11:48:28 -0400628 return false;
629 }
Robert Phillips7bbbf622017-10-17 07:36:59 -0400630 tempProxy = nullptr;
631
Brian Osman409e74f2017-04-17 11:48:28 -0400632 SkMatrix matrix;
633 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400634 GrRenderTargetContext* renderTargetContext = dst->asRenderTargetContext();
Brian Osman409e74f2017-04-17 11:48:28 -0400635 if (!renderTargetContext) {
636 return false;
637 }
638 GrPaint paint;
639 paint.addColorFragmentProcessor(std::move(fp));
640 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonf3569f02017-10-24 12:52:33 -0400641 paint.setAllowSRGBInputs(dst->colorSpaceInfo().isGammaCorrect() ||
642 GrPixelConfigIsSRGB(dst->colorSpaceInfo().config()));
Brian Osman409e74f2017-04-17 11:48:28 -0400643 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
644 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, matrix, rect,
645 nullptr);
646
647 if (kFlushWrites_PixelOp & pixelOpsFlags) {
648 this->flushSurfaceWrites(renderTargetContext->asRenderTargetProxy());
649 }
650 } else {
Brian Salomonc320b152018-02-20 14:05:36 -0500651 return fContext->fGpu->writePixels(dstSurface, dstProxy->origin(), left, top, width, height,
652 srcColorType, buffer, rowBytes);
bsalomon81beccc2014-10-13 12:32:55 -0700653 }
bsalomon81beccc2014-10-13 12:32:55 -0700654 return true;
655}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000656
Brian Salomonc320b152018-02-20 14:05:36 -0500657bool GrContextPriv::readSurfacePixels(GrSurfaceContext* src, int left, int top, int width,
658 int height, GrColorType dstColorType,
659 SkColorSpace* dstColorSpace, void* buffer, size_t rowBytes,
660 uint32_t flags) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400661 bool useLegacyPath = false;
662#ifdef SK_LEGACY_GPU_PIXEL_OPS
663 useLegacyPath = true;
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400664#endif
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400665 // Newly added color types/configs are only supported by the new code path.
666 if (dstColorType == GrColorType::kRGB_888x ||
667 src->asSurfaceProxy()->config() == kRGB_888_GrPixelConfig) {
668 useLegacyPath = false;
669 }
670
671 if (!useLegacyPath) {
672 return this->readSurfacePixels2(src, left, top, width, height, dstColorType, dstColorSpace,
673 buffer, rowBytes, flags);
674 }
675
Brian Osmanb62ea222016-12-22 11:12:16 -0500676 // TODO: Color space conversion
677
Robert Phillipse78b7252017-04-06 07:59:41 -0400678 ASSERT_SINGLE_OWNER_PRIV
679 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400680 SkASSERT(src);
681 ASSERT_OWNED_PROXY_PRIV(src->asSurfaceProxy());
Brian Salomondcbb9d92017-07-19 10:53:20 -0400682 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "readSurfacePixels", fContext);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400683 SkASSERT(!(flags & kDontFlush_PixelOpsFlag));
684 if (flags & kDontFlush_PixelOpsFlag) {
685 return false;
686 }
bsalomon32ab2602015-09-09 18:57:49 -0700687
Robert Phillipse78b7252017-04-06 07:59:41 -0400688 // MDB TODO: delay this instantiation until later in the method
Robert Phillips6be756b2018-01-16 15:07:54 -0500689 if (!src->asSurfaceProxy()->instantiate(this->resourceProvider())) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400690 return false;
691 }
692
Robert Phillips16d8ec62017-07-27 16:16:25 -0400693 GrSurfaceProxy* srcProxy = src->asSurfaceProxy();
694 GrSurface* srcSurface = srcProxy->priv().peekSurface();
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400695
Brian Osmand2ca59a2017-04-13 14:03:57 -0400696 // The src is premul but the dst is unpremul -> unpremul the src after or as part of the read
697 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
Brian Salomonc320b152018-02-20 14:05:36 -0500698
699 if (!valid_pixel_conversion(dstColorType, srcProxy->config(), unpremul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400700 return false;
701 }
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400702 // The source is alpha-only but the dst is not. TODO: Make non-alpha channels in the dst be 0?
703 if (GrPixelConfigIsAlphaOnly(srcProxy->config()) && !GrColorTypeIsAlphaOnly(dstColorType)) {
704 return false;
705 }
706 // The source has no alpha, the dst is alpha-only. TODO: set all values in dst to 1?
707 if (GrPixelConfigIsOpaque(srcProxy->config()) && GrColorTypeIsAlphaOnly(dstColorType)) {
708 return false;
709 }
710 // Not clear if we should unpremul in this case.
711 if (!GrPixelConfigIsOpaque(srcProxy->config()) && !GrColorTypeHasAlpha(dstColorType)) {
712 return false;
713 }
Brian Osmand2ca59a2017-04-13 14:03:57 -0400714
Brian Osman409e74f2017-04-17 11:48:28 -0400715 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
716 // without any color spaces attached, and the caller wants us to unpremul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400717 bool useConfigConversionEffect =
Brian Salomonf3569f02017-10-24 12:52:33 -0400718 unpremul &&
719 pm_upm_must_round_trip(srcProxy->config(), src->colorSpaceInfo().colorSpace()) &&
Brian Salomonc320b152018-02-20 14:05:36 -0500720 pm_upm_must_round_trip(dstColorType, dstColorSpace);
Brian Osman409e74f2017-04-17 11:48:28 -0400721
722 // Are we going to try to unpremul as part of a draw? For the non-legacy case, we always allow
723 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
724 bool unpremulOnGpu = unpremul &&
725 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon6c6f6582015-09-10 08:12:46 -0700726
bsalomone8d21e82015-07-16 08:23:13 -0700727 // Adjust the params so that if we wind up using an intermediate surface we've already done
728 // all the trimming and the temporary can be the min size required.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400729 if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(),
Brian Salomonc320b152018-02-20 14:05:36 -0500730 GrColorTypeBytesPerPixel(dstColorType), &left, &top,
731 &width, &height, &buffer, &rowBytes)) {
bsalomone8d21e82015-07-16 08:23:13 -0700732 return false;
733 }
734
Brian Osman409e74f2017-04-17 11:48:28 -0400735 GrGpu::DrawPreference drawPreference = unpremulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
736 : GrGpu::kNoDraw_DrawPreference;
bsalomon39826022015-07-23 08:07:21 -0700737 GrGpu::ReadPixelTempDrawInfo tempDrawInfo;
Brian Salomon9b009bb2018-02-14 13:53:55 -0500738 GrSRGBConversion srgbConversion = determine_read_pixels_srgb_conversion(
739 GrPixelConfigIsSRGBEncoded(srcProxy->config()), src->colorSpaceInfo().colorSpace(),
Brian Salomonc320b152018-02-20 14:05:36 -0500740 dstColorType, dstColorSpace, *fContext->caps());
741
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400742 if (!fContext->fGpu->getReadPixelsInfo(srcSurface, srcProxy->origin(), width, height, rowBytes,
Brian Salomonc320b152018-02-20 14:05:36 -0500743 dstColorType, srgbConversion, &drawPreference,
Brian Salomon9b009bb2018-02-14 13:53:55 -0500744 &tempDrawInfo)) {
bsalomon39826022015-07-23 08:07:21 -0700745 return false;
746 }
bsalomon191bcc02014-11-14 11:31:13 -0800747
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400748 if (srcSurface->surfacePriv().hasPendingWrite()) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400749 this->flush(nullptr); // MDB TODO: tighten this
750 }
751
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400752 sk_sp<GrSurfaceProxy> proxyToRead = src->asSurfaceProxyRef();
bsalomon39826022015-07-23 08:07:21 -0700753 bool didTempDraw = false;
754 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomonb117ff12016-07-19 07:24:40 -0700755 if (SkBackingFit::kExact == tempDrawInfo.fTempSurfaceFit) {
bsalomon39826022015-07-23 08:07:21 -0700756 // We only respect this when the entire src is being read. Otherwise we can trigger too
757 // many odd ball texture sizes and trash the cache.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400758 if (width != srcSurface->width() || height != srcSurface->height()) {
bsalomonb117ff12016-07-19 07:24:40 -0700759 tempDrawInfo.fTempSurfaceFit= SkBackingFit::kApprox;
bsalomon39826022015-07-23 08:07:21 -0700760 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000761 }
brianosmandfe4f2e2016-07-21 13:28:36 -0700762 // TODO: Need to decide the semantics of this function for color spaces. Do we support
763 // conversion to a passed-in color space? For now, specifying nullptr means that this
Brian Salomon366093f2018-02-13 09:25:22 -0500764 // path will do no conversion, so it will match the behavior of the non-draw path. For
765 // now we simply infer an sRGB color space if the config is sRGB in order to avoid an
766 // illegal combination.
767 sk_sp<SkColorSpace> colorSpace;
768 if (GrPixelConfigIsSRGB(tempDrawInfo.fTempSurfaceDesc.fConfig)) {
769 colorSpace = SkColorSpace::MakeSRGB();
770 }
771 sk_sp<GrRenderTargetContext> tempRTC =
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500772 fContext->contextPriv().makeDeferredRenderTargetContext(
773 tempDrawInfo.fTempSurfaceFit,
Brian Salomon366093f2018-02-13 09:25:22 -0500774 tempDrawInfo.fTempSurfaceDesc.fWidth,
775 tempDrawInfo.fTempSurfaceDesc.fHeight,
776 tempDrawInfo.fTempSurfaceDesc.fConfig,
777 std::move(colorSpace),
778 tempDrawInfo.fTempSurfaceDesc.fSampleCnt,
779 GrMipMapped::kNo,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500780 kTopLeft_GrSurfaceOrigin);
Brian Osman693a5402016-10-27 15:13:22 -0400781 if (tempRTC) {
Greg Danielf44cb482018-02-27 14:26:32 -0500782 // Adding discard to appease vulkan validation warning about loading uninitialized data
783 // on draw
784 tempRTC->discard();
Robert Phillips67c18d62017-01-20 12:44:06 -0500785 SkMatrix textureMatrix = SkMatrix::MakeTrans(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400786 sk_sp<GrTextureProxy> proxy = src->asTextureProxyRef();
Brian Osman2240be92017-10-18 13:15:13 -0400787 auto fp = GrSimpleTextureEffect::Make(std::move(proxy), textureMatrix);
Brian Osman409e74f2017-04-17 11:48:28 -0400788 if (unpremulOnGpu) {
789 fp = fContext->createPMToUPMEffect(std::move(fp), useConfigConversionEffect);
790 // We no longer need to do this on CPU after the read back.
791 unpremul = false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000792 }
Brian Osman409e74f2017-04-17 11:48:28 -0400793 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400794 if (!fp) {
795 return false;
796 }
Brian Osman60cd57e2017-04-06 10:19:06 -0400797
Brian Osman409e74f2017-04-17 11:48:28 -0400798 GrPaint paint;
799 paint.addColorFragmentProcessor(std::move(fp));
800 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
801 paint.setAllowSRGBInputs(true);
802 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
803 tempRTC->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect,
804 nullptr);
805 proxyToRead = tempRTC->asTextureProxyRef();
806 left = 0;
807 top = 0;
808 didTempDraw = true;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000809 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000810 }
joshualitt5c55fef2014-10-31 14:04:35 -0700811
Robert Phillipse78b7252017-04-06 07:59:41 -0400812 if (!proxyToRead) {
813 return false;
814 }
815
bsalomon39826022015-07-23 08:07:21 -0700816 if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000817 return false;
818 }
Brian Salomonc320b152018-02-20 14:05:36 -0500819 GrColorType colorTypeToRead = dstColorType;
bsalomon39826022015-07-23 08:07:21 -0700820 if (didTempDraw) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400821 this->flushSurfaceWrites(proxyToRead.get());
Brian Salomonc320b152018-02-20 14:05:36 -0500822 colorTypeToRead = tempDrawInfo.fReadColorType;
bsalomon39826022015-07-23 08:07:21 -0700823 }
Robert Phillips09dfc472017-09-13 15:25:47 -0400824
Robert Phillips6be756b2018-01-16 15:07:54 -0500825 if (!proxyToRead->instantiate(this->resourceProvider())) {
Robert Phillips09dfc472017-09-13 15:25:47 -0400826 return false;
827 }
828
829 GrSurface* surfaceToRead = proxyToRead->priv().peekSurface();
830
Brian Salomonc320b152018-02-20 14:05:36 -0500831 if (!fContext->fGpu->readPixels(surfaceToRead, proxyToRead->origin(), left, top, width, height,
832 colorTypeToRead, buffer, rowBytes)) {
bsalomon39826022015-07-23 08:07:21 -0700833 return false;
834 }
835
836 // Perform umpremul conversion if we weren't able to perform it as a draw.
837 if (unpremul) {
Brian Salomonc320b152018-02-20 14:05:36 -0500838 SkColorType colorType = GrColorTypeToSkColorType(dstColorType);
839 if (kUnknown_SkColorType == colorType || 4 != SkColorTypeBytesPerPixel(colorType)) {
reed@google.com7111d462014-03-25 16:20:24 +0000840 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000841 }
reed@google.com7111d462014-03-25 16:20:24 +0000842
Matt Sarettc7b29082017-02-09 16:22:39 -0500843 for (int y = 0; y < height; y++) {
844 SkUnpremultiplyRow<false>((uint32_t*) buffer, (const uint32_t*) buffer, width);
845 buffer = SkTAddOffset<void>(buffer, rowBytes);
846 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000847 }
848 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000849}
850
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500851bool GrContextPriv::writeSurfacePixels2(GrSurfaceContext* dst, int left, int top, int width,
852 int height, GrColorType srcColorType,
853 SkColorSpace* srcColorSpace, const void* buffer,
854 size_t rowBytes, uint32_t pixelOpsFlags) {
855 ASSERT_SINGLE_OWNER_PRIV
856 RETURN_FALSE_IF_ABANDONED_PRIV
857 SkASSERT(dst);
858 SkASSERT(buffer);
859 ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy());
860 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels2", fContext);
861
862 if (GrColorType::kUnknown == srcColorType) {
863 return false;
864 }
865
866 if (!dst->asSurfaceProxy()->instantiate(this->resourceProvider())) {
867 return false;
868 }
869
870 GrSurfaceProxy* dstProxy = dst->asSurfaceProxy();
871 GrSurface* dstSurface = dstProxy->priv().peekSurface();
872
873 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
874 GrColorTypeBytesPerPixel(srcColorType), &left, &top,
875 &width, &height, &buffer, &rowBytes)) {
876 return false;
877 }
878
Brian Salomon3d86a192018-02-27 16:46:11 -0500879 if (!fContext->caps()->surfaceSupportsWritePixels(dstSurface)) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400880 // We don't expect callers that are skipping flushes to require an intermediate draw.
881 SkASSERT(!(pixelOpsFlags & kDontFlush_PixelOpsFlag));
882 if (pixelOpsFlags & kDontFlush_PixelOpsFlag) {
883 return false;
884 }
885
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500886 GrSurfaceDesc desc;
887 desc.fConfig = dstProxy->config();
888 desc.fWidth = width;
889 desc.fHeight = height;
890 desc.fSampleCnt = 1;
Brian Salomon2a4f9832018-03-03 22:43:43 -0500891 auto tempProxy = this->proxyProvider()->createProxy(
892 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kApprox, SkBudgeted::kYes);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500893 if (!tempProxy) {
894 return false;
895 }
896 auto tempCtx = this->drawingManager()->makeTextureContext(
897 tempProxy, dst->colorSpaceInfo().refColorSpace());
898 if (!tempCtx) {
899 return false;
900 }
901 if (!this->writeSurfacePixels2(tempCtx.get(), 0, 0, width, height, srcColorType,
902 srcColorSpace, buffer, rowBytes, pixelOpsFlags)) {
903 return false;
904 }
Brian Salomon3d86a192018-02-27 16:46:11 -0500905 return dst->copy(tempProxy.get(), SkIRect::MakeWH(width, height), {left, top});
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500906 }
907
908 // TODO: Make GrSurfaceContext know its alpha type and pass src buffer's alpha type.
909 bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
910 bool convert = premul;
911
912 if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) {
913 return false;
914 }
915
916 GrColorType allowedColorType =
917 fContext->caps()->supportedWritePixelsColorType(dstProxy->config(), srcColorType);
918 convert = convert || (srcColorType != allowedColorType);
919
920 if (!dst->colorSpaceInfo().colorSpace()) {
921 // "Legacy" mode - no color space conversions.
922 srcColorSpace = nullptr;
923 }
924 convert = convert || !SkColorSpace::Equals(srcColorSpace, dst->colorSpaceInfo().colorSpace());
925
926 std::unique_ptr<char[]> tempBuffer;
927 if (convert) {
928 auto srcSkColorType = GrColorTypeToSkColorType(srcColorType);
929 auto dstSkColorType = GrColorTypeToSkColorType(allowedColorType);
930 if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) {
931 return false;
932 }
933 auto srcAlphaType = premul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
934 SkPixmap src(SkImageInfo::Make(width, height, srcSkColorType, srcAlphaType,
935 sk_ref_sp(srcColorSpace)),
936 buffer, rowBytes);
937 auto tempSrcII = SkImageInfo::Make(width, height, dstSkColorType, kPremul_SkAlphaType,
938 dst->colorSpaceInfo().refColorSpace());
939 auto size = tempSrcII.computeMinByteSize();
940 if (!size) {
941 return false;
942 }
943 tempBuffer.reset(new char[size]);
944 SkPixmap tempSrc(tempSrcII, tempBuffer.get(), tempSrcII.minRowBytes());
945 if (!src.readPixels(tempSrc)) {
946 return false;
947 }
948 srcColorType = allowedColorType;
949 buffer = tempSrc.addr();
950 rowBytes = tempSrc.rowBytes();
951 if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
952 std::unique_ptr<char[]> row(new char[rowBytes]);
953 for (int y = 0; y < height / 2; ++y) {
954 memcpy(row.get(), tempSrc.addr(0, y), rowBytes);
955 memcpy(tempSrc.writable_addr(0, y), tempSrc.addr(0, height - 1 - y), rowBytes);
956 memcpy(tempSrc.writable_addr(0, height - 1 - y), row.get(), rowBytes);
957 }
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400958 top = dstSurface->height() - top - height;
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500959 }
960 } else if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
961 size_t trimRowBytes = GrColorTypeBytesPerPixel(srcColorType) * width;
962 tempBuffer.reset(new char[trimRowBytes * height]);
963 char* dst = reinterpret_cast<char*>(tempBuffer.get()) + trimRowBytes * (height - 1);
964 const char* src = reinterpret_cast<const char*>(buffer);
965 for (int i = 0; i < height; ++i, src += rowBytes, dst -= trimRowBytes) {
966 memcpy(dst, src, trimRowBytes);
967 }
968 buffer = tempBuffer.get();
969 rowBytes = trimRowBytes;
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400970 top = dstSurface->height() - top - height;
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500971 }
972
973 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) {
974 this->flush(nullptr); // MDB TODO: tighten this
975 }
976
977 return this->getGpu()->writePixels(dstSurface, left, top, width, height, srcColorType, buffer,
978 rowBytes);
979}
980
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400981bool GrContextPriv::readSurfacePixels2(GrSurfaceContext* src, int left, int top, int width,
982 int height, GrColorType dstColorType,
983 SkColorSpace* dstColorSpace, void* buffer, size_t rowBytes,
984 uint32_t flags) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400985 ASSERT_SINGLE_OWNER_PRIV
986 RETURN_FALSE_IF_ABANDONED_PRIV
987 SkASSERT(src);
988 SkASSERT(buffer);
989 ASSERT_OWNED_PROXY_PRIV(src->asSurfaceProxy());
990 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "readSurfacePixels2", fContext);
991
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400992 SkASSERT(!(flags & kDontFlush_PixelOpsFlag));
993 if (flags & kDontFlush_PixelOpsFlag) {
994 return false;
995 }
996
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400997 // MDB TODO: delay this instantiation until later in the method
998 if (!src->asSurfaceProxy()->instantiate(this->resourceProvider())) {
999 return false;
1000 }
1001
1002 GrSurfaceProxy* srcProxy = src->asSurfaceProxy();
1003 GrSurface* srcSurface = srcProxy->priv().peekSurface();
1004
1005 if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(),
1006 GrColorTypeBytesPerPixel(dstColorType), &left, &top,
1007 &width, &height, &buffer, &rowBytes)) {
1008 return false;
1009 }
1010
1011 // TODO: Make GrSurfaceContext know its alpha type and pass dst buffer's alpha type.
1012 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
1013 bool convert = unpremul;
1014
1015 if (!valid_pixel_conversion(dstColorType, srcProxy->config(), unpremul)) {
1016 return false;
1017 }
1018
1019 if (!fContext->caps()->surfaceSupportsReadPixels(srcSurface)) {
1020 GrSurfaceDesc desc;
1021 desc.fConfig = srcProxy->config();
1022 desc.fWidth = width;
1023 desc.fHeight = height;
1024 desc.fSampleCnt = 1;
1025 auto tempProxy = this->proxyProvider()->createProxy(
1026 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kApprox, SkBudgeted::kYes);
1027 if (!tempProxy) {
1028 return false;
1029 }
1030 auto tempCtx = this->drawingManager()->makeTextureContext(
1031 tempProxy, src->colorSpaceInfo().refColorSpace());
1032 if (!tempCtx) {
1033 return false;
1034 }
1035 if (!tempCtx->copy(srcProxy, {left, top, width, height}, {0, 0})) {
1036 return false;
1037 }
1038 return this->readSurfacePixels2(tempCtx.get(), 0, 0, width, height, dstColorType,
1039 dstColorSpace, buffer, rowBytes, flags);
1040 }
1041
1042 bool flip = srcProxy->origin() == kBottomLeft_GrSurfaceOrigin;
1043 if (flip) {
1044 top = srcSurface->height() - top - height;
1045 }
1046
1047 GrColorType allowedColorType =
1048 fContext->caps()->supportedReadPixelsColorType(srcProxy->config(), dstColorType);
1049 convert = convert || (dstColorType != allowedColorType);
1050
1051 if (!src->colorSpaceInfo().colorSpace()) {
1052 // "Legacy" mode - no color space conversions.
1053 dstColorSpace = nullptr;
1054 }
1055 convert = convert || !SkColorSpace::Equals(dstColorSpace, src->colorSpaceInfo().colorSpace());
1056
1057 SkAutoPixmapStorage tempPixmap;
1058 SkPixmap finalPixmap;
1059 if (convert) {
1060 SkColorType srcSkColorType = GrColorTypeToSkColorType(allowedColorType);
1061 SkColorType dstSkColorType = GrColorTypeToSkColorType(dstColorType);
1062 if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) {
1063 return false;
1064 }
1065 auto tempAT = SkColorTypeIsAlwaysOpaque(srcSkColorType) ? kOpaque_SkAlphaType
1066 : kPremul_SkAlphaType;
1067 auto tempII = SkImageInfo::Make(width, height, srcSkColorType, tempAT,
1068 src->colorSpaceInfo().refColorSpace());
1069 SkASSERT(!unpremul || !SkColorTypeIsAlwaysOpaque(dstSkColorType));
1070 auto finalAT = SkColorTypeIsAlwaysOpaque(srcSkColorType)
1071 ? kOpaque_SkAlphaType
1072 : unpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
1073 auto finalII =
1074 SkImageInfo::Make(width, height, dstSkColorType, finalAT, sk_ref_sp(dstColorSpace));
1075 if (!SkImageInfoValidConversion(finalII, tempII)) {
1076 return false;
1077 }
1078 tempPixmap.alloc(tempII);
1079 finalPixmap.reset(finalII, buffer, rowBytes);
1080 buffer = tempPixmap.writable_addr();
1081 rowBytes = tempPixmap.rowBytes();
1082 }
1083
1084 if (srcSurface->surfacePriv().hasPendingWrite()) {
1085 this->flush(nullptr); // MDB TODO: tighten this
1086 }
1087
1088 if (!fContext->fGpu->readPixels(srcSurface, left, top, width, height, allowedColorType, buffer,
1089 rowBytes)) {
1090 return false;
1091 }
1092
1093 if (flip) {
1094 size_t trimRowBytes = GrColorTypeBytesPerPixel(allowedColorType) * width;
1095 std::unique_ptr<char[]> row(new char[trimRowBytes]);
1096 char* upper = reinterpret_cast<char*>(buffer);
1097 char* lower = reinterpret_cast<char*>(buffer) + (height - 1) * rowBytes;
1098 for (int y = 0; y < height / 2; ++y, upper += rowBytes, lower -= rowBytes) {
1099 memcpy(row.get(), upper, trimRowBytes);
1100 memcpy(upper, lower, trimRowBytes);
1101 memcpy(lower, row.get(), trimRowBytes);
1102 }
1103 }
1104 if (convert) {
1105 if (!tempPixmap.readPixels(finalPixmap)) {
1106 return false;
1107 }
1108 }
1109 return true;
1110}
1111
Robert Phillips7ee385e2017-03-30 08:02:11 -04001112void GrContextPriv::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) {
1113 ASSERT_SINGLE_OWNER_PRIV
1114 RETURN_IF_ABANDONED_PRIV
1115 SkASSERT(proxy);
1116 ASSERT_OWNED_PROXY_PRIV(proxy);
Greg Daniel51316782017-08-02 15:10:09 +00001117 fContext->fDrawingManager->prepareSurfaceForExternalIO(proxy, 0, nullptr);
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001118}
1119
Robert Phillips7ee385e2017-03-30 08:02:11 -04001120void GrContextPriv::flushSurfaceWrites(GrSurfaceProxy* proxy) {
1121 ASSERT_SINGLE_OWNER_PRIV
1122 RETURN_IF_ABANDONED_PRIV
1123 SkASSERT(proxy);
1124 ASSERT_OWNED_PROXY_PRIV(proxy);
1125 if (proxy->priv().hasPendingWrite()) {
1126 this->flush(proxy);
bsalomonf80bfed2014-10-07 05:56:02 -07001127 }
1128}
1129
Robert Phillips7ee385e2017-03-30 08:02:11 -04001130void GrContextPriv::flushSurfaceIO(GrSurfaceProxy* proxy) {
1131 ASSERT_SINGLE_OWNER_PRIV
1132 RETURN_IF_ABANDONED_PRIV
1133 SkASSERT(proxy);
1134 ASSERT_OWNED_PROXY_PRIV(proxy);
1135 if (proxy->priv().hasPendingIO()) {
1136 this->flush(proxy);
ajuma95243eb2016-08-24 08:19:02 -07001137 }
1138}
1139
bsalomon@google.com27847de2011-02-22 20:59:41 +00001140////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +00001141
Robert Phillips2c862492017-01-18 10:08:39 -05001142sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy,
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -05001143 sk_sp<SkColorSpace> colorSpace,
1144 const SkSurfaceProps* props) {
Brian Osman45580d32016-11-23 09:37:01 -05001145 ASSERT_SINGLE_OWNER_PRIV
1146
Brian Salomon366093f2018-02-13 09:25:22 -05001147 // sRGB pixel configs may only be used with near-sRGB gamma color spaces.
1148 if (GrPixelConfigIsSRGB(proxy->config())) {
1149 if (!colorSpace || !colorSpace->gammaCloseToSRGB()) {
1150 return nullptr;
1151 }
1152 }
Brian Osman45580d32016-11-23 09:37:01 -05001153 if (proxy->asRenderTargetProxy()) {
Robert Phillips2c862492017-01-18 10:08:39 -05001154 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -05001155 std::move(colorSpace), props);
Brian Osman45580d32016-11-23 09:37:01 -05001156 } else {
1157 SkASSERT(proxy->asTextureProxy());
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -05001158 SkASSERT(!props);
Robert Phillips2c862492017-01-18 10:08:39 -05001159 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Brian Osman45580d32016-11-23 09:37:01 -05001160 }
1161}
1162
Robert Phillipse2f7d182016-12-15 09:23:05 -05001163sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrSurfaceDesc& dstDesc,
Brian Salomon2a4f9832018-03-03 22:43:43 -05001164 GrSurfaceOrigin origin,
Greg Daniel65c7f662017-10-30 13:39:09 -04001165 GrMipMapped mipMapped,
Robert Phillipse2f7d182016-12-15 09:23:05 -05001166 SkBackingFit fit,
Brian Salomon366093f2018-02-13 09:25:22 -05001167 SkBudgeted isDstBudgeted,
1168 sk_sp<SkColorSpace> colorSpace,
1169 const SkSurfaceProps* props) {
Greg Daniel65c7f662017-10-30 13:39:09 -04001170 sk_sp<GrTextureProxy> proxy;
1171 if (GrMipMapped::kNo == mipMapped) {
Brian Salomon2a4f9832018-03-03 22:43:43 -05001172 proxy = this->proxyProvider()->createProxy(dstDesc, origin, fit, isDstBudgeted);
Greg Daniel65c7f662017-10-30 13:39:09 -04001173 } else {
1174 SkASSERT(SkBackingFit::kExact == fit);
Brian Salomon2a4f9832018-03-03 22:43:43 -05001175 proxy = this->proxyProvider()->createMipMapProxy(dstDesc, origin, isDstBudgeted);
Greg Daniel65c7f662017-10-30 13:39:09 -04001176 }
Robert Phillips77b3f322017-01-31 18:24:12 -05001177 if (!proxy) {
1178 return nullptr;
1179 }
Robert Phillipse2f7d182016-12-15 09:23:05 -05001180
Greg Daniel55fa6472018-03-16 16:13:10 -04001181 sk_sp<GrSurfaceContext> sContext = this->makeWrappedSurfaceContext(std::move(proxy),
1182 std::move(colorSpace),
1183 props);
1184 if (sContext && sContext->asRenderTargetContext()) {
1185 sContext->asRenderTargetContext()->discard();
1186 }
1187
1188 return sContext;
Robert Phillipse2f7d182016-12-15 09:23:05 -05001189}
1190
Brian Salomond17f6582017-07-19 18:28:58 -04001191sk_sp<GrTextureContext> GrContextPriv::makeBackendTextureContext(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +00001192 GrSurfaceOrigin origin,
Brian Osmanc1e37052017-03-09 14:19:20 -05001193 sk_sp<SkColorSpace> colorSpace) {
Robert Phillips26caf892017-01-27 10:58:31 -05001194 ASSERT_SINGLE_OWNER_PRIV
1195
Brian Salomon7578f3e2018-03-07 14:39:54 -05001196 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->wrapBackendTexture(tex, origin);
Robert Phillips77b3f322017-01-31 18:24:12 -05001197 if (!proxy) {
1198 return nullptr;
1199 }
Robert Phillips26caf892017-01-27 10:58:31 -05001200
Brian Salomond17f6582017-07-19 18:28:58 -04001201 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Robert Phillips26caf892017-01-27 10:58:31 -05001202}
1203
Brian Osman11052242016-10-27 14:47:55 -04001204sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +00001205 const GrBackendTexture& tex,
1206 GrSurfaceOrigin origin,
1207 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -04001208 sk_sp<SkColorSpace> colorSpace,
Brian Osmanc1e37052017-03-09 14:19:20 -05001209 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -07001210 ASSERT_SINGLE_OWNER_PRIV
Brian Salomonbdecacf2018-02-02 20:32:49 -05001211 SkASSERT(sampleCnt > 0);
robertphillips4fd74ae2016-08-03 14:26:53 -07001212
Brian Salomon7578f3e2018-03-07 14:39:54 -05001213 sk_sp<GrTextureProxy> proxy(
1214 this->proxyProvider()->wrapRenderableBackendTexture(tex, origin, sampleCnt));
Robert Phillips77b3f322017-01-31 18:24:12 -05001215 if (!proxy) {
1216 return nullptr;
1217 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001218
Robert Phillips37430132016-11-09 06:50:43 -05001219 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Brian Osman11052242016-10-27 14:47:55 -04001220 std::move(colorSpace), props);
robertphillips4fd74ae2016-08-03 14:26:53 -07001221}
1222
Brian Osman11052242016-10-27 14:47:55 -04001223sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +00001224 const GrBackendRenderTarget& backendRT,
1225 GrSurfaceOrigin origin,
robertphillips4fd74ae2016-08-03 14:26:53 -07001226 sk_sp<SkColorSpace> colorSpace,
1227 const SkSurfaceProps* surfaceProps) {
1228 ASSERT_SINGLE_OWNER_PRIV
1229
Brian Salomon7578f3e2018-03-07 14:39:54 -05001230 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->wrapBackendRenderTarget(backendRT, origin);
Robert Phillips77b3f322017-01-31 18:24:12 -05001231 if (!proxy) {
1232 return nullptr;
1233 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001234
Robert Phillips37430132016-11-09 06:50:43 -05001235 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001236 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -04001237 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -07001238}
1239
Brian Osman11052242016-10-27 14:47:55 -04001240sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +00001241 const GrBackendTexture& tex,
1242 GrSurfaceOrigin origin,
1243 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -07001244 sk_sp<SkColorSpace> colorSpace,
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001245 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -07001246 ASSERT_SINGLE_OWNER_PRIV
Brian Salomonbdecacf2018-02-02 20:32:49 -05001247 SkASSERT(sampleCnt > 0);
Brian Salomon7578f3e2018-03-07 14:39:54 -05001248 sk_sp<GrSurfaceProxy> proxy(
1249 this->proxyProvider()->wrapBackendTextureAsRenderTarget(tex, origin, sampleCnt));
Robert Phillips77b3f322017-01-31 18:24:12 -05001250 if (!proxy) {
1251 return nullptr;
1252 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001253
Robert Phillips37430132016-11-09 06:50:43 -05001254 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001255 std::move(colorSpace),
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001256 props);
robertphillips77a2e522015-10-17 07:43:27 -07001257}
1258
Chris Daltonfe199b72017-05-05 11:26:15 -04001259void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
1260 fContext->fDrawingManager->addOnFlushCallbackObject(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -04001261}
1262
Robert Phillips62000362018-02-01 09:10:04 -05001263void GrContextPriv::moveOpListsToDDL(SkDeferredDisplayList* ddl) {
1264 fContext->fDrawingManager->moveOpListsToDDL(ddl);
1265}
1266
1267void GrContextPriv::copyOpListsFromDDL(const SkDeferredDisplayList* ddl,
1268 GrRenderTargetProxy* newDest) {
1269 fContext->fDrawingManager->copyOpListsFromDDL(ddl, newDest);
1270}
1271
robertphillips48fde9c2016-09-06 05:20:20 -07001272static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
Brian Osman78f20e02017-01-12 10:28:01 -05001273 switch (config) {
1274 case kAlpha_8_GrPixelConfig:
1275 case kRGB_565_GrPixelConfig:
1276 case kRGBA_4444_GrPixelConfig:
1277 case kBGRA_8888_GrPixelConfig:
Brian Osman10fc6fd2018-03-02 11:01:10 -05001278 case kRGBA_1010102_GrPixelConfig:
Brian Osman78f20e02017-01-12 10:28:01 -05001279 return kRGBA_8888_GrPixelConfig;
1280 case kSBGRA_8888_GrPixelConfig:
1281 return kSRGBA_8888_GrPixelConfig;
1282 case kAlpha_half_GrPixelConfig:
1283 return kRGBA_half_GrPixelConfig;
1284 default:
1285 return kUnknown_GrPixelConfig;
1286 }
robertphillips48fde9c2016-09-06 05:20:20 -07001287}
1288
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001289sk_sp<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContextWithFallback(
robertphillipsd728f0c2016-11-21 11:05:03 -08001290 SkBackingFit fit,
1291 int width, int height,
1292 GrPixelConfig config,
1293 sk_sp<SkColorSpace> colorSpace,
1294 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -04001295 GrMipMapped mipMapped,
robertphillipsd728f0c2016-11-21 11:05:03 -08001296 GrSurfaceOrigin origin,
1297 const SkSurfaceProps* surfaceProps,
1298 SkBudgeted budgeted) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05001299 SkASSERT(sampleCnt > 0);
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001300 if (0 == fContext->caps()->getRenderTargetSampleCount(sampleCnt, config)) {
robertphillipsd728f0c2016-11-21 11:05:03 -08001301 config = GrPixelConfigFallback(config);
1302 }
1303
1304 return this->makeDeferredRenderTargetContext(fit, width, height, config, std::move(colorSpace),
Greg Daniel45d63032017-10-30 13:41:26 -04001305 sampleCnt, mipMapped, origin, surfaceProps,
Greg Daniele1da1d92017-10-06 15:59:27 -04001306 budgeted);
robertphillipsd728f0c2016-11-21 11:05:03 -08001307}
1308
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001309sk_sp<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContext(
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001310 SkBackingFit fit,
1311 int width, int height,
1312 GrPixelConfig config,
1313 sk_sp<SkColorSpace> colorSpace,
1314 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -04001315 GrMipMapped mipMapped,
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001316 GrSurfaceOrigin origin,
1317 const SkSurfaceProps* surfaceProps,
1318 SkBudgeted budgeted) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05001319 SkASSERT(sampleCnt > 0);
Brian Salomon79e4d1b2017-07-13 11:17:11 -04001320 if (this->abandoned()) {
1321 return nullptr;
1322 }
1323
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001324 GrSurfaceDesc desc;
1325 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001326 desc.fWidth = width;
1327 desc.fHeight = height;
1328 desc.fConfig = config;
1329 desc.fSampleCnt = sampleCnt;
1330
Greg Daniele1da1d92017-10-06 15:59:27 -04001331 sk_sp<GrTextureProxy> rtp;
Greg Daniel45d63032017-10-30 13:41:26 -04001332 if (GrMipMapped::kNo == mipMapped) {
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001333 rtp = fContext->fProxyProvider->createProxy(desc, origin, fit, budgeted);
Greg Daniele1da1d92017-10-06 15:59:27 -04001334 } else {
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001335 rtp = fContext->fProxyProvider->createMipMapProxy(desc, origin, budgeted);
Greg Daniele1da1d92017-10-06 15:59:27 -04001336 }
Robert Phillips08c5ec72017-01-30 12:26:47 -05001337 if (!rtp) {
1338 return nullptr;
1339 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001340
Robert Phillips1119dc32017-04-11 12:54:57 -04001341 sk_sp<GrRenderTargetContext> renderTargetContext(
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001342 fContext->fDrawingManager->makeRenderTargetContext(std::move(rtp),
1343 std::move(colorSpace),
1344 surfaceProps));
Robert Phillips1119dc32017-04-11 12:54:57 -04001345 if (!renderTargetContext) {
1346 return nullptr;
1347 }
1348
1349 renderTargetContext->discard();
1350
1351 return renderTargetContext;
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001352}
1353
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001354bool GrContextPriv::abandoned() const {
1355 ASSERT_SINGLE_OWNER_PRIV
1356 return fContext->fDrawingManager->wasAbandoned();
robertphillips77a2e522015-10-17 07:43:27 -07001357}
1358
Brian Salomonaff329b2017-08-11 09:40:37 -04001359std::unique_ptr<GrFragmentProcessor> GrContext::createPMToUPMEffect(
1360 std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) {
Robert Phillips757914d2017-01-25 15:48:30 -05001361 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -04001362 // We have specialized effects that guarantee round-trip conversion for some formats
1363 if (useConfigConversionEffect) {
1364 // We should have already called this->validPMUPMConversionExists() in this case
1365 SkASSERT(fDidTestPMConversions);
1366 // ...and it should have succeeded
1367 SkASSERT(this->validPMUPMConversionExists());
1368
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001369 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
Brian Osman2d2da4f2017-04-12 17:07:22 -04001370 } else {
1371 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
1372 // explicitly round the results. Just do the obvious, naive thing in the shader.
1373 return GrFragmentProcessor::UnpremulOutput(std::move(fp));
Robert Phillips757914d2017-01-25 15:48:30 -05001374 }
1375}
1376
Brian Salomonaff329b2017-08-11 09:40:37 -04001377std::unique_ptr<GrFragmentProcessor> GrContext::createUPMToPMEffect(
1378 std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) {
joshualitt1de610a2016-01-06 08:26:09 -08001379 ASSERT_SINGLE_OWNER
Brian Osman2d2da4f2017-04-12 17:07:22 -04001380 // We have specialized effects that guarantee round-trip conversion for these formats
Brian Osman409e74f2017-04-17 11:48:28 -04001381 if (useConfigConversionEffect) {
1382 // We should have already called this->validPMUPMConversionExists() in this case
1383 SkASSERT(fDidTestPMConversions);
1384 // ...and it should have succeeded
1385 SkASSERT(this->validPMUPMConversionExists());
1386
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001387 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
Brian Osman2d2da4f2017-04-12 17:07:22 -04001388 } else {
1389 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
1390 // explicitly round the results. Just do the obvious, naive thing in the shader.
1391 return GrFragmentProcessor::PremulOutput(std::move(fp));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001392 }
1393}
1394
Brian Osman409e74f2017-04-17 11:48:28 -04001395bool GrContext::validPMUPMConversionExists() {
joshualitt1de610a2016-01-06 08:26:09 -08001396 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -04001397 if (!fDidTestPMConversions) {
Brian Osman28804f32017-04-20 10:24:36 -04001398 fPMUPMConversionsRoundTrip = GrConfigConversionEffect::TestForPreservingPMConversions(this);
Brian Osman409e74f2017-04-17 11:48:28 -04001399 fDidTestPMConversions = true;
1400 }
1401
bsalomon636e8022015-07-29 06:08:46 -07001402 // The PM<->UPM tests fail or succeed together so we only need to check one.
Brian Osman28804f32017-04-20 10:24:36 -04001403 return fPMUPMConversionsRoundTrip;
bsalomon636e8022015-07-29 06:08:46 -07001404}
1405
bsalomon37f9a262015-02-02 13:00:10 -08001406//////////////////////////////////////////////////////////////////////////////
1407
Robert Phillipsfc711a22018-02-13 17:03:00 -05001408// DDL TODO: remove 'maxResources'
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001409void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -08001410 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001411 if (maxResources) {
1412 *maxResources = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -08001413 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001414 if (maxResourceBytes) {
1415 *maxResourceBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -08001416 }
1417}
1418
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001419void GrContext::setResourceCacheLimits(int maxResources, size_t maxResourceBytes) {
joshualitt1de610a2016-01-06 08:26:09 -08001420 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001421 fResourceCache->setLimits(maxResources, maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -08001422}
1423
ericrk0a5fa482015-09-15 14:16:10 -07001424//////////////////////////////////////////////////////////////////////////////
ericrk0a5fa482015-09-15 14:16:10 -07001425void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -08001426 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -07001427 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
1428}
Brian Osman71a18892017-08-10 10:23:25 -04001429
1430//////////////////////////////////////////////////////////////////////////////
1431
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001432SkString GrContextPriv::dump() const {
Brian Osman71a18892017-08-10 10:23:25 -04001433 SkDynamicMemoryWStream stream;
1434 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
1435 writer.beginObject();
1436
1437 static const char* kBackendStr[] = {
1438 "Metal",
1439 "OpenGL",
1440 "Vulkan",
1441 "Mock",
1442 };
1443 GR_STATIC_ASSERT(0 == kMetal_GrBackend);
1444 GR_STATIC_ASSERT(1 == kOpenGL_GrBackend);
1445 GR_STATIC_ASSERT(2 == kVulkan_GrBackend);
1446 GR_STATIC_ASSERT(3 == kMock_GrBackend);
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001447 writer.appendString("backend", kBackendStr[fContext->fBackend]);
Brian Osman71a18892017-08-10 10:23:25 -04001448
1449 writer.appendName("caps");
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001450 fContext->fCaps->dumpJSON(&writer);
Brian Osman71a18892017-08-10 10:23:25 -04001451
1452 writer.appendName("gpu");
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001453 fContext->fGpu->dumpJSON(&writer);
Brian Osman71a18892017-08-10 10:23:25 -04001454
1455 // Flush JSON to the memory stream
1456 writer.endObject();
1457 writer.flush();
1458
1459 // Null terminate the JSON data in the memory stream
1460 stream.write8(0);
1461
1462 // Allocate a string big enough to hold all the data, then copy out of the stream
1463 SkString result(stream.bytesWritten());
1464 stream.copyToAndReset(result.writable_str());
1465 return result;
1466}