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