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