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" |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 9 | #include "GrContextOptions.h" |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 10 | #include "GrDrawingManager.h" |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 11 | #include "GrDrawContext.h" |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 12 | #include "GrLayerCache.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 | |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 21 | #include "effects/GrConfigConversionEffect.h" |
joshualitt | e804292 | 2015-12-11 06:11:21 -0800 | [diff] [blame] | 22 | #include "text/GrTextBlobCache.h" |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 23 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 24 | #define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this) |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 25 | #define ASSERT_SINGLE_OWNER \ |
| 26 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fSingleOwner);) |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 27 | #define RETURN_IF_ABANDONED if (fDrawingManager->abandoned()) { return; } |
| 28 | #define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->abandoned()) { return false; } |
| 29 | #define RETURN_NULL_IF_ABANDONED if (fDrawingManager->abandoned()) { return nullptr; } |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 30 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 31 | //////////////////////////////////////////////////////////////////////////////// |
| 32 | |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 33 | GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext) { |
| 34 | GrContextOptions defaultOptions; |
| 35 | return Create(backend, backendContext, defaultOptions); |
| 36 | } |
bsalomon | f28cff7 | 2015-05-22 12:25:41 -0700 | [diff] [blame] | 37 | |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 38 | GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext, |
| 39 | const GrContextOptions& options) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 40 | GrContext* context = new GrContext; |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 41 | |
| 42 | if (context->init(backend, backendContext, options)) { |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 43 | return context; |
| 44 | } else { |
| 45 | context->unref(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 46 | return nullptr; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 47 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 48 | } |
| 49 | |
joshualitt | 0acd0d3 | 2015-05-07 08:23:19 -0700 | [diff] [blame] | 50 | static int32_t gNextID = 1; |
| 51 | static int32_t next_id() { |
| 52 | int32_t id; |
| 53 | do { |
| 54 | id = sk_atomic_inc(&gNextID); |
| 55 | } while (id == SK_InvalidGenID); |
| 56 | return id; |
| 57 | } |
| 58 | |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 59 | GrContext::GrContext() : fUniqueID(next_id()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 60 | fGpu = nullptr; |
| 61 | fCaps = nullptr; |
| 62 | fResourceCache = nullptr; |
| 63 | fResourceProvider = nullptr; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 64 | fBatchFontCache = nullptr; |
bsalomon | f21dab9 | 2014-11-13 13:33:28 -0800 | [diff] [blame] | 65 | fFlushToReduceCacheSize = false; |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 66 | } |
| 67 | |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 68 | bool GrContext::init(GrBackend backend, GrBackendContext backendContext, |
| 69 | const GrContextOptions& options) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 70 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 71 | SkASSERT(!fGpu); |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 72 | |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 73 | fGpu = GrGpu::Create(backend, backendContext, options, this); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 74 | if (!fGpu) { |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 75 | return false; |
| 76 | } |
bsalomon | 69cfe95 | 2015-11-30 13:27:47 -0800 | [diff] [blame] | 77 | this->initCommon(options); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 78 | return true; |
| 79 | } |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 80 | |
bsalomon | 69cfe95 | 2015-11-30 13:27:47 -0800 | [diff] [blame] | 81 | void GrContext::initCommon(const GrContextOptions& options) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 82 | ASSERT_SINGLE_OWNER |
| 83 | |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 84 | fCaps = SkRef(fGpu->caps()); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 85 | fResourceCache = new GrResourceCache(fCaps); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 86 | fResourceCache->setOverBudgetCallback(OverBudgetCB, this); |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 87 | fResourceProvider = new GrResourceProvider(fGpu, fResourceCache, &fSingleOwner); |
commit-bot@chromium.org | 1836d33 | 2013-07-16 22:55:03 +0000 | [diff] [blame] | 88 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 89 | fLayerCache.reset(new GrLayerCache(this)); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +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; |
joshualitt | de8dc7e | 2016-01-08 10:09:13 -0800 | [diff] [blame] | 97 | fDrawingManager.reset(new GrDrawingManager(this, dtOptions, &fSingleOwner)); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 98 | |
| 99 | // GrBatchFontCache will eventually replace GrFontCache |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 100 | fBatchFontCache = new GrBatchFontCache(this); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 101 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 102 | fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB, this)); |
bsalomon@google.com | c0af317 | 2012-06-15 14:10:09 +0000 | [diff] [blame] | 103 | } |
| 104 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 105 | GrContext::~GrContext() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 106 | ASSERT_SINGLE_OWNER |
| 107 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 108 | if (!fGpu) { |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 109 | SkASSERT(!fCaps); |
bsalomon@google.com | 733c062 | 2013-04-24 17:59:32 +0000 | [diff] [blame] | 110 | return; |
| 111 | } |
| 112 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 113 | this->flush(); |
robertphillips@google.com | 5acc0e3 | 2012-05-17 12:01:02 +0000 | [diff] [blame] | 114 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 115 | fDrawingManager->cleanup(); |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 116 | |
robertphillips@google.com | 950b1b0 | 2013-10-21 17:37:28 +0000 | [diff] [blame] | 117 | for (int i = 0; i < fCleanUpData.count(); ++i) { |
| 118 | (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo); |
| 119 | } |
| 120 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 121 | delete fResourceProvider; |
| 122 | delete fResourceCache; |
| 123 | delete fBatchFontCache; |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 124 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 125 | fGpu->unref(); |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 126 | fCaps->unref(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 127 | } |
| 128 | |
bsalomon | 2354f84 | 2014-07-28 13:48:36 -0700 | [diff] [blame] | 129 | void GrContext::abandonContext() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 130 | ASSERT_SINGLE_OWNER |
| 131 | |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 132 | fResourceProvider->abandon(); |
robertphillips | 0dfa62c | 2015-11-16 06:23:31 -0800 | [diff] [blame] | 133 | |
| 134 | // Need to abandon the drawing manager first so all the render targets |
| 135 | // will be released/forgotten before they too are abandoned. |
| 136 | fDrawingManager->abandon(); |
| 137 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 138 | // abandon first to so destructors |
| 139 | // don't try to free the resources in the API. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 140 | fResourceCache->abandonAll(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 141 | |
robertphillips | e337130 | 2014-09-17 06:01:06 -0700 | [diff] [blame] | 142 | fGpu->contextAbandoned(); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 143 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 144 | fBatchFontCache->freeAll(); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 145 | fLayerCache->freeAll(); |
joshualitt | 26ffc00 | 2015-04-16 11:24:04 -0700 | [diff] [blame] | 146 | fTextBlobCache->freeAll(); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 147 | } |
| 148 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 149 | void GrContext::resetContext(uint32_t state) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 150 | ASSERT_SINGLE_OWNER |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 151 | fGpu->markContextDirty(state); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | void GrContext::freeGpuResources() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 155 | ASSERT_SINGLE_OWNER |
| 156 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 157 | this->flush(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 158 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 159 | fBatchFontCache->freeAll(); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 160 | fLayerCache->freeAll(); |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 161 | |
| 162 | fDrawingManager->freeGpuResources(); |
bsalomon | 3033b9f | 2015-04-13 11:09:56 -0700 | [diff] [blame] | 163 | |
| 164 | fResourceCache->purgeAllUnlocked(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 165 | } |
| 166 | |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 167 | void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 168 | ASSERT_SINGLE_OWNER |
| 169 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 170 | if (resourceCount) { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 171 | *resourceCount = fResourceCache->getBudgetedResourceCount(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 172 | } |
| 173 | if (resourceBytes) { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 174 | *resourceBytes = fResourceCache->getBudgetedResourceBytes(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 175 | } |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 176 | } |
| 177 | |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 178 | //////////////////////////////////////////////////////////////////////////////// |
| 179 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 180 | void GrContext::OverBudgetCB(void* data) { |
bsalomon | 66a450f | 2014-11-13 13:19:10 -0800 | [diff] [blame] | 181 | SkASSERT(data); |
bsalomon | f21dab9 | 2014-11-13 13:33:28 -0800 | [diff] [blame] | 182 | |
bsalomon | 66a450f | 2014-11-13 13:19:10 -0800 | [diff] [blame] | 183 | GrContext* context = reinterpret_cast<GrContext*>(data); |
bsalomon | f21dab9 | 2014-11-13 13:33:28 -0800 | [diff] [blame] | 184 | |
joshualitt | b542bae | 2015-07-28 09:58:39 -0700 | [diff] [blame] | 185 | // Flush the GrBufferedDrawTarget to possibly free up some textures |
bsalomon | f21dab9 | 2014-11-13 13:33:28 -0800 | [diff] [blame] | 186 | context->fFlushToReduceCacheSize = true; |
commit-bot@chromium.org | cae27fe | 2013-07-10 10:14:35 +0000 | [diff] [blame] | 187 | } |
| 188 | |
joshualitt | 0db6dfa | 2015-04-10 07:01:30 -0700 | [diff] [blame] | 189 | void GrContext::TextBlobCacheOverBudgetCB(void* data) { |
| 190 | SkASSERT(data); |
| 191 | |
| 192 | // Unlike the GrResourceCache, TextBlobs are drawn at the SkGpuDevice level, therefore they |
| 193 | // cannot use fFlushTorReduceCacheSize because it uses AutoCheckFlush. The solution is to move |
| 194 | // drawText calls to below the GrContext level, but this is not trivial because they call |
| 195 | // drawPath on SkGpuDevice |
| 196 | GrContext* context = reinterpret_cast<GrContext*>(data); |
| 197 | context->flush(); |
| 198 | } |
| 199 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 200 | //////////////////////////////////////////////////////////////////////////////// |
| 201 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 202 | void GrContext::flush(int flagsBitfield) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 203 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 204 | RETURN_IF_ABANDONED |
robertphillips@google.com | e7db8d6 | 2013-07-04 11:48:52 +0000 | [diff] [blame] | 205 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 206 | if (kDiscard_FlushBit & flagsBitfield) { |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 207 | fDrawingManager->reset(); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 208 | } else { |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 209 | fDrawingManager->flush(); |
junov@google.com | 53a5584 | 2011-06-08 22:55:10 +0000 | [diff] [blame] | 210 | } |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 211 | fResourceCache->notifyFlushOccurred(); |
bsalomon | f21dab9 | 2014-11-13 13:33:28 -0800 | [diff] [blame] | 212 | fFlushToReduceCacheSize = false; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 213 | } |
| 214 | |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 215 | bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes, |
| 216 | const void* inPixels, size_t outRowBytes, void* outPixels) { |
| 217 | SkSrcPixelInfo srcPI; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 218 | if (!GrPixelConfig2ColorAndProfileType(srcConfig, &srcPI.fColorType, nullptr)) { |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 219 | return false; |
| 220 | } |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 221 | srcPI.fAlphaType = kUnpremul_SkAlphaType; |
| 222 | srcPI.fPixels = inPixels; |
| 223 | srcPI.fRowBytes = inRowBytes; |
| 224 | |
| 225 | SkDstPixelInfo dstPI; |
| 226 | dstPI.fColorType = srcPI.fColorType; |
| 227 | dstPI.fAlphaType = kPremul_SkAlphaType; |
| 228 | dstPI.fPixels = outPixels; |
| 229 | dstPI.fRowBytes = outRowBytes; |
| 230 | |
| 231 | return srcPI.convertPixelsTo(&dstPI, width, height); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 232 | } |
| 233 | |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 234 | bool GrContext::writeSurfacePixels(GrSurface* surface, |
| 235 | int left, int top, int width, int height, |
| 236 | GrPixelConfig srcConfig, const void* buffer, size_t rowBytes, |
| 237 | uint32_t pixelOpsFlags) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 238 | ASSERT_SINGLE_OWNER |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 239 | RETURN_FALSE_IF_ABANDONED |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 240 | ASSERT_OWNED_RESOURCE(surface); |
| 241 | SkASSERT(surface); |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 242 | GR_AUDIT_TRAIL_AUTO_FRAME(&fAuditTrail, "GrContext::writeSurfacePixels"); |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 243 | |
| 244 | this->testPMConversionsIfNecessary(pixelOpsFlags); |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 245 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 246 | // 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] | 247 | // necessary and because GrGpu::getWritePixelsInfo requires it. |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 248 | if (!GrSurfacePriv::AdjustWritePixelParams(surface->width(), surface->height(), |
| 249 | GrBytesPerPixel(srcConfig), &left, &top, &width, |
| 250 | &height, &buffer, &rowBytes)) { |
| 251 | return false; |
| 252 | } |
| 253 | |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 254 | bool applyPremulToSrc = false; |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 255 | if (kUnpremul_PixelOpsFlag & pixelOpsFlags) { |
| 256 | if (!GrPixelConfigIs8888(srcConfig)) { |
| 257 | return false; |
| 258 | } |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 259 | applyPremulToSrc = true; |
| 260 | } |
bsalomon | 636e802 | 2015-07-29 06:08:46 -0700 | [diff] [blame] | 261 | |
| 262 | GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference; |
| 263 | // Don't prefer to draw for the conversion (and thereby access a texture from the cache) when |
| 264 | // we've already determined that there isn't a roundtrip preserving conversion processor pair. |
| 265 | if (applyPremulToSrc && !this->didFailPMUPMConversionTest()) { |
| 266 | drawPreference = GrGpu::kCallerPrefersDraw_DrawPreference; |
| 267 | } |
| 268 | |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 269 | GrGpu::WritePixelTempDrawInfo tempDrawInfo; |
| 270 | if (!fGpu->getWritePixelsInfo(surface, width, height, rowBytes, srcConfig, &drawPreference, |
| 271 | &tempDrawInfo)) { |
| 272 | return false; |
| 273 | } |
| 274 | |
| 275 | if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && surface->surfacePriv().hasPendingIO()) { |
| 276 | this->flush(); |
| 277 | } |
| 278 | |
| 279 | SkAutoTUnref<GrTexture> tempTexture; |
| 280 | if (GrGpu::kNoDraw_DrawPreference != drawPreference) { |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 281 | tempTexture.reset( |
| 282 | this->textureProvider()->createApproxTexture(tempDrawInfo.fTempSurfaceDesc)); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 283 | if (!tempTexture && GrGpu::kRequireDraw_DrawPreference == drawPreference) { |
| 284 | return false; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | // temp buffer for doing sw premul conversion, if needed. |
benjaminwagner | 67e8bd2 | 2016-02-02 16:01:39 -0800 | [diff] [blame^] | 289 | #if defined(GOOGLE3) |
| 290 | // Stack frame size is limited in GOOGLE3. |
| 291 | SkAutoSTMalloc<48 * 48, uint32_t> tmpPixels(0); |
| 292 | #else |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 293 | SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0); |
benjaminwagner | 67e8bd2 | 2016-02-02 16:01:39 -0800 | [diff] [blame^] | 294 | #endif |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 295 | if (tempTexture) { |
| 296 | SkAutoTUnref<const GrFragmentProcessor> fp; |
| 297 | SkMatrix textureMatrix; |
| 298 | textureMatrix.setIDiv(tempTexture->width(), tempTexture->height()); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 299 | if (applyPremulToSrc) { |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 300 | fp.reset(this->createUPMToPMEffect(tempTexture, tempDrawInfo.fSwizzle, |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 301 | textureMatrix)); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 302 | // If premultiplying was the only reason for the draw, fall back to a straight write. |
| 303 | if (!fp) { |
| 304 | if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 305 | tempTexture.reset(nullptr); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 306 | } |
| 307 | } else { |
| 308 | applyPremulToSrc = false; |
| 309 | } |
| 310 | } |
| 311 | if (tempTexture) { |
| 312 | if (!fp) { |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 313 | fp.reset(GrConfigConversionEffect::Create(tempTexture, tempDrawInfo.fSwizzle, |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 314 | GrConfigConversionEffect::kNone_PMConversion, textureMatrix)); |
| 315 | if (!fp) { |
| 316 | return false; |
| 317 | } |
| 318 | } |
| 319 | GrRenderTarget* renderTarget = surface->asRenderTarget(); |
| 320 | SkASSERT(renderTarget); |
| 321 | if (tempTexture->surfacePriv().hasPendingIO()) { |
| 322 | this->flush(); |
| 323 | } |
| 324 | if (applyPremulToSrc) { |
| 325 | size_t tmpRowBytes = 4 * width; |
| 326 | tmpPixels.reset(width * height); |
| 327 | if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes, |
| 328 | tmpPixels.get())) { |
| 329 | return false; |
| 330 | } |
| 331 | rowBytes = tmpRowBytes; |
| 332 | buffer = tmpPixels.get(); |
| 333 | applyPremulToSrc = false; |
| 334 | } |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 335 | if (!fGpu->writePixels(tempTexture, 0, 0, width, height, |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 336 | tempDrawInfo.fWriteConfig, buffer, |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 337 | rowBytes)) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 338 | return false; |
| 339 | } |
| 340 | SkMatrix matrix; |
| 341 | matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top)); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 342 | SkAutoTUnref<GrDrawContext> drawContext(this->drawContext(renderTarget)); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 343 | if (!drawContext) { |
| 344 | return false; |
| 345 | } |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 346 | GrPaint paint; |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 347 | paint.addColorFragmentProcessor(fp); |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 348 | paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 349 | SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 350 | drawContext->drawRect(GrClip::WideOpen(), paint, matrix, rect, nullptr); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 351 | |
| 352 | if (kFlushWrites_PixelOp & pixelOpsFlags) { |
| 353 | this->flushSurfaceWrites(surface); |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | if (!tempTexture) { |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 358 | if (applyPremulToSrc) { |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 359 | size_t tmpRowBytes = 4 * width; |
| 360 | tmpPixels.reset(width * height); |
| 361 | if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes, |
| 362 | tmpPixels.get())) { |
| 363 | return false; |
| 364 | } |
| 365 | rowBytes = tmpRowBytes; |
| 366 | buffer = tmpPixels.get(); |
bsalomon | f067451 | 2015-07-28 13:26:15 -0700 | [diff] [blame] | 367 | applyPremulToSrc = false; |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 368 | } |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 369 | return fGpu->writePixels(surface, left, top, width, height, srcConfig, buffer, rowBytes); |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 370 | } |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 371 | return true; |
| 372 | } |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 373 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 374 | bool GrContext::readSurfacePixels(GrSurface* src, |
| 375 | int left, int top, int width, int height, |
| 376 | GrPixelConfig dstConfig, void* buffer, size_t rowBytes, |
| 377 | uint32_t flags) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 378 | ASSERT_SINGLE_OWNER |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 379 | RETURN_FALSE_IF_ABANDONED |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 380 | ASSERT_OWNED_RESOURCE(src); |
| 381 | SkASSERT(src); |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 382 | GR_AUDIT_TRAIL_AUTO_FRAME(&fAuditTrail, "GrContext::readSurfacePixels"); |
bsalomon | 32ab260 | 2015-09-09 18:57:49 -0700 | [diff] [blame] | 383 | |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 384 | this->testPMConversionsIfNecessary(flags); |
| 385 | SkAutoMutexAcquire ama(fReadPixelsMutex); |
| 386 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 387 | // Adjust the params so that if we wind up using an intermediate surface we've already done |
| 388 | // all the trimming and the temporary can be the min size required. |
| 389 | if (!GrSurfacePriv::AdjustReadPixelParams(src->width(), src->height(), |
| 390 | GrBytesPerPixel(dstConfig), &left, |
| 391 | &top, &width, &height, &buffer, &rowBytes)) { |
| 392 | return false; |
| 393 | } |
| 394 | |
| 395 | if (!(kDontFlush_PixelOpsFlag & flags) && src->surfacePriv().hasPendingWrite()) { |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 396 | this->flush(); |
| 397 | } |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 398 | |
bsalomon | e8d21e8 | 2015-07-16 08:23:13 -0700 | [diff] [blame] | 399 | bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags); |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 400 | if (unpremul && !GrPixelConfigIs8888(dstConfig)) { |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 401 | // The unpremul flag is only allowed for 8888 configs. |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 402 | return false; |
| 403 | } |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 404 | |
bsalomon | 636e802 | 2015-07-29 06:08:46 -0700 | [diff] [blame] | 405 | GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference; |
| 406 | // Don't prefer to draw for the conversion (and thereby access a texture from the cache) when |
| 407 | // we've already determined that there isn't a roundtrip preserving conversion processor pair. |
| 408 | if (unpremul && !this->didFailPMUPMConversionTest()) { |
| 409 | drawPreference = GrGpu::kCallerPrefersDraw_DrawPreference; |
| 410 | } |
| 411 | |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 412 | GrGpu::ReadPixelTempDrawInfo tempDrawInfo; |
| 413 | if (!fGpu->getReadPixelsInfo(src, width, height, rowBytes, dstConfig, &drawPreference, |
| 414 | &tempDrawInfo)) { |
| 415 | return false; |
| 416 | } |
bsalomon | 191bcc0 | 2014-11-14 11:31:13 -0800 | [diff] [blame] | 417 | |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 418 | SkAutoTUnref<GrSurface> surfaceToRead(SkRef(src)); |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 419 | bool didTempDraw = false; |
| 420 | if (GrGpu::kNoDraw_DrawPreference != drawPreference) { |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 421 | if (tempDrawInfo.fUseExactScratch) { |
| 422 | // We only respect this when the entire src is being read. Otherwise we can trigger too |
| 423 | // many odd ball texture sizes and trash the cache. |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 424 | if (width != src->width() || height != src->height()) { |
| 425 | tempDrawInfo.fUseExactScratch = false; |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 426 | } |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 427 | } |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 428 | SkAutoTUnref<GrTexture> temp; |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 429 | if (tempDrawInfo.fUseExactScratch) { |
| 430 | temp.reset(this->textureProvider()->createTexture(tempDrawInfo.fTempSurfaceDesc, true)); |
| 431 | } else { |
| 432 | temp.reset(this->textureProvider()->createApproxTexture(tempDrawInfo.fTempSurfaceDesc)); |
| 433 | } |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 434 | if (temp) { |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 435 | SkMatrix textureMatrix; |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 436 | textureMatrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top)); |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 437 | textureMatrix.postIDiv(src->width(), src->height()); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 438 | SkAutoTUnref<const GrFragmentProcessor> fp; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 439 | if (unpremul) { |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 440 | fp.reset(this->createPMToUPMEffect(src->asTexture(), tempDrawInfo.fSwizzle, |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 441 | textureMatrix)); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 442 | if (fp) { |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 443 | 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] | 444 | } else if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) { |
| 445 | // We only wanted to do the draw in order to perform the unpremul so don't |
| 446 | // bother. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 447 | temp.reset(nullptr); |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 448 | } |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 449 | } |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 450 | if (!fp && temp) { |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 451 | fp.reset(GrConfigConversionEffect::Create(src->asTexture(), tempDrawInfo.fSwizzle, |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 452 | GrConfigConversionEffect::kNone_PMConversion, textureMatrix)); |
| 453 | } |
| 454 | if (fp) { |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 455 | GrPaint paint; |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 456 | paint.addColorFragmentProcessor(fp); |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 457 | paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 458 | SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 459 | SkAutoTUnref<GrDrawContext> drawContext(this->drawContext(temp->asRenderTarget())); |
| 460 | drawContext->drawRect(GrClip::WideOpen(), paint, SkMatrix::I(), rect, nullptr); |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 461 | surfaceToRead.reset(SkRef(temp.get())); |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 462 | left = 0; |
| 463 | top = 0; |
| 464 | didTempDraw = true; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 465 | } |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 466 | } |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 467 | } |
joshualitt | 5c55fef | 2014-10-31 14:04:35 -0700 | [diff] [blame] | 468 | |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 469 | if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) { |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 470 | return false; |
| 471 | } |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 472 | GrPixelConfig configToRead = dstConfig; |
| 473 | if (didTempDraw) { |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 474 | this->flushSurfaceWrites(surfaceToRead); |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 475 | configToRead = tempDrawInfo.fReadConfig; |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 476 | } |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 477 | if (!fGpu->readPixels(surfaceToRead, left, top, width, height, configToRead, buffer, |
| 478 | rowBytes)) { |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 479 | return false; |
| 480 | } |
| 481 | |
| 482 | // Perform umpremul conversion if we weren't able to perform it as a draw. |
| 483 | if (unpremul) { |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 484 | SkDstPixelInfo dstPI; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 485 | if (!GrPixelConfig2ColorAndProfileType(dstConfig, &dstPI.fColorType, nullptr)) { |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 486 | return false; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 487 | } |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 488 | dstPI.fAlphaType = kUnpremul_SkAlphaType; |
| 489 | dstPI.fPixels = buffer; |
| 490 | dstPI.fRowBytes = rowBytes; |
| 491 | |
| 492 | SkSrcPixelInfo srcPI; |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 493 | srcPI.fColorType = dstPI.fColorType; |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 494 | srcPI.fAlphaType = kPremul_SkAlphaType; |
| 495 | srcPI.fPixels = buffer; |
| 496 | srcPI.fRowBytes = rowBytes; |
| 497 | |
| 498 | return srcPI.convertPixelsTo(&dstPI, width, height); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 499 | } |
| 500 | return true; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 501 | } |
| 502 | |
bsalomon | c49e868 | 2015-06-30 11:37:35 -0700 | [diff] [blame] | 503 | void GrContext::prepareSurfaceForExternalIO(GrSurface* surface) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 504 | ASSERT_SINGLE_OWNER |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 505 | RETURN_IF_ABANDONED |
bsalomon | 87a94eb | 2014-11-03 14:28:32 -0800 | [diff] [blame] | 506 | SkASSERT(surface); |
| 507 | ASSERT_OWNED_RESOURCE(surface); |
| 508 | if (surface->surfacePriv().hasPendingIO()) { |
| 509 | this->flush(); |
| 510 | } |
| 511 | GrRenderTarget* rt = surface->asRenderTarget(); |
| 512 | if (fGpu && rt) { |
| 513 | fGpu->resolveRenderTarget(rt); |
bsalomon | 41ebbdd | 2014-08-04 08:31:39 -0700 | [diff] [blame] | 514 | } |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 515 | } |
| 516 | |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 517 | void GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect, |
| 518 | const SkIPoint& dstPoint, uint32_t pixelOpsFlags) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 519 | ASSERT_SINGLE_OWNER |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 520 | RETURN_IF_ABANDONED |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 521 | GR_AUDIT_TRAIL_AUTO_FRAME(&fAuditTrail, "GrContext::copySurface"); |
| 522 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 523 | if (!src || !dst) { |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 524 | return; |
| 525 | } |
bsalomon | e3d4bf2 | 2014-09-23 09:15:03 -0700 | [diff] [blame] | 526 | ASSERT_OWNED_RESOURCE(src); |
junov | 2bb5210 | 2014-09-29 10:18:59 -0700 | [diff] [blame] | 527 | ASSERT_OWNED_RESOURCE(dst); |
Brian Salomon | 34a9895 | 2014-09-24 11:41:24 -0400 | [diff] [blame] | 528 | |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 529 | // Since we're going to the draw target and not GPU, no need to check kNoFlush |
| 530 | // here. |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 531 | if (!dst->asRenderTarget()) { |
junov | 96c118e | 2014-09-26 13:09:47 -0700 | [diff] [blame] | 532 | return; |
| 533 | } |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 534 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 535 | SkAutoTUnref<GrDrawContext> drawContext(this->drawContext(dst->asRenderTarget())); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 536 | if (!drawContext) { |
| 537 | return; |
| 538 | } |
| 539 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 540 | drawContext->copySurface(src, srcRect, dstPoint); |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 541 | |
| 542 | if (kFlushWrites_PixelOp & pixelOpsFlags) { |
| 543 | this->flush(); |
| 544 | } |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 545 | } |
| 546 | |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 547 | void GrContext::flushSurfaceWrites(GrSurface* surface) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 548 | ASSERT_SINGLE_OWNER |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 549 | RETURN_IF_ABANDONED |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 550 | if (surface->surfacePriv().hasPendingWrite()) { |
| 551 | this->flush(); |
| 552 | } |
| 553 | } |
| 554 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 555 | //////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | b471a32 | 2014-03-10 07:40:03 +0000 | [diff] [blame] | 556 | int GrContext::getRecommendedSampleCount(GrPixelConfig config, |
| 557 | SkScalar dpi) const { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 558 | ASSERT_SINGLE_OWNER |
| 559 | |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 560 | if (!this->caps()->isConfigRenderable(config, true)) { |
commit-bot@chromium.org | b471a32 | 2014-03-10 07:40:03 +0000 | [diff] [blame] | 561 | return 0; |
| 562 | } |
| 563 | int chosenSampleCount = 0; |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 564 | if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) { |
commit-bot@chromium.org | b471a32 | 2014-03-10 07:40:03 +0000 | [diff] [blame] | 565 | if (dpi >= 250.0f) { |
| 566 | chosenSampleCount = 4; |
| 567 | } else { |
| 568 | chosenSampleCount = 16; |
| 569 | } |
| 570 | } |
egdaniel | eed519e | 2016-01-15 11:36:18 -0800 | [diff] [blame] | 571 | return chosenSampleCount <= fGpu->caps()->maxSampleCount() ? chosenSampleCount : 0; |
commit-bot@chromium.org | b471a32 | 2014-03-10 07:40:03 +0000 | [diff] [blame] | 572 | } |
| 573 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 574 | |
| 575 | GrDrawContext* GrContext::drawContext(GrRenderTarget* rt, const SkSurfaceProps* surfaceProps) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 576 | ASSERT_SINGLE_OWNER |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 577 | return fDrawingManager->drawContext(rt, surfaceProps); |
| 578 | } |
| 579 | |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 580 | bool GrContext::abandoned() const { |
| 581 | ASSERT_SINGLE_OWNER |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 582 | return fDrawingManager->abandoned(); |
| 583 | } |
| 584 | |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 585 | namespace { |
| 586 | void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) { |
| 587 | GrConfigConversionEffect::PMConversion pmToUPM; |
| 588 | GrConfigConversionEffect::PMConversion upmToPM; |
| 589 | GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM); |
| 590 | *pmToUPMValue = pmToUPM; |
| 591 | *upmToPMValue = upmToPM; |
| 592 | } |
| 593 | } |
| 594 | |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 595 | void GrContext::testPMConversionsIfNecessary(uint32_t flags) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 596 | ASSERT_SINGLE_OWNER |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 597 | if (SkToBool(kUnpremul_PixelOpsFlag & flags)) { |
| 598 | SkAutoMutexAcquire ama(fTestPMConversionsMutex); |
| 599 | if (!fDidTestPMConversions) { |
| 600 | test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion); |
| 601 | fDidTestPMConversions = true; |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 606 | const GrFragmentProcessor* GrContext::createPMToUPMEffect(GrTexture* texture, |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 607 | const GrSwizzle& swizzle, |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 608 | const SkMatrix& matrix) const { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 609 | ASSERT_SINGLE_OWNER |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 610 | // We should have already called this->testPMConversionsIfNecessary(). |
| 611 | SkASSERT(fDidTestPMConversions); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 612 | GrConfigConversionEffect::PMConversion pmToUPM = |
| 613 | static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion); |
| 614 | if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) { |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 615 | return GrConfigConversionEffect::Create(texture, swizzle, pmToUPM, matrix); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 616 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 617 | return nullptr; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 618 | } |
| 619 | } |
| 620 | |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 621 | const GrFragmentProcessor* GrContext::createUPMToPMEffect(GrTexture* texture, |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 622 | const GrSwizzle& swizzle, |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 623 | const SkMatrix& matrix) const { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 624 | ASSERT_SINGLE_OWNER |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 625 | // We should have already called this->testPMConversionsIfNecessary(). |
| 626 | SkASSERT(fDidTestPMConversions); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 627 | GrConfigConversionEffect::PMConversion upmToPM = |
| 628 | static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion); |
| 629 | if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) { |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 630 | return GrConfigConversionEffect::Create(texture, swizzle, upmToPM, matrix); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 631 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 632 | return nullptr; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 633 | } |
| 634 | } |
| 635 | |
bsalomon | 636e802 | 2015-07-29 06:08:46 -0700 | [diff] [blame] | 636 | bool GrContext::didFailPMUPMConversionTest() const { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 637 | ASSERT_SINGLE_OWNER |
bsalomon | 6c6f658 | 2015-09-10 08:12:46 -0700 | [diff] [blame] | 638 | // We should have already called this->testPMConversionsIfNecessary(). |
| 639 | SkASSERT(fDidTestPMConversions); |
bsalomon | 636e802 | 2015-07-29 06:08:46 -0700 | [diff] [blame] | 640 | // 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] | 641 | return GrConfigConversionEffect::kNone_PMConversion == fPMToUPMConversion; |
bsalomon | 636e802 | 2015-07-29 06:08:46 -0700 | [diff] [blame] | 642 | } |
| 643 | |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 644 | ////////////////////////////////////////////////////////////////////////////// |
| 645 | |
| 646 | void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 647 | ASSERT_SINGLE_OWNER |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 648 | if (maxTextures) { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 649 | *maxTextures = fResourceCache->getMaxResourceCount(); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 650 | } |
| 651 | if (maxTextureBytes) { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 652 | *maxTextureBytes = fResourceCache->getMaxResourceBytes(); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | |
| 656 | void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 657 | ASSERT_SINGLE_OWNER |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 658 | fResourceCache->setLimits(maxTextures, maxTextureBytes); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 659 | } |
| 660 | |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 661 | ////////////////////////////////////////////////////////////////////////////// |
| 662 | |
| 663 | void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 664 | ASSERT_SINGLE_OWNER |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 665 | fResourceCache->dumpMemoryStatistics(traceMemoryDump); |
| 666 | } |