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