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 | c65aec9 | 2017-03-09 09:03:58 -0500 | [diff] [blame] | 9 | #include "GrClip.h" |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 10 | #include "GrContextOptions.h" |
Brian Salomon | c65aec9 | 2017-03-09 09:03:58 -0500 | [diff] [blame] | 11 | #include "GrContextPriv.h" |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 12 | #include "GrDrawingManager.h" |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 13 | #include "GrRenderTargetContext.h" |
Brian Salomon | c65aec9 | 2017-03-09 09:03:58 -0500 | [diff] [blame] | 14 | #include "GrRenderTargetProxy.h" |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 15 | #include "GrResourceCache.h" |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 16 | #include "GrResourceProvider.h" |
Greg Daniel | d85f97d | 2017-03-07 13:37:21 -0500 | [diff] [blame] | 17 | #include "GrSemaphore.h" |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 18 | #include "GrSoftwarePathRenderer.h" |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 19 | #include "GrSurfaceContext.h" |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 20 | #include "GrSurfacePriv.h" |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 21 | #include "GrSurfaceProxyPriv.h" |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 22 | #include "GrTextureContext.h" |
Matt Sarett | 485c499 | 2017-02-14 14:18:27 -0500 | [diff] [blame] | 23 | #include "SkConvertPixels.h" |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 24 | #include "SkGr.h" |
Matt Sarett | c7b2908 | 2017-02-09 16:22:39 -0500 | [diff] [blame] | 25 | #include "SkUnPreMultiplyPriv.h" |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 26 | #include "effects/GrConfigConversionEffect.h" |
joshualitt | e804292 | 2015-12-11 06:11:21 -0800 | [diff] [blame] | 27 | #include "text/GrTextBlobCache.h" |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 28 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 29 | #define ASSERT_OWNED_PROXY(P) \ |
| 30 | SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == this) |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 31 | #define ASSERT_OWNED_PROXY_PRIV(P) \ |
| 32 | SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == fContext) |
| 33 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 34 | #define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this) |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 35 | #define ASSERT_SINGLE_OWNER \ |
| 36 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fSingleOwner);) |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 37 | #define ASSERT_SINGLE_OWNER_PRIV \ |
| 38 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fContext->fSingleOwner);) |
robertphillips | 7761d61 | 2016-05-16 09:14:53 -0700 | [diff] [blame] | 39 | #define RETURN_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return; } |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 40 | #define RETURN_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return; } |
robertphillips | 7761d61 | 2016-05-16 09:14:53 -0700 | [diff] [blame] | 41 | #define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return false; } |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 42 | #define RETURN_FALSE_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return false; } |
robertphillips | 7761d61 | 2016-05-16 09:14:53 -0700 | [diff] [blame] | 43 | #define RETURN_NULL_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return nullptr; } |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 44 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 45 | //////////////////////////////////////////////////////////////////////////////// |
| 46 | |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 47 | GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext) { |
| 48 | GrContextOptions defaultOptions; |
| 49 | return Create(backend, backendContext, defaultOptions); |
| 50 | } |
bsalomon | f28cff7 | 2015-05-22 12:25:41 -0700 | [diff] [blame] | 51 | |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 52 | GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext, |
| 53 | const GrContextOptions& options) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 54 | GrContext* context = new GrContext; |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 55 | |
| 56 | if (context->init(backend, backendContext, options)) { |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 57 | return context; |
| 58 | } else { |
| 59 | context->unref(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 60 | return nullptr; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 61 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 62 | } |
| 63 | |
joshualitt | 0acd0d3 | 2015-05-07 08:23:19 -0700 | [diff] [blame] | 64 | static int32_t gNextID = 1; |
| 65 | static int32_t next_id() { |
| 66 | int32_t id; |
| 67 | do { |
| 68 | id = sk_atomic_inc(&gNextID); |
| 69 | } while (id == SK_InvalidGenID); |
| 70 | return id; |
| 71 | } |
| 72 | |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 73 | GrContext::GrContext() : fUniqueID(next_id()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 74 | fGpu = nullptr; |
| 75 | fCaps = nullptr; |
| 76 | fResourceCache = nullptr; |
| 77 | fResourceProvider = nullptr; |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 78 | fAtlasGlyphCache = nullptr; |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 79 | } |
| 80 | |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 81 | bool GrContext::init(GrBackend backend, GrBackendContext backendContext, |
| 82 | const GrContextOptions& options) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 83 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 84 | SkASSERT(!fGpu); |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 85 | |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 86 | fGpu = GrGpu::Create(backend, backendContext, options, this); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 87 | if (!fGpu) { |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 88 | return false; |
| 89 | } |
bsalomon | 69cfe95 | 2015-11-30 13:27:47 -0800 | [diff] [blame] | 90 | this->initCommon(options); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 91 | return true; |
| 92 | } |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 93 | |
bsalomon | 69cfe95 | 2015-11-30 13:27:47 -0800 | [diff] [blame] | 94 | void GrContext::initCommon(const GrContextOptions& options) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 95 | ASSERT_SINGLE_OWNER |
| 96 | |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 97 | fCaps = SkRef(fGpu->caps()); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 98 | fResourceCache = new GrResourceCache(fCaps); |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 99 | fResourceProvider = new GrResourceProvider(fGpu, fResourceCache, &fSingleOwner); |
commit-bot@chromium.org | 1836d33 | 2013-07-16 22:55:03 +0000 | [diff] [blame] | 100 | |
Brian Osman | 46da1cc | 2017-02-14 14:15:48 -0500 | [diff] [blame] | 101 | fDisableGpuYUVConversion = options.fDisableGpuYUVConversion; |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 102 | fDidTestPMConversions = false; |
| 103 | |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 104 | GrRenderTargetOpList::Options rtOpListOptions; |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 105 | rtOpListOptions.fMaxOpCombineLookback = options.fMaxOpCombineLookback; |
| 106 | rtOpListOptions.fMaxOpCombineLookahead = options.fMaxOpCombineLookahead; |
bsalomon | 6b2552f | 2016-09-15 13:50:26 -0700 | [diff] [blame] | 107 | GrPathRendererChain::Options prcOptions; |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 108 | prcOptions.fAllowPathMaskCaching = options.fAllowPathMaskCaching; |
csmartdalton | 008b9d8 | 2017-02-22 12:00:42 -0700 | [diff] [blame] | 109 | prcOptions.fGpuPathRenderers = options.fGpuPathRenderers; |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 110 | fDrawingManager.reset(new GrDrawingManager(this, rtOpListOptions, prcOptions, |
| 111 | options.fImmediateMode, &fSingleOwner)); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 112 | |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 113 | fAtlasGlyphCache = new GrAtlasGlyphCache(this); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 114 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 115 | fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB, this)); |
bsalomon@google.com | c0af317 | 2012-06-15 14:10:09 +0000 | [diff] [blame] | 116 | } |
| 117 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 118 | GrContext::~GrContext() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 119 | ASSERT_SINGLE_OWNER |
| 120 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 121 | if (!fGpu) { |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 122 | SkASSERT(!fCaps); |
bsalomon@google.com | 733c062 | 2013-04-24 17:59:32 +0000 | [diff] [blame] | 123 | return; |
| 124 | } |
| 125 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 126 | this->flush(); |
robertphillips@google.com | 5acc0e3 | 2012-05-17 12:01:02 +0000 | [diff] [blame] | 127 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 128 | fDrawingManager->cleanup(); |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 129 | |
robertphillips@google.com | 950b1b0 | 2013-10-21 17:37:28 +0000 | [diff] [blame] | 130 | for (int i = 0; i < fCleanUpData.count(); ++i) { |
| 131 | (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo); |
| 132 | } |
| 133 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 134 | delete fResourceProvider; |
| 135 | delete fResourceCache; |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 136 | delete fAtlasGlyphCache; |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 137 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 138 | fGpu->unref(); |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 139 | fCaps->unref(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 140 | } |
| 141 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 142 | sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() { |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 143 | if (!fThreadSafeProxy) { |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 144 | fThreadSafeProxy.reset(new GrContextThreadSafeProxy(sk_ref_sp(fCaps), this->uniqueID())); |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 145 | } |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 146 | return fThreadSafeProxy; |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 147 | } |
| 148 | |
bsalomon | 2354f84 | 2014-07-28 13:48:36 -0700 | [diff] [blame] | 149 | void GrContext::abandonContext() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 150 | ASSERT_SINGLE_OWNER |
| 151 | |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 152 | fResourceProvider->abandon(); |
robertphillips | 0dfa62c | 2015-11-16 06:23:31 -0800 | [diff] [blame] | 153 | |
| 154 | // Need to abandon the drawing manager first so all the render targets |
| 155 | // will be released/forgotten before they too are abandoned. |
| 156 | fDrawingManager->abandon(); |
| 157 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 158 | // abandon first to so destructors |
| 159 | // don't try to free the resources in the API. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 160 | fResourceCache->abandonAll(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 161 | |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 162 | fGpu->disconnect(GrGpu::DisconnectType::kAbandon); |
| 163 | |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 164 | fAtlasGlyphCache->freeAll(); |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 165 | fTextBlobCache->freeAll(); |
| 166 | } |
| 167 | |
| 168 | void GrContext::releaseResourcesAndAbandonContext() { |
| 169 | ASSERT_SINGLE_OWNER |
| 170 | |
| 171 | fResourceProvider->abandon(); |
| 172 | |
| 173 | // Need to abandon the drawing manager first so all the render targets |
| 174 | // will be released/forgotten before they too are abandoned. |
| 175 | fDrawingManager->abandon(); |
| 176 | |
| 177 | // Release all resources in the backend 3D API. |
| 178 | fResourceCache->releaseAll(); |
| 179 | |
| 180 | fGpu->disconnect(GrGpu::DisconnectType::kCleanup); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 181 | |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 182 | fAtlasGlyphCache->freeAll(); |
joshualitt | 26ffc00 | 2015-04-16 11:24:04 -0700 | [diff] [blame] | 183 | fTextBlobCache->freeAll(); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 184 | } |
| 185 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 186 | void GrContext::resetContext(uint32_t state) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 187 | ASSERT_SINGLE_OWNER |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 188 | fGpu->markContextDirty(state); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | void GrContext::freeGpuResources() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 192 | ASSERT_SINGLE_OWNER |
| 193 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 194 | this->flush(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 195 | |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 196 | fAtlasGlyphCache->freeAll(); |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 197 | |
| 198 | fDrawingManager->freeGpuResources(); |
bsalomon | 3033b9f | 2015-04-13 11:09:56 -0700 | [diff] [blame] | 199 | |
| 200 | fResourceCache->purgeAllUnlocked(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 203 | void GrContext::purgeResourcesNotUsedInMs(std::chrono::milliseconds ms) { |
| 204 | ASSERT_SINGLE_OWNER |
| 205 | fResourceCache->purgeResourcesNotUsedSince(GrStdSteadyClock::now() - ms); |
| 206 | } |
| 207 | |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 208 | void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 209 | ASSERT_SINGLE_OWNER |
| 210 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 211 | if (resourceCount) { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 212 | *resourceCount = fResourceCache->getBudgetedResourceCount(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 213 | } |
| 214 | if (resourceBytes) { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 215 | *resourceBytes = fResourceCache->getBudgetedResourceBytes(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 216 | } |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 217 | } |
| 218 | |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 219 | //////////////////////////////////////////////////////////////////////////////// |
| 220 | |
joshualitt | 0db6dfa | 2015-04-10 07:01:30 -0700 | [diff] [blame] | 221 | void GrContext::TextBlobCacheOverBudgetCB(void* data) { |
| 222 | SkASSERT(data); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 223 | // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on |
| 224 | // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls |
| 225 | // to below the GrContext level, but this is not trivial because they call drawPath on |
| 226 | // SkGpuDevice. |
joshualitt | 0db6dfa | 2015-04-10 07:01:30 -0700 | [diff] [blame] | 227 | GrContext* context = reinterpret_cast<GrContext*>(data); |
| 228 | context->flush(); |
| 229 | } |
| 230 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 231 | //////////////////////////////////////////////////////////////////////////////// |
| 232 | |
bsalomon | b77a907 | 2016-09-07 10:02:04 -0700 | [diff] [blame] | 233 | void GrContext::flush() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 234 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 235 | RETURN_IF_ABANDONED |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 236 | |
| 237 | fDrawingManager->flush(nullptr); |
| 238 | } |
| 239 | |
| 240 | void GrContextPriv::flush(GrSurfaceProxy* proxy) { |
| 241 | ASSERT_SINGLE_OWNER_PRIV |
| 242 | RETURN_IF_ABANDONED_PRIV |
| 243 | ASSERT_OWNED_PROXY_PRIV(proxy); |
| 244 | |
| 245 | fContext->fDrawingManager->flush(proxy); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 246 | } |
| 247 | |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 248 | bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes, |
| 249 | const void* inPixels, size_t outRowBytes, void* outPixels) { |
Matt Sarett | c7b2908 | 2017-02-09 16:22:39 -0500 | [diff] [blame] | 250 | SkColorType colorType; |
| 251 | if (!GrPixelConfigToColorType(srcConfig, &colorType) || |
| 252 | 4 != SkColorTypeBytesPerPixel(colorType)) |
| 253 | { |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 254 | return false; |
| 255 | } |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 256 | |
Matt Sarett | c7b2908 | 2017-02-09 16:22:39 -0500 | [diff] [blame] | 257 | for (int y = 0; y < height; y++) { |
| 258 | SkOpts::RGBA_to_rgbA((uint32_t*) outPixels, inPixels, width); |
| 259 | outPixels = SkTAddOffset<void>(outPixels, outRowBytes); |
| 260 | inPixels = SkTAddOffset<const void>(inPixels, inRowBytes); |
| 261 | } |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 262 | |
Matt Sarett | c7b2908 | 2017-02-09 16:22:39 -0500 | [diff] [blame] | 263 | return true; |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 266 | static bool valid_unpremul_config(GrPixelConfig config) { |
| 267 | return GrPixelConfigIs8888Unorm(config) || kRGBA_half_GrPixelConfig == config; |
| 268 | } |
| 269 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 270 | bool GrContextPriv::writeSurfacePixels(GrSurfaceProxy* srcProxy, SkColorSpace* dstColorSpace, |
| 271 | int left, int top, int width, int height, |
| 272 | GrPixelConfig srcConfig, SkColorSpace* srcColorSpace, |
| 273 | const void* buffer, size_t rowBytes, |
| 274 | uint32_t pixelOpsFlags) { |
Brian Osman | b62ea22 | 2016-12-22 11:12:16 -0500 | [diff] [blame] | 275 | // TODO: Color space conversion |
| 276 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 277 | ASSERT_SINGLE_OWNER_PRIV |
| 278 | RETURN_FALSE_IF_ABANDONED_PRIV |
| 279 | ASSERT_OWNED_PROXY_PRIV(srcProxy); |
| 280 | SkASSERT(srcProxy); |
| 281 | GR_AUDIT_TRAIL_AUTO_FRAME(&fContext->fAuditTrail, "GrContextPriv::writeSurfacePixels"); |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 282 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 283 | GrSurface* surface = srcProxy->instantiate(fContext->resourceProvider()); |
| 284 | if (!surface) { |
| 285 | return false; |
| 286 | } |
| 287 | |
| 288 | fContext->testPMConversionsIfNecessary(pixelOpsFlags); |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 289 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 290 | // 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] | 291 | // necessary and because GrGpu::getWritePixelsInfo requires it. |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 292 | if (!GrSurfacePriv::AdjustWritePixelParams(surface->width(), surface->height(), |
| 293 | GrBytesPerPixel(srcConfig), &left, &top, &width, |
| 294 | &height, &buffer, &rowBytes)) { |
| 295 | return false; |
| 296 | } |
| 297 | |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 298 | bool applyPremulToSrc = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags); |
| 299 | if (applyPremulToSrc && !valid_unpremul_config(srcConfig)) { |
| 300 | return false; |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 301 | } |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 302 | // We don't allow conversion between integer configs and float/fixed configs. |
| 303 | if (GrPixelConfigIsSint(surface->config()) != GrPixelConfigIsSint(srcConfig)) { |
| 304 | return false; |
| 305 | } |
bsalomon | 636e802 | 2015-07-29 06:08:46 -0700 | [diff] [blame] | 306 | |
| 307 | GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference; |
| 308 | // Don't prefer to draw for the conversion (and thereby access a texture from the cache) when |
| 309 | // we've already determined that there isn't a roundtrip preserving conversion processor pair. |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 310 | if (applyPremulToSrc && fContext->validPMUPMConversionExists(srcConfig)) { |
bsalomon | 636e802 | 2015-07-29 06:08:46 -0700 | [diff] [blame] | 311 | drawPreference = GrGpu::kCallerPrefersDraw_DrawPreference; |
| 312 | } |
| 313 | |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 314 | GrGpu::WritePixelTempDrawInfo tempDrawInfo; |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 315 | if (!fContext->fGpu->getWritePixelsInfo(surface, width, height, srcConfig, |
| 316 | &drawPreference, &tempDrawInfo)) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 317 | return false; |
| 318 | } |
| 319 | |
| 320 | if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && surface->surfacePriv().hasPendingIO()) { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 321 | this->flush(nullptr); // MDB TODO: tighten this |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 324 | sk_sp<GrTextureProxy> tempProxy; |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 325 | if (GrGpu::kNoDraw_DrawPreference != drawPreference) { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 326 | tempProxy = GrSurfaceProxy::MakeDeferred(fContext->resourceProvider(), |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 327 | tempDrawInfo.fTempSurfaceDesc, |
| 328 | SkBackingFit::kApprox, |
| 329 | SkBudgeted::kYes); |
| 330 | if (!tempProxy && GrGpu::kRequireDraw_DrawPreference == drawPreference) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 331 | return false; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | // temp buffer for doing sw premul conversion, if needed. |
| 336 | SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0); |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 337 | if (tempProxy) { |
Brian Osman | 60cd57e | 2017-04-06 10:19:06 -0400 | [diff] [blame] | 338 | sk_sp<GrFragmentProcessor> texFP = GrSimpleTextureEffect::Make( |
| 339 | fContext->resourceProvider(), tempProxy, nullptr, SkMatrix::I()); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 340 | sk_sp<GrFragmentProcessor> fp; |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 341 | if (applyPremulToSrc) { |
Brian Osman | 60cd57e | 2017-04-06 10:19:06 -0400 | [diff] [blame] | 342 | fp = fContext->createUPMToPMEffect(texFP, tempProxy->config()); |
| 343 | if (fp) { |
| 344 | // We no longer need to do this on CPU before the upload. |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 345 | applyPremulToSrc = false; |
Brian Osman | 60cd57e | 2017-04-06 10:19:06 -0400 | [diff] [blame] | 346 | } else if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) { |
| 347 | // We only wanted to do the draw to perform the premul so don't bother. |
| 348 | tempProxy.reset(nullptr); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 349 | } |
| 350 | } |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 351 | if (tempProxy) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 352 | if (!fp) { |
Brian Osman | 60cd57e | 2017-04-06 10:19:06 -0400 | [diff] [blame] | 353 | fp = std::move(texFP); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 354 | } |
Brian Osman | 60cd57e | 2017-04-06 10:19:06 -0400 | [diff] [blame] | 355 | fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle); |
| 356 | SkASSERT(fp); |
| 357 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 358 | if (tempProxy->priv().hasPendingIO()) { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 359 | this->flush(tempProxy.get()); |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 360 | } |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 361 | GrTexture* texture = tempProxy->instantiate(fContext->resourceProvider()); |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 362 | if (!texture) { |
| 363 | return false; |
| 364 | } |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 365 | if (applyPremulToSrc) { |
| 366 | size_t tmpRowBytes = 4 * width; |
| 367 | tmpPixels.reset(width * height); |
| 368 | if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes, |
| 369 | tmpPixels.get())) { |
| 370 | return false; |
| 371 | } |
| 372 | rowBytes = tmpRowBytes; |
| 373 | buffer = tmpPixels.get(); |
| 374 | applyPremulToSrc = false; |
| 375 | } |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 376 | if (!fContext->fGpu->writePixels(texture, 0, 0, width, height, |
| 377 | tempDrawInfo.fWriteConfig, buffer, |
| 378 | rowBytes)) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 379 | return false; |
| 380 | } |
| 381 | SkMatrix matrix; |
| 382 | matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top)); |
brianosman | dfe4f2e | 2016-07-21 13:28:36 -0700 | [diff] [blame] | 383 | // TODO: Need to decide the semantics of this function for color spaces. Do we support |
| 384 | // conversion from a passed-in color space? For now, specifying nullptr means that this |
| 385 | // path will do no conversion, so it will match the behavior of the non-draw path. |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 386 | GrRenderTarget* renderTarget = surface->asRenderTarget(); |
| 387 | SkASSERT(renderTarget); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 388 | sk_sp<GrRenderTargetContext> renderTargetContext( |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 389 | this->makeWrappedRenderTargetContext(sk_ref_sp(renderTarget), nullptr)); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 390 | if (!renderTargetContext) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 391 | return false; |
| 392 | } |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 393 | GrPaint paint; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 394 | paint.addColorFragmentProcessor(std::move(fp)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 395 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
brianosman | a167e74 | 2016-05-24 06:18:48 -0700 | [diff] [blame] | 396 | paint.setAllowSRGBInputs(true); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 397 | SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 398 | renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, matrix, rect, |
| 399 | nullptr); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 400 | |
| 401 | if (kFlushWrites_PixelOp & pixelOpsFlags) { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 402 | this->flushSurfaceWrites(renderTargetContext->asRenderTargetProxy()); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | } |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 406 | if (!tempProxy) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 407 | if (applyPremulToSrc) { |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 408 | size_t tmpRowBytes = 4 * width; |
| 409 | tmpPixels.reset(width * height); |
| 410 | if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes, |
| 411 | tmpPixels.get())) { |
| 412 | return false; |
| 413 | } |
| 414 | rowBytes = tmpRowBytes; |
| 415 | buffer = tmpPixels.get(); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 416 | applyPremulToSrc = false; |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 417 | } |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 418 | return fContext->fGpu->writePixels(surface, left, top, width, height, srcConfig, |
| 419 | buffer, rowBytes); |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 420 | } |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 421 | return true; |
| 422 | } |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 423 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 424 | bool GrContextPriv::readSurfacePixels(GrSurfaceProxy* srcProxy, SkColorSpace* srcColorSpace, |
| 425 | int left, int top, int width, int height, |
| 426 | GrPixelConfig dstConfig, SkColorSpace* dstColorSpace, |
| 427 | void* buffer, size_t rowBytes, uint32_t flags) { |
Brian Osman | b62ea22 | 2016-12-22 11:12:16 -0500 | [diff] [blame] | 428 | // TODO: Color space conversion |
| 429 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 430 | ASSERT_SINGLE_OWNER_PRIV |
| 431 | RETURN_FALSE_IF_ABANDONED_PRIV |
| 432 | ASSERT_OWNED_PROXY_PRIV(srcProxy); |
| 433 | SkASSERT(srcProxy); |
| 434 | GR_AUDIT_TRAIL_AUTO_FRAME(&fContext->fAuditTrail, "GrContextPriv::readSurfacePixels"); |
bsalomon | 32ab260 | 2015-09-09 18:57:49 -0700 | [diff] [blame] | 435 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 436 | // MDB TODO: delay this instantiation until later in the method |
| 437 | GrSurface* src = srcProxy->instantiate(fContext->resourceProvider()); |
| 438 | if (!src) { |
| 439 | return false; |
| 440 | } |
| 441 | |
| 442 | fContext->testPMConversionsIfNecessary(flags); |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 443 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 444 | // Adjust the params so that if we wind up using an intermediate surface we've already done |
| 445 | // all the trimming and the temporary can be the min size required. |
| 446 | if (!GrSurfacePriv::AdjustReadPixelParams(src->width(), src->height(), |
| 447 | GrBytesPerPixel(dstConfig), &left, |
| 448 | &top, &width, &height, &buffer, &rowBytes)) { |
| 449 | return false; |
| 450 | } |
| 451 | |
| 452 | if (!(kDontFlush_PixelOpsFlag & flags) && src->surfacePriv().hasPendingWrite()) { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 453 | this->flush(nullptr); // MDB TODO: tighten this |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 454 | } |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 455 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 456 | bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags); |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 457 | if (unpremul && !valid_unpremul_config(dstConfig)) { |
Brian Osman | de1a605 | 2017-03-22 10:57:00 -0400 | [diff] [blame] | 458 | // The unpremul flag is only allowed for 8888 and F16 configs. |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 459 | return false; |
| 460 | } |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 461 | // We don't allow conversion between integer configs and float/fixed configs. |
| 462 | if (GrPixelConfigIsSint(src->config()) != GrPixelConfigIsSint(dstConfig)) { |
| 463 | return false; |
| 464 | } |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 465 | |
bsalomon | 636e802 | 2015-07-29 06:08:46 -0700 | [diff] [blame] | 466 | GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference; |
| 467 | // Don't prefer to draw for the conversion (and thereby access a texture from the cache) when |
| 468 | // we've already determined that there isn't a roundtrip preserving conversion processor pair. |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 469 | if (unpremul && fContext->validPMUPMConversionExists(src->config())) { |
bsalomon | 636e802 | 2015-07-29 06:08:46 -0700 | [diff] [blame] | 470 | drawPreference = GrGpu::kCallerPrefersDraw_DrawPreference; |
| 471 | } |
| 472 | |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 473 | GrGpu::ReadPixelTempDrawInfo tempDrawInfo; |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 474 | if (!fContext->fGpu->getReadPixelsInfo(src, width, height, rowBytes, dstConfig, |
| 475 | &drawPreference, &tempDrawInfo)) { |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 476 | return false; |
| 477 | } |
bsalomon | 191bcc0 | 2014-11-14 11:31:13 -0800 | [diff] [blame] | 478 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 479 | sk_sp<GrSurfaceProxy> proxyToRead = sk_ref_sp(srcProxy); |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 480 | bool didTempDraw = false; |
| 481 | if (GrGpu::kNoDraw_DrawPreference != drawPreference) { |
bsalomon | b117ff1 | 2016-07-19 07:24:40 -0700 | [diff] [blame] | 482 | if (SkBackingFit::kExact == tempDrawInfo.fTempSurfaceFit) { |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 483 | // We only respect this when the entire src is being read. Otherwise we can trigger too |
| 484 | // many odd ball texture sizes and trash the cache. |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 485 | if (width != src->width() || height != src->height()) { |
bsalomon | b117ff1 | 2016-07-19 07:24:40 -0700 | [diff] [blame] | 486 | tempDrawInfo.fTempSurfaceFit= SkBackingFit::kApprox; |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 487 | } |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 488 | } |
brianosman | dfe4f2e | 2016-07-21 13:28:36 -0700 | [diff] [blame] | 489 | // TODO: Need to decide the semantics of this function for color spaces. Do we support |
| 490 | // conversion to a passed-in color space? For now, specifying nullptr means that this |
| 491 | // path will do no conversion, so it will match the behavior of the non-draw path. |
Robert Phillips | d83ec04 | 2017-04-10 11:10:22 -0400 | [diff] [blame^] | 492 | sk_sp<GrRenderTargetContext> tempRTC = fContext->makeDeferredRenderTargetContext( |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 493 | tempDrawInfo.fTempSurfaceFit, |
bsalomon | b117ff1 | 2016-07-19 07:24:40 -0700 | [diff] [blame] | 494 | tempDrawInfo.fTempSurfaceDesc.fWidth, |
| 495 | tempDrawInfo.fTempSurfaceDesc.fHeight, |
| 496 | tempDrawInfo.fTempSurfaceDesc.fConfig, |
brianosman | dfe4f2e | 2016-07-21 13:28:36 -0700 | [diff] [blame] | 497 | nullptr, |
bsalomon | b117ff1 | 2016-07-19 07:24:40 -0700 | [diff] [blame] | 498 | tempDrawInfo.fTempSurfaceDesc.fSampleCnt, |
| 499 | tempDrawInfo.fTempSurfaceDesc.fOrigin); |
Brian Osman | 693a540 | 2016-10-27 15:13:22 -0400 | [diff] [blame] | 500 | if (tempRTC) { |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 501 | SkMatrix textureMatrix = SkMatrix::MakeTrans(SkIntToScalar(left), SkIntToScalar(top)); |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 502 | sk_sp<GrTextureProxy> proxy = sk_ref_sp(srcProxy->asTextureProxy()); |
Brian Osman | 60cd57e | 2017-04-06 10:19:06 -0400 | [diff] [blame] | 503 | sk_sp<GrFragmentProcessor> texFP = GrSimpleTextureEffect::Make( |
| 504 | fContext->resourceProvider(), proxy, nullptr, textureMatrix); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 505 | sk_sp<GrFragmentProcessor> fp; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 506 | if (unpremul) { |
Brian Osman | 60cd57e | 2017-04-06 10:19:06 -0400 | [diff] [blame] | 507 | fp = fContext->createPMToUPMEffect(texFP, proxy->config()); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 508 | if (fp) { |
Brian Osman | 60cd57e | 2017-04-06 10:19:06 -0400 | [diff] [blame] | 509 | // We no longer need to do this on CPU after the read back. |
| 510 | unpremul = false; |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 511 | } else if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) { |
Brian Osman | 60cd57e | 2017-04-06 10:19:06 -0400 | [diff] [blame] | 512 | // We only wanted to do the draw to perform the unpremul so don't bother. |
Brian Osman | 693a540 | 2016-10-27 15:13:22 -0400 | [diff] [blame] | 513 | tempRTC.reset(nullptr); |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 514 | } |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 515 | } |
Brian Osman | 60cd57e | 2017-04-06 10:19:06 -0400 | [diff] [blame] | 516 | if (tempRTC) { |
| 517 | if (!fp) { |
| 518 | fp = std::move(texFP); |
| 519 | } |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 520 | fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle); |
Brian Osman | 60cd57e | 2017-04-06 10:19:06 -0400 | [diff] [blame] | 521 | SkASSERT(fp); |
| 522 | |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 523 | GrPaint paint; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 524 | paint.addColorFragmentProcessor(std::move(fp)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 525 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
brianosman | a167e74 | 2016-05-24 06:18:48 -0700 | [diff] [blame] | 526 | paint.setAllowSRGBInputs(true); |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 527 | SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 528 | tempRTC->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect, |
| 529 | nullptr); |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 530 | proxyToRead = tempRTC->asTextureProxyRef(); |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 531 | left = 0; |
| 532 | top = 0; |
| 533 | didTempDraw = true; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 534 | } |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 535 | } |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 536 | } |
joshualitt | 5c55fef | 2014-10-31 14:04:35 -0700 | [diff] [blame] | 537 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 538 | if (!proxyToRead) { |
| 539 | return false; |
| 540 | } |
| 541 | |
| 542 | GrSurface* surfaceToRead = proxyToRead->instantiate(fContext->resourceProvider()); |
Robert Phillips | 833dcf4 | 2016-11-18 08:44:13 -0500 | [diff] [blame] | 543 | if (!surfaceToRead) { |
| 544 | return false; |
| 545 | } |
| 546 | |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 547 | if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) { |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 548 | return false; |
| 549 | } |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 550 | GrPixelConfig configToRead = dstConfig; |
| 551 | if (didTempDraw) { |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 552 | this->flushSurfaceWrites(proxyToRead.get()); |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 553 | configToRead = tempDrawInfo.fReadConfig; |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 554 | } |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 555 | if (!fContext->fGpu->readPixels(surfaceToRead, left, top, width, height, configToRead, |
| 556 | buffer, rowBytes)) { |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 557 | return false; |
| 558 | } |
| 559 | |
| 560 | // Perform umpremul conversion if we weren't able to perform it as a draw. |
| 561 | if (unpremul) { |
Matt Sarett | c7b2908 | 2017-02-09 16:22:39 -0500 | [diff] [blame] | 562 | SkColorType colorType; |
| 563 | if (!GrPixelConfigToColorType(dstConfig, &colorType) || |
| 564 | 4 != SkColorTypeBytesPerPixel(colorType)) |
| 565 | { |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 566 | return false; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 567 | } |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 568 | |
Matt Sarett | c7b2908 | 2017-02-09 16:22:39 -0500 | [diff] [blame] | 569 | for (int y = 0; y < height; y++) { |
| 570 | SkUnpremultiplyRow<false>((uint32_t*) buffer, (const uint32_t*) buffer, width); |
| 571 | buffer = SkTAddOffset<void>(buffer, rowBytes); |
| 572 | } |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 573 | } |
| 574 | return true; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 577 | void GrContextPriv::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) { |
| 578 | ASSERT_SINGLE_OWNER_PRIV |
| 579 | RETURN_IF_ABANDONED_PRIV |
| 580 | SkASSERT(proxy); |
| 581 | ASSERT_OWNED_PROXY_PRIV(proxy); |
| 582 | fContext->fDrawingManager->prepareSurfaceForExternalIO(proxy); |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 585 | void GrContextPriv::flushSurfaceWrites(GrSurfaceProxy* proxy) { |
| 586 | ASSERT_SINGLE_OWNER_PRIV |
| 587 | RETURN_IF_ABANDONED_PRIV |
| 588 | SkASSERT(proxy); |
| 589 | ASSERT_OWNED_PROXY_PRIV(proxy); |
| 590 | if (proxy->priv().hasPendingWrite()) { |
| 591 | this->flush(proxy); |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 592 | } |
| 593 | } |
| 594 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 595 | void GrContextPriv::flushSurfaceIO(GrSurfaceProxy* proxy) { |
| 596 | ASSERT_SINGLE_OWNER_PRIV |
| 597 | RETURN_IF_ABANDONED_PRIV |
| 598 | SkASSERT(proxy); |
| 599 | ASSERT_OWNED_PROXY_PRIV(proxy); |
| 600 | if (proxy->priv().hasPendingIO()) { |
| 601 | this->flush(proxy); |
ajuma | 95243eb | 2016-08-24 08:19:02 -0700 | [diff] [blame] | 602 | } |
| 603 | } |
| 604 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 605 | //////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | b471a32 | 2014-03-10 07:40:03 +0000 | [diff] [blame] | 606 | int GrContext::getRecommendedSampleCount(GrPixelConfig config, |
| 607 | SkScalar dpi) const { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 608 | ASSERT_SINGLE_OWNER |
| 609 | |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 610 | if (!this->caps()->isConfigRenderable(config, true)) { |
commit-bot@chromium.org | b471a32 | 2014-03-10 07:40:03 +0000 | [diff] [blame] | 611 | return 0; |
| 612 | } |
| 613 | int chosenSampleCount = 0; |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 614 | if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) { |
commit-bot@chromium.org | b471a32 | 2014-03-10 07:40:03 +0000 | [diff] [blame] | 615 | if (dpi >= 250.0f) { |
| 616 | chosenSampleCount = 4; |
| 617 | } else { |
| 618 | chosenSampleCount = 16; |
| 619 | } |
| 620 | } |
egdaniel | eed519e | 2016-01-15 11:36:18 -0800 | [diff] [blame] | 621 | return chosenSampleCount <= fGpu->caps()->maxSampleCount() ? chosenSampleCount : 0; |
commit-bot@chromium.org | b471a32 | 2014-03-10 07:40:03 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 624 | sk_sp<GrRenderTargetContext> GrContextPriv::makeWrappedRenderTargetContext( |
| 625 | sk_sp<GrRenderTarget> rt, |
| 626 | sk_sp<SkColorSpace> colorSpace, |
| 627 | const SkSurfaceProps* surfaceProps) { |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 628 | ASSERT_SINGLE_OWNER_PRIV |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 629 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 630 | sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(rt))); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 631 | if (!proxy) { |
| 632 | return nullptr; |
| 633 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 634 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 635 | return this->drawingManager()->makeRenderTargetContext(std::move(proxy), |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 636 | std::move(colorSpace), |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 637 | surfaceProps); |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 638 | } |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 639 | |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 640 | sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy, |
| 641 | sk_sp<SkColorSpace> colorSpace) { |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 642 | ASSERT_SINGLE_OWNER_PRIV |
| 643 | |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 644 | if (proxy->asRenderTargetProxy()) { |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 645 | return this->drawingManager()->makeRenderTargetContext(std::move(proxy), |
| 646 | std::move(colorSpace), nullptr); |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 647 | } else { |
| 648 | SkASSERT(proxy->asTextureProxy()); |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 649 | return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace)); |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | |
Robert Phillips | 31c2608 | 2016-12-14 15:12:15 -0500 | [diff] [blame] | 653 | sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurface> surface) { |
| 654 | ASSERT_SINGLE_OWNER_PRIV |
| 655 | |
| 656 | sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface))); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 657 | if (!proxy) { |
| 658 | return nullptr; |
| 659 | } |
Robert Phillips | 31c2608 | 2016-12-14 15:12:15 -0500 | [diff] [blame] | 660 | |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 661 | return this->makeWrappedSurfaceContext(std::move(proxy), nullptr); |
Robert Phillips | 31c2608 | 2016-12-14 15:12:15 -0500 | [diff] [blame] | 662 | } |
| 663 | |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 664 | sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrSurfaceDesc& dstDesc, |
| 665 | SkBackingFit fit, |
| 666 | SkBudgeted isDstBudgeted) { |
| 667 | |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 668 | sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeDeferred(fContext->resourceProvider(), |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 669 | dstDesc, fit, isDstBudgeted); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 670 | if (!proxy) { |
| 671 | return nullptr; |
| 672 | } |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 673 | |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 674 | return this->makeWrappedSurfaceContext(std::move(proxy), nullptr); |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 675 | } |
| 676 | |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 677 | sk_sp<GrSurfaceContext> GrContextPriv::makeBackendSurfaceContext(const GrBackendTextureDesc& desc, |
Brian Osman | c1e3705 | 2017-03-09 14:19:20 -0500 | [diff] [blame] | 678 | sk_sp<SkColorSpace> colorSpace) { |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 679 | ASSERT_SINGLE_OWNER_PRIV |
| 680 | |
Brian Osman | c1e3705 | 2017-03-09 14:19:20 -0500 | [diff] [blame] | 681 | sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTexture(desc)); |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 682 | if (!surface) { |
| 683 | return nullptr; |
| 684 | } |
| 685 | |
| 686 | sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface))); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 687 | if (!proxy) { |
| 688 | return nullptr; |
| 689 | } |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 690 | |
| 691 | return this->makeWrappedSurfaceContext(std::move(proxy), std::move(colorSpace)); |
| 692 | } |
| 693 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 694 | sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext( |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 695 | const GrBackendTextureDesc& desc, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 696 | sk_sp<SkColorSpace> colorSpace, |
Brian Osman | c1e3705 | 2017-03-09 14:19:20 -0500 | [diff] [blame] | 697 | const SkSurfaceProps* props) { |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 698 | ASSERT_SINGLE_OWNER_PRIV |
| 699 | SkASSERT(desc.fFlags & kRenderTarget_GrBackendTextureFlag); |
| 700 | |
Brian Osman | c1e3705 | 2017-03-09 14:19:20 -0500 | [diff] [blame] | 701 | sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTexture(desc)); |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 702 | if (!surface) { |
| 703 | return nullptr; |
| 704 | } |
| 705 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 706 | sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface))); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 707 | if (!proxy) { |
| 708 | return nullptr; |
| 709 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 710 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 711 | return this->drawingManager()->makeRenderTargetContext(std::move(proxy), |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 712 | std::move(colorSpace), props); |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 713 | } |
| 714 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 715 | sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext( |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 716 | const GrBackendRenderTargetDesc& desc, |
| 717 | sk_sp<SkColorSpace> colorSpace, |
| 718 | const SkSurfaceProps* surfaceProps) { |
| 719 | ASSERT_SINGLE_OWNER_PRIV |
| 720 | |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 721 | sk_sp<GrRenderTarget> rt(fContext->resourceProvider()->wrapBackendRenderTarget(desc)); |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 722 | if (!rt) { |
| 723 | return nullptr; |
| 724 | } |
| 725 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 726 | sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(rt))); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 727 | if (!proxy) { |
| 728 | return nullptr; |
| 729 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 730 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 731 | return this->drawingManager()->makeRenderTargetContext(std::move(proxy), |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 732 | std::move(colorSpace), |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 733 | surfaceProps); |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 734 | } |
| 735 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 736 | sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext( |
egdaniel | a95d46b | 2016-08-15 08:06:29 -0700 | [diff] [blame] | 737 | const GrBackendTextureDesc& desc, |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 738 | sk_sp<SkColorSpace> colorSpace, |
| 739 | const SkSurfaceProps* surfaceProps) { |
| 740 | ASSERT_SINGLE_OWNER_PRIV |
| 741 | SkASSERT(desc.fFlags & kRenderTarget_GrBackendTextureFlag); |
| 742 | |
| 743 | sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTextureAsRenderTarget(desc)); |
| 744 | if (!surface) { |
| 745 | return nullptr; |
| 746 | } |
| 747 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 748 | sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface))); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 749 | if (!proxy) { |
| 750 | return nullptr; |
| 751 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 752 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 753 | return this->drawingManager()->makeRenderTargetContext(std::move(proxy), |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 754 | std::move(colorSpace), |
| 755 | surfaceProps); |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 756 | } |
| 757 | |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 758 | void GrContextPriv::addPreFlushCallbackObject(sk_sp<GrPreFlushCallbackObject> preFlushCBObject) { |
| 759 | fContext->fDrawingManager->addPreFlushCallbackObject(std::move(preFlushCBObject)); |
| 760 | } |
| 761 | |
| 762 | |
robertphillips | 48fde9c | 2016-09-06 05:20:20 -0700 | [diff] [blame] | 763 | static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) { |
Brian Osman | 78f20e0 | 2017-01-12 10:28:01 -0500 | [diff] [blame] | 764 | switch (config) { |
| 765 | case kAlpha_8_GrPixelConfig: |
| 766 | case kRGB_565_GrPixelConfig: |
| 767 | case kRGBA_4444_GrPixelConfig: |
| 768 | case kBGRA_8888_GrPixelConfig: |
| 769 | return kRGBA_8888_GrPixelConfig; |
| 770 | case kSBGRA_8888_GrPixelConfig: |
| 771 | return kSRGBA_8888_GrPixelConfig; |
| 772 | case kAlpha_half_GrPixelConfig: |
| 773 | return kRGBA_half_GrPixelConfig; |
| 774 | default: |
| 775 | return kUnknown_GrPixelConfig; |
| 776 | } |
robertphillips | 48fde9c | 2016-09-06 05:20:20 -0700 | [diff] [blame] | 777 | } |
| 778 | |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 779 | sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContextWithFallback( |
| 780 | SkBackingFit fit, |
| 781 | int width, int height, |
| 782 | GrPixelConfig config, |
| 783 | sk_sp<SkColorSpace> colorSpace, |
| 784 | int sampleCnt, |
| 785 | GrSurfaceOrigin origin, |
| 786 | const SkSurfaceProps* surfaceProps, |
| 787 | SkBudgeted budgeted) { |
| 788 | if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) { |
| 789 | config = GrPixelConfigFallback(config); |
| 790 | } |
| 791 | |
| 792 | return this->makeDeferredRenderTargetContext(fit, width, height, config, std::move(colorSpace), |
| 793 | sampleCnt, origin, surfaceProps, budgeted); |
| 794 | } |
| 795 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 796 | sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContext( |
| 797 | SkBackingFit fit, |
| 798 | int width, int height, |
| 799 | GrPixelConfig config, |
| 800 | sk_sp<SkColorSpace> colorSpace, |
| 801 | int sampleCnt, |
| 802 | GrSurfaceOrigin origin, |
| 803 | const SkSurfaceProps* surfaceProps, |
| 804 | SkBudgeted budgeted) { |
Robert Phillips | d83ec04 | 2017-04-10 11:10:22 -0400 | [diff] [blame^] | 805 | SkASSERT(kDefault_GrSurfaceOrigin != origin); |
| 806 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 807 | GrSurfaceDesc desc; |
| 808 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 809 | desc.fOrigin = origin; |
| 810 | desc.fWidth = width; |
| 811 | desc.fHeight = height; |
| 812 | desc.fConfig = config; |
| 813 | desc.fSampleCnt = sampleCnt; |
| 814 | |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 815 | sk_sp<GrTextureProxy> rtp = GrSurfaceProxy::MakeDeferred(this->resourceProvider(), |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 816 | desc, fit, budgeted); |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 817 | if (!rtp) { |
| 818 | return nullptr; |
| 819 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 820 | |
| 821 | return fDrawingManager->makeRenderTargetContext(std::move(rtp), |
| 822 | std::move(colorSpace), |
| 823 | surfaceProps); |
| 824 | } |
| 825 | |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 826 | bool GrContext::abandoned() const { |
| 827 | ASSERT_SINGLE_OWNER |
robertphillips | 7761d61 | 2016-05-16 09:14:53 -0700 | [diff] [blame] | 828 | return fDrawingManager->wasAbandoned(); |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 829 | } |
| 830 | |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 831 | namespace { |
| 832 | void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) { |
| 833 | GrConfigConversionEffect::PMConversion pmToUPM; |
| 834 | GrConfigConversionEffect::PMConversion upmToPM; |
| 835 | GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM); |
| 836 | *pmToUPMValue = pmToUPM; |
| 837 | *upmToPMValue = upmToPM; |
| 838 | } |
| 839 | } |
| 840 | |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 841 | void GrContext::testPMConversionsIfNecessary(uint32_t flags) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 842 | ASSERT_SINGLE_OWNER |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 843 | if (SkToBool(GrContextPriv::kUnpremul_PixelOpsFlag & flags)) { |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 844 | if (!fDidTestPMConversions) { |
| 845 | test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion); |
| 846 | fDidTestPMConversions = true; |
| 847 | } |
| 848 | } |
| 849 | } |
| 850 | |
Brian Osman | 60cd57e | 2017-04-06 10:19:06 -0400 | [diff] [blame] | 851 | sk_sp<GrFragmentProcessor> GrContext::createPMToUPMEffect(sk_sp<GrFragmentProcessor> fp, |
| 852 | GrPixelConfig config) { |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 853 | ASSERT_SINGLE_OWNER |
| 854 | // We should have already called this->testPMConversionsIfNecessary(). |
| 855 | SkASSERT(fDidTestPMConversions); |
Brian Osman | ee80532 | 2017-04-05 10:09:00 -0400 | [diff] [blame] | 856 | if (kRGBA_half_GrPixelConfig == config) { |
| 857 | return GrFragmentProcessor::UnpremulOutput(std::move(fp)); |
| 858 | } else if (kRGBA_8888_GrPixelConfig == config || kBGRA_8888_GrPixelConfig == config) { |
Brian Osman | de1a605 | 2017-03-22 10:57:00 -0400 | [diff] [blame] | 859 | GrConfigConversionEffect::PMConversion pmToUPM = |
| 860 | static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion); |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 861 | if (GrConfigConversionEffect::kPMConversionCnt != pmToUPM) { |
Brian Osman | ee80532 | 2017-04-05 10:09:00 -0400 | [diff] [blame] | 862 | return GrConfigConversionEffect::Make(std::move(fp), pmToUPM); |
Brian Osman | de1a605 | 2017-03-22 10:57:00 -0400 | [diff] [blame] | 863 | } |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 864 | } |
Brian Osman | ee80532 | 2017-04-05 10:09:00 -0400 | [diff] [blame] | 865 | return nullptr; |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 866 | } |
| 867 | |
Brian Osman | 60cd57e | 2017-04-06 10:19:06 -0400 | [diff] [blame] | 868 | sk_sp<GrFragmentProcessor> GrContext::createUPMToPMEffect(sk_sp<GrFragmentProcessor> fp, |
| 869 | GrPixelConfig config) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 870 | ASSERT_SINGLE_OWNER |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 871 | // We should have already called this->testPMConversionsIfNecessary(). |
| 872 | SkASSERT(fDidTestPMConversions); |
Brian Osman | ee80532 | 2017-04-05 10:09:00 -0400 | [diff] [blame] | 873 | if (kRGBA_half_GrPixelConfig == config) { |
| 874 | return GrFragmentProcessor::PremulOutput(std::move(fp)); |
| 875 | } else if (kRGBA_8888_GrPixelConfig == config || kBGRA_8888_GrPixelConfig == config) { |
Brian Osman | de1a605 | 2017-03-22 10:57:00 -0400 | [diff] [blame] | 876 | GrConfigConversionEffect::PMConversion upmToPM = |
| 877 | static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion); |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 878 | if (GrConfigConversionEffect::kPMConversionCnt != upmToPM) { |
Brian Osman | ee80532 | 2017-04-05 10:09:00 -0400 | [diff] [blame] | 879 | return GrConfigConversionEffect::Make(std::move(fp), upmToPM); |
Brian Osman | de1a605 | 2017-03-22 10:57:00 -0400 | [diff] [blame] | 880 | } |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 881 | } |
Brian Osman | ee80532 | 2017-04-05 10:09:00 -0400 | [diff] [blame] | 882 | return nullptr; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Brian Osman | de1a605 | 2017-03-22 10:57:00 -0400 | [diff] [blame] | 885 | bool GrContext::validPMUPMConversionExists(GrPixelConfig config) const { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 886 | ASSERT_SINGLE_OWNER |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 887 | // We should have already called this->testPMConversionsIfNecessary(). |
| 888 | SkASSERT(fDidTestPMConversions); |
bsalomon | 636e802 | 2015-07-29 06:08:46 -0700 | [diff] [blame] | 889 | // The PM<->UPM tests fail or succeed together so we only need to check one. |
Brian Osman | de1a605 | 2017-03-22 10:57:00 -0400 | [diff] [blame] | 890 | // For F16, we always allow PM/UPM conversion on the GPU, even if it doesn't round-trip. |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 891 | return GrConfigConversionEffect::kPMConversionCnt != fPMToUPMConversion || |
Brian Osman | de1a605 | 2017-03-22 10:57:00 -0400 | [diff] [blame] | 892 | kRGBA_half_GrPixelConfig == config; |
bsalomon | 636e802 | 2015-07-29 06:08:46 -0700 | [diff] [blame] | 893 | } |
| 894 | |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 895 | ////////////////////////////////////////////////////////////////////////////// |
| 896 | |
| 897 | void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 898 | ASSERT_SINGLE_OWNER |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 899 | if (maxTextures) { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 900 | *maxTextures = fResourceCache->getMaxResourceCount(); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 901 | } |
| 902 | if (maxTextureBytes) { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 903 | *maxTextureBytes = fResourceCache->getMaxResourceBytes(); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 904 | } |
| 905 | } |
| 906 | |
| 907 | void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 908 | ASSERT_SINGLE_OWNER |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 909 | fResourceCache->setLimits(maxTextures, maxTextureBytes); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 910 | } |
| 911 | |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 912 | ////////////////////////////////////////////////////////////////////////////// |
| 913 | |
| 914 | void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 915 | ASSERT_SINGLE_OWNER |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 916 | fResourceCache->dumpMemoryStatistics(traceMemoryDump); |
| 917 | } |