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