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 | aaee31f | 2017-04-05 12:18:58 -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 | aaee31f | 2017-04-05 12:18:58 -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 | aaee31f | 2017-04-05 12:18:58 -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 | aaee31f | 2017-04-05 12:18:58 -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 | aaee31f | 2017-04-05 12:18:58 -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 | aaee31f | 2017-04-05 12:18:58 -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 | aaee31f | 2017-04-05 12:18:58 -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 | aaee31f | 2017-04-05 12:18:58 -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 | aaee31f | 2017-04-05 12:18:58 -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) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 338 | sk_sp<GrFragmentProcessor> fp; |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 339 | if (applyPremulToSrc) { |
Robert Phillips | aaee31f | 2017-04-05 12:18:58 -0400 | [diff] [blame] | 340 | fp = fContext->createUPMToPMEffect(tempProxy, SkMatrix::I()); |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 341 | fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 342 | // If premultiplying was the only reason for the draw, fall back to a straight write. |
| 343 | if (!fp) { |
| 344 | if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) { |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 345 | tempProxy.reset(nullptr); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 346 | } |
| 347 | } else { |
| 348 | applyPremulToSrc = false; |
| 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) { |
Robert Phillips | aaee31f | 2017-04-05 12:18:58 -0400 | [diff] [blame] | 353 | fp = GrSimpleTextureEffect::Make(fContext->resourceProvider(), tempProxy, nullptr, |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 354 | SkMatrix::I()); |
| 355 | fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle); |
| 356 | |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 357 | if (!fp) { |
| 358 | return false; |
| 359 | } |
| 360 | } |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 361 | if (tempProxy->priv().hasPendingIO()) { |
Robert Phillips | aaee31f | 2017-04-05 12:18:58 -0400 | [diff] [blame] | 362 | this->flush(tempProxy.get()); |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 363 | } |
Robert Phillips | aaee31f | 2017-04-05 12:18:58 -0400 | [diff] [blame] | 364 | GrTexture* texture = tempProxy->instantiate(fContext->resourceProvider()); |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 365 | if (!texture) { |
| 366 | return false; |
| 367 | } |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 368 | if (applyPremulToSrc) { |
| 369 | size_t tmpRowBytes = 4 * width; |
| 370 | tmpPixels.reset(width * height); |
| 371 | if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes, |
| 372 | tmpPixels.get())) { |
| 373 | return false; |
| 374 | } |
| 375 | rowBytes = tmpRowBytes; |
| 376 | buffer = tmpPixels.get(); |
| 377 | applyPremulToSrc = false; |
| 378 | } |
Robert Phillips | aaee31f | 2017-04-05 12:18:58 -0400 | [diff] [blame] | 379 | if (!fContext->fGpu->writePixels(texture, 0, 0, width, height, |
| 380 | tempDrawInfo.fWriteConfig, buffer, |
| 381 | rowBytes)) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 382 | return false; |
| 383 | } |
| 384 | SkMatrix matrix; |
| 385 | matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top)); |
brianosman | dfe4f2e | 2016-07-21 13:28:36 -0700 | [diff] [blame] | 386 | // TODO: Need to decide the semantics of this function for color spaces. Do we support |
| 387 | // conversion from a passed-in color space? For now, specifying nullptr means that this |
| 388 | // 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] | 389 | GrRenderTarget* renderTarget = surface->asRenderTarget(); |
| 390 | SkASSERT(renderTarget); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 391 | sk_sp<GrRenderTargetContext> renderTargetContext( |
Robert Phillips | aaee31f | 2017-04-05 12:18:58 -0400 | [diff] [blame] | 392 | this->makeWrappedRenderTargetContext(sk_ref_sp(renderTarget), nullptr)); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 393 | if (!renderTargetContext) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 394 | return false; |
| 395 | } |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 396 | GrPaint paint; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 397 | paint.addColorFragmentProcessor(std::move(fp)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 398 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
brianosman | a167e74 | 2016-05-24 06:18:48 -0700 | [diff] [blame] | 399 | paint.setAllowSRGBInputs(true); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 400 | SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 401 | renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, matrix, rect, |
| 402 | nullptr); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 403 | |
| 404 | if (kFlushWrites_PixelOp & pixelOpsFlags) { |
Robert Phillips | aaee31f | 2017-04-05 12:18:58 -0400 | [diff] [blame] | 405 | this->flushSurfaceWrites(renderTargetContext->asRenderTargetProxy()); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | } |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 409 | if (!tempProxy) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 410 | if (applyPremulToSrc) { |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 411 | size_t tmpRowBytes = 4 * width; |
| 412 | tmpPixels.reset(width * height); |
| 413 | if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes, |
| 414 | tmpPixels.get())) { |
| 415 | return false; |
| 416 | } |
| 417 | rowBytes = tmpRowBytes; |
| 418 | buffer = tmpPixels.get(); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 419 | applyPremulToSrc = false; |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 420 | } |
Robert Phillips | aaee31f | 2017-04-05 12:18:58 -0400 | [diff] [blame] | 421 | return fContext->fGpu->writePixels(surface, left, top, width, height, srcConfig, |
| 422 | buffer, rowBytes); |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 423 | } |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 424 | return true; |
| 425 | } |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 426 | |
Robert Phillips | aaee31f | 2017-04-05 12:18:58 -0400 | [diff] [blame] | 427 | bool GrContextPriv::readSurfacePixels(GrSurfaceProxy* srcProxy, SkColorSpace* srcColorSpace, |
| 428 | int left, int top, int width, int height, |
| 429 | GrPixelConfig dstConfig, SkColorSpace* dstColorSpace, |
| 430 | void* buffer, size_t rowBytes, uint32_t flags) { |
Brian Osman | b62ea22 | 2016-12-22 11:12:16 -0500 | [diff] [blame] | 431 | // TODO: Color space conversion |
| 432 | |
Robert Phillips | aaee31f | 2017-04-05 12:18:58 -0400 | [diff] [blame] | 433 | ASSERT_SINGLE_OWNER_PRIV |
| 434 | RETURN_FALSE_IF_ABANDONED_PRIV |
| 435 | ASSERT_OWNED_PROXY_PRIV(srcProxy); |
| 436 | SkASSERT(srcProxy); |
| 437 | GR_AUDIT_TRAIL_AUTO_FRAME(&fContext->fAuditTrail, "GrContextPriv::readSurfacePixels"); |
bsalomon | 32ab260 | 2015-09-09 18:57:49 -0700 | [diff] [blame] | 438 | |
Robert Phillips | aaee31f | 2017-04-05 12:18:58 -0400 | [diff] [blame] | 439 | // MDB TODO: delay this instantiation until later in the method |
| 440 | GrSurface* src = srcProxy->instantiate(fContext->resourceProvider()); |
| 441 | if (!src) { |
| 442 | return false; |
| 443 | } |
| 444 | |
| 445 | fContext->testPMConversionsIfNecessary(flags); |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 446 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 447 | // Adjust the params so that if we wind up using an intermediate surface we've already done |
| 448 | // all the trimming and the temporary can be the min size required. |
| 449 | if (!GrSurfacePriv::AdjustReadPixelParams(src->width(), src->height(), |
| 450 | GrBytesPerPixel(dstConfig), &left, |
| 451 | &top, &width, &height, &buffer, &rowBytes)) { |
| 452 | return false; |
| 453 | } |
| 454 | |
| 455 | if (!(kDontFlush_PixelOpsFlag & flags) && src->surfacePriv().hasPendingWrite()) { |
Robert Phillips | aaee31f | 2017-04-05 12:18:58 -0400 | [diff] [blame] | 456 | this->flush(nullptr); // MDB TODO: tighten this |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 457 | } |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 458 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 459 | bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags); |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 460 | if (unpremul && !valid_unpremul_config(dstConfig)) { |
Brian Osman | de1a605 | 2017-03-22 10:57:00 -0400 | [diff] [blame] | 461 | // The unpremul flag is only allowed for 8888 and F16 configs. |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 462 | return false; |
| 463 | } |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 464 | // We don't allow conversion between integer configs and float/fixed configs. |
| 465 | if (GrPixelConfigIsSint(src->config()) != GrPixelConfigIsSint(dstConfig)) { |
| 466 | return false; |
| 467 | } |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 468 | |
bsalomon | 636e802 | 2015-07-29 06:08:46 -0700 | [diff] [blame] | 469 | GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference; |
| 470 | // Don't prefer to draw for the conversion (and thereby access a texture from the cache) when |
| 471 | // we've already determined that there isn't a roundtrip preserving conversion processor pair. |
Robert Phillips | aaee31f | 2017-04-05 12:18:58 -0400 | [diff] [blame] | 472 | if (unpremul && fContext->validPMUPMConversionExists(src->config())) { |
bsalomon | 636e802 | 2015-07-29 06:08:46 -0700 | [diff] [blame] | 473 | drawPreference = GrGpu::kCallerPrefersDraw_DrawPreference; |
| 474 | } |
| 475 | |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 476 | GrGpu::ReadPixelTempDrawInfo tempDrawInfo; |
Robert Phillips | aaee31f | 2017-04-05 12:18:58 -0400 | [diff] [blame] | 477 | if (!fContext->fGpu->getReadPixelsInfo(src, width, height, rowBytes, dstConfig, |
| 478 | &drawPreference, &tempDrawInfo)) { |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 479 | return false; |
| 480 | } |
bsalomon | 191bcc0 | 2014-11-14 11:31:13 -0800 | [diff] [blame] | 481 | |
Robert Phillips | aaee31f | 2017-04-05 12:18:58 -0400 | [diff] [blame] | 482 | sk_sp<GrSurfaceProxy> proxyToRead = sk_ref_sp(srcProxy); |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 483 | bool didTempDraw = false; |
| 484 | if (GrGpu::kNoDraw_DrawPreference != drawPreference) { |
bsalomon | b117ff1 | 2016-07-19 07:24:40 -0700 | [diff] [blame] | 485 | if (SkBackingFit::kExact == tempDrawInfo.fTempSurfaceFit) { |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 486 | // We only respect this when the entire src is being read. Otherwise we can trigger too |
| 487 | // many odd ball texture sizes and trash the cache. |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 488 | if (width != src->width() || height != src->height()) { |
bsalomon | b117ff1 | 2016-07-19 07:24:40 -0700 | [diff] [blame] | 489 | tempDrawInfo.fTempSurfaceFit= SkBackingFit::kApprox; |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 490 | } |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 491 | } |
brianosman | dfe4f2e | 2016-07-21 13:28:36 -0700 | [diff] [blame] | 492 | // TODO: Need to decide the semantics of this function for color spaces. Do we support |
| 493 | // conversion to a passed-in color space? For now, specifying nullptr means that this |
| 494 | // path will do no conversion, so it will match the behavior of the non-draw path. |
Robert Phillips | aaee31f | 2017-04-05 12:18:58 -0400 | [diff] [blame] | 495 | sk_sp<GrRenderTargetContext> tempRTC = fContext->makeRenderTargetContext( |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 496 | tempDrawInfo.fTempSurfaceFit, |
bsalomon | b117ff1 | 2016-07-19 07:24:40 -0700 | [diff] [blame] | 497 | tempDrawInfo.fTempSurfaceDesc.fWidth, |
| 498 | tempDrawInfo.fTempSurfaceDesc.fHeight, |
| 499 | tempDrawInfo.fTempSurfaceDesc.fConfig, |
brianosman | dfe4f2e | 2016-07-21 13:28:36 -0700 | [diff] [blame] | 500 | nullptr, |
bsalomon | b117ff1 | 2016-07-19 07:24:40 -0700 | [diff] [blame] | 501 | tempDrawInfo.fTempSurfaceDesc.fSampleCnt, |
| 502 | tempDrawInfo.fTempSurfaceDesc.fOrigin); |
Brian Osman | 693a540 | 2016-10-27 15:13:22 -0400 | [diff] [blame] | 503 | if (tempRTC) { |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 504 | SkMatrix textureMatrix = SkMatrix::MakeTrans(SkIntToScalar(left), SkIntToScalar(top)); |
Robert Phillips | aaee31f | 2017-04-05 12:18:58 -0400 | [diff] [blame] | 505 | sk_sp<GrTextureProxy> proxy = sk_ref_sp(srcProxy->asTextureProxy()); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 506 | sk_sp<GrFragmentProcessor> fp; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 507 | if (unpremul) { |
Robert Phillips | aaee31f | 2017-04-05 12:18:58 -0400 | [diff] [blame] | 508 | fp = fContext->createPMToUPMEffect(proxy, textureMatrix); |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 509 | fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 510 | if (fp) { |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 511 | unpremul = false; // we no longer need to do this on CPU after the read back. |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 512 | } else if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) { |
| 513 | // We only wanted to do the draw in order to perform the unpremul so don't |
| 514 | // bother. |
Brian Osman | 693a540 | 2016-10-27 15:13:22 -0400 | [diff] [blame] | 515 | tempRTC.reset(nullptr); |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 516 | } |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 517 | } |
Brian Osman | 693a540 | 2016-10-27 15:13:22 -0400 | [diff] [blame] | 518 | if (!fp && tempRTC) { |
Robert Phillips | aaee31f | 2017-04-05 12:18:58 -0400 | [diff] [blame] | 519 | fp = GrSimpleTextureEffect::Make(fContext->resourceProvider(), std::move(proxy), |
Robert Phillips | 769e80d | 2017-03-29 13:06:57 -0400 | [diff] [blame] | 520 | nullptr, textureMatrix); |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 521 | fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle); |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 522 | } |
| 523 | if (fp) { |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 524 | GrPaint paint; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 525 | paint.addColorFragmentProcessor(std::move(fp)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 526 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
brianosman | a167e74 | 2016-05-24 06:18:48 -0700 | [diff] [blame] | 527 | paint.setAllowSRGBInputs(true); |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 528 | SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 529 | tempRTC->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect, |
| 530 | nullptr); |
Robert Phillips | aaee31f | 2017-04-05 12:18:58 -0400 | [diff] [blame] | 531 | proxyToRead = tempRTC->asTextureProxyRef(); |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 532 | left = 0; |
| 533 | top = 0; |
| 534 | didTempDraw = true; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 535 | } |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 536 | } |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 537 | } |
joshualitt | 5c55fef | 2014-10-31 14:04:35 -0700 | [diff] [blame] | 538 | |
Robert Phillips | aaee31f | 2017-04-05 12:18:58 -0400 | [diff] [blame] | 539 | if (!proxyToRead) { |
| 540 | return false; |
| 541 | } |
| 542 | |
| 543 | GrSurface* surfaceToRead = proxyToRead->instantiate(fContext->resourceProvider()); |
Robert Phillips | 833dcf4 | 2016-11-18 08:44:13 -0500 | [diff] [blame] | 544 | if (!surfaceToRead) { |
| 545 | return false; |
| 546 | } |
| 547 | |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 548 | if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) { |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 549 | return false; |
| 550 | } |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 551 | GrPixelConfig configToRead = dstConfig; |
| 552 | if (didTempDraw) { |
Robert Phillips | aaee31f | 2017-04-05 12:18:58 -0400 | [diff] [blame] | 553 | this->flushSurfaceWrites(proxyToRead.get()); |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 554 | configToRead = tempDrawInfo.fReadConfig; |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 555 | } |
Robert Phillips | aaee31f | 2017-04-05 12:18:58 -0400 | [diff] [blame] | 556 | if (!fContext->fGpu->readPixels(surfaceToRead, left, top, width, height, configToRead, |
| 557 | buffer, rowBytes)) { |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 558 | return false; |
| 559 | } |
| 560 | |
| 561 | // Perform umpremul conversion if we weren't able to perform it as a draw. |
| 562 | if (unpremul) { |
Matt Sarett | c7b2908 | 2017-02-09 16:22:39 -0500 | [diff] [blame] | 563 | SkColorType colorType; |
| 564 | if (!GrPixelConfigToColorType(dstConfig, &colorType) || |
| 565 | 4 != SkColorTypeBytesPerPixel(colorType)) |
| 566 | { |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 567 | return false; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 568 | } |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 569 | |
Matt Sarett | c7b2908 | 2017-02-09 16:22:39 -0500 | [diff] [blame] | 570 | for (int y = 0; y < height; y++) { |
| 571 | SkUnpremultiplyRow<false>((uint32_t*) buffer, (const uint32_t*) buffer, width); |
| 572 | buffer = SkTAddOffset<void>(buffer, rowBytes); |
| 573 | } |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 574 | } |
| 575 | return true; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 578 | void GrContextPriv::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) { |
| 579 | ASSERT_SINGLE_OWNER_PRIV |
| 580 | RETURN_IF_ABANDONED_PRIV |
| 581 | SkASSERT(proxy); |
| 582 | ASSERT_OWNED_PROXY_PRIV(proxy); |
| 583 | fContext->fDrawingManager->prepareSurfaceForExternalIO(proxy); |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 586 | void GrContextPriv::flushSurfaceWrites(GrSurfaceProxy* proxy) { |
| 587 | ASSERT_SINGLE_OWNER_PRIV |
| 588 | RETURN_IF_ABANDONED_PRIV |
| 589 | SkASSERT(proxy); |
| 590 | ASSERT_OWNED_PROXY_PRIV(proxy); |
| 591 | if (proxy->priv().hasPendingWrite()) { |
| 592 | this->flush(proxy); |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 596 | void GrContextPriv::flushSurfaceIO(GrSurfaceProxy* proxy) { |
| 597 | ASSERT_SINGLE_OWNER_PRIV |
| 598 | RETURN_IF_ABANDONED_PRIV |
| 599 | SkASSERT(proxy); |
| 600 | ASSERT_OWNED_PROXY_PRIV(proxy); |
| 601 | if (proxy->priv().hasPendingIO()) { |
| 602 | this->flush(proxy); |
ajuma | 95243eb | 2016-08-24 08:19:02 -0700 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 606 | //////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | b471a32 | 2014-03-10 07:40:03 +0000 | [diff] [blame] | 607 | int GrContext::getRecommendedSampleCount(GrPixelConfig config, |
| 608 | SkScalar dpi) const { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 609 | ASSERT_SINGLE_OWNER |
| 610 | |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 611 | if (!this->caps()->isConfigRenderable(config, true)) { |
commit-bot@chromium.org | b471a32 | 2014-03-10 07:40:03 +0000 | [diff] [blame] | 612 | return 0; |
| 613 | } |
| 614 | int chosenSampleCount = 0; |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 615 | if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) { |
commit-bot@chromium.org | b471a32 | 2014-03-10 07:40:03 +0000 | [diff] [blame] | 616 | if (dpi >= 250.0f) { |
| 617 | chosenSampleCount = 4; |
| 618 | } else { |
| 619 | chosenSampleCount = 16; |
| 620 | } |
| 621 | } |
egdaniel | eed519e | 2016-01-15 11:36:18 -0800 | [diff] [blame] | 622 | return chosenSampleCount <= fGpu->caps()->maxSampleCount() ? chosenSampleCount : 0; |
commit-bot@chromium.org | b471a32 | 2014-03-10 07:40:03 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 625 | sk_sp<GrRenderTargetContext> GrContextPriv::makeWrappedRenderTargetContext( |
| 626 | sk_sp<GrRenderTarget> rt, |
| 627 | sk_sp<SkColorSpace> colorSpace, |
| 628 | const SkSurfaceProps* surfaceProps) { |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 629 | ASSERT_SINGLE_OWNER_PRIV |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 630 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 631 | sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(rt))); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 632 | if (!proxy) { |
| 633 | return nullptr; |
| 634 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 635 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 636 | return this->drawingManager()->makeRenderTargetContext(std::move(proxy), |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 637 | std::move(colorSpace), |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 638 | surfaceProps); |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 639 | } |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 640 | |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 641 | sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy, |
| 642 | sk_sp<SkColorSpace> colorSpace) { |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 643 | ASSERT_SINGLE_OWNER_PRIV |
| 644 | |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 645 | if (proxy->asRenderTargetProxy()) { |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 646 | return this->drawingManager()->makeRenderTargetContext(std::move(proxy), |
| 647 | std::move(colorSpace), nullptr); |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 648 | } else { |
| 649 | SkASSERT(proxy->asTextureProxy()); |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 650 | return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace)); |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 651 | } |
| 652 | } |
| 653 | |
Robert Phillips | 31c2608 | 2016-12-14 15:12:15 -0500 | [diff] [blame] | 654 | sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurface> surface) { |
| 655 | ASSERT_SINGLE_OWNER_PRIV |
| 656 | |
| 657 | sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface))); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 658 | if (!proxy) { |
| 659 | return nullptr; |
| 660 | } |
Robert Phillips | 31c2608 | 2016-12-14 15:12:15 -0500 | [diff] [blame] | 661 | |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 662 | return this->makeWrappedSurfaceContext(std::move(proxy), nullptr); |
Robert Phillips | 31c2608 | 2016-12-14 15:12:15 -0500 | [diff] [blame] | 663 | } |
| 664 | |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 665 | sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrSurfaceDesc& dstDesc, |
| 666 | SkBackingFit fit, |
| 667 | SkBudgeted isDstBudgeted) { |
| 668 | |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 669 | sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeDeferred(fContext->resourceProvider(), |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 670 | dstDesc, fit, isDstBudgeted); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 671 | if (!proxy) { |
| 672 | return nullptr; |
| 673 | } |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 674 | |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 675 | return this->makeWrappedSurfaceContext(std::move(proxy), nullptr); |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 676 | } |
| 677 | |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 678 | sk_sp<GrSurfaceContext> GrContextPriv::makeBackendSurfaceContext(const GrBackendTextureDesc& desc, |
Brian Osman | c1e3705 | 2017-03-09 14:19:20 -0500 | [diff] [blame] | 679 | sk_sp<SkColorSpace> colorSpace) { |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 680 | ASSERT_SINGLE_OWNER_PRIV |
| 681 | |
Brian Osman | c1e3705 | 2017-03-09 14:19:20 -0500 | [diff] [blame] | 682 | sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTexture(desc)); |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 683 | if (!surface) { |
| 684 | return nullptr; |
| 685 | } |
| 686 | |
| 687 | sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface))); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 688 | if (!proxy) { |
| 689 | return nullptr; |
| 690 | } |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 691 | |
| 692 | return this->makeWrappedSurfaceContext(std::move(proxy), std::move(colorSpace)); |
| 693 | } |
| 694 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 695 | sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext( |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 696 | const GrBackendTextureDesc& desc, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 697 | sk_sp<SkColorSpace> colorSpace, |
Brian Osman | c1e3705 | 2017-03-09 14:19:20 -0500 | [diff] [blame] | 698 | const SkSurfaceProps* props) { |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 699 | ASSERT_SINGLE_OWNER_PRIV |
| 700 | SkASSERT(desc.fFlags & kRenderTarget_GrBackendTextureFlag); |
| 701 | |
Brian Osman | c1e3705 | 2017-03-09 14:19:20 -0500 | [diff] [blame] | 702 | sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTexture(desc)); |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 703 | if (!surface) { |
| 704 | return nullptr; |
| 705 | } |
| 706 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 707 | sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface))); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 708 | if (!proxy) { |
| 709 | return nullptr; |
| 710 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 711 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 712 | return this->drawingManager()->makeRenderTargetContext(std::move(proxy), |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 713 | std::move(colorSpace), props); |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 714 | } |
| 715 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 716 | sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext( |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 717 | const GrBackendRenderTargetDesc& desc, |
| 718 | sk_sp<SkColorSpace> colorSpace, |
| 719 | const SkSurfaceProps* surfaceProps) { |
| 720 | ASSERT_SINGLE_OWNER_PRIV |
| 721 | |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 722 | sk_sp<GrRenderTarget> rt(fContext->resourceProvider()->wrapBackendRenderTarget(desc)); |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 723 | if (!rt) { |
| 724 | return nullptr; |
| 725 | } |
| 726 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 727 | sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(rt))); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 728 | if (!proxy) { |
| 729 | return nullptr; |
| 730 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 731 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 732 | return this->drawingManager()->makeRenderTargetContext(std::move(proxy), |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 733 | std::move(colorSpace), |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 734 | surfaceProps); |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 735 | } |
| 736 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 737 | sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext( |
egdaniel | a95d46b | 2016-08-15 08:06:29 -0700 | [diff] [blame] | 738 | const GrBackendTextureDesc& desc, |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 739 | sk_sp<SkColorSpace> colorSpace, |
| 740 | const SkSurfaceProps* surfaceProps) { |
| 741 | ASSERT_SINGLE_OWNER_PRIV |
| 742 | SkASSERT(desc.fFlags & kRenderTarget_GrBackendTextureFlag); |
| 743 | |
| 744 | sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTextureAsRenderTarget(desc)); |
| 745 | if (!surface) { |
| 746 | return nullptr; |
| 747 | } |
| 748 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 749 | sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface))); |
Robert Phillips | 77b3f32 | 2017-01-31 18:24:12 -0500 | [diff] [blame] | 750 | if (!proxy) { |
| 751 | return nullptr; |
| 752 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 753 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 754 | return this->drawingManager()->makeRenderTargetContext(std::move(proxy), |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 755 | std::move(colorSpace), |
| 756 | surfaceProps); |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 757 | } |
| 758 | |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 759 | void GrContextPriv::addPreFlushCallbackObject(sk_sp<GrPreFlushCallbackObject> preFlushCBObject) { |
| 760 | fContext->fDrawingManager->addPreFlushCallbackObject(std::move(preFlushCBObject)); |
| 761 | } |
| 762 | |
| 763 | |
robertphillips | 48fde9c | 2016-09-06 05:20:20 -0700 | [diff] [blame] | 764 | static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) { |
Brian Osman | 78f20e0 | 2017-01-12 10:28:01 -0500 | [diff] [blame] | 765 | switch (config) { |
| 766 | case kAlpha_8_GrPixelConfig: |
| 767 | case kRGB_565_GrPixelConfig: |
| 768 | case kRGBA_4444_GrPixelConfig: |
| 769 | case kBGRA_8888_GrPixelConfig: |
| 770 | return kRGBA_8888_GrPixelConfig; |
| 771 | case kSBGRA_8888_GrPixelConfig: |
| 772 | return kSRGBA_8888_GrPixelConfig; |
| 773 | case kAlpha_half_GrPixelConfig: |
| 774 | return kRGBA_half_GrPixelConfig; |
| 775 | default: |
| 776 | return kUnknown_GrPixelConfig; |
| 777 | } |
robertphillips | 48fde9c | 2016-09-06 05:20:20 -0700 | [diff] [blame] | 778 | } |
| 779 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 780 | sk_sp<GrRenderTargetContext> GrContext::makeRenderTargetContextWithFallback( |
| 781 | SkBackingFit fit, |
| 782 | int width, int height, |
| 783 | GrPixelConfig config, |
| 784 | sk_sp<SkColorSpace> colorSpace, |
| 785 | int sampleCnt, |
| 786 | GrSurfaceOrigin origin, |
| 787 | const SkSurfaceProps* surfaceProps, |
| 788 | SkBudgeted budgeted) { |
robertphillips | 48fde9c | 2016-09-06 05:20:20 -0700 | [diff] [blame] | 789 | if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) { |
| 790 | config = GrPixelConfigFallback(config); |
| 791 | } |
| 792 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 793 | return this->makeRenderTargetContext(fit, width, height, config, std::move(colorSpace), |
| 794 | sampleCnt, origin, surfaceProps, budgeted); |
robertphillips | 48fde9c | 2016-09-06 05:20:20 -0700 | [diff] [blame] | 795 | } |
| 796 | |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 797 | sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContextWithFallback( |
| 798 | SkBackingFit fit, |
| 799 | int width, int height, |
| 800 | GrPixelConfig config, |
| 801 | sk_sp<SkColorSpace> colorSpace, |
| 802 | int sampleCnt, |
| 803 | GrSurfaceOrigin origin, |
| 804 | const SkSurfaceProps* surfaceProps, |
| 805 | SkBudgeted budgeted) { |
| 806 | if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) { |
| 807 | config = GrPixelConfigFallback(config); |
| 808 | } |
| 809 | |
| 810 | return this->makeDeferredRenderTargetContext(fit, width, height, config, std::move(colorSpace), |
| 811 | sampleCnt, origin, surfaceProps, budgeted); |
| 812 | } |
| 813 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 814 | sk_sp<GrRenderTargetContext> GrContext::makeRenderTargetContext(SkBackingFit fit, |
| 815 | int width, int height, |
| 816 | GrPixelConfig config, |
| 817 | sk_sp<SkColorSpace> colorSpace, |
| 818 | int sampleCnt, |
| 819 | GrSurfaceOrigin origin, |
| 820 | const SkSurfaceProps* surfaceProps, |
| 821 | SkBudgeted budgeted) { |
robertphillips | 48fde9c | 2016-09-06 05:20:20 -0700 | [diff] [blame] | 822 | if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) { |
| 823 | return nullptr; |
| 824 | } |
| 825 | |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 826 | GrSurfaceDesc desc; |
| 827 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 828 | desc.fOrigin = origin; |
| 829 | desc.fWidth = width; |
| 830 | desc.fHeight = height; |
| 831 | desc.fConfig = config; |
| 832 | desc.fSampleCnt = sampleCnt; |
| 833 | |
| 834 | sk_sp<GrTexture> tex; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 835 | if (SkBackingFit::kExact == fit) { |
Robert Phillips | aaee31f | 2017-04-05 12:18:58 -0400 | [diff] [blame] | 836 | tex = this->resourceProvider()->createTexture(desc, budgeted); |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 837 | } else { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 838 | tex.reset(this->resourceProvider()->createApproxTexture(desc, 0)); |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 839 | } |
| 840 | if (!tex) { |
| 841 | return nullptr; |
| 842 | } |
| 843 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 844 | sk_sp<GrRenderTargetContext> renderTargetContext( |
| 845 | this->contextPriv().makeWrappedRenderTargetContext(sk_ref_sp(tex->asRenderTarget()), |
robertphillips | 6738c70 | 2016-07-27 12:13:51 -0700 | [diff] [blame] | 846 | std::move(colorSpace), surfaceProps)); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 847 | if (!renderTargetContext) { |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 848 | return nullptr; |
| 849 | } |
| 850 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 851 | return renderTargetContext; |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 852 | } |
| 853 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 854 | sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContext( |
| 855 | SkBackingFit fit, |
| 856 | int width, int height, |
| 857 | GrPixelConfig config, |
| 858 | sk_sp<SkColorSpace> colorSpace, |
| 859 | int sampleCnt, |
| 860 | GrSurfaceOrigin origin, |
| 861 | const SkSurfaceProps* surfaceProps, |
| 862 | SkBudgeted budgeted) { |
| 863 | GrSurfaceDesc desc; |
| 864 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 865 | desc.fOrigin = origin; |
| 866 | desc.fWidth = width; |
| 867 | desc.fHeight = height; |
| 868 | desc.fConfig = config; |
| 869 | desc.fSampleCnt = sampleCnt; |
| 870 | |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 871 | sk_sp<GrTextureProxy> rtp = GrSurfaceProxy::MakeDeferred(this->resourceProvider(), |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 872 | desc, fit, budgeted); |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 873 | if (!rtp) { |
| 874 | return nullptr; |
| 875 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 876 | |
| 877 | return fDrawingManager->makeRenderTargetContext(std::move(rtp), |
| 878 | std::move(colorSpace), |
| 879 | surfaceProps); |
| 880 | } |
| 881 | |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 882 | bool GrContext::abandoned() const { |
| 883 | ASSERT_SINGLE_OWNER |
robertphillips | 7761d61 | 2016-05-16 09:14:53 -0700 | [diff] [blame] | 884 | return fDrawingManager->wasAbandoned(); |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 885 | } |
| 886 | |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 887 | namespace { |
| 888 | void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) { |
| 889 | GrConfigConversionEffect::PMConversion pmToUPM; |
| 890 | GrConfigConversionEffect::PMConversion upmToPM; |
| 891 | GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM); |
| 892 | *pmToUPMValue = pmToUPM; |
| 893 | *upmToPMValue = upmToPM; |
| 894 | } |
| 895 | } |
| 896 | |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 897 | void GrContext::testPMConversionsIfNecessary(uint32_t flags) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 898 | ASSERT_SINGLE_OWNER |
Robert Phillips | aaee31f | 2017-04-05 12:18:58 -0400 | [diff] [blame] | 899 | if (SkToBool(GrContextPriv::kUnpremul_PixelOpsFlag & flags)) { |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 900 | if (!fDidTestPMConversions) { |
| 901 | test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion); |
| 902 | fDidTestPMConversions = true; |
| 903 | } |
| 904 | } |
| 905 | } |
| 906 | |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 907 | sk_sp<GrFragmentProcessor> GrContext::createPMToUPMEffect(sk_sp<GrTextureProxy> proxy, |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 908 | const SkMatrix& matrix) { |
| 909 | ASSERT_SINGLE_OWNER |
| 910 | // We should have already called this->testPMConversionsIfNecessary(). |
| 911 | SkASSERT(fDidTestPMConversions); |
Brian Osman | ee80532 | 2017-04-05 10:09:00 -0400 | [diff] [blame^] | 912 | GrPixelConfig config = proxy->config(); |
| 913 | sk_sp<GrFragmentProcessor> fp = GrSimpleTextureEffect::Make(this->resourceProvider(), |
| 914 | std::move(proxy), nullptr, matrix); |
| 915 | if (kRGBA_half_GrPixelConfig == config) { |
| 916 | return GrFragmentProcessor::UnpremulOutput(std::move(fp)); |
| 917 | } else if (kRGBA_8888_GrPixelConfig == config || kBGRA_8888_GrPixelConfig == config) { |
Brian Osman | de1a605 | 2017-03-22 10:57:00 -0400 | [diff] [blame] | 918 | GrConfigConversionEffect::PMConversion pmToUPM = |
| 919 | static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion); |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 920 | if (GrConfigConversionEffect::kPMConversionCnt != pmToUPM) { |
Brian Osman | ee80532 | 2017-04-05 10:09:00 -0400 | [diff] [blame^] | 921 | return GrConfigConversionEffect::Make(std::move(fp), pmToUPM); |
Brian Osman | de1a605 | 2017-03-22 10:57:00 -0400 | [diff] [blame] | 922 | } |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 923 | } |
Brian Osman | ee80532 | 2017-04-05 10:09:00 -0400 | [diff] [blame^] | 924 | return nullptr; |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | sk_sp<GrFragmentProcessor> GrContext::createUPMToPMEffect(sk_sp<GrTextureProxy> proxy, |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 928 | const SkMatrix& matrix) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 929 | ASSERT_SINGLE_OWNER |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 930 | // We should have already called this->testPMConversionsIfNecessary(). |
| 931 | SkASSERT(fDidTestPMConversions); |
Brian Osman | ee80532 | 2017-04-05 10:09:00 -0400 | [diff] [blame^] | 932 | GrPixelConfig config = proxy->config(); |
| 933 | sk_sp<GrFragmentProcessor> fp = GrSimpleTextureEffect::Make(this->resourceProvider(), |
| 934 | std::move(proxy), nullptr, matrix); |
| 935 | if (kRGBA_half_GrPixelConfig == config) { |
| 936 | return GrFragmentProcessor::PremulOutput(std::move(fp)); |
| 937 | } else if (kRGBA_8888_GrPixelConfig == config || kBGRA_8888_GrPixelConfig == config) { |
Brian Osman | de1a605 | 2017-03-22 10:57:00 -0400 | [diff] [blame] | 938 | GrConfigConversionEffect::PMConversion upmToPM = |
| 939 | static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion); |
Brian Osman | ce42551 | 2017-03-22 14:37:50 -0400 | [diff] [blame] | 940 | if (GrConfigConversionEffect::kPMConversionCnt != upmToPM) { |
Brian Osman | ee80532 | 2017-04-05 10:09:00 -0400 | [diff] [blame^] | 941 | return GrConfigConversionEffect::Make(std::move(fp), upmToPM); |
Brian Osman | de1a605 | 2017-03-22 10:57:00 -0400 | [diff] [blame] | 942 | } |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 943 | } |
Brian Osman | ee80532 | 2017-04-05 10:09:00 -0400 | [diff] [blame^] | 944 | return nullptr; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 945 | } |
| 946 | |
Brian Osman | de1a605 | 2017-03-22 10:57:00 -0400 | [diff] [blame] | 947 | bool GrContext::validPMUPMConversionExists(GrPixelConfig config) const { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 948 | ASSERT_SINGLE_OWNER |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 949 | // We should have already called this->testPMConversionsIfNecessary(). |
| 950 | SkASSERT(fDidTestPMConversions); |
bsalomon | 636e802 | 2015-07-29 06:08:46 -0700 | [diff] [blame] | 951 | // 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] | 952 | // 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] | 953 | return GrConfigConversionEffect::kPMConversionCnt != fPMToUPMConversion || |
Brian Osman | de1a605 | 2017-03-22 10:57:00 -0400 | [diff] [blame] | 954 | kRGBA_half_GrPixelConfig == config; |
bsalomon | 636e802 | 2015-07-29 06:08:46 -0700 | [diff] [blame] | 955 | } |
| 956 | |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 957 | ////////////////////////////////////////////////////////////////////////////// |
| 958 | |
| 959 | void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 960 | ASSERT_SINGLE_OWNER |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 961 | if (maxTextures) { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 962 | *maxTextures = fResourceCache->getMaxResourceCount(); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 963 | } |
| 964 | if (maxTextureBytes) { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 965 | *maxTextureBytes = fResourceCache->getMaxResourceBytes(); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 966 | } |
| 967 | } |
| 968 | |
| 969 | void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 970 | ASSERT_SINGLE_OWNER |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 971 | fResourceCache->setLimits(maxTextures, maxTextureBytes); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 972 | } |
| 973 | |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 974 | ////////////////////////////////////////////////////////////////////////////// |
| 975 | |
| 976 | void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 977 | ASSERT_SINGLE_OWNER |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 978 | fResourceCache->dumpMemoryStatistics(traceMemoryDump); |
| 979 | } |