blob: f88da28970f3bf2fd86604b840652159414b0c83 [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"
Brian Salomondcbb9d92017-07-19 10:53:20 -040027#include "GrTracing.h"
Matt Sarett485c4992017-02-14 14:18:27 -050028#include "SkConvertPixels.h"
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -050029#include "SkDeferredDisplayList.h"
Brian Osman3b655982017-03-07 16:58:08 -050030#include "SkGr.h"
Mike Reed7fcfb622018-02-09 13:26:46 -050031#include "SkImageInfoPriv.h"
Brian Osman71a18892017-08-10 10:23:25 -040032#include "SkJSONWriter.h"
Brian Osman51279982017-08-23 10:12:00 -040033#include "SkMakeUnique.h"
34#include "SkTaskGroup.h"
Matt Sarettc7b29082017-02-09 16:22:39 -050035#include "SkUnPreMultiplyPriv.h"
joshualitt5478d422014-11-14 16:00:38 -080036#include "effects/GrConfigConversionEffect.h"
Brian Salomon5f33a8c2018-02-26 14:32:39 -050037#include "text/GrTextBlobCache.h"
Greg Danielb76a72a2017-07-13 15:07:54 -040038
Robert Phillipse78b7252017-04-06 07:59:41 -040039#define ASSERT_OWNED_PROXY(P) \
40SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == this)
Robert Phillips7ee385e2017-03-30 08:02:11 -040041#define ASSERT_OWNED_PROXY_PRIV(P) \
42SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == fContext)
43
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000044#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt1de610a2016-01-06 08:26:09 -080045#define ASSERT_SINGLE_OWNER \
46 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fSingleOwner);)
robertphillips4fd74ae2016-08-03 14:26:53 -070047#define ASSERT_SINGLE_OWNER_PRIV \
48 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fContext->fSingleOwner);)
robertphillips7761d612016-05-16 09:14:53 -070049#define RETURN_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return; }
Robert Phillips7ee385e2017-03-30 08:02:11 -040050#define RETURN_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return; }
robertphillips7761d612016-05-16 09:14:53 -070051#define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return false; }
Robert Phillipse78b7252017-04-06 07:59:41 -040052#define RETURN_FALSE_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return false; }
robertphillips7761d612016-05-16 09:14:53 -070053#define RETURN_NULL_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000054
robertphillipsea461502015-05-26 11:38:03 -070055////////////////////////////////////////////////////////////////////////////////
56
joshualitt0acd0d32015-05-07 08:23:19 -070057static int32_t gNextID = 1;
58static int32_t next_id() {
59 int32_t id;
60 do {
61 id = sk_atomic_inc(&gNextID);
62 } while (id == SK_InvalidGenID);
63 return id;
64}
65
Robert Phillipsfde6fa02018-03-02 08:53:14 -050066GrContext::GrContext(GrBackend backend, int32_t id)
67 : fBackend(backend)
68 , fUniqueID(SK_InvalidGenID == id ? next_id() : id) {
halcanary96fcdcc2015-08-27 07:41:13 -070069 fResourceCache = nullptr;
70 fResourceProvider = nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050071 fProxyProvider = nullptr;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050072 fGlyphCache = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000073}
74
Robert Phillipsfde6fa02018-03-02 08:53:14 -050075bool GrContext::initCommon(const GrContextOptions& options) {
Greg Danielb76a72a2017-07-13 15:07:54 -040076 ASSERT_SINGLE_OWNER
Robert Phillipsfde6fa02018-03-02 08:53:14 -050077 SkASSERT(fCaps); // needs to have been initialized by derived classes
78 SkASSERT(fThreadSafeProxy); // needs to have been initialized by derived classes
Robert Phillips88260b52018-01-19 12:56:09 -050079
80 if (fGpu) {
81 fCaps = fGpu->refCaps();
82 fResourceCache = new GrResourceCache(fCaps.get(), fUniqueID);
Robert Phillips4150eea2018-02-07 17:08:21 -050083 fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, &fSingleOwner,
84 options.fExplicitlyAllocateGPUResources);
Robert Phillips88260b52018-01-19 12:56:09 -050085 }
86
Robert Phillips1afd4cd2018-01-08 13:40:32 -050087 fProxyProvider = new GrProxyProvider(fResourceProvider, fResourceCache, fCaps, &fSingleOwner);
Robert Phillips88260b52018-01-19 12:56:09 -050088
89 if (fResourceCache) {
90 fResourceCache->setProxyProvider(fProxyProvider);
91 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050092
Brian Osman46da1cc2017-02-14 14:15:48 -050093 fDisableGpuYUVConversion = options.fDisableGpuYUVConversion;
Brian Osman8a83ca42018-02-12 14:32:17 -050094 fSharpenMipmappedTextures = options.fSharpenMipmappedTextures;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000095 fDidTestPMConversions = false;
96
bsalomon6b2552f2016-09-15 13:50:26 -070097 GrPathRendererChain::Options prcOptions;
bsalomon39ef7fb2016-09-21 11:16:05 -070098 prcOptions.fAllowPathMaskCaching = options.fAllowPathMaskCaching;
Brian Osman195c05b2017-08-30 15:14:04 -040099#if GR_TEST_UTILS
csmartdalton008b9d82017-02-22 12:00:42 -0700100 prcOptions.fGpuPathRenderers = options.fGpuPathRenderers;
Brian Osman195c05b2017-08-30 15:14:04 -0400101#endif
Brian Osmanb350ae22017-08-29 16:15:39 -0400102 if (options.fDisableDistanceFieldPaths) {
Brian Osman195c05b2017-08-30 15:14:04 -0400103 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
Brian Osmanb350ae22017-08-29 16:15:39 -0400104 }
Brian Salomonaf597482017-11-07 16:23:34 -0500105
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500106 if (!fResourceCache) {
107 // DDL TODO: remove this crippling of the path renderer chain
108 // Disable the small path renderer bc of the proxies in the atlas. They need to be
109 // unified when the opLists are added back to the destination drawing manager.
110 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
111 }
112
Brian Salomonaf597482017-11-07 16:23:34 -0500113 GrAtlasTextContext::Options atlasTextContextOptions;
114 atlasTextContextOptions.fMaxDistanceFieldFontSize = options.fGlyphsAsPathsFontSize;
115 atlasTextContextOptions.fMinDistanceFieldFontSize = options.fMinDistanceFieldFontSize;
Brian Salomonb5086962017-12-13 10:59:33 -0500116 atlasTextContextOptions.fDistanceFieldVerticesAlwaysHaveW = false;
117#if SK_SUPPORT_ATLAS_TEXT
118 if (GrContextOptions::Enable::kYes == options.fDistanceFieldGlyphVerticesAlwaysHaveW) {
119 atlasTextContextOptions.fDistanceFieldVerticesAlwaysHaveW = true;
120 }
121#endif
Brian Salomonaf597482017-11-07 16:23:34 -0500122
Robert Phillips4150eea2018-02-07 17:08:21 -0500123 fDrawingManager.reset(new GrDrawingManager(this, prcOptions, atlasTextContextOptions,
124 &fSingleOwner, options.fSortRenderTargets));
joshualitt7c3a2f82015-03-31 13:32:05 -0700125
Robert Phillipsaf4adef2018-03-07 11:59:37 -0500126 fGlyphCache = new GrGlyphCache(fCaps.get(), options.fGlyphCacheTextureMaximumBytes);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500127
Robert Phillips303cd582018-02-14 18:54:01 -0500128 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB,
129 this, this->uniqueID(), SkToBool(fGpu)));
Brian Salomon91a3e522017-06-23 10:58:19 -0400130
Robert Phillipsfde6fa02018-03-02 08:53:14 -0500131 // DDL TODO: we need to think through how the task group & persistent cache
132 // get passed on to/shared between all the DDLRecorders created with this context.
Brian Osman51279982017-08-23 10:12:00 -0400133 if (options.fExecutor) {
134 fTaskGroup = skstd::make_unique<SkTaskGroup>(*options.fExecutor);
135 }
136
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400137 fPersistentCache = options.fPersistentCache;
138
Brian Salomon91a3e522017-06-23 10:58:19 -0400139 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000140}
141
bsalomon@google.com27847de2011-02-22 20:59:41 +0000142GrContext::~GrContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800143 ASSERT_SINGLE_OWNER
144
Robert Phillips2e6feed2018-01-22 15:27:20 -0500145 if (fDrawingManager) {
146 fDrawingManager->cleanup();
147 }
robertphillips2334fb62015-06-17 05:43:33 -0700148
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000149 for (int i = 0; i < fCleanUpData.count(); ++i) {
150 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
151 }
152
halcanary385fe4d2015-08-26 13:07:48 -0700153 delete fResourceProvider;
154 delete fResourceCache;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500155 delete fProxyProvider;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500156 delete fGlyphCache;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000157}
158
bungeman6bd52842016-10-27 09:30:08 -0700159sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
bungeman6bd52842016-10-27 09:30:08 -0700160 return fThreadSafeProxy;
bsalomon41b952c2016-03-11 06:46:33 -0800161}
162
Robert Phillipsfc711a22018-02-13 17:03:00 -0500163SkSurfaceCharacterization GrContextThreadSafeProxy::createCharacterization(
164 size_t cacheMaxResourceBytes,
165 const SkImageInfo& ii, const GrBackendFormat& backendFormat,
166 int sampleCnt, GrSurfaceOrigin origin,
167 const SkSurfaceProps& surfaceProps,
168 bool isMipMapped) {
169 if (!backendFormat.isValid()) {
170 return SkSurfaceCharacterization(); // return an invalid characterization
171 }
172
173 // We're assuming GrFSAAType::kMixedSamples will never be specified via this code path
174 GrFSAAType FSAAType = sampleCnt > 1 ? GrFSAAType::kUnifiedMSAA : GrFSAAType::kNone;
175
176 if (!fCaps->mipMapSupport()) {
177 isMipMapped = false;
178 }
179
180 GrPixelConfig config = kUnknown_GrPixelConfig;
181 if (!fCaps->getConfigFromBackendFormat(backendFormat, ii.colorType(), &config)) {
182 return SkSurfaceCharacterization(); // return an invalid characterization
183 }
184
185 // This surface characterization factory assumes that the resulting characterization is
186 // textureable.
187 if (!fCaps->isConfigTexturable(config)) {
188 return SkSurfaceCharacterization(); // return an invalid characterization
189 }
190
191 return SkSurfaceCharacterization(sk_ref_sp<GrContextThreadSafeProxy>(this),
192 cacheMaxResourceBytes,
193 origin, ii.width(), ii.height(), config, FSAAType, sampleCnt,
194 SkSurfaceCharacterization::Textureable(true),
195 SkSurfaceCharacterization::MipMapped(isMipMapped),
196 ii.refColorSpace(), surfaceProps);
197}
198
bsalomon2354f842014-07-28 13:48:36 -0700199void GrContext::abandonContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800200 ASSERT_SINGLE_OWNER
201
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500202 fProxyProvider->abandon();
bsalomond309e7a2015-04-30 14:18:54 -0700203 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800204
205 // Need to abandon the drawing manager first so all the render targets
206 // will be released/forgotten before they too are abandoned.
207 fDrawingManager->abandon();
208
bsalomon@google.com205d4602011-04-25 12:43:45 +0000209 // abandon first to so destructors
210 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800211 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700212
bsalomon6e2aad42016-04-01 11:54:31 -0700213 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
214
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500215 fGlyphCache->freeAll();
bsalomon6e2aad42016-04-01 11:54:31 -0700216 fTextBlobCache->freeAll();
217}
218
219void GrContext::releaseResourcesAndAbandonContext() {
220 ASSERT_SINGLE_OWNER
221
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500222 fProxyProvider->abandon();
bsalomon6e2aad42016-04-01 11:54:31 -0700223 fResourceProvider->abandon();
224
225 // Need to abandon the drawing manager first so all the render targets
226 // will be released/forgotten before they too are abandoned.
227 fDrawingManager->abandon();
228
229 // Release all resources in the backend 3D API.
230 fResourceCache->releaseAll();
231
232 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000233
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500234 fGlyphCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700235 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000236}
237
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000238void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800239 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000240 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000241}
242
243void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800244 ASSERT_SINGLE_OWNER
245
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500246 fGlyphCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700247
248 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700249
250 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000251}
252
Jim Van Verth76d917c2017-12-13 09:26:37 -0500253void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Brian Salomon5e150852017-03-22 14:53:13 -0400254 ASSERT_SINGLE_OWNER
Jim Van Verth76d917c2017-12-13 09:26:37 -0500255 fResourceCache->purgeAsNeeded();
256 fResourceCache->purgeResourcesNotUsedSince(GrStdSteadyClock::now() - msNotUsed);
257
258 fTextBlobCache->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400259}
260
Derek Sollenberger5480a182017-05-25 16:43:59 -0400261void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
262 ASSERT_SINGLE_OWNER
263 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
264}
265
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000266void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800267 ASSERT_SINGLE_OWNER
268
bsalomon71cb0c22014-11-14 12:10:14 -0800269 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800270 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800271 }
272 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800273 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800274 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000275}
276
Derek Sollenbergeree479142017-05-24 11:41:33 -0400277size_t GrContext::getResourceCachePurgeableBytes() const {
278 ASSERT_SINGLE_OWNER
279 return fResourceCache->getPurgeableBytes();
280}
281
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000282////////////////////////////////////////////////////////////////////////////////
283
Brian Salomonbdecacf2018-02-02 20:32:49 -0500284bool GrContext::colorTypeSupportedAsImage(SkColorType colorType) const {
285 GrPixelConfig config = SkImageInfo2GrPixelConfig(colorType, nullptr, *this->caps());
286 return this->caps()->isConfigTexturable(config);
287}
288
289int GrContext::maxSurfaceSampleCountForColorType(SkColorType colorType) const {
290 GrPixelConfig config = SkImageInfo2GrPixelConfig(colorType, nullptr, *this->caps());
291 return this->caps()->maxRenderTargetSampleCount(config);
292}
293
294////////////////////////////////////////////////////////////////////////////////
295
joshualitt0db6dfa2015-04-10 07:01:30 -0700296void GrContext::TextBlobCacheOverBudgetCB(void* data) {
297 SkASSERT(data);
Brian Osman11052242016-10-27 14:47:55 -0400298 // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on
299 // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls
300 // to below the GrContext level, but this is not trivial because they call drawPath on
301 // SkGpuDevice.
joshualitt0db6dfa2015-04-10 07:01:30 -0700302 GrContext* context = reinterpret_cast<GrContext*>(data);
303 context->flush();
304}
305
bsalomon@google.com27847de2011-02-22 20:59:41 +0000306////////////////////////////////////////////////////////////////////////////////
307
bsalomonb77a9072016-09-07 10:02:04 -0700308void GrContext::flush() {
joshualitt1de610a2016-01-06 08:26:09 -0800309 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700310 RETURN_IF_ABANDONED
Robert Phillips7ee385e2017-03-30 08:02:11 -0400311
312 fDrawingManager->flush(nullptr);
313}
314
Greg Daniel51316782017-08-02 15:10:09 +0000315GrSemaphoresSubmitted GrContext::flushAndSignalSemaphores(int numSemaphores,
316 GrBackendSemaphore signalSemaphores[]) {
317 ASSERT_SINGLE_OWNER
318 if (fDrawingManager->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; }
319
320 return fDrawingManager->flush(nullptr, numSemaphores, signalSemaphores);
321}
322
Robert Phillips7ee385e2017-03-30 08:02:11 -0400323void GrContextPriv::flush(GrSurfaceProxy* proxy) {
324 ASSERT_SINGLE_OWNER_PRIV
325 RETURN_IF_ABANDONED_PRIV
326 ASSERT_OWNED_PROXY_PRIV(proxy);
327
328 fContext->fDrawingManager->flush(proxy);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000329}
330
Brian Salomonc320b152018-02-20 14:05:36 -0500331bool sw_convert_to_premul(GrColorType srcColorType, int width, int height, size_t inRowBytes,
bsalomon81beccc2014-10-13 12:32:55 -0700332 const void* inPixels, size_t outRowBytes, void* outPixels) {
Brian Salomonc320b152018-02-20 14:05:36 -0500333 SkColorType colorType = GrColorTypeToSkColorType(srcColorType);
334 if (kUnknown_SkColorType == colorType || 4 != SkColorTypeBytesPerPixel(colorType)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000335 return false;
336 }
bsalomon81beccc2014-10-13 12:32:55 -0700337
Matt Sarettc7b29082017-02-09 16:22:39 -0500338 for (int y = 0; y < height; y++) {
339 SkOpts::RGBA_to_rgbA((uint32_t*) outPixels, inPixels, width);
340 outPixels = SkTAddOffset<void>(outPixels, outRowBytes);
341 inPixels = SkTAddOffset<const void>(inPixels, inRowBytes);
342 }
bsalomon81beccc2014-10-13 12:32:55 -0700343
Matt Sarettc7b29082017-02-09 16:22:39 -0500344 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000345}
346
Brian Salomonc320b152018-02-20 14:05:36 -0500347// TODO: This will be removed when GrSurfaceContexts are aware of their color types.
348// (skbug.com/6718)
Brian Osmand2ca59a2017-04-13 14:03:57 -0400349static bool valid_premul_config(GrPixelConfig config) {
Brian Salomonc320b152018-02-20 14:05:36 -0500350 switch (config) {
351 case kUnknown_GrPixelConfig: return false;
352 case kAlpha_8_GrPixelConfig: return false;
353 case kGray_8_GrPixelConfig: return false;
354 case kRGB_565_GrPixelConfig: return false;
355 case kRGBA_4444_GrPixelConfig: return true;
356 case kRGBA_8888_GrPixelConfig: return true;
357 case kBGRA_8888_GrPixelConfig: return true;
358 case kSRGBA_8888_GrPixelConfig: return true;
359 case kSBGRA_8888_GrPixelConfig: return true;
Brian Osman10fc6fd2018-03-02 11:01:10 -0500360 case kRGBA_1010102_GrPixelConfig: return true;
Brian Salomonc320b152018-02-20 14:05:36 -0500361 case kRGBA_float_GrPixelConfig: return true;
362 case kRG_float_GrPixelConfig: return false;
363 case kAlpha_half_GrPixelConfig: return false;
364 case kRGBA_half_GrPixelConfig: return true;
365 case kAlpha_8_as_Alpha_GrPixelConfig: return false;
366 case kAlpha_8_as_Red_GrPixelConfig: return false;
367 case kAlpha_half_as_Red_GrPixelConfig: return false;
368 case kGray_8_as_Lum_GrPixelConfig: return false;
369 case kGray_8_as_Red_GrPixelConfig: return false;
370 }
371 SK_ABORT("Invalid GrPixelConfig");
372 return false;
Brian Osmance425512017-03-22 14:37:50 -0400373}
374
Brian Salomonc320b152018-02-20 14:05:36 -0500375static bool valid_premul_color_type(GrColorType ct) {
376 switch (ct) {
Brian Osman10fc6fd2018-03-02 11:01:10 -0500377 case GrColorType::kUnknown: return false;
378 case GrColorType::kAlpha_8: return false;
379 case GrColorType::kRGB_565: return false;
380 case GrColorType::kABGR_4444: return true;
381 case GrColorType::kRGBA_8888: return true;
382 case GrColorType::kBGRA_8888: return true;
383 case GrColorType::kRGBA_1010102: return true;
384 case GrColorType::kGray_8: return false;
385 case GrColorType::kAlpha_F16: return false;
386 case GrColorType::kRGBA_F16: return true;
387 case GrColorType::kRG_F32: return false;
388 case GrColorType::kRGBA_F32: return true;
Brian Salomonc320b152018-02-20 14:05:36 -0500389 }
390 SK_ABORT("Invalid GrColorType");
391 return false;
392}
393
394static bool valid_pixel_conversion(GrColorType cpuColorType, GrPixelConfig gpuConfig,
Brian Osmand2ca59a2017-04-13 14:03:57 -0400395 bool premulConversion) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400396 // We only allow premul <-> unpremul conversions for some formats
Brian Salomonc320b152018-02-20 14:05:36 -0500397 if (premulConversion &&
398 (!valid_premul_color_type(cpuColorType) || !valid_premul_config(gpuConfig))) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400399 return false;
400 }
401
402 return true;
403}
404
Brian Salomonc320b152018-02-20 14:05:36 -0500405static bool pm_upm_must_round_trip(GrColorType cpuColorType, const SkColorSpace* cpuColorSpace) {
406 return !cpuColorSpace &&
407 (GrColorType::kRGBA_8888 == cpuColorType || GrColorType::kBGRA_8888 == cpuColorType);
Brian Osman409e74f2017-04-17 11:48:28 -0400408}
409
Brian Salomonc320b152018-02-20 14:05:36 -0500410// TODO: This will be removed when GrSurfaceContexts are aware of their color types.
411// (skbug.com/6718)
412static bool pm_upm_must_round_trip(GrPixelConfig surfaceConfig,
413 const SkColorSpace* surfaceColorSpace) {
414 return !surfaceColorSpace &&
415 (kRGBA_8888_GrPixelConfig == surfaceConfig || kBGRA_8888_GrPixelConfig == surfaceConfig);
416}
417
418static GrSRGBConversion determine_write_pixels_srgb_conversion(GrColorType srcColorType,
419 const SkColorSpace* srcColorSpace,
420 GrSRGBEncoded dstSRGBEncoded,
421 const SkColorSpace* dstColorSpace,
422 const GrCaps& caps) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500423 // No support for sRGB-encoded alpha.
Brian Salomonc320b152018-02-20 14:05:36 -0500424 if (GrColorTypeIsAlphaOnly(srcColorType)) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500425 return GrSRGBConversion::kNone;
426 }
Brian Salomonc320b152018-02-20 14:05:36 -0500427 // No conversions without GPU support for sRGB. (Legacy mode)
428 if (!caps.srgbSupport()) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500429 return GrSRGBConversion::kNone;
430 }
Brian Salomonc320b152018-02-20 14:05:36 -0500431 // If the GrSurfaceContext has no color space then it is in legacy mode.
432 if (!dstColorSpace) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500433 return GrSRGBConversion::kNone;
434 }
435
436 bool srcColorSpaceIsSRGB = srcColorSpace && srcColorSpace->gammaCloseToSRGB();
437 bool dstColorSpaceIsSRGB = dstColorSpace->gammaCloseToSRGB();
438
439 // For now we are assuming that if color space of the dst does not have sRGB gamma then the
440 // texture format is not sRGB encoded and vice versa. Note that we already checked for "legacy"
Brian Salomonc320b152018-02-20 14:05:36 -0500441 // mode being forced on by caps above. This may change in the future. We will then have to
442 // perform shader based conversions.
Brian Salomon9b009bb2018-02-14 13:53:55 -0500443 SkASSERT(dstColorSpaceIsSRGB == (GrSRGBEncoded::kYes == dstSRGBEncoded));
444
Brian Salomon9b009bb2018-02-14 13:53:55 -0500445 if (srcColorSpaceIsSRGB == dstColorSpaceIsSRGB) {
446 return GrSRGBConversion::kNone;
447 }
448 return srcColorSpaceIsSRGB ? GrSRGBConversion::kSRGBToLinear : GrSRGBConversion::kLinearToSRGB;
449}
450
Brian Salomonc320b152018-02-20 14:05:36 -0500451static GrSRGBConversion determine_read_pixels_srgb_conversion(GrSRGBEncoded srcSRGBEncoded,
452 const SkColorSpace* srcColorSpace,
453 GrColorType dstColorType,
454 const SkColorSpace* dstColorSpace,
455 const GrCaps& caps) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500456 // This is symmetrical with the write version.
Brian Salomonc320b152018-02-20 14:05:36 -0500457 switch (determine_write_pixels_srgb_conversion(dstColorType, dstColorSpace, srcSRGBEncoded,
458 srcColorSpace, caps)) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500459 case GrSRGBConversion::kNone: return GrSRGBConversion::kNone;
460 case GrSRGBConversion::kLinearToSRGB: return GrSRGBConversion::kSRGBToLinear;
461 case GrSRGBConversion::kSRGBToLinear: return GrSRGBConversion::kLinearToSRGB;
462 }
463 return GrSRGBConversion::kNone;
464}
465
Brian Salomonc320b152018-02-20 14:05:36 -0500466bool GrContextPriv::writeSurfacePixels(GrSurfaceContext* dst, int left, int top, int width,
467 int height, GrColorType srcColorType,
468 SkColorSpace* srcColorSpace, const void* buffer,
469 size_t rowBytes, uint32_t pixelOpsFlags) {
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500470#ifndef SK_LEGACY_GPU_PIXEL_OPS
471 return this->writeSurfacePixels2(dst, left, top, width, height, srcColorType, srcColorSpace,
472 buffer, rowBytes, pixelOpsFlags);
473#endif
474
Brian Osmanb62ea222016-12-22 11:12:16 -0500475 // TODO: Color space conversion
476
Robert Phillipse78b7252017-04-06 07:59:41 -0400477 ASSERT_SINGLE_OWNER_PRIV
478 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400479 SkASSERT(dst);
480 ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy());
Brian Salomondcbb9d92017-07-19 10:53:20 -0400481 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels", fContext);
bsalomon6c6f6582015-09-10 08:12:46 -0700482
Robert Phillips6be756b2018-01-16 15:07:54 -0500483 if (!dst->asSurfaceProxy()->instantiate(this->resourceProvider())) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400484 return false;
485 }
486
Robert Phillips16d8ec62017-07-27 16:16:25 -0400487 GrSurfaceProxy* dstProxy = dst->asSurfaceProxy();
488 GrSurface* dstSurface = dstProxy->priv().peekSurface();
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400489
Brian Osmand2ca59a2017-04-13 14:03:57 -0400490 // 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 -0400491 const bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
Brian Salomonc320b152018-02-20 14:05:36 -0500492
493 if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400494 return false;
495 }
496
Brian Osman409e74f2017-04-17 11:48:28 -0400497 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
498 // without any color spaces attached, and the caller wants us to premul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400499 bool useConfigConversionEffect =
Brian Salomonc320b152018-02-20 14:05:36 -0500500 premul && pm_upm_must_round_trip(srcColorType, srcColorSpace) &&
Brian Salomonf3569f02017-10-24 12:52:33 -0400501 pm_upm_must_round_trip(dstProxy->config(), dst->colorSpaceInfo().colorSpace());
Brian Osman409e74f2017-04-17 11:48:28 -0400502
503 // Are we going to try to premul as part of a draw? For the non-legacy case, we always allow
504 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
505 bool premulOnGpu = premul &&
506 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon81beccc2014-10-13 12:32:55 -0700507
bsalomone8d21e82015-07-16 08:23:13 -0700508 // 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 -0700509 // necessary and because GrGpu::getWritePixelsInfo requires it.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400510 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
Brian Salomonc320b152018-02-20 14:05:36 -0500511 GrColorTypeBytesPerPixel(srcColorType), &left, &top,
512 &width, &height, &buffer, &rowBytes)) {
bsalomone8d21e82015-07-16 08:23:13 -0700513 return false;
514 }
515
Brian Osman409e74f2017-04-17 11:48:28 -0400516 GrGpu::DrawPreference drawPreference = premulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
517 : GrGpu::kNoDraw_DrawPreference;
bsalomonf0674512015-07-28 13:26:15 -0700518 GrGpu::WritePixelTempDrawInfo tempDrawInfo;
Brian Salomon9b009bb2018-02-14 13:53:55 -0500519 GrSRGBConversion srgbConversion = determine_write_pixels_srgb_conversion(
Brian Salomonc320b152018-02-20 14:05:36 -0500520 srcColorType, srcColorSpace, GrPixelConfigIsSRGBEncoded(dstProxy->config()),
521 dst->colorSpaceInfo().colorSpace(), *fContext->caps());
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400522 if (!fContext->fGpu->getWritePixelsInfo(dstSurface, dstProxy->origin(), width, height,
Brian Salomonc320b152018-02-20 14:05:36 -0500523 srcColorType, srgbConversion, &drawPreference,
Brian Salomon9b009bb2018-02-14 13:53:55 -0500524 &tempDrawInfo)) {
bsalomonf0674512015-07-28 13:26:15 -0700525 return false;
526 }
527
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400528 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400529 this->flush(nullptr); // MDB TODO: tighten this
bsalomonf0674512015-07-28 13:26:15 -0700530 }
531
Robert Phillips2f493142017-03-02 18:18:38 -0500532 sk_sp<GrTextureProxy> tempProxy;
bsalomonf0674512015-07-28 13:26:15 -0700533 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500534 tempProxy = this->proxyProvider()->createProxy(tempDrawInfo.fTempSurfaceDesc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500535 kTopLeft_GrSurfaceOrigin,
536 SkBackingFit::kApprox, SkBudgeted::kYes);
Robert Phillips2f493142017-03-02 18:18:38 -0500537 if (!tempProxy && GrGpu::kRequireDraw_DrawPreference == drawPreference) {
bsalomonf0674512015-07-28 13:26:15 -0700538 return false;
539 }
540 }
541
542 // temp buffer for doing sw premul conversion, if needed.
543 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
Brian Osman409e74f2017-04-17 11:48:28 -0400544 // We need to do sw premul if we were unable to create a RT for drawing, or if we can't do the
545 // premul on the GPU
546 if (premul && (!tempProxy || !premulOnGpu)) {
547 size_t tmpRowBytes = 4 * width;
548 tmpPixels.reset(width * height);
Brian Salomonc320b152018-02-20 14:05:36 -0500549 if (!sw_convert_to_premul(srcColorType, width, height, rowBytes, buffer, tmpRowBytes,
Brian Osman409e74f2017-04-17 11:48:28 -0400550 tmpPixels.get())) {
551 return false;
bsalomonf0674512015-07-28 13:26:15 -0700552 }
Brian Osman409e74f2017-04-17 11:48:28 -0400553 rowBytes = tmpRowBytes;
554 buffer = tmpPixels.get();
bsalomonf0674512015-07-28 13:26:15 -0700555 }
Brian Osman409e74f2017-04-17 11:48:28 -0400556
557 if (tempProxy) {
Brian Osman2240be92017-10-18 13:15:13 -0400558 auto fp = GrSimpleTextureEffect::Make(tempProxy, SkMatrix::I());
Brian Osman409e74f2017-04-17 11:48:28 -0400559 if (premulOnGpu) {
560 fp = fContext->createUPMToPMEffect(std::move(fp), useConfigConversionEffect);
bsalomon81beccc2014-10-13 12:32:55 -0700561 }
Brian Osman409e74f2017-04-17 11:48:28 -0400562 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400563 if (!fp) {
564 return false;
565 }
Brian Osman409e74f2017-04-17 11:48:28 -0400566
Robert Phillips6be756b2018-01-16 15:07:54 -0500567 if (!tempProxy->instantiate(this->resourceProvider())) {
Brian Osman409e74f2017-04-17 11:48:28 -0400568 return false;
569 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400570 GrTexture* texture = tempProxy->priv().peekTexture();
Robert Phillips7bbbf622017-10-17 07:36:59 -0400571
572 if (tempProxy->priv().hasPendingIO()) {
573 this->flush(tempProxy.get());
574 }
575
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400576 if (!fContext->fGpu->writePixels(texture, tempProxy->origin(), 0, 0, width, height,
Brian Salomonc320b152018-02-20 14:05:36 -0500577 tempDrawInfo.fWriteColorType, buffer, rowBytes)) {
Brian Osman409e74f2017-04-17 11:48:28 -0400578 return false;
579 }
Robert Phillips7bbbf622017-10-17 07:36:59 -0400580 tempProxy = nullptr;
581
Brian Osman409e74f2017-04-17 11:48:28 -0400582 SkMatrix matrix;
583 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400584 GrRenderTargetContext* renderTargetContext = dst->asRenderTargetContext();
Brian Osman409e74f2017-04-17 11:48:28 -0400585 if (!renderTargetContext) {
586 return false;
587 }
588 GrPaint paint;
589 paint.addColorFragmentProcessor(std::move(fp));
590 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonf3569f02017-10-24 12:52:33 -0400591 paint.setAllowSRGBInputs(dst->colorSpaceInfo().isGammaCorrect() ||
592 GrPixelConfigIsSRGB(dst->colorSpaceInfo().config()));
Brian Osman409e74f2017-04-17 11:48:28 -0400593 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
594 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, matrix, rect,
595 nullptr);
596
597 if (kFlushWrites_PixelOp & pixelOpsFlags) {
598 this->flushSurfaceWrites(renderTargetContext->asRenderTargetProxy());
599 }
600 } else {
Brian Salomonc320b152018-02-20 14:05:36 -0500601 return fContext->fGpu->writePixels(dstSurface, dstProxy->origin(), left, top, width, height,
602 srcColorType, buffer, rowBytes);
bsalomon81beccc2014-10-13 12:32:55 -0700603 }
bsalomon81beccc2014-10-13 12:32:55 -0700604 return true;
605}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000606
Brian Salomonc320b152018-02-20 14:05:36 -0500607bool GrContextPriv::readSurfacePixels(GrSurfaceContext* src, int left, int top, int width,
608 int height, GrColorType dstColorType,
609 SkColorSpace* dstColorSpace, void* buffer, size_t rowBytes,
610 uint32_t flags) {
Brian Osmanb62ea222016-12-22 11:12:16 -0500611 // TODO: Color space conversion
612
Robert Phillipse78b7252017-04-06 07:59:41 -0400613 ASSERT_SINGLE_OWNER_PRIV
614 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400615 SkASSERT(src);
616 ASSERT_OWNED_PROXY_PRIV(src->asSurfaceProxy());
Brian Salomondcbb9d92017-07-19 10:53:20 -0400617 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "readSurfacePixels", fContext);
bsalomon32ab2602015-09-09 18:57:49 -0700618
Robert Phillipse78b7252017-04-06 07:59:41 -0400619 // MDB TODO: delay this instantiation until later in the method
Robert Phillips6be756b2018-01-16 15:07:54 -0500620 if (!src->asSurfaceProxy()->instantiate(this->resourceProvider())) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400621 return false;
622 }
623
Robert Phillips16d8ec62017-07-27 16:16:25 -0400624 GrSurfaceProxy* srcProxy = src->asSurfaceProxy();
625 GrSurface* srcSurface = srcProxy->priv().peekSurface();
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400626
Brian Osmand2ca59a2017-04-13 14:03:57 -0400627 // The src is premul but the dst is unpremul -> unpremul the src after or as part of the read
628 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
Brian Salomonc320b152018-02-20 14:05:36 -0500629
630 if (!valid_pixel_conversion(dstColorType, srcProxy->config(), unpremul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400631 return false;
632 }
633
Brian Osman409e74f2017-04-17 11:48:28 -0400634 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
635 // without any color spaces attached, and the caller wants us to unpremul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400636 bool useConfigConversionEffect =
Brian Salomonf3569f02017-10-24 12:52:33 -0400637 unpremul &&
638 pm_upm_must_round_trip(srcProxy->config(), src->colorSpaceInfo().colorSpace()) &&
Brian Salomonc320b152018-02-20 14:05:36 -0500639 pm_upm_must_round_trip(dstColorType, dstColorSpace);
Brian Osman409e74f2017-04-17 11:48:28 -0400640
641 // Are we going to try to unpremul as part of a draw? For the non-legacy case, we always allow
642 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
643 bool unpremulOnGpu = unpremul &&
644 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon6c6f6582015-09-10 08:12:46 -0700645
bsalomone8d21e82015-07-16 08:23:13 -0700646 // Adjust the params so that if we wind up using an intermediate surface we've already done
647 // all the trimming and the temporary can be the min size required.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400648 if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(),
Brian Salomonc320b152018-02-20 14:05:36 -0500649 GrColorTypeBytesPerPixel(dstColorType), &left, &top,
650 &width, &height, &buffer, &rowBytes)) {
bsalomone8d21e82015-07-16 08:23:13 -0700651 return false;
652 }
653
Brian Osman409e74f2017-04-17 11:48:28 -0400654 GrGpu::DrawPreference drawPreference = unpremulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
655 : GrGpu::kNoDraw_DrawPreference;
bsalomon39826022015-07-23 08:07:21 -0700656 GrGpu::ReadPixelTempDrawInfo tempDrawInfo;
Brian Salomon9b009bb2018-02-14 13:53:55 -0500657 GrSRGBConversion srgbConversion = determine_read_pixels_srgb_conversion(
658 GrPixelConfigIsSRGBEncoded(srcProxy->config()), src->colorSpaceInfo().colorSpace(),
Brian Salomonc320b152018-02-20 14:05:36 -0500659 dstColorType, dstColorSpace, *fContext->caps());
660
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400661 if (!fContext->fGpu->getReadPixelsInfo(srcSurface, srcProxy->origin(), width, height, rowBytes,
Brian Salomonc320b152018-02-20 14:05:36 -0500662 dstColorType, srgbConversion, &drawPreference,
Brian Salomon9b009bb2018-02-14 13:53:55 -0500663 &tempDrawInfo)) {
bsalomon39826022015-07-23 08:07:21 -0700664 return false;
665 }
bsalomon191bcc02014-11-14 11:31:13 -0800666
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400667 if (!(kDontFlush_PixelOpsFlag & flags) && srcSurface->surfacePriv().hasPendingWrite()) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400668 this->flush(nullptr); // MDB TODO: tighten this
669 }
670
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400671 sk_sp<GrSurfaceProxy> proxyToRead = src->asSurfaceProxyRef();
bsalomon39826022015-07-23 08:07:21 -0700672 bool didTempDraw = false;
673 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomonb117ff12016-07-19 07:24:40 -0700674 if (SkBackingFit::kExact == tempDrawInfo.fTempSurfaceFit) {
bsalomon39826022015-07-23 08:07:21 -0700675 // We only respect this when the entire src is being read. Otherwise we can trigger too
676 // many odd ball texture sizes and trash the cache.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400677 if (width != srcSurface->width() || height != srcSurface->height()) {
bsalomonb117ff12016-07-19 07:24:40 -0700678 tempDrawInfo.fTempSurfaceFit= SkBackingFit::kApprox;
bsalomon39826022015-07-23 08:07:21 -0700679 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000680 }
brianosmandfe4f2e2016-07-21 13:28:36 -0700681 // TODO: Need to decide the semantics of this function for color spaces. Do we support
682 // conversion to a passed-in color space? For now, specifying nullptr means that this
Brian Salomon366093f2018-02-13 09:25:22 -0500683 // path will do no conversion, so it will match the behavior of the non-draw path. For
684 // now we simply infer an sRGB color space if the config is sRGB in order to avoid an
685 // illegal combination.
686 sk_sp<SkColorSpace> colorSpace;
687 if (GrPixelConfigIsSRGB(tempDrawInfo.fTempSurfaceDesc.fConfig)) {
688 colorSpace = SkColorSpace::MakeSRGB();
689 }
690 sk_sp<GrRenderTargetContext> tempRTC =
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500691 fContext->contextPriv().makeDeferredRenderTargetContext(
692 tempDrawInfo.fTempSurfaceFit,
Brian Salomon366093f2018-02-13 09:25:22 -0500693 tempDrawInfo.fTempSurfaceDesc.fWidth,
694 tempDrawInfo.fTempSurfaceDesc.fHeight,
695 tempDrawInfo.fTempSurfaceDesc.fConfig,
696 std::move(colorSpace),
697 tempDrawInfo.fTempSurfaceDesc.fSampleCnt,
698 GrMipMapped::kNo,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500699 kTopLeft_GrSurfaceOrigin);
Brian Osman693a5402016-10-27 15:13:22 -0400700 if (tempRTC) {
Greg Danielf44cb482018-02-27 14:26:32 -0500701 // Adding discard to appease vulkan validation warning about loading uninitialized data
702 // on draw
703 tempRTC->discard();
Robert Phillips67c18d62017-01-20 12:44:06 -0500704 SkMatrix textureMatrix = SkMatrix::MakeTrans(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400705 sk_sp<GrTextureProxy> proxy = src->asTextureProxyRef();
Brian Osman2240be92017-10-18 13:15:13 -0400706 auto fp = GrSimpleTextureEffect::Make(std::move(proxy), textureMatrix);
Brian Osman409e74f2017-04-17 11:48:28 -0400707 if (unpremulOnGpu) {
708 fp = fContext->createPMToUPMEffect(std::move(fp), useConfigConversionEffect);
709 // We no longer need to do this on CPU after the read back.
710 unpremul = false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000711 }
Brian Osman409e74f2017-04-17 11:48:28 -0400712 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400713 if (!fp) {
714 return false;
715 }
Brian Osman60cd57e2017-04-06 10:19:06 -0400716
Brian Osman409e74f2017-04-17 11:48:28 -0400717 GrPaint paint;
718 paint.addColorFragmentProcessor(std::move(fp));
719 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
720 paint.setAllowSRGBInputs(true);
721 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
722 tempRTC->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect,
723 nullptr);
724 proxyToRead = tempRTC->asTextureProxyRef();
725 left = 0;
726 top = 0;
727 didTempDraw = true;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000728 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000729 }
joshualitt5c55fef2014-10-31 14:04:35 -0700730
Robert Phillipse78b7252017-04-06 07:59:41 -0400731 if (!proxyToRead) {
732 return false;
733 }
734
bsalomon39826022015-07-23 08:07:21 -0700735 if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000736 return false;
737 }
Brian Salomonc320b152018-02-20 14:05:36 -0500738 GrColorType colorTypeToRead = dstColorType;
bsalomon39826022015-07-23 08:07:21 -0700739 if (didTempDraw) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400740 this->flushSurfaceWrites(proxyToRead.get());
Brian Salomonc320b152018-02-20 14:05:36 -0500741 colorTypeToRead = tempDrawInfo.fReadColorType;
bsalomon39826022015-07-23 08:07:21 -0700742 }
Robert Phillips09dfc472017-09-13 15:25:47 -0400743
Robert Phillips6be756b2018-01-16 15:07:54 -0500744 if (!proxyToRead->instantiate(this->resourceProvider())) {
Robert Phillips09dfc472017-09-13 15:25:47 -0400745 return false;
746 }
747
748 GrSurface* surfaceToRead = proxyToRead->priv().peekSurface();
749
Brian Salomonc320b152018-02-20 14:05:36 -0500750 if (!fContext->fGpu->readPixels(surfaceToRead, proxyToRead->origin(), left, top, width, height,
751 colorTypeToRead, buffer, rowBytes)) {
bsalomon39826022015-07-23 08:07:21 -0700752 return false;
753 }
754
755 // Perform umpremul conversion if we weren't able to perform it as a draw.
756 if (unpremul) {
Brian Salomonc320b152018-02-20 14:05:36 -0500757 SkColorType colorType = GrColorTypeToSkColorType(dstColorType);
758 if (kUnknown_SkColorType == colorType || 4 != SkColorTypeBytesPerPixel(colorType)) {
reed@google.com7111d462014-03-25 16:20:24 +0000759 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000760 }
reed@google.com7111d462014-03-25 16:20:24 +0000761
Matt Sarettc7b29082017-02-09 16:22:39 -0500762 for (int y = 0; y < height; y++) {
763 SkUnpremultiplyRow<false>((uint32_t*) buffer, (const uint32_t*) buffer, width);
764 buffer = SkTAddOffset<void>(buffer, rowBytes);
765 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000766 }
767 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000768}
769
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500770bool GrContextPriv::writeSurfacePixels2(GrSurfaceContext* dst, int left, int top, int width,
771 int height, GrColorType srcColorType,
772 SkColorSpace* srcColorSpace, const void* buffer,
773 size_t rowBytes, uint32_t pixelOpsFlags) {
774 ASSERT_SINGLE_OWNER_PRIV
775 RETURN_FALSE_IF_ABANDONED_PRIV
776 SkASSERT(dst);
777 SkASSERT(buffer);
778 ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy());
779 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels2", fContext);
780
781 if (GrColorType::kUnknown == srcColorType) {
782 return false;
783 }
784
785 if (!dst->asSurfaceProxy()->instantiate(this->resourceProvider())) {
786 return false;
787 }
788
789 GrSurfaceProxy* dstProxy = dst->asSurfaceProxy();
790 GrSurface* dstSurface = dstProxy->priv().peekSurface();
791
792 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
793 GrColorTypeBytesPerPixel(srcColorType), &left, &top,
794 &width, &height, &buffer, &rowBytes)) {
795 return false;
796 }
797
Brian Salomon3d86a192018-02-27 16:46:11 -0500798 if (!fContext->caps()->surfaceSupportsWritePixels(dstSurface)) {
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500799 GrSurfaceDesc desc;
800 desc.fConfig = dstProxy->config();
801 desc.fWidth = width;
802 desc.fHeight = height;
803 desc.fSampleCnt = 1;
Brian Salomon2a4f9832018-03-03 22:43:43 -0500804 auto tempProxy = this->proxyProvider()->createProxy(
805 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kApprox, SkBudgeted::kYes);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500806 if (!tempProxy) {
807 return false;
808 }
809 auto tempCtx = this->drawingManager()->makeTextureContext(
810 tempProxy, dst->colorSpaceInfo().refColorSpace());
811 if (!tempCtx) {
812 return false;
813 }
814 if (!this->writeSurfacePixels2(tempCtx.get(), 0, 0, width, height, srcColorType,
815 srcColorSpace, buffer, rowBytes, pixelOpsFlags)) {
816 return false;
817 }
Brian Salomon3d86a192018-02-27 16:46:11 -0500818 return dst->copy(tempProxy.get(), SkIRect::MakeWH(width, height), {left, top});
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500819 }
820
821 // TODO: Make GrSurfaceContext know its alpha type and pass src buffer's alpha type.
822 bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
823 bool convert = premul;
824
825 if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) {
826 return false;
827 }
828
829 GrColorType allowedColorType =
830 fContext->caps()->supportedWritePixelsColorType(dstProxy->config(), srcColorType);
831 convert = convert || (srcColorType != allowedColorType);
832
833 if (!dst->colorSpaceInfo().colorSpace()) {
834 // "Legacy" mode - no color space conversions.
835 srcColorSpace = nullptr;
836 }
837 convert = convert || !SkColorSpace::Equals(srcColorSpace, dst->colorSpaceInfo().colorSpace());
838
839 std::unique_ptr<char[]> tempBuffer;
840 if (convert) {
841 auto srcSkColorType = GrColorTypeToSkColorType(srcColorType);
842 auto dstSkColorType = GrColorTypeToSkColorType(allowedColorType);
843 if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) {
844 return false;
845 }
846 auto srcAlphaType = premul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
847 SkPixmap src(SkImageInfo::Make(width, height, srcSkColorType, srcAlphaType,
848 sk_ref_sp(srcColorSpace)),
849 buffer, rowBytes);
850 auto tempSrcII = SkImageInfo::Make(width, height, dstSkColorType, kPremul_SkAlphaType,
851 dst->colorSpaceInfo().refColorSpace());
852 auto size = tempSrcII.computeMinByteSize();
853 if (!size) {
854 return false;
855 }
856 tempBuffer.reset(new char[size]);
857 SkPixmap tempSrc(tempSrcII, tempBuffer.get(), tempSrcII.minRowBytes());
858 if (!src.readPixels(tempSrc)) {
859 return false;
860 }
861 srcColorType = allowedColorType;
862 buffer = tempSrc.addr();
863 rowBytes = tempSrc.rowBytes();
864 if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
865 std::unique_ptr<char[]> row(new char[rowBytes]);
866 for (int y = 0; y < height / 2; ++y) {
867 memcpy(row.get(), tempSrc.addr(0, y), rowBytes);
868 memcpy(tempSrc.writable_addr(0, y), tempSrc.addr(0, height - 1 - y), rowBytes);
869 memcpy(tempSrc.writable_addr(0, height - 1 - y), row.get(), rowBytes);
870 }
871 top = dstProxy->height() - top - height;
872 }
873 } else if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
874 size_t trimRowBytes = GrColorTypeBytesPerPixel(srcColorType) * width;
875 tempBuffer.reset(new char[trimRowBytes * height]);
876 char* dst = reinterpret_cast<char*>(tempBuffer.get()) + trimRowBytes * (height - 1);
877 const char* src = reinterpret_cast<const char*>(buffer);
878 for (int i = 0; i < height; ++i, src += rowBytes, dst -= trimRowBytes) {
879 memcpy(dst, src, trimRowBytes);
880 }
881 buffer = tempBuffer.get();
882 rowBytes = trimRowBytes;
883 top = dstProxy->height() - top - height;
884 }
885
886 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) {
887 this->flush(nullptr); // MDB TODO: tighten this
888 }
889
890 return this->getGpu()->writePixels(dstSurface, left, top, width, height, srcColorType, buffer,
891 rowBytes);
892}
893
Robert Phillips7ee385e2017-03-30 08:02:11 -0400894void GrContextPriv::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) {
895 ASSERT_SINGLE_OWNER_PRIV
896 RETURN_IF_ABANDONED_PRIV
897 SkASSERT(proxy);
898 ASSERT_OWNED_PROXY_PRIV(proxy);
Greg Daniel51316782017-08-02 15:10:09 +0000899 fContext->fDrawingManager->prepareSurfaceForExternalIO(proxy, 0, nullptr);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000900}
901
Robert Phillips7ee385e2017-03-30 08:02:11 -0400902void GrContextPriv::flushSurfaceWrites(GrSurfaceProxy* proxy) {
903 ASSERT_SINGLE_OWNER_PRIV
904 RETURN_IF_ABANDONED_PRIV
905 SkASSERT(proxy);
906 ASSERT_OWNED_PROXY_PRIV(proxy);
907 if (proxy->priv().hasPendingWrite()) {
908 this->flush(proxy);
bsalomonf80bfed2014-10-07 05:56:02 -0700909 }
910}
911
Robert Phillips7ee385e2017-03-30 08:02:11 -0400912void GrContextPriv::flushSurfaceIO(GrSurfaceProxy* proxy) {
913 ASSERT_SINGLE_OWNER_PRIV
914 RETURN_IF_ABANDONED_PRIV
915 SkASSERT(proxy);
916 ASSERT_OWNED_PROXY_PRIV(proxy);
917 if (proxy->priv().hasPendingIO()) {
918 this->flush(proxy);
ajuma95243eb2016-08-24 08:19:02 -0700919 }
920}
921
bsalomon@google.com27847de2011-02-22 20:59:41 +0000922////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000923
Robert Phillips2c862492017-01-18 10:08:39 -0500924sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy,
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500925 sk_sp<SkColorSpace> colorSpace,
926 const SkSurfaceProps* props) {
Brian Osman45580d32016-11-23 09:37:01 -0500927 ASSERT_SINGLE_OWNER_PRIV
928
Brian Salomon366093f2018-02-13 09:25:22 -0500929 // sRGB pixel configs may only be used with near-sRGB gamma color spaces.
930 if (GrPixelConfigIsSRGB(proxy->config())) {
931 if (!colorSpace || !colorSpace->gammaCloseToSRGB()) {
932 return nullptr;
933 }
934 }
Brian Osman45580d32016-11-23 09:37:01 -0500935 if (proxy->asRenderTargetProxy()) {
Robert Phillips2c862492017-01-18 10:08:39 -0500936 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500937 std::move(colorSpace), props);
Brian Osman45580d32016-11-23 09:37:01 -0500938 } else {
939 SkASSERT(proxy->asTextureProxy());
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500940 SkASSERT(!props);
Robert Phillips2c862492017-01-18 10:08:39 -0500941 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Brian Osman45580d32016-11-23 09:37:01 -0500942 }
943}
944
Robert Phillipse2f7d182016-12-15 09:23:05 -0500945sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrSurfaceDesc& dstDesc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500946 GrSurfaceOrigin origin,
Greg Daniel65c7f662017-10-30 13:39:09 -0400947 GrMipMapped mipMapped,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500948 SkBackingFit fit,
Brian Salomon366093f2018-02-13 09:25:22 -0500949 SkBudgeted isDstBudgeted,
950 sk_sp<SkColorSpace> colorSpace,
951 const SkSurfaceProps* props) {
Greg Daniel65c7f662017-10-30 13:39:09 -0400952 sk_sp<GrTextureProxy> proxy;
953 if (GrMipMapped::kNo == mipMapped) {
Brian Salomon2a4f9832018-03-03 22:43:43 -0500954 proxy = this->proxyProvider()->createProxy(dstDesc, origin, fit, isDstBudgeted);
Greg Daniel65c7f662017-10-30 13:39:09 -0400955 } else {
956 SkASSERT(SkBackingFit::kExact == fit);
Brian Salomon2a4f9832018-03-03 22:43:43 -0500957 proxy = this->proxyProvider()->createMipMapProxy(dstDesc, origin, isDstBudgeted);
Greg Daniel65c7f662017-10-30 13:39:09 -0400958 }
Robert Phillips77b3f322017-01-31 18:24:12 -0500959 if (!proxy) {
960 return nullptr;
961 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500962
Brian Salomon366093f2018-02-13 09:25:22 -0500963 return this->makeWrappedSurfaceContext(std::move(proxy), std::move(colorSpace), props);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500964}
965
Brian Salomond17f6582017-07-19 18:28:58 -0400966sk_sp<GrTextureContext> GrContextPriv::makeBackendTextureContext(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000967 GrSurfaceOrigin origin,
Brian Osmanc1e37052017-03-09 14:19:20 -0500968 sk_sp<SkColorSpace> colorSpace) {
Robert Phillips26caf892017-01-27 10:58:31 -0500969 ASSERT_SINGLE_OWNER_PRIV
970
Brian Salomon7578f3e2018-03-07 14:39:54 -0500971 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->wrapBackendTexture(tex, origin);
Robert Phillips77b3f322017-01-31 18:24:12 -0500972 if (!proxy) {
973 return nullptr;
974 }
Robert Phillips26caf892017-01-27 10:58:31 -0500975
Brian Salomond17f6582017-07-19 18:28:58 -0400976 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Robert Phillips26caf892017-01-27 10:58:31 -0500977}
978
Brian Osman11052242016-10-27 14:47:55 -0400979sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000980 const GrBackendTexture& tex,
981 GrSurfaceOrigin origin,
982 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -0400983 sk_sp<SkColorSpace> colorSpace,
Brian Osmanc1e37052017-03-09 14:19:20 -0500984 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700985 ASSERT_SINGLE_OWNER_PRIV
Brian Salomonbdecacf2018-02-02 20:32:49 -0500986 SkASSERT(sampleCnt > 0);
robertphillips4fd74ae2016-08-03 14:26:53 -0700987
Brian Salomon7578f3e2018-03-07 14:39:54 -0500988 sk_sp<GrTextureProxy> proxy(
989 this->proxyProvider()->wrapRenderableBackendTexture(tex, origin, sampleCnt));
Robert Phillips77b3f322017-01-31 18:24:12 -0500990 if (!proxy) {
991 return nullptr;
992 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400993
Robert Phillips37430132016-11-09 06:50:43 -0500994 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Brian Osman11052242016-10-27 14:47:55 -0400995 std::move(colorSpace), props);
robertphillips4fd74ae2016-08-03 14:26:53 -0700996}
997
Brian Osman11052242016-10-27 14:47:55 -0400998sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +0000999 const GrBackendRenderTarget& backendRT,
1000 GrSurfaceOrigin origin,
robertphillips4fd74ae2016-08-03 14:26:53 -07001001 sk_sp<SkColorSpace> colorSpace,
1002 const SkSurfaceProps* surfaceProps) {
1003 ASSERT_SINGLE_OWNER_PRIV
1004
Brian Salomon7578f3e2018-03-07 14:39:54 -05001005 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->wrapBackendRenderTarget(backendRT, origin);
Robert Phillips77b3f322017-01-31 18:24:12 -05001006 if (!proxy) {
1007 return nullptr;
1008 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001009
Robert Phillips37430132016-11-09 06:50:43 -05001010 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001011 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -04001012 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -07001013}
1014
Brian Osman11052242016-10-27 14:47:55 -04001015sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +00001016 const GrBackendTexture& tex,
1017 GrSurfaceOrigin origin,
1018 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -07001019 sk_sp<SkColorSpace> colorSpace,
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001020 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -07001021 ASSERT_SINGLE_OWNER_PRIV
Brian Salomonbdecacf2018-02-02 20:32:49 -05001022 SkASSERT(sampleCnt > 0);
Brian Salomon7578f3e2018-03-07 14:39:54 -05001023 sk_sp<GrSurfaceProxy> proxy(
1024 this->proxyProvider()->wrapBackendTextureAsRenderTarget(tex, origin, sampleCnt));
Robert Phillips77b3f322017-01-31 18:24:12 -05001025 if (!proxy) {
1026 return nullptr;
1027 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001028
Robert Phillips37430132016-11-09 06:50:43 -05001029 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001030 std::move(colorSpace),
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001031 props);
robertphillips77a2e522015-10-17 07:43:27 -07001032}
1033
Chris Daltonfe199b72017-05-05 11:26:15 -04001034void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
1035 fContext->fDrawingManager->addOnFlushCallbackObject(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -04001036}
1037
Robert Phillips62000362018-02-01 09:10:04 -05001038void GrContextPriv::moveOpListsToDDL(SkDeferredDisplayList* ddl) {
1039 fContext->fDrawingManager->moveOpListsToDDL(ddl);
1040}
1041
1042void GrContextPriv::copyOpListsFromDDL(const SkDeferredDisplayList* ddl,
1043 GrRenderTargetProxy* newDest) {
1044 fContext->fDrawingManager->copyOpListsFromDDL(ddl, newDest);
1045}
1046
robertphillips48fde9c2016-09-06 05:20:20 -07001047static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
Brian Osman78f20e02017-01-12 10:28:01 -05001048 switch (config) {
1049 case kAlpha_8_GrPixelConfig:
1050 case kRGB_565_GrPixelConfig:
1051 case kRGBA_4444_GrPixelConfig:
1052 case kBGRA_8888_GrPixelConfig:
Brian Osman10fc6fd2018-03-02 11:01:10 -05001053 case kRGBA_1010102_GrPixelConfig:
Brian Osman78f20e02017-01-12 10:28:01 -05001054 return kRGBA_8888_GrPixelConfig;
1055 case kSBGRA_8888_GrPixelConfig:
1056 return kSRGBA_8888_GrPixelConfig;
1057 case kAlpha_half_GrPixelConfig:
1058 return kRGBA_half_GrPixelConfig;
1059 default:
1060 return kUnknown_GrPixelConfig;
1061 }
robertphillips48fde9c2016-09-06 05:20:20 -07001062}
1063
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001064sk_sp<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContextWithFallback(
robertphillipsd728f0c2016-11-21 11:05:03 -08001065 SkBackingFit fit,
1066 int width, int height,
1067 GrPixelConfig config,
1068 sk_sp<SkColorSpace> colorSpace,
1069 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -04001070 GrMipMapped mipMapped,
robertphillipsd728f0c2016-11-21 11:05:03 -08001071 GrSurfaceOrigin origin,
1072 const SkSurfaceProps* surfaceProps,
1073 SkBudgeted budgeted) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05001074 SkASSERT(sampleCnt > 0);
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001075 if (0 == fContext->caps()->getRenderTargetSampleCount(sampleCnt, config)) {
robertphillipsd728f0c2016-11-21 11:05:03 -08001076 config = GrPixelConfigFallback(config);
1077 }
1078
1079 return this->makeDeferredRenderTargetContext(fit, width, height, config, std::move(colorSpace),
Greg Daniel45d63032017-10-30 13:41:26 -04001080 sampleCnt, mipMapped, origin, surfaceProps,
Greg Daniele1da1d92017-10-06 15:59:27 -04001081 budgeted);
robertphillipsd728f0c2016-11-21 11:05:03 -08001082}
1083
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001084sk_sp<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContext(
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001085 SkBackingFit fit,
1086 int width, int height,
1087 GrPixelConfig config,
1088 sk_sp<SkColorSpace> colorSpace,
1089 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -04001090 GrMipMapped mipMapped,
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001091 GrSurfaceOrigin origin,
1092 const SkSurfaceProps* surfaceProps,
1093 SkBudgeted budgeted) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05001094 SkASSERT(sampleCnt > 0);
Brian Salomon79e4d1b2017-07-13 11:17:11 -04001095 if (this->abandoned()) {
1096 return nullptr;
1097 }
1098
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001099 GrSurfaceDesc desc;
1100 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001101 desc.fWidth = width;
1102 desc.fHeight = height;
1103 desc.fConfig = config;
1104 desc.fSampleCnt = sampleCnt;
1105
Greg Daniele1da1d92017-10-06 15:59:27 -04001106 sk_sp<GrTextureProxy> rtp;
Greg Daniel45d63032017-10-30 13:41:26 -04001107 if (GrMipMapped::kNo == mipMapped) {
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001108 rtp = fContext->fProxyProvider->createProxy(desc, origin, fit, budgeted);
Greg Daniele1da1d92017-10-06 15:59:27 -04001109 } else {
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001110 rtp = fContext->fProxyProvider->createMipMapProxy(desc, origin, budgeted);
Greg Daniele1da1d92017-10-06 15:59:27 -04001111 }
Robert Phillips08c5ec72017-01-30 12:26:47 -05001112 if (!rtp) {
1113 return nullptr;
1114 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001115
Robert Phillips1119dc32017-04-11 12:54:57 -04001116 sk_sp<GrRenderTargetContext> renderTargetContext(
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001117 fContext->fDrawingManager->makeRenderTargetContext(std::move(rtp),
1118 std::move(colorSpace),
1119 surfaceProps));
Robert Phillips1119dc32017-04-11 12:54:57 -04001120 if (!renderTargetContext) {
1121 return nullptr;
1122 }
1123
1124 renderTargetContext->discard();
1125
1126 return renderTargetContext;
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001127}
1128
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001129bool GrContextPriv::abandoned() const {
1130 ASSERT_SINGLE_OWNER_PRIV
1131 return fContext->fDrawingManager->wasAbandoned();
robertphillips77a2e522015-10-17 07:43:27 -07001132}
1133
Brian Salomonaff329b2017-08-11 09:40:37 -04001134std::unique_ptr<GrFragmentProcessor> GrContext::createPMToUPMEffect(
1135 std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) {
Robert Phillips757914d2017-01-25 15:48:30 -05001136 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -04001137 // We have specialized effects that guarantee round-trip conversion for some formats
1138 if (useConfigConversionEffect) {
1139 // We should have already called this->validPMUPMConversionExists() in this case
1140 SkASSERT(fDidTestPMConversions);
1141 // ...and it should have succeeded
1142 SkASSERT(this->validPMUPMConversionExists());
1143
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001144 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
Brian Osman2d2da4f2017-04-12 17:07:22 -04001145 } else {
1146 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
1147 // explicitly round the results. Just do the obvious, naive thing in the shader.
1148 return GrFragmentProcessor::UnpremulOutput(std::move(fp));
Robert Phillips757914d2017-01-25 15:48:30 -05001149 }
1150}
1151
Brian Salomonaff329b2017-08-11 09:40:37 -04001152std::unique_ptr<GrFragmentProcessor> GrContext::createUPMToPMEffect(
1153 std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) {
joshualitt1de610a2016-01-06 08:26:09 -08001154 ASSERT_SINGLE_OWNER
Brian Osman2d2da4f2017-04-12 17:07:22 -04001155 // We have specialized effects that guarantee round-trip conversion for these formats
Brian Osman409e74f2017-04-17 11:48:28 -04001156 if (useConfigConversionEffect) {
1157 // We should have already called this->validPMUPMConversionExists() in this case
1158 SkASSERT(fDidTestPMConversions);
1159 // ...and it should have succeeded
1160 SkASSERT(this->validPMUPMConversionExists());
1161
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001162 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
Brian Osman2d2da4f2017-04-12 17:07:22 -04001163 } else {
1164 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
1165 // explicitly round the results. Just do the obvious, naive thing in the shader.
1166 return GrFragmentProcessor::PremulOutput(std::move(fp));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001167 }
1168}
1169
Brian Osman409e74f2017-04-17 11:48:28 -04001170bool GrContext::validPMUPMConversionExists() {
joshualitt1de610a2016-01-06 08:26:09 -08001171 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -04001172 if (!fDidTestPMConversions) {
Brian Osman28804f32017-04-20 10:24:36 -04001173 fPMUPMConversionsRoundTrip = GrConfigConversionEffect::TestForPreservingPMConversions(this);
Brian Osman409e74f2017-04-17 11:48:28 -04001174 fDidTestPMConversions = true;
1175 }
1176
bsalomon636e8022015-07-29 06:08:46 -07001177 // The PM<->UPM tests fail or succeed together so we only need to check one.
Brian Osman28804f32017-04-20 10:24:36 -04001178 return fPMUPMConversionsRoundTrip;
bsalomon636e8022015-07-29 06:08:46 -07001179}
1180
bsalomon37f9a262015-02-02 13:00:10 -08001181//////////////////////////////////////////////////////////////////////////////
1182
Robert Phillipsfc711a22018-02-13 17:03:00 -05001183// DDL TODO: remove 'maxResources'
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001184void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -08001185 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001186 if (maxResources) {
1187 *maxResources = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -08001188 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001189 if (maxResourceBytes) {
1190 *maxResourceBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -08001191 }
1192}
1193
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001194void GrContext::setResourceCacheLimits(int maxResources, size_t maxResourceBytes) {
joshualitt1de610a2016-01-06 08:26:09 -08001195 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001196 fResourceCache->setLimits(maxResources, maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -08001197}
1198
ericrk0a5fa482015-09-15 14:16:10 -07001199//////////////////////////////////////////////////////////////////////////////
ericrk0a5fa482015-09-15 14:16:10 -07001200void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -08001201 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -07001202 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
1203}
Brian Osman71a18892017-08-10 10:23:25 -04001204
1205//////////////////////////////////////////////////////////////////////////////
1206
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001207SkString GrContextPriv::dump() const {
Brian Osman71a18892017-08-10 10:23:25 -04001208 SkDynamicMemoryWStream stream;
1209 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
1210 writer.beginObject();
1211
1212 static const char* kBackendStr[] = {
1213 "Metal",
1214 "OpenGL",
1215 "Vulkan",
1216 "Mock",
1217 };
1218 GR_STATIC_ASSERT(0 == kMetal_GrBackend);
1219 GR_STATIC_ASSERT(1 == kOpenGL_GrBackend);
1220 GR_STATIC_ASSERT(2 == kVulkan_GrBackend);
1221 GR_STATIC_ASSERT(3 == kMock_GrBackend);
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001222 writer.appendString("backend", kBackendStr[fContext->fBackend]);
Brian Osman71a18892017-08-10 10:23:25 -04001223
1224 writer.appendName("caps");
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001225 fContext->fCaps->dumpJSON(&writer);
Brian Osman71a18892017-08-10 10:23:25 -04001226
1227 writer.appendName("gpu");
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001228 fContext->fGpu->dumpJSON(&writer);
Brian Osman71a18892017-08-10 10:23:25 -04001229
1230 // Flush JSON to the memory stream
1231 writer.endObject();
1232 writer.flush();
1233
1234 // Null terminate the JSON data in the memory stream
1235 stream.write8(0);
1236
1237 // Allocate a string big enough to hold all the data, then copy out of the stream
1238 SkString result(stream.bytesWritten());
1239 stream.copyToAndReset(result.writable_str());
1240 return result;
1241}