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" |
Robert Phillips | 41a3b87 | 2018-03-09 12:00:34 -0500 | [diff] [blame] | 27 | #include "GrTextureStripAtlas.h" |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 28 | #include "GrTracing.h" |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 29 | #include "SkAutoPixmapStorage.h" |
Matt Sarett | 485c499 | 2017-02-14 14:18:27 -0500 | [diff] [blame] | 30 | #include "SkConvertPixels.h" |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 31 | #include "SkDeferredDisplayList.h" |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 32 | #include "SkGr.h" |
Mike Reed | 7fcfb62 | 2018-02-09 13:26:46 -0500 | [diff] [blame] | 33 | #include "SkImageInfoPriv.h" |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 34 | #include "SkJSONWriter.h" |
Brian Osman | 5127998 | 2017-08-23 10:12:00 -0400 | [diff] [blame] | 35 | #include "SkMakeUnique.h" |
Robert Phillips | 6b6fcc7 | 2018-03-30 13:57:00 -0400 | [diff] [blame] | 36 | #include "SkSurface_Gpu.h" |
Brian Osman | 5127998 | 2017-08-23 10:12:00 -0400 | [diff] [blame] | 37 | #include "SkTaskGroup.h" |
Matt Sarett | c7b2908 | 2017-02-09 16:22:39 -0500 | [diff] [blame] | 38 | #include "SkUnPreMultiplyPriv.h" |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 39 | #include "effects/GrConfigConversionEffect.h" |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 40 | #include "text/GrTextBlobCache.h" |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 41 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 42 | #define ASSERT_OWNED_PROXY(P) \ |
| 43 | SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == this) |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 44 | #define ASSERT_OWNED_PROXY_PRIV(P) \ |
| 45 | SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == fContext) |
| 46 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 47 | #define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this) |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 48 | #define ASSERT_SINGLE_OWNER \ |
| 49 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fSingleOwner);) |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 50 | #define ASSERT_SINGLE_OWNER_PRIV \ |
| 51 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fContext->fSingleOwner);) |
robertphillips | 7761d61 | 2016-05-16 09:14:53 -0700 | [diff] [blame] | 52 | #define RETURN_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return; } |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 53 | #define RETURN_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return; } |
robertphillips | 7761d61 | 2016-05-16 09:14:53 -0700 | [diff] [blame] | 54 | #define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return false; } |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 55 | #define RETURN_FALSE_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return false; } |
robertphillips | 7761d61 | 2016-05-16 09:14:53 -0700 | [diff] [blame] | 56 | #define RETURN_NULL_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return nullptr; } |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 57 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 58 | //////////////////////////////////////////////////////////////////////////////// |
| 59 | |
joshualitt | 0acd0d3 | 2015-05-07 08:23:19 -0700 | [diff] [blame] | 60 | static int32_t gNextID = 1; |
| 61 | static int32_t next_id() { |
| 62 | int32_t id; |
| 63 | do { |
| 64 | id = sk_atomic_inc(&gNextID); |
| 65 | } while (id == SK_InvalidGenID); |
| 66 | return id; |
| 67 | } |
| 68 | |
Robert Phillips | fde6fa0 | 2018-03-02 08:53:14 -0500 | [diff] [blame] | 69 | GrContext::GrContext(GrBackend backend, int32_t id) |
| 70 | : fBackend(backend) |
| 71 | , fUniqueID(SK_InvalidGenID == id ? next_id() : id) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 72 | fResourceCache = nullptr; |
| 73 | fResourceProvider = nullptr; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 74 | fProxyProvider = nullptr; |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 75 | fGlyphCache = nullptr; |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Robert Phillips | fde6fa0 | 2018-03-02 08:53:14 -0500 | [diff] [blame] | 78 | bool GrContext::initCommon(const GrContextOptions& options) { |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 79 | ASSERT_SINGLE_OWNER |
Robert Phillips | fde6fa0 | 2018-03-02 08:53:14 -0500 | [diff] [blame] | 80 | SkASSERT(fCaps); // needs to have been initialized by derived classes |
| 81 | SkASSERT(fThreadSafeProxy); // needs to have been initialized by derived classes |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 82 | |
| 83 | if (fGpu) { |
| 84 | fCaps = fGpu->refCaps(); |
| 85 | fResourceCache = new GrResourceCache(fCaps.get(), fUniqueID); |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 86 | fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, &fSingleOwner, |
| 87 | options.fExplicitlyAllocateGPUResources); |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 88 | } |
| 89 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 90 | fProxyProvider = new GrProxyProvider(fResourceProvider, fResourceCache, fCaps, &fSingleOwner); |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 91 | |
| 92 | if (fResourceCache) { |
| 93 | fResourceCache->setProxyProvider(fProxyProvider); |
| 94 | } |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 95 | |
Robert Phillips | 41a3b87 | 2018-03-09 12:00:34 -0500 | [diff] [blame] | 96 | fTextureStripAtlasManager.reset(new GrTextureStripAtlasManager); |
| 97 | |
Brian Osman | 46da1cc | 2017-02-14 14:15:48 -0500 | [diff] [blame] | 98 | fDisableGpuYUVConversion = options.fDisableGpuYUVConversion; |
Brian Osman | 8a83ca4 | 2018-02-12 14:32:17 -0500 | [diff] [blame] | 99 | fSharpenMipmappedTextures = options.fSharpenMipmappedTextures; |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 100 | fDidTestPMConversions = false; |
| 101 | |
bsalomon | 6b2552f | 2016-09-15 13:50:26 -0700 | [diff] [blame] | 102 | GrPathRendererChain::Options prcOptions; |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 103 | prcOptions.fAllowPathMaskCaching = options.fAllowPathMaskCaching; |
Brian Osman | 195c05b | 2017-08-30 15:14:04 -0400 | [diff] [blame] | 104 | #if GR_TEST_UTILS |
csmartdalton | 008b9d8 | 2017-02-22 12:00:42 -0700 | [diff] [blame] | 105 | prcOptions.fGpuPathRenderers = options.fGpuPathRenderers; |
Brian Osman | 195c05b | 2017-08-30 15:14:04 -0400 | [diff] [blame] | 106 | #endif |
Brian Osman | b350ae2 | 2017-08-29 16:15:39 -0400 | [diff] [blame] | 107 | if (options.fDisableDistanceFieldPaths) { |
Brian Osman | 195c05b | 2017-08-30 15:14:04 -0400 | [diff] [blame] | 108 | prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall; |
Brian Osman | b350ae2 | 2017-08-29 16:15:39 -0400 | [diff] [blame] | 109 | } |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 110 | |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 111 | if (!fResourceCache) { |
| 112 | // DDL TODO: remove this crippling of the path renderer chain |
| 113 | // Disable the small path renderer bc of the proxies in the atlas. They need to be |
| 114 | // unified when the opLists are added back to the destination drawing manager. |
| 115 | prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall; |
Robert Phillips | f733a72 | 2018-03-15 13:59:35 -0400 | [diff] [blame] | 116 | prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kCoverageCounting; |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 117 | } |
| 118 | |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 119 | GrAtlasTextContext::Options atlasTextContextOptions; |
| 120 | atlasTextContextOptions.fMaxDistanceFieldFontSize = options.fGlyphsAsPathsFontSize; |
| 121 | atlasTextContextOptions.fMinDistanceFieldFontSize = options.fMinDistanceFieldFontSize; |
Brian Salomon | b508696 | 2017-12-13 10:59:33 -0500 | [diff] [blame] | 122 | atlasTextContextOptions.fDistanceFieldVerticesAlwaysHaveW = false; |
| 123 | #if SK_SUPPORT_ATLAS_TEXT |
| 124 | if (GrContextOptions::Enable::kYes == options.fDistanceFieldGlyphVerticesAlwaysHaveW) { |
| 125 | atlasTextContextOptions.fDistanceFieldVerticesAlwaysHaveW = true; |
| 126 | } |
| 127 | #endif |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 128 | |
Robert Phillips | 64ecdce | 2018-04-02 10:26:39 -0400 | [diff] [blame] | 129 | bool explicitlyAllocatingResources = fResourceProvider |
| 130 | ? fResourceProvider->explicitlyAllocateGPUResources() |
| 131 | : false; |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 132 | fDrawingManager.reset(new GrDrawingManager(this, prcOptions, atlasTextContextOptions, |
Robert Phillips | 64ecdce | 2018-04-02 10:26:39 -0400 | [diff] [blame] | 133 | &fSingleOwner, explicitlyAllocatingResources, |
| 134 | options.fSortRenderTargets)); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 135 | |
Robert Phillips | af4adef | 2018-03-07 11:59:37 -0500 | [diff] [blame] | 136 | fGlyphCache = new GrGlyphCache(fCaps.get(), options.fGlyphCacheTextureMaximumBytes); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 137 | |
Robert Phillips | 303cd58 | 2018-02-14 18:54:01 -0500 | [diff] [blame] | 138 | fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB, |
| 139 | this, this->uniqueID(), SkToBool(fGpu))); |
Brian Salomon | 91a3e52 | 2017-06-23 10:58:19 -0400 | [diff] [blame] | 140 | |
Robert Phillips | fde6fa0 | 2018-03-02 08:53:14 -0500 | [diff] [blame] | 141 | // DDL TODO: we need to think through how the task group & persistent cache |
| 142 | // get passed on to/shared between all the DDLRecorders created with this context. |
Brian Osman | 5127998 | 2017-08-23 10:12:00 -0400 | [diff] [blame] | 143 | if (options.fExecutor) { |
| 144 | fTaskGroup = skstd::make_unique<SkTaskGroup>(*options.fExecutor); |
| 145 | } |
| 146 | |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 147 | fPersistentCache = options.fPersistentCache; |
| 148 | |
Brian Salomon | 91a3e52 | 2017-06-23 10:58:19 -0400 | [diff] [blame] | 149 | return true; |
bsalomon@google.com | c0af317 | 2012-06-15 14:10:09 +0000 | [diff] [blame] | 150 | } |
| 151 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 152 | GrContext::~GrContext() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 153 | ASSERT_SINGLE_OWNER |
| 154 | |
Robert Phillips | 2e6feed | 2018-01-22 15:27:20 -0500 | [diff] [blame] | 155 | if (fDrawingManager) { |
| 156 | fDrawingManager->cleanup(); |
| 157 | } |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 158 | |
Robert Phillips | 41a3b87 | 2018-03-09 12:00:34 -0500 | [diff] [blame] | 159 | fTextureStripAtlasManager = nullptr; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 160 | delete fResourceProvider; |
| 161 | delete fResourceCache; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 162 | delete fProxyProvider; |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 163 | delete fGlyphCache; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 164 | } |
| 165 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 166 | sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() { |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 167 | return fThreadSafeProxy; |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Robert Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 170 | SkSurfaceCharacterization GrContextThreadSafeProxy::createCharacterization( |
| 171 | size_t cacheMaxResourceBytes, |
| 172 | const SkImageInfo& ii, const GrBackendFormat& backendFormat, |
| 173 | int sampleCnt, GrSurfaceOrigin origin, |
| 174 | const SkSurfaceProps& surfaceProps, |
| 175 | bool isMipMapped) { |
| 176 | if (!backendFormat.isValid()) { |
| 177 | return SkSurfaceCharacterization(); // return an invalid characterization |
| 178 | } |
| 179 | |
Robert Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 180 | if (!fCaps->mipMapSupport()) { |
| 181 | isMipMapped = false; |
| 182 | } |
| 183 | |
| 184 | GrPixelConfig config = kUnknown_GrPixelConfig; |
| 185 | if (!fCaps->getConfigFromBackendFormat(backendFormat, ii.colorType(), &config)) { |
| 186 | return SkSurfaceCharacterization(); // return an invalid characterization |
| 187 | } |
| 188 | |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 189 | if (!SkSurface_Gpu::Valid(fCaps.get(), config, ii.colorSpace())) { |
| 190 | return SkSurfaceCharacterization(); // return an invalid characterization |
| 191 | } |
| 192 | |
Robert Phillips | 6b6fcc7 | 2018-03-30 13:57:00 -0400 | [diff] [blame] | 193 | sampleCnt = fCaps->getRenderTargetSampleCount(sampleCnt, config); |
| 194 | if (!sampleCnt) { |
| 195 | return SkSurfaceCharacterization(); // return an invalid characterization |
| 196 | } |
| 197 | |
| 198 | GrFSAAType FSAAType = GrFSAAType::kNone; |
| 199 | if (sampleCnt > 1) { |
| 200 | FSAAType = fCaps->usesMixedSamples() ? GrFSAAType::kMixedSamples : GrFSAAType::kUnifiedMSAA; |
| 201 | } |
| 202 | |
Robert Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 203 | // This surface characterization factory assumes that the resulting characterization is |
| 204 | // textureable. |
| 205 | if (!fCaps->isConfigTexturable(config)) { |
| 206 | return SkSurfaceCharacterization(); // return an invalid characterization |
| 207 | } |
| 208 | |
| 209 | return SkSurfaceCharacterization(sk_ref_sp<GrContextThreadSafeProxy>(this), |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 210 | cacheMaxResourceBytes, ii, |
| 211 | origin, config, FSAAType, sampleCnt, |
Robert Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 212 | SkSurfaceCharacterization::Textureable(true), |
| 213 | SkSurfaceCharacterization::MipMapped(isMipMapped), |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 214 | surfaceProps); |
Robert Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 215 | } |
| 216 | |
bsalomon | 2354f84 | 2014-07-28 13:48:36 -0700 | [diff] [blame] | 217 | void GrContext::abandonContext() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 218 | ASSERT_SINGLE_OWNER |
| 219 | |
Robert Phillips | 41a3b87 | 2018-03-09 12:00:34 -0500 | [diff] [blame] | 220 | fTextureStripAtlasManager->abandon(); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 221 | fProxyProvider->abandon(); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 222 | fResourceProvider->abandon(); |
robertphillips | 0dfa62c | 2015-11-16 06:23:31 -0800 | [diff] [blame] | 223 | |
| 224 | // Need to abandon the drawing manager first so all the render targets |
| 225 | // will be released/forgotten before they too are abandoned. |
| 226 | fDrawingManager->abandon(); |
| 227 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 228 | // abandon first to so destructors |
| 229 | // don't try to free the resources in the API. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 230 | fResourceCache->abandonAll(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 231 | |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 232 | fGpu->disconnect(GrGpu::DisconnectType::kAbandon); |
| 233 | |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 234 | fGlyphCache->freeAll(); |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 235 | fTextBlobCache->freeAll(); |
| 236 | } |
| 237 | |
| 238 | void GrContext::releaseResourcesAndAbandonContext() { |
| 239 | ASSERT_SINGLE_OWNER |
| 240 | |
Robert Phillips | 41a3b87 | 2018-03-09 12:00:34 -0500 | [diff] [blame] | 241 | fTextureStripAtlasManager->abandon(); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 242 | fProxyProvider->abandon(); |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 243 | fResourceProvider->abandon(); |
| 244 | |
| 245 | // Need to abandon the drawing manager first so all the render targets |
| 246 | // will be released/forgotten before they too are abandoned. |
| 247 | fDrawingManager->abandon(); |
| 248 | |
| 249 | // Release all resources in the backend 3D API. |
| 250 | fResourceCache->releaseAll(); |
| 251 | |
| 252 | fGpu->disconnect(GrGpu::DisconnectType::kCleanup); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 253 | |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 254 | fGlyphCache->freeAll(); |
joshualitt | 26ffc00 | 2015-04-16 11:24:04 -0700 | [diff] [blame] | 255 | fTextBlobCache->freeAll(); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 256 | } |
| 257 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 258 | void GrContext::resetContext(uint32_t state) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 259 | ASSERT_SINGLE_OWNER |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 260 | fGpu->markContextDirty(state); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | void GrContext::freeGpuResources() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 264 | ASSERT_SINGLE_OWNER |
| 265 | |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 266 | fGlyphCache->freeAll(); |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 267 | |
| 268 | fDrawingManager->freeGpuResources(); |
bsalomon | 3033b9f | 2015-04-13 11:09:56 -0700 | [diff] [blame] | 269 | |
| 270 | fResourceCache->purgeAllUnlocked(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Robert Phillips | 6eba063 | 2018-03-28 12:25:42 -0400 | [diff] [blame] | 273 | void GrContext::purgeUnlockedResources(bool scratchResourcesOnly) { |
| 274 | ASSERT_SINGLE_OWNER |
| 275 | fResourceCache->purgeUnlockedResources(scratchResourcesOnly); |
| 276 | fResourceCache->purgeAsNeeded(); |
| 277 | fTextBlobCache->purgeStaleBlobs(); |
| 278 | } |
| 279 | |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 280 | void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) { |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 281 | ASSERT_SINGLE_OWNER |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 282 | fResourceCache->purgeAsNeeded(); |
| 283 | fResourceCache->purgeResourcesNotUsedSince(GrStdSteadyClock::now() - msNotUsed); |
| 284 | |
| 285 | fTextBlobCache->purgeStaleBlobs(); |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 286 | } |
| 287 | |
Derek Sollenberger | 5480a18 | 2017-05-25 16:43:59 -0400 | [diff] [blame] | 288 | void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) { |
| 289 | ASSERT_SINGLE_OWNER |
| 290 | fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources); |
| 291 | } |
| 292 | |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 293 | void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 294 | ASSERT_SINGLE_OWNER |
| 295 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 296 | if (resourceCount) { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 297 | *resourceCount = fResourceCache->getBudgetedResourceCount(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 298 | } |
| 299 | if (resourceBytes) { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 300 | *resourceBytes = fResourceCache->getBudgetedResourceBytes(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 301 | } |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 304 | size_t GrContext::getResourceCachePurgeableBytes() const { |
| 305 | ASSERT_SINGLE_OWNER |
| 306 | return fResourceCache->getPurgeableBytes(); |
| 307 | } |
| 308 | |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 309 | //////////////////////////////////////////////////////////////////////////////// |
| 310 | |
Brian Salomon | f932a63 | 2018-04-05 12:46:09 -0400 | [diff] [blame] | 311 | int GrContext::maxTextureSize() const { return this->caps()->maxTextureSize(); } |
| 312 | |
| 313 | int GrContext::maxRenderTargetSize() const { return this->caps()->maxRenderTargetSize(); } |
| 314 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 315 | bool GrContext::colorTypeSupportedAsImage(SkColorType colorType) const { |
| 316 | GrPixelConfig config = SkImageInfo2GrPixelConfig(colorType, nullptr, *this->caps()); |
| 317 | return this->caps()->isConfigTexturable(config); |
| 318 | } |
| 319 | |
| 320 | int GrContext::maxSurfaceSampleCountForColorType(SkColorType colorType) const { |
| 321 | GrPixelConfig config = SkImageInfo2GrPixelConfig(colorType, nullptr, *this->caps()); |
| 322 | return this->caps()->maxRenderTargetSampleCount(config); |
| 323 | } |
| 324 | |
| 325 | //////////////////////////////////////////////////////////////////////////////// |
| 326 | |
joshualitt | 0db6dfa | 2015-04-10 07:01:30 -0700 | [diff] [blame] | 327 | void GrContext::TextBlobCacheOverBudgetCB(void* data) { |
| 328 | SkASSERT(data); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 329 | // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on |
| 330 | // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls |
| 331 | // to below the GrContext level, but this is not trivial because they call drawPath on |
| 332 | // SkGpuDevice. |
joshualitt | 0db6dfa | 2015-04-10 07:01:30 -0700 | [diff] [blame] | 333 | GrContext* context = reinterpret_cast<GrContext*>(data); |
| 334 | context->flush(); |
| 335 | } |
| 336 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 337 | //////////////////////////////////////////////////////////////////////////////// |
| 338 | |
bsalomon | b77a907 | 2016-09-07 10:02:04 -0700 | [diff] [blame] | 339 | void GrContext::flush() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 340 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 341 | RETURN_IF_ABANDONED |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 342 | |
| 343 | fDrawingManager->flush(nullptr); |
| 344 | } |
| 345 | |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 346 | GrSemaphoresSubmitted GrContext::flushAndSignalSemaphores(int numSemaphores, |
| 347 | GrBackendSemaphore signalSemaphores[]) { |
| 348 | ASSERT_SINGLE_OWNER |
| 349 | if (fDrawingManager->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; } |
| 350 | |
| 351 | return fDrawingManager->flush(nullptr, numSemaphores, signalSemaphores); |
| 352 | } |
| 353 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 354 | void GrContextPriv::flush(GrSurfaceProxy* proxy) { |
| 355 | ASSERT_SINGLE_OWNER_PRIV |
| 356 | RETURN_IF_ABANDONED_PRIV |
| 357 | ASSERT_OWNED_PROXY_PRIV(proxy); |
| 358 | |
| 359 | fContext->fDrawingManager->flush(proxy); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 362 | 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] | 363 | const void* inPixels, size_t outRowBytes, void* outPixels) { |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 364 | SkColorType colorType = GrColorTypeToSkColorType(srcColorType); |
| 365 | if (kUnknown_SkColorType == colorType || 4 != SkColorTypeBytesPerPixel(colorType)) { |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 366 | return false; |
| 367 | } |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 368 | |
Matt Sarett | c7b2908 | 2017-02-09 16:22:39 -0500 | [diff] [blame] | 369 | for (int y = 0; y < height; y++) { |
| 370 | SkOpts::RGBA_to_rgbA((uint32_t*) outPixels, inPixels, width); |
| 371 | outPixels = SkTAddOffset<void>(outPixels, outRowBytes); |
| 372 | inPixels = SkTAddOffset<const void>(inPixels, inRowBytes); |
| 373 | } |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 374 | |
Matt Sarett | c7b2908 | 2017-02-09 16:22:39 -0500 | [diff] [blame] | 375 | return true; |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 378 | // TODO: This will be removed when GrSurfaceContexts are aware of their color types. |
| 379 | // (skbug.com/6718) |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 380 | static bool valid_premul_config(GrPixelConfig config) { |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 381 | switch (config) { |
| 382 | case kUnknown_GrPixelConfig: return false; |
| 383 | case kAlpha_8_GrPixelConfig: return false; |
| 384 | case kGray_8_GrPixelConfig: return false; |
| 385 | case kRGB_565_GrPixelConfig: return false; |
| 386 | case kRGBA_4444_GrPixelConfig: return true; |
| 387 | case kRGBA_8888_GrPixelConfig: return true; |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 388 | case kRGB_888_GrPixelConfig: return false; |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 389 | case kBGRA_8888_GrPixelConfig: return true; |
| 390 | case kSRGBA_8888_GrPixelConfig: return true; |
| 391 | case kSBGRA_8888_GrPixelConfig: return true; |
Brian Osman | 10fc6fd | 2018-03-02 11:01:10 -0500 | [diff] [blame] | 392 | case kRGBA_1010102_GrPixelConfig: return true; |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 393 | case kRGBA_float_GrPixelConfig: return true; |
| 394 | case kRG_float_GrPixelConfig: return false; |
| 395 | case kAlpha_half_GrPixelConfig: return false; |
| 396 | case kRGBA_half_GrPixelConfig: return true; |
| 397 | case kAlpha_8_as_Alpha_GrPixelConfig: return false; |
| 398 | case kAlpha_8_as_Red_GrPixelConfig: return false; |
| 399 | case kAlpha_half_as_Red_GrPixelConfig: return false; |
| 400 | case kGray_8_as_Lum_GrPixelConfig: return false; |
| 401 | case kGray_8_as_Red_GrPixelConfig: return false; |
| 402 | } |
| 403 | SK_ABORT("Invalid GrPixelConfig"); |
| 404 | return false; |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 405 | } |
| 406 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 407 | static bool valid_premul_color_type(GrColorType ct) { |
| 408 | switch (ct) { |
Brian Osman | 10fc6fd | 2018-03-02 11:01:10 -0500 | [diff] [blame] | 409 | case GrColorType::kUnknown: return false; |
| 410 | case GrColorType::kAlpha_8: return false; |
| 411 | case GrColorType::kRGB_565: return false; |
| 412 | case GrColorType::kABGR_4444: return true; |
| 413 | case GrColorType::kRGBA_8888: return true; |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 414 | case GrColorType::kRGB_888x: return false; |
Brian Osman | 10fc6fd | 2018-03-02 11:01:10 -0500 | [diff] [blame] | 415 | case GrColorType::kBGRA_8888: return true; |
| 416 | case GrColorType::kRGBA_1010102: return true; |
| 417 | case GrColorType::kGray_8: return false; |
| 418 | case GrColorType::kAlpha_F16: return false; |
| 419 | case GrColorType::kRGBA_F16: return true; |
| 420 | case GrColorType::kRG_F32: return false; |
| 421 | case GrColorType::kRGBA_F32: return true; |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 422 | } |
| 423 | SK_ABORT("Invalid GrColorType"); |
| 424 | return false; |
| 425 | } |
| 426 | |
| 427 | static bool valid_pixel_conversion(GrColorType cpuColorType, GrPixelConfig gpuConfig, |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 428 | bool premulConversion) { |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 429 | // We only allow premul <-> unpremul conversions for some formats |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 430 | if (premulConversion && |
| 431 | (!valid_premul_color_type(cpuColorType) || !valid_premul_config(gpuConfig))) { |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 432 | return false; |
| 433 | } |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 434 | return true; |
| 435 | } |
| 436 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 437 | static bool pm_upm_must_round_trip(GrColorType cpuColorType, const SkColorSpace* cpuColorSpace) { |
| 438 | return !cpuColorSpace && |
| 439 | (GrColorType::kRGBA_8888 == cpuColorType || GrColorType::kBGRA_8888 == cpuColorType); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 440 | } |
| 441 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 442 | // TODO: This will be removed when GrSurfaceContexts are aware of their color types. |
| 443 | // (skbug.com/6718) |
| 444 | static bool pm_upm_must_round_trip(GrPixelConfig surfaceConfig, |
| 445 | const SkColorSpace* surfaceColorSpace) { |
| 446 | return !surfaceColorSpace && |
| 447 | (kRGBA_8888_GrPixelConfig == surfaceConfig || kBGRA_8888_GrPixelConfig == surfaceConfig); |
| 448 | } |
| 449 | |
| 450 | static GrSRGBConversion determine_write_pixels_srgb_conversion(GrColorType srcColorType, |
| 451 | const SkColorSpace* srcColorSpace, |
| 452 | GrSRGBEncoded dstSRGBEncoded, |
| 453 | const SkColorSpace* dstColorSpace, |
| 454 | const GrCaps& caps) { |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 455 | // No support for sRGB-encoded alpha. |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 456 | if (GrColorTypeIsAlphaOnly(srcColorType)) { |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 457 | return GrSRGBConversion::kNone; |
| 458 | } |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 459 | // No conversions without GPU support for sRGB. (Legacy mode) |
| 460 | if (!caps.srgbSupport()) { |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 461 | return GrSRGBConversion::kNone; |
| 462 | } |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 463 | // If the GrSurfaceContext has no color space then it is in legacy mode. |
| 464 | if (!dstColorSpace) { |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 465 | return GrSRGBConversion::kNone; |
| 466 | } |
| 467 | |
| 468 | bool srcColorSpaceIsSRGB = srcColorSpace && srcColorSpace->gammaCloseToSRGB(); |
| 469 | bool dstColorSpaceIsSRGB = dstColorSpace->gammaCloseToSRGB(); |
| 470 | |
| 471 | // For now we are assuming that if color space of the dst does not have sRGB gamma then the |
| 472 | // texture format is not sRGB encoded and vice versa. Note that we already checked for "legacy" |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 473 | // mode being forced on by caps above. This may change in the future. We will then have to |
| 474 | // perform shader based conversions. |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 475 | SkASSERT(dstColorSpaceIsSRGB == (GrSRGBEncoded::kYes == dstSRGBEncoded)); |
| 476 | |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 477 | if (srcColorSpaceIsSRGB == dstColorSpaceIsSRGB) { |
| 478 | return GrSRGBConversion::kNone; |
| 479 | } |
| 480 | return srcColorSpaceIsSRGB ? GrSRGBConversion::kSRGBToLinear : GrSRGBConversion::kLinearToSRGB; |
| 481 | } |
| 482 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 483 | static GrSRGBConversion determine_read_pixels_srgb_conversion(GrSRGBEncoded srcSRGBEncoded, |
| 484 | const SkColorSpace* srcColorSpace, |
| 485 | GrColorType dstColorType, |
| 486 | const SkColorSpace* dstColorSpace, |
| 487 | const GrCaps& caps) { |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 488 | // This is symmetrical with the write version. |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 489 | switch (determine_write_pixels_srgb_conversion(dstColorType, dstColorSpace, srcSRGBEncoded, |
| 490 | srcColorSpace, caps)) { |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 491 | case GrSRGBConversion::kNone: return GrSRGBConversion::kNone; |
| 492 | case GrSRGBConversion::kLinearToSRGB: return GrSRGBConversion::kSRGBToLinear; |
| 493 | case GrSRGBConversion::kSRGBToLinear: return GrSRGBConversion::kLinearToSRGB; |
| 494 | } |
| 495 | return GrSRGBConversion::kNone; |
| 496 | } |
| 497 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 498 | bool GrContextPriv::writeSurfacePixels(GrSurfaceContext* dst, int left, int top, int width, |
| 499 | int height, GrColorType srcColorType, |
| 500 | SkColorSpace* srcColorSpace, const void* buffer, |
| 501 | size_t rowBytes, uint32_t pixelOpsFlags) { |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 502 | bool useLegacyPath = false; |
| 503 | #ifdef SK_LEGACY_GPU_PIXEL_OPS |
| 504 | useLegacyPath = true; |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 505 | #endif |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 506 | // Newly added color types/configs are only supported by the new code path. |
| 507 | if (srcColorType == GrColorType::kRGB_888x || |
| 508 | dst->asSurfaceProxy()->config() == kRGB_888_GrPixelConfig) { |
| 509 | useLegacyPath = false; |
| 510 | } |
| 511 | |
| 512 | if (!useLegacyPath) { |
| 513 | return this->writeSurfacePixels2(dst, left, top, width, height, srcColorType, srcColorSpace, |
| 514 | buffer, rowBytes, pixelOpsFlags); |
| 515 | } |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 516 | |
Brian Osman | b62ea22 | 2016-12-22 11:12:16 -0500 | [diff] [blame] | 517 | // TODO: Color space conversion |
| 518 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 519 | ASSERT_SINGLE_OWNER_PRIV |
| 520 | RETURN_FALSE_IF_ABANDONED_PRIV |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 521 | SkASSERT(dst); |
| 522 | ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy()); |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 523 | GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels", fContext); |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 524 | |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 525 | if (!dst->asSurfaceProxy()->instantiate(this->resourceProvider())) { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 526 | return false; |
| 527 | } |
| 528 | |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 529 | GrSurfaceProxy* dstProxy = dst->asSurfaceProxy(); |
| 530 | GrSurface* dstSurface = dstProxy->priv().peekSurface(); |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 531 | |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 532 | // 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] | 533 | const bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 534 | |
| 535 | if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) { |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 536 | return false; |
| 537 | } |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 538 | // There is no way to store alpha values in the dst. |
| 539 | if (GrColorTypeHasAlpha(srcColorType) && GrPixelConfigIsOpaque(dstProxy->config())) { |
| 540 | return false; |
| 541 | } |
| 542 | // The source has no alpha value and the dst is only alpha |
| 543 | if (!GrColorTypeHasAlpha(srcColorType) && GrPixelConfigIsAlphaOnly(dstProxy->config())) { |
| 544 | return false; |
| 545 | } |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 546 | |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 547 | // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data, |
| 548 | // without any color spaces attached, and the caller wants us to premul. |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 549 | bool useConfigConversionEffect = |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 550 | premul && pm_upm_must_round_trip(srcColorType, srcColorSpace) && |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 551 | pm_upm_must_round_trip(dstProxy->config(), dst->colorSpaceInfo().colorSpace()); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 552 | |
| 553 | // Are we going to try to premul as part of a draw? For the non-legacy case, we always allow |
| 554 | // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly. |
| 555 | bool premulOnGpu = premul && |
| 556 | (!useConfigConversionEffect || fContext->validPMUPMConversionExists()); |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 557 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 558 | // 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] | 559 | // necessary and because GrGpu::getWritePixelsInfo requires it. |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 560 | if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(), |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 561 | GrColorTypeBytesPerPixel(srcColorType), &left, &top, |
| 562 | &width, &height, &buffer, &rowBytes)) { |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 563 | return false; |
| 564 | } |
| 565 | |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 566 | GrGpu::DrawPreference drawPreference = premulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference |
| 567 | : GrGpu::kNoDraw_DrawPreference; |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 568 | GrGpu::WritePixelTempDrawInfo tempDrawInfo; |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 569 | GrSRGBConversion srgbConversion = determine_write_pixels_srgb_conversion( |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 570 | srcColorType, srcColorSpace, GrPixelConfigIsSRGBEncoded(dstProxy->config()), |
| 571 | dst->colorSpaceInfo().colorSpace(), *fContext->caps()); |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 572 | if (!fContext->fGpu->getWritePixelsInfo(dstSurface, dstProxy->origin(), width, height, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 573 | srcColorType, srgbConversion, &drawPreference, |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 574 | &tempDrawInfo)) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 575 | return false; |
| 576 | } |
| 577 | |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 578 | if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 579 | this->flush(nullptr); // MDB TODO: tighten this |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 582 | sk_sp<GrTextureProxy> tempProxy; |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 583 | if (GrGpu::kNoDraw_DrawPreference != drawPreference) { |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 584 | tempProxy = this->proxyProvider()->createProxy(tempDrawInfo.fTempSurfaceDesc, |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 585 | kTopLeft_GrSurfaceOrigin, |
| 586 | SkBackingFit::kApprox, SkBudgeted::kYes); |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 587 | if (!tempProxy && GrGpu::kRequireDraw_DrawPreference == drawPreference) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 588 | return false; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | // temp buffer for doing sw premul conversion, if needed. |
| 593 | SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 594 | // We need to do sw premul if we were unable to create a RT for drawing, or if we can't do the |
| 595 | // premul on the GPU |
| 596 | if (premul && (!tempProxy || !premulOnGpu)) { |
| 597 | size_t tmpRowBytes = 4 * width; |
| 598 | tmpPixels.reset(width * height); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 599 | if (!sw_convert_to_premul(srcColorType, width, height, rowBytes, buffer, tmpRowBytes, |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 600 | tmpPixels.get())) { |
| 601 | return false; |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 602 | } |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 603 | rowBytes = tmpRowBytes; |
| 604 | buffer = tmpPixels.get(); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 605 | } |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 606 | |
| 607 | if (tempProxy) { |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 608 | auto fp = GrSimpleTextureEffect::Make(tempProxy, SkMatrix::I()); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 609 | if (premulOnGpu) { |
| 610 | fp = fContext->createUPMToPMEffect(std::move(fp), useConfigConversionEffect); |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 611 | } |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 612 | fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle); |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 613 | if (!fp) { |
| 614 | return false; |
| 615 | } |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 616 | |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 617 | if (!tempProxy->instantiate(this->resourceProvider())) { |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 618 | return false; |
| 619 | } |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 620 | GrTexture* texture = tempProxy->priv().peekTexture(); |
Robert Phillips | 7bbbf62 | 2017-10-17 07:36:59 -0400 | [diff] [blame] | 621 | |
| 622 | if (tempProxy->priv().hasPendingIO()) { |
| 623 | this->flush(tempProxy.get()); |
| 624 | } |
| 625 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 626 | if (!fContext->fGpu->writePixels(texture, tempProxy->origin(), 0, 0, width, height, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 627 | tempDrawInfo.fWriteColorType, buffer, rowBytes)) { |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 628 | return false; |
| 629 | } |
Robert Phillips | 7bbbf62 | 2017-10-17 07:36:59 -0400 | [diff] [blame] | 630 | tempProxy = nullptr; |
| 631 | |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 632 | SkMatrix matrix; |
| 633 | matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top)); |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 634 | GrRenderTargetContext* renderTargetContext = dst->asRenderTargetContext(); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 635 | if (!renderTargetContext) { |
| 636 | return false; |
| 637 | } |
| 638 | GrPaint paint; |
| 639 | paint.addColorFragmentProcessor(std::move(fp)); |
| 640 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 641 | paint.setAllowSRGBInputs(dst->colorSpaceInfo().isGammaCorrect() || |
| 642 | GrPixelConfigIsSRGB(dst->colorSpaceInfo().config())); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 643 | SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)); |
| 644 | renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, matrix, rect, |
| 645 | nullptr); |
| 646 | |
| 647 | if (kFlushWrites_PixelOp & pixelOpsFlags) { |
| 648 | this->flushSurfaceWrites(renderTargetContext->asRenderTargetProxy()); |
| 649 | } |
| 650 | } else { |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 651 | return fContext->fGpu->writePixels(dstSurface, dstProxy->origin(), left, top, width, height, |
| 652 | srcColorType, buffer, rowBytes); |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 653 | } |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 654 | return true; |
| 655 | } |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 656 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 657 | bool GrContextPriv::readSurfacePixels(GrSurfaceContext* src, int left, int top, int width, |
| 658 | int height, GrColorType dstColorType, |
| 659 | SkColorSpace* dstColorSpace, void* buffer, size_t rowBytes, |
| 660 | uint32_t flags) { |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 661 | bool useLegacyPath = false; |
| 662 | #ifdef SK_LEGACY_GPU_PIXEL_OPS |
| 663 | useLegacyPath = true; |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 664 | #endif |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 665 | // Newly added color types/configs are only supported by the new code path. |
| 666 | if (dstColorType == GrColorType::kRGB_888x || |
| 667 | src->asSurfaceProxy()->config() == kRGB_888_GrPixelConfig) { |
| 668 | useLegacyPath = false; |
| 669 | } |
| 670 | |
| 671 | if (!useLegacyPath) { |
| 672 | return this->readSurfacePixels2(src, left, top, width, height, dstColorType, dstColorSpace, |
| 673 | buffer, rowBytes, flags); |
| 674 | } |
| 675 | |
Brian Osman | b62ea22 | 2016-12-22 11:12:16 -0500 | [diff] [blame] | 676 | // TODO: Color space conversion |
| 677 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 678 | ASSERT_SINGLE_OWNER_PRIV |
| 679 | RETURN_FALSE_IF_ABANDONED_PRIV |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 680 | SkASSERT(src); |
| 681 | ASSERT_OWNED_PROXY_PRIV(src->asSurfaceProxy()); |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 682 | GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "readSurfacePixels", fContext); |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 683 | SkASSERT(!(flags & kDontFlush_PixelOpsFlag)); |
| 684 | if (flags & kDontFlush_PixelOpsFlag) { |
| 685 | return false; |
| 686 | } |
bsalomon | 32ab260 | 2015-09-09 18:57:49 -0700 | [diff] [blame] | 687 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 688 | // MDB TODO: delay this instantiation until later in the method |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 689 | if (!src->asSurfaceProxy()->instantiate(this->resourceProvider())) { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 690 | return false; |
| 691 | } |
| 692 | |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 693 | GrSurfaceProxy* srcProxy = src->asSurfaceProxy(); |
| 694 | GrSurface* srcSurface = srcProxy->priv().peekSurface(); |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 695 | |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 696 | // The src is premul but the dst is unpremul -> unpremul the src after or as part of the read |
| 697 | bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 698 | |
| 699 | if (!valid_pixel_conversion(dstColorType, srcProxy->config(), unpremul)) { |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 700 | return false; |
| 701 | } |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 702 | // The source is alpha-only but the dst is not. TODO: Make non-alpha channels in the dst be 0? |
| 703 | if (GrPixelConfigIsAlphaOnly(srcProxy->config()) && !GrColorTypeIsAlphaOnly(dstColorType)) { |
| 704 | return false; |
| 705 | } |
| 706 | // The source has no alpha, the dst is alpha-only. TODO: set all values in dst to 1? |
| 707 | if (GrPixelConfigIsOpaque(srcProxy->config()) && GrColorTypeIsAlphaOnly(dstColorType)) { |
| 708 | return false; |
| 709 | } |
| 710 | // Not clear if we should unpremul in this case. |
| 711 | if (!GrPixelConfigIsOpaque(srcProxy->config()) && !GrColorTypeHasAlpha(dstColorType)) { |
| 712 | return false; |
| 713 | } |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 714 | |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 715 | // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data, |
| 716 | // without any color spaces attached, and the caller wants us to unpremul. |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 717 | bool useConfigConversionEffect = |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 718 | unpremul && |
| 719 | pm_upm_must_round_trip(srcProxy->config(), src->colorSpaceInfo().colorSpace()) && |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 720 | pm_upm_must_round_trip(dstColorType, dstColorSpace); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 721 | |
| 722 | // Are we going to try to unpremul as part of a draw? For the non-legacy case, we always allow |
| 723 | // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly. |
| 724 | bool unpremulOnGpu = unpremul && |
| 725 | (!useConfigConversionEffect || fContext->validPMUPMConversionExists()); |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 726 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 727 | // Adjust the params so that if we wind up using an intermediate surface we've already done |
| 728 | // all the trimming and the temporary can be the min size required. |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 729 | if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(), |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 730 | GrColorTypeBytesPerPixel(dstColorType), &left, &top, |
| 731 | &width, &height, &buffer, &rowBytes)) { |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 732 | return false; |
| 733 | } |
| 734 | |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 735 | GrGpu::DrawPreference drawPreference = unpremulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference |
| 736 | : GrGpu::kNoDraw_DrawPreference; |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 737 | GrGpu::ReadPixelTempDrawInfo tempDrawInfo; |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 738 | GrSRGBConversion srgbConversion = determine_read_pixels_srgb_conversion( |
| 739 | GrPixelConfigIsSRGBEncoded(srcProxy->config()), src->colorSpaceInfo().colorSpace(), |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 740 | dstColorType, dstColorSpace, *fContext->caps()); |
| 741 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 742 | if (!fContext->fGpu->getReadPixelsInfo(srcSurface, srcProxy->origin(), width, height, rowBytes, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 743 | dstColorType, srgbConversion, &drawPreference, |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 744 | &tempDrawInfo)) { |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 745 | return false; |
| 746 | } |
bsalomon | 191bcc0 | 2014-11-14 11:31:13 -0800 | [diff] [blame] | 747 | |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 748 | if (srcSurface->surfacePriv().hasPendingWrite()) { |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 749 | this->flush(nullptr); // MDB TODO: tighten this |
| 750 | } |
| 751 | |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 752 | sk_sp<GrSurfaceProxy> proxyToRead = src->asSurfaceProxyRef(); |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 753 | bool didTempDraw = false; |
| 754 | if (GrGpu::kNoDraw_DrawPreference != drawPreference) { |
bsalomon | b117ff1 | 2016-07-19 07:24:40 -0700 | [diff] [blame] | 755 | if (SkBackingFit::kExact == tempDrawInfo.fTempSurfaceFit) { |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 756 | // We only respect this when the entire src is being read. Otherwise we can trigger too |
| 757 | // many odd ball texture sizes and trash the cache. |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 758 | if (width != srcSurface->width() || height != srcSurface->height()) { |
bsalomon | b117ff1 | 2016-07-19 07:24:40 -0700 | [diff] [blame] | 759 | tempDrawInfo.fTempSurfaceFit= SkBackingFit::kApprox; |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 760 | } |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 761 | } |
brianosman | dfe4f2e | 2016-07-21 13:28:36 -0700 | [diff] [blame] | 762 | // TODO: Need to decide the semantics of this function for color spaces. Do we support |
| 763 | // conversion to a passed-in color space? For now, specifying nullptr means that this |
Brian Salomon | 366093f | 2018-02-13 09:25:22 -0500 | [diff] [blame] | 764 | // path will do no conversion, so it will match the behavior of the non-draw path. For |
| 765 | // now we simply infer an sRGB color space if the config is sRGB in order to avoid an |
| 766 | // illegal combination. |
| 767 | sk_sp<SkColorSpace> colorSpace; |
| 768 | if (GrPixelConfigIsSRGB(tempDrawInfo.fTempSurfaceDesc.fConfig)) { |
| 769 | colorSpace = SkColorSpace::MakeSRGB(); |
| 770 | } |
| 771 | sk_sp<GrRenderTargetContext> tempRTC = |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 772 | fContext->contextPriv().makeDeferredRenderTargetContext( |
| 773 | tempDrawInfo.fTempSurfaceFit, |
Brian Salomon | 366093f | 2018-02-13 09:25:22 -0500 | [diff] [blame] | 774 | tempDrawInfo.fTempSurfaceDesc.fWidth, |
| 775 | tempDrawInfo.fTempSurfaceDesc.fHeight, |
| 776 | tempDrawInfo.fTempSurfaceDesc.fConfig, |
| 777 | std::move(colorSpace), |
| 778 | tempDrawInfo.fTempSurfaceDesc.fSampleCnt, |
| 779 | GrMipMapped::kNo, |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 780 | kTopLeft_GrSurfaceOrigin); |
Brian Osman | 693a540 | 2016-10-27 15:13:22 -0400 | [diff] [blame] | 781 | if (tempRTC) { |
Greg Daniel | f44cb48 | 2018-02-27 14:26:32 -0500 | [diff] [blame] | 782 | // Adding discard to appease vulkan validation warning about loading uninitialized data |
| 783 | // on draw |
| 784 | tempRTC->discard(); |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 785 | SkMatrix textureMatrix = SkMatrix::MakeTrans(SkIntToScalar(left), SkIntToScalar(top)); |
Robert Phillips | f41c22f | 2017-04-18 07:48:58 -0400 | [diff] [blame] | 786 | sk_sp<GrTextureProxy> proxy = src->asTextureProxyRef(); |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 787 | auto fp = GrSimpleTextureEffect::Make(std::move(proxy), textureMatrix); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 788 | if (unpremulOnGpu) { |
| 789 | fp = fContext->createPMToUPMEffect(std::move(fp), useConfigConversionEffect); |
| 790 | // We no longer need to do this on CPU after the read back. |
| 791 | unpremul = false; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 792 | } |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 793 | fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle); |
Robert Phillips | 1c9686b | 2017-06-30 08:40:28 -0400 | [diff] [blame] | 794 | if (!fp) { |
| 795 | return false; |
| 796 | } |
Brian Osman | 60cd57e | 2017-04-06 10:19:06 -0400 | [diff] [blame] | 797 | |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 798 | GrPaint paint; |
| 799 | paint.addColorFragmentProcessor(std::move(fp)); |
| 800 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 801 | paint.setAllowSRGBInputs(true); |
| 802 | SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)); |
| 803 | tempRTC->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect, |
| 804 | nullptr); |
| 805 | proxyToRead = tempRTC->asTextureProxyRef(); |
| 806 | left = 0; |
| 807 | top = 0; |
| 808 | didTempDraw = true; |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 809 | } |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 810 | } |
joshualitt | 5c55fef | 2014-10-31 14:04:35 -0700 | [diff] [blame] | 811 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 812 | if (!proxyToRead) { |
| 813 | return false; |
| 814 | } |
| 815 | |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 816 | if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) { |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 817 | return false; |
| 818 | } |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 819 | GrColorType colorTypeToRead = dstColorType; |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 820 | if (didTempDraw) { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 821 | this->flushSurfaceWrites(proxyToRead.get()); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 822 | colorTypeToRead = tempDrawInfo.fReadColorType; |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 823 | } |
Robert Phillips | 09dfc47 | 2017-09-13 15:25:47 -0400 | [diff] [blame] | 824 | |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 825 | if (!proxyToRead->instantiate(this->resourceProvider())) { |
Robert Phillips | 09dfc47 | 2017-09-13 15:25:47 -0400 | [diff] [blame] | 826 | return false; |
| 827 | } |
| 828 | |
| 829 | GrSurface* surfaceToRead = proxyToRead->priv().peekSurface(); |
| 830 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 831 | if (!fContext->fGpu->readPixels(surfaceToRead, proxyToRead->origin(), left, top, width, height, |
| 832 | colorTypeToRead, buffer, rowBytes)) { |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 833 | return false; |
| 834 | } |
| 835 | |
| 836 | // Perform umpremul conversion if we weren't able to perform it as a draw. |
| 837 | if (unpremul) { |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 838 | SkColorType colorType = GrColorTypeToSkColorType(dstColorType); |
| 839 | if (kUnknown_SkColorType == colorType || 4 != SkColorTypeBytesPerPixel(colorType)) { |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 840 | return false; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 841 | } |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 842 | |
Matt Sarett | c7b2908 | 2017-02-09 16:22:39 -0500 | [diff] [blame] | 843 | for (int y = 0; y < height; y++) { |
| 844 | SkUnpremultiplyRow<false>((uint32_t*) buffer, (const uint32_t*) buffer, width); |
| 845 | buffer = SkTAddOffset<void>(buffer, rowBytes); |
| 846 | } |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 847 | } |
| 848 | return true; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 851 | bool GrContextPriv::writeSurfacePixels2(GrSurfaceContext* dst, int left, int top, int width, |
| 852 | int height, GrColorType srcColorType, |
| 853 | SkColorSpace* srcColorSpace, const void* buffer, |
| 854 | size_t rowBytes, uint32_t pixelOpsFlags) { |
| 855 | ASSERT_SINGLE_OWNER_PRIV |
| 856 | RETURN_FALSE_IF_ABANDONED_PRIV |
| 857 | SkASSERT(dst); |
| 858 | SkASSERT(buffer); |
| 859 | ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy()); |
| 860 | GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels2", fContext); |
| 861 | |
| 862 | if (GrColorType::kUnknown == srcColorType) { |
| 863 | return false; |
| 864 | } |
| 865 | |
| 866 | if (!dst->asSurfaceProxy()->instantiate(this->resourceProvider())) { |
| 867 | return false; |
| 868 | } |
| 869 | |
| 870 | GrSurfaceProxy* dstProxy = dst->asSurfaceProxy(); |
| 871 | GrSurface* dstSurface = dstProxy->priv().peekSurface(); |
| 872 | |
| 873 | if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(), |
| 874 | GrColorTypeBytesPerPixel(srcColorType), &left, &top, |
| 875 | &width, &height, &buffer, &rowBytes)) { |
| 876 | return false; |
| 877 | } |
| 878 | |
Brian Salomon | 3d86a19 | 2018-02-27 16:46:11 -0500 | [diff] [blame] | 879 | if (!fContext->caps()->surfaceSupportsWritePixels(dstSurface)) { |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 880 | // We don't expect callers that are skipping flushes to require an intermediate draw. |
| 881 | SkASSERT(!(pixelOpsFlags & kDontFlush_PixelOpsFlag)); |
| 882 | if (pixelOpsFlags & kDontFlush_PixelOpsFlag) { |
| 883 | return false; |
| 884 | } |
| 885 | |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 886 | GrSurfaceDesc desc; |
| 887 | desc.fConfig = dstProxy->config(); |
| 888 | desc.fWidth = width; |
| 889 | desc.fHeight = height; |
| 890 | desc.fSampleCnt = 1; |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 891 | auto tempProxy = this->proxyProvider()->createProxy( |
| 892 | desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kApprox, SkBudgeted::kYes); |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 893 | if (!tempProxy) { |
| 894 | return false; |
| 895 | } |
| 896 | auto tempCtx = this->drawingManager()->makeTextureContext( |
| 897 | tempProxy, dst->colorSpaceInfo().refColorSpace()); |
| 898 | if (!tempCtx) { |
| 899 | return false; |
| 900 | } |
| 901 | if (!this->writeSurfacePixels2(tempCtx.get(), 0, 0, width, height, srcColorType, |
| 902 | srcColorSpace, buffer, rowBytes, pixelOpsFlags)) { |
| 903 | return false; |
| 904 | } |
Brian Salomon | 3d86a19 | 2018-02-27 16:46:11 -0500 | [diff] [blame] | 905 | return dst->copy(tempProxy.get(), SkIRect::MakeWH(width, height), {left, top}); |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | // TODO: Make GrSurfaceContext know its alpha type and pass src buffer's alpha type. |
| 909 | bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags); |
| 910 | bool convert = premul; |
| 911 | |
| 912 | if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) { |
| 913 | return false; |
| 914 | } |
| 915 | |
| 916 | GrColorType allowedColorType = |
| 917 | fContext->caps()->supportedWritePixelsColorType(dstProxy->config(), srcColorType); |
| 918 | convert = convert || (srcColorType != allowedColorType); |
| 919 | |
| 920 | if (!dst->colorSpaceInfo().colorSpace()) { |
| 921 | // "Legacy" mode - no color space conversions. |
| 922 | srcColorSpace = nullptr; |
| 923 | } |
| 924 | convert = convert || !SkColorSpace::Equals(srcColorSpace, dst->colorSpaceInfo().colorSpace()); |
| 925 | |
| 926 | std::unique_ptr<char[]> tempBuffer; |
| 927 | if (convert) { |
| 928 | auto srcSkColorType = GrColorTypeToSkColorType(srcColorType); |
| 929 | auto dstSkColorType = GrColorTypeToSkColorType(allowedColorType); |
| 930 | if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) { |
| 931 | return false; |
| 932 | } |
| 933 | auto srcAlphaType = premul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType; |
| 934 | SkPixmap src(SkImageInfo::Make(width, height, srcSkColorType, srcAlphaType, |
| 935 | sk_ref_sp(srcColorSpace)), |
| 936 | buffer, rowBytes); |
| 937 | auto tempSrcII = SkImageInfo::Make(width, height, dstSkColorType, kPremul_SkAlphaType, |
| 938 | dst->colorSpaceInfo().refColorSpace()); |
| 939 | auto size = tempSrcII.computeMinByteSize(); |
| 940 | if (!size) { |
| 941 | return false; |
| 942 | } |
| 943 | tempBuffer.reset(new char[size]); |
| 944 | SkPixmap tempSrc(tempSrcII, tempBuffer.get(), tempSrcII.minRowBytes()); |
| 945 | if (!src.readPixels(tempSrc)) { |
| 946 | return false; |
| 947 | } |
| 948 | srcColorType = allowedColorType; |
| 949 | buffer = tempSrc.addr(); |
| 950 | rowBytes = tempSrc.rowBytes(); |
| 951 | if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) { |
| 952 | std::unique_ptr<char[]> row(new char[rowBytes]); |
| 953 | for (int y = 0; y < height / 2; ++y) { |
| 954 | memcpy(row.get(), tempSrc.addr(0, y), rowBytes); |
| 955 | memcpy(tempSrc.writable_addr(0, y), tempSrc.addr(0, height - 1 - y), rowBytes); |
| 956 | memcpy(tempSrc.writable_addr(0, height - 1 - y), row.get(), rowBytes); |
| 957 | } |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 958 | top = dstSurface->height() - top - height; |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 959 | } |
| 960 | } else if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) { |
| 961 | size_t trimRowBytes = GrColorTypeBytesPerPixel(srcColorType) * width; |
| 962 | tempBuffer.reset(new char[trimRowBytes * height]); |
| 963 | char* dst = reinterpret_cast<char*>(tempBuffer.get()) + trimRowBytes * (height - 1); |
| 964 | const char* src = reinterpret_cast<const char*>(buffer); |
| 965 | for (int i = 0; i < height; ++i, src += rowBytes, dst -= trimRowBytes) { |
| 966 | memcpy(dst, src, trimRowBytes); |
| 967 | } |
| 968 | buffer = tempBuffer.get(); |
| 969 | rowBytes = trimRowBytes; |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 970 | top = dstSurface->height() - top - height; |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) { |
| 974 | this->flush(nullptr); // MDB TODO: tighten this |
| 975 | } |
| 976 | |
| 977 | return this->getGpu()->writePixels(dstSurface, left, top, width, height, srcColorType, buffer, |
| 978 | rowBytes); |
| 979 | } |
| 980 | |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 981 | bool GrContextPriv::readSurfacePixels2(GrSurfaceContext* src, int left, int top, int width, |
| 982 | int height, GrColorType dstColorType, |
| 983 | SkColorSpace* dstColorSpace, void* buffer, size_t rowBytes, |
| 984 | uint32_t flags) { |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 985 | ASSERT_SINGLE_OWNER_PRIV |
| 986 | RETURN_FALSE_IF_ABANDONED_PRIV |
| 987 | SkASSERT(src); |
| 988 | SkASSERT(buffer); |
| 989 | ASSERT_OWNED_PROXY_PRIV(src->asSurfaceProxy()); |
| 990 | GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "readSurfacePixels2", fContext); |
| 991 | |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 992 | SkASSERT(!(flags & kDontFlush_PixelOpsFlag)); |
| 993 | if (flags & kDontFlush_PixelOpsFlag) { |
| 994 | return false; |
| 995 | } |
| 996 | |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 997 | // MDB TODO: delay this instantiation until later in the method |
| 998 | if (!src->asSurfaceProxy()->instantiate(this->resourceProvider())) { |
| 999 | return false; |
| 1000 | } |
| 1001 | |
| 1002 | GrSurfaceProxy* srcProxy = src->asSurfaceProxy(); |
| 1003 | GrSurface* srcSurface = srcProxy->priv().peekSurface(); |
| 1004 | |
| 1005 | if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(), |
| 1006 | GrColorTypeBytesPerPixel(dstColorType), &left, &top, |
| 1007 | &width, &height, &buffer, &rowBytes)) { |
| 1008 | return false; |
| 1009 | } |
| 1010 | |
| 1011 | // TODO: Make GrSurfaceContext know its alpha type and pass dst buffer's alpha type. |
| 1012 | bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags); |
| 1013 | bool convert = unpremul; |
| 1014 | |
| 1015 | if (!valid_pixel_conversion(dstColorType, srcProxy->config(), unpremul)) { |
| 1016 | return false; |
| 1017 | } |
| 1018 | |
| 1019 | if (!fContext->caps()->surfaceSupportsReadPixels(srcSurface)) { |
| 1020 | GrSurfaceDesc desc; |
| 1021 | desc.fConfig = srcProxy->config(); |
| 1022 | desc.fWidth = width; |
| 1023 | desc.fHeight = height; |
| 1024 | desc.fSampleCnt = 1; |
| 1025 | auto tempProxy = this->proxyProvider()->createProxy( |
| 1026 | desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kApprox, SkBudgeted::kYes); |
| 1027 | if (!tempProxy) { |
| 1028 | return false; |
| 1029 | } |
| 1030 | auto tempCtx = this->drawingManager()->makeTextureContext( |
| 1031 | tempProxy, src->colorSpaceInfo().refColorSpace()); |
| 1032 | if (!tempCtx) { |
| 1033 | return false; |
| 1034 | } |
| 1035 | if (!tempCtx->copy(srcProxy, {left, top, width, height}, {0, 0})) { |
| 1036 | return false; |
| 1037 | } |
| 1038 | return this->readSurfacePixels2(tempCtx.get(), 0, 0, width, height, dstColorType, |
| 1039 | dstColorSpace, buffer, rowBytes, flags); |
| 1040 | } |
| 1041 | |
| 1042 | bool flip = srcProxy->origin() == kBottomLeft_GrSurfaceOrigin; |
| 1043 | if (flip) { |
| 1044 | top = srcSurface->height() - top - height; |
| 1045 | } |
| 1046 | |
| 1047 | GrColorType allowedColorType = |
| 1048 | fContext->caps()->supportedReadPixelsColorType(srcProxy->config(), dstColorType); |
| 1049 | convert = convert || (dstColorType != allowedColorType); |
| 1050 | |
| 1051 | if (!src->colorSpaceInfo().colorSpace()) { |
| 1052 | // "Legacy" mode - no color space conversions. |
| 1053 | dstColorSpace = nullptr; |
| 1054 | } |
| 1055 | convert = convert || !SkColorSpace::Equals(dstColorSpace, src->colorSpaceInfo().colorSpace()); |
| 1056 | |
| 1057 | SkAutoPixmapStorage tempPixmap; |
| 1058 | SkPixmap finalPixmap; |
| 1059 | if (convert) { |
| 1060 | SkColorType srcSkColorType = GrColorTypeToSkColorType(allowedColorType); |
| 1061 | SkColorType dstSkColorType = GrColorTypeToSkColorType(dstColorType); |
| 1062 | if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) { |
| 1063 | return false; |
| 1064 | } |
| 1065 | auto tempAT = SkColorTypeIsAlwaysOpaque(srcSkColorType) ? kOpaque_SkAlphaType |
| 1066 | : kPremul_SkAlphaType; |
| 1067 | auto tempII = SkImageInfo::Make(width, height, srcSkColorType, tempAT, |
| 1068 | src->colorSpaceInfo().refColorSpace()); |
| 1069 | SkASSERT(!unpremul || !SkColorTypeIsAlwaysOpaque(dstSkColorType)); |
| 1070 | auto finalAT = SkColorTypeIsAlwaysOpaque(srcSkColorType) |
| 1071 | ? kOpaque_SkAlphaType |
| 1072 | : unpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType; |
| 1073 | auto finalII = |
| 1074 | SkImageInfo::Make(width, height, dstSkColorType, finalAT, sk_ref_sp(dstColorSpace)); |
| 1075 | if (!SkImageInfoValidConversion(finalII, tempII)) { |
| 1076 | return false; |
| 1077 | } |
| 1078 | tempPixmap.alloc(tempII); |
| 1079 | finalPixmap.reset(finalII, buffer, rowBytes); |
| 1080 | buffer = tempPixmap.writable_addr(); |
| 1081 | rowBytes = tempPixmap.rowBytes(); |
| 1082 | } |
| 1083 | |
| 1084 | if (srcSurface->surfacePriv().hasPendingWrite()) { |
| 1085 | this->flush(nullptr); // MDB TODO: tighten this |
| 1086 | } |
| 1087 | |
| 1088 | if (!fContext->fGpu->readPixels(srcSurface, left, top, width, height, allowedColorType, buffer, |
| 1089 | rowBytes)) { |
| 1090 | return false; |
| 1091 | } |
| 1092 | |
| 1093 | if (flip) { |
| 1094 | size_t trimRowBytes = GrColorTypeBytesPerPixel(allowedColorType) * width; |
| 1095 | std::unique_ptr<char[]> row(new char[trimRowBytes]); |
| 1096 | char* upper = reinterpret_cast<char*>(buffer); |
| 1097 | char* lower = reinterpret_cast<char*>(buffer) + (height - 1) * rowBytes; |
| 1098 | for (int y = 0; y < height / 2; ++y, upper += rowBytes, lower -= rowBytes) { |
| 1099 | memcpy(row.get(), upper, trimRowBytes); |
| 1100 | memcpy(upper, lower, trimRowBytes); |
| 1101 | memcpy(lower, row.get(), trimRowBytes); |
| 1102 | } |
| 1103 | } |
| 1104 | if (convert) { |
| 1105 | if (!tempPixmap.readPixels(finalPixmap)) { |
| 1106 | return false; |
| 1107 | } |
| 1108 | } |
| 1109 | return true; |
| 1110 | } |
| 1111 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 1112 | void GrContextPriv::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) { |
| 1113 | ASSERT_SINGLE_OWNER_PRIV |
| 1114 | RETURN_IF_ABANDONED_PRIV |
| 1115 | SkASSERT(proxy); |
| 1116 | ASSERT_OWNED_PROXY_PRIV(proxy); |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 1117 | fContext->fDrawingManager->prepareSurfaceForExternalIO(proxy, 0, nullptr); |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 1120 | void GrContextPriv::flushSurfaceWrites(GrSurfaceProxy* proxy) { |
| 1121 | ASSERT_SINGLE_OWNER_PRIV |
| 1122 | RETURN_IF_ABANDONED_PRIV |
| 1123 | SkASSERT(proxy); |
| 1124 | ASSERT_OWNED_PROXY_PRIV(proxy); |
| 1125 | if (proxy->priv().hasPendingWrite()) { |
| 1126 | this->flush(proxy); |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 1127 | } |
| 1128 | } |
| 1129 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 1130 | void GrContextPriv::flushSurfaceIO(GrSurfaceProxy* proxy) { |
| 1131 | ASSERT_SINGLE_OWNER_PRIV |
| 1132 | RETURN_IF_ABANDONED_PRIV |
| 1133 | SkASSERT(proxy); |
| 1134 | ASSERT_OWNED_PROXY_PRIV(proxy); |
| 1135 | if (proxy->priv().hasPendingIO()) { |
| 1136 | this->flush(proxy); |
ajuma | 95243eb | 2016-08-24 08:19:02 -0700 | [diff] [blame] | 1137 | } |
| 1138 | } |
| 1139 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1140 | //////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | b471a32 | 2014-03-10 07:40:03 +0000 | [diff] [blame] | 1141 | |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 1142 | sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy, |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 1143 | sk_sp<SkColorSpace> colorSpace, |
| 1144 | const SkSurfaceProps* props) { |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 1145 | ASSERT_SINGLE_OWNER_PRIV |
| 1146 | |
Brian Salomon | 366093f | 2018-02-13 09:25:22 -0500 | [diff] [blame] | 1147 | // sRGB pixel configs may only be used with near-sRGB gamma color spaces. |
| 1148 | if (GrPixelConfigIsSRGB(proxy->config())) { |
| 1149 | if (!colorSpace || !colorSpace->gammaCloseToSRGB()) { |
| 1150 | return nullptr; |
| 1151 | } |
| 1152 | } |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 1153 | if (proxy->asRenderTargetProxy()) { |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 1154 | return this->drawingManager()->makeRenderTargetContext(std::move(proxy), |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 1155 | std::move(colorSpace), props); |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 1156 | } else { |
| 1157 | SkASSERT(proxy->asTextureProxy()); |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 1158 | SkASSERT(!props); |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 1159 | return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace)); |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 1160 | } |
| 1161 | } |
| 1162 | |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 1163 | sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrSurfaceDesc& dstDesc, |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 1164 | GrSurfaceOrigin origin, |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 1165 | GrMipMapped mipMapped, |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 1166 | SkBackingFit fit, |
Brian Salomon | 366093f | 2018-02-13 09:25:22 -0500 | [diff] [blame] | 1167 | SkBudgeted isDstBudgeted, |
| 1168 | sk_sp<SkColorSpace> colorSpace, |
| 1169 | const SkSurfaceProps* props) { |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 1170 | sk_sp<GrTextureProxy> proxy; |
| 1171 | if (GrMipMapped::kNo == mipMapped) { |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 1172 | proxy = this->proxyProvider()->createProxy(dstDesc, origin, fit, isDstBudgeted); |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 1173 | } else { |
| 1174 | SkASSERT(SkBackingFit::kExact == fit); |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 1175 | proxy = this->proxyProvider()->createMipMapProxy(dstDesc, origin, isDstBudgeted); |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 1176 | } |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 1177 | if (!proxy) { |
| 1178 | return nullptr; |
| 1179 | } |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 1180 | |
Greg Daniel | 55fa647 | 2018-03-16 16:13:10 -0400 | [diff] [blame] | 1181 | sk_sp<GrSurfaceContext> sContext = this->makeWrappedSurfaceContext(std::move(proxy), |
| 1182 | std::move(colorSpace), |
| 1183 | props); |
| 1184 | if (sContext && sContext->asRenderTargetContext()) { |
| 1185 | sContext->asRenderTargetContext()->discard(); |
| 1186 | } |
| 1187 | |
| 1188 | return sContext; |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 1189 | } |
| 1190 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 1191 | sk_sp<GrTextureContext> GrContextPriv::makeBackendTextureContext(const GrBackendTexture& tex, |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 1192 | GrSurfaceOrigin origin, |
Brian Osman | c1e3705 | 2017-03-09 14:19:20 -0500 | [diff] [blame] | 1193 | sk_sp<SkColorSpace> colorSpace) { |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 1194 | ASSERT_SINGLE_OWNER_PRIV |
| 1195 | |
Brian Salomon | 7578f3e | 2018-03-07 14:39:54 -0500 | [diff] [blame] | 1196 | sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->wrapBackendTexture(tex, origin); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 1197 | if (!proxy) { |
| 1198 | return nullptr; |
| 1199 | } |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 1200 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 1201 | return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace)); |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 1202 | } |
| 1203 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1204 | sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext( |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 1205 | const GrBackendTexture& tex, |
| 1206 | GrSurfaceOrigin origin, |
| 1207 | int sampleCnt, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1208 | sk_sp<SkColorSpace> colorSpace, |
Brian Osman | c1e3705 | 2017-03-09 14:19:20 -0500 | [diff] [blame] | 1209 | const SkSurfaceProps* props) { |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 1210 | ASSERT_SINGLE_OWNER_PRIV |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1211 | SkASSERT(sampleCnt > 0); |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 1212 | |
Brian Salomon | 7578f3e | 2018-03-07 14:39:54 -0500 | [diff] [blame] | 1213 | sk_sp<GrTextureProxy> proxy( |
| 1214 | this->proxyProvider()->wrapRenderableBackendTexture(tex, origin, sampleCnt)); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 1215 | if (!proxy) { |
| 1216 | return nullptr; |
| 1217 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1218 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 1219 | return this->drawingManager()->makeRenderTargetContext(std::move(proxy), |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1220 | std::move(colorSpace), props); |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 1221 | } |
| 1222 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1223 | sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext( |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 1224 | const GrBackendRenderTarget& backendRT, |
| 1225 | GrSurfaceOrigin origin, |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 1226 | sk_sp<SkColorSpace> colorSpace, |
| 1227 | const SkSurfaceProps* surfaceProps) { |
| 1228 | ASSERT_SINGLE_OWNER_PRIV |
| 1229 | |
Brian Salomon | 7578f3e | 2018-03-07 14:39:54 -0500 | [diff] [blame] | 1230 | sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->wrapBackendRenderTarget(backendRT, origin); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 1231 | if (!proxy) { |
| 1232 | return nullptr; |
| 1233 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1234 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 1235 | return this->drawingManager()->makeRenderTargetContext(std::move(proxy), |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1236 | std::move(colorSpace), |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1237 | surfaceProps); |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 1238 | } |
| 1239 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1240 | sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext( |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 1241 | const GrBackendTexture& tex, |
| 1242 | GrSurfaceOrigin origin, |
| 1243 | int sampleCnt, |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 1244 | sk_sp<SkColorSpace> colorSpace, |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 1245 | const SkSurfaceProps* props) { |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 1246 | ASSERT_SINGLE_OWNER_PRIV |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1247 | SkASSERT(sampleCnt > 0); |
Brian Salomon | 7578f3e | 2018-03-07 14:39:54 -0500 | [diff] [blame] | 1248 | sk_sp<GrSurfaceProxy> proxy( |
| 1249 | this->proxyProvider()->wrapBackendTextureAsRenderTarget(tex, origin, sampleCnt)); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 1250 | if (!proxy) { |
| 1251 | return nullptr; |
| 1252 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1253 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 1254 | return this->drawingManager()->makeRenderTargetContext(std::move(proxy), |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1255 | std::move(colorSpace), |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 1256 | props); |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 1257 | } |
| 1258 | |
Chris Dalton | fe199b7 | 2017-05-05 11:26:15 -0400 | [diff] [blame] | 1259 | void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) { |
| 1260 | fContext->fDrawingManager->addOnFlushCallbackObject(onFlushCBObject); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 1261 | } |
| 1262 | |
Robert Phillips | 6200036 | 2018-02-01 09:10:04 -0500 | [diff] [blame] | 1263 | void GrContextPriv::moveOpListsToDDL(SkDeferredDisplayList* ddl) { |
| 1264 | fContext->fDrawingManager->moveOpListsToDDL(ddl); |
| 1265 | } |
| 1266 | |
| 1267 | void GrContextPriv::copyOpListsFromDDL(const SkDeferredDisplayList* ddl, |
| 1268 | GrRenderTargetProxy* newDest) { |
| 1269 | fContext->fDrawingManager->copyOpListsFromDDL(ddl, newDest); |
| 1270 | } |
| 1271 | |
robertphillips | 48fde9c | 2016-09-06 05:20:20 -0700 | [diff] [blame] | 1272 | static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) { |
Brian Osman | 78f20e0 | 2017-01-12 10:28:01 -0500 | [diff] [blame] | 1273 | switch (config) { |
| 1274 | case kAlpha_8_GrPixelConfig: |
| 1275 | case kRGB_565_GrPixelConfig: |
| 1276 | case kRGBA_4444_GrPixelConfig: |
| 1277 | case kBGRA_8888_GrPixelConfig: |
Brian Osman | 10fc6fd | 2018-03-02 11:01:10 -0500 | [diff] [blame] | 1278 | case kRGBA_1010102_GrPixelConfig: |
Brian Osman | 78f20e0 | 2017-01-12 10:28:01 -0500 | [diff] [blame] | 1279 | return kRGBA_8888_GrPixelConfig; |
| 1280 | case kSBGRA_8888_GrPixelConfig: |
| 1281 | return kSRGBA_8888_GrPixelConfig; |
| 1282 | case kAlpha_half_GrPixelConfig: |
| 1283 | return kRGBA_half_GrPixelConfig; |
| 1284 | default: |
| 1285 | return kUnknown_GrPixelConfig; |
| 1286 | } |
robertphillips | 48fde9c | 2016-09-06 05:20:20 -0700 | [diff] [blame] | 1287 | } |
| 1288 | |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 1289 | sk_sp<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContextWithFallback( |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 1290 | SkBackingFit fit, |
| 1291 | int width, int height, |
| 1292 | GrPixelConfig config, |
| 1293 | sk_sp<SkColorSpace> colorSpace, |
| 1294 | int sampleCnt, |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 1295 | GrMipMapped mipMapped, |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 1296 | GrSurfaceOrigin origin, |
| 1297 | const SkSurfaceProps* surfaceProps, |
| 1298 | SkBudgeted budgeted) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1299 | SkASSERT(sampleCnt > 0); |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 1300 | if (0 == fContext->caps()->getRenderTargetSampleCount(sampleCnt, config)) { |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 1301 | config = GrPixelConfigFallback(config); |
| 1302 | } |
| 1303 | |
| 1304 | return this->makeDeferredRenderTargetContext(fit, width, height, config, std::move(colorSpace), |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 1305 | sampleCnt, mipMapped, origin, surfaceProps, |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 1306 | budgeted); |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 1307 | } |
| 1308 | |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 1309 | sk_sp<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContext( |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1310 | SkBackingFit fit, |
| 1311 | int width, int height, |
| 1312 | GrPixelConfig config, |
| 1313 | sk_sp<SkColorSpace> colorSpace, |
| 1314 | int sampleCnt, |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 1315 | GrMipMapped mipMapped, |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1316 | GrSurfaceOrigin origin, |
| 1317 | const SkSurfaceProps* surfaceProps, |
| 1318 | SkBudgeted budgeted) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1319 | SkASSERT(sampleCnt > 0); |
Brian Salomon | 79e4d1b | 2017-07-13 11:17:11 -0400 | [diff] [blame] | 1320 | if (this->abandoned()) { |
| 1321 | return nullptr; |
| 1322 | } |
| 1323 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1324 | GrSurfaceDesc desc; |
| 1325 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1326 | desc.fWidth = width; |
| 1327 | desc.fHeight = height; |
| 1328 | desc.fConfig = config; |
| 1329 | desc.fSampleCnt = sampleCnt; |
| 1330 | |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 1331 | sk_sp<GrTextureProxy> rtp; |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 1332 | if (GrMipMapped::kNo == mipMapped) { |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 1333 | rtp = fContext->fProxyProvider->createProxy(desc, origin, fit, budgeted); |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 1334 | } else { |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 1335 | rtp = fContext->fProxyProvider->createMipMapProxy(desc, origin, budgeted); |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 1336 | } |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 1337 | if (!rtp) { |
| 1338 | return nullptr; |
| 1339 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1340 | |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 1341 | sk_sp<GrRenderTargetContext> renderTargetContext( |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 1342 | fContext->fDrawingManager->makeRenderTargetContext(std::move(rtp), |
| 1343 | std::move(colorSpace), |
| 1344 | surfaceProps)); |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 1345 | if (!renderTargetContext) { |
| 1346 | return nullptr; |
| 1347 | } |
| 1348 | |
| 1349 | renderTargetContext->discard(); |
| 1350 | |
| 1351 | return renderTargetContext; |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1352 | } |
| 1353 | |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 1354 | bool GrContextPriv::abandoned() const { |
| 1355 | ASSERT_SINGLE_OWNER_PRIV |
| 1356 | return fContext->fDrawingManager->wasAbandoned(); |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 1357 | } |
| 1358 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 1359 | std::unique_ptr<GrFragmentProcessor> GrContext::createPMToUPMEffect( |
| 1360 | std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) { |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 1361 | ASSERT_SINGLE_OWNER |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 1362 | // We have specialized effects that guarantee round-trip conversion for some formats |
| 1363 | if (useConfigConversionEffect) { |
| 1364 | // We should have already called this->validPMUPMConversionExists() in this case |
| 1365 | SkASSERT(fDidTestPMConversions); |
| 1366 | // ...and it should have succeeded |
| 1367 | SkASSERT(this->validPMUPMConversionExists()); |
| 1368 | |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1369 | return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul); |
Brian Osman | 2d2da4f | 2017-04-12 17:07:22 -0400 | [diff] [blame] | 1370 | } else { |
| 1371 | // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and |
| 1372 | // explicitly round the results. Just do the obvious, naive thing in the shader. |
| 1373 | return GrFragmentProcessor::UnpremulOutput(std::move(fp)); |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 1374 | } |
| 1375 | } |
| 1376 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 1377 | std::unique_ptr<GrFragmentProcessor> GrContext::createUPMToPMEffect( |
| 1378 | std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1379 | ASSERT_SINGLE_OWNER |
Brian Osman | 2d2da4f | 2017-04-12 17:07:22 -0400 | [diff] [blame] | 1380 | // We have specialized effects that guarantee round-trip conversion for these formats |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 1381 | if (useConfigConversionEffect) { |
| 1382 | // We should have already called this->validPMUPMConversionExists() in this case |
| 1383 | SkASSERT(fDidTestPMConversions); |
| 1384 | // ...and it should have succeeded |
| 1385 | SkASSERT(this->validPMUPMConversionExists()); |
| 1386 | |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 1387 | return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul); |
Brian Osman | 2d2da4f | 2017-04-12 17:07:22 -0400 | [diff] [blame] | 1388 | } else { |
| 1389 | // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and |
| 1390 | // explicitly round the results. Just do the obvious, naive thing in the shader. |
| 1391 | return GrFragmentProcessor::PremulOutput(std::move(fp)); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1392 | } |
| 1393 | } |
| 1394 | |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 1395 | bool GrContext::validPMUPMConversionExists() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1396 | ASSERT_SINGLE_OWNER |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 1397 | if (!fDidTestPMConversions) { |
Brian Osman | 28804f3 | 2017-04-20 10:24:36 -0400 | [diff] [blame] | 1398 | fPMUPMConversionsRoundTrip = GrConfigConversionEffect::TestForPreservingPMConversions(this); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 1399 | fDidTestPMConversions = true; |
| 1400 | } |
| 1401 | |
bsalomon | 636e802 | 2015-07-29 06:08:46 -0700 | [diff] [blame] | 1402 | // 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] | 1403 | return fPMUPMConversionsRoundTrip; |
bsalomon | 636e802 | 2015-07-29 06:08:46 -0700 | [diff] [blame] | 1404 | } |
| 1405 | |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 1406 | ////////////////////////////////////////////////////////////////////////////// |
| 1407 | |
Robert Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 1408 | // DDL TODO: remove 'maxResources' |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 1409 | void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1410 | ASSERT_SINGLE_OWNER |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 1411 | if (maxResources) { |
| 1412 | *maxResources = fResourceCache->getMaxResourceCount(); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 1413 | } |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 1414 | if (maxResourceBytes) { |
| 1415 | *maxResourceBytes = fResourceCache->getMaxResourceBytes(); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 1416 | } |
| 1417 | } |
| 1418 | |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 1419 | void GrContext::setResourceCacheLimits(int maxResources, size_t maxResourceBytes) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1420 | ASSERT_SINGLE_OWNER |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 1421 | fResourceCache->setLimits(maxResources, maxResourceBytes); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 1422 | } |
| 1423 | |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 1424 | ////////////////////////////////////////////////////////////////////////////// |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 1425 | void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1426 | ASSERT_SINGLE_OWNER |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 1427 | fResourceCache->dumpMemoryStatistics(traceMemoryDump); |
| 1428 | } |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 1429 | |
| 1430 | ////////////////////////////////////////////////////////////////////////////// |
| 1431 | |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 1432 | SkString GrContextPriv::dump() const { |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 1433 | SkDynamicMemoryWStream stream; |
| 1434 | SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty); |
| 1435 | writer.beginObject(); |
| 1436 | |
| 1437 | static const char* kBackendStr[] = { |
| 1438 | "Metal", |
| 1439 | "OpenGL", |
| 1440 | "Vulkan", |
| 1441 | "Mock", |
| 1442 | }; |
| 1443 | GR_STATIC_ASSERT(0 == kMetal_GrBackend); |
| 1444 | GR_STATIC_ASSERT(1 == kOpenGL_GrBackend); |
| 1445 | GR_STATIC_ASSERT(2 == kVulkan_GrBackend); |
| 1446 | GR_STATIC_ASSERT(3 == kMock_GrBackend); |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 1447 | writer.appendString("backend", kBackendStr[fContext->fBackend]); |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 1448 | |
| 1449 | writer.appendName("caps"); |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 1450 | fContext->fCaps->dumpJSON(&writer); |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 1451 | |
| 1452 | writer.appendName("gpu"); |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 1453 | fContext->fGpu->dumpJSON(&writer); |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 1454 | |
| 1455 | // Flush JSON to the memory stream |
| 1456 | writer.endObject(); |
| 1457 | writer.flush(); |
| 1458 | |
| 1459 | // Null terminate the JSON data in the memory stream |
| 1460 | stream.write8(0); |
| 1461 | |
| 1462 | // Allocate a string big enough to hold all the data, then copy out of the stream |
| 1463 | SkString result(stream.bytesWritten()); |
| 1464 | stream.copyToAndReset(result.writable_str()); |
| 1465 | return result; |
| 1466 | } |