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