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