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