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