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 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
bsalomon@google.com | 1fadb20 | 2011-12-12 16:10:08 +0000 | [diff] [blame] | 10 | #include "GrContext.h" |
| 11 | |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 12 | #include "effects/GrConfigConversionEffect.h" |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 13 | #include "effects/GrDashingEffect.h" |
| 14 | #include "effects/GrSingleTextureEffect.h" |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 15 | |
jvanverth@google.com | bfe2b9d | 2013-09-06 16:57:29 +0000 | [diff] [blame] | 16 | #include "GrAARectRenderer.h" |
tomhudson@google.com | 278cbb4 | 2011-06-30 19:37:01 +0000 | [diff] [blame] | 17 | #include "GrBufferAllocPool.h" |
commit-bot@chromium.org | dcb8ef9 | 2014-03-27 11:26:10 +0000 | [diff] [blame] | 18 | #include "GrGpu.h" |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 19 | #include "GrDistanceFieldTextContext.h" |
commit-bot@chromium.org | 4784182 | 2014-03-27 14:19:17 +0000 | [diff] [blame] | 20 | #include "GrDrawTargetCaps.h" |
commit-bot@chromium.org | dcb8ef9 | 2014-03-27 11:26:10 +0000 | [diff] [blame] | 21 | #include "GrIndexBuffer.h" |
commit-bot@chromium.org | 4784182 | 2014-03-27 14:19:17 +0000 | [diff] [blame] | 22 | #include "GrInOrderDrawBuffer.h" |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 23 | #include "GrLayerCache.h" |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 24 | #include "GrOvalRenderer.h" |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 25 | #include "GrPathRenderer.h" |
tomhudson@google.com | d22b6e4 | 2011-06-24 15:53:40 +0000 | [diff] [blame] | 26 | #include "GrPathUtils.h" |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 27 | #include "GrResourceCache.h" |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 28 | #include "GrResourceCache2.h" |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 29 | #include "GrSoftwarePathRenderer.h" |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 30 | #include "GrStencilBuffer.h" |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 31 | #include "GrStencilAndCoverTextContext.h" |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 32 | #include "GrStrokeInfo.h" |
tomhudson@google.com | 278cbb4 | 2011-06-30 19:37:01 +0000 | [diff] [blame] | 33 | #include "GrTextStrike.h" |
egdaniel | bbcb38d | 2014-06-19 10:19:29 -0700 | [diff] [blame] | 34 | #include "GrTraceMarker.h" |
commit-bot@chromium.org | 2a05de0 | 2014-03-25 15:17:32 +0000 | [diff] [blame] | 35 | #include "GrTracing.h" |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 36 | #include "SkDashPathPriv.h" |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 37 | #include "SkGr.h" |
commit-bot@chromium.org | dcb8ef9 | 2014-03-27 11:26:10 +0000 | [diff] [blame] | 38 | #include "SkRTConf.h" |
commit-bot@chromium.org | 4784182 | 2014-03-27 14:19:17 +0000 | [diff] [blame] | 39 | #include "SkRRect.h" |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 40 | #include "SkStrokeRec.h" |
commit-bot@chromium.org | dcb8ef9 | 2014-03-27 11:26:10 +0000 | [diff] [blame] | 41 | #include "SkTLazy.h" |
commit-bot@chromium.org | 4784182 | 2014-03-27 14:19:17 +0000 | [diff] [blame] | 42 | #include "SkTLS.h" |
commit-bot@chromium.org | 933e65d | 2014-03-20 20:00:24 +0000 | [diff] [blame] | 43 | #include "SkTraceEvent.h" |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 44 | |
bsalomon@google.com | 82b0ec6 | 2013-02-07 21:02:36 +0000 | [diff] [blame] | 45 | // It can be useful to set this to false to test whether a bug is caused by using the |
bsalomon@google.com | 1d4edd3 | 2012-08-16 18:36:06 +0000 | [diff] [blame] | 46 | // InOrderDrawBuffer, to compare performance of using/not using InOrderDrawBuffer, or to make |
bsalomon@google.com | 82b0ec6 | 2013-02-07 21:02:36 +0000 | [diff] [blame] | 47 | // debugging simpler. |
bsalomon@google.com | e932c98 | 2013-09-10 19:47:01 +0000 | [diff] [blame] | 48 | SK_CONF_DECLARE(bool, c_Defer, "gpu.deferContext", true, |
jvanverth@google.com | 65eb4d5 | 2013-03-19 18:51:02 +0000 | [diff] [blame] | 49 | "Defers rendering in GrContext via GrInOrderDrawBuffer."); |
bsalomon@google.com | 82b0ec6 | 2013-02-07 21:02:36 +0000 | [diff] [blame] | 50 | |
| 51 | #define BUFFERED_DRAW (c_Defer ? kYes_BufferedDraw : kNo_BufferedDraw) |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 52 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 53 | #ifdef SK_DEBUG |
reed@google.com | 4b2d3f3 | 2012-05-15 18:05:50 +0000 | [diff] [blame] | 54 | // change this to a 1 to see notifications when partial coverage fails |
| 55 | #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0 |
| 56 | #else |
| 57 | #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0 |
| 58 | #endif |
| 59 | |
robertphillips@google.com | 4e5559a | 2013-10-30 17:04:16 +0000 | [diff] [blame] | 60 | static const size_t MAX_RESOURCE_CACHE_COUNT = GR_DEFAULT_RESOURCE_CACHE_COUNT_LIMIT; |
| 61 | static const size_t MAX_RESOURCE_CACHE_BYTES = GR_DEFAULT_RESOURCE_CACHE_MB_LIMIT * 1024 * 1024; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 62 | |
bsalomon@google.com | 6036149 | 2012-03-15 17:47:06 +0000 | [diff] [blame] | 63 | static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 64 | static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4; |
| 65 | |
bsalomon@google.com | 1d4edd3 | 2012-08-16 18:36:06 +0000 | [diff] [blame] | 66 | static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = 1 << 11; |
| 67 | static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = 4; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 68 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 69 | #define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this) |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 70 | |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 71 | // Glorified typedef to avoid including GrDrawState.h in GrContext.h |
| 72 | class GrContext::AutoRestoreEffects : public GrDrawState::AutoRestoreEffects {}; |
| 73 | |
commit-bot@chromium.org | 5dbb149 | 2013-10-04 16:23:58 +0000 | [diff] [blame] | 74 | class GrContext::AutoCheckFlush { |
| 75 | public: |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 76 | AutoCheckFlush(GrContext* context) : fContext(context) { SkASSERT(context); } |
commit-bot@chromium.org | 5dbb149 | 2013-10-04 16:23:58 +0000 | [diff] [blame] | 77 | |
| 78 | ~AutoCheckFlush() { |
| 79 | if (fContext->fFlushToReduceCacheSize) { |
| 80 | fContext->flush(); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | private: |
| 85 | GrContext* fContext; |
| 86 | }; |
| 87 | |
krajcevski | 9c6d4d7 | 2014-08-12 07:26:25 -0700 | [diff] [blame] | 88 | GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext, |
| 89 | const Options* opts) { |
| 90 | GrContext* context; |
| 91 | if (NULL == opts) { |
| 92 | context = SkNEW_ARGS(GrContext, (Options())); |
| 93 | } else { |
| 94 | context = SkNEW_ARGS(GrContext, (*opts)); |
| 95 | } |
| 96 | |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 97 | if (context->init(backend, backendContext)) { |
| 98 | return context; |
| 99 | } else { |
| 100 | context->unref(); |
| 101 | return NULL; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 102 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 103 | } |
| 104 | |
krajcevski | 9c6d4d7 | 2014-08-12 07:26:25 -0700 | [diff] [blame] | 105 | GrContext::GrContext(const Options& opts) : fOptions(opts) { |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 106 | fDrawState = NULL; |
| 107 | fGpu = NULL; |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 108 | fClip = NULL; |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 109 | fPathRendererChain = NULL; |
| 110 | fSoftwarePathRenderer = NULL; |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 111 | fResourceCache = NULL; |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 112 | fResourceCache2 = NULL; |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 113 | fFontCache = NULL; |
| 114 | fDrawBuffer = NULL; |
| 115 | fDrawBufferVBAllocPool = NULL; |
| 116 | fDrawBufferIBAllocPool = NULL; |
commit-bot@chromium.org | 5dbb149 | 2013-10-04 16:23:58 +0000 | [diff] [blame] | 117 | fFlushToReduceCacheSize = false; |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 118 | fAARectRenderer = NULL; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 119 | fOvalRenderer = NULL; |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 120 | fViewMatrix.reset(); |
robertphillips@google.com | 44a91dc | 2013-07-25 15:32:06 +0000 | [diff] [blame] | 121 | fMaxTextureSizeOverride = 1 << 20; |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | bool GrContext::init(GrBackend backend, GrBackendContext backendContext) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 125 | SkASSERT(NULL == fGpu); |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 126 | |
| 127 | fGpu = GrGpu::Create(backend, backendContext, this); |
| 128 | if (NULL == fGpu) { |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | fDrawState = SkNEW(GrDrawState); |
| 133 | fGpu->setDrawState(fDrawState); |
| 134 | |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 135 | fResourceCache = SkNEW_ARGS(GrResourceCache, (MAX_RESOURCE_CACHE_COUNT, |
| 136 | MAX_RESOURCE_CACHE_BYTES)); |
| 137 | fResourceCache->setOverbudgetCallback(OverbudgetCB, this); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 138 | fResourceCache2 = SkNEW(GrResourceCache2); |
commit-bot@chromium.org | 1836d33 | 2013-07-16 22:55:03 +0000 | [diff] [blame] | 139 | |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 140 | fFontCache = SkNEW_ARGS(GrFontCache, (fGpu)); |
| 141 | |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 142 | fLayerCache.reset(SkNEW_ARGS(GrLayerCache, (this))); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 143 | |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 144 | fLastDrawWasBuffered = kNo_BufferedDraw; |
| 145 | |
| 146 | fAARectRenderer = SkNEW(GrAARectRenderer); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 147 | fOvalRenderer = SkNEW(GrOvalRenderer); |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 148 | |
| 149 | fDidTestPMConversions = false; |
| 150 | |
| 151 | this->setupDrawBuffer(); |
| 152 | |
| 153 | return true; |
bsalomon@google.com | c0af317 | 2012-06-15 14:10:09 +0000 | [diff] [blame] | 154 | } |
| 155 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 156 | GrContext::~GrContext() { |
bsalomon@google.com | 733c062 | 2013-04-24 17:59:32 +0000 | [diff] [blame] | 157 | if (NULL == fGpu) { |
| 158 | return; |
| 159 | } |
| 160 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 161 | this->flush(); |
robertphillips@google.com | 5acc0e3 | 2012-05-17 12:01:02 +0000 | [diff] [blame] | 162 | |
robertphillips@google.com | 950b1b0 | 2013-10-21 17:37:28 +0000 | [diff] [blame] | 163 | for (int i = 0; i < fCleanUpData.count(); ++i) { |
| 164 | (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo); |
| 165 | } |
| 166 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 167 | delete fResourceCache2; |
| 168 | fResourceCache2 = NULL; |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 169 | delete fResourceCache; |
| 170 | fResourceCache = NULL; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 171 | delete fFontCache; |
| 172 | delete fDrawBuffer; |
| 173 | delete fDrawBufferVBAllocPool; |
bsalomon@google.com | de6ac2d | 2011-02-25 21:50:42 +0000 | [diff] [blame] | 174 | delete fDrawBufferIBAllocPool; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 175 | |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 176 | fAARectRenderer->unref(); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 177 | fOvalRenderer->unref(); |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 178 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 179 | fGpu->unref(); |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 180 | SkSafeUnref(fPathRendererChain); |
| 181 | SkSafeUnref(fSoftwarePathRenderer); |
bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 182 | fDrawState->unref(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 183 | } |
| 184 | |
bsalomon | 2354f84 | 2014-07-28 13:48:36 -0700 | [diff] [blame] | 185 | void GrContext::abandonContext() { |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 186 | // abandon first to so destructors |
| 187 | // don't try to free the resources in the API. |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 188 | fResourceCache2->abandonAll(); |
| 189 | |
robertphillips | e337130 | 2014-09-17 06:01:06 -0700 | [diff] [blame] | 190 | fGpu->contextAbandoned(); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 191 | |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 192 | // a path renderer may be holding onto resources that |
| 193 | // are now unusable |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 194 | SkSafeSetNull(fPathRendererChain); |
| 195 | SkSafeSetNull(fSoftwarePathRenderer); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 196 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 197 | delete fDrawBuffer; |
| 198 | fDrawBuffer = NULL; |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 199 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 200 | delete fDrawBufferVBAllocPool; |
| 201 | fDrawBufferVBAllocPool = NULL; |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 202 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 203 | delete fDrawBufferIBAllocPool; |
| 204 | fDrawBufferIBAllocPool = NULL; |
| 205 | |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 206 | fAARectRenderer->reset(); |
commit-bot@chromium.org | ef284a8 | 2013-07-11 22:29:29 +0000 | [diff] [blame] | 207 | fOvalRenderer->reset(); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 208 | |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 209 | fResourceCache->purgeAllUnlocked(); |
commit-bot@chromium.org | 5c8ee25 | 2013-11-01 15:23:44 +0000 | [diff] [blame] | 210 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 211 | fFontCache->freeAll(); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 212 | fLayerCache->freeAll(); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 213 | } |
| 214 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 215 | void GrContext::resetContext(uint32_t state) { |
| 216 | fGpu->markContextDirty(state); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | void GrContext::freeGpuResources() { |
| 220 | this->flush(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 221 | |
robertphillips@google.com | ff17584 | 2012-05-14 19:31:39 +0000 | [diff] [blame] | 222 | fGpu->purgeResources(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 223 | if (fDrawBuffer) { |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 224 | fDrawBuffer->purgeResources(); |
| 225 | } |
robertphillips@google.com | ff17584 | 2012-05-14 19:31:39 +0000 | [diff] [blame] | 226 | |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 227 | fAARectRenderer->reset(); |
commit-bot@chromium.org | ef284a8 | 2013-07-11 22:29:29 +0000 | [diff] [blame] | 228 | fOvalRenderer->reset(); |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 229 | |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 230 | fResourceCache->purgeAllUnlocked(); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 231 | fFontCache->freeAll(); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 232 | fLayerCache->freeAll(); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 233 | // a path renderer may be holding onto resources |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 234 | SkSafeSetNull(fPathRendererChain); |
| 235 | SkSafeSetNull(fSoftwarePathRenderer); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 236 | } |
| 237 | |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 238 | void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 239 | if (resourceCount) { |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 240 | *resourceCount = fResourceCache->getCachedResourceCount(); |
| 241 | } |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 242 | if (resourceBytes) { |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 243 | *resourceBytes = fResourceCache->getCachedResourceBytes(); |
| 244 | } |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 245 | } |
| 246 | |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 247 | GrTextContext* GrContext::createTextContext(GrRenderTarget* renderTarget, |
| 248 | const SkDeviceProperties& |
| 249 | leakyProperties, |
| 250 | bool enableDistanceFieldFonts) { |
| 251 | if (fGpu->caps()->pathRenderingSupport()) { |
| 252 | if (renderTarget->getStencilBuffer() && renderTarget->isMultisampled()) { |
| 253 | return SkNEW_ARGS(GrStencilAndCoverTextContext, (this, leakyProperties)); |
| 254 | } |
| 255 | } |
| 256 | return SkNEW_ARGS(GrDistanceFieldTextContext, (this, leakyProperties, |
| 257 | enableDistanceFieldFonts)); |
| 258 | } |
| 259 | |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 260 | //////////////////////////////////////////////////////////////////////////////// |
| 261 | |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 262 | GrTexture* GrContext::findAndRefTexture(const GrTextureDesc& desc, |
| 263 | const GrCacheID& cacheID, |
| 264 | const GrTextureParams* params) { |
commit-bot@chromium.org | e49157f | 2014-05-09 20:46:48 +0000 | [diff] [blame] | 265 | GrResourceKey resourceKey = GrTextureImpl::ComputeKey(fGpu, params, desc, cacheID); |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 266 | GrGpuResource* resource = fResourceCache->find(resourceKey); |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 267 | SkSafeRef(resource); |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 268 | return static_cast<GrTexture*>(resource); |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 269 | } |
| 270 | |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 271 | bool GrContext::isTextureInCache(const GrTextureDesc& desc, |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 272 | const GrCacheID& cacheID, |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 273 | const GrTextureParams* params) const { |
commit-bot@chromium.org | e49157f | 2014-05-09 20:46:48 +0000 | [diff] [blame] | 274 | GrResourceKey resourceKey = GrTextureImpl::ComputeKey(fGpu, params, desc, cacheID); |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 275 | return fResourceCache->hasKey(resourceKey); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 276 | } |
| 277 | |
robertphillips@google.com | 9fbcad0 | 2012-09-09 14:44:15 +0000 | [diff] [blame] | 278 | void GrContext::addStencilBuffer(GrStencilBuffer* sb) { |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 279 | ASSERT_OWNED_RESOURCE(sb); |
robertphillips@google.com | 46a8600 | 2012-08-08 10:42:44 +0000 | [diff] [blame] | 280 | |
| 281 | GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(sb->width(), |
| 282 | sb->height(), |
| 283 | sb->numSamples()); |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 284 | fResourceCache->addResource(resourceKey, sb); |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 285 | } |
| 286 | |
robertphillips@google.com | 9fbcad0 | 2012-09-09 14:44:15 +0000 | [diff] [blame] | 287 | GrStencilBuffer* GrContext::findStencilBuffer(int width, int height, |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 288 | int sampleCnt) { |
robertphillips@google.com | 46a8600 | 2012-08-08 10:42:44 +0000 | [diff] [blame] | 289 | GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(width, |
| 290 | height, |
| 291 | sampleCnt); |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 292 | GrGpuResource* resource = fResourceCache->find(resourceKey); |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 293 | return static_cast<GrStencilBuffer*>(resource); |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 294 | } |
| 295 | |
commit-bot@chromium.org | b0ce4b6 | 2014-05-16 17:22:51 +0000 | [diff] [blame] | 296 | static void stretch_image(void* dst, |
| 297 | int dstW, |
| 298 | int dstH, |
commit-bot@chromium.org | f9bd04f | 2014-05-29 19:26:48 +0000 | [diff] [blame] | 299 | const void* src, |
commit-bot@chromium.org | b0ce4b6 | 2014-05-16 17:22:51 +0000 | [diff] [blame] | 300 | int srcW, |
| 301 | int srcH, |
| 302 | size_t bpp) { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 303 | SkFixed dx = (srcW << 16) / dstW; |
| 304 | SkFixed dy = (srcH << 16) / dstH; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 305 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 306 | SkFixed y = dy >> 1; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 307 | |
robertphillips@google.com | 8b16931 | 2013-10-15 17:47:36 +0000 | [diff] [blame] | 308 | size_t dstXLimit = dstW*bpp; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 309 | for (int j = 0; j < dstH; ++j) { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 310 | SkFixed x = dx >> 1; |
commit-bot@chromium.org | f9bd04f | 2014-05-29 19:26:48 +0000 | [diff] [blame] | 311 | const uint8_t* srcRow = reinterpret_cast<const uint8_t *>(src) + (y>>16)*srcW*bpp; |
| 312 | uint8_t* dstRow = reinterpret_cast<uint8_t *>(dst) + j*dstW*bpp; |
robertphillips@google.com | 8b16931 | 2013-10-15 17:47:36 +0000 | [diff] [blame] | 313 | for (size_t i = 0; i < dstXLimit; i += bpp) { |
commit-bot@chromium.org | f9bd04f | 2014-05-29 19:26:48 +0000 | [diff] [blame] | 314 | memcpy(dstRow + i, srcRow + (x>>16)*bpp, bpp); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 315 | x += dx; |
| 316 | } |
| 317 | y += dy; |
| 318 | } |
| 319 | } |
| 320 | |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 321 | namespace { |
| 322 | |
| 323 | // position + local coordinate |
| 324 | extern const GrVertexAttrib gVertexAttribs[] = { |
| 325 | {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 326 | {kVec2f_GrVertexAttribType, sizeof(SkPoint), kLocalCoord_GrVertexAttribBinding} |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 327 | }; |
| 328 | |
| 329 | }; |
| 330 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 331 | // The desired texture is NPOT and tiled but that isn't supported by |
robertphillips@google.com | 3319f33 | 2012-08-13 18:00:36 +0000 | [diff] [blame] | 332 | // the current hardware. Resize the texture to be a POT |
| 333 | GrTexture* GrContext::createResizedTexture(const GrTextureDesc& desc, |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 334 | const GrCacheID& cacheID, |
commit-bot@chromium.org | f9bd04f | 2014-05-29 19:26:48 +0000 | [diff] [blame] | 335 | const void* srcData, |
robertphillips@google.com | 3319f33 | 2012-08-13 18:00:36 +0000 | [diff] [blame] | 336 | size_t rowBytes, |
humper@google.com | b86add1 | 2013-07-25 18:49:07 +0000 | [diff] [blame] | 337 | bool filter) { |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 338 | SkAutoTUnref<GrTexture> clampedTexture(this->findAndRefTexture(desc, cacheID, NULL)); |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 339 | if (NULL == clampedTexture) { |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 340 | clampedTexture.reset(this->createTexture(NULL, desc, cacheID, srcData, rowBytes)); |
robertphillips@google.com | 9fbcad0 | 2012-09-09 14:44:15 +0000 | [diff] [blame] | 341 | |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 342 | if (NULL == clampedTexture) { |
robertphillips@google.com | 3319f33 | 2012-08-13 18:00:36 +0000 | [diff] [blame] | 343 | return NULL; |
| 344 | } |
| 345 | } |
robertphillips@google.com | 0d25eef | 2012-09-11 21:25:51 +0000 | [diff] [blame] | 346 | |
robertphillips@google.com | 3319f33 | 2012-08-13 18:00:36 +0000 | [diff] [blame] | 347 | GrTextureDesc rtDesc = desc; |
| 348 | rtDesc.fFlags = rtDesc.fFlags | |
| 349 | kRenderTarget_GrTextureFlagBit | |
| 350 | kNoStencil_GrTextureFlagBit; |
commit-bot@chromium.org | 5898dce | 2013-09-18 18:52:16 +0000 | [diff] [blame] | 351 | rtDesc.fWidth = GrNextPow2(desc.fWidth); |
| 352 | rtDesc.fHeight = GrNextPow2(desc.fHeight); |
robertphillips@google.com | 3319f33 | 2012-08-13 18:00:36 +0000 | [diff] [blame] | 353 | |
| 354 | GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0); |
| 355 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 356 | if (texture) { |
robertphillips@google.com | 3319f33 | 2012-08-13 18:00:36 +0000 | [diff] [blame] | 357 | GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit); |
| 358 | GrDrawState* drawState = fGpu->drawState(); |
| 359 | drawState->setRenderTarget(texture->asRenderTarget()); |
| 360 | |
| 361 | // if filtering is not desired then we want to ensure all |
| 362 | // texels in the resampled image are copies of texels from |
| 363 | // the original. |
humper@google.com | b86add1 | 2013-07-25 18:49:07 +0000 | [diff] [blame] | 364 | GrTextureParams params(SkShader::kClamp_TileMode, filter ? GrTextureParams::kBilerp_FilterMode : |
| 365 | GrTextureParams::kNone_FilterMode); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 366 | drawState->addColorTextureEffect(clampedTexture, SkMatrix::I(), params); |
robertphillips@google.com | 3319f33 | 2012-08-13 18:00:36 +0000 | [diff] [blame] | 367 | |
egdaniel | 7b3d5ee | 2014-08-28 05:41:14 -0700 | [diff] [blame] | 368 | drawState->setVertexAttribs<gVertexAttribs>(SK_ARRAY_COUNT(gVertexAttribs), |
| 369 | 2 * sizeof(SkPoint)); |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 370 | |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 371 | GrDrawTarget::AutoReleaseGeometry arg(fGpu, 4, 0); |
robertphillips@google.com | 3319f33 | 2012-08-13 18:00:36 +0000 | [diff] [blame] | 372 | |
| 373 | if (arg.succeeded()) { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 374 | SkPoint* verts = (SkPoint*) arg.vertices(); |
| 375 | verts[0].setIRectFan(0, 0, texture->width(), texture->height(), 2 * sizeof(SkPoint)); |
| 376 | verts[1].setIRectFan(0, 0, 1, 1, 2 * sizeof(SkPoint)); |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 377 | fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4); |
robertphillips@google.com | 3319f33 | 2012-08-13 18:00:36 +0000 | [diff] [blame] | 378 | } |
robertphillips@google.com | 3319f33 | 2012-08-13 18:00:36 +0000 | [diff] [blame] | 379 | } else { |
| 380 | // TODO: Our CPU stretch doesn't filter. But we create separate |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 381 | // stretched textures when the texture params is either filtered or |
robertphillips@google.com | 3319f33 | 2012-08-13 18:00:36 +0000 | [diff] [blame] | 382 | // not. Either implement filtered stretch blit on CPU or just create |
| 383 | // one when FBO case fails. |
| 384 | |
| 385 | rtDesc.fFlags = kNone_GrTextureFlags; |
| 386 | // no longer need to clamp at min RT size. |
| 387 | rtDesc.fWidth = GrNextPow2(desc.fWidth); |
| 388 | rtDesc.fHeight = GrNextPow2(desc.fHeight); |
commit-bot@chromium.org | 6e7ddaa | 2014-05-30 13:55:58 +0000 | [diff] [blame] | 389 | |
| 390 | // We shouldn't be resizing a compressed texture. |
| 391 | SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig)); |
| 392 | |
robertphillips@google.com | 8b16931 | 2013-10-15 17:47:36 +0000 | [diff] [blame] | 393 | size_t bpp = GrBytesPerPixel(desc.fConfig); |
george | b62508b | 2014-08-12 18:00:47 -0700 | [diff] [blame] | 394 | GrAutoMalloc<128*128*4> stretchedPixels(bpp * rtDesc.fWidth * rtDesc.fHeight); |
commit-bot@chromium.org | b0ce4b6 | 2014-05-16 17:22:51 +0000 | [diff] [blame] | 395 | stretch_image(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight, |
| 396 | srcData, desc.fWidth, desc.fHeight, bpp); |
robertphillips@google.com | 3319f33 | 2012-08-13 18:00:36 +0000 | [diff] [blame] | 397 | |
| 398 | size_t stretchedRowBytes = rtDesc.fWidth * bpp; |
| 399 | |
commit-bot@chromium.org | b0ce4b6 | 2014-05-16 17:22:51 +0000 | [diff] [blame] | 400 | texture = fGpu->createTexture(rtDesc, stretchedPixels.get(), stretchedRowBytes); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 401 | SkASSERT(texture); |
robertphillips@google.com | 3319f33 | 2012-08-13 18:00:36 +0000 | [diff] [blame] | 402 | } |
robertphillips@google.com | 3319f33 | 2012-08-13 18:00:36 +0000 | [diff] [blame] | 403 | |
| 404 | return texture; |
| 405 | } |
| 406 | |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 407 | GrTexture* GrContext::createTexture(const GrTextureParams* params, |
| 408 | const GrTextureDesc& desc, |
| 409 | const GrCacheID& cacheID, |
commit-bot@chromium.org | f9bd04f | 2014-05-29 19:26:48 +0000 | [diff] [blame] | 410 | const void* srcData, |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 411 | size_t rowBytes, |
| 412 | GrResourceKey* cacheKey) { |
commit-bot@chromium.org | e49157f | 2014-05-09 20:46:48 +0000 | [diff] [blame] | 413 | GrResourceKey resourceKey = GrTextureImpl::ComputeKey(fGpu, params, desc, cacheID); |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 414 | |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 415 | GrTexture* texture; |
commit-bot@chromium.org | e49157f | 2014-05-09 20:46:48 +0000 | [diff] [blame] | 416 | if (GrTextureImpl::NeedsResizing(resourceKey)) { |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 417 | // We do not know how to resize compressed textures. |
| 418 | SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig)); |
| 419 | |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 420 | texture = this->createResizedTexture(desc, cacheID, |
| 421 | srcData, rowBytes, |
commit-bot@chromium.org | e49157f | 2014-05-09 20:46:48 +0000 | [diff] [blame] | 422 | GrTextureImpl::NeedsBilerp(resourceKey)); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 423 | } else { |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 424 | texture = fGpu->createTexture(desc, srcData, rowBytes); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 425 | } |
robertphillips@google.com | 3319f33 | 2012-08-13 18:00:36 +0000 | [diff] [blame] | 426 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 427 | if (texture) { |
robertphillips@google.com | 1622a6d | 2013-07-18 17:11:45 +0000 | [diff] [blame] | 428 | // Adding a resource could put us overbudget. Try to free up the |
| 429 | // necessary space before adding it. |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 430 | fResourceCache->purgeAsNeeded(1, texture->gpuMemorySize()); |
| 431 | fResourceCache->addResource(resourceKey, texture); |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 432 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 433 | if (cacheKey) { |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 434 | *cacheKey = resourceKey; |
| 435 | } |
robertphillips@google.com | 3319f33 | 2012-08-13 18:00:36 +0000 | [diff] [blame] | 436 | } |
| 437 | |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 438 | return texture; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 439 | } |
| 440 | |
skia.committer@gmail.com | 1f3c738 | 2013-07-20 07:00:58 +0000 | [diff] [blame] | 441 | static GrTexture* create_scratch_texture(GrGpu* gpu, |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 442 | GrResourceCache* resourceCache, |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 443 | const GrTextureDesc& desc) { |
| 444 | GrTexture* texture = gpu->createTexture(desc, NULL, 0); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 445 | if (texture) { |
commit-bot@chromium.org | e49157f | 2014-05-09 20:46:48 +0000 | [diff] [blame] | 446 | GrResourceKey key = GrTextureImpl::ComputeScratchKey(texture->desc()); |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 447 | // Adding a resource could put us overbudget. Try to free up the |
| 448 | // necessary space before adding it. |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 449 | resourceCache->purgeAsNeeded(1, texture->gpuMemorySize()); |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 450 | // Make the resource exclusive so future 'find' calls don't return it |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 451 | resourceCache->addResource(key, texture, GrResourceCache::kHide_OwnershipFlag); |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 452 | } |
| 453 | return texture; |
| 454 | } |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 455 | |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 456 | GrTexture* GrContext::lockAndRefScratchTexture(const GrTextureDesc& inDesc, ScratchTexMatch match) { |
| 457 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 458 | SkASSERT((inDesc.fFlags & kRenderTarget_GrTextureFlagBit) || |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 459 | !(inDesc.fFlags & kNoStencil_GrTextureFlagBit)); |
| 460 | |
| 461 | // Renderable A8 targets are not universally supported (e.g., not on ANGLE) |
bsalomon@google.com | 8aaac8d | 2013-10-15 14:35:06 +0000 | [diff] [blame] | 462 | SkASSERT(this->isConfigRenderable(kAlpha_8_GrPixelConfig, inDesc.fSampleCnt > 0) || |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 463 | !(inDesc.fFlags & kRenderTarget_GrTextureFlagBit) || |
| 464 | (inDesc.fConfig != kAlpha_8_GrPixelConfig)); |
| 465 | |
robertphillips@google.com | 2d2e5c4 | 2013-10-30 21:30:43 +0000 | [diff] [blame] | 466 | if (!fGpu->caps()->reuseScratchTextures() && |
| 467 | !(inDesc.fFlags & kRenderTarget_GrTextureFlagBit)) { |
| 468 | // If we're never recycling this texture we can always make it the right size |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 469 | return create_scratch_texture(fGpu, fResourceCache, inDesc); |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | GrTextureDesc desc = inDesc; |
rileya@google.com | 2afb8ec | 2012-08-23 14:08:57 +0000 | [diff] [blame] | 473 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 474 | if (kApprox_ScratchTexMatch == match) { |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 475 | // bin by pow2 with a reasonable min |
bsalomon@google.com | 40db6dc | 2013-03-06 16:35:49 +0000 | [diff] [blame] | 476 | static const int MIN_SIZE = 16; |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 477 | desc.fWidth = SkTMax(MIN_SIZE, GrNextPow2(desc.fWidth)); |
| 478 | desc.fHeight = SkTMax(MIN_SIZE, GrNextPow2(desc.fHeight)); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 479 | } |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 480 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 481 | GrGpuResource* resource = NULL; |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 482 | int origWidth = desc.fWidth; |
| 483 | int origHeight = desc.fHeight; |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 484 | |
| 485 | do { |
commit-bot@chromium.org | e49157f | 2014-05-09 20:46:48 +0000 | [diff] [blame] | 486 | GrResourceKey key = GrTextureImpl::ComputeScratchKey(desc); |
robertphillips@google.com | 209a114 | 2012-10-31 12:25:21 +0000 | [diff] [blame] | 487 | // Ensure we have exclusive access to the texture so future 'find' calls don't return it |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 488 | resource = fResourceCache->find(key, GrResourceCache::kHide_OwnershipFlag); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 489 | if (resource) { |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 490 | resource->ref(); |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 491 | break; |
| 492 | } |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 493 | if (kExact_ScratchTexMatch == match) { |
| 494 | break; |
| 495 | } |
bsalomon@google.com | 40db6dc | 2013-03-06 16:35:49 +0000 | [diff] [blame] | 496 | // We had a cache miss and we are in approx mode, relax the fit of the flags. |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 497 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 498 | // We no longer try to reuse textures that were previously used as render targets in |
rileya@google.com | 2afb8ec | 2012-08-23 14:08:57 +0000 | [diff] [blame] | 499 | // situations where no RT is needed; doing otherwise can confuse the video driver and |
| 500 | // cause significant performance problems in some cases. |
| 501 | if (desc.fFlags & kNoStencil_GrTextureFlagBit) { |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 502 | desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit; |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 503 | } else { |
| 504 | break; |
| 505 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 506 | |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 507 | } while (true); |
| 508 | |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 509 | if (NULL == resource) { |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 510 | desc.fFlags = inDesc.fFlags; |
| 511 | desc.fWidth = origWidth; |
| 512 | desc.fHeight = origHeight; |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 513 | resource = create_scratch_texture(fGpu, fResourceCache, desc); |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 514 | } |
| 515 | |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 516 | return static_cast<GrTexture*>(resource); |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 517 | } |
| 518 | |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 519 | void GrContext::addExistingTextureToCache(GrTexture* texture) { |
| 520 | |
| 521 | if (NULL == texture) { |
| 522 | return; |
| 523 | } |
| 524 | |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 525 | // This texture should already have a cache entry since it was once |
| 526 | // attached |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 527 | SkASSERT(texture->getCacheEntry()); |
robertphillips@google.com | 9c2ea84 | 2012-08-13 17:47:59 +0000 | [diff] [blame] | 528 | |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 529 | // Conceptually, the cache entry is going to assume responsibility |
robertphillips@google.com | 9ef0426 | 2013-10-29 14:06:15 +0000 | [diff] [blame] | 530 | // for the creation ref. Assert refcnt == 1. |
bsalomon | 944bcf0 | 2014-07-29 08:01:52 -0700 | [diff] [blame] | 531 | // Except that this also gets called when the texture is prematurely |
| 532 | // abandoned. In that case the ref count may be > 1. |
| 533 | // SkASSERT(texture->unique()); |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 534 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 535 | if (fGpu->caps()->reuseScratchTextures() || texture->asRenderTarget()) { |
robertphillips@google.com | 9ef0426 | 2013-10-29 14:06:15 +0000 | [diff] [blame] | 536 | // Since this texture came from an AutoScratchTexture it should |
| 537 | // still be in the exclusive pile. Recycle it. |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 538 | fResourceCache->makeNonExclusive(texture->getCacheEntry()); |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 539 | this->purgeCache(); |
bsalomon | dcabb05 | 2014-07-21 14:24:01 -0700 | [diff] [blame] | 540 | } else { |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 541 | // When we aren't reusing textures we know this scratch texture |
| 542 | // will never be reused and would be just wasting time in the cache |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 543 | fResourceCache->makeNonExclusive(texture->getCacheEntry()); |
| 544 | fResourceCache->deleteResource(texture->getCacheEntry()); |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 545 | } |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 546 | } |
| 547 | |
robertphillips@google.com | 9fbcad0 | 2012-09-09 14:44:15 +0000 | [diff] [blame] | 548 | void GrContext::unlockScratchTexture(GrTexture* texture) { |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 549 | if (texture->wasDestroyed()) { |
| 550 | if (texture->getCacheEntry()->key().isScratch()) { |
| 551 | // This texture was detached from the cache but the cache still had a ref to it but |
bsalomon | 02e36f2 | 2014-08-22 12:01:46 -0700 | [diff] [blame] | 552 | // not a pointer to it. This will unref the texture and delete its resource cache |
| 553 | // entry. |
| 554 | delete texture->getCacheEntry(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 555 | } |
| 556 | return; |
| 557 | } |
| 558 | |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 559 | ASSERT_OWNED_RESOURCE(texture); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 560 | SkASSERT(texture->getCacheEntry()); |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 561 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 562 | // If this is a scratch texture we detached it from the cache |
| 563 | // while it was locked (to avoid two callers simultaneously getting |
| 564 | // the same texture). |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 565 | if (texture->getCacheEntry()->key().isScratch()) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 566 | if (fGpu->caps()->reuseScratchTextures() || texture->asRenderTarget()) { |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 567 | fResourceCache->makeNonExclusive(texture->getCacheEntry()); |
robertphillips@google.com | 9ef0426 | 2013-10-29 14:06:15 +0000 | [diff] [blame] | 568 | this->purgeCache(); |
bsalomon | dcabb05 | 2014-07-21 14:24:01 -0700 | [diff] [blame] | 569 | } else if (texture->unique()) { |
skia.committer@gmail.com | b77f0f4 | 2013-10-30 07:01:56 +0000 | [diff] [blame] | 570 | // Only the cache now knows about this texture. Since we're never |
| 571 | // reusing scratch textures (in this code path) it would just be |
robertphillips@google.com | 9ef0426 | 2013-10-29 14:06:15 +0000 | [diff] [blame] | 572 | // wasting time sitting in the cache. |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 573 | fResourceCache->makeNonExclusive(texture->getCacheEntry()); |
| 574 | fResourceCache->deleteResource(texture->getCacheEntry()); |
robertphillips@google.com | 9ef0426 | 2013-10-29 14:06:15 +0000 | [diff] [blame] | 575 | } else { |
bsalomon | dcabb05 | 2014-07-21 14:24:01 -0700 | [diff] [blame] | 576 | // In this case (there is still a non-cache ref) but we don't really |
skia.committer@gmail.com | b77f0f4 | 2013-10-30 07:01:56 +0000 | [diff] [blame] | 577 | // want to readd it to the cache (since it will never be reused). |
robertphillips@google.com | 9ef0426 | 2013-10-29 14:06:15 +0000 | [diff] [blame] | 578 | // Instead, give up the cache's ref and leave the decision up to |
| 579 | // addExistingTextureToCache once its ref count reaches 0. For |
| 580 | // this to work we need to leave it in the exclusive list. |
commit-bot@chromium.org | e49157f | 2014-05-09 20:46:48 +0000 | [diff] [blame] | 581 | texture->impl()->setFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_FlagBit); |
robertphillips@google.com | 9ef0426 | 2013-10-29 14:06:15 +0000 | [diff] [blame] | 582 | // Give up the cache's ref to the texture |
| 583 | texture->unref(); |
| 584 | } |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 585 | } |
robertphillips@google.com | 50a035d | 2012-09-07 19:44:33 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | void GrContext::purgeCache() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 589 | if (fResourceCache) { |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 590 | fResourceCache->purgeAsNeeded(); |
robertphillips@google.com | eb9b3e1 | 2012-09-11 12:50:53 +0000 | [diff] [blame] | 591 | } |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 592 | } |
| 593 | |
commit-bot@chromium.org | cae27fe | 2013-07-10 10:14:35 +0000 | [diff] [blame] | 594 | bool GrContext::OverbudgetCB(void* data) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 595 | SkASSERT(data); |
commit-bot@chromium.org | cae27fe | 2013-07-10 10:14:35 +0000 | [diff] [blame] | 596 | |
| 597 | GrContext* context = reinterpret_cast<GrContext*>(data); |
| 598 | |
| 599 | // Flush the InOrderDrawBuffer to possibly free up some textures |
commit-bot@chromium.org | 5dbb149 | 2013-10-04 16:23:58 +0000 | [diff] [blame] | 600 | context->fFlushToReduceCacheSize = true; |
commit-bot@chromium.org | cae27fe | 2013-07-10 10:14:35 +0000 | [diff] [blame] | 601 | |
commit-bot@chromium.org | cae27fe | 2013-07-10 10:14:35 +0000 | [diff] [blame] | 602 | return true; |
| 603 | } |
| 604 | |
| 605 | |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 606 | GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn, |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 607 | void* srcData, |
| 608 | size_t rowBytes) { |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 609 | GrTextureDesc descCopy = descIn; |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 610 | return fGpu->createTexture(descCopy, srcData, rowBytes); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 611 | } |
| 612 | |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 613 | void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const { |
| 614 | fResourceCache->getLimits(maxTextures, maxTextureBytes); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 615 | } |
| 616 | |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 617 | void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) { |
| 618 | fResourceCache->setLimits(maxTextures, maxTextureBytes); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 619 | } |
| 620 | |
bsalomon@google.com | 9195836 | 2011-06-13 17:58:13 +0000 | [diff] [blame] | 621 | int GrContext::getMaxTextureSize() const { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 622 | return SkTMin(fGpu->caps()->maxTextureSize(), fMaxTextureSizeOverride); |
bsalomon@google.com | 9195836 | 2011-06-13 17:58:13 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | int GrContext::getMaxRenderTargetSize() const { |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 626 | return fGpu->caps()->maxRenderTargetSize(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 627 | } |
| 628 | |
bsalomon@google.com | 8a70eef | 2013-03-19 13:58:55 +0000 | [diff] [blame] | 629 | int GrContext::getMaxSampleCount() const { |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 630 | return fGpu->caps()->maxSampleCount(); |
bsalomon@google.com | 8a70eef | 2013-03-19 13:58:55 +0000 | [diff] [blame] | 631 | } |
| 632 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 633 | /////////////////////////////////////////////////////////////////////////////// |
| 634 | |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 635 | GrTexture* GrContext::wrapBackendTexture(const GrBackendTextureDesc& desc) { |
| 636 | return fGpu->wrapBackendTexture(desc); |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 637 | } |
| 638 | |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 639 | GrRenderTarget* GrContext::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) { |
| 640 | return fGpu->wrapBackendRenderTarget(desc); |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 641 | } |
| 642 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 643 | /////////////////////////////////////////////////////////////////////////////// |
| 644 | |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 645 | bool GrContext::supportsIndex8PixelConfig(const GrTextureParams* params, |
bsalomon@google.com | 1f221a7 | 2011-08-23 20:54:07 +0000 | [diff] [blame] | 646 | int width, int height) const { |
bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 647 | const GrDrawTargetCaps* caps = fGpu->caps(); |
commit-bot@chromium.org | 6e7ddaa | 2014-05-30 13:55:58 +0000 | [diff] [blame] | 648 | if (!caps->isConfigTexturable(kIndex_8_GrPixelConfig)) { |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 649 | return false; |
| 650 | } |
| 651 | |
tfarina | f9dae78 | 2014-06-06 06:35:28 -0700 | [diff] [blame] | 652 | bool isPow2 = SkIsPow2(width) && SkIsPow2(height); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 653 | |
| 654 | if (!isPow2) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 655 | bool tiled = params && params->isTiled(); |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 656 | if (tiled && !caps->npotTextureTileSupport()) { |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 657 | return false; |
| 658 | } |
| 659 | } |
| 660 | return true; |
| 661 | } |
| 662 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 663 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 664 | //////////////////////////////////////////////////////////////////////////////// |
| 665 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 666 | void GrContext::clear(const SkIRect* rect, |
bsalomon@google.com | 07ea2db | 2012-08-17 14:06:49 +0000 | [diff] [blame] | 667 | const GrColor color, |
robertphillips@google.com | 56ce48a | 2013-10-31 21:44:25 +0000 | [diff] [blame] | 668 | bool canIgnoreRect, |
bsalomon | 41ebbdd | 2014-08-04 08:31:39 -0700 | [diff] [blame] | 669 | GrRenderTarget* renderTarget) { |
| 670 | ASSERT_OWNED_RESOURCE(renderTarget); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 671 | AutoRestoreEffects are; |
commit-bot@chromium.org | 5dbb149 | 2013-10-04 16:23:58 +0000 | [diff] [blame] | 672 | AutoCheckFlush acf(this); |
egdaniel | d78a168 | 2014-07-09 10:41:26 -0700 | [diff] [blame] | 673 | GR_CREATE_TRACE_MARKER_CONTEXT("GrContext::clear", this); |
bsalomon | 41ebbdd | 2014-08-04 08:31:39 -0700 | [diff] [blame] | 674 | GrDrawTarget* target = this->prepareToDraw(NULL, BUFFERED_DRAW, &are, &acf); |
| 675 | if (NULL == target) { |
| 676 | return; |
| 677 | } |
| 678 | target->clear(rect, color, canIgnoreRect, renderTarget); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 679 | } |
| 680 | |
bsalomon@google.com | 5dc26b9 | 2012-10-11 19:32:32 +0000 | [diff] [blame] | 681 | void GrContext::drawPaint(const GrPaint& origPaint) { |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 682 | // set rect to be big enough to fill the space, but not super-huge, so we |
| 683 | // don't overflow fixed-point implementations |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 684 | SkRect r; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 685 | r.setLTRB(0, 0, |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 686 | SkIntToScalar(getRenderTarget()->width()), |
| 687 | SkIntToScalar(getRenderTarget()->height())); |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 688 | SkMatrix inverse; |
bsalomon@google.com | 5dc26b9 | 2012-10-11 19:32:32 +0000 | [diff] [blame] | 689 | SkTCopyOnFirstWrite<GrPaint> paint(origPaint); |
robertphillips@google.com | fea85ac | 2012-07-11 18:53:23 +0000 | [diff] [blame] | 690 | AutoMatrix am; |
egdaniel | d78a168 | 2014-07-09 10:41:26 -0700 | [diff] [blame] | 691 | GR_CREATE_TRACE_MARKER_CONTEXT("GrContext::drawPaint", this); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 692 | |
bsalomon@google.com | 4f83be8 | 2011-09-12 13:52:51 +0000 | [diff] [blame] | 693 | // We attempt to map r by the inverse matrix and draw that. mapRect will |
| 694 | // map the four corners and bound them with a new rect. This will not |
| 695 | // produce a correct result for some perspective matrices. |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 696 | if (!this->getMatrix().hasPerspective()) { |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 697 | if (!fViewMatrix.invert(&inverse)) { |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 698 | GrPrintf("Could not invert matrix\n"); |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 699 | return; |
| 700 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 701 | inverse.mapRect(&r); |
| 702 | } else { |
bsalomon@google.com | 3cbaa2d | 2012-10-12 14:51:52 +0000 | [diff] [blame] | 703 | if (!am.setIdentity(this, paint.writable())) { |
| 704 | GrPrintf("Could not invert matrix\n"); |
| 705 | return; |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 706 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 707 | } |
bsalomon@google.com | 4f83be8 | 2011-09-12 13:52:51 +0000 | [diff] [blame] | 708 | // by definition this fills the entire clip, no need for AA |
bsalomon@google.com | 5dc26b9 | 2012-10-11 19:32:32 +0000 | [diff] [blame] | 709 | if (paint->isAntiAlias()) { |
| 710 | paint.writable()->setAntiAlias(false); |
bsalomon@google.com | 4f83be8 | 2011-09-12 13:52:51 +0000 | [diff] [blame] | 711 | } |
bsalomon@google.com | 5dc26b9 | 2012-10-11 19:32:32 +0000 | [diff] [blame] | 712 | this->drawRect(*paint, r); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 713 | } |
| 714 | |
commit-bot@chromium.org | 03e3e89 | 2013-10-02 18:19:17 +0000 | [diff] [blame] | 715 | #ifdef SK_DEVELOPER |
| 716 | void GrContext::dumpFontCache() const { |
| 717 | fFontCache->dump(); |
| 718 | } |
| 719 | #endif |
| 720 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 721 | //////////////////////////////////////////////////////////////////////////////// |
| 722 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 723 | /* create a triangle strip that strokes the specified triangle. There are 8 |
| 724 | unique vertices, but we repreat the last 2 to close up. Alternatively we |
| 725 | could use an indices array, and then only send 8 verts, but not sure that |
| 726 | would be faster. |
| 727 | */ |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 728 | static void setStrokeRectStrip(SkPoint verts[10], SkRect rect, |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 729 | SkScalar width) { |
| 730 | const SkScalar rad = SkScalarHalf(width); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 731 | rect.sort(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 732 | |
| 733 | verts[0].set(rect.fLeft + rad, rect.fTop + rad); |
| 734 | verts[1].set(rect.fLeft - rad, rect.fTop - rad); |
| 735 | verts[2].set(rect.fRight - rad, rect.fTop + rad); |
| 736 | verts[3].set(rect.fRight + rad, rect.fTop - rad); |
| 737 | verts[4].set(rect.fRight - rad, rect.fBottom - rad); |
| 738 | verts[5].set(rect.fRight + rad, rect.fBottom + rad); |
| 739 | verts[6].set(rect.fLeft + rad, rect.fBottom - rad); |
| 740 | verts[7].set(rect.fLeft - rad, rect.fBottom + rad); |
| 741 | verts[8] = verts[0]; |
| 742 | verts[9] = verts[1]; |
| 743 | } |
| 744 | |
bsalomon | c30aaa0 | 2014-08-13 07:15:29 -0700 | [diff] [blame] | 745 | static inline bool is_irect(const SkRect& r) { |
| 746 | return SkScalarIsInt(r.fLeft) && SkScalarIsInt(r.fTop) && |
| 747 | SkScalarIsInt(r.fRight) && SkScalarIsInt(r.fBottom); |
| 748 | } |
| 749 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 750 | static bool apply_aa_to_rect(GrDrawTarget* target, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 751 | const SkRect& rect, |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 752 | SkScalar strokeWidth, |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 753 | const SkMatrix& combinedMatrix, |
bsalomon | 9c0822a | 2014-08-11 11:07:48 -0700 | [diff] [blame] | 754 | SkRect* devBoundRect) { |
| 755 | if (!target->getDrawState().canTweakAlphaForCoverage() && |
| 756 | target->shouldDisableCoverageAAForBlend()) { |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 757 | #ifdef SK_DEBUG |
bsalomon | 9c0822a | 2014-08-11 11:07:48 -0700 | [diff] [blame] | 758 | //GrPrintf("Turning off AA to correctly apply blend.\n"); |
bsalomon@google.com | 1983f39 | 2011-10-10 15:17:58 +0000 | [diff] [blame] | 759 | #endif |
bsalomon | 9c0822a | 2014-08-11 11:07:48 -0700 | [diff] [blame] | 760 | return false; |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 761 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 762 | const GrDrawState& drawState = target->getDrawState(); |
| 763 | if (drawState.getRenderTarget()->isMultisampled()) { |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 764 | return false; |
| 765 | } |
| 766 | |
robertphillips@google.com | 4b140b5 | 2013-05-02 17:13:13 +0000 | [diff] [blame] | 767 | #if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT) |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 768 | if (strokeWidth >= 0) { |
| 769 | #endif |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 770 | if (!combinedMatrix.preservesAxisAlignment()) { |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 771 | return false; |
| 772 | } |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 773 | |
robertphillips@google.com | 4b140b5 | 2013-05-02 17:13:13 +0000 | [diff] [blame] | 774 | #if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT) |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 775 | } else { |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 776 | if (!combinedMatrix.preservesRightAngles()) { |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 777 | return false; |
| 778 | } |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 779 | } |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 780 | #endif |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 781 | |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 782 | combinedMatrix.mapRect(devBoundRect, rect); |
bsalomon | c30aaa0 | 2014-08-13 07:15:29 -0700 | [diff] [blame] | 783 | if (strokeWidth < 0) { |
| 784 | return !is_irect(*devBoundRect); |
| 785 | } |
robertphillips@google.com | 28ac96e | 2013-05-13 13:38:35 +0000 | [diff] [blame] | 786 | |
bsalomon | 9c0822a | 2014-08-11 11:07:48 -0700 | [diff] [blame] | 787 | return true; |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 788 | } |
| 789 | |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 790 | static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) { |
| 791 | return point.fX >= rect.fLeft && point.fX <= rect.fRight && |
| 792 | point.fY >= rect.fTop && point.fY <= rect.fBottom; |
| 793 | } |
| 794 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 795 | void GrContext::drawRect(const GrPaint& paint, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 796 | const SkRect& rect, |
bsalomon | 01c8da1 | 2014-08-04 09:21:30 -0700 | [diff] [blame] | 797 | const GrStrokeInfo* strokeInfo) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 798 | if (strokeInfo && strokeInfo->isDashed()) { |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 799 | SkPath path; |
| 800 | path.addRect(rect); |
| 801 | this->drawPath(paint, path, *strokeInfo); |
| 802 | return; |
| 803 | } |
| 804 | |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 805 | AutoRestoreEffects are; |
commit-bot@chromium.org | 5dbb149 | 2013-10-04 16:23:58 +0000 | [diff] [blame] | 806 | AutoCheckFlush acf(this); |
| 807 | GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf); |
bsalomon | 41ebbdd | 2014-08-04 08:31:39 -0700 | [diff] [blame] | 808 | if (NULL == target) { |
| 809 | return; |
| 810 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 811 | |
commit-bot@chromium.org | 2a05de0 | 2014-03-25 15:17:32 +0000 | [diff] [blame] | 812 | GR_CREATE_TRACE_MARKER("GrContext::drawRect", target); |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 813 | SkScalar width = NULL == strokeInfo ? -1 : strokeInfo->getStrokeRec().getWidth(); |
bsalomon | 01c8da1 | 2014-08-04 09:21:30 -0700 | [diff] [blame] | 814 | SkMatrix matrix = target->drawState()->getViewMatrix(); |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 815 | |
| 816 | // Check if this is a full RT draw and can be replaced with a clear. We don't bother checking |
| 817 | // cases where the RT is fully inside a stroke. |
| 818 | if (width < 0) { |
| 819 | SkRect rtRect; |
| 820 | target->getDrawState().getRenderTarget()->getBoundsRect(&rtRect); |
| 821 | SkRect clipSpaceRTRect = rtRect; |
| 822 | bool checkClip = false; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 823 | if (this->getClip()) { |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 824 | checkClip = true; |
| 825 | clipSpaceRTRect.offset(SkIntToScalar(this->getClip()->fOrigin.fX), |
| 826 | SkIntToScalar(this->getClip()->fOrigin.fY)); |
| 827 | } |
| 828 | // Does the clip contain the entire RT? |
| 829 | if (!checkClip || target->getClip()->fClipStack->quickContains(clipSpaceRTRect)) { |
| 830 | SkMatrix invM; |
bsalomon | 01c8da1 | 2014-08-04 09:21:30 -0700 | [diff] [blame] | 831 | if (!matrix.invert(&invM)) { |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 832 | return; |
| 833 | } |
| 834 | // Does the rect bound the RT? |
| 835 | SkPoint srcSpaceRTQuad[4]; |
| 836 | invM.mapRectToQuad(srcSpaceRTQuad, rtRect); |
| 837 | if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) && |
| 838 | rect_contains_inclusive(rect, srcSpaceRTQuad[1]) && |
| 839 | rect_contains_inclusive(rect, srcSpaceRTQuad[2]) && |
| 840 | rect_contains_inclusive(rect, srcSpaceRTQuad[3])) { |
| 841 | // Will it blend? |
| 842 | GrColor clearColor; |
| 843 | if (paint.isOpaqueAndConstantColor(&clearColor)) { |
robertphillips@google.com | 56ce48a | 2013-10-31 21:44:25 +0000 | [diff] [blame] | 844 | target->clear(NULL, clearColor, true); |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 845 | return; |
| 846 | } |
| 847 | } |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | SkRect devBoundRect; |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 852 | bool needAA = paint.isAntiAlias() && |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 853 | !target->getDrawState().getRenderTarget()->isMultisampled(); |
bsalomon | 9c0822a | 2014-08-11 11:07:48 -0700 | [diff] [blame] | 854 | bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix, &devBoundRect); |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 855 | |
| 856 | const SkStrokeRec& strokeRec = strokeInfo->getStrokeRec(); |
| 857 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 858 | if (doAA) { |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 859 | GrDrawState::AutoViewMatrixRestore avmr; |
| 860 | if (!avmr.setIdentity(target->drawState())) { |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 861 | return; |
| 862 | } |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 863 | if (width >= 0) { |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 864 | fAARectRenderer->strokeAARect(this->getGpu(), target, rect, |
bsalomon | 01c8da1 | 2014-08-04 09:21:30 -0700 | [diff] [blame] | 865 | matrix, devBoundRect, |
bsalomon | 9c0822a | 2014-08-11 11:07:48 -0700 | [diff] [blame] | 866 | strokeRec); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 867 | } else { |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 868 | // filled AA rect |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 869 | fAARectRenderer->fillAARect(this->getGpu(), target, |
bsalomon | 9c0822a | 2014-08-11 11:07:48 -0700 | [diff] [blame] | 870 | rect, matrix, devBoundRect); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 871 | } |
| 872 | return; |
| 873 | } |
| 874 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 875 | if (width >= 0) { |
| 876 | // TODO: consider making static vertex buffers for these cases. |
bsalomon@google.com | 6438695 | 2013-02-08 21:22:44 +0000 | [diff] [blame] | 877 | // Hairline could be done by just adding closing vertex to |
| 878 | // unitSquareVertexBuffer() |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 879 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 880 | static const int worstCaseVertCount = 10; |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 881 | target->drawState()->setDefaultVertexAttribs(); |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 882 | GrDrawTarget::AutoReleaseGeometry geo(target, worstCaseVertCount, 0); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 883 | |
| 884 | if (!geo.succeeded()) { |
bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 885 | GrPrintf("Failed to get space for vertices!\n"); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 886 | return; |
| 887 | } |
| 888 | |
| 889 | GrPrimitiveType primType; |
| 890 | int vertCount; |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 891 | SkPoint* vertex = geo.positions(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 892 | |
| 893 | if (width > 0) { |
| 894 | vertCount = 10; |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 895 | primType = kTriangleStrip_GrPrimitiveType; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 896 | setStrokeRectStrip(vertex, rect, width); |
| 897 | } else { |
| 898 | // hairline |
| 899 | vertCount = 5; |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 900 | primType = kLineStrip_GrPrimitiveType; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 901 | vertex[0].set(rect.fLeft, rect.fTop); |
| 902 | vertex[1].set(rect.fRight, rect.fTop); |
| 903 | vertex[2].set(rect.fRight, rect.fBottom); |
| 904 | vertex[3].set(rect.fLeft, rect.fBottom); |
| 905 | vertex[4].set(rect.fLeft, rect.fTop); |
| 906 | } |
| 907 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 908 | target->drawNonIndexed(primType, 0, vertCount); |
| 909 | } else { |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 910 | // filled BW rect |
bsalomon | 01c8da1 | 2014-08-04 09:21:30 -0700 | [diff] [blame] | 911 | target->drawSimpleRect(rect); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 912 | } |
| 913 | } |
| 914 | |
| 915 | void GrContext::drawRectToRect(const GrPaint& paint, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 916 | const SkRect& dstRect, |
| 917 | const SkRect& localRect, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 918 | const SkMatrix* localMatrix) { |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 919 | AutoRestoreEffects are; |
commit-bot@chromium.org | 5dbb149 | 2013-10-04 16:23:58 +0000 | [diff] [blame] | 920 | AutoCheckFlush acf(this); |
| 921 | GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf); |
bsalomon | 41ebbdd | 2014-08-04 08:31:39 -0700 | [diff] [blame] | 922 | if (NULL == target) { |
| 923 | return; |
| 924 | } |
bsalomon@google.com | 6438695 | 2013-02-08 21:22:44 +0000 | [diff] [blame] | 925 | |
commit-bot@chromium.org | 2a05de0 | 2014-03-25 15:17:32 +0000 | [diff] [blame] | 926 | GR_CREATE_TRACE_MARKER("GrContext::drawRectToRect", target); |
| 927 | |
bsalomon | 01c8da1 | 2014-08-04 09:21:30 -0700 | [diff] [blame] | 928 | target->drawRect(dstRect, &localRect, localMatrix); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 929 | } |
| 930 | |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 931 | namespace { |
| 932 | |
| 933 | extern const GrVertexAttrib gPosUVColorAttribs[] = { |
| 934 | {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding }, |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 935 | {kVec2f_GrVertexAttribType, sizeof(SkPoint), kLocalCoord_GrVertexAttribBinding }, |
| 936 | {kVec4ub_GrVertexAttribType, 2*sizeof(SkPoint), kColor_GrVertexAttribBinding} |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 937 | }; |
| 938 | |
egdaniel | 7b3d5ee | 2014-08-28 05:41:14 -0700 | [diff] [blame] | 939 | static const size_t kPosUVAttribsSize = 2 * sizeof(SkPoint); |
| 940 | static const size_t kPosUVColorAttribsSize = 2 * sizeof(SkPoint) + sizeof(GrColor); |
| 941 | |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 942 | extern const GrVertexAttrib gPosColorAttribs[] = { |
| 943 | {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 944 | {kVec4ub_GrVertexAttribType, sizeof(SkPoint), kColor_GrVertexAttribBinding}, |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 945 | }; |
| 946 | |
egdaniel | 7b3d5ee | 2014-08-28 05:41:14 -0700 | [diff] [blame] | 947 | static const size_t kPosAttribsSize = sizeof(SkPoint); |
| 948 | static const size_t kPosColorAttribsSize = sizeof(SkPoint) + sizeof(GrColor); |
| 949 | |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 950 | static void set_vertex_attributes(GrDrawState* drawState, |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 951 | const SkPoint* texCoords, |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 952 | const GrColor* colors, |
| 953 | int* colorOffset, |
| 954 | int* texOffset) { |
| 955 | *texOffset = -1; |
| 956 | *colorOffset = -1; |
| 957 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 958 | if (texCoords && colors) { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 959 | *texOffset = sizeof(SkPoint); |
| 960 | *colorOffset = 2*sizeof(SkPoint); |
egdaniel | 7b3d5ee | 2014-08-28 05:41:14 -0700 | [diff] [blame] | 961 | drawState->setVertexAttribs<gPosUVColorAttribs>(3, kPosUVColorAttribsSize); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 962 | } else if (texCoords) { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 963 | *texOffset = sizeof(SkPoint); |
egdaniel | 7b3d5ee | 2014-08-28 05:41:14 -0700 | [diff] [blame] | 964 | drawState->setVertexAttribs<gPosUVColorAttribs>(2, kPosUVAttribsSize); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 965 | } else if (colors) { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 966 | *colorOffset = sizeof(SkPoint); |
egdaniel | 7b3d5ee | 2014-08-28 05:41:14 -0700 | [diff] [blame] | 967 | drawState->setVertexAttribs<gPosColorAttribs>(2, kPosColorAttribsSize); |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 968 | } else { |
egdaniel | 7b3d5ee | 2014-08-28 05:41:14 -0700 | [diff] [blame] | 969 | drawState->setVertexAttribs<gPosColorAttribs>(1, kPosAttribsSize); |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 970 | } |
| 971 | } |
| 972 | |
| 973 | }; |
| 974 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 975 | void GrContext::drawVertices(const GrPaint& paint, |
| 976 | GrPrimitiveType primitiveType, |
| 977 | int vertexCount, |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 978 | const SkPoint positions[], |
| 979 | const SkPoint texCoords[], |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 980 | const GrColor colors[], |
| 981 | const uint16_t indices[], |
| 982 | int indexCount) { |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 983 | AutoRestoreEffects are; |
commit-bot@chromium.org | 5dbb149 | 2013-10-04 16:23:58 +0000 | [diff] [blame] | 984 | AutoCheckFlush acf(this); |
commit-bot@chromium.org | 5a56793 | 2014-01-08 21:26:09 +0000 | [diff] [blame] | 985 | GrDrawTarget::AutoReleaseGeometry geo; // must be inside AutoCheckFlush scope |
| 986 | |
commit-bot@chromium.org | 5dbb149 | 2013-10-04 16:23:58 +0000 | [diff] [blame] | 987 | GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf); |
bsalomon | 41ebbdd | 2014-08-04 08:31:39 -0700 | [diff] [blame] | 988 | if (NULL == target) { |
| 989 | return; |
| 990 | } |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 991 | GrDrawState* drawState = target->drawState(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 992 | |
commit-bot@chromium.org | 2a05de0 | 2014-03-25 15:17:32 +0000 | [diff] [blame] | 993 | GR_CREATE_TRACE_MARKER("GrContext::drawVertices", target); |
| 994 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 995 | int colorOffset = -1, texOffset = -1; |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 996 | set_vertex_attributes(drawState, texCoords, colors, &colorOffset, &texOffset); |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 997 | |
egdaniel | 7b3d5ee | 2014-08-28 05:41:14 -0700 | [diff] [blame] | 998 | size_t VertexStride = drawState->getVertexStride(); |
| 999 | if (sizeof(SkPoint) != VertexStride) { |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 1000 | if (!geo.set(target, vertexCount, 0)) { |
bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 1001 | GrPrintf("Failed to get space for vertices!\n"); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1002 | return; |
| 1003 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1004 | void* curVertex = geo.vertices(); |
| 1005 | |
| 1006 | for (int i = 0; i < vertexCount; ++i) { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 1007 | *((SkPoint*)curVertex) = positions[i]; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1008 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 1009 | if (texOffset >= 0) { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 1010 | *(SkPoint*)((intptr_t)curVertex + texOffset) = texCoords[i]; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1011 | } |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 1012 | if (colorOffset >= 0) { |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1013 | *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i]; |
| 1014 | } |
egdaniel | 7b3d5ee | 2014-08-28 05:41:14 -0700 | [diff] [blame] | 1015 | curVertex = (void*)((intptr_t)curVertex + VertexStride); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1016 | } |
| 1017 | } else { |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 1018 | target->setVertexSourceToArray(positions, vertexCount); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1021 | // we don't currently apply offscreen AA to this path. Need improved |
bsalomon@google.com | 9195836 | 2011-06-13 17:58:13 +0000 | [diff] [blame] | 1022 | // management of GrDrawTarget's geometry to avoid copying points per-tile. |
bsalomon@google.com | a47a48d | 2011-04-26 20:22:11 +0000 | [diff] [blame] | 1023 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1024 | if (indices) { |
bsalomon@google.com | 9195836 | 2011-06-13 17:58:13 +0000 | [diff] [blame] | 1025 | target->setIndexSourceToArray(indices, indexCount); |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1026 | target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount); |
bsalomon@google.com | 0406b9e | 2013-04-02 21:00:15 +0000 | [diff] [blame] | 1027 | target->resetIndexSource(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1028 | } else { |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1029 | target->drawNonIndexed(primitiveType, 0, vertexCount); |
| 1030 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 1033 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 1034 | |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1035 | void GrContext::drawRRect(const GrPaint& paint, |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1036 | const SkRRect& rrect, |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1037 | const GrStrokeInfo& strokeInfo) { |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1038 | if (rrect.isEmpty()) { |
commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 1039 | return; |
| 1040 | } |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1041 | |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1042 | if (strokeInfo.isDashed()) { |
| 1043 | SkPath path; |
| 1044 | path.addRRect(rrect); |
| 1045 | this->drawPath(paint, path, strokeInfo); |
| 1046 | return; |
| 1047 | } |
| 1048 | |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 1049 | AutoRestoreEffects are; |
commit-bot@chromium.org | 5dbb149 | 2013-10-04 16:23:58 +0000 | [diff] [blame] | 1050 | AutoCheckFlush acf(this); |
| 1051 | GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf); |
bsalomon | 41ebbdd | 2014-08-04 08:31:39 -0700 | [diff] [blame] | 1052 | if (NULL == target) { |
| 1053 | return; |
| 1054 | } |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1055 | |
commit-bot@chromium.org | 2a05de0 | 2014-03-25 15:17:32 +0000 | [diff] [blame] | 1056 | GR_CREATE_TRACE_MARKER("GrContext::drawRRect", target); |
| 1057 | |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1058 | const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec(); |
| 1059 | |
| 1060 | if (!fOvalRenderer->drawRRect(target, this, paint.isAntiAlias(), rrect, strokeRec)) { |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1061 | SkPath path; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1062 | path.addRRect(rrect); |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1063 | this->internalDrawPath(target, paint.isAntiAlias(), path, strokeInfo); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | /////////////////////////////////////////////////////////////////////////////// |
| 1068 | |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1069 | void GrContext::drawDRRect(const GrPaint& paint, |
| 1070 | const SkRRect& outer, |
| 1071 | const SkRRect& inner) { |
| 1072 | if (outer.isEmpty()) { |
| 1073 | return; |
| 1074 | } |
| 1075 | |
| 1076 | AutoRestoreEffects are; |
| 1077 | AutoCheckFlush acf(this); |
| 1078 | GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf); |
| 1079 | |
| 1080 | GR_CREATE_TRACE_MARKER("GrContext::drawDRRect", target); |
| 1081 | |
| 1082 | if (!fOvalRenderer->drawDRRect(target, this, paint.isAntiAlias(), outer, inner)) { |
| 1083 | SkPath path; |
| 1084 | path.addRRect(inner); |
| 1085 | path.addRRect(outer); |
| 1086 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 1087 | |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1088 | GrStrokeInfo fillRec(SkStrokeRec::kFill_InitStyle); |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1089 | this->internalDrawPath(target, paint.isAntiAlias(), path, fillRec); |
| 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | /////////////////////////////////////////////////////////////////////////////// |
| 1094 | |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 1095 | void GrContext::drawOval(const GrPaint& paint, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 1096 | const SkRect& oval, |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1097 | const GrStrokeInfo& strokeInfo) { |
commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 1098 | if (oval.isEmpty()) { |
| 1099 | return; |
| 1100 | } |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 1101 | |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1102 | if (strokeInfo.isDashed()) { |
| 1103 | SkPath path; |
| 1104 | path.addOval(oval); |
| 1105 | this->drawPath(paint, path, strokeInfo); |
| 1106 | return; |
| 1107 | } |
| 1108 | |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 1109 | AutoRestoreEffects are; |
commit-bot@chromium.org | 5dbb149 | 2013-10-04 16:23:58 +0000 | [diff] [blame] | 1110 | AutoCheckFlush acf(this); |
| 1111 | GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf); |
bsalomon | 41ebbdd | 2014-08-04 08:31:39 -0700 | [diff] [blame] | 1112 | if (NULL == target) { |
| 1113 | return; |
| 1114 | } |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 1115 | |
commit-bot@chromium.org | 2a05de0 | 2014-03-25 15:17:32 +0000 | [diff] [blame] | 1116 | GR_CREATE_TRACE_MARKER("GrContext::drawOval", target); |
| 1117 | |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1118 | const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec(); |
| 1119 | |
| 1120 | |
| 1121 | if (!fOvalRenderer->drawOval(target, this, paint.isAntiAlias(), oval, strokeRec)) { |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 1122 | SkPath path; |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 1123 | path.addOval(oval); |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1124 | this->internalDrawPath(target, paint.isAntiAlias(), path, strokeInfo); |
skia.committer@gmail.com | 98ded84 | 2013-01-23 07:06:17 +0000 | [diff] [blame] | 1125 | } |
bsalomon@google.com | 150d284 | 2012-01-12 20:19:56 +0000 | [diff] [blame] | 1126 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1127 | |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1128 | // Can 'path' be drawn as a pair of filled nested rectangles? |
| 1129 | static bool is_nested_rects(GrDrawTarget* target, |
| 1130 | const SkPath& path, |
| 1131 | const SkStrokeRec& stroke, |
bsalomon | 9c0822a | 2014-08-11 11:07:48 -0700 | [diff] [blame] | 1132 | SkRect rects[2]) { |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1133 | SkASSERT(stroke.isFillStyle()); |
| 1134 | |
| 1135 | if (path.isInverseFillType()) { |
| 1136 | return false; |
| 1137 | } |
| 1138 | |
| 1139 | const GrDrawState& drawState = target->getDrawState(); |
| 1140 | |
| 1141 | // TODO: this restriction could be lifted if we were willing to apply |
| 1142 | // the matrix to all the points individually rather than just to the rect |
| 1143 | if (!drawState.getViewMatrix().preservesAxisAlignment()) { |
| 1144 | return false; |
| 1145 | } |
| 1146 | |
bsalomon | 9c0822a | 2014-08-11 11:07:48 -0700 | [diff] [blame] | 1147 | if (!target->getDrawState().canTweakAlphaForCoverage() && |
| 1148 | target->shouldDisableCoverageAAForBlend()) { |
| 1149 | return false; |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | SkPath::Direction dirs[2]; |
| 1153 | if (!path.isNestedRects(rects, dirs)) { |
| 1154 | return false; |
| 1155 | } |
| 1156 | |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 1157 | if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) { |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1158 | // The two rects need to be wound opposite to each other |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 1159 | return false; |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1160 | } |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1161 | |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 1162 | // Right now, nested rects where the margin is not the same width |
| 1163 | // all around do not render correctly |
| 1164 | const SkScalar* outer = rects[0].asScalars(); |
| 1165 | const SkScalar* inner = rects[1].asScalars(); |
| 1166 | |
| 1167 | SkScalar margin = SkScalarAbs(outer[0] - inner[0]); |
| 1168 | for (int i = 1; i < 4; ++i) { |
| 1169 | SkScalar temp = SkScalarAbs(outer[i] - inner[i]); |
| 1170 | if (!SkScalarNearlyEqual(margin, temp)) { |
| 1171 | return false; |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | return true; |
| 1176 | } |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1177 | |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1178 | void GrContext::drawPath(const GrPaint& paint, const SkPath& path, const GrStrokeInfo& strokeInfo) { |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1179 | |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 1180 | if (path.isEmpty()) { |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 1181 | if (path.isInverseFillType()) { |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 1182 | this->drawPaint(paint); |
| 1183 | } |
| 1184 | return; |
| 1185 | } |
| 1186 | |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1187 | if (strokeInfo.isDashed()) { |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 1188 | SkPoint pts[2]; |
| 1189 | if (path.isLine(pts)) { |
| 1190 | AutoRestoreEffects are; |
| 1191 | AutoCheckFlush acf(this); |
| 1192 | GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf); |
bsalomon | 41ebbdd | 2014-08-04 08:31:39 -0700 | [diff] [blame] | 1193 | if (NULL == target) { |
| 1194 | return; |
| 1195 | } |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 1196 | GrDrawState* drawState = target->drawState(); |
| 1197 | |
| 1198 | SkMatrix origViewMatrix = drawState->getViewMatrix(); |
| 1199 | GrDrawState::AutoViewMatrixRestore avmr; |
| 1200 | if (avmr.setIdentity(target->drawState())) { |
| 1201 | if (GrDashingEffect::DrawDashLine(pts, paint, strokeInfo, fGpu, target, |
| 1202 | origViewMatrix)) { |
| 1203 | return; |
| 1204 | } |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | // Filter dashed path into new path with the dashing applied |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1209 | const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo(); |
| 1210 | SkTLazy<SkPath> effectPath; |
| 1211 | GrStrokeInfo newStrokeInfo(strokeInfo, false); |
| 1212 | SkStrokeRec* stroke = newStrokeInfo.getStrokeRecPtr(); |
| 1213 | if (SkDashPath::FilterDashPath(effectPath.init(), path, stroke, NULL, info)) { |
| 1214 | this->drawPath(paint, *effectPath.get(), newStrokeInfo); |
| 1215 | return; |
| 1216 | } |
| 1217 | |
| 1218 | this->drawPath(paint, path, newStrokeInfo); |
| 1219 | return; |
| 1220 | } |
| 1221 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 1222 | // Note that internalDrawPath may sw-rasterize the path into a scratch texture. |
bsalomon@google.com | fb4ce6f | 2012-03-14 13:27:54 +0000 | [diff] [blame] | 1223 | // Scratch textures can be recycled after they are returned to the texture |
| 1224 | // cache. This presents a potential hazard for buffered drawing. However, |
| 1225 | // the writePixels that uploads to the scratch will perform a flush so we're |
| 1226 | // OK. |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 1227 | AutoRestoreEffects are; |
commit-bot@chromium.org | 5dbb149 | 2013-10-04 16:23:58 +0000 | [diff] [blame] | 1228 | AutoCheckFlush acf(this); |
| 1229 | GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf); |
bsalomon | 41ebbdd | 2014-08-04 08:31:39 -0700 | [diff] [blame] | 1230 | if (NULL == target) { |
| 1231 | return; |
| 1232 | } |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 1233 | GrDrawState* drawState = target->drawState(); |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 1234 | |
egdaniel | 93a37bc | 2014-07-21 13:47:57 -0700 | [diff] [blame] | 1235 | GR_CREATE_TRACE_MARKER1("GrContext::drawPath", target, "Is Convex", path.isConvex()); |
commit-bot@chromium.org | 2a05de0 | 2014-03-25 15:17:32 +0000 | [diff] [blame] | 1236 | |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1237 | const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec(); |
| 1238 | |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 1239 | bool useCoverageAA = paint.isAntiAlias() && !drawState->getRenderTarget()->isMultisampled(); |
| 1240 | |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1241 | if (useCoverageAA && strokeRec.getWidth() < 0 && !path.isConvex()) { |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1242 | // Concave AA paths are expensive - try to avoid them for special cases |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1243 | SkRect rects[2]; |
| 1244 | |
bsalomon | 9c0822a | 2014-08-11 11:07:48 -0700 | [diff] [blame] | 1245 | if (is_nested_rects(target, path, strokeRec, rects)) { |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 1246 | SkMatrix origViewMatrix = drawState->getViewMatrix(); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 1247 | GrDrawState::AutoViewMatrixRestore avmr; |
| 1248 | if (!avmr.setIdentity(target->drawState())) { |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1249 | return; |
| 1250 | } |
| 1251 | |
bsalomon | 9c0822a | 2014-08-11 11:07:48 -0700 | [diff] [blame] | 1252 | fAARectRenderer->fillAANestedRects(this->getGpu(), target, rects, origViewMatrix); |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1253 | return; |
| 1254 | } |
| 1255 | } |
| 1256 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 1257 | SkRect ovalRect; |
| 1258 | bool isOval = path.isOval(&ovalRect); |
| 1259 | |
skia.committer@gmail.com | 7e32851 | 2013-03-23 07:01:28 +0000 | [diff] [blame] | 1260 | if (!isOval || path.isInverseFillType() |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1261 | || !fOvalRenderer->drawOval(target, this, paint.isAntiAlias(), ovalRect, strokeRec)) { |
| 1262 | this->internalDrawPath(target, paint.isAntiAlias(), path, strokeInfo); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 1263 | } |
| 1264 | } |
| 1265 | |
bsalomon@google.com | 1b20a10 | 2013-11-08 14:42:56 +0000 | [diff] [blame] | 1266 | void GrContext::internalDrawPath(GrDrawTarget* target, bool useAA, const SkPath& path, |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1267 | const GrStrokeInfo& strokeInfo) { |
commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 1268 | SkASSERT(!path.isEmpty()); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 1269 | |
commit-bot@chromium.org | 2a05de0 | 2014-03-25 15:17:32 +0000 | [diff] [blame] | 1270 | GR_CREATE_TRACE_MARKER("GrContext::internalDrawPath", target); |
| 1271 | |
| 1272 | |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 1273 | // An Assumption here is that path renderer would use some form of tweaking |
| 1274 | // the src color (either the input alpha or in the frag shader) to implement |
| 1275 | // aa. If we have some future driver-mojo path AA that can do the right |
| 1276 | // thing WRT to the blend then we'll need some query on the PR. |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 1277 | bool useCoverageAA = useAA && |
| 1278 | !target->getDrawState().getRenderTarget()->isMultisampled() && |
| 1279 | !target->shouldDisableCoverageAAForBlend(); |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 1280 | |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 1281 | |
| 1282 | GrPathRendererChain::DrawType type = |
| 1283 | useCoverageAA ? GrPathRendererChain::kColorAntiAlias_DrawType : |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 1284 | GrPathRendererChain::kColor_DrawType; |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 1285 | |
robertphillips@google.com | eb928ea | 2013-01-08 13:45:09 +0000 | [diff] [blame] | 1286 | const SkPath* pathPtr = &path; |
commit-bot@chromium.org | f0c41e2 | 2014-01-14 18:42:34 +0000 | [diff] [blame] | 1287 | SkTLazy<SkPath> tmpPath; |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1288 | SkTCopyOnFirstWrite<SkStrokeRec> stroke(strokeInfo.getStrokeRec()); |
robertphillips@google.com | eb928ea | 2013-01-08 13:45:09 +0000 | [diff] [blame] | 1289 | |
| 1290 | // Try a 1st time without stroking the path and without allowing the SW renderer |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 1291 | GrPathRenderer* pr = this->getPathRenderer(*pathPtr, *stroke, target, false, type); |
robertphillips@google.com | eb928ea | 2013-01-08 13:45:09 +0000 | [diff] [blame] | 1292 | |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 1293 | if (NULL == pr) { |
commit-bot@chromium.org | f0c41e2 | 2014-01-14 18:42:34 +0000 | [diff] [blame] | 1294 | if (!GrPathRenderer::IsStrokeHairlineOrEquivalent(*stroke, this->getMatrix(), NULL)) { |
robertphillips@google.com | eb928ea | 2013-01-08 13:45:09 +0000 | [diff] [blame] | 1295 | // It didn't work the 1st time, so try again with the stroked path |
commit-bot@chromium.org | f0c41e2 | 2014-01-14 18:42:34 +0000 | [diff] [blame] | 1296 | if (stroke->applyToPath(tmpPath.init(), *pathPtr)) { |
| 1297 | pathPtr = tmpPath.get(); |
| 1298 | stroke.writable()->setFillStyle(); |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 1299 | if (pathPtr->isEmpty()) { |
| 1300 | return; |
| 1301 | } |
robertphillips@google.com | eb928ea | 2013-01-08 13:45:09 +0000 | [diff] [blame] | 1302 | } |
| 1303 | } |
commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 1304 | |
robertphillips@google.com | eb928ea | 2013-01-08 13:45:09 +0000 | [diff] [blame] | 1305 | // This time, allow SW renderer |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 1306 | pr = this->getPathRenderer(*pathPtr, *stroke, target, true, type); |
robertphillips@google.com | eb928ea | 2013-01-08 13:45:09 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 1309 | if (NULL == pr) { |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 1310 | #ifdef SK_DEBUG |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 1311 | GrPrintf("Unable to find path renderer compatible with path.\n"); |
bsalomon@google.com | 1983f39 | 2011-10-10 15:17:58 +0000 | [diff] [blame] | 1312 | #endif |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 1313 | return; |
| 1314 | } |
| 1315 | |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 1316 | pr->drawPath(*pathPtr, *stroke, target, useCoverageAA); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1317 | } |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1318 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1319 | //////////////////////////////////////////////////////////////////////////////// |
| 1320 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 1321 | void GrContext::flush(int flagsBitfield) { |
robertphillips@google.com | e7db8d6 | 2013-07-04 11:48:52 +0000 | [diff] [blame] | 1322 | if (NULL == fDrawBuffer) { |
| 1323 | return; |
| 1324 | } |
| 1325 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 1326 | if (kDiscard_FlushBit & flagsBitfield) { |
| 1327 | fDrawBuffer->reset(); |
| 1328 | } else { |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 1329 | fDrawBuffer->flush(); |
junov@google.com | 53a5584 | 2011-06-08 22:55:10 +0000 | [diff] [blame] | 1330 | } |
commit-bot@chromium.org | 5dbb149 | 2013-10-04 16:23:58 +0000 | [diff] [blame] | 1331 | fFlushToReduceCacheSize = false; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 1334 | bool GrContext::writeTexturePixels(GrTexture* texture, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 1335 | int left, int top, int width, int height, |
| 1336 | GrPixelConfig config, const void* buffer, size_t rowBytes, |
| 1337 | uint32_t flags) { |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 1338 | ASSERT_OWNED_RESOURCE(texture); |
| 1339 | |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 1340 | if ((kUnpremul_PixelOpsFlag & flags) || !fGpu->canWriteTexturePixels(texture, config)) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1341 | if (texture->asRenderTarget()) { |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 1342 | return this->writeRenderTargetPixels(texture->asRenderTarget(), |
| 1343 | left, top, width, height, |
| 1344 | config, buffer, rowBytes, flags); |
| 1345 | } else { |
| 1346 | return false; |
| 1347 | } |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 1348 | } |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 1349 | |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1350 | if (!(kDontFlush_PixelOpsFlag & flags)) { |
| 1351 | this->flush(); |
| 1352 | } |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1353 | |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 1354 | return fGpu->writeTexturePixels(texture, left, top, width, height, |
| 1355 | config, buffer, rowBytes); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 1358 | bool GrContext::readTexturePixels(GrTexture* texture, |
| 1359 | int left, int top, int width, int height, |
| 1360 | GrPixelConfig config, void* buffer, size_t rowBytes, |
| 1361 | uint32_t flags) { |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 1362 | ASSERT_OWNED_RESOURCE(texture); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1363 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1364 | GrRenderTarget* target = texture->asRenderTarget(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1365 | if (target) { |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 1366 | return this->readRenderTargetPixels(target, |
| 1367 | left, top, width, height, |
| 1368 | config, buffer, rowBytes, |
| 1369 | flags); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1370 | } else { |
commit-bot@chromium.org | 3f80dd5 | 2013-10-23 15:32:39 +0000 | [diff] [blame] | 1371 | // TODO: make this more efficient for cases where we're reading the entire |
| 1372 | // texture, i.e., use GetTexImage() instead |
| 1373 | |
| 1374 | // create scratch rendertarget and read from that |
| 1375 | GrAutoScratchTexture ast; |
| 1376 | GrTextureDesc desc; |
| 1377 | desc.fFlags = kRenderTarget_GrTextureFlagBit; |
| 1378 | desc.fWidth = width; |
| 1379 | desc.fHeight = height; |
| 1380 | desc.fConfig = config; |
| 1381 | desc.fOrigin = kTopLeft_GrSurfaceOrigin; |
| 1382 | ast.set(this, desc, kExact_ScratchTexMatch); |
| 1383 | GrTexture* dst = ast.texture(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1384 | if (dst && (target = dst->asRenderTarget())) { |
commit-bot@chromium.org | 3f80dd5 | 2013-10-23 15:32:39 +0000 | [diff] [blame] | 1385 | this->copyTexture(texture, target, NULL); |
| 1386 | return this->readRenderTargetPixels(target, |
| 1387 | left, top, width, height, |
| 1388 | config, buffer, rowBytes, |
| 1389 | flags); |
| 1390 | } |
skia.committer@gmail.com | 583b18a | 2013-10-24 07:01:59 +0000 | [diff] [blame] | 1391 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1392 | return false; |
| 1393 | } |
| 1394 | } |
| 1395 | |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 1396 | #include "SkConfig8888.h" |
| 1397 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 1398 | // toggles between RGBA and BGRA |
| 1399 | static SkColorType toggle_colortype32(SkColorType ct) { |
| 1400 | if (kRGBA_8888_SkColorType == ct) { |
| 1401 | return kBGRA_8888_SkColorType; |
| 1402 | } else { |
| 1403 | SkASSERT(kBGRA_8888_SkColorType == ct); |
| 1404 | return kRGBA_8888_SkColorType; |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 1405 | } |
| 1406 | } |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1407 | |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 1408 | bool GrContext::readRenderTargetPixels(GrRenderTarget* target, |
| 1409 | int left, int top, int width, int height, |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 1410 | GrPixelConfig dstConfig, void* buffer, size_t rowBytes, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 1411 | uint32_t flags) { |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 1412 | ASSERT_OWNED_RESOURCE(target); |
| 1413 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1414 | if (NULL == target) { |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 1415 | target = fRenderTarget.get(); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1416 | if (NULL == target) { |
| 1417 | return false; |
| 1418 | } |
| 1419 | } |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1420 | |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1421 | if (!(kDontFlush_PixelOpsFlag & flags)) { |
| 1422 | this->flush(); |
| 1423 | } |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1424 | |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1425 | // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul. |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 1426 | |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1427 | // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll |
| 1428 | // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read. |
| 1429 | bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top, |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 1430 | width, height, dstConfig, |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1431 | rowBytes); |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 1432 | // We ignore the preferred config if it is different than our config unless it is an R/B swap. |
| 1433 | // In that case we'll perform an R and B swap while drawing to a scratch texture of the swapped |
| 1434 | // config. Then we will call readPixels on the scratch with the swapped config. The swaps during |
| 1435 | // the draw cancels out the fact that we call readPixels with a config that is R/B swapped from |
| 1436 | // dstConfig. |
| 1437 | GrPixelConfig readConfig = dstConfig; |
| 1438 | bool swapRAndB = false; |
commit-bot@chromium.org | 5d1d79a | 2013-05-24 18:52:52 +0000 | [diff] [blame] | 1439 | if (GrPixelConfigSwapRAndB(dstConfig) == |
| 1440 | fGpu->preferredReadPixelsConfig(dstConfig, target->config())) { |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 1441 | readConfig = GrPixelConfigSwapRAndB(readConfig); |
| 1442 | swapRAndB = true; |
| 1443 | } |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1444 | |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 1445 | bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1446 | |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 1447 | if (unpremul && !GrPixelConfigIs8888(dstConfig)) { |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1448 | // The unpremul flag is only allowed for these two configs. |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1449 | return false; |
| 1450 | } |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1451 | |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1452 | // If the src is a texture and we would have to do conversions after read pixels, we instead |
| 1453 | // do the conversions by drawing the src to a scratch texture. If we handle any of the |
| 1454 | // conversions in the draw we set the corresponding bool to false so that we don't reapply it |
| 1455 | // on the read back pixels. |
| 1456 | GrTexture* src = target->asTexture(); |
bsalomon@google.com | c4ff22a | 2011-11-10 21:56:21 +0000 | [diff] [blame] | 1457 | GrAutoScratchTexture ast; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1458 | if (src && (swapRAndB || unpremul || flipY)) { |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1459 | // Make the scratch a render target because we don't have a robust readTexturePixels as of |
| 1460 | // yet. It calls this function. |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 1461 | GrTextureDesc desc; |
| 1462 | desc.fFlags = kRenderTarget_GrTextureFlagBit; |
| 1463 | desc.fWidth = width; |
| 1464 | desc.fHeight = height; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1465 | desc.fConfig = readConfig; |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 1466 | desc.fOrigin = kTopLeft_GrSurfaceOrigin; |
bsalomon@google.com | c4ff22a | 2011-11-10 21:56:21 +0000 | [diff] [blame] | 1467 | |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 1468 | // When a full read back is faster than a partial we could always make the scratch exactly |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1469 | // match the passed rect. However, if we see many different size rectangles we will trash |
| 1470 | // our texture cache and pay the cost of creating and destroying many textures. So, we only |
| 1471 | // request an exact match when the caller is reading an entire RT. |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 1472 | ScratchTexMatch match = kApprox_ScratchTexMatch; |
| 1473 | if (0 == left && |
| 1474 | 0 == top && |
| 1475 | target->width() == width && |
| 1476 | target->height() == height && |
| 1477 | fGpu->fullReadPixelsIsFasterThanPartial()) { |
| 1478 | match = kExact_ScratchTexMatch; |
| 1479 | } |
| 1480 | ast.set(this, desc, match); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1481 | GrTexture* texture = ast.texture(); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1482 | if (texture) { |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 1483 | // compute a matrix to perform the draw |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 1484 | SkMatrix textureMatrix; |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 1485 | textureMatrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top); |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 1486 | textureMatrix.postIDiv(src->width(), src->height()); |
| 1487 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 1488 | SkAutoTUnref<const GrEffect> effect; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1489 | if (unpremul) { |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 1490 | effect.reset(this->createPMToUPMEffect(src, swapRAndB, textureMatrix)); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1491 | if (effect) { |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 1492 | unpremul = false; // we no longer need to do this on CPU after the read back. |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 1493 | } |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1494 | } |
| 1495 | // If we failed to create a PM->UPM effect and have no other conversions to perform then |
| 1496 | // there is no longer any point to using the scratch. |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1497 | if (effect || flipY || swapRAndB) { |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 1498 | if (!effect) { |
| 1499 | effect.reset(GrConfigConversionEffect::Create( |
| 1500 | src, |
| 1501 | swapRAndB, |
| 1502 | GrConfigConversionEffect::kNone_PMConversion, |
| 1503 | textureMatrix)); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1504 | } |
| 1505 | swapRAndB = false; // we will handle the swap in the draw. |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1506 | |
robertphillips@google.com | 13f181f | 2013-03-02 12:02:08 +0000 | [diff] [blame] | 1507 | // We protect the existing geometry here since it may not be |
| 1508 | // clear to the caller that a draw operation (i.e., drawSimpleRect) |
| 1509 | // can be invoked in this method |
| 1510 | GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1511 | GrDrawState* drawState = fGpu->drawState(); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 1512 | SkASSERT(effect); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 1513 | drawState->addColorEffect(effect); |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 1514 | |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1515 | drawState->setRenderTarget(texture->asRenderTarget()); |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 1516 | SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)); |
bsalomon | 01c8da1 | 2014-08-04 09:21:30 -0700 | [diff] [blame] | 1517 | fGpu->drawSimpleRect(rect); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1518 | // we want to read back from the scratch's origin |
| 1519 | left = 0; |
| 1520 | top = 0; |
| 1521 | target = texture->asRenderTarget(); |
| 1522 | } |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 1523 | } |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1524 | } |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1525 | if (!fGpu->readPixels(target, |
| 1526 | left, top, width, height, |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 1527 | readConfig, buffer, rowBytes)) { |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1528 | return false; |
| 1529 | } |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 1530 | // Perform any conversions we weren't able to perform using a scratch texture. |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 1531 | if (unpremul || swapRAndB) { |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 1532 | SkDstPixelInfo dstPI; |
| 1533 | if (!GrPixelConfig2ColorType(dstConfig, &dstPI.fColorType)) { |
| 1534 | return false; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1535 | } |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 1536 | dstPI.fAlphaType = kUnpremul_SkAlphaType; |
| 1537 | dstPI.fPixels = buffer; |
| 1538 | dstPI.fRowBytes = rowBytes; |
| 1539 | |
| 1540 | SkSrcPixelInfo srcPI; |
| 1541 | srcPI.fColorType = swapRAndB ? toggle_colortype32(dstPI.fColorType) : dstPI.fColorType; |
| 1542 | srcPI.fAlphaType = kPremul_SkAlphaType; |
| 1543 | srcPI.fPixels = buffer; |
| 1544 | srcPI.fRowBytes = rowBytes; |
| 1545 | |
| 1546 | return srcPI.convertPixelsTo(&dstPI, width, height); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1547 | } |
| 1548 | return true; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 1551 | void GrContext::resolveRenderTarget(GrRenderTarget* target) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 1552 | SkASSERT(target); |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 1553 | ASSERT_OWNED_RESOURCE(target); |
| 1554 | // In the future we may track whether there are any pending draws to this |
| 1555 | // target. We don't today so we always perform a flush. We don't promise |
| 1556 | // this to our clients, though. |
| 1557 | this->flush(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1558 | if (fGpu) { |
bsalomon | 41ebbdd | 2014-08-04 08:31:39 -0700 | [diff] [blame] | 1559 | fGpu->resolveRenderTarget(target); |
| 1560 | } |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
bsalomon | 41ebbdd | 2014-08-04 08:31:39 -0700 | [diff] [blame] | 1563 | void GrContext::discardRenderTarget(GrRenderTarget* renderTarget) { |
| 1564 | SkASSERT(renderTarget); |
| 1565 | ASSERT_OWNED_RESOURCE(renderTarget); |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 1566 | AutoRestoreEffects are; |
| 1567 | AutoCheckFlush acf(this); |
bsalomon | 41ebbdd | 2014-08-04 08:31:39 -0700 | [diff] [blame] | 1568 | GrDrawTarget* target = this->prepareToDraw(NULL, BUFFERED_DRAW, &are, &acf); |
| 1569 | if (NULL == target) { |
| 1570 | return; |
| 1571 | } |
| 1572 | target->discard(renderTarget); |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 1575 | void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst, const SkIPoint* topLeft) { |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 1576 | if (NULL == src || NULL == dst) { |
| 1577 | return; |
| 1578 | } |
| 1579 | ASSERT_OWNED_RESOURCE(src); |
| 1580 | |
twiz@google.com | 1ac87ff | 2012-04-27 19:39:33 +0000 | [diff] [blame] | 1581 | // Writes pending to the source texture are not tracked, so a flush |
| 1582 | // is required to ensure that the copy captures the most recent contents |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 1583 | // of the source texture. See similar behavior in |
twiz@google.com | 1ac87ff | 2012-04-27 19:39:33 +0000 | [diff] [blame] | 1584 | // GrContext::resolveRenderTarget. |
| 1585 | this->flush(); |
| 1586 | |
bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 1587 | GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1588 | GrDrawState* drawState = fGpu->drawState(); |
| 1589 | drawState->setRenderTarget(dst); |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 1590 | SkMatrix sampleM; |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 1591 | sampleM.setIDiv(src->width(), src->height()); |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 1592 | SkIRect srcRect = SkIRect::MakeWH(dst->width(), dst->height()); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1593 | if (topLeft) { |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 1594 | srcRect.offset(*topLeft); |
| 1595 | } |
| 1596 | SkIRect srcBounds = SkIRect::MakeWH(src->width(), src->height()); |
| 1597 | if (!srcRect.intersect(srcBounds)) { |
| 1598 | return; |
| 1599 | } |
| 1600 | sampleM.preTranslate(SkIntToScalar(srcRect.fLeft), SkIntToScalar(srcRect.fTop)); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 1601 | drawState->addColorTextureEffect(src, sampleM); |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 1602 | SkRect dstR = SkRect::MakeWH(SkIntToScalar(srcRect.width()), SkIntToScalar(srcRect.height())); |
bsalomon | 01c8da1 | 2014-08-04 09:21:30 -0700 | [diff] [blame] | 1603 | fGpu->drawSimpleRect(dstR); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 1606 | bool GrContext::writeRenderTargetPixels(GrRenderTarget* target, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 1607 | int left, int top, int width, int height, |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 1608 | GrPixelConfig srcConfig, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 1609 | const void* buffer, |
| 1610 | size_t rowBytes, |
| 1611 | uint32_t flags) { |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 1612 | ASSERT_OWNED_RESOURCE(target); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1613 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1614 | if (NULL == target) { |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 1615 | target = fRenderTarget.get(); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1616 | if (NULL == target) { |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 1617 | return false; |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1618 | } |
| 1619 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1620 | |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1621 | // TODO: when underlying api has a direct way to do this we should use it (e.g. glDrawPixels on |
| 1622 | // desktop GL). |
| 1623 | |
| 1624 | // We will always call some form of writeTexturePixels and we will pass our flags on to it. |
| 1625 | // Thus, we don't perform a flush here since that call will do it (if the kNoFlush flag isn't |
| 1626 | // set.) |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1627 | |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 1628 | // If the RT is also a texture and we don't have to premultiply then take the texture path. |
| 1629 | // We expect to be at least as fast or faster since it doesn't use an intermediate texture as |
| 1630 | // we do below. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1631 | |
commit-bot@chromium.org | 4382330 | 2013-09-25 20:57:51 +0000 | [diff] [blame] | 1632 | #if !defined(SK_BUILD_FOR_MAC) |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1633 | // At least some drivers on the Mac get confused when glTexImage2D is called on a texture |
| 1634 | // attached to an FBO. The FBO still sees the old image. TODO: determine what OS versions and/or |
| 1635 | // HW is affected. |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1636 | if (target->asTexture() && !(kUnpremul_PixelOpsFlag & flags) && |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 1637 | fGpu->canWriteTexturePixels(target->asTexture(), srcConfig)) { |
| 1638 | return this->writeTexturePixels(target->asTexture(), |
| 1639 | left, top, width, height, |
| 1640 | srcConfig, buffer, rowBytes, flags); |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 1641 | } |
| 1642 | #endif |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 1643 | |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 1644 | // We ignore the preferred config unless it is a R/B swap of the src config. In that case |
| 1645 | // we will upload the original src data to a scratch texture but we will spoof it as the swapped |
| 1646 | // config. This scratch will then have R and B swapped. We correct for this by swapping again |
| 1647 | // when drawing the scratch to the dst using a conversion effect. |
| 1648 | bool swapRAndB = false; |
| 1649 | GrPixelConfig writeConfig = srcConfig; |
commit-bot@chromium.org | 5d1d79a | 2013-05-24 18:52:52 +0000 | [diff] [blame] | 1650 | if (GrPixelConfigSwapRAndB(srcConfig) == |
| 1651 | fGpu->preferredWritePixelsConfig(srcConfig, target->config())) { |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 1652 | writeConfig = GrPixelConfigSwapRAndB(srcConfig); |
| 1653 | swapRAndB = true; |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 1654 | } |
| 1655 | |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 1656 | GrTextureDesc desc; |
| 1657 | desc.fWidth = width; |
| 1658 | desc.fHeight = height; |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 1659 | desc.fConfig = writeConfig; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 1660 | GrAutoScratchTexture ast(this, desc); |
| 1661 | GrTexture* texture = ast.texture(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1662 | if (NULL == texture) { |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 1663 | return false; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1664 | } |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 1665 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 1666 | SkAutoTUnref<const GrEffect> effect; |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 1667 | SkMatrix textureMatrix; |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 1668 | textureMatrix.setIDiv(texture->width(), texture->height()); |
| 1669 | |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1670 | // allocate a tmp buffer and sw convert the pixels to premul |
| 1671 | SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0); |
| 1672 | |
| 1673 | if (kUnpremul_PixelOpsFlag & flags) { |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 1674 | if (!GrPixelConfigIs8888(srcConfig)) { |
| 1675 | return false; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1676 | } |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 1677 | effect.reset(this->createUPMToPMEffect(texture, swapRAndB, textureMatrix)); |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 1678 | // handle the unpremul step on the CPU if we couldn't create an effect to do it. |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 1679 | if (NULL == effect) { |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 1680 | SkSrcPixelInfo srcPI; |
| 1681 | if (!GrPixelConfig2ColorType(srcConfig, &srcPI.fColorType)) { |
| 1682 | return false; |
| 1683 | } |
| 1684 | srcPI.fAlphaType = kUnpremul_SkAlphaType; |
| 1685 | srcPI.fPixels = buffer; |
| 1686 | srcPI.fRowBytes = rowBytes; |
| 1687 | |
commit-bot@chromium.org | 231f6b8 | 2014-03-25 13:38:44 +0000 | [diff] [blame] | 1688 | tmpPixels.reset(width * height); |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 1689 | |
| 1690 | SkDstPixelInfo dstPI; |
| 1691 | dstPI.fColorType = srcPI.fColorType; |
| 1692 | dstPI.fAlphaType = kPremul_SkAlphaType; |
| 1693 | dstPI.fPixels = tmpPixels.get(); |
| 1694 | dstPI.fRowBytes = 4 * width; |
| 1695 | |
| 1696 | if (!srcPI.convertPixelsTo(&dstPI, width, height)) { |
| 1697 | return false; |
| 1698 | } |
| 1699 | |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1700 | buffer = tmpPixels.get(); |
| 1701 | rowBytes = 4 * width; |
| 1702 | } |
| 1703 | } |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 1704 | if (NULL == effect) { |
| 1705 | effect.reset(GrConfigConversionEffect::Create(texture, |
| 1706 | swapRAndB, |
| 1707 | GrConfigConversionEffect::kNone_PMConversion, |
| 1708 | textureMatrix)); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 1711 | if (!this->writeTexturePixels(texture, |
| 1712 | 0, 0, width, height, |
| 1713 | writeConfig, buffer, rowBytes, |
| 1714 | flags & ~kUnpremul_PixelOpsFlag)) { |
| 1715 | return false; |
| 1716 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1717 | |
robertphillips@google.com | 13f181f | 2013-03-02 12:02:08 +0000 | [diff] [blame] | 1718 | // writeRenderTargetPixels can be called in the midst of drawing another |
skia.committer@gmail.com | 0c23faf | 2013-03-03 07:16:29 +0000 | [diff] [blame] | 1719 | // object (e.g., when uploading a SW path rendering to the gpu while |
robertphillips@google.com | 13f181f | 2013-03-02 12:02:08 +0000 | [diff] [blame] | 1720 | // drawing a rect) so preserve the current geometry. |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 1721 | SkMatrix matrix; |
| 1722 | matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top)); |
| 1723 | GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &matrix); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1724 | GrDrawState* drawState = fGpu->drawState(); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 1725 | SkASSERT(effect); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 1726 | drawState->addColorEffect(effect); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1727 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1728 | drawState->setRenderTarget(target); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1729 | |
bsalomon | 01c8da1 | 2014-08-04 09:21:30 -0700 | [diff] [blame] | 1730 | fGpu->drawSimpleRect(SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height))); |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 1731 | return true; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1732 | } |
| 1733 | //////////////////////////////////////////////////////////////////////////////// |
| 1734 | |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 1735 | GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint, |
| 1736 | BufferedDraw buffered, |
commit-bot@chromium.org | 5dbb149 | 2013-10-04 16:23:58 +0000 | [diff] [blame] | 1737 | AutoRestoreEffects* are, |
| 1738 | AutoCheckFlush* acf) { |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 1739 | // All users of this draw state should be freeing up all effects when they're done. |
| 1740 | // Otherwise effects that own resources may keep those resources alive indefinitely. |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 1741 | SkASSERT(0 == fDrawState->numColorStages() && 0 == fDrawState->numCoverageStages() && |
| 1742 | !fDrawState->hasGeometryProcessor()); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 1743 | |
bsalomon | 41ebbdd | 2014-08-04 08:31:39 -0700 | [diff] [blame] | 1744 | if (NULL == fGpu) { |
| 1745 | return NULL; |
| 1746 | } |
| 1747 | |
bsalomon@google.com | 1d4edd3 | 2012-08-16 18:36:06 +0000 | [diff] [blame] | 1748 | if (kNo_BufferedDraw == buffered && kYes_BufferedDraw == fLastDrawWasBuffered) { |
robertphillips@google.com | 1267fbd | 2013-07-03 18:37:27 +0000 | [diff] [blame] | 1749 | fDrawBuffer->flush(); |
bsalomon@google.com | 1d4edd3 | 2012-08-16 18:36:06 +0000 | [diff] [blame] | 1750 | fLastDrawWasBuffered = kNo_BufferedDraw; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1751 | } |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 1752 | ASSERT_OWNED_RESOURCE(fRenderTarget.get()); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1753 | if (paint) { |
| 1754 | SkASSERT(are); |
| 1755 | SkASSERT(acf); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 1756 | are->set(fDrawState); |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 1757 | fDrawState->setFromPaint(*paint, fViewMatrix, fRenderTarget.get()); |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 1758 | #if GR_DEBUG_PARTIAL_COVERAGE_CHECK |
| 1759 | if ((paint->hasMask() || 0xff != paint->fCoverage) && |
bsalomon | 62c447d | 2014-08-08 08:08:50 -0700 | [diff] [blame] | 1760 | !fDrawState->couldApplyCoverage(fGpu->caps())) { |
bsalomon@google.com | af84e74 | 2012-10-05 13:23:24 +0000 | [diff] [blame] | 1761 | GrPrintf("Partial pixel coverage will be incorrectly blended.\n"); |
| 1762 | } |
| 1763 | #endif |
bsalomon | 9c0822a | 2014-08-11 11:07:48 -0700 | [diff] [blame] | 1764 | // Clear any vertex attributes configured for the previous use of the |
| 1765 | // GrDrawState which can effect which blend optimizations are in effect. |
| 1766 | fDrawState->setDefaultVertexAttribs(); |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 1767 | } else { |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 1768 | fDrawState->reset(fViewMatrix); |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 1769 | fDrawState->setRenderTarget(fRenderTarget.get()); |
bsalomon@google.com | 07ea2db | 2012-08-17 14:06:49 +0000 | [diff] [blame] | 1770 | } |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 1771 | GrDrawTarget* target; |
bsalomon@google.com | 1d4edd3 | 2012-08-16 18:36:06 +0000 | [diff] [blame] | 1772 | if (kYes_BufferedDraw == buffered) { |
bsalomon@google.com | 1d4edd3 | 2012-08-16 18:36:06 +0000 | [diff] [blame] | 1773 | fLastDrawWasBuffered = kYes_BufferedDraw; |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 1774 | target = fDrawBuffer; |
bsalomon@google.com | 1d4edd3 | 2012-08-16 18:36:06 +0000 | [diff] [blame] | 1775 | } else { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 1776 | SkASSERT(kNo_BufferedDraw == buffered); |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 1777 | fLastDrawWasBuffered = kNo_BufferedDraw; |
| 1778 | target = fGpu; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1779 | } |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1780 | fDrawState->setState(GrDrawState::kClip_StateBit, fClip && |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 1781 | !fClip->fClipStack->isWideOpen()); |
| 1782 | target->setClip(fClip); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 1783 | SkASSERT(fDrawState == target->drawState()); |
commit-bot@chromium.org | bb6a317 | 2013-05-28 17:25:49 +0000 | [diff] [blame] | 1784 | return target; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1785 | } |
| 1786 | |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 1787 | /* |
| 1788 | * This method finds a path renderer that can draw the specified path on |
| 1789 | * the provided target. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1790 | * 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] | 1791 | * can be individually allowed/disallowed via the "allowSW" boolean. |
| 1792 | */ |
bsalomon@google.com | 8d033a1 | 2012-04-27 15:52:53 +0000 | [diff] [blame] | 1793 | GrPathRenderer* GrContext::getPathRenderer(const SkPath& path, |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 1794 | const SkStrokeRec& stroke, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 1795 | const GrDrawTarget* target, |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 1796 | bool allowSW, |
| 1797 | GrPathRendererChain::DrawType drawType, |
| 1798 | GrPathRendererChain::StencilSupport* stencilSupport) { |
| 1799 | |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 1800 | if (NULL == fPathRendererChain) { |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 1801 | fPathRendererChain = SkNEW_ARGS(GrPathRendererChain, (this)); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 1802 | } |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 1803 | |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 1804 | GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path, |
| 1805 | stroke, |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 1806 | target, |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 1807 | drawType, |
| 1808 | stencilSupport); |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 1809 | |
| 1810 | if (NULL == pr && allowSW) { |
| 1811 | if (NULL == fSoftwarePathRenderer) { |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 1812 | fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this)); |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 1813 | } |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 1814 | pr = fSoftwarePathRenderer; |
| 1815 | } |
| 1816 | |
| 1817 | return pr; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 1818 | } |
| 1819 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1820 | //////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | 6b7938f | 2013-10-15 14:18:16 +0000 | [diff] [blame] | 1821 | bool GrContext::isConfigRenderable(GrPixelConfig config, bool withMSAA) const { |
| 1822 | return fGpu->caps()->isConfigRenderable(config, withMSAA); |
robertphillips@google.com | 99a5ac0 | 2012-04-10 19:26:38 +0000 | [diff] [blame] | 1823 | } |
| 1824 | |
commit-bot@chromium.org | b471a32 | 2014-03-10 07:40:03 +0000 | [diff] [blame] | 1825 | int GrContext::getRecommendedSampleCount(GrPixelConfig config, |
| 1826 | SkScalar dpi) const { |
| 1827 | if (!this->isConfigRenderable(config, true)) { |
| 1828 | return 0; |
| 1829 | } |
| 1830 | int chosenSampleCount = 0; |
| 1831 | if (fGpu->caps()->pathRenderingSupport()) { |
| 1832 | if (dpi >= 250.0f) { |
| 1833 | chosenSampleCount = 4; |
| 1834 | } else { |
| 1835 | chosenSampleCount = 16; |
| 1836 | } |
| 1837 | } |
| 1838 | return chosenSampleCount <= fGpu->caps()->maxSampleCount() ? |
| 1839 | chosenSampleCount : 0; |
| 1840 | } |
| 1841 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 1842 | void GrContext::setupDrawBuffer() { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 1843 | SkASSERT(NULL == fDrawBuffer); |
| 1844 | SkASSERT(NULL == fDrawBufferVBAllocPool); |
| 1845 | SkASSERT(NULL == fDrawBufferIBAllocPool); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 1846 | |
bsalomon@google.com | de6ac2d | 2011-02-25 21:50:42 +0000 | [diff] [blame] | 1847 | fDrawBufferVBAllocPool = |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 1848 | SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false, |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1849 | DRAW_BUFFER_VBPOOL_BUFFER_SIZE, |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 1850 | DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS)); |
bsalomon@google.com | de6ac2d | 2011-02-25 21:50:42 +0000 | [diff] [blame] | 1851 | fDrawBufferIBAllocPool = |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 1852 | SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false, |
bsalomon@google.com | de6ac2d | 2011-02-25 21:50:42 +0000 | [diff] [blame] | 1853 | DRAW_BUFFER_IBPOOL_BUFFER_SIZE, |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 1854 | DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS)); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1855 | |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 1856 | fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu, |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 1857 | fDrawBufferVBAllocPool, |
| 1858 | fDrawBufferIBAllocPool)); |
bsalomon@google.com | 3c4d032 | 2012-04-03 18:04:51 +0000 | [diff] [blame] | 1859 | |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 1860 | fDrawBuffer->setDrawState(fDrawState); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1861 | } |
| 1862 | |
bsalomon@google.com | 21c10c5 | 2013-06-13 17:44:07 +0000 | [diff] [blame] | 1863 | GrDrawTarget* GrContext::getTextTarget() { |
commit-bot@chromium.org | 5dbb149 | 2013-10-04 16:23:58 +0000 | [diff] [blame] | 1864 | return this->prepareToDraw(NULL, BUFFERED_DRAW, NULL, NULL); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
| 1867 | const GrIndexBuffer* GrContext::getQuadIndexBuffer() const { |
| 1868 | return fGpu->getQuadIndexBuffer(); |
| 1869 | } |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 1870 | |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1871 | namespace { |
| 1872 | void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) { |
| 1873 | GrConfigConversionEffect::PMConversion pmToUPM; |
| 1874 | GrConfigConversionEffect::PMConversion upmToPM; |
| 1875 | GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM); |
| 1876 | *pmToUPMValue = pmToUPM; |
| 1877 | *upmToPMValue = upmToPM; |
| 1878 | } |
| 1879 | } |
| 1880 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 1881 | const GrEffect* GrContext::createPMToUPMEffect(GrTexture* texture, |
| 1882 | bool swapRAndB, |
| 1883 | const SkMatrix& matrix) { |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1884 | if (!fDidTestPMConversions) { |
| 1885 | test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion); |
bsalomon@google.com | d0f3f68 | 2012-08-28 13:08:14 +0000 | [diff] [blame] | 1886 | fDidTestPMConversions = true; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1887 | } |
| 1888 | GrConfigConversionEffect::PMConversion pmToUPM = |
| 1889 | static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion); |
| 1890 | if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) { |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 1891 | return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, matrix); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1892 | } else { |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 1893 | return NULL; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1894 | } |
| 1895 | } |
| 1896 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 1897 | const GrEffect* GrContext::createUPMToPMEffect(GrTexture* texture, |
| 1898 | bool swapRAndB, |
| 1899 | const SkMatrix& matrix) { |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1900 | if (!fDidTestPMConversions) { |
| 1901 | test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion); |
bsalomon@google.com | d0f3f68 | 2012-08-28 13:08:14 +0000 | [diff] [blame] | 1902 | fDidTestPMConversions = true; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1903 | } |
| 1904 | GrConfigConversionEffect::PMConversion upmToPM = |
| 1905 | static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion); |
| 1906 | if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) { |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 1907 | return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, matrix); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1908 | } else { |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 1909 | return NULL; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1910 | } |
| 1911 | } |
| 1912 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 1913 | void GrContext::addResourceToCache(const GrResourceKey& resourceKey, GrGpuResource* resource) { |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 1914 | fResourceCache->purgeAsNeeded(1, resource->gpuMemorySize()); |
| 1915 | fResourceCache->addResource(resourceKey, resource); |
commit-bot@chromium.org | 95a2b0e | 2014-05-05 19:21:16 +0000 | [diff] [blame] | 1916 | } |
| 1917 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 1918 | GrGpuResource* GrContext::findAndRefCachedResource(const GrResourceKey& resourceKey) { |
| 1919 | GrGpuResource* resource = fResourceCache->find(resourceKey); |
commit-bot@chromium.org | 95a2b0e | 2014-05-05 19:21:16 +0000 | [diff] [blame] | 1920 | SkSafeRef(resource); |
| 1921 | return resource; |
| 1922 | } |
| 1923 | |
egdaniel | bbcb38d | 2014-06-19 10:19:29 -0700 | [diff] [blame] | 1924 | void GrContext::addGpuTraceMarker(const GrGpuTraceMarker* marker) { |
| 1925 | fGpu->addGpuTraceMarker(marker); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1926 | if (fDrawBuffer) { |
egdaniel | bbcb38d | 2014-06-19 10:19:29 -0700 | [diff] [blame] | 1927 | fDrawBuffer->addGpuTraceMarker(marker); |
| 1928 | } |
| 1929 | } |
| 1930 | |
| 1931 | void GrContext::removeGpuTraceMarker(const GrGpuTraceMarker* marker) { |
| 1932 | fGpu->removeGpuTraceMarker(marker); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1933 | if (fDrawBuffer) { |
egdaniel | bbcb38d | 2014-06-19 10:19:29 -0700 | [diff] [blame] | 1934 | fDrawBuffer->removeGpuTraceMarker(marker); |
| 1935 | } |
| 1936 | } |
| 1937 | |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1938 | /////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | 5955202 | 2012-08-31 13:07:37 +0000 | [diff] [blame] | 1939 | #if GR_CACHE_STATS |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame] | 1940 | void GrContext::printCacheStats() const { |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 1941 | fResourceCache->printStats(); |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame] | 1942 | } |
| 1943 | #endif |
robertphillips | 754f4e9 | 2014-09-18 13:52:08 -0700 | [diff] [blame^] | 1944 | |
| 1945 | #if GR_GPU_STATS |
| 1946 | const GrContext::GPUStats* GrContext::gpuStats() const { |
| 1947 | return fGpu->gpuStats(); |
| 1948 | } |
| 1949 | #endif |
| 1950 | |