blob: 510225e01eb4c4c5ed0e435d27f570af71373530 [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"
Matt Sarett485c4992017-02-14 14:18:27 -050029#include "SkConvertPixels.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 Osman71a18892017-08-10 10:23:25 -040033#include "SkJSONWriter.h"
Brian Osman51279982017-08-23 10:12:00 -040034#include "SkMakeUnique.h"
35#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"
Brian Salomon5f33a8c2018-02-26 14:32:39 -050038#include "text/GrTextBlobCache.h"
Greg Danielb76a72a2017-07-13 15:07:54 -040039
Robert Phillipse78b7252017-04-06 07:59:41 -040040#define ASSERT_OWNED_PROXY(P) \
41SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == this)
Robert Phillips7ee385e2017-03-30 08:02:11 -040042#define ASSERT_OWNED_PROXY_PRIV(P) \
43SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == fContext)
44
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000045#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt1de610a2016-01-06 08:26:09 -080046#define ASSERT_SINGLE_OWNER \
47 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fSingleOwner);)
robertphillips4fd74ae2016-08-03 14:26:53 -070048#define ASSERT_SINGLE_OWNER_PRIV \
49 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fContext->fSingleOwner);)
robertphillips7761d612016-05-16 09:14:53 -070050#define RETURN_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return; }
Robert Phillips7ee385e2017-03-30 08:02:11 -040051#define RETURN_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return; }
robertphillips7761d612016-05-16 09:14:53 -070052#define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return false; }
Robert Phillipse78b7252017-04-06 07:59:41 -040053#define RETURN_FALSE_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return false; }
robertphillips7761d612016-05-16 09:14:53 -070054#define RETURN_NULL_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000055
robertphillipsea461502015-05-26 11:38:03 -070056////////////////////////////////////////////////////////////////////////////////
57
joshualitt0acd0d32015-05-07 08:23:19 -070058static int32_t gNextID = 1;
59static int32_t next_id() {
60 int32_t id;
61 do {
62 id = sk_atomic_inc(&gNextID);
63 } while (id == SK_InvalidGenID);
64 return id;
65}
66
Robert Phillipsfde6fa02018-03-02 08:53:14 -050067GrContext::GrContext(GrBackend backend, int32_t id)
68 : fBackend(backend)
69 , fUniqueID(SK_InvalidGenID == id ? next_id() : id) {
halcanary96fcdcc2015-08-27 07:41:13 -070070 fResourceCache = nullptr;
71 fResourceProvider = nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050072 fProxyProvider = nullptr;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050073 fGlyphCache = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000074}
75
Robert Phillipsfde6fa02018-03-02 08:53:14 -050076bool GrContext::initCommon(const GrContextOptions& options) {
Greg Danielb76a72a2017-07-13 15:07:54 -040077 ASSERT_SINGLE_OWNER
Robert Phillipsfde6fa02018-03-02 08:53:14 -050078 SkASSERT(fCaps); // needs to have been initialized by derived classes
79 SkASSERT(fThreadSafeProxy); // needs to have been initialized by derived classes
Robert Phillips88260b52018-01-19 12:56:09 -050080
81 if (fGpu) {
82 fCaps = fGpu->refCaps();
83 fResourceCache = new GrResourceCache(fCaps.get(), fUniqueID);
Robert Phillips4150eea2018-02-07 17:08:21 -050084 fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, &fSingleOwner,
85 options.fExplicitlyAllocateGPUResources);
Robert Phillips88260b52018-01-19 12:56:09 -050086 }
87
Robert Phillips1afd4cd2018-01-08 13:40:32 -050088 fProxyProvider = new GrProxyProvider(fResourceProvider, fResourceCache, fCaps, &fSingleOwner);
Robert Phillips88260b52018-01-19 12:56:09 -050089
90 if (fResourceCache) {
91 fResourceCache->setProxyProvider(fProxyProvider);
92 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050093
Robert Phillips41a3b872018-03-09 12:00:34 -050094 fTextureStripAtlasManager.reset(new GrTextureStripAtlasManager);
95
Brian Osman46da1cc2017-02-14 14:15:48 -050096 fDisableGpuYUVConversion = options.fDisableGpuYUVConversion;
Brian Osman8a83ca42018-02-12 14:32:17 -050097 fSharpenMipmappedTextures = options.fSharpenMipmappedTextures;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000098 fDidTestPMConversions = false;
99
bsalomon6b2552f2016-09-15 13:50:26 -0700100 GrPathRendererChain::Options prcOptions;
bsalomon39ef7fb2016-09-21 11:16:05 -0700101 prcOptions.fAllowPathMaskCaching = options.fAllowPathMaskCaching;
Brian Osman195c05b2017-08-30 15:14:04 -0400102#if GR_TEST_UTILS
csmartdalton008b9d82017-02-22 12:00:42 -0700103 prcOptions.fGpuPathRenderers = options.fGpuPathRenderers;
Brian Osman195c05b2017-08-30 15:14:04 -0400104#endif
Brian Osmanb350ae22017-08-29 16:15:39 -0400105 if (options.fDisableDistanceFieldPaths) {
Brian Osman195c05b2017-08-30 15:14:04 -0400106 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
Brian Osmanb350ae22017-08-29 16:15:39 -0400107 }
Brian Salomonaf597482017-11-07 16:23:34 -0500108
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500109 if (!fResourceCache) {
110 // DDL TODO: remove this crippling of the path renderer chain
111 // Disable the small path renderer bc of the proxies in the atlas. They need to be
112 // unified when the opLists are added back to the destination drawing manager.
113 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
Robert Phillipsf733a722018-03-15 13:59:35 -0400114 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kCoverageCounting;
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500115 }
116
Brian Salomonaf597482017-11-07 16:23:34 -0500117 GrAtlasTextContext::Options atlasTextContextOptions;
118 atlasTextContextOptions.fMaxDistanceFieldFontSize = options.fGlyphsAsPathsFontSize;
119 atlasTextContextOptions.fMinDistanceFieldFontSize = options.fMinDistanceFieldFontSize;
Brian Salomonb5086962017-12-13 10:59:33 -0500120 atlasTextContextOptions.fDistanceFieldVerticesAlwaysHaveW = false;
121#if SK_SUPPORT_ATLAS_TEXT
122 if (GrContextOptions::Enable::kYes == options.fDistanceFieldGlyphVerticesAlwaysHaveW) {
123 atlasTextContextOptions.fDistanceFieldVerticesAlwaysHaveW = true;
124 }
125#endif
Brian Salomonaf597482017-11-07 16:23:34 -0500126
Robert Phillips4150eea2018-02-07 17:08:21 -0500127 fDrawingManager.reset(new GrDrawingManager(this, prcOptions, atlasTextContextOptions,
128 &fSingleOwner, options.fSortRenderTargets));
joshualitt7c3a2f82015-03-31 13:32:05 -0700129
Robert Phillipsaf4adef2018-03-07 11:59:37 -0500130 fGlyphCache = new GrGlyphCache(fCaps.get(), options.fGlyphCacheTextureMaximumBytes);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500131
Robert Phillips303cd582018-02-14 18:54:01 -0500132 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB,
133 this, this->uniqueID(), SkToBool(fGpu)));
Brian Salomon91a3e522017-06-23 10:58:19 -0400134
Robert Phillipsfde6fa02018-03-02 08:53:14 -0500135 // DDL TODO: we need to think through how the task group & persistent cache
136 // get passed on to/shared between all the DDLRecorders created with this context.
Brian Osman51279982017-08-23 10:12:00 -0400137 if (options.fExecutor) {
138 fTaskGroup = skstd::make_unique<SkTaskGroup>(*options.fExecutor);
139 }
140
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400141 fPersistentCache = options.fPersistentCache;
142
Brian Salomon91a3e522017-06-23 10:58:19 -0400143 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000144}
145
bsalomon@google.com27847de2011-02-22 20:59:41 +0000146GrContext::~GrContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800147 ASSERT_SINGLE_OWNER
148
Robert Phillips2e6feed2018-01-22 15:27:20 -0500149 if (fDrawingManager) {
150 fDrawingManager->cleanup();
151 }
robertphillips2334fb62015-06-17 05:43:33 -0700152
Robert Phillips41a3b872018-03-09 12:00:34 -0500153 fTextureStripAtlasManager = nullptr;
halcanary385fe4d2015-08-26 13:07:48 -0700154 delete fResourceProvider;
155 delete fResourceCache;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500156 delete fProxyProvider;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500157 delete fGlyphCache;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000158}
159
bungeman6bd52842016-10-27 09:30:08 -0700160sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
bungeman6bd52842016-10-27 09:30:08 -0700161 return fThreadSafeProxy;
bsalomon41b952c2016-03-11 06:46:33 -0800162}
163
Robert Phillipsfc711a22018-02-13 17:03:00 -0500164SkSurfaceCharacterization GrContextThreadSafeProxy::createCharacterization(
165 size_t cacheMaxResourceBytes,
166 const SkImageInfo& ii, const GrBackendFormat& backendFormat,
167 int sampleCnt, GrSurfaceOrigin origin,
168 const SkSurfaceProps& surfaceProps,
169 bool isMipMapped) {
170 if (!backendFormat.isValid()) {
171 return SkSurfaceCharacterization(); // return an invalid characterization
172 }
173
174 // We're assuming GrFSAAType::kMixedSamples will never be specified via this code path
175 GrFSAAType FSAAType = sampleCnt > 1 ? GrFSAAType::kUnifiedMSAA : GrFSAAType::kNone;
176
177 if (!fCaps->mipMapSupport()) {
178 isMipMapped = false;
179 }
180
181 GrPixelConfig config = kUnknown_GrPixelConfig;
182 if (!fCaps->getConfigFromBackendFormat(backendFormat, ii.colorType(), &config)) {
183 return SkSurfaceCharacterization(); // return an invalid characterization
184 }
185
186 // This surface characterization factory assumes that the resulting characterization is
187 // textureable.
188 if (!fCaps->isConfigTexturable(config)) {
189 return SkSurfaceCharacterization(); // return an invalid characterization
190 }
191
192 return SkSurfaceCharacterization(sk_ref_sp<GrContextThreadSafeProxy>(this),
193 cacheMaxResourceBytes,
194 origin, ii.width(), ii.height(), config, FSAAType, sampleCnt,
195 SkSurfaceCharacterization::Textureable(true),
196 SkSurfaceCharacterization::MipMapped(isMipMapped),
197 ii.refColorSpace(), surfaceProps);
198}
199
bsalomon2354f842014-07-28 13:48:36 -0700200void GrContext::abandonContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800201 ASSERT_SINGLE_OWNER
202
Robert Phillips41a3b872018-03-09 12:00:34 -0500203 fTextureStripAtlasManager->abandon();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500204 fProxyProvider->abandon();
bsalomond309e7a2015-04-30 14:18:54 -0700205 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800206
207 // Need to abandon the drawing manager first so all the render targets
208 // will be released/forgotten before they too are abandoned.
209 fDrawingManager->abandon();
210
bsalomon@google.com205d4602011-04-25 12:43:45 +0000211 // abandon first to so destructors
212 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800213 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700214
bsalomon6e2aad42016-04-01 11:54:31 -0700215 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
216
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500217 fGlyphCache->freeAll();
bsalomon6e2aad42016-04-01 11:54:31 -0700218 fTextBlobCache->freeAll();
219}
220
221void GrContext::releaseResourcesAndAbandonContext() {
222 ASSERT_SINGLE_OWNER
223
Robert Phillips41a3b872018-03-09 12:00:34 -0500224 fTextureStripAtlasManager->abandon();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500225 fProxyProvider->abandon();
bsalomon6e2aad42016-04-01 11:54:31 -0700226 fResourceProvider->abandon();
227
228 // Need to abandon the drawing manager first so all the render targets
229 // will be released/forgotten before they too are abandoned.
230 fDrawingManager->abandon();
231
232 // Release all resources in the backend 3D API.
233 fResourceCache->releaseAll();
234
235 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000236
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500237 fGlyphCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700238 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000239}
240
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000241void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800242 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000243 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000244}
245
246void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800247 ASSERT_SINGLE_OWNER
248
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500249 fGlyphCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700250
251 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700252
253 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000254}
255
Jim Van Verth76d917c2017-12-13 09:26:37 -0500256void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Brian Salomon5e150852017-03-22 14:53:13 -0400257 ASSERT_SINGLE_OWNER
Jim Van Verth76d917c2017-12-13 09:26:37 -0500258 fResourceCache->purgeAsNeeded();
259 fResourceCache->purgeResourcesNotUsedSince(GrStdSteadyClock::now() - msNotUsed);
260
261 fTextBlobCache->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400262}
263
Derek Sollenberger5480a182017-05-25 16:43:59 -0400264void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
265 ASSERT_SINGLE_OWNER
266 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
267}
268
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000269void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800270 ASSERT_SINGLE_OWNER
271
bsalomon71cb0c22014-11-14 12:10:14 -0800272 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800273 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800274 }
275 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800276 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800277 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000278}
279
Derek Sollenbergeree479142017-05-24 11:41:33 -0400280size_t GrContext::getResourceCachePurgeableBytes() const {
281 ASSERT_SINGLE_OWNER
282 return fResourceCache->getPurgeableBytes();
283}
284
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000285////////////////////////////////////////////////////////////////////////////////
286
Brian Salomonbdecacf2018-02-02 20:32:49 -0500287bool GrContext::colorTypeSupportedAsImage(SkColorType colorType) const {
288 GrPixelConfig config = SkImageInfo2GrPixelConfig(colorType, nullptr, *this->caps());
289 return this->caps()->isConfigTexturable(config);
290}
291
292int GrContext::maxSurfaceSampleCountForColorType(SkColorType colorType) const {
293 GrPixelConfig config = SkImageInfo2GrPixelConfig(colorType, nullptr, *this->caps());
294 return this->caps()->maxRenderTargetSampleCount(config);
295}
296
297////////////////////////////////////////////////////////////////////////////////
298
joshualitt0db6dfa2015-04-10 07:01:30 -0700299void GrContext::TextBlobCacheOverBudgetCB(void* data) {
300 SkASSERT(data);
Brian Osman11052242016-10-27 14:47:55 -0400301 // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on
302 // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls
303 // to below the GrContext level, but this is not trivial because they call drawPath on
304 // SkGpuDevice.
joshualitt0db6dfa2015-04-10 07:01:30 -0700305 GrContext* context = reinterpret_cast<GrContext*>(data);
306 context->flush();
307}
308
bsalomon@google.com27847de2011-02-22 20:59:41 +0000309////////////////////////////////////////////////////////////////////////////////
310
bsalomonb77a9072016-09-07 10:02:04 -0700311void GrContext::flush() {
joshualitt1de610a2016-01-06 08:26:09 -0800312 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700313 RETURN_IF_ABANDONED
Robert Phillips7ee385e2017-03-30 08:02:11 -0400314
315 fDrawingManager->flush(nullptr);
316}
317
Greg Daniel51316782017-08-02 15:10:09 +0000318GrSemaphoresSubmitted GrContext::flushAndSignalSemaphores(int numSemaphores,
319 GrBackendSemaphore signalSemaphores[]) {
320 ASSERT_SINGLE_OWNER
321 if (fDrawingManager->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; }
322
323 return fDrawingManager->flush(nullptr, numSemaphores, signalSemaphores);
324}
325
Robert Phillips7ee385e2017-03-30 08:02:11 -0400326void GrContextPriv::flush(GrSurfaceProxy* proxy) {
327 ASSERT_SINGLE_OWNER_PRIV
328 RETURN_IF_ABANDONED_PRIV
329 ASSERT_OWNED_PROXY_PRIV(proxy);
330
331 fContext->fDrawingManager->flush(proxy);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000332}
333
Brian Salomonc320b152018-02-20 14:05:36 -0500334bool sw_convert_to_premul(GrColorType srcColorType, int width, int height, size_t inRowBytes,
bsalomon81beccc2014-10-13 12:32:55 -0700335 const void* inPixels, size_t outRowBytes, void* outPixels) {
Brian Salomonc320b152018-02-20 14:05:36 -0500336 SkColorType colorType = GrColorTypeToSkColorType(srcColorType);
337 if (kUnknown_SkColorType == colorType || 4 != SkColorTypeBytesPerPixel(colorType)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000338 return false;
339 }
bsalomon81beccc2014-10-13 12:32:55 -0700340
Matt Sarettc7b29082017-02-09 16:22:39 -0500341 for (int y = 0; y < height; y++) {
342 SkOpts::RGBA_to_rgbA((uint32_t*) outPixels, inPixels, width);
343 outPixels = SkTAddOffset<void>(outPixels, outRowBytes);
344 inPixels = SkTAddOffset<const void>(inPixels, inRowBytes);
345 }
bsalomon81beccc2014-10-13 12:32:55 -0700346
Matt Sarettc7b29082017-02-09 16:22:39 -0500347 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000348}
349
Brian Salomonc320b152018-02-20 14:05:36 -0500350// TODO: This will be removed when GrSurfaceContexts are aware of their color types.
351// (skbug.com/6718)
Brian Osmand2ca59a2017-04-13 14:03:57 -0400352static bool valid_premul_config(GrPixelConfig config) {
Brian Salomonc320b152018-02-20 14:05:36 -0500353 switch (config) {
354 case kUnknown_GrPixelConfig: return false;
355 case kAlpha_8_GrPixelConfig: return false;
356 case kGray_8_GrPixelConfig: return false;
357 case kRGB_565_GrPixelConfig: return false;
358 case kRGBA_4444_GrPixelConfig: return true;
359 case kRGBA_8888_GrPixelConfig: return true;
360 case kBGRA_8888_GrPixelConfig: return true;
361 case kSRGBA_8888_GrPixelConfig: return true;
362 case kSBGRA_8888_GrPixelConfig: return true;
Brian Osman10fc6fd2018-03-02 11:01:10 -0500363 case kRGBA_1010102_GrPixelConfig: return true;
Brian Salomonc320b152018-02-20 14:05:36 -0500364 case kRGBA_float_GrPixelConfig: return true;
365 case kRG_float_GrPixelConfig: return false;
366 case kAlpha_half_GrPixelConfig: return false;
367 case kRGBA_half_GrPixelConfig: return true;
368 case kAlpha_8_as_Alpha_GrPixelConfig: return false;
369 case kAlpha_8_as_Red_GrPixelConfig: return false;
370 case kAlpha_half_as_Red_GrPixelConfig: return false;
371 case kGray_8_as_Lum_GrPixelConfig: return false;
372 case kGray_8_as_Red_GrPixelConfig: return false;
373 }
374 SK_ABORT("Invalid GrPixelConfig");
375 return false;
Brian Osmance425512017-03-22 14:37:50 -0400376}
377
Brian Salomonc320b152018-02-20 14:05:36 -0500378static bool valid_premul_color_type(GrColorType ct) {
379 switch (ct) {
Brian Osman10fc6fd2018-03-02 11:01:10 -0500380 case GrColorType::kUnknown: return false;
381 case GrColorType::kAlpha_8: return false;
382 case GrColorType::kRGB_565: return false;
383 case GrColorType::kABGR_4444: return true;
384 case GrColorType::kRGBA_8888: return true;
385 case GrColorType::kBGRA_8888: return true;
386 case GrColorType::kRGBA_1010102: return true;
387 case GrColorType::kGray_8: return false;
388 case GrColorType::kAlpha_F16: return false;
389 case GrColorType::kRGBA_F16: return true;
390 case GrColorType::kRG_F32: return false;
391 case GrColorType::kRGBA_F32: return true;
Brian Salomonc320b152018-02-20 14:05:36 -0500392 }
393 SK_ABORT("Invalid GrColorType");
394 return false;
395}
396
397static bool valid_pixel_conversion(GrColorType cpuColorType, GrPixelConfig gpuConfig,
Brian Osmand2ca59a2017-04-13 14:03:57 -0400398 bool premulConversion) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400399 // We only allow premul <-> unpremul conversions for some formats
Brian Salomonc320b152018-02-20 14:05:36 -0500400 if (premulConversion &&
401 (!valid_premul_color_type(cpuColorType) || !valid_premul_config(gpuConfig))) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400402 return false;
403 }
404
405 return true;
406}
407
Brian Salomonc320b152018-02-20 14:05:36 -0500408static bool pm_upm_must_round_trip(GrColorType cpuColorType, const SkColorSpace* cpuColorSpace) {
409 return !cpuColorSpace &&
410 (GrColorType::kRGBA_8888 == cpuColorType || GrColorType::kBGRA_8888 == cpuColorType);
Brian Osman409e74f2017-04-17 11:48:28 -0400411}
412
Brian Salomonc320b152018-02-20 14:05:36 -0500413// TODO: This will be removed when GrSurfaceContexts are aware of their color types.
414// (skbug.com/6718)
415static bool pm_upm_must_round_trip(GrPixelConfig surfaceConfig,
416 const SkColorSpace* surfaceColorSpace) {
417 return !surfaceColorSpace &&
418 (kRGBA_8888_GrPixelConfig == surfaceConfig || kBGRA_8888_GrPixelConfig == surfaceConfig);
419}
420
421static GrSRGBConversion determine_write_pixels_srgb_conversion(GrColorType srcColorType,
422 const SkColorSpace* srcColorSpace,
423 GrSRGBEncoded dstSRGBEncoded,
424 const SkColorSpace* dstColorSpace,
425 const GrCaps& caps) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500426 // No support for sRGB-encoded alpha.
Brian Salomonc320b152018-02-20 14:05:36 -0500427 if (GrColorTypeIsAlphaOnly(srcColorType)) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500428 return GrSRGBConversion::kNone;
429 }
Brian Salomonc320b152018-02-20 14:05:36 -0500430 // No conversions without GPU support for sRGB. (Legacy mode)
431 if (!caps.srgbSupport()) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500432 return GrSRGBConversion::kNone;
433 }
Brian Salomonc320b152018-02-20 14:05:36 -0500434 // If the GrSurfaceContext has no color space then it is in legacy mode.
435 if (!dstColorSpace) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500436 return GrSRGBConversion::kNone;
437 }
438
439 bool srcColorSpaceIsSRGB = srcColorSpace && srcColorSpace->gammaCloseToSRGB();
440 bool dstColorSpaceIsSRGB = dstColorSpace->gammaCloseToSRGB();
441
442 // For now we are assuming that if color space of the dst does not have sRGB gamma then the
443 // texture format is not sRGB encoded and vice versa. Note that we already checked for "legacy"
Brian Salomonc320b152018-02-20 14:05:36 -0500444 // mode being forced on by caps above. This may change in the future. We will then have to
445 // perform shader based conversions.
Brian Salomon9b009bb2018-02-14 13:53:55 -0500446 SkASSERT(dstColorSpaceIsSRGB == (GrSRGBEncoded::kYes == dstSRGBEncoded));
447
Brian Salomon9b009bb2018-02-14 13:53:55 -0500448 if (srcColorSpaceIsSRGB == dstColorSpaceIsSRGB) {
449 return GrSRGBConversion::kNone;
450 }
451 return srcColorSpaceIsSRGB ? GrSRGBConversion::kSRGBToLinear : GrSRGBConversion::kLinearToSRGB;
452}
453
Brian Salomonc320b152018-02-20 14:05:36 -0500454static GrSRGBConversion determine_read_pixels_srgb_conversion(GrSRGBEncoded srcSRGBEncoded,
455 const SkColorSpace* srcColorSpace,
456 GrColorType dstColorType,
457 const SkColorSpace* dstColorSpace,
458 const GrCaps& caps) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500459 // This is symmetrical with the write version.
Brian Salomonc320b152018-02-20 14:05:36 -0500460 switch (determine_write_pixels_srgb_conversion(dstColorType, dstColorSpace, srcSRGBEncoded,
461 srcColorSpace, caps)) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500462 case GrSRGBConversion::kNone: return GrSRGBConversion::kNone;
463 case GrSRGBConversion::kLinearToSRGB: return GrSRGBConversion::kSRGBToLinear;
464 case GrSRGBConversion::kSRGBToLinear: return GrSRGBConversion::kLinearToSRGB;
465 }
466 return GrSRGBConversion::kNone;
467}
468
Brian Salomonc320b152018-02-20 14:05:36 -0500469bool GrContextPriv::writeSurfacePixels(GrSurfaceContext* dst, int left, int top, int width,
470 int height, GrColorType srcColorType,
471 SkColorSpace* srcColorSpace, const void* buffer,
472 size_t rowBytes, uint32_t pixelOpsFlags) {
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500473#ifndef SK_LEGACY_GPU_PIXEL_OPS
474 return this->writeSurfacePixels2(dst, left, top, width, height, srcColorType, srcColorSpace,
475 buffer, rowBytes, pixelOpsFlags);
476#endif
477
Brian Osmanb62ea222016-12-22 11:12:16 -0500478 // TODO: Color space conversion
479
Robert Phillipse78b7252017-04-06 07:59:41 -0400480 ASSERT_SINGLE_OWNER_PRIV
481 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400482 SkASSERT(dst);
483 ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy());
Brian Salomondcbb9d92017-07-19 10:53:20 -0400484 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels", fContext);
bsalomon6c6f6582015-09-10 08:12:46 -0700485
Robert Phillips6be756b2018-01-16 15:07:54 -0500486 if (!dst->asSurfaceProxy()->instantiate(this->resourceProvider())) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400487 return false;
488 }
489
Robert Phillips16d8ec62017-07-27 16:16:25 -0400490 GrSurfaceProxy* dstProxy = dst->asSurfaceProxy();
491 GrSurface* dstSurface = dstProxy->priv().peekSurface();
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400492
Brian Osmand2ca59a2017-04-13 14:03:57 -0400493 // 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 -0400494 const bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
Brian Salomonc320b152018-02-20 14:05:36 -0500495
496 if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400497 return false;
498 }
499
Brian Osman409e74f2017-04-17 11:48:28 -0400500 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
501 // without any color spaces attached, and the caller wants us to premul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400502 bool useConfigConversionEffect =
Brian Salomonc320b152018-02-20 14:05:36 -0500503 premul && pm_upm_must_round_trip(srcColorType, srcColorSpace) &&
Brian Salomonf3569f02017-10-24 12:52:33 -0400504 pm_upm_must_round_trip(dstProxy->config(), dst->colorSpaceInfo().colorSpace());
Brian Osman409e74f2017-04-17 11:48:28 -0400505
506 // Are we going to try to premul as part of a draw? For the non-legacy case, we always allow
507 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
508 bool premulOnGpu = premul &&
509 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon81beccc2014-10-13 12:32:55 -0700510
bsalomone8d21e82015-07-16 08:23:13 -0700511 // 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 -0700512 // necessary and because GrGpu::getWritePixelsInfo requires it.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400513 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
Brian Salomonc320b152018-02-20 14:05:36 -0500514 GrColorTypeBytesPerPixel(srcColorType), &left, &top,
515 &width, &height, &buffer, &rowBytes)) {
bsalomone8d21e82015-07-16 08:23:13 -0700516 return false;
517 }
518
Brian Osman409e74f2017-04-17 11:48:28 -0400519 GrGpu::DrawPreference drawPreference = premulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
520 : GrGpu::kNoDraw_DrawPreference;
bsalomonf0674512015-07-28 13:26:15 -0700521 GrGpu::WritePixelTempDrawInfo tempDrawInfo;
Brian Salomon9b009bb2018-02-14 13:53:55 -0500522 GrSRGBConversion srgbConversion = determine_write_pixels_srgb_conversion(
Brian Salomonc320b152018-02-20 14:05:36 -0500523 srcColorType, srcColorSpace, GrPixelConfigIsSRGBEncoded(dstProxy->config()),
524 dst->colorSpaceInfo().colorSpace(), *fContext->caps());
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400525 if (!fContext->fGpu->getWritePixelsInfo(dstSurface, dstProxy->origin(), width, height,
Brian Salomonc320b152018-02-20 14:05:36 -0500526 srcColorType, srgbConversion, &drawPreference,
Brian Salomon9b009bb2018-02-14 13:53:55 -0500527 &tempDrawInfo)) {
bsalomonf0674512015-07-28 13:26:15 -0700528 return false;
529 }
530
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400531 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400532 this->flush(nullptr); // MDB TODO: tighten this
bsalomonf0674512015-07-28 13:26:15 -0700533 }
534
Robert Phillips2f493142017-03-02 18:18:38 -0500535 sk_sp<GrTextureProxy> tempProxy;
bsalomonf0674512015-07-28 13:26:15 -0700536 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500537 tempProxy = this->proxyProvider()->createProxy(tempDrawInfo.fTempSurfaceDesc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500538 kTopLeft_GrSurfaceOrigin,
539 SkBackingFit::kApprox, SkBudgeted::kYes);
Robert Phillips2f493142017-03-02 18:18:38 -0500540 if (!tempProxy && GrGpu::kRequireDraw_DrawPreference == drawPreference) {
bsalomonf0674512015-07-28 13:26:15 -0700541 return false;
542 }
543 }
544
545 // temp buffer for doing sw premul conversion, if needed.
546 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
Brian Osman409e74f2017-04-17 11:48:28 -0400547 // We need to do sw premul if we were unable to create a RT for drawing, or if we can't do the
548 // premul on the GPU
549 if (premul && (!tempProxy || !premulOnGpu)) {
550 size_t tmpRowBytes = 4 * width;
551 tmpPixels.reset(width * height);
Brian Salomonc320b152018-02-20 14:05:36 -0500552 if (!sw_convert_to_premul(srcColorType, width, height, rowBytes, buffer, tmpRowBytes,
Brian Osman409e74f2017-04-17 11:48:28 -0400553 tmpPixels.get())) {
554 return false;
bsalomonf0674512015-07-28 13:26:15 -0700555 }
Brian Osman409e74f2017-04-17 11:48:28 -0400556 rowBytes = tmpRowBytes;
557 buffer = tmpPixels.get();
bsalomonf0674512015-07-28 13:26:15 -0700558 }
Brian Osman409e74f2017-04-17 11:48:28 -0400559
560 if (tempProxy) {
Brian Osman2240be92017-10-18 13:15:13 -0400561 auto fp = GrSimpleTextureEffect::Make(tempProxy, SkMatrix::I());
Brian Osman409e74f2017-04-17 11:48:28 -0400562 if (premulOnGpu) {
563 fp = fContext->createUPMToPMEffect(std::move(fp), useConfigConversionEffect);
bsalomon81beccc2014-10-13 12:32:55 -0700564 }
Brian Osman409e74f2017-04-17 11:48:28 -0400565 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400566 if (!fp) {
567 return false;
568 }
Brian Osman409e74f2017-04-17 11:48:28 -0400569
Robert Phillips6be756b2018-01-16 15:07:54 -0500570 if (!tempProxy->instantiate(this->resourceProvider())) {
Brian Osman409e74f2017-04-17 11:48:28 -0400571 return false;
572 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400573 GrTexture* texture = tempProxy->priv().peekTexture();
Robert Phillips7bbbf622017-10-17 07:36:59 -0400574
575 if (tempProxy->priv().hasPendingIO()) {
576 this->flush(tempProxy.get());
577 }
578
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400579 if (!fContext->fGpu->writePixels(texture, tempProxy->origin(), 0, 0, width, height,
Brian Salomonc320b152018-02-20 14:05:36 -0500580 tempDrawInfo.fWriteColorType, buffer, rowBytes)) {
Brian Osman409e74f2017-04-17 11:48:28 -0400581 return false;
582 }
Robert Phillips7bbbf622017-10-17 07:36:59 -0400583 tempProxy = nullptr;
584
Brian Osman409e74f2017-04-17 11:48:28 -0400585 SkMatrix matrix;
586 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400587 GrRenderTargetContext* renderTargetContext = dst->asRenderTargetContext();
Brian Osman409e74f2017-04-17 11:48:28 -0400588 if (!renderTargetContext) {
589 return false;
590 }
591 GrPaint paint;
592 paint.addColorFragmentProcessor(std::move(fp));
593 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonf3569f02017-10-24 12:52:33 -0400594 paint.setAllowSRGBInputs(dst->colorSpaceInfo().isGammaCorrect() ||
595 GrPixelConfigIsSRGB(dst->colorSpaceInfo().config()));
Brian Osman409e74f2017-04-17 11:48:28 -0400596 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
597 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, matrix, rect,
598 nullptr);
599
600 if (kFlushWrites_PixelOp & pixelOpsFlags) {
601 this->flushSurfaceWrites(renderTargetContext->asRenderTargetProxy());
602 }
603 } else {
Brian Salomonc320b152018-02-20 14:05:36 -0500604 return fContext->fGpu->writePixels(dstSurface, dstProxy->origin(), left, top, width, height,
605 srcColorType, buffer, rowBytes);
bsalomon81beccc2014-10-13 12:32:55 -0700606 }
bsalomon81beccc2014-10-13 12:32:55 -0700607 return true;
608}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000609
Brian Salomonc320b152018-02-20 14:05:36 -0500610bool GrContextPriv::readSurfacePixels(GrSurfaceContext* src, int left, int top, int width,
611 int height, GrColorType dstColorType,
612 SkColorSpace* dstColorSpace, void* buffer, size_t rowBytes,
613 uint32_t flags) {
Brian Osmanb62ea222016-12-22 11:12:16 -0500614 // TODO: Color space conversion
615
Robert Phillipse78b7252017-04-06 07:59:41 -0400616 ASSERT_SINGLE_OWNER_PRIV
617 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400618 SkASSERT(src);
619 ASSERT_OWNED_PROXY_PRIV(src->asSurfaceProxy());
Brian Salomondcbb9d92017-07-19 10:53:20 -0400620 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "readSurfacePixels", fContext);
bsalomon32ab2602015-09-09 18:57:49 -0700621
Robert Phillipse78b7252017-04-06 07:59:41 -0400622 // MDB TODO: delay this instantiation until later in the method
Robert Phillips6be756b2018-01-16 15:07:54 -0500623 if (!src->asSurfaceProxy()->instantiate(this->resourceProvider())) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400624 return false;
625 }
626
Robert Phillips16d8ec62017-07-27 16:16:25 -0400627 GrSurfaceProxy* srcProxy = src->asSurfaceProxy();
628 GrSurface* srcSurface = srcProxy->priv().peekSurface();
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400629
Brian Osmand2ca59a2017-04-13 14:03:57 -0400630 // The src is premul but the dst is unpremul -> unpremul the src after or as part of the read
631 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
Brian Salomonc320b152018-02-20 14:05:36 -0500632
633 if (!valid_pixel_conversion(dstColorType, srcProxy->config(), unpremul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400634 return false;
635 }
636
Brian Osman409e74f2017-04-17 11:48:28 -0400637 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
638 // without any color spaces attached, and the caller wants us to unpremul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400639 bool useConfigConversionEffect =
Brian Salomonf3569f02017-10-24 12:52:33 -0400640 unpremul &&
641 pm_upm_must_round_trip(srcProxy->config(), src->colorSpaceInfo().colorSpace()) &&
Brian Salomonc320b152018-02-20 14:05:36 -0500642 pm_upm_must_round_trip(dstColorType, dstColorSpace);
Brian Osman409e74f2017-04-17 11:48:28 -0400643
644 // Are we going to try to unpremul as part of a draw? For the non-legacy case, we always allow
645 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
646 bool unpremulOnGpu = unpremul &&
647 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon6c6f6582015-09-10 08:12:46 -0700648
bsalomone8d21e82015-07-16 08:23:13 -0700649 // Adjust the params so that if we wind up using an intermediate surface we've already done
650 // all the trimming and the temporary can be the min size required.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400651 if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(),
Brian Salomonc320b152018-02-20 14:05:36 -0500652 GrColorTypeBytesPerPixel(dstColorType), &left, &top,
653 &width, &height, &buffer, &rowBytes)) {
bsalomone8d21e82015-07-16 08:23:13 -0700654 return false;
655 }
656
Brian Osman409e74f2017-04-17 11:48:28 -0400657 GrGpu::DrawPreference drawPreference = unpremulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
658 : GrGpu::kNoDraw_DrawPreference;
bsalomon39826022015-07-23 08:07:21 -0700659 GrGpu::ReadPixelTempDrawInfo tempDrawInfo;
Brian Salomon9b009bb2018-02-14 13:53:55 -0500660 GrSRGBConversion srgbConversion = determine_read_pixels_srgb_conversion(
661 GrPixelConfigIsSRGBEncoded(srcProxy->config()), src->colorSpaceInfo().colorSpace(),
Brian Salomonc320b152018-02-20 14:05:36 -0500662 dstColorType, dstColorSpace, *fContext->caps());
663
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400664 if (!fContext->fGpu->getReadPixelsInfo(srcSurface, srcProxy->origin(), width, height, rowBytes,
Brian Salomonc320b152018-02-20 14:05:36 -0500665 dstColorType, srgbConversion, &drawPreference,
Brian Salomon9b009bb2018-02-14 13:53:55 -0500666 &tempDrawInfo)) {
bsalomon39826022015-07-23 08:07:21 -0700667 return false;
668 }
bsalomon191bcc02014-11-14 11:31:13 -0800669
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400670 if (!(kDontFlush_PixelOpsFlag & flags) && srcSurface->surfacePriv().hasPendingWrite()) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400671 this->flush(nullptr); // MDB TODO: tighten this
672 }
673
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400674 sk_sp<GrSurfaceProxy> proxyToRead = src->asSurfaceProxyRef();
bsalomon39826022015-07-23 08:07:21 -0700675 bool didTempDraw = false;
676 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomonb117ff12016-07-19 07:24:40 -0700677 if (SkBackingFit::kExact == tempDrawInfo.fTempSurfaceFit) {
bsalomon39826022015-07-23 08:07:21 -0700678 // We only respect this when the entire src is being read. Otherwise we can trigger too
679 // many odd ball texture sizes and trash the cache.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400680 if (width != srcSurface->width() || height != srcSurface->height()) {
bsalomonb117ff12016-07-19 07:24:40 -0700681 tempDrawInfo.fTempSurfaceFit= SkBackingFit::kApprox;
bsalomon39826022015-07-23 08:07:21 -0700682 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000683 }
brianosmandfe4f2e2016-07-21 13:28:36 -0700684 // TODO: Need to decide the semantics of this function for color spaces. Do we support
685 // conversion to a passed-in color space? For now, specifying nullptr means that this
Brian Salomon366093f2018-02-13 09:25:22 -0500686 // path will do no conversion, so it will match the behavior of the non-draw path. For
687 // now we simply infer an sRGB color space if the config is sRGB in order to avoid an
688 // illegal combination.
689 sk_sp<SkColorSpace> colorSpace;
690 if (GrPixelConfigIsSRGB(tempDrawInfo.fTempSurfaceDesc.fConfig)) {
691 colorSpace = SkColorSpace::MakeSRGB();
692 }
693 sk_sp<GrRenderTargetContext> tempRTC =
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500694 fContext->contextPriv().makeDeferredRenderTargetContext(
695 tempDrawInfo.fTempSurfaceFit,
Brian Salomon366093f2018-02-13 09:25:22 -0500696 tempDrawInfo.fTempSurfaceDesc.fWidth,
697 tempDrawInfo.fTempSurfaceDesc.fHeight,
698 tempDrawInfo.fTempSurfaceDesc.fConfig,
699 std::move(colorSpace),
700 tempDrawInfo.fTempSurfaceDesc.fSampleCnt,
701 GrMipMapped::kNo,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500702 kTopLeft_GrSurfaceOrigin);
Brian Osman693a5402016-10-27 15:13:22 -0400703 if (tempRTC) {
Greg Danielf44cb482018-02-27 14:26:32 -0500704 // Adding discard to appease vulkan validation warning about loading uninitialized data
705 // on draw
706 tempRTC->discard();
Robert Phillips67c18d62017-01-20 12:44:06 -0500707 SkMatrix textureMatrix = SkMatrix::MakeTrans(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400708 sk_sp<GrTextureProxy> proxy = src->asTextureProxyRef();
Brian Osman2240be92017-10-18 13:15:13 -0400709 auto fp = GrSimpleTextureEffect::Make(std::move(proxy), textureMatrix);
Brian Osman409e74f2017-04-17 11:48:28 -0400710 if (unpremulOnGpu) {
711 fp = fContext->createPMToUPMEffect(std::move(fp), useConfigConversionEffect);
712 // We no longer need to do this on CPU after the read back.
713 unpremul = false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000714 }
Brian Osman409e74f2017-04-17 11:48:28 -0400715 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400716 if (!fp) {
717 return false;
718 }
Brian Osman60cd57e2017-04-06 10:19:06 -0400719
Brian Osman409e74f2017-04-17 11:48:28 -0400720 GrPaint paint;
721 paint.addColorFragmentProcessor(std::move(fp));
722 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
723 paint.setAllowSRGBInputs(true);
724 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
725 tempRTC->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect,
726 nullptr);
727 proxyToRead = tempRTC->asTextureProxyRef();
728 left = 0;
729 top = 0;
730 didTempDraw = true;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000731 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000732 }
joshualitt5c55fef2014-10-31 14:04:35 -0700733
Robert Phillipse78b7252017-04-06 07:59:41 -0400734 if (!proxyToRead) {
735 return false;
736 }
737
bsalomon39826022015-07-23 08:07:21 -0700738 if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000739 return false;
740 }
Brian Salomonc320b152018-02-20 14:05:36 -0500741 GrColorType colorTypeToRead = dstColorType;
bsalomon39826022015-07-23 08:07:21 -0700742 if (didTempDraw) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400743 this->flushSurfaceWrites(proxyToRead.get());
Brian Salomonc320b152018-02-20 14:05:36 -0500744 colorTypeToRead = tempDrawInfo.fReadColorType;
bsalomon39826022015-07-23 08:07:21 -0700745 }
Robert Phillips09dfc472017-09-13 15:25:47 -0400746
Robert Phillips6be756b2018-01-16 15:07:54 -0500747 if (!proxyToRead->instantiate(this->resourceProvider())) {
Robert Phillips09dfc472017-09-13 15:25:47 -0400748 return false;
749 }
750
751 GrSurface* surfaceToRead = proxyToRead->priv().peekSurface();
752
Brian Salomonc320b152018-02-20 14:05:36 -0500753 if (!fContext->fGpu->readPixels(surfaceToRead, proxyToRead->origin(), left, top, width, height,
754 colorTypeToRead, buffer, rowBytes)) {
bsalomon39826022015-07-23 08:07:21 -0700755 return false;
756 }
757
758 // Perform umpremul conversion if we weren't able to perform it as a draw.
759 if (unpremul) {
Brian Salomonc320b152018-02-20 14:05:36 -0500760 SkColorType colorType = GrColorTypeToSkColorType(dstColorType);
761 if (kUnknown_SkColorType == colorType || 4 != SkColorTypeBytesPerPixel(colorType)) {
reed@google.com7111d462014-03-25 16:20:24 +0000762 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000763 }
reed@google.com7111d462014-03-25 16:20:24 +0000764
Matt Sarettc7b29082017-02-09 16:22:39 -0500765 for (int y = 0; y < height; y++) {
766 SkUnpremultiplyRow<false>((uint32_t*) buffer, (const uint32_t*) buffer, width);
767 buffer = SkTAddOffset<void>(buffer, rowBytes);
768 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000769 }
770 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000771}
772
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500773bool GrContextPriv::writeSurfacePixels2(GrSurfaceContext* dst, int left, int top, int width,
774 int height, GrColorType srcColorType,
775 SkColorSpace* srcColorSpace, const void* buffer,
776 size_t rowBytes, uint32_t pixelOpsFlags) {
777 ASSERT_SINGLE_OWNER_PRIV
778 RETURN_FALSE_IF_ABANDONED_PRIV
779 SkASSERT(dst);
780 SkASSERT(buffer);
781 ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy());
782 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels2", fContext);
783
784 if (GrColorType::kUnknown == srcColorType) {
785 return false;
786 }
787
788 if (!dst->asSurfaceProxy()->instantiate(this->resourceProvider())) {
789 return false;
790 }
791
792 GrSurfaceProxy* dstProxy = dst->asSurfaceProxy();
793 GrSurface* dstSurface = dstProxy->priv().peekSurface();
794
795 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
796 GrColorTypeBytesPerPixel(srcColorType), &left, &top,
797 &width, &height, &buffer, &rowBytes)) {
798 return false;
799 }
800
Brian Salomon3d86a192018-02-27 16:46:11 -0500801 if (!fContext->caps()->surfaceSupportsWritePixels(dstSurface)) {
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500802 GrSurfaceDesc desc;
803 desc.fConfig = dstProxy->config();
804 desc.fWidth = width;
805 desc.fHeight = height;
806 desc.fSampleCnt = 1;
Brian Salomon2a4f9832018-03-03 22:43:43 -0500807 auto tempProxy = this->proxyProvider()->createProxy(
808 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kApprox, SkBudgeted::kYes);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500809 if (!tempProxy) {
810 return false;
811 }
812 auto tempCtx = this->drawingManager()->makeTextureContext(
813 tempProxy, dst->colorSpaceInfo().refColorSpace());
814 if (!tempCtx) {
815 return false;
816 }
817 if (!this->writeSurfacePixels2(tempCtx.get(), 0, 0, width, height, srcColorType,
818 srcColorSpace, buffer, rowBytes, pixelOpsFlags)) {
819 return false;
820 }
Brian Salomon3d86a192018-02-27 16:46:11 -0500821 return dst->copy(tempProxy.get(), SkIRect::MakeWH(width, height), {left, top});
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500822 }
823
824 // TODO: Make GrSurfaceContext know its alpha type and pass src buffer's alpha type.
825 bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
826 bool convert = premul;
827
828 if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) {
829 return false;
830 }
831
832 GrColorType allowedColorType =
833 fContext->caps()->supportedWritePixelsColorType(dstProxy->config(), srcColorType);
834 convert = convert || (srcColorType != allowedColorType);
835
836 if (!dst->colorSpaceInfo().colorSpace()) {
837 // "Legacy" mode - no color space conversions.
838 srcColorSpace = nullptr;
839 }
840 convert = convert || !SkColorSpace::Equals(srcColorSpace, dst->colorSpaceInfo().colorSpace());
841
842 std::unique_ptr<char[]> tempBuffer;
843 if (convert) {
844 auto srcSkColorType = GrColorTypeToSkColorType(srcColorType);
845 auto dstSkColorType = GrColorTypeToSkColorType(allowedColorType);
846 if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) {
847 return false;
848 }
849 auto srcAlphaType = premul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
850 SkPixmap src(SkImageInfo::Make(width, height, srcSkColorType, srcAlphaType,
851 sk_ref_sp(srcColorSpace)),
852 buffer, rowBytes);
853 auto tempSrcII = SkImageInfo::Make(width, height, dstSkColorType, kPremul_SkAlphaType,
854 dst->colorSpaceInfo().refColorSpace());
855 auto size = tempSrcII.computeMinByteSize();
856 if (!size) {
857 return false;
858 }
859 tempBuffer.reset(new char[size]);
860 SkPixmap tempSrc(tempSrcII, tempBuffer.get(), tempSrcII.minRowBytes());
861 if (!src.readPixels(tempSrc)) {
862 return false;
863 }
864 srcColorType = allowedColorType;
865 buffer = tempSrc.addr();
866 rowBytes = tempSrc.rowBytes();
867 if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
868 std::unique_ptr<char[]> row(new char[rowBytes]);
869 for (int y = 0; y < height / 2; ++y) {
870 memcpy(row.get(), tempSrc.addr(0, y), rowBytes);
871 memcpy(tempSrc.writable_addr(0, y), tempSrc.addr(0, height - 1 - y), rowBytes);
872 memcpy(tempSrc.writable_addr(0, height - 1 - y), row.get(), rowBytes);
873 }
874 top = dstProxy->height() - top - height;
875 }
876 } else if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
877 size_t trimRowBytes = GrColorTypeBytesPerPixel(srcColorType) * width;
878 tempBuffer.reset(new char[trimRowBytes * height]);
879 char* dst = reinterpret_cast<char*>(tempBuffer.get()) + trimRowBytes * (height - 1);
880 const char* src = reinterpret_cast<const char*>(buffer);
881 for (int i = 0; i < height; ++i, src += rowBytes, dst -= trimRowBytes) {
882 memcpy(dst, src, trimRowBytes);
883 }
884 buffer = tempBuffer.get();
885 rowBytes = trimRowBytes;
886 top = dstProxy->height() - top - height;
887 }
888
889 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) {
890 this->flush(nullptr); // MDB TODO: tighten this
891 }
892
893 return this->getGpu()->writePixels(dstSurface, left, top, width, height, srcColorType, buffer,
894 rowBytes);
895}
896
Robert Phillips7ee385e2017-03-30 08:02:11 -0400897void GrContextPriv::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) {
898 ASSERT_SINGLE_OWNER_PRIV
899 RETURN_IF_ABANDONED_PRIV
900 SkASSERT(proxy);
901 ASSERT_OWNED_PROXY_PRIV(proxy);
Greg Daniel51316782017-08-02 15:10:09 +0000902 fContext->fDrawingManager->prepareSurfaceForExternalIO(proxy, 0, nullptr);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000903}
904
Robert Phillips7ee385e2017-03-30 08:02:11 -0400905void GrContextPriv::flushSurfaceWrites(GrSurfaceProxy* proxy) {
906 ASSERT_SINGLE_OWNER_PRIV
907 RETURN_IF_ABANDONED_PRIV
908 SkASSERT(proxy);
909 ASSERT_OWNED_PROXY_PRIV(proxy);
910 if (proxy->priv().hasPendingWrite()) {
911 this->flush(proxy);
bsalomonf80bfed2014-10-07 05:56:02 -0700912 }
913}
914
Robert Phillips7ee385e2017-03-30 08:02:11 -0400915void GrContextPriv::flushSurfaceIO(GrSurfaceProxy* proxy) {
916 ASSERT_SINGLE_OWNER_PRIV
917 RETURN_IF_ABANDONED_PRIV
918 SkASSERT(proxy);
919 ASSERT_OWNED_PROXY_PRIV(proxy);
920 if (proxy->priv().hasPendingIO()) {
921 this->flush(proxy);
ajuma95243eb2016-08-24 08:19:02 -0700922 }
923}
924
bsalomon@google.com27847de2011-02-22 20:59:41 +0000925////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000926
Robert Phillips2c862492017-01-18 10:08:39 -0500927sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy,
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500928 sk_sp<SkColorSpace> colorSpace,
929 const SkSurfaceProps* props) {
Brian Osman45580d32016-11-23 09:37:01 -0500930 ASSERT_SINGLE_OWNER_PRIV
931
Brian Salomon366093f2018-02-13 09:25:22 -0500932 // sRGB pixel configs may only be used with near-sRGB gamma color spaces.
933 if (GrPixelConfigIsSRGB(proxy->config())) {
934 if (!colorSpace || !colorSpace->gammaCloseToSRGB()) {
935 return nullptr;
936 }
937 }
Brian Osman45580d32016-11-23 09:37:01 -0500938 if (proxy->asRenderTargetProxy()) {
Robert Phillips2c862492017-01-18 10:08:39 -0500939 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500940 std::move(colorSpace), props);
Brian Osman45580d32016-11-23 09:37:01 -0500941 } else {
942 SkASSERT(proxy->asTextureProxy());
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500943 SkASSERT(!props);
Robert Phillips2c862492017-01-18 10:08:39 -0500944 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Brian Osman45580d32016-11-23 09:37:01 -0500945 }
946}
947
Robert Phillipse2f7d182016-12-15 09:23:05 -0500948sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrSurfaceDesc& dstDesc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500949 GrSurfaceOrigin origin,
Greg Daniel65c7f662017-10-30 13:39:09 -0400950 GrMipMapped mipMapped,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500951 SkBackingFit fit,
Brian Salomon366093f2018-02-13 09:25:22 -0500952 SkBudgeted isDstBudgeted,
953 sk_sp<SkColorSpace> colorSpace,
954 const SkSurfaceProps* props) {
Greg Daniel65c7f662017-10-30 13:39:09 -0400955 sk_sp<GrTextureProxy> proxy;
956 if (GrMipMapped::kNo == mipMapped) {
Brian Salomon2a4f9832018-03-03 22:43:43 -0500957 proxy = this->proxyProvider()->createProxy(dstDesc, origin, fit, isDstBudgeted);
Greg Daniel65c7f662017-10-30 13:39:09 -0400958 } else {
959 SkASSERT(SkBackingFit::kExact == fit);
Brian Salomon2a4f9832018-03-03 22:43:43 -0500960 proxy = this->proxyProvider()->createMipMapProxy(dstDesc, origin, isDstBudgeted);
Greg Daniel65c7f662017-10-30 13:39:09 -0400961 }
Robert Phillips77b3f322017-01-31 18:24:12 -0500962 if (!proxy) {
963 return nullptr;
964 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500965
Brian Salomon366093f2018-02-13 09:25:22 -0500966 return this->makeWrappedSurfaceContext(std::move(proxy), std::move(colorSpace), props);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500967}
968
Brian Salomond17f6582017-07-19 18:28:58 -0400969sk_sp<GrTextureContext> GrContextPriv::makeBackendTextureContext(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000970 GrSurfaceOrigin origin,
Brian Osmanc1e37052017-03-09 14:19:20 -0500971 sk_sp<SkColorSpace> colorSpace) {
Robert Phillips26caf892017-01-27 10:58:31 -0500972 ASSERT_SINGLE_OWNER_PRIV
973
Brian Salomon7578f3e2018-03-07 14:39:54 -0500974 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->wrapBackendTexture(tex, origin);
Robert Phillips77b3f322017-01-31 18:24:12 -0500975 if (!proxy) {
976 return nullptr;
977 }
Robert Phillips26caf892017-01-27 10:58:31 -0500978
Brian Salomond17f6582017-07-19 18:28:58 -0400979 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Robert Phillips26caf892017-01-27 10:58:31 -0500980}
981
Brian Osman11052242016-10-27 14:47:55 -0400982sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000983 const GrBackendTexture& tex,
984 GrSurfaceOrigin origin,
985 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -0400986 sk_sp<SkColorSpace> colorSpace,
Brian Osmanc1e37052017-03-09 14:19:20 -0500987 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700988 ASSERT_SINGLE_OWNER_PRIV
Brian Salomonbdecacf2018-02-02 20:32:49 -0500989 SkASSERT(sampleCnt > 0);
robertphillips4fd74ae2016-08-03 14:26:53 -0700990
Brian Salomon7578f3e2018-03-07 14:39:54 -0500991 sk_sp<GrTextureProxy> proxy(
992 this->proxyProvider()->wrapRenderableBackendTexture(tex, origin, sampleCnt));
Robert Phillips77b3f322017-01-31 18:24:12 -0500993 if (!proxy) {
994 return nullptr;
995 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400996
Robert Phillips37430132016-11-09 06:50:43 -0500997 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Brian Osman11052242016-10-27 14:47:55 -0400998 std::move(colorSpace), props);
robertphillips4fd74ae2016-08-03 14:26:53 -0700999}
1000
Brian Osman11052242016-10-27 14:47:55 -04001001sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +00001002 const GrBackendRenderTarget& backendRT,
1003 GrSurfaceOrigin origin,
robertphillips4fd74ae2016-08-03 14:26:53 -07001004 sk_sp<SkColorSpace> colorSpace,
1005 const SkSurfaceProps* surfaceProps) {
1006 ASSERT_SINGLE_OWNER_PRIV
1007
Brian Salomon7578f3e2018-03-07 14:39:54 -05001008 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->wrapBackendRenderTarget(backendRT, origin);
Robert Phillips77b3f322017-01-31 18:24:12 -05001009 if (!proxy) {
1010 return nullptr;
1011 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001012
Robert Phillips37430132016-11-09 06:50:43 -05001013 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001014 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -04001015 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -07001016}
1017
Brian Osman11052242016-10-27 14:47:55 -04001018sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +00001019 const GrBackendTexture& tex,
1020 GrSurfaceOrigin origin,
1021 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -07001022 sk_sp<SkColorSpace> colorSpace,
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001023 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -07001024 ASSERT_SINGLE_OWNER_PRIV
Brian Salomonbdecacf2018-02-02 20:32:49 -05001025 SkASSERT(sampleCnt > 0);
Brian Salomon7578f3e2018-03-07 14:39:54 -05001026 sk_sp<GrSurfaceProxy> proxy(
1027 this->proxyProvider()->wrapBackendTextureAsRenderTarget(tex, origin, sampleCnt));
Robert Phillips77b3f322017-01-31 18:24:12 -05001028 if (!proxy) {
1029 return nullptr;
1030 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001031
Robert Phillips37430132016-11-09 06:50:43 -05001032 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001033 std::move(colorSpace),
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001034 props);
robertphillips77a2e522015-10-17 07:43:27 -07001035}
1036
Chris Daltonfe199b72017-05-05 11:26:15 -04001037void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
1038 fContext->fDrawingManager->addOnFlushCallbackObject(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -04001039}
1040
Robert Phillips62000362018-02-01 09:10:04 -05001041void GrContextPriv::moveOpListsToDDL(SkDeferredDisplayList* ddl) {
1042 fContext->fDrawingManager->moveOpListsToDDL(ddl);
1043}
1044
1045void GrContextPriv::copyOpListsFromDDL(const SkDeferredDisplayList* ddl,
1046 GrRenderTargetProxy* newDest) {
1047 fContext->fDrawingManager->copyOpListsFromDDL(ddl, newDest);
1048}
1049
robertphillips48fde9c2016-09-06 05:20:20 -07001050static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
Brian Osman78f20e02017-01-12 10:28:01 -05001051 switch (config) {
1052 case kAlpha_8_GrPixelConfig:
1053 case kRGB_565_GrPixelConfig:
1054 case kRGBA_4444_GrPixelConfig:
1055 case kBGRA_8888_GrPixelConfig:
Brian Osman10fc6fd2018-03-02 11:01:10 -05001056 case kRGBA_1010102_GrPixelConfig:
Brian Osman78f20e02017-01-12 10:28:01 -05001057 return kRGBA_8888_GrPixelConfig;
1058 case kSBGRA_8888_GrPixelConfig:
1059 return kSRGBA_8888_GrPixelConfig;
1060 case kAlpha_half_GrPixelConfig:
1061 return kRGBA_half_GrPixelConfig;
1062 default:
1063 return kUnknown_GrPixelConfig;
1064 }
robertphillips48fde9c2016-09-06 05:20:20 -07001065}
1066
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001067sk_sp<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContextWithFallback(
robertphillipsd728f0c2016-11-21 11:05:03 -08001068 SkBackingFit fit,
1069 int width, int height,
1070 GrPixelConfig config,
1071 sk_sp<SkColorSpace> colorSpace,
1072 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -04001073 GrMipMapped mipMapped,
robertphillipsd728f0c2016-11-21 11:05:03 -08001074 GrSurfaceOrigin origin,
1075 const SkSurfaceProps* surfaceProps,
1076 SkBudgeted budgeted) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05001077 SkASSERT(sampleCnt > 0);
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001078 if (0 == fContext->caps()->getRenderTargetSampleCount(sampleCnt, config)) {
robertphillipsd728f0c2016-11-21 11:05:03 -08001079 config = GrPixelConfigFallback(config);
1080 }
1081
1082 return this->makeDeferredRenderTargetContext(fit, width, height, config, std::move(colorSpace),
Greg Daniel45d63032017-10-30 13:41:26 -04001083 sampleCnt, mipMapped, origin, surfaceProps,
Greg Daniele1da1d92017-10-06 15:59:27 -04001084 budgeted);
robertphillipsd728f0c2016-11-21 11:05:03 -08001085}
1086
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001087sk_sp<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContext(
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001088 SkBackingFit fit,
1089 int width, int height,
1090 GrPixelConfig config,
1091 sk_sp<SkColorSpace> colorSpace,
1092 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -04001093 GrMipMapped mipMapped,
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001094 GrSurfaceOrigin origin,
1095 const SkSurfaceProps* surfaceProps,
1096 SkBudgeted budgeted) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05001097 SkASSERT(sampleCnt > 0);
Brian Salomon79e4d1b2017-07-13 11:17:11 -04001098 if (this->abandoned()) {
1099 return nullptr;
1100 }
1101
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001102 GrSurfaceDesc desc;
1103 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001104 desc.fWidth = width;
1105 desc.fHeight = height;
1106 desc.fConfig = config;
1107 desc.fSampleCnt = sampleCnt;
1108
Greg Daniele1da1d92017-10-06 15:59:27 -04001109 sk_sp<GrTextureProxy> rtp;
Greg Daniel45d63032017-10-30 13:41:26 -04001110 if (GrMipMapped::kNo == mipMapped) {
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001111 rtp = fContext->fProxyProvider->createProxy(desc, origin, fit, budgeted);
Greg Daniele1da1d92017-10-06 15:59:27 -04001112 } else {
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001113 rtp = fContext->fProxyProvider->createMipMapProxy(desc, origin, budgeted);
Greg Daniele1da1d92017-10-06 15:59:27 -04001114 }
Robert Phillips08c5ec72017-01-30 12:26:47 -05001115 if (!rtp) {
1116 return nullptr;
1117 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001118
Robert Phillips1119dc32017-04-11 12:54:57 -04001119 sk_sp<GrRenderTargetContext> renderTargetContext(
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001120 fContext->fDrawingManager->makeRenderTargetContext(std::move(rtp),
1121 std::move(colorSpace),
1122 surfaceProps));
Robert Phillips1119dc32017-04-11 12:54:57 -04001123 if (!renderTargetContext) {
1124 return nullptr;
1125 }
1126
1127 renderTargetContext->discard();
1128
1129 return renderTargetContext;
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001130}
1131
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001132bool GrContextPriv::abandoned() const {
1133 ASSERT_SINGLE_OWNER_PRIV
1134 return fContext->fDrawingManager->wasAbandoned();
robertphillips77a2e522015-10-17 07:43:27 -07001135}
1136
Brian Salomonaff329b2017-08-11 09:40:37 -04001137std::unique_ptr<GrFragmentProcessor> GrContext::createPMToUPMEffect(
1138 std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) {
Robert Phillips757914d2017-01-25 15:48:30 -05001139 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -04001140 // We have specialized effects that guarantee round-trip conversion for some formats
1141 if (useConfigConversionEffect) {
1142 // We should have already called this->validPMUPMConversionExists() in this case
1143 SkASSERT(fDidTestPMConversions);
1144 // ...and it should have succeeded
1145 SkASSERT(this->validPMUPMConversionExists());
1146
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001147 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
Brian Osman2d2da4f2017-04-12 17:07:22 -04001148 } else {
1149 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
1150 // explicitly round the results. Just do the obvious, naive thing in the shader.
1151 return GrFragmentProcessor::UnpremulOutput(std::move(fp));
Robert Phillips757914d2017-01-25 15:48:30 -05001152 }
1153}
1154
Brian Salomonaff329b2017-08-11 09:40:37 -04001155std::unique_ptr<GrFragmentProcessor> GrContext::createUPMToPMEffect(
1156 std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) {
joshualitt1de610a2016-01-06 08:26:09 -08001157 ASSERT_SINGLE_OWNER
Brian Osman2d2da4f2017-04-12 17:07:22 -04001158 // We have specialized effects that guarantee round-trip conversion for these formats
Brian Osman409e74f2017-04-17 11:48:28 -04001159 if (useConfigConversionEffect) {
1160 // We should have already called this->validPMUPMConversionExists() in this case
1161 SkASSERT(fDidTestPMConversions);
1162 // ...and it should have succeeded
1163 SkASSERT(this->validPMUPMConversionExists());
1164
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001165 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
Brian Osman2d2da4f2017-04-12 17:07:22 -04001166 } else {
1167 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
1168 // explicitly round the results. Just do the obvious, naive thing in the shader.
1169 return GrFragmentProcessor::PremulOutput(std::move(fp));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001170 }
1171}
1172
Brian Osman409e74f2017-04-17 11:48:28 -04001173bool GrContext::validPMUPMConversionExists() {
joshualitt1de610a2016-01-06 08:26:09 -08001174 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -04001175 if (!fDidTestPMConversions) {
Brian Osman28804f32017-04-20 10:24:36 -04001176 fPMUPMConversionsRoundTrip = GrConfigConversionEffect::TestForPreservingPMConversions(this);
Brian Osman409e74f2017-04-17 11:48:28 -04001177 fDidTestPMConversions = true;
1178 }
1179
bsalomon636e8022015-07-29 06:08:46 -07001180 // The PM<->UPM tests fail or succeed together so we only need to check one.
Brian Osman28804f32017-04-20 10:24:36 -04001181 return fPMUPMConversionsRoundTrip;
bsalomon636e8022015-07-29 06:08:46 -07001182}
1183
bsalomon37f9a262015-02-02 13:00:10 -08001184//////////////////////////////////////////////////////////////////////////////
1185
Robert Phillipsfc711a22018-02-13 17:03:00 -05001186// DDL TODO: remove 'maxResources'
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001187void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -08001188 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001189 if (maxResources) {
1190 *maxResources = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -08001191 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001192 if (maxResourceBytes) {
1193 *maxResourceBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -08001194 }
1195}
1196
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001197void GrContext::setResourceCacheLimits(int maxResources, size_t maxResourceBytes) {
joshualitt1de610a2016-01-06 08:26:09 -08001198 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001199 fResourceCache->setLimits(maxResources, maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -08001200}
1201
ericrk0a5fa482015-09-15 14:16:10 -07001202//////////////////////////////////////////////////////////////////////////////
ericrk0a5fa482015-09-15 14:16:10 -07001203void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -08001204 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -07001205 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
1206}
Brian Osman71a18892017-08-10 10:23:25 -04001207
1208//////////////////////////////////////////////////////////////////////////////
1209
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001210SkString GrContextPriv::dump() const {
Brian Osman71a18892017-08-10 10:23:25 -04001211 SkDynamicMemoryWStream stream;
1212 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
1213 writer.beginObject();
1214
1215 static const char* kBackendStr[] = {
1216 "Metal",
1217 "OpenGL",
1218 "Vulkan",
1219 "Mock",
1220 };
1221 GR_STATIC_ASSERT(0 == kMetal_GrBackend);
1222 GR_STATIC_ASSERT(1 == kOpenGL_GrBackend);
1223 GR_STATIC_ASSERT(2 == kVulkan_GrBackend);
1224 GR_STATIC_ASSERT(3 == kMock_GrBackend);
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001225 writer.appendString("backend", kBackendStr[fContext->fBackend]);
Brian Osman71a18892017-08-10 10:23:25 -04001226
1227 writer.appendName("caps");
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001228 fContext->fCaps->dumpJSON(&writer);
Brian Osman71a18892017-08-10 10:23:25 -04001229
1230 writer.appendName("gpu");
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001231 fContext->fGpu->dumpJSON(&writer);
Brian Osman71a18892017-08-10 10:23:25 -04001232
1233 // Flush JSON to the memory stream
1234 writer.endObject();
1235 writer.flush();
1236
1237 // Null terminate the JSON data in the memory stream
1238 stream.write8(0);
1239
1240 // Allocate a string big enough to hold all the data, then copy out of the stream
1241 SkString result(stream.bytesWritten());
1242 stream.copyToAndReset(result.writable_str());
1243 return result;
1244}