bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * 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.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
bsalomon@google.com | 1fadb20 | 2011-12-12 16:10:08 +0000 | [diff] [blame] | 8 | #include "GrContext.h" |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 9 | #include "GrBackendSemaphore.h" |
Brian Salomon | c65aec9 | 2017-03-09 09:03:58 -0500 | [diff] [blame] | 10 | #include "GrClip.h" |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 11 | #include "GrContextOptions.h" |
Brian Salomon | c65aec9 | 2017-03-09 09:03:58 -0500 | [diff] [blame] | 12 | #include "GrContextPriv.h" |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 13 | #include "GrDrawingManager.h" |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 14 | #include "GrGpu.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 15 | #include "GrProxyProvider.h" |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 16 | #include "GrRenderTargetContext.h" |
Brian Salomon | c65aec9 | 2017-03-09 09:03:58 -0500 | [diff] [blame] | 17 | #include "GrRenderTargetProxy.h" |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 18 | #include "GrResourceCache.h" |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 19 | #include "GrResourceProvider.h" |
Greg Daniel | d85f97d | 2017-03-07 13:37:21 -0500 | [diff] [blame] | 20 | #include "GrSemaphore.h" |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 21 | #include "GrSoftwarePathRenderer.h" |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 22 | #include "GrSurfaceContext.h" |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 23 | #include "GrSurfacePriv.h" |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 24 | #include "GrSurfaceProxyPriv.h" |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 25 | #include "GrTexture.h" |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 26 | #include "GrTextureContext.h" |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 27 | #include "GrTracing.h" |
Matt Sarett | 485c499 | 2017-02-14 14:18:27 -0500 | [diff] [blame] | 28 | #include "SkConvertPixels.h" |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 29 | #include "SkDeferredDisplayList.h" |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 30 | #include "SkGr.h" |
Mike Reed | 7fcfb62 | 2018-02-09 13:26:46 -0500 | [diff] [blame] | 31 | #include "SkImageInfoPriv.h" |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 32 | #include "SkJSONWriter.h" |
Brian Osman | 5127998 | 2017-08-23 10:12:00 -0400 | [diff] [blame] | 33 | #include "SkMakeUnique.h" |
| 34 | #include "SkTaskGroup.h" |
Matt Sarett | c7b2908 | 2017-02-09 16:22:39 -0500 | [diff] [blame] | 35 | #include "SkUnPreMultiplyPriv.h" |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 36 | #include "effects/GrConfigConversionEffect.h" |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 37 | #include "text/GrTextBlobCache.h" |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 38 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 39 | #define ASSERT_OWNED_PROXY(P) \ |
| 40 | SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == this) |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 41 | #define ASSERT_OWNED_PROXY_PRIV(P) \ |
| 42 | SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == fContext) |
| 43 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 44 | #define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this) |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 45 | #define ASSERT_SINGLE_OWNER \ |
| 46 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fSingleOwner);) |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 47 | #define ASSERT_SINGLE_OWNER_PRIV \ |
| 48 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fContext->fSingleOwner);) |
robertphillips | 7761d61 | 2016-05-16 09:14:53 -0700 | [diff] [blame] | 49 | #define RETURN_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return; } |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 50 | #define RETURN_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return; } |
robertphillips | 7761d61 | 2016-05-16 09:14:53 -0700 | [diff] [blame] | 51 | #define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return false; } |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 52 | #define RETURN_FALSE_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return false; } |
robertphillips | 7761d61 | 2016-05-16 09:14:53 -0700 | [diff] [blame] | 53 | #define RETURN_NULL_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return nullptr; } |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 54 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 55 | //////////////////////////////////////////////////////////////////////////////// |
| 56 | |
joshualitt | 0acd0d3 | 2015-05-07 08:23:19 -0700 | [diff] [blame] | 57 | static int32_t gNextID = 1; |
| 58 | static 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 Phillips | fde6fa0 | 2018-03-02 08:53:14 -0500 | [diff] [blame] | 66 | GrContext::GrContext(GrBackend backend, int32_t id) |
| 67 | : fBackend(backend) |
| 68 | , fUniqueID(SK_InvalidGenID == id ? next_id() : id) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 69 | fResourceCache = nullptr; |
| 70 | fResourceProvider = nullptr; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 71 | fProxyProvider = nullptr; |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 72 | fGlyphCache = nullptr; |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Robert Phillips | fde6fa0 | 2018-03-02 08:53:14 -0500 | [diff] [blame] | 75 | bool GrContext::initCommon(const GrContextOptions& options) { |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 76 | ASSERT_SINGLE_OWNER |
Robert Phillips | fde6fa0 | 2018-03-02 08:53:14 -0500 | [diff] [blame] | 77 | SkASSERT(fCaps); // needs to have been initialized by derived classes |
| 78 | SkASSERT(fThreadSafeProxy); // needs to have been initialized by derived classes |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 79 | |
| 80 | if (fGpu) { |
| 81 | fCaps = fGpu->refCaps(); |
| 82 | fResourceCache = new GrResourceCache(fCaps.get(), fUniqueID); |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 83 | fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, &fSingleOwner, |
| 84 | options.fExplicitlyAllocateGPUResources); |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 85 | } |
| 86 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 87 | fProxyProvider = new GrProxyProvider(fResourceProvider, fResourceCache, fCaps, &fSingleOwner); |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 88 | |
| 89 | if (fResourceCache) { |
| 90 | fResourceCache->setProxyProvider(fProxyProvider); |
| 91 | } |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 92 | |
Brian Osman | 46da1cc | 2017-02-14 14:15:48 -0500 | [diff] [blame] | 93 | fDisableGpuYUVConversion = options.fDisableGpuYUVConversion; |
Brian Osman | 8a83ca4 | 2018-02-12 14:32:17 -0500 | [diff] [blame] | 94 | fSharpenMipmappedTextures = options.fSharpenMipmappedTextures; |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 95 | fDidTestPMConversions = false; |
| 96 | |
bsalomon | 6b2552f | 2016-09-15 13:50:26 -0700 | [diff] [blame] | 97 | GrPathRendererChain::Options prcOptions; |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 98 | prcOptions.fAllowPathMaskCaching = options.fAllowPathMaskCaching; |
Brian Osman | 195c05b | 2017-08-30 15:14:04 -0400 | [diff] [blame] | 99 | #if GR_TEST_UTILS |
csmartdalton | 008b9d8 | 2017-02-22 12:00:42 -0700 | [diff] [blame] | 100 | prcOptions.fGpuPathRenderers = options.fGpuPathRenderers; |
Brian Osman | 195c05b | 2017-08-30 15:14:04 -0400 | [diff] [blame] | 101 | #endif |
Brian Osman | b350ae2 | 2017-08-29 16:15:39 -0400 | [diff] [blame] | 102 | if (options.fDisableDistanceFieldPaths) { |
Brian Osman | 195c05b | 2017-08-30 15:14:04 -0400 | [diff] [blame] | 103 | prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall; |
Brian Osman | b350ae2 | 2017-08-29 16:15:39 -0400 | [diff] [blame] | 104 | } |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 105 | |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 106 | 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 Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 113 | GrAtlasTextContext::Options atlasTextContextOptions; |
| 114 | atlasTextContextOptions.fMaxDistanceFieldFontSize = options.fGlyphsAsPathsFontSize; |
| 115 | atlasTextContextOptions.fMinDistanceFieldFontSize = options.fMinDistanceFieldFontSize; |
Brian Salomon | b508696 | 2017-12-13 10:59:33 -0500 | [diff] [blame] | 116 | atlasTextContextOptions.fDistanceFieldVerticesAlwaysHaveW = false; |
| 117 | #if SK_SUPPORT_ATLAS_TEXT |
| 118 | if (GrContextOptions::Enable::kYes == options.fDistanceFieldGlyphVerticesAlwaysHaveW) { |
| 119 | atlasTextContextOptions.fDistanceFieldVerticesAlwaysHaveW = true; |
| 120 | } |
| 121 | #endif |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 122 | |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 123 | fDrawingManager.reset(new GrDrawingManager(this, prcOptions, atlasTextContextOptions, |
| 124 | &fSingleOwner, options.fSortRenderTargets)); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 125 | |
Robert Phillips | af4adef | 2018-03-07 11:59:37 -0500 | [diff] [blame] | 126 | fGlyphCache = new GrGlyphCache(fCaps.get(), options.fGlyphCacheTextureMaximumBytes); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 127 | |
Robert Phillips | 303cd58 | 2018-02-14 18:54:01 -0500 | [diff] [blame] | 128 | fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB, |
| 129 | this, this->uniqueID(), SkToBool(fGpu))); |
Brian Salomon | 91a3e52 | 2017-06-23 10:58:19 -0400 | [diff] [blame] | 130 | |
Robert Phillips | fde6fa0 | 2018-03-02 08:53:14 -0500 | [diff] [blame] | 131 | // 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 Osman | 5127998 | 2017-08-23 10:12:00 -0400 | [diff] [blame] | 133 | if (options.fExecutor) { |
| 134 | fTaskGroup = skstd::make_unique<SkTaskGroup>(*options.fExecutor); |
| 135 | } |
| 136 | |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 137 | fPersistentCache = options.fPersistentCache; |
| 138 | |
Brian Salomon | 91a3e52 | 2017-06-23 10:58:19 -0400 | [diff] [blame] | 139 | return true; |
bsalomon@google.com | c0af317 | 2012-06-15 14:10:09 +0000 | [diff] [blame] | 140 | } |
| 141 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 142 | GrContext::~GrContext() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 143 | ASSERT_SINGLE_OWNER |
| 144 | |
Robert Phillips | 2e6feed | 2018-01-22 15:27:20 -0500 | [diff] [blame] | 145 | if (fDrawingManager) { |
| 146 | fDrawingManager->cleanup(); |
| 147 | } |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 148 | |
robertphillips@google.com | 950b1b0 | 2013-10-21 17:37:28 +0000 | [diff] [blame] | 149 | for (int i = 0; i < fCleanUpData.count(); ++i) { |
| 150 | (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo); |
| 151 | } |
| 152 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 153 | delete fResourceProvider; |
| 154 | delete fResourceCache; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 155 | delete fProxyProvider; |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 156 | delete fGlyphCache; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 157 | } |
| 158 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 159 | sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() { |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 160 | return fThreadSafeProxy; |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Robert Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 163 | SkSurfaceCharacterization 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 | |
bsalomon | 2354f84 | 2014-07-28 13:48:36 -0700 | [diff] [blame] | 199 | void GrContext::abandonContext() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 200 | ASSERT_SINGLE_OWNER |
| 201 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 202 | fProxyProvider->abandon(); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 203 | fResourceProvider->abandon(); |
robertphillips | 0dfa62c | 2015-11-16 06:23:31 -0800 | [diff] [blame] | 204 | |
| 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.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 209 | // abandon first to so destructors |
| 210 | // don't try to free the resources in the API. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 211 | fResourceCache->abandonAll(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 212 | |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 213 | fGpu->disconnect(GrGpu::DisconnectType::kAbandon); |
| 214 | |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 215 | fGlyphCache->freeAll(); |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 216 | fTextBlobCache->freeAll(); |
| 217 | } |
| 218 | |
| 219 | void GrContext::releaseResourcesAndAbandonContext() { |
| 220 | ASSERT_SINGLE_OWNER |
| 221 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 222 | fProxyProvider->abandon(); |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 223 | 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.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 233 | |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 234 | fGlyphCache->freeAll(); |
joshualitt | 26ffc00 | 2015-04-16 11:24:04 -0700 | [diff] [blame] | 235 | fTextBlobCache->freeAll(); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 236 | } |
| 237 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 238 | void GrContext::resetContext(uint32_t state) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 239 | ASSERT_SINGLE_OWNER |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 240 | fGpu->markContextDirty(state); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | void GrContext::freeGpuResources() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 244 | ASSERT_SINGLE_OWNER |
| 245 | |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 246 | fGlyphCache->freeAll(); |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 247 | |
| 248 | fDrawingManager->freeGpuResources(); |
bsalomon | 3033b9f | 2015-04-13 11:09:56 -0700 | [diff] [blame] | 249 | |
| 250 | fResourceCache->purgeAllUnlocked(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 253 | void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) { |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 254 | ASSERT_SINGLE_OWNER |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 255 | fResourceCache->purgeAsNeeded(); |
| 256 | fResourceCache->purgeResourcesNotUsedSince(GrStdSteadyClock::now() - msNotUsed); |
| 257 | |
| 258 | fTextBlobCache->purgeStaleBlobs(); |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 259 | } |
| 260 | |
Derek Sollenberger | 5480a18 | 2017-05-25 16:43:59 -0400 | [diff] [blame] | 261 | void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) { |
| 262 | ASSERT_SINGLE_OWNER |
| 263 | fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources); |
| 264 | } |
| 265 | |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 266 | void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 267 | ASSERT_SINGLE_OWNER |
| 268 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 269 | if (resourceCount) { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 270 | *resourceCount = fResourceCache->getBudgetedResourceCount(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 271 | } |
| 272 | if (resourceBytes) { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 273 | *resourceBytes = fResourceCache->getBudgetedResourceBytes(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 274 | } |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 277 | size_t GrContext::getResourceCachePurgeableBytes() const { |
| 278 | ASSERT_SINGLE_OWNER |
| 279 | return fResourceCache->getPurgeableBytes(); |
| 280 | } |
| 281 | |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 282 | //////////////////////////////////////////////////////////////////////////////// |
| 283 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 284 | bool GrContext::colorTypeSupportedAsImage(SkColorType colorType) const { |
| 285 | GrPixelConfig config = SkImageInfo2GrPixelConfig(colorType, nullptr, *this->caps()); |
| 286 | return this->caps()->isConfigTexturable(config); |
| 287 | } |
| 288 | |
| 289 | int GrContext::maxSurfaceSampleCountForColorType(SkColorType colorType) const { |
| 290 | GrPixelConfig config = SkImageInfo2GrPixelConfig(colorType, nullptr, *this->caps()); |
| 291 | return this->caps()->maxRenderTargetSampleCount(config); |
| 292 | } |
| 293 | |
| 294 | //////////////////////////////////////////////////////////////////////////////// |
| 295 | |
joshualitt | 0db6dfa | 2015-04-10 07:01:30 -0700 | [diff] [blame] | 296 | void GrContext::TextBlobCacheOverBudgetCB(void* data) { |
| 297 | SkASSERT(data); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 298 | // 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. |
joshualitt | 0db6dfa | 2015-04-10 07:01:30 -0700 | [diff] [blame] | 302 | GrContext* context = reinterpret_cast<GrContext*>(data); |
| 303 | context->flush(); |
| 304 | } |
| 305 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 306 | //////////////////////////////////////////////////////////////////////////////// |
| 307 | |
bsalomon | b77a907 | 2016-09-07 10:02:04 -0700 | [diff] [blame] | 308 | void GrContext::flush() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 309 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 310 | RETURN_IF_ABANDONED |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 311 | |
| 312 | fDrawingManager->flush(nullptr); |
| 313 | } |
| 314 | |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 315 | GrSemaphoresSubmitted 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 Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 323 | void 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.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 331 | bool sw_convert_to_premul(GrColorType srcColorType, int width, int height, size_t inRowBytes, |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 332 | const void* inPixels, size_t outRowBytes, void* outPixels) { |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 333 | SkColorType colorType = GrColorTypeToSkColorType(srcColorType); |
| 334 | if (kUnknown_SkColorType == colorType || 4 != SkColorTypeBytesPerPixel(colorType)) { |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 335 | return false; |
| 336 | } |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 337 | |
Matt Sarett | c7b2908 | 2017-02-09 16:22:39 -0500 | [diff] [blame] | 338 | 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 | } |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 343 | |
Matt Sarett | c7b2908 | 2017-02-09 16:22:39 -0500 | [diff] [blame] | 344 | return true; |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 347 | // TODO: This will be removed when GrSurfaceContexts are aware of their color types. |
| 348 | // (skbug.com/6718) |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 349 | static bool valid_premul_config(GrPixelConfig config) { |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 350 | 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 Osman | 10fc6fd | 2018-03-02 11:01:10 -0500 | [diff] [blame] | 360 | case kRGBA_1010102_GrPixelConfig: return true; |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 361 | 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 Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 373 | } |
| 374 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 375 | static bool valid_premul_color_type(GrColorType ct) { |
| 376 | switch (ct) { |
Brian Osman | 10fc6fd | 2018-03-02 11:01:10 -0500 | [diff] [blame] | 377 | 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 Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 389 | } |
| 390 | SK_ABORT("Invalid GrColorType"); |
| 391 | return false; |
| 392 | } |
| 393 | |
| 394 | static bool valid_pixel_conversion(GrColorType cpuColorType, GrPixelConfig gpuConfig, |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 395 | bool premulConversion) { |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 396 | // We only allow premul <-> unpremul conversions for some formats |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 397 | if (premulConversion && |
| 398 | (!valid_premul_color_type(cpuColorType) || !valid_premul_config(gpuConfig))) { |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 399 | return false; |
| 400 | } |
| 401 | |
| 402 | return true; |
| 403 | } |
| 404 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 405 | static bool pm_upm_must_round_trip(GrColorType cpuColorType, const SkColorSpace* cpuColorSpace) { |
| 406 | return !cpuColorSpace && |
| 407 | (GrColorType::kRGBA_8888 == cpuColorType || GrColorType::kBGRA_8888 == cpuColorType); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 408 | } |
| 409 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 410 | // TODO: This will be removed when GrSurfaceContexts are aware of their color types. |
| 411 | // (skbug.com/6718) |
| 412 | static 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 | |
| 418 | static GrSRGBConversion determine_write_pixels_srgb_conversion(GrColorType srcColorType, |
| 419 | const SkColorSpace* srcColorSpace, |
| 420 | GrSRGBEncoded dstSRGBEncoded, |
| 421 | const SkColorSpace* dstColorSpace, |
| 422 | const GrCaps& caps) { |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 423 | // No support for sRGB-encoded alpha. |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 424 | if (GrColorTypeIsAlphaOnly(srcColorType)) { |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 425 | return GrSRGBConversion::kNone; |
| 426 | } |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 427 | // No conversions without GPU support for sRGB. (Legacy mode) |
| 428 | if (!caps.srgbSupport()) { |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 429 | return GrSRGBConversion::kNone; |
| 430 | } |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 431 | // If the GrSurfaceContext has no color space then it is in legacy mode. |
| 432 | if (!dstColorSpace) { |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 433 | 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 Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 441 | // mode being forced on by caps above. This may change in the future. We will then have to |
| 442 | // perform shader based conversions. |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 443 | SkASSERT(dstColorSpaceIsSRGB == (GrSRGBEncoded::kYes == dstSRGBEncoded)); |
| 444 | |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 445 | if (srcColorSpaceIsSRGB == dstColorSpaceIsSRGB) { |
| 446 | return GrSRGBConversion::kNone; |
| 447 | } |
| 448 | return srcColorSpaceIsSRGB ? GrSRGBConversion::kSRGBToLinear : GrSRGBConversion::kLinearToSRGB; |
| 449 | } |
| 450 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 451 | static GrSRGBConversion determine_read_pixels_srgb_conversion(GrSRGBEncoded srcSRGBEncoded, |
| 452 | const SkColorSpace* srcColorSpace, |
| 453 | GrColorType dstColorType, |
| 454 | const SkColorSpace* dstColorSpace, |
| 455 | const GrCaps& caps) { |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 456 | // This is symmetrical with the write version. |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 457 | switch (determine_write_pixels_srgb_conversion(dstColorType, dstColorSpace, srcSRGBEncoded, |
| 458 | srcColorSpace, caps)) { |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 459 | 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 Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 466 | bool 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 Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 470 | #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 Osman | b62ea22 | 2016-12-22 11:12:16 -0500 | [diff] [blame] | 475 | // TODO: Color space conversion |
| 476 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 477 | ASSERT_SINGLE_OWNER_PRIV |
| 478 | RETURN_FALSE_IF_ABANDONED_PRIV |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 479 | SkASSERT(dst); |
| 480 | ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy()); |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 481 | GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels", fContext); |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 482 | |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 483 | if (!dst->asSurfaceProxy()->instantiate(this->resourceProvider())) { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 484 | return false; |
| 485 | } |
| 486 | |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 487 | GrSurfaceProxy* dstProxy = dst->asSurfaceProxy(); |
| 488 | GrSurface* dstSurface = dstProxy->priv().peekSurface(); |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 489 | |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 490 | // The src is unpremul but the dst is premul -> premul the src before or as part of the write |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 491 | const bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 492 | |
| 493 | if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) { |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 494 | return false; |
| 495 | } |
| 496 | |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 497 | // 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 Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 499 | bool useConfigConversionEffect = |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 500 | premul && pm_upm_must_round_trip(srcColorType, srcColorSpace) && |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 501 | pm_upm_must_round_trip(dstProxy->config(), dst->colorSpaceInfo().colorSpace()); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 502 | |
| 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()); |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 507 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 508 | // Trim the params here so that if we wind up making a temporary surface it can be as small as |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 509 | // necessary and because GrGpu::getWritePixelsInfo requires it. |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 510 | if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(), |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 511 | GrColorTypeBytesPerPixel(srcColorType), &left, &top, |
| 512 | &width, &height, &buffer, &rowBytes)) { |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 513 | return false; |
| 514 | } |
| 515 | |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 516 | GrGpu::DrawPreference drawPreference = premulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference |
| 517 | : GrGpu::kNoDraw_DrawPreference; |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 518 | GrGpu::WritePixelTempDrawInfo tempDrawInfo; |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 519 | GrSRGBConversion srgbConversion = determine_write_pixels_srgb_conversion( |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 520 | srcColorType, srcColorSpace, GrPixelConfigIsSRGBEncoded(dstProxy->config()), |
| 521 | dst->colorSpaceInfo().colorSpace(), *fContext->caps()); |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 522 | if (!fContext->fGpu->getWritePixelsInfo(dstSurface, dstProxy->origin(), width, height, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 523 | srcColorType, srgbConversion, &drawPreference, |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 524 | &tempDrawInfo)) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 525 | return false; |
| 526 | } |
| 527 | |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 528 | if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 529 | this->flush(nullptr); // MDB TODO: tighten this |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 532 | sk_sp<GrTextureProxy> tempProxy; |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 533 | if (GrGpu::kNoDraw_DrawPreference != drawPreference) { |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 534 | tempProxy = this->proxyProvider()->createProxy(tempDrawInfo.fTempSurfaceDesc, |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 535 | kTopLeft_GrSurfaceOrigin, |
| 536 | SkBackingFit::kApprox, SkBudgeted::kYes); |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 537 | if (!tempProxy && GrGpu::kRequireDraw_DrawPreference == drawPreference) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 538 | return false; |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | // temp buffer for doing sw premul conversion, if needed. |
| 543 | SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 544 | // 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 Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 549 | if (!sw_convert_to_premul(srcColorType, width, height, rowBytes, buffer, tmpRowBytes, |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 550 | tmpPixels.get())) { |
| 551 | return false; |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 552 | } |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 553 | rowBytes = tmpRowBytes; |
| 554 | buffer = tmpPixels.get(); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 555 | } |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 556 | |
| 557 | if (tempProxy) { |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 558 | auto fp = GrSimpleTextureEffect::Make(tempProxy, SkMatrix::I()); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 559 | if (premulOnGpu) { |
| 560 | fp = fContext->createUPMToPMEffect(std::move(fp), useConfigConversionEffect); |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 561 | } |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 562 | fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle); |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 563 | if (!fp) { |
| 564 | return false; |
| 565 | } |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 566 | |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 567 | if (!tempProxy->instantiate(this->resourceProvider())) { |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 568 | return false; |
| 569 | } |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 570 | GrTexture* texture = tempProxy->priv().peekTexture(); |
Robert Phillips | 7bbbf62 | 2017-10-17 07:36:59 -0400 | [diff] [blame] | 571 | |
| 572 | if (tempProxy->priv().hasPendingIO()) { |
| 573 | this->flush(tempProxy.get()); |
| 574 | } |
| 575 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 576 | if (!fContext->fGpu->writePixels(texture, tempProxy->origin(), 0, 0, width, height, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 577 | tempDrawInfo.fWriteColorType, buffer, rowBytes)) { |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 578 | return false; |
| 579 | } |
Robert Phillips | 7bbbf62 | 2017-10-17 07:36:59 -0400 | [diff] [blame] | 580 | tempProxy = nullptr; |
| 581 | |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 582 | SkMatrix matrix; |
| 583 | matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top)); |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 584 | GrRenderTargetContext* renderTargetContext = dst->asRenderTargetContext(); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 585 | if (!renderTargetContext) { |
| 586 | return false; |
| 587 | } |
| 588 | GrPaint paint; |
| 589 | paint.addColorFragmentProcessor(std::move(fp)); |
| 590 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 591 | paint.setAllowSRGBInputs(dst->colorSpaceInfo().isGammaCorrect() || |
| 592 | GrPixelConfigIsSRGB(dst->colorSpaceInfo().config())); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 593 | 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 Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 601 | return fContext->fGpu->writePixels(dstSurface, dstProxy->origin(), left, top, width, height, |
| 602 | srcColorType, buffer, rowBytes); |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 603 | } |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 604 | return true; |
| 605 | } |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 606 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 607 | bool 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 Osman | b62ea22 | 2016-12-22 11:12:16 -0500 | [diff] [blame] | 611 | // TODO: Color space conversion |
| 612 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 613 | ASSERT_SINGLE_OWNER_PRIV |
| 614 | RETURN_FALSE_IF_ABANDONED_PRIV |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 615 | SkASSERT(src); |
| 616 | ASSERT_OWNED_PROXY_PRIV(src->asSurfaceProxy()); |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 617 | GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "readSurfacePixels", fContext); |
bsalomon | 32ab260 | 2015-09-09 18:57:49 -0700 | [diff] [blame] | 618 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 619 | // MDB TODO: delay this instantiation until later in the method |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 620 | if (!src->asSurfaceProxy()->instantiate(this->resourceProvider())) { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 621 | return false; |
| 622 | } |
| 623 | |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 624 | GrSurfaceProxy* srcProxy = src->asSurfaceProxy(); |
| 625 | GrSurface* srcSurface = srcProxy->priv().peekSurface(); |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 626 | |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 627 | // 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 Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 629 | |
| 630 | if (!valid_pixel_conversion(dstColorType, srcProxy->config(), unpremul)) { |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 631 | return false; |
| 632 | } |
| 633 | |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 634 | // 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 Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 636 | bool useConfigConversionEffect = |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 637 | unpremul && |
| 638 | pm_upm_must_round_trip(srcProxy->config(), src->colorSpaceInfo().colorSpace()) && |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 639 | pm_upm_must_round_trip(dstColorType, dstColorSpace); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 640 | |
| 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()); |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 645 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 646 | // 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 Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 648 | if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(), |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 649 | GrColorTypeBytesPerPixel(dstColorType), &left, &top, |
| 650 | &width, &height, &buffer, &rowBytes)) { |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 651 | return false; |
| 652 | } |
| 653 | |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 654 | GrGpu::DrawPreference drawPreference = unpremulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference |
| 655 | : GrGpu::kNoDraw_DrawPreference; |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 656 | GrGpu::ReadPixelTempDrawInfo tempDrawInfo; |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 657 | GrSRGBConversion srgbConversion = determine_read_pixels_srgb_conversion( |
| 658 | GrPixelConfigIsSRGBEncoded(srcProxy->config()), src->colorSpaceInfo().colorSpace(), |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 659 | dstColorType, dstColorSpace, *fContext->caps()); |
| 660 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 661 | if (!fContext->fGpu->getReadPixelsInfo(srcSurface, srcProxy->origin(), width, height, rowBytes, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 662 | dstColorType, srgbConversion, &drawPreference, |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 663 | &tempDrawInfo)) { |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 664 | return false; |
| 665 | } |
bsalomon | 191bcc0 | 2014-11-14 11:31:13 -0800 | [diff] [blame] | 666 | |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 667 | if (!(kDontFlush_PixelOpsFlag & flags) && srcSurface->surfacePriv().hasPendingWrite()) { |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 668 | this->flush(nullptr); // MDB TODO: tighten this |
| 669 | } |
| 670 | |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 671 | sk_sp<GrSurfaceProxy> proxyToRead = src->asSurfaceProxyRef(); |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 672 | bool didTempDraw = false; |
| 673 | if (GrGpu::kNoDraw_DrawPreference != drawPreference) { |
bsalomon | b117ff1 | 2016-07-19 07:24:40 -0700 | [diff] [blame] | 674 | if (SkBackingFit::kExact == tempDrawInfo.fTempSurfaceFit) { |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 675 | // 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 Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 677 | if (width != srcSurface->width() || height != srcSurface->height()) { |
bsalomon | b117ff1 | 2016-07-19 07:24:40 -0700 | [diff] [blame] | 678 | tempDrawInfo.fTempSurfaceFit= SkBackingFit::kApprox; |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 679 | } |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 680 | } |
brianosman | dfe4f2e | 2016-07-21 13:28:36 -0700 | [diff] [blame] | 681 | // 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 Salomon | 366093f | 2018-02-13 09:25:22 -0500 | [diff] [blame] | 683 | // 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 Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 691 | fContext->contextPriv().makeDeferredRenderTargetContext( |
| 692 | tempDrawInfo.fTempSurfaceFit, |
Brian Salomon | 366093f | 2018-02-13 09:25:22 -0500 | [diff] [blame] | 693 | tempDrawInfo.fTempSurfaceDesc.fWidth, |
| 694 | tempDrawInfo.fTempSurfaceDesc.fHeight, |
| 695 | tempDrawInfo.fTempSurfaceDesc.fConfig, |
| 696 | std::move(colorSpace), |
| 697 | tempDrawInfo.fTempSurfaceDesc.fSampleCnt, |
| 698 | GrMipMapped::kNo, |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 699 | kTopLeft_GrSurfaceOrigin); |
Brian Osman | 693a540 | 2016-10-27 15:13:22 -0400 | [diff] [blame] | 700 | if (tempRTC) { |
Greg Daniel | f44cb48 | 2018-02-27 14:26:32 -0500 | [diff] [blame] | 701 | // Adding discard to appease vulkan validation warning about loading uninitialized data |
| 702 | // on draw |
| 703 | tempRTC->discard(); |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 704 | SkMatrix textureMatrix = SkMatrix::MakeTrans(SkIntToScalar(left), SkIntToScalar(top)); |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 705 | sk_sp<GrTextureProxy> proxy = src->asTextureProxyRef(); |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 706 | auto fp = GrSimpleTextureEffect::Make(std::move(proxy), textureMatrix); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 707 | 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.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 711 | } |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 712 | fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle); |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 713 | if (!fp) { |
| 714 | return false; |
| 715 | } |
Brian Osman | 60cd57e | 2017-04-06 10:19:06 -0400 | [diff] [blame] | 716 | |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 717 | 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.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 728 | } |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 729 | } |
joshualitt | 5c55fef | 2014-10-31 14:04:35 -0700 | [diff] [blame] | 730 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 731 | if (!proxyToRead) { |
| 732 | return false; |
| 733 | } |
| 734 | |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 735 | if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) { |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 736 | return false; |
| 737 | } |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 738 | GrColorType colorTypeToRead = dstColorType; |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 739 | if (didTempDraw) { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 740 | this->flushSurfaceWrites(proxyToRead.get()); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 741 | colorTypeToRead = tempDrawInfo.fReadColorType; |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 742 | } |
Robert Phillips | 09dfc47 | 2017-09-13 15:25:47 -0400 | [diff] [blame] | 743 | |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 744 | if (!proxyToRead->instantiate(this->resourceProvider())) { |
Robert Phillips | 09dfc47 | 2017-09-13 15:25:47 -0400 | [diff] [blame] | 745 | return false; |
| 746 | } |
| 747 | |
| 748 | GrSurface* surfaceToRead = proxyToRead->priv().peekSurface(); |
| 749 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 750 | if (!fContext->fGpu->readPixels(surfaceToRead, proxyToRead->origin(), left, top, width, height, |
| 751 | colorTypeToRead, buffer, rowBytes)) { |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 752 | return false; |
| 753 | } |
| 754 | |
| 755 | // Perform umpremul conversion if we weren't able to perform it as a draw. |
| 756 | if (unpremul) { |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 757 | SkColorType colorType = GrColorTypeToSkColorType(dstColorType); |
| 758 | if (kUnknown_SkColorType == colorType || 4 != SkColorTypeBytesPerPixel(colorType)) { |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 759 | return false; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 760 | } |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 761 | |
Matt Sarett | c7b2908 | 2017-02-09 16:22:39 -0500 | [diff] [blame] | 762 | 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.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 766 | } |
| 767 | return true; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 770 | bool 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 Salomon | 3d86a19 | 2018-02-27 16:46:11 -0500 | [diff] [blame] | 798 | if (!fContext->caps()->surfaceSupportsWritePixels(dstSurface)) { |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 799 | GrSurfaceDesc desc; |
| 800 | desc.fConfig = dstProxy->config(); |
| 801 | desc.fWidth = width; |
| 802 | desc.fHeight = height; |
| 803 | desc.fSampleCnt = 1; |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 804 | auto tempProxy = this->proxyProvider()->createProxy( |
| 805 | desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kApprox, SkBudgeted::kYes); |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 806 | 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 Salomon | 3d86a19 | 2018-02-27 16:46:11 -0500 | [diff] [blame] | 818 | return dst->copy(tempProxy.get(), SkIRect::MakeWH(width, height), {left, top}); |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 819 | } |
| 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 Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 894 | void GrContextPriv::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) { |
| 895 | ASSERT_SINGLE_OWNER_PRIV |
| 896 | RETURN_IF_ABANDONED_PRIV |
| 897 | SkASSERT(proxy); |
| 898 | ASSERT_OWNED_PROXY_PRIV(proxy); |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 899 | fContext->fDrawingManager->prepareSurfaceForExternalIO(proxy, 0, nullptr); |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 900 | } |
| 901 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 902 | void 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); |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 909 | } |
| 910 | } |
| 911 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 912 | void 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); |
ajuma | 95243eb | 2016-08-24 08:19:02 -0700 | [diff] [blame] | 919 | } |
| 920 | } |
| 921 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 922 | //////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | b471a32 | 2014-03-10 07:40:03 +0000 | [diff] [blame] | 923 | |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 924 | sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy, |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 925 | sk_sp<SkColorSpace> colorSpace, |
| 926 | const SkSurfaceProps* props) { |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 927 | ASSERT_SINGLE_OWNER_PRIV |
| 928 | |
Brian Salomon | 366093f | 2018-02-13 09:25:22 -0500 | [diff] [blame] | 929 | // 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 Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 935 | if (proxy->asRenderTargetProxy()) { |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 936 | return this->drawingManager()->makeRenderTargetContext(std::move(proxy), |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 937 | std::move(colorSpace), props); |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 938 | } else { |
| 939 | SkASSERT(proxy->asTextureProxy()); |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 940 | SkASSERT(!props); |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 941 | return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace)); |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 942 | } |
| 943 | } |
| 944 | |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 945 | sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrSurfaceDesc& dstDesc, |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 946 | GrSurfaceOrigin origin, |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 947 | GrMipMapped mipMapped, |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 948 | SkBackingFit fit, |
Brian Salomon | 366093f | 2018-02-13 09:25:22 -0500 | [diff] [blame] | 949 | SkBudgeted isDstBudgeted, |
| 950 | sk_sp<SkColorSpace> colorSpace, |
| 951 | const SkSurfaceProps* props) { |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 952 | sk_sp<GrTextureProxy> proxy; |
| 953 | if (GrMipMapped::kNo == mipMapped) { |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 954 | proxy = this->proxyProvider()->createProxy(dstDesc, origin, fit, isDstBudgeted); |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 955 | } else { |
| 956 | SkASSERT(SkBackingFit::kExact == fit); |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 957 | proxy = this->proxyProvider()->createMipMapProxy(dstDesc, origin, isDstBudgeted); |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 958 | } |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 959 | if (!proxy) { |
| 960 | return nullptr; |
| 961 | } |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 962 | |
Brian Salomon | 366093f | 2018-02-13 09:25:22 -0500 | [diff] [blame] | 963 | return this->makeWrappedSurfaceContext(std::move(proxy), std::move(colorSpace), props); |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 964 | } |
| 965 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 966 | sk_sp<GrTextureContext> GrContextPriv::makeBackendTextureContext(const GrBackendTexture& tex, |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 967 | GrSurfaceOrigin origin, |
Brian Osman | c1e3705 | 2017-03-09 14:19:20 -0500 | [diff] [blame] | 968 | sk_sp<SkColorSpace> colorSpace) { |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 969 | ASSERT_SINGLE_OWNER_PRIV |
| 970 | |
Brian Salomon | 7578f3e | 2018-03-07 14:39:54 -0500 | [diff] [blame] | 971 | sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->wrapBackendTexture(tex, origin); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 972 | if (!proxy) { |
| 973 | return nullptr; |
| 974 | } |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 975 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 976 | return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace)); |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 977 | } |
| 978 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 979 | sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext( |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 980 | const GrBackendTexture& tex, |
| 981 | GrSurfaceOrigin origin, |
| 982 | int sampleCnt, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 983 | sk_sp<SkColorSpace> colorSpace, |
Brian Osman | c1e3705 | 2017-03-09 14:19:20 -0500 | [diff] [blame] | 984 | const SkSurfaceProps* props) { |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 985 | ASSERT_SINGLE_OWNER_PRIV |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 986 | SkASSERT(sampleCnt > 0); |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 987 | |
Brian Salomon | 7578f3e | 2018-03-07 14:39:54 -0500 | [diff] [blame] | 988 | sk_sp<GrTextureProxy> proxy( |
| 989 | this->proxyProvider()->wrapRenderableBackendTexture(tex, origin, sampleCnt)); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 990 | if (!proxy) { |
| 991 | return nullptr; |
| 992 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 993 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 994 | return this->drawingManager()->makeRenderTargetContext(std::move(proxy), |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 995 | std::move(colorSpace), props); |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 996 | } |
| 997 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 998 | sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext( |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 999 | const GrBackendRenderTarget& backendRT, |
| 1000 | GrSurfaceOrigin origin, |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 1001 | sk_sp<SkColorSpace> colorSpace, |
| 1002 | const SkSurfaceProps* surfaceProps) { |
| 1003 | ASSERT_SINGLE_OWNER_PRIV |
| 1004 | |
Brian Salomon | 7578f3e | 2018-03-07 14:39:54 -0500 | [diff] [blame] | 1005 | sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->wrapBackendRenderTarget(backendRT, origin); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 1006 | if (!proxy) { |
| 1007 | return nullptr; |
| 1008 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1009 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 1010 | return this->drawingManager()->makeRenderTargetContext(std::move(proxy), |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1011 | std::move(colorSpace), |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1012 | surfaceProps); |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1015 | sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext( |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 1016 | const GrBackendTexture& tex, |
| 1017 | GrSurfaceOrigin origin, |
| 1018 | int sampleCnt, |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 1019 | sk_sp<SkColorSpace> colorSpace, |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 1020 | const SkSurfaceProps* props) { |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 1021 | ASSERT_SINGLE_OWNER_PRIV |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1022 | SkASSERT(sampleCnt > 0); |
Brian Salomon | 7578f3e | 2018-03-07 14:39:54 -0500 | [diff] [blame] | 1023 | sk_sp<GrSurfaceProxy> proxy( |
| 1024 | this->proxyProvider()->wrapBackendTextureAsRenderTarget(tex, origin, sampleCnt)); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 1025 | if (!proxy) { |
| 1026 | return nullptr; |
| 1027 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1028 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 1029 | return this->drawingManager()->makeRenderTargetContext(std::move(proxy), |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1030 | std::move(colorSpace), |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 1031 | props); |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
Chris Dalton | fe199b7 | 2017-05-05 11:26:15 -0400 | [diff] [blame] | 1034 | void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) { |
| 1035 | fContext->fDrawingManager->addOnFlushCallbackObject(onFlushCBObject); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 1036 | } |
| 1037 | |
Robert Phillips | 6200036 | 2018-02-01 09:10:04 -0500 | [diff] [blame] | 1038 | void GrContextPriv::moveOpListsToDDL(SkDeferredDisplayList* ddl) { |
| 1039 | fContext->fDrawingManager->moveOpListsToDDL(ddl); |
| 1040 | } |
| 1041 | |
| 1042 | void GrContextPriv::copyOpListsFromDDL(const SkDeferredDisplayList* ddl, |
| 1043 | GrRenderTargetProxy* newDest) { |
| 1044 | fContext->fDrawingManager->copyOpListsFromDDL(ddl, newDest); |
| 1045 | } |
| 1046 | |
robertphillips | 48fde9c | 2016-09-06 05:20:20 -0700 | [diff] [blame] | 1047 | static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) { |
Brian Osman | 78f20e0 | 2017-01-12 10:28:01 -0500 | [diff] [blame] | 1048 | switch (config) { |
| 1049 | case kAlpha_8_GrPixelConfig: |
| 1050 | case kRGB_565_GrPixelConfig: |
| 1051 | case kRGBA_4444_GrPixelConfig: |
| 1052 | case kBGRA_8888_GrPixelConfig: |
Brian Osman | 10fc6fd | 2018-03-02 11:01:10 -0500 | [diff] [blame] | 1053 | case kRGBA_1010102_GrPixelConfig: |
Brian Osman | 78f20e0 | 2017-01-12 10:28:01 -0500 | [diff] [blame] | 1054 | 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 | } |
robertphillips | 48fde9c | 2016-09-06 05:20:20 -0700 | [diff] [blame] | 1062 | } |
| 1063 | |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 1064 | sk_sp<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContextWithFallback( |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 1065 | SkBackingFit fit, |
| 1066 | int width, int height, |
| 1067 | GrPixelConfig config, |
| 1068 | sk_sp<SkColorSpace> colorSpace, |
| 1069 | int sampleCnt, |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 1070 | GrMipMapped mipMapped, |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 1071 | GrSurfaceOrigin origin, |
| 1072 | const SkSurfaceProps* surfaceProps, |
| 1073 | SkBudgeted budgeted) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1074 | SkASSERT(sampleCnt > 0); |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 1075 | if (0 == fContext->caps()->getRenderTargetSampleCount(sampleCnt, config)) { |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 1076 | config = GrPixelConfigFallback(config); |
| 1077 | } |
| 1078 | |
| 1079 | return this->makeDeferredRenderTargetContext(fit, width, height, config, std::move(colorSpace), |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 1080 | sampleCnt, mipMapped, origin, surfaceProps, |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 1081 | budgeted); |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 1082 | } |
| 1083 | |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 1084 | sk_sp<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContext( |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1085 | SkBackingFit fit, |
| 1086 | int width, int height, |
| 1087 | GrPixelConfig config, |
| 1088 | sk_sp<SkColorSpace> colorSpace, |
| 1089 | int sampleCnt, |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 1090 | GrMipMapped mipMapped, |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1091 | GrSurfaceOrigin origin, |
| 1092 | const SkSurfaceProps* surfaceProps, |
| 1093 | SkBudgeted budgeted) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1094 | SkASSERT(sampleCnt > 0); |
Brian Salomon | 79e4d1b | 2017-07-13 11:17:11 -0400 | [diff] [blame] | 1095 | if (this->abandoned()) { |
| 1096 | return nullptr; |
| 1097 | } |
| 1098 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1099 | GrSurfaceDesc desc; |
| 1100 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1101 | desc.fWidth = width; |
| 1102 | desc.fHeight = height; |
| 1103 | desc.fConfig = config; |
| 1104 | desc.fSampleCnt = sampleCnt; |
| 1105 | |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 1106 | sk_sp<GrTextureProxy> rtp; |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 1107 | if (GrMipMapped::kNo == mipMapped) { |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 1108 | rtp = fContext->fProxyProvider->createProxy(desc, origin, fit, budgeted); |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 1109 | } else { |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 1110 | rtp = fContext->fProxyProvider->createMipMapProxy(desc, origin, budgeted); |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 1111 | } |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 1112 | if (!rtp) { |
| 1113 | return nullptr; |
| 1114 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1115 | |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 1116 | sk_sp<GrRenderTargetContext> renderTargetContext( |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 1117 | fContext->fDrawingManager->makeRenderTargetContext(std::move(rtp), |
| 1118 | std::move(colorSpace), |
| 1119 | surfaceProps)); |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 1120 | if (!renderTargetContext) { |
| 1121 | return nullptr; |
| 1122 | } |
| 1123 | |
| 1124 | renderTargetContext->discard(); |
| 1125 | |
| 1126 | return renderTargetContext; |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1127 | } |
| 1128 | |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 1129 | bool GrContextPriv::abandoned() const { |
| 1130 | ASSERT_SINGLE_OWNER_PRIV |
| 1131 | return fContext->fDrawingManager->wasAbandoned(); |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 1132 | } |
| 1133 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 1134 | std::unique_ptr<GrFragmentProcessor> GrContext::createPMToUPMEffect( |
| 1135 | std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) { |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 1136 | ASSERT_SINGLE_OWNER |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 1137 | // 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 Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1144 | return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul); |
Brian Osman | 2d2da4f | 2017-04-12 17:07:22 -0400 | [diff] [blame] | 1145 | } 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 Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 1149 | } |
| 1150 | } |
| 1151 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 1152 | std::unique_ptr<GrFragmentProcessor> GrContext::createUPMToPMEffect( |
| 1153 | std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1154 | ASSERT_SINGLE_OWNER |
Brian Osman | 2d2da4f | 2017-04-12 17:07:22 -0400 | [diff] [blame] | 1155 | // We have specialized effects that guarantee round-trip conversion for these formats |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 1156 | 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 Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1162 | return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul); |
Brian Osman | 2d2da4f | 2017-04-12 17:07:22 -0400 | [diff] [blame] | 1163 | } 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.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1167 | } |
| 1168 | } |
| 1169 | |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 1170 | bool GrContext::validPMUPMConversionExists() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1171 | ASSERT_SINGLE_OWNER |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 1172 | if (!fDidTestPMConversions) { |
Brian Osman | 28804f3 | 2017-04-20 10:24:36 -0400 | [diff] [blame] | 1173 | fPMUPMConversionsRoundTrip = GrConfigConversionEffect::TestForPreservingPMConversions(this); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 1174 | fDidTestPMConversions = true; |
| 1175 | } |
| 1176 | |
bsalomon | 636e802 | 2015-07-29 06:08:46 -0700 | [diff] [blame] | 1177 | // The PM<->UPM tests fail or succeed together so we only need to check one. |
Brian Osman | 28804f3 | 2017-04-20 10:24:36 -0400 | [diff] [blame] | 1178 | return fPMUPMConversionsRoundTrip; |
bsalomon | 636e802 | 2015-07-29 06:08:46 -0700 | [diff] [blame] | 1179 | } |
| 1180 | |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 1181 | ////////////////////////////////////////////////////////////////////////////// |
| 1182 | |
Robert Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 1183 | // DDL TODO: remove 'maxResources' |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 1184 | void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1185 | ASSERT_SINGLE_OWNER |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 1186 | if (maxResources) { |
| 1187 | *maxResources = fResourceCache->getMaxResourceCount(); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 1188 | } |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 1189 | if (maxResourceBytes) { |
| 1190 | *maxResourceBytes = fResourceCache->getMaxResourceBytes(); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 1191 | } |
| 1192 | } |
| 1193 | |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 1194 | void GrContext::setResourceCacheLimits(int maxResources, size_t maxResourceBytes) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1195 | ASSERT_SINGLE_OWNER |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 1196 | fResourceCache->setLimits(maxResources, maxResourceBytes); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 1197 | } |
| 1198 | |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 1199 | ////////////////////////////////////////////////////////////////////////////// |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 1200 | void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1201 | ASSERT_SINGLE_OWNER |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 1202 | fResourceCache->dumpMemoryStatistics(traceMemoryDump); |
| 1203 | } |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 1204 | |
| 1205 | ////////////////////////////////////////////////////////////////////////////// |
| 1206 | |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 1207 | SkString GrContextPriv::dump() const { |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 1208 | 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 Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 1222 | writer.appendString("backend", kBackendStr[fContext->fBackend]); |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 1223 | |
| 1224 | writer.appendName("caps"); |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 1225 | fContext->fCaps->dumpJSON(&writer); |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 1226 | |
| 1227 | writer.appendName("gpu"); |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 1228 | fContext->fGpu->dumpJSON(&writer); |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 1229 | |
| 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 | } |