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 | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 15 | #include "GrMemoryPool.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 16 | #include "GrProxyProvider.h" |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 17 | #include "GrRenderTargetContext.h" |
Brian Salomon | c65aec9 | 2017-03-09 09:03:58 -0500 | [diff] [blame] | 18 | #include "GrRenderTargetProxy.h" |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 19 | #include "GrResourceCache.h" |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 20 | #include "GrResourceProvider.h" |
Greg Daniel | d85f97d | 2017-03-07 13:37:21 -0500 | [diff] [blame] | 21 | #include "GrSemaphore.h" |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 22 | #include "GrSoftwarePathRenderer.h" |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 23 | #include "GrSurfaceContext.h" |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 24 | #include "GrSurfacePriv.h" |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 25 | #include "GrSurfaceProxyPriv.h" |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 26 | #include "GrTexture.h" |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 27 | #include "GrTextureContext.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" |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 30 | #include "SkDeferredDisplayList.h" |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 31 | #include "SkGr.h" |
Mike Reed | 7fcfb62 | 2018-02-09 13:26:46 -0500 | [diff] [blame] | 32 | #include "SkImageInfoPriv.h" |
Brian Osman | 5127998 | 2017-08-23 10:12:00 -0400 | [diff] [blame] | 33 | #include "SkMakeUnique.h" |
Robert Phillips | 6b6fcc7 | 2018-03-30 13:57:00 -0400 | [diff] [blame] | 34 | #include "SkSurface_Gpu.h" |
Brian Osman | 5127998 | 2017-08-23 10:12:00 -0400 | [diff] [blame] | 35 | #include "SkTaskGroup.h" |
Khushal | 71652e2 | 2018-10-29 13:05:36 -0700 | [diff] [blame] | 36 | #include "SkTraceMemoryDump.h" |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 37 | #include "effects/GrConfigConversionEffect.h" |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 38 | #include "effects/GrSkSLFP.h" |
Chris Dalton | 6c3879d | 2018-11-01 11:13:19 -0600 | [diff] [blame] | 39 | #include "ccpr/GrCoverageCountingPathRenderer.h" |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 40 | #include "text/GrTextBlobCache.h" |
Mike Klein | 0ec1c57 | 2018-12-04 11:52:51 -0500 | [diff] [blame] | 41 | #include <atomic> |
| 42 | #include <unordered_map> |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 43 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 44 | #define ASSERT_OWNED_PROXY(P) \ |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 45 | SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == this) |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 46 | #define ASSERT_OWNED_PROXY_PRIV(P) \ |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 47 | SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == fContext) |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 48 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 49 | #define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this) |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 50 | #define ASSERT_SINGLE_OWNER \ |
| 51 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fSingleOwner);) |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 52 | #define ASSERT_SINGLE_OWNER_PRIV \ |
| 53 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fContext->fSingleOwner);) |
robertphillips | 7761d61 | 2016-05-16 09:14:53 -0700 | [diff] [blame] | 54 | #define RETURN_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return; } |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 55 | #define RETURN_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return; } |
robertphillips | 7761d61 | 2016-05-16 09:14:53 -0700 | [diff] [blame] | 56 | #define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return false; } |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 57 | #define RETURN_FALSE_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return false; } |
robertphillips | 7761d61 | 2016-05-16 09:14:53 -0700 | [diff] [blame] | 58 | #define RETURN_NULL_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return nullptr; } |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 59 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 60 | //////////////////////////////////////////////////////////////////////////////// |
| 61 | |
joshualitt | 0acd0d3 | 2015-05-07 08:23:19 -0700 | [diff] [blame] | 62 | static int32_t next_id() { |
Mike Klein | 0ec1c57 | 2018-12-04 11:52:51 -0500 | [diff] [blame] | 63 | static std::atomic<int32_t> nextID{1}; |
joshualitt | 0acd0d3 | 2015-05-07 08:23:19 -0700 | [diff] [blame] | 64 | int32_t id; |
| 65 | do { |
Mike Klein | 0ec1c57 | 2018-12-04 11:52:51 -0500 | [diff] [blame] | 66 | id = nextID++; |
joshualitt | 0acd0d3 | 2015-05-07 08:23:19 -0700 | [diff] [blame] | 67 | } while (id == SK_InvalidGenID); |
| 68 | return id; |
| 69 | } |
| 70 | |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 71 | GrContext::GrContext(GrBackendApi backend, int32_t id) |
Robert Phillips | fde6fa0 | 2018-03-02 08:53:14 -0500 | [diff] [blame] | 72 | : fBackend(backend) |
| 73 | , fUniqueID(SK_InvalidGenID == id ? next_id() : id) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 74 | fResourceCache = nullptr; |
| 75 | fResourceProvider = nullptr; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 76 | fProxyProvider = nullptr; |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 77 | fGlyphCache = nullptr; |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Robert Phillips | fde6fa0 | 2018-03-02 08:53:14 -0500 | [diff] [blame] | 80 | bool GrContext::initCommon(const GrContextOptions& options) { |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 81 | ASSERT_SINGLE_OWNER |
Robert Phillips | fde6fa0 | 2018-03-02 08:53:14 -0500 | [diff] [blame] | 82 | SkASSERT(fCaps); // needs to have been initialized by derived classes |
| 83 | SkASSERT(fThreadSafeProxy); // needs to have been initialized by derived classes |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 84 | |
| 85 | if (fGpu) { |
| 86 | fCaps = fGpu->refCaps(); |
Brian Salomon | 8f8995a | 2018-10-15 14:32:15 -0400 | [diff] [blame] | 87 | fResourceCache = new GrResourceCache(fCaps.get(), &fSingleOwner, fUniqueID); |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 88 | fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, &fSingleOwner, |
| 89 | options.fExplicitlyAllocateGPUResources); |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 90 | fProxyProvider = |
| 91 | new GrProxyProvider(fResourceProvider, fResourceCache, fCaps, &fSingleOwner); |
| 92 | } else { |
| 93 | fProxyProvider = new GrProxyProvider(this->uniqueID(), fCaps, &fSingleOwner); |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 94 | } |
| 95 | |
Robert Phillips | 88260b5 | 2018-01-19 12:56:09 -0500 | [diff] [blame] | 96 | if (fResourceCache) { |
| 97 | fResourceCache->setProxyProvider(fProxyProvider); |
| 98 | } |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 99 | |
Brian Osman | 46da1cc | 2017-02-14 14:15:48 -0500 | [diff] [blame] | 100 | fDisableGpuYUVConversion = options.fDisableGpuYUVConversion; |
Brian Osman | 8a83ca4 | 2018-02-12 14:32:17 -0500 | [diff] [blame] | 101 | fSharpenMipmappedTextures = options.fSharpenMipmappedTextures; |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 102 | fDidTestPMConversions = false; |
| 103 | |
bsalomon | 6b2552f | 2016-09-15 13:50:26 -0700 | [diff] [blame] | 104 | GrPathRendererChain::Options prcOptions; |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 105 | prcOptions.fAllowPathMaskCaching = options.fAllowPathMaskCaching; |
Brian Osman | 195c05b | 2017-08-30 15:14:04 -0400 | [diff] [blame] | 106 | #if GR_TEST_UTILS |
csmartdalton | 008b9d8 | 2017-02-22 12:00:42 -0700 | [diff] [blame] | 107 | prcOptions.fGpuPathRenderers = options.fGpuPathRenderers; |
Brian Osman | 195c05b | 2017-08-30 15:14:04 -0400 | [diff] [blame] | 108 | #endif |
Chris Dalton | ef12509 | 2018-06-26 18:16:47 -0600 | [diff] [blame] | 109 | if (options.fDisableCoverageCountingPaths) { |
| 110 | prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kCoverageCounting; |
| 111 | } |
Brian Osman | b350ae2 | 2017-08-29 16:15:39 -0400 | [diff] [blame] | 112 | if (options.fDisableDistanceFieldPaths) { |
Brian Osman | 195c05b | 2017-08-30 15:14:04 -0400 | [diff] [blame] | 113 | prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall; |
Brian Osman | b350ae2 | 2017-08-29 16:15:39 -0400 | [diff] [blame] | 114 | } |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 115 | |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 116 | if (!fResourceCache) { |
| 117 | // DDL TODO: remove this crippling of the path renderer chain |
| 118 | // Disable the small path renderer bc of the proxies in the atlas. They need to be |
| 119 | // unified when the opLists are added back to the destination drawing manager. |
| 120 | prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall; |
Robert Phillips | 5ffcb4d | 2018-07-03 16:39:40 -0400 | [diff] [blame] | 121 | prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kStencilAndCover; |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 122 | } |
| 123 | |
Herb Derby | 26cbe51 | 2018-05-24 14:39:01 -0400 | [diff] [blame] | 124 | GrTextContext::Options textContextOptions; |
| 125 | textContextOptions.fMaxDistanceFieldFontSize = options.fGlyphsAsPathsFontSize; |
| 126 | textContextOptions.fMinDistanceFieldFontSize = options.fMinDistanceFieldFontSize; |
| 127 | textContextOptions.fDistanceFieldVerticesAlwaysHaveW = false; |
Brian Salomon | b508696 | 2017-12-13 10:59:33 -0500 | [diff] [blame] | 128 | #if SK_SUPPORT_ATLAS_TEXT |
| 129 | if (GrContextOptions::Enable::kYes == options.fDistanceFieldGlyphVerticesAlwaysHaveW) { |
Herb Derby | 26cbe51 | 2018-05-24 14:39:01 -0400 | [diff] [blame] | 130 | textContextOptions.fDistanceFieldVerticesAlwaysHaveW = true; |
Brian Salomon | b508696 | 2017-12-13 10:59:33 -0500 | [diff] [blame] | 131 | } |
| 132 | #endif |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 133 | |
Robert Phillips | 64ecdce | 2018-04-02 10:26:39 -0400 | [diff] [blame] | 134 | bool explicitlyAllocatingResources = fResourceProvider |
| 135 | ? fResourceProvider->explicitlyAllocateGPUResources() |
| 136 | : false; |
Herb Derby | 26cbe51 | 2018-05-24 14:39:01 -0400 | [diff] [blame] | 137 | fDrawingManager.reset(new GrDrawingManager(this, prcOptions, textContextOptions, |
Robert Phillips | 64ecdce | 2018-04-02 10:26:39 -0400 | [diff] [blame] | 138 | &fSingleOwner, explicitlyAllocatingResources, |
Robert Phillips | 46acf9d | 2018-10-09 09:31:40 -0400 | [diff] [blame] | 139 | options.fSortRenderTargets, |
| 140 | options.fReduceOpListSplitting)); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 141 | |
Robert Phillips | af4adef | 2018-03-07 11:59:37 -0500 | [diff] [blame] | 142 | fGlyphCache = new GrGlyphCache(fCaps.get(), options.fGlyphCacheTextureMaximumBytes); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 143 | |
Robert Phillips | 303cd58 | 2018-02-14 18:54:01 -0500 | [diff] [blame] | 144 | fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB, |
Herb Derby | b12175f | 2018-05-23 16:38:09 -0400 | [diff] [blame] | 145 | this, this->uniqueID())); |
Brian Salomon | 91a3e52 | 2017-06-23 10:58:19 -0400 | [diff] [blame] | 146 | |
Robert Phillips | fde6fa0 | 2018-03-02 08:53:14 -0500 | [diff] [blame] | 147 | // DDL TODO: we need to think through how the task group & persistent cache |
| 148 | // 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] | 149 | if (options.fExecutor) { |
| 150 | fTaskGroup = skstd::make_unique<SkTaskGroup>(*options.fExecutor); |
| 151 | } |
| 152 | |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 153 | fPersistentCache = options.fPersistentCache; |
| 154 | |
Brian Salomon | 91a3e52 | 2017-06-23 10:58:19 -0400 | [diff] [blame] | 155 | return true; |
bsalomon@google.com | c0af317 | 2012-06-15 14:10:09 +0000 | [diff] [blame] | 156 | } |
| 157 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 158 | GrContext::~GrContext() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 159 | ASSERT_SINGLE_OWNER |
| 160 | |
Robert Phillips | 2e6feed | 2018-01-22 15:27:20 -0500 | [diff] [blame] | 161 | if (fDrawingManager) { |
| 162 | fDrawingManager->cleanup(); |
| 163 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 164 | delete fResourceProvider; |
| 165 | delete fResourceCache; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 166 | delete fProxyProvider; |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 167 | delete fGlyphCache; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 170 | ////////////////////////////////////////////////////////////////////////////// |
| 171 | |
| 172 | GrContextThreadSafeProxy::GrContextThreadSafeProxy(sk_sp<const GrCaps> caps, uint32_t uniqueID, |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 173 | GrBackendApi backend, |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 174 | const GrContextOptions& options, |
| 175 | sk_sp<GrSkSLFPFactoryCache> cache) |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 176 | : fCaps(std::move(caps)) |
| 177 | , fContextUniqueID(uniqueID) |
| 178 | , fBackend(backend) |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 179 | , fOptions(options) |
| 180 | , fFPFactoryCache(std::move(cache)) {} |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 181 | |
Brian Salomon | 103d6f6 | 2018-05-11 11:19:22 -0400 | [diff] [blame] | 182 | GrContextThreadSafeProxy::~GrContextThreadSafeProxy() = default; |
| 183 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 184 | sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() { |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 185 | return fThreadSafeProxy; |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Robert Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 188 | SkSurfaceCharacterization GrContextThreadSafeProxy::createCharacterization( |
| 189 | size_t cacheMaxResourceBytes, |
| 190 | const SkImageInfo& ii, const GrBackendFormat& backendFormat, |
| 191 | int sampleCnt, GrSurfaceOrigin origin, |
| 192 | const SkSurfaceProps& surfaceProps, |
Greg Daniel | a070ed7 | 2018-04-26 16:31:38 -0400 | [diff] [blame] | 193 | bool isMipMapped, bool willUseGLFBO0) { |
Robert Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 194 | if (!backendFormat.isValid()) { |
| 195 | return SkSurfaceCharacterization(); // return an invalid characterization |
| 196 | } |
| 197 | |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 198 | if (GrBackendApi::kOpenGL != backendFormat.backend() && willUseGLFBO0) { |
Greg Daniel | a070ed7 | 2018-04-26 16:31:38 -0400 | [diff] [blame] | 199 | // The willUseGLFBO0 flags can only be used for a GL backend. |
| 200 | return SkSurfaceCharacterization(); // return an invalid characterization |
| 201 | } |
| 202 | |
Robert Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 203 | if (!fCaps->mipMapSupport()) { |
| 204 | isMipMapped = false; |
| 205 | } |
| 206 | |
Brian Salomon | f391d0f | 2018-12-14 09:18:50 -0500 | [diff] [blame] | 207 | GrPixelConfig config = fCaps->getConfigFromBackendFormat(backendFormat, ii.colorType()); |
| 208 | if (config == kUnknown_GrPixelConfig) { |
Robert Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 209 | return SkSurfaceCharacterization(); // return an invalid characterization |
| 210 | } |
| 211 | |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 212 | if (!SkSurface_Gpu::Valid(fCaps.get(), config, ii.colorSpace())) { |
| 213 | return SkSurfaceCharacterization(); // return an invalid characterization |
| 214 | } |
| 215 | |
Robert Phillips | 6b6fcc7 | 2018-03-30 13:57:00 -0400 | [diff] [blame] | 216 | sampleCnt = fCaps->getRenderTargetSampleCount(sampleCnt, config); |
| 217 | if (!sampleCnt) { |
| 218 | return SkSurfaceCharacterization(); // return an invalid characterization |
| 219 | } |
| 220 | |
| 221 | GrFSAAType FSAAType = GrFSAAType::kNone; |
| 222 | if (sampleCnt > 1) { |
| 223 | FSAAType = fCaps->usesMixedSamples() ? GrFSAAType::kMixedSamples : GrFSAAType::kUnifiedMSAA; |
| 224 | } |
| 225 | |
Robert Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 226 | // This surface characterization factory assumes that the resulting characterization is |
| 227 | // textureable. |
| 228 | if (!fCaps->isConfigTexturable(config)) { |
| 229 | return SkSurfaceCharacterization(); // return an invalid characterization |
| 230 | } |
| 231 | |
| 232 | return SkSurfaceCharacterization(sk_ref_sp<GrContextThreadSafeProxy>(this), |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 233 | cacheMaxResourceBytes, ii, |
| 234 | origin, config, FSAAType, sampleCnt, |
Robert Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 235 | SkSurfaceCharacterization::Textureable(true), |
| 236 | SkSurfaceCharacterization::MipMapped(isMipMapped), |
Greg Daniel | a070ed7 | 2018-04-26 16:31:38 -0400 | [diff] [blame] | 237 | SkSurfaceCharacterization::UsesGLFBO0(willUseGLFBO0), |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 238 | SkSurfaceCharacterization::VulkanSecondaryCBCompatible(false), |
Robert Phillips | be77a02 | 2018-04-03 17:17:05 -0400 | [diff] [blame] | 239 | surfaceProps); |
Robert Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 240 | } |
| 241 | |
bsalomon | 2354f84 | 2014-07-28 13:48:36 -0700 | [diff] [blame] | 242 | void GrContext::abandonContext() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 243 | ASSERT_SINGLE_OWNER |
| 244 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 245 | fProxyProvider->abandon(); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 246 | fResourceProvider->abandon(); |
robertphillips | 0dfa62c | 2015-11-16 06:23:31 -0800 | [diff] [blame] | 247 | |
| 248 | // Need to abandon the drawing manager first so all the render targets |
| 249 | // will be released/forgotten before they too are abandoned. |
| 250 | fDrawingManager->abandon(); |
| 251 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 252 | // abandon first to so destructors |
| 253 | // don't try to free the resources in the API. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 254 | fResourceCache->abandonAll(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 255 | |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 256 | fGpu->disconnect(GrGpu::DisconnectType::kAbandon); |
| 257 | |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 258 | fGlyphCache->freeAll(); |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 259 | fTextBlobCache->freeAll(); |
| 260 | } |
| 261 | |
Khushal | c421ca1 | 2018-06-26 14:38:34 -0700 | [diff] [blame] | 262 | bool GrContext::abandoned() const { |
| 263 | ASSERT_SINGLE_OWNER |
| 264 | return fDrawingManager->wasAbandoned(); |
| 265 | } |
| 266 | |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 267 | void GrContext::releaseResourcesAndAbandonContext() { |
| 268 | ASSERT_SINGLE_OWNER |
| 269 | |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 270 | if (this->abandoned()) { |
| 271 | return; |
| 272 | } |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 273 | fProxyProvider->abandon(); |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 274 | fResourceProvider->abandon(); |
| 275 | |
| 276 | // Need to abandon the drawing manager first so all the render targets |
| 277 | // will be released/forgotten before they too are abandoned. |
| 278 | fDrawingManager->abandon(); |
| 279 | |
| 280 | // Release all resources in the backend 3D API. |
| 281 | fResourceCache->releaseAll(); |
| 282 | |
| 283 | fGpu->disconnect(GrGpu::DisconnectType::kCleanup); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 284 | |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 285 | fGlyphCache->freeAll(); |
joshualitt | 26ffc00 | 2015-04-16 11:24:04 -0700 | [diff] [blame] | 286 | fTextBlobCache->freeAll(); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 287 | } |
| 288 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 289 | void GrContext::resetContext(uint32_t state) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 290 | ASSERT_SINGLE_OWNER |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 291 | fGpu->markContextDirty(state); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | void GrContext::freeGpuResources() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 295 | ASSERT_SINGLE_OWNER |
| 296 | |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 297 | fGlyphCache->freeAll(); |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 298 | |
| 299 | fDrawingManager->freeGpuResources(); |
bsalomon | 3033b9f | 2015-04-13 11:09:56 -0700 | [diff] [blame] | 300 | |
| 301 | fResourceCache->purgeAllUnlocked(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Robert Phillips | 6eba063 | 2018-03-28 12:25:42 -0400 | [diff] [blame] | 304 | void GrContext::purgeUnlockedResources(bool scratchResourcesOnly) { |
| 305 | ASSERT_SINGLE_OWNER |
| 306 | fResourceCache->purgeUnlockedResources(scratchResourcesOnly); |
| 307 | fResourceCache->purgeAsNeeded(); |
| 308 | fTextBlobCache->purgeStaleBlobs(); |
| 309 | } |
| 310 | |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 311 | void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) { |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 312 | ASSERT_SINGLE_OWNER |
Chris Dalton | 6c3879d | 2018-11-01 11:13:19 -0600 | [diff] [blame] | 313 | |
| 314 | auto purgeTime = GrStdSteadyClock::now() - msNotUsed; |
| 315 | |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 316 | fResourceCache->purgeAsNeeded(); |
Chris Dalton | 6c3879d | 2018-11-01 11:13:19 -0600 | [diff] [blame] | 317 | fResourceCache->purgeResourcesNotUsedSince(purgeTime); |
| 318 | |
| 319 | if (auto ccpr = fDrawingManager->getCoverageCountingPathRenderer()) { |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 320 | ccpr->purgeCacheEntriesOlderThan(fProxyProvider, purgeTime); |
Chris Dalton | 6c3879d | 2018-11-01 11:13:19 -0600 | [diff] [blame] | 321 | } |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 322 | |
| 323 | fTextBlobCache->purgeStaleBlobs(); |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 324 | } |
| 325 | |
Derek Sollenberger | 5480a18 | 2017-05-25 16:43:59 -0400 | [diff] [blame] | 326 | void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) { |
| 327 | ASSERT_SINGLE_OWNER |
| 328 | fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources); |
| 329 | } |
| 330 | |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 331 | void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 332 | ASSERT_SINGLE_OWNER |
| 333 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 334 | if (resourceCount) { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 335 | *resourceCount = fResourceCache->getBudgetedResourceCount(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 336 | } |
| 337 | if (resourceBytes) { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 338 | *resourceBytes = fResourceCache->getBudgetedResourceBytes(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 339 | } |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 342 | size_t GrContext::getResourceCachePurgeableBytes() const { |
| 343 | ASSERT_SINGLE_OWNER |
| 344 | return fResourceCache->getPurgeableBytes(); |
| 345 | } |
| 346 | |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 347 | //////////////////////////////////////////////////////////////////////////////// |
| 348 | |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 349 | int GrContext::maxTextureSize() const { return fCaps->maxTextureSize(); } |
Brian Salomon | f932a63 | 2018-04-05 12:46:09 -0400 | [diff] [blame] | 350 | |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 351 | int GrContext::maxRenderTargetSize() const { return fCaps->maxRenderTargetSize(); } |
Brian Salomon | f932a63 | 2018-04-05 12:46:09 -0400 | [diff] [blame] | 352 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 353 | bool GrContext::colorTypeSupportedAsImage(SkColorType colorType) const { |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 354 | GrPixelConfig config = SkColorType2GrPixelConfig(colorType); |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 355 | return fCaps->isConfigTexturable(config); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | int GrContext::maxSurfaceSampleCountForColorType(SkColorType colorType) const { |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 359 | GrPixelConfig config = SkColorType2GrPixelConfig(colorType); |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 360 | return fCaps->maxRenderTargetSampleCount(config); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | //////////////////////////////////////////////////////////////////////////////// |
| 364 | |
joshualitt | 0db6dfa | 2015-04-10 07:01:30 -0700 | [diff] [blame] | 365 | void GrContext::TextBlobCacheOverBudgetCB(void* data) { |
| 366 | SkASSERT(data); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 367 | // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on |
| 368 | // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls |
| 369 | // to below the GrContext level, but this is not trivial because they call drawPath on |
| 370 | // SkGpuDevice. |
joshualitt | 0db6dfa | 2015-04-10 07:01:30 -0700 | [diff] [blame] | 371 | GrContext* context = reinterpret_cast<GrContext*>(data); |
| 372 | context->flush(); |
| 373 | } |
| 374 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 375 | //////////////////////////////////////////////////////////////////////////////// |
| 376 | |
bsalomon | b77a907 | 2016-09-07 10:02:04 -0700 | [diff] [blame] | 377 | void GrContext::flush() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 378 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 379 | RETURN_IF_ABANDONED |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 380 | |
| 381 | fDrawingManager->flush(nullptr); |
| 382 | } |
| 383 | |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 384 | GrSemaphoresSubmitted GrContext::flushAndSignalSemaphores(int numSemaphores, |
| 385 | GrBackendSemaphore signalSemaphores[]) { |
| 386 | ASSERT_SINGLE_OWNER |
| 387 | if (fDrawingManager->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; } |
| 388 | |
| 389 | return fDrawingManager->flush(nullptr, numSemaphores, signalSemaphores); |
| 390 | } |
| 391 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 392 | void GrContextPriv::flush(GrSurfaceProxy* proxy) { |
| 393 | ASSERT_SINGLE_OWNER_PRIV |
| 394 | RETURN_IF_ABANDONED_PRIV |
| 395 | ASSERT_OWNED_PROXY_PRIV(proxy); |
| 396 | |
| 397 | fContext->fDrawingManager->flush(proxy); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Greg Daniel | a870b46 | 2019-01-08 15:49:46 -0500 | [diff] [blame] | 400 | //////////////////////////////////////////////////////////////////////////////// |
| 401 | |
| 402 | void GrContext::storeVkPipelineCacheData() { |
| 403 | if (fGpu) { |
| 404 | fGpu->storeVkPipelineCacheData(); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | //////////////////////////////////////////////////////////////////////////////// |
| 409 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 410 | // TODO: This will be removed when GrSurfaceContexts are aware of their color types. |
| 411 | // (skbug.com/6718) |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 412 | static bool valid_premul_config(GrPixelConfig config) { |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 413 | switch (config) { |
| 414 | case kUnknown_GrPixelConfig: return false; |
| 415 | case kAlpha_8_GrPixelConfig: return false; |
| 416 | case kGray_8_GrPixelConfig: return false; |
| 417 | case kRGB_565_GrPixelConfig: return false; |
| 418 | case kRGBA_4444_GrPixelConfig: return true; |
| 419 | case kRGBA_8888_GrPixelConfig: return true; |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 420 | case kRGB_888_GrPixelConfig: return false; |
Jim Van Verth | 69e5785 | 2018-12-05 13:38:59 -0500 | [diff] [blame] | 421 | case kRG_88_GrPixelConfig: return false; |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 422 | case kBGRA_8888_GrPixelConfig: return true; |
| 423 | case kSRGBA_8888_GrPixelConfig: return true; |
| 424 | case kSBGRA_8888_GrPixelConfig: return true; |
Brian Osman | 10fc6fd | 2018-03-02 11:01:10 -0500 | [diff] [blame] | 425 | case kRGBA_1010102_GrPixelConfig: return true; |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 426 | case kRGBA_float_GrPixelConfig: return true; |
| 427 | case kRG_float_GrPixelConfig: return false; |
| 428 | case kAlpha_half_GrPixelConfig: return false; |
| 429 | case kRGBA_half_GrPixelConfig: return true; |
| 430 | case kAlpha_8_as_Alpha_GrPixelConfig: return false; |
| 431 | case kAlpha_8_as_Red_GrPixelConfig: return false; |
| 432 | case kAlpha_half_as_Red_GrPixelConfig: return false; |
| 433 | case kGray_8_as_Lum_GrPixelConfig: return false; |
| 434 | case kGray_8_as_Red_GrPixelConfig: return false; |
| 435 | } |
| 436 | SK_ABORT("Invalid GrPixelConfig"); |
| 437 | return false; |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 438 | } |
| 439 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 440 | static bool valid_premul_color_type(GrColorType ct) { |
| 441 | switch (ct) { |
Brian Osman | 10fc6fd | 2018-03-02 11:01:10 -0500 | [diff] [blame] | 442 | case GrColorType::kUnknown: return false; |
| 443 | case GrColorType::kAlpha_8: return false; |
| 444 | case GrColorType::kRGB_565: return false; |
| 445 | case GrColorType::kABGR_4444: return true; |
| 446 | case GrColorType::kRGBA_8888: return true; |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 447 | case GrColorType::kRGB_888x: return false; |
Jim Van Verth | 69e5785 | 2018-12-05 13:38:59 -0500 | [diff] [blame] | 448 | case GrColorType::kRG_88: return false; |
Brian Osman | 10fc6fd | 2018-03-02 11:01:10 -0500 | [diff] [blame] | 449 | case GrColorType::kBGRA_8888: return true; |
| 450 | case GrColorType::kRGBA_1010102: return true; |
| 451 | case GrColorType::kGray_8: return false; |
| 452 | case GrColorType::kAlpha_F16: return false; |
| 453 | case GrColorType::kRGBA_F16: return true; |
| 454 | case GrColorType::kRG_F32: return false; |
| 455 | case GrColorType::kRGBA_F32: return true; |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 456 | } |
| 457 | SK_ABORT("Invalid GrColorType"); |
| 458 | return false; |
| 459 | } |
| 460 | |
| 461 | static bool valid_pixel_conversion(GrColorType cpuColorType, GrPixelConfig gpuConfig, |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 462 | bool premulConversion) { |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 463 | // We only allow premul <-> unpremul conversions for some formats |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 464 | if (premulConversion && |
| 465 | (!valid_premul_color_type(cpuColorType) || !valid_premul_config(gpuConfig))) { |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 466 | return false; |
| 467 | } |
Brian Osman | d2ca59a | 2017-04-13 14:03:57 -0400 | [diff] [blame] | 468 | return true; |
| 469 | } |
| 470 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 471 | bool GrContextPriv::writeSurfacePixels(GrSurfaceContext* dst, int left, int top, int width, |
| 472 | int height, GrColorType srcColorType, |
| 473 | SkColorSpace* srcColorSpace, const void* buffer, |
| 474 | size_t rowBytes, uint32_t pixelOpsFlags) { |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 475 | ASSERT_SINGLE_OWNER_PRIV |
| 476 | RETURN_FALSE_IF_ABANDONED_PRIV |
| 477 | SkASSERT(dst); |
| 478 | SkASSERT(buffer); |
| 479 | ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy()); |
Brian Salomon | d494f6e | 2018-06-01 14:13:43 -0400 | [diff] [blame] | 480 | GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels", fContext); |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 481 | |
| 482 | if (GrColorType::kUnknown == srcColorType) { |
| 483 | return false; |
| 484 | } |
| 485 | |
| 486 | if (!dst->asSurfaceProxy()->instantiate(this->resourceProvider())) { |
| 487 | return false; |
| 488 | } |
| 489 | |
| 490 | GrSurfaceProxy* dstProxy = dst->asSurfaceProxy(); |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 491 | GrSurface* dstSurface = dstProxy->peekSurface(); |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 492 | |
| 493 | if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(), |
| 494 | GrColorTypeBytesPerPixel(srcColorType), &left, &top, |
| 495 | &width, &height, &buffer, &rowBytes)) { |
| 496 | return false; |
| 497 | } |
| 498 | |
Brian Salomon | f3c4e0c | 2018-04-06 20:32:06 -0400 | [diff] [blame] | 499 | // TODO: Make GrSurfaceContext know its alpha type and pass src buffer's alpha type. |
| 500 | bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags); |
| 501 | |
| 502 | // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs |
| 503 | // that are premultiplied on the GPU. This is kept as narrow as possible for now. |
Brian Salomon | 817847c | 2018-05-07 13:33:50 -0400 | [diff] [blame] | 504 | bool canvas2DFastPath = |
Greg Daniel | 4374e96 | 2018-09-28 15:09:47 -0400 | [diff] [blame] | 505 | !fContext->contextPriv().caps()->avoidWritePixelsFastPath() && |
Brian Salomon | 817847c | 2018-05-07 13:33:50 -0400 | [diff] [blame] | 506 | premul && |
Brian Osman | 34ec374 | 2018-07-03 10:40:57 -0400 | [diff] [blame] | 507 | !dst->colorSpaceInfo().colorSpace() && |
Brian Salomon | 817847c | 2018-05-07 13:33:50 -0400 | [diff] [blame] | 508 | (srcColorType == GrColorType::kRGBA_8888 || srcColorType == GrColorType::kBGRA_8888) && |
| 509 | SkToBool(dst->asRenderTargetContext()) && |
| 510 | (dstProxy->config() == kRGBA_8888_GrPixelConfig || |
| 511 | dstProxy->config() == kBGRA_8888_GrPixelConfig) && |
| 512 | !(pixelOpsFlags & kDontFlush_PixelOpsFlag) && |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 513 | fContext->contextPriv().caps()->isConfigTexturable(kRGBA_8888_GrPixelConfig) && |
Brian Salomon | 817847c | 2018-05-07 13:33:50 -0400 | [diff] [blame] | 514 | fContext->validPMUPMConversionExists(); |
Brian Salomon | f3c4e0c | 2018-04-06 20:32:06 -0400 | [diff] [blame] | 515 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 516 | const GrCaps* caps = this->caps(); |
| 517 | if (!caps->surfaceSupportsWritePixels(dstSurface) || |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 518 | canvas2DFastPath) { |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 519 | // We don't expect callers that are skipping flushes to require an intermediate draw. |
| 520 | SkASSERT(!(pixelOpsFlags & kDontFlush_PixelOpsFlag)); |
| 521 | if (pixelOpsFlags & kDontFlush_PixelOpsFlag) { |
| 522 | return false; |
| 523 | } |
| 524 | |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 525 | GrSurfaceDesc desc; |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 526 | desc.fWidth = width; |
| 527 | desc.fHeight = height; |
| 528 | desc.fSampleCnt = 1; |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 529 | |
| 530 | GrBackendFormat format; |
| 531 | if (canvas2DFastPath) { |
| 532 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 533 | format = |
| 534 | fContext->contextPriv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType); |
| 535 | } else { |
| 536 | desc.fConfig = dstProxy->config(); |
| 537 | format = dstProxy->backendFormat().makeTexture2D(); |
| 538 | if (!format.isValid()) { |
| 539 | return false; |
| 540 | } |
| 541 | } |
| 542 | |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 543 | auto tempProxy = this->proxyProvider()->createProxy( |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 544 | format, desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kApprox, SkBudgeted::kYes); |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 545 | if (!tempProxy) { |
| 546 | return false; |
| 547 | } |
| 548 | auto tempCtx = this->drawingManager()->makeTextureContext( |
| 549 | tempProxy, dst->colorSpaceInfo().refColorSpace()); |
| 550 | if (!tempCtx) { |
| 551 | return false; |
| 552 | } |
Brian Salomon | f3c4e0c | 2018-04-06 20:32:06 -0400 | [diff] [blame] | 553 | uint32_t flags = canvas2DFastPath ? 0 : pixelOpsFlags; |
Brian Salomon | 817847c | 2018-05-07 13:33:50 -0400 | [diff] [blame] | 554 | // In the fast path we always write the srcData to the temp context as though it were RGBA. |
| 555 | // When the data is really BGRA the write will cause the R and B channels to be swapped in |
| 556 | // the intermediate surface which gets corrected by a swizzle effect when drawing to the |
| 557 | // dst. |
| 558 | auto tmpColorType = canvas2DFastPath ? GrColorType::kRGBA_8888 : srcColorType; |
Brian Salomon | d494f6e | 2018-06-01 14:13:43 -0400 | [diff] [blame] | 559 | if (!this->writeSurfacePixels(tempCtx.get(), 0, 0, width, height, tmpColorType, |
| 560 | srcColorSpace, buffer, rowBytes, flags)) { |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 561 | return false; |
| 562 | } |
Brian Salomon | f3c4e0c | 2018-04-06 20:32:06 -0400 | [diff] [blame] | 563 | if (canvas2DFastPath) { |
| 564 | GrPaint paint; |
| 565 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Brian Salomon | 817847c | 2018-05-07 13:33:50 -0400 | [diff] [blame] | 566 | auto fp = fContext->createUPMToPMEffect( |
Brian Osman | 5ea96bf | 2018-10-02 14:58:05 -0400 | [diff] [blame] | 567 | GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I())); |
Brian Salomon | 817847c | 2018-05-07 13:33:50 -0400 | [diff] [blame] | 568 | if (srcColorType == GrColorType::kBGRA_8888) { |
| 569 | fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA()); |
| 570 | } |
| 571 | if (!fp) { |
| 572 | return false; |
| 573 | } |
| 574 | paint.addColorFragmentProcessor(std::move(fp)); |
Brian Salomon | f3c4e0c | 2018-04-06 20:32:06 -0400 | [diff] [blame] | 575 | dst->asRenderTargetContext()->fillRectToRect( |
| 576 | GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), |
| 577 | SkRect::MakeXYWH(left, top, width, height), SkRect::MakeWH(width, height)); |
| 578 | return true; |
| 579 | } else { |
| 580 | return dst->copy(tempProxy.get(), SkIRect::MakeWH(width, height), {left, top}); |
| 581 | } |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 582 | } |
| 583 | |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 584 | bool convert = premul; |
| 585 | |
| 586 | if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) { |
| 587 | return false; |
| 588 | } |
| 589 | |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 590 | GrColorType allowedColorType = fContext->contextPriv().caps()->supportedWritePixelsColorType( |
| 591 | dstProxy->config(), srcColorType); |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 592 | convert = convert || (srcColorType != allowedColorType); |
| 593 | |
| 594 | if (!dst->colorSpaceInfo().colorSpace()) { |
| 595 | // "Legacy" mode - no color space conversions. |
| 596 | srcColorSpace = nullptr; |
| 597 | } |
| 598 | convert = convert || !SkColorSpace::Equals(srcColorSpace, dst->colorSpaceInfo().colorSpace()); |
| 599 | |
| 600 | std::unique_ptr<char[]> tempBuffer; |
| 601 | if (convert) { |
| 602 | auto srcSkColorType = GrColorTypeToSkColorType(srcColorType); |
| 603 | auto dstSkColorType = GrColorTypeToSkColorType(allowedColorType); |
| 604 | if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) { |
| 605 | return false; |
| 606 | } |
Brian Osman | 2091fbb | 2018-10-11 11:05:37 -0400 | [diff] [blame] | 607 | auto srcAlphaType = SkColorTypeIsAlwaysOpaque(srcSkColorType) |
| 608 | ? kOpaque_SkAlphaType |
| 609 | : (premul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType); |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 610 | SkPixmap src(SkImageInfo::Make(width, height, srcSkColorType, srcAlphaType, |
| 611 | sk_ref_sp(srcColorSpace)), |
| 612 | buffer, rowBytes); |
| 613 | auto tempSrcII = SkImageInfo::Make(width, height, dstSkColorType, kPremul_SkAlphaType, |
| 614 | dst->colorSpaceInfo().refColorSpace()); |
| 615 | auto size = tempSrcII.computeMinByteSize(); |
| 616 | if (!size) { |
| 617 | return false; |
| 618 | } |
| 619 | tempBuffer.reset(new char[size]); |
| 620 | SkPixmap tempSrc(tempSrcII, tempBuffer.get(), tempSrcII.minRowBytes()); |
| 621 | if (!src.readPixels(tempSrc)) { |
| 622 | return false; |
| 623 | } |
| 624 | srcColorType = allowedColorType; |
| 625 | buffer = tempSrc.addr(); |
| 626 | rowBytes = tempSrc.rowBytes(); |
| 627 | if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) { |
| 628 | std::unique_ptr<char[]> row(new char[rowBytes]); |
| 629 | for (int y = 0; y < height / 2; ++y) { |
| 630 | memcpy(row.get(), tempSrc.addr(0, y), rowBytes); |
| 631 | memcpy(tempSrc.writable_addr(0, y), tempSrc.addr(0, height - 1 - y), rowBytes); |
| 632 | memcpy(tempSrc.writable_addr(0, height - 1 - y), row.get(), rowBytes); |
| 633 | } |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 634 | top = dstSurface->height() - top - height; |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 635 | } |
| 636 | } else if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) { |
| 637 | size_t trimRowBytes = GrColorTypeBytesPerPixel(srcColorType) * width; |
| 638 | tempBuffer.reset(new char[trimRowBytes * height]); |
| 639 | char* dst = reinterpret_cast<char*>(tempBuffer.get()) + trimRowBytes * (height - 1); |
| 640 | const char* src = reinterpret_cast<const char*>(buffer); |
| 641 | for (int i = 0; i < height; ++i, src += rowBytes, dst -= trimRowBytes) { |
| 642 | memcpy(dst, src, trimRowBytes); |
| 643 | } |
| 644 | buffer = tempBuffer.get(); |
| 645 | rowBytes = trimRowBytes; |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 646 | top = dstSurface->height() - top - height; |
Brian Salomon | 5f33a8c | 2018-02-26 14:32:39 -0500 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) { |
| 650 | this->flush(nullptr); // MDB TODO: tighten this |
| 651 | } |
| 652 | |
| 653 | return this->getGpu()->writePixels(dstSurface, left, top, width, height, srcColorType, buffer, |
| 654 | rowBytes); |
| 655 | } |
| 656 | |
Brian Salomon | d494f6e | 2018-06-01 14:13:43 -0400 | [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 pixelOpsFlags) { |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 661 | ASSERT_SINGLE_OWNER_PRIV |
| 662 | RETURN_FALSE_IF_ABANDONED_PRIV |
| 663 | SkASSERT(src); |
| 664 | SkASSERT(buffer); |
| 665 | ASSERT_OWNED_PROXY_PRIV(src->asSurfaceProxy()); |
Brian Salomon | d494f6e | 2018-06-01 14:13:43 -0400 | [diff] [blame] | 666 | GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "readSurfacePixels", fContext); |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 667 | |
Brian Salomon | f3c4e0c | 2018-04-06 20:32:06 -0400 | [diff] [blame] | 668 | SkASSERT(!(pixelOpsFlags & kDontFlush_PixelOpsFlag)); |
| 669 | if (pixelOpsFlags & kDontFlush_PixelOpsFlag) { |
Brian Salomon | 5fba7ad | 2018-03-22 10:01:16 -0400 | [diff] [blame] | 670 | return false; |
| 671 | } |
| 672 | |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 673 | // MDB TODO: delay this instantiation until later in the method |
| 674 | if (!src->asSurfaceProxy()->instantiate(this->resourceProvider())) { |
| 675 | return false; |
| 676 | } |
| 677 | |
| 678 | GrSurfaceProxy* srcProxy = src->asSurfaceProxy(); |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 679 | GrSurface* srcSurface = srcProxy->peekSurface(); |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 680 | |
| 681 | if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(), |
| 682 | GrColorTypeBytesPerPixel(dstColorType), &left, &top, |
| 683 | &width, &height, &buffer, &rowBytes)) { |
| 684 | return false; |
| 685 | } |
| 686 | |
| 687 | // TODO: Make GrSurfaceContext know its alpha type and pass dst buffer's alpha type. |
Brian Salomon | f3c4e0c | 2018-04-06 20:32:06 -0400 | [diff] [blame] | 688 | bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags); |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 689 | |
| 690 | if (!valid_pixel_conversion(dstColorType, srcProxy->config(), unpremul)) { |
| 691 | return false; |
| 692 | } |
| 693 | |
Brian Salomon | f3c4e0c | 2018-04-06 20:32:06 -0400 | [diff] [blame] | 694 | // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't |
| 695 | // care so much about getImageData performance. However, in order to ensure putImageData/ |
| 696 | // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary |
Brian Salomon | d494f6e | 2018-06-01 14:13:43 -0400 | [diff] [blame] | 697 | // unpremul step to writeSurfacePixels's premul step (which is determined empirically in |
Brian Salomon | f3c4e0c | 2018-04-06 20:32:06 -0400 | [diff] [blame] | 698 | // fContext->vaildaPMUPMConversionExists()). |
Brian Salomon | 817847c | 2018-05-07 13:33:50 -0400 | [diff] [blame] | 699 | bool canvas2DFastPath = |
| 700 | unpremul && |
Brian Osman | 34ec374 | 2018-07-03 10:40:57 -0400 | [diff] [blame] | 701 | !src->colorSpaceInfo().colorSpace() && |
Brian Salomon | 817847c | 2018-05-07 13:33:50 -0400 | [diff] [blame] | 702 | (GrColorType::kRGBA_8888 == dstColorType || GrColorType::kBGRA_8888 == dstColorType) && |
| 703 | SkToBool(srcProxy->asTextureProxy()) && |
| 704 | (srcProxy->config() == kRGBA_8888_GrPixelConfig || |
| 705 | srcProxy->config() == kBGRA_8888_GrPixelConfig) && |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 706 | fContext->contextPriv().caps()->isConfigRenderable(kRGBA_8888_GrPixelConfig) && |
Brian Salomon | 817847c | 2018-05-07 13:33:50 -0400 | [diff] [blame] | 707 | fContext->validPMUPMConversionExists(); |
Brian Salomon | f3c4e0c | 2018-04-06 20:32:06 -0400 | [diff] [blame] | 708 | |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 709 | if (!fContext->contextPriv().caps()->surfaceSupportsReadPixels(srcSurface) || |
| 710 | canvas2DFastPath) { |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 711 | GrSurfaceDesc desc; |
Brian Salomon | f3c4e0c | 2018-04-06 20:32:06 -0400 | [diff] [blame] | 712 | desc.fFlags = canvas2DFastPath ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags; |
| 713 | desc.fConfig = canvas2DFastPath ? kRGBA_8888_GrPixelConfig : srcProxy->config(); |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 714 | desc.fWidth = width; |
| 715 | desc.fHeight = height; |
| 716 | desc.fSampleCnt = 1; |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 717 | |
| 718 | GrBackendFormat format; |
| 719 | if (canvas2DFastPath) { |
| 720 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 721 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 722 | format = this->caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType); |
| 723 | } else { |
| 724 | desc.fFlags = kNone_GrSurfaceFlags; |
| 725 | desc.fConfig = srcProxy->config(); |
| 726 | format = srcProxy->backendFormat().makeTexture2D(); |
| 727 | if (!format.isValid()) { |
| 728 | return false; |
| 729 | } |
| 730 | } |
| 731 | |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 732 | auto tempProxy = this->proxyProvider()->createProxy( |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 733 | format, desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kApprox, SkBudgeted::kYes); |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 734 | if (!tempProxy) { |
| 735 | return false; |
| 736 | } |
Brian Salomon | f3c4e0c | 2018-04-06 20:32:06 -0400 | [diff] [blame] | 737 | sk_sp<GrSurfaceContext> tempCtx; |
| 738 | if (canvas2DFastPath) { |
| 739 | tempCtx = this->drawingManager()->makeRenderTargetContext(std::move(tempProxy), nullptr, |
| 740 | nullptr); |
Greg Daniel | 4231401 | 2018-04-23 10:57:37 -0400 | [diff] [blame] | 741 | SkASSERT(tempCtx->asRenderTargetContext()); |
| 742 | tempCtx->asRenderTargetContext()->discard(); |
Brian Salomon | f3c4e0c | 2018-04-06 20:32:06 -0400 | [diff] [blame] | 743 | } else { |
| 744 | tempCtx = this->drawingManager()->makeTextureContext( |
| 745 | std::move(tempProxy), src->colorSpaceInfo().refColorSpace()); |
| 746 | } |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 747 | if (!tempCtx) { |
| 748 | return false; |
| 749 | } |
Brian Salomon | f3c4e0c | 2018-04-06 20:32:06 -0400 | [diff] [blame] | 750 | if (canvas2DFastPath) { |
| 751 | GrPaint paint; |
| 752 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Brian Salomon | 817847c | 2018-05-07 13:33:50 -0400 | [diff] [blame] | 753 | auto fp = fContext->createPMToUPMEffect( |
Brian Salomon | f3c4e0c | 2018-04-06 20:32:06 -0400 | [diff] [blame] | 754 | GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()), |
Brian Osman | 5ea96bf | 2018-10-02 14:58:05 -0400 | [diff] [blame] | 755 | SkMatrix::I())); |
Brian Salomon | 817847c | 2018-05-07 13:33:50 -0400 | [diff] [blame] | 756 | if (dstColorType == GrColorType::kBGRA_8888) { |
| 757 | fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA()); |
| 758 | dstColorType = GrColorType::kRGBA_8888; |
| 759 | } |
| 760 | if (!fp) { |
| 761 | return false; |
| 762 | } |
| 763 | paint.addColorFragmentProcessor(std::move(fp)); |
Brian Salomon | f3c4e0c | 2018-04-06 20:32:06 -0400 | [diff] [blame] | 764 | tempCtx->asRenderTargetContext()->fillRectToRect( |
| 765 | GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), |
| 766 | SkRect::MakeWH(width, height), SkRect::MakeXYWH(left, top, width, height)); |
| 767 | } else if (!tempCtx->copy(srcProxy, SkIRect::MakeXYWH(left, top, width, height), {0, 0})) { |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 768 | return false; |
| 769 | } |
Brian Salomon | f3c4e0c | 2018-04-06 20:32:06 -0400 | [diff] [blame] | 770 | uint32_t flags = canvas2DFastPath ? 0 : pixelOpsFlags; |
Brian Salomon | d494f6e | 2018-06-01 14:13:43 -0400 | [diff] [blame] | 771 | return this->readSurfacePixels(tempCtx.get(), 0, 0, width, height, dstColorType, |
| 772 | dstColorSpace, buffer, rowBytes, flags); |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 773 | } |
| 774 | |
Brian Salomon | f3c4e0c | 2018-04-06 20:32:06 -0400 | [diff] [blame] | 775 | bool convert = unpremul; |
| 776 | |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 777 | bool flip = srcProxy->origin() == kBottomLeft_GrSurfaceOrigin; |
| 778 | if (flip) { |
| 779 | top = srcSurface->height() - top - height; |
| 780 | } |
| 781 | |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 782 | GrColorType allowedColorType = fContext->contextPriv().caps()->supportedReadPixelsColorType( |
| 783 | srcProxy->config(), dstColorType); |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 784 | convert = convert || (dstColorType != allowedColorType); |
| 785 | |
| 786 | if (!src->colorSpaceInfo().colorSpace()) { |
| 787 | // "Legacy" mode - no color space conversions. |
| 788 | dstColorSpace = nullptr; |
| 789 | } |
| 790 | convert = convert || !SkColorSpace::Equals(dstColorSpace, src->colorSpaceInfo().colorSpace()); |
| 791 | |
| 792 | SkAutoPixmapStorage tempPixmap; |
| 793 | SkPixmap finalPixmap; |
| 794 | if (convert) { |
| 795 | SkColorType srcSkColorType = GrColorTypeToSkColorType(allowedColorType); |
| 796 | SkColorType dstSkColorType = GrColorTypeToSkColorType(dstColorType); |
Brian Osman | 2091fbb | 2018-10-11 11:05:37 -0400 | [diff] [blame] | 797 | bool srcAlwaysOpaque = SkColorTypeIsAlwaysOpaque(srcSkColorType); |
| 798 | bool dstAlwaysOpaque = SkColorTypeIsAlwaysOpaque(dstSkColorType); |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 799 | if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) { |
| 800 | return false; |
| 801 | } |
Brian Osman | 2091fbb | 2018-10-11 11:05:37 -0400 | [diff] [blame] | 802 | auto tempAT = srcAlwaysOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType; |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 803 | auto tempII = SkImageInfo::Make(width, height, srcSkColorType, tempAT, |
| 804 | src->colorSpaceInfo().refColorSpace()); |
Brian Osman | 2091fbb | 2018-10-11 11:05:37 -0400 | [diff] [blame] | 805 | SkASSERT(!unpremul || !dstAlwaysOpaque); |
| 806 | auto finalAT = (srcAlwaysOpaque || dstAlwaysOpaque) |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 807 | ? kOpaque_SkAlphaType |
| 808 | : unpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType; |
| 809 | auto finalII = |
| 810 | SkImageInfo::Make(width, height, dstSkColorType, finalAT, sk_ref_sp(dstColorSpace)); |
| 811 | if (!SkImageInfoValidConversion(finalII, tempII)) { |
| 812 | return false; |
| 813 | } |
Brian Salomon | 6196fc1 | 2018-07-12 16:39:23 -0400 | [diff] [blame] | 814 | if (!tempPixmap.tryAlloc(tempII)) { |
| 815 | return false; |
| 816 | } |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 817 | finalPixmap.reset(finalII, buffer, rowBytes); |
| 818 | buffer = tempPixmap.writable_addr(); |
| 819 | rowBytes = tempPixmap.rowBytes(); |
Brian Salomon | 3d945e8 | 2018-05-14 14:54:06 -0400 | [diff] [blame] | 820 | // Chrome msan bots require this. |
| 821 | sk_bzero(buffer, tempPixmap.computeByteSize()); |
Brian Salomon | 19eaf2d | 2018-03-19 16:06:44 -0400 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | if (srcSurface->surfacePriv().hasPendingWrite()) { |
| 825 | this->flush(nullptr); // MDB TODO: tighten this |
| 826 | } |
| 827 | |
| 828 | if (!fContext->fGpu->readPixels(srcSurface, left, top, width, height, allowedColorType, buffer, |
| 829 | rowBytes)) { |
| 830 | return false; |
| 831 | } |
| 832 | |
| 833 | if (flip) { |
| 834 | size_t trimRowBytes = GrColorTypeBytesPerPixel(allowedColorType) * width; |
| 835 | std::unique_ptr<char[]> row(new char[trimRowBytes]); |
| 836 | char* upper = reinterpret_cast<char*>(buffer); |
| 837 | char* lower = reinterpret_cast<char*>(buffer) + (height - 1) * rowBytes; |
| 838 | for (int y = 0; y < height / 2; ++y, upper += rowBytes, lower -= rowBytes) { |
| 839 | memcpy(row.get(), upper, trimRowBytes); |
| 840 | memcpy(upper, lower, trimRowBytes); |
| 841 | memcpy(lower, row.get(), trimRowBytes); |
| 842 | } |
| 843 | } |
| 844 | if (convert) { |
| 845 | if (!tempPixmap.readPixels(finalPixmap)) { |
| 846 | return false; |
| 847 | } |
| 848 | } |
| 849 | return true; |
| 850 | } |
| 851 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 852 | void GrContextPriv::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) { |
| 853 | ASSERT_SINGLE_OWNER_PRIV |
| 854 | RETURN_IF_ABANDONED_PRIV |
| 855 | SkASSERT(proxy); |
| 856 | ASSERT_OWNED_PROXY_PRIV(proxy); |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 857 | fContext->fDrawingManager->prepareSurfaceForExternalIO(proxy, 0, nullptr); |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 860 | void GrContextPriv::flushSurfaceWrites(GrSurfaceProxy* proxy) { |
| 861 | ASSERT_SINGLE_OWNER_PRIV |
| 862 | RETURN_IF_ABANDONED_PRIV |
| 863 | SkASSERT(proxy); |
| 864 | ASSERT_OWNED_PROXY_PRIV(proxy); |
| 865 | if (proxy->priv().hasPendingWrite()) { |
| 866 | this->flush(proxy); |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 867 | } |
| 868 | } |
| 869 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 870 | void GrContextPriv::flushSurfaceIO(GrSurfaceProxy* proxy) { |
| 871 | ASSERT_SINGLE_OWNER_PRIV |
| 872 | RETURN_IF_ABANDONED_PRIV |
| 873 | SkASSERT(proxy); |
| 874 | ASSERT_OWNED_PROXY_PRIV(proxy); |
| 875 | if (proxy->priv().hasPendingIO()) { |
| 876 | this->flush(proxy); |
ajuma | 95243eb | 2016-08-24 08:19:02 -0700 | [diff] [blame] | 877 | } |
| 878 | } |
| 879 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 880 | //////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | b471a32 | 2014-03-10 07:40:03 +0000 | [diff] [blame] | 881 | |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 882 | sk_sp<GrOpMemoryPool> GrContextPriv::refOpMemoryPool() { |
| 883 | if (!fContext->fOpMemoryPool) { |
| 884 | // DDL TODO: should the size of the memory pool be decreased in DDL mode? CPU-side memory |
| 885 | // consumed in DDL mode vs. normal mode for a single skp might be a good metric of wasted |
| 886 | // memory. |
| 887 | fContext->fOpMemoryPool = sk_sp<GrOpMemoryPool>(new GrOpMemoryPool(16384, 16384)); |
| 888 | } |
| 889 | |
| 890 | SkASSERT(fContext->fOpMemoryPool); |
| 891 | return fContext->fOpMemoryPool; |
| 892 | } |
| 893 | |
| 894 | GrOpMemoryPool* GrContextPriv::opMemoryPool() { |
| 895 | return this->refOpMemoryPool().get(); |
| 896 | } |
| 897 | |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 898 | sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy, |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 899 | sk_sp<SkColorSpace> colorSpace, |
| 900 | const SkSurfaceProps* props) { |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 901 | ASSERT_SINGLE_OWNER_PRIV |
| 902 | |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 903 | if (proxy->asRenderTargetProxy()) { |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 904 | return this->drawingManager()->makeRenderTargetContext(std::move(proxy), |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 905 | std::move(colorSpace), props); |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 906 | } else { |
| 907 | SkASSERT(proxy->asTextureProxy()); |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 908 | SkASSERT(!props); |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 909 | return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace)); |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 910 | } |
| 911 | } |
| 912 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 913 | sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrBackendFormat& format, |
| 914 | const GrSurfaceDesc& dstDesc, |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 915 | GrSurfaceOrigin origin, |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 916 | GrMipMapped mipMapped, |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 917 | SkBackingFit fit, |
Brian Salomon | 366093f | 2018-02-13 09:25:22 -0500 | [diff] [blame] | 918 | SkBudgeted isDstBudgeted, |
| 919 | sk_sp<SkColorSpace> colorSpace, |
| 920 | const SkSurfaceProps* props) { |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 921 | sk_sp<GrTextureProxy> proxy; |
| 922 | if (GrMipMapped::kNo == mipMapped) { |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 923 | proxy = this->proxyProvider()->createProxy(format, dstDesc, origin, fit, isDstBudgeted); |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 924 | } else { |
| 925 | SkASSERT(SkBackingFit::kExact == fit); |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 926 | proxy = this->proxyProvider()->createMipMapProxy(format, dstDesc, origin, isDstBudgeted); |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 927 | } |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 928 | if (!proxy) { |
| 929 | return nullptr; |
| 930 | } |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 931 | |
Greg Daniel | 55fa647 | 2018-03-16 16:13:10 -0400 | [diff] [blame] | 932 | sk_sp<GrSurfaceContext> sContext = this->makeWrappedSurfaceContext(std::move(proxy), |
| 933 | std::move(colorSpace), |
| 934 | props); |
| 935 | if (sContext && sContext->asRenderTargetContext()) { |
| 936 | sContext->asRenderTargetContext()->discard(); |
| 937 | } |
| 938 | |
| 939 | return sContext; |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 940 | } |
| 941 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 942 | sk_sp<GrTextureContext> GrContextPriv::makeBackendTextureContext(const GrBackendTexture& tex, |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 943 | GrSurfaceOrigin origin, |
Brian Osman | c1e3705 | 2017-03-09 14:19:20 -0500 | [diff] [blame] | 944 | sk_sp<SkColorSpace> colorSpace) { |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 945 | ASSERT_SINGLE_OWNER_PRIV |
| 946 | |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 947 | sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->wrapBackendTexture( |
| 948 | tex, origin, kBorrow_GrWrapOwnership, kRW_GrIOType); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 949 | if (!proxy) { |
| 950 | return nullptr; |
| 951 | } |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 952 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 953 | return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace)); |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 954 | } |
| 955 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 956 | sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext( |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 957 | const GrBackendTexture& tex, |
| 958 | GrSurfaceOrigin origin, |
| 959 | int sampleCnt, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 960 | sk_sp<SkColorSpace> colorSpace, |
Brian Osman | c1e3705 | 2017-03-09 14:19:20 -0500 | [diff] [blame] | 961 | const SkSurfaceProps* props) { |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 962 | ASSERT_SINGLE_OWNER_PRIV |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 963 | SkASSERT(sampleCnt > 0); |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 964 | |
Brian Salomon | 7578f3e | 2018-03-07 14:39:54 -0500 | [diff] [blame] | 965 | sk_sp<GrTextureProxy> proxy( |
| 966 | this->proxyProvider()->wrapRenderableBackendTexture(tex, origin, sampleCnt)); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 967 | if (!proxy) { |
| 968 | return nullptr; |
| 969 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 970 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 971 | return this->drawingManager()->makeRenderTargetContext(std::move(proxy), |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 972 | std::move(colorSpace), props); |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 973 | } |
| 974 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 975 | sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext( |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 976 | const GrBackendRenderTarget& backendRT, |
| 977 | GrSurfaceOrigin origin, |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 978 | sk_sp<SkColorSpace> colorSpace, |
| 979 | const SkSurfaceProps* surfaceProps) { |
| 980 | ASSERT_SINGLE_OWNER_PRIV |
| 981 | |
Brian Salomon | 7578f3e | 2018-03-07 14:39:54 -0500 | [diff] [blame] | 982 | sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->wrapBackendRenderTarget(backendRT, origin); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 983 | if (!proxy) { |
| 984 | return nullptr; |
| 985 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 986 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 987 | return this->drawingManager()->makeRenderTargetContext(std::move(proxy), |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 988 | std::move(colorSpace), |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 989 | surfaceProps); |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 990 | } |
| 991 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 992 | sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext( |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 993 | const GrBackendTexture& tex, |
| 994 | GrSurfaceOrigin origin, |
| 995 | int sampleCnt, |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 996 | sk_sp<SkColorSpace> colorSpace, |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 997 | const SkSurfaceProps* props) { |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 998 | ASSERT_SINGLE_OWNER_PRIV |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 999 | SkASSERT(sampleCnt > 0); |
Brian Salomon | 7578f3e | 2018-03-07 14:39:54 -0500 | [diff] [blame] | 1000 | sk_sp<GrSurfaceProxy> proxy( |
| 1001 | this->proxyProvider()->wrapBackendTextureAsRenderTarget(tex, origin, sampleCnt)); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 1002 | if (!proxy) { |
| 1003 | return nullptr; |
| 1004 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1005 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 1006 | return this->drawingManager()->makeRenderTargetContext(std::move(proxy), |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1007 | std::move(colorSpace), |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 1008 | props); |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 1011 | sk_sp<GrRenderTargetContext> GrContextPriv::makeVulkanSecondaryCBRenderTargetContext( |
| 1012 | const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo, const SkSurfaceProps* props) { |
| 1013 | ASSERT_SINGLE_OWNER_PRIV |
| 1014 | sk_sp<GrSurfaceProxy> proxy( |
| 1015 | this->proxyProvider()->wrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo)); |
| 1016 | if (!proxy) { |
| 1017 | return nullptr; |
| 1018 | } |
| 1019 | |
| 1020 | return this->drawingManager()->makeRenderTargetContext(std::move(proxy), |
| 1021 | imageInfo.refColorSpace(), |
| 1022 | props); |
| 1023 | } |
| 1024 | |
Chris Dalton | fe199b7 | 2017-05-05 11:26:15 -0400 | [diff] [blame] | 1025 | void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) { |
| 1026 | fContext->fDrawingManager->addOnFlushCallbackObject(onFlushCBObject); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 1027 | } |
| 1028 | |
Robert Phillips | 6200036 | 2018-02-01 09:10:04 -0500 | [diff] [blame] | 1029 | void GrContextPriv::moveOpListsToDDL(SkDeferredDisplayList* ddl) { |
| 1030 | fContext->fDrawingManager->moveOpListsToDDL(ddl); |
| 1031 | } |
| 1032 | |
| 1033 | void GrContextPriv::copyOpListsFromDDL(const SkDeferredDisplayList* ddl, |
| 1034 | GrRenderTargetProxy* newDest) { |
| 1035 | fContext->fDrawingManager->copyOpListsFromDDL(ddl, newDest); |
| 1036 | } |
| 1037 | |
robertphillips | 48fde9c | 2016-09-06 05:20:20 -0700 | [diff] [blame] | 1038 | static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) { |
Brian Osman | 78f20e0 | 2017-01-12 10:28:01 -0500 | [diff] [blame] | 1039 | switch (config) { |
| 1040 | case kAlpha_8_GrPixelConfig: |
Greg Daniel | 93b4ddd | 2018-04-16 16:16:47 -0400 | [diff] [blame] | 1041 | case kAlpha_8_as_Alpha_GrPixelConfig: |
| 1042 | case kAlpha_8_as_Red_GrPixelConfig: |
Brian Osman | 78f20e0 | 2017-01-12 10:28:01 -0500 | [diff] [blame] | 1043 | case kRGB_565_GrPixelConfig: |
| 1044 | case kRGBA_4444_GrPixelConfig: |
| 1045 | case kBGRA_8888_GrPixelConfig: |
Brian Osman | 10fc6fd | 2018-03-02 11:01:10 -0500 | [diff] [blame] | 1046 | case kRGBA_1010102_GrPixelConfig: |
Brian Osman | 9c11135 | 2018-10-16 10:18:26 -0400 | [diff] [blame] | 1047 | case kRGBA_half_GrPixelConfig: |
Brian Osman | 78f20e0 | 2017-01-12 10:28:01 -0500 | [diff] [blame] | 1048 | return kRGBA_8888_GrPixelConfig; |
| 1049 | case kSBGRA_8888_GrPixelConfig: |
| 1050 | return kSRGBA_8888_GrPixelConfig; |
| 1051 | case kAlpha_half_GrPixelConfig: |
Greg Daniel | 93b4ddd | 2018-04-16 16:16:47 -0400 | [diff] [blame] | 1052 | case kAlpha_half_as_Red_GrPixelConfig: |
Brian Osman | 78f20e0 | 2017-01-12 10:28:01 -0500 | [diff] [blame] | 1053 | return kRGBA_half_GrPixelConfig; |
Greg Daniel | 93b4ddd | 2018-04-16 16:16:47 -0400 | [diff] [blame] | 1054 | case kGray_8_GrPixelConfig: |
| 1055 | case kGray_8_as_Lum_GrPixelConfig: |
| 1056 | case kGray_8_as_Red_GrPixelConfig: |
| 1057 | return kRGB_888_GrPixelConfig; |
Brian Osman | 78f20e0 | 2017-01-12 10:28:01 -0500 | [diff] [blame] | 1058 | default: |
| 1059 | return kUnknown_GrPixelConfig; |
| 1060 | } |
robertphillips | 48fde9c | 2016-09-06 05:20:20 -0700 | [diff] [blame] | 1061 | } |
| 1062 | |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 1063 | sk_sp<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContextWithFallback( |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 1064 | const GrBackendFormat& format, |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 1065 | SkBackingFit fit, |
| 1066 | int width, int height, |
| 1067 | GrPixelConfig config, |
| 1068 | sk_sp<SkColorSpace> colorSpace, |
| 1069 | int sampleCnt, |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 1070 | GrMipMapped mipMapped, |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 1071 | GrSurfaceOrigin origin, |
| 1072 | const SkSurfaceProps* surfaceProps, |
| 1073 | SkBudgeted budgeted) { |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 1074 | GrBackendFormat localFormat = format; |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1075 | SkASSERT(sampleCnt > 0); |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 1076 | if (0 == fContext->contextPriv().caps()->getRenderTargetSampleCount(sampleCnt, config)) { |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 1077 | config = GrPixelConfigFallback(config); |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 1078 | // TODO: First we should be checking the getRenderTargetSampleCount from the GrBackendFormat |
| 1079 | // and not GrPixelConfig. Besides that, we should implement the fallback in the caps, but |
| 1080 | // for now we just convert the fallback pixel config to an SkColorType and then get the |
| 1081 | // GrBackendFormat from that. |
| 1082 | SkColorType colorType; |
| 1083 | if (!GrPixelConfigToColorType(config, &colorType)) { |
| 1084 | return nullptr; |
| 1085 | } |
| 1086 | localFormat = fContext->fCaps->getBackendFormatFromColorType(colorType); |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 1087 | } |
| 1088 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 1089 | return this->makeDeferredRenderTargetContext(localFormat, fit, width, height, config, |
| 1090 | std::move(colorSpace), sampleCnt, mipMapped, |
| 1091 | origin, surfaceProps, budgeted); |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 1092 | } |
| 1093 | |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 1094 | sk_sp<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContext( |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 1095 | const GrBackendFormat& format, |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1096 | SkBackingFit fit, |
| 1097 | int width, int height, |
| 1098 | GrPixelConfig config, |
| 1099 | sk_sp<SkColorSpace> colorSpace, |
| 1100 | int sampleCnt, |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 1101 | GrMipMapped mipMapped, |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1102 | GrSurfaceOrigin origin, |
| 1103 | const SkSurfaceProps* surfaceProps, |
| 1104 | SkBudgeted budgeted) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1105 | SkASSERT(sampleCnt > 0); |
Khushal | c421ca1 | 2018-06-26 14:38:34 -0700 | [diff] [blame] | 1106 | if (fContext->abandoned()) { |
Brian Salomon | 79e4d1b | 2017-07-13 11:17:11 -0400 | [diff] [blame] | 1107 | return nullptr; |
| 1108 | } |
| 1109 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1110 | GrSurfaceDesc desc; |
| 1111 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1112 | desc.fWidth = width; |
| 1113 | desc.fHeight = height; |
| 1114 | desc.fConfig = config; |
| 1115 | desc.fSampleCnt = sampleCnt; |
| 1116 | |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 1117 | sk_sp<GrTextureProxy> rtp; |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 1118 | if (GrMipMapped::kNo == mipMapped) { |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 1119 | rtp = fContext->fProxyProvider->createProxy(format, desc, origin, fit, budgeted); |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 1120 | } else { |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 1121 | rtp = fContext->fProxyProvider->createMipMapProxy(format, desc, origin, budgeted); |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 1122 | } |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 1123 | if (!rtp) { |
| 1124 | return nullptr; |
| 1125 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1126 | |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 1127 | sk_sp<GrRenderTargetContext> renderTargetContext( |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 1128 | fContext->fDrawingManager->makeRenderTargetContext(std::move(rtp), |
| 1129 | std::move(colorSpace), |
| 1130 | surfaceProps)); |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 1131 | if (!renderTargetContext) { |
| 1132 | return nullptr; |
| 1133 | } |
| 1134 | |
| 1135 | renderTargetContext->discard(); |
| 1136 | |
| 1137 | return renderTargetContext; |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 1138 | } |
| 1139 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 1140 | std::unique_ptr<GrFragmentProcessor> GrContext::createPMToUPMEffect( |
Brian Osman | 5ea96bf | 2018-10-02 14:58:05 -0400 | [diff] [blame] | 1141 | std::unique_ptr<GrFragmentProcessor> fp) { |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 1142 | ASSERT_SINGLE_OWNER |
Brian Osman | 5ea96bf | 2018-10-02 14:58:05 -0400 | [diff] [blame] | 1143 | // We should have already called this->validPMUPMConversionExists() in this case |
| 1144 | SkASSERT(fDidTestPMConversions); |
| 1145 | // ...and it should have succeeded |
| 1146 | SkASSERT(this->validPMUPMConversionExists()); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 1147 | |
Brian Osman | 5ea96bf | 2018-10-02 14:58:05 -0400 | [diff] [blame] | 1148 | return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul); |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 1149 | } |
| 1150 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 1151 | std::unique_ptr<GrFragmentProcessor> GrContext::createUPMToPMEffect( |
Brian Osman | 5ea96bf | 2018-10-02 14:58:05 -0400 | [diff] [blame] | 1152 | std::unique_ptr<GrFragmentProcessor> fp) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1153 | ASSERT_SINGLE_OWNER |
Brian Osman | 5ea96bf | 2018-10-02 14:58:05 -0400 | [diff] [blame] | 1154 | // We should have already called this->validPMUPMConversionExists() in this case |
| 1155 | SkASSERT(fDidTestPMConversions); |
| 1156 | // ...and it should have succeeded |
| 1157 | SkASSERT(this->validPMUPMConversionExists()); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 1158 | |
Brian Osman | 5ea96bf | 2018-10-02 14:58:05 -0400 | [diff] [blame] | 1159 | return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 1162 | bool GrContext::validPMUPMConversionExists() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1163 | ASSERT_SINGLE_OWNER |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 1164 | if (!fDidTestPMConversions) { |
Brian Osman | 28804f3 | 2017-04-20 10:24:36 -0400 | [diff] [blame] | 1165 | fPMUPMConversionsRoundTrip = GrConfigConversionEffect::TestForPreservingPMConversions(this); |
Brian Osman | 409e74f | 2017-04-17 11:48:28 -0400 | [diff] [blame] | 1166 | fDidTestPMConversions = true; |
| 1167 | } |
| 1168 | |
bsalomon | 636e802 | 2015-07-29 06:08:46 -0700 | [diff] [blame] | 1169 | // 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] | 1170 | return fPMUPMConversionsRoundTrip; |
bsalomon | 636e802 | 2015-07-29 06:08:46 -0700 | [diff] [blame] | 1171 | } |
| 1172 | |
Khushal | 3e7548c | 2018-05-23 15:45:01 -0700 | [diff] [blame] | 1173 | bool GrContext::supportsDistanceFieldText() const { |
| 1174 | return fCaps->shaderCaps()->supportsDistanceFieldText(); |
| 1175 | } |
| 1176 | |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 1177 | ////////////////////////////////////////////////////////////////////////////// |
| 1178 | |
Robert Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 1179 | // DDL TODO: remove 'maxResources' |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 1180 | void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1181 | ASSERT_SINGLE_OWNER |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 1182 | if (maxResources) { |
| 1183 | *maxResources = fResourceCache->getMaxResourceCount(); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 1184 | } |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 1185 | if (maxResourceBytes) { |
| 1186 | *maxResourceBytes = fResourceCache->getMaxResourceBytes(); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 1187 | } |
| 1188 | } |
| 1189 | |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 1190 | void GrContext::setResourceCacheLimits(int maxResources, size_t maxResourceBytes) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1191 | ASSERT_SINGLE_OWNER |
Robert Phillips | 8d1e67e | 2017-12-04 13:48:14 -0500 | [diff] [blame] | 1192 | fResourceCache->setLimits(maxResources, maxResourceBytes); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 1193 | } |
| 1194 | |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 1195 | ////////////////////////////////////////////////////////////////////////////// |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 1196 | void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1197 | ASSERT_SINGLE_OWNER |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 1198 | fResourceCache->dumpMemoryStatistics(traceMemoryDump); |
Khushal | 71652e2 | 2018-10-29 13:05:36 -0700 | [diff] [blame] | 1199 | traceMemoryDump->dumpNumericValue("skia/gr_text_blob_cache", "size", "bytes", |
| 1200 | fTextBlobCache->usedBytes()); |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 1201 | } |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 1202 | |
| 1203 | ////////////////////////////////////////////////////////////////////////////// |
Kevin Lubick | f4def34 | 2018-10-04 12:52:50 -0400 | [diff] [blame] | 1204 | #ifdef SK_ENABLE_DUMP_GPU |
| 1205 | #include "SkJSONWriter.h" |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 1206 | SkString GrContextPriv::dump() const { |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 1207 | SkDynamicMemoryWStream stream; |
| 1208 | SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty); |
| 1209 | writer.beginObject(); |
| 1210 | |
| 1211 | static const char* kBackendStr[] = { |
| 1212 | "Metal", |
| 1213 | "OpenGL", |
| 1214 | "Vulkan", |
| 1215 | "Mock", |
| 1216 | }; |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 1217 | GR_STATIC_ASSERT(0 == (unsigned)GrBackendApi::kMetal); |
| 1218 | GR_STATIC_ASSERT(1 == (unsigned)GrBackendApi::kOpenGL); |
| 1219 | GR_STATIC_ASSERT(2 == (unsigned)GrBackendApi::kVulkan); |
| 1220 | GR_STATIC_ASSERT(3 == (unsigned)GrBackendApi::kMock); |
| 1221 | writer.appendString("backend", kBackendStr[(unsigned)fContext->fBackend]); |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 1222 | |
| 1223 | writer.appendName("caps"); |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 1224 | fContext->fCaps->dumpJSON(&writer); |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 1225 | |
| 1226 | writer.appendName("gpu"); |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 1227 | fContext->fGpu->dumpJSON(&writer); |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 1228 | |
| 1229 | // Flush JSON to the memory stream |
| 1230 | writer.endObject(); |
| 1231 | writer.flush(); |
| 1232 | |
| 1233 | // Null terminate the JSON data in the memory stream |
| 1234 | stream.write8(0); |
| 1235 | |
| 1236 | // Allocate a string big enough to hold all the data, then copy out of the stream |
| 1237 | SkString result(stream.bytesWritten()); |
| 1238 | stream.copyToAndReset(result.writable_str()); |
| 1239 | return result; |
| 1240 | } |
Kevin Lubick | f4def34 | 2018-10-04 12:52:50 -0400 | [diff] [blame] | 1241 | #endif |