epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
bsalomon@google.com | 1fadb20 | 2011-12-12 16:10:08 +0000 | [diff] [blame] | 9 | #include "GrContext.h" |
| 10 | |
jvanverth@google.com | bfe2b9d | 2013-09-06 16:57:29 +0000 | [diff] [blame] | 11 | #include "GrAARectRenderer.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 12 | #include "GrAtlasTextContext.h" |
joshualitt | 0823bfa | 2015-02-27 10:06:15 -0800 | [diff] [blame] | 13 | #include "GrBatch.h" |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 14 | #include "GrBatchFontCache.h" |
joshualitt | 0823bfa | 2015-02-27 10:06:15 -0800 | [diff] [blame] | 15 | #include "GrBatchTarget.h" |
tomhudson@google.com | 278cbb4 | 2011-06-30 19:37:01 +0000 | [diff] [blame] | 16 | #include "GrBufferAllocPool.h" |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 17 | #include "GrDefaultGeoProcFactory.h" |
jvanverth | 787cdf9 | 2014-12-04 10:46:50 -0800 | [diff] [blame] | 18 | #include "GrFontCache.h" |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 19 | #include "GrGpuResource.h" |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 20 | #include "GrGpuResourcePriv.h" |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 21 | #include "GrDistanceFieldTextContext.h" |
commit-bot@chromium.org | 4784182 | 2014-03-27 14:19:17 +0000 | [diff] [blame] | 22 | #include "GrDrawTargetCaps.h" |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 23 | #include "GrGpu.h" |
commit-bot@chromium.org | dcb8ef9 | 2014-03-27 11:26:10 +0000 | [diff] [blame] | 24 | #include "GrIndexBuffer.h" |
commit-bot@chromium.org | 4784182 | 2014-03-27 14:19:17 +0000 | [diff] [blame] | 25 | #include "GrInOrderDrawBuffer.h" |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 26 | #include "GrLayerCache.h" |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 27 | #include "GrOvalRenderer.h" |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 28 | #include "GrPathRenderer.h" |
tomhudson@google.com | d22b6e4 | 2011-06-24 15:53:40 +0000 | [diff] [blame] | 29 | #include "GrPathUtils.h" |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 30 | #include "GrRenderTargetPriv.h" |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 31 | #include "GrResourceCache.h" |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 32 | #include "GrSoftwarePathRenderer.h" |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 33 | #include "GrStencilAndCoverTextContext.h" |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 34 | #include "GrStrokeInfo.h" |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 35 | #include "GrSurfacePriv.h" |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 36 | #include "GrTextBlobCache.h" |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 37 | #include "GrTexturePriv.h" |
egdaniel | bbcb38d | 2014-06-19 10:19:29 -0700 | [diff] [blame] | 38 | #include "GrTraceMarker.h" |
commit-bot@chromium.org | 2a05de0 | 2014-03-25 15:17:32 +0000 | [diff] [blame] | 39 | #include "GrTracing.h" |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 40 | #include "SkDashPathPriv.h" |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 41 | #include "SkConfig8888.h" |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 42 | #include "SkGr.h" |
commit-bot@chromium.org | 4784182 | 2014-03-27 14:19:17 +0000 | [diff] [blame] | 43 | #include "SkRRect.h" |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 44 | #include "SkStrokeRec.h" |
commit-bot@chromium.org | dcb8ef9 | 2014-03-27 11:26:10 +0000 | [diff] [blame] | 45 | #include "SkTLazy.h" |
commit-bot@chromium.org | 4784182 | 2014-03-27 14:19:17 +0000 | [diff] [blame] | 46 | #include "SkTLS.h" |
commit-bot@chromium.org | 933e65d | 2014-03-20 20:00:24 +0000 | [diff] [blame] | 47 | #include "SkTraceEvent.h" |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 48 | |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 49 | #include "effects/GrConfigConversionEffect.h" |
| 50 | #include "effects/GrDashingEffect.h" |
| 51 | #include "effects/GrSingleTextureEffect.h" |
| 52 | |
bsalomon@google.com | 6036149 | 2012-03-15 17:47:06 +0000 | [diff] [blame] | 53 | static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 54 | static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4; |
| 55 | |
bsalomon@google.com | 1d4edd3 | 2012-08-16 18:36:06 +0000 | [diff] [blame] | 56 | static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = 1 << 11; |
| 57 | static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = 4; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 58 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 59 | #define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this) |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 60 | #define RETURN_IF_ABANDONED if (!fDrawBuffer) { return; } |
| 61 | #define RETURN_FALSE_IF_ABANDONED if (!fDrawBuffer) { return false; } |
| 62 | #define RETURN_NULL_IF_ABANDONED if (!fDrawBuffer) { return NULL; } |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 63 | |
bsalomon | f21dab9 | 2014-11-13 13:33:28 -0800 | [diff] [blame] | 64 | class GrContext::AutoCheckFlush { |
| 65 | public: |
| 66 | AutoCheckFlush(GrContext* context) : fContext(context) { SkASSERT(context); } |
| 67 | |
| 68 | ~AutoCheckFlush() { |
| 69 | if (fContext->fFlushToReduceCacheSize) { |
| 70 | fContext->flush(); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | private: |
| 75 | GrContext* fContext; |
| 76 | }; |
| 77 | |
krajcevski | 9c6d4d7 | 2014-08-12 07:26:25 -0700 | [diff] [blame] | 78 | GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext, |
| 79 | const Options* opts) { |
| 80 | GrContext* context; |
| 81 | if (NULL == opts) { |
| 82 | context = SkNEW_ARGS(GrContext, (Options())); |
| 83 | } else { |
| 84 | context = SkNEW_ARGS(GrContext, (*opts)); |
| 85 | } |
| 86 | |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 87 | if (context->init(backend, backendContext)) { |
| 88 | return context; |
| 89 | } else { |
| 90 | context->unref(); |
| 91 | return NULL; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 92 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 93 | } |
| 94 | |
krajcevski | 9c6d4d7 | 2014-08-12 07:26:25 -0700 | [diff] [blame] | 95 | GrContext::GrContext(const Options& opts) : fOptions(opts) { |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 96 | fGpu = NULL; |
| 97 | fPathRendererChain = NULL; |
| 98 | fSoftwarePathRenderer = NULL; |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 99 | fResourceCache = NULL; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 100 | fBatchFontCache = NULL; |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 101 | fFontCache = NULL; |
| 102 | fDrawBuffer = NULL; |
| 103 | fDrawBufferVBAllocPool = NULL; |
| 104 | fDrawBufferIBAllocPool = NULL; |
bsalomon | f21dab9 | 2014-11-13 13:33:28 -0800 | [diff] [blame] | 105 | fFlushToReduceCacheSize = false; |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 106 | fAARectRenderer = NULL; |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 107 | fOvalRenderer = NULL; |
robertphillips@google.com | 44a91dc | 2013-07-25 15:32:06 +0000 | [diff] [blame] | 108 | fMaxTextureSizeOverride = 1 << 20; |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | bool GrContext::init(GrBackend backend, GrBackendContext backendContext) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 112 | SkASSERT(NULL == fGpu); |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 113 | |
| 114 | fGpu = GrGpu::Create(backend, backendContext, this); |
| 115 | if (NULL == fGpu) { |
| 116 | return false; |
| 117 | } |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 118 | this->initCommon(); |
| 119 | return true; |
| 120 | } |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 121 | |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 122 | void GrContext::initCommon() { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 123 | fResourceCache = SkNEW(GrResourceCache); |
| 124 | fResourceCache->setOverBudgetCallback(OverBudgetCB, this); |
commit-bot@chromium.org | 1836d33 | 2013-07-16 22:55:03 +0000 | [diff] [blame] | 125 | |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 126 | fFontCache = SkNEW_ARGS(GrFontCache, (fGpu)); |
| 127 | |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 128 | fLayerCache.reset(SkNEW_ARGS(GrLayerCache, (this))); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 129 | |
joshualitt | b44293e | 2014-10-28 08:12:18 -0700 | [diff] [blame] | 130 | fAARectRenderer = SkNEW_ARGS(GrAARectRenderer, (fGpu)); |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 131 | fOvalRenderer = SkNEW_ARGS(GrOvalRenderer, (fGpu)); |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 132 | |
| 133 | fDidTestPMConversions = false; |
| 134 | |
| 135 | this->setupDrawBuffer(); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 136 | |
| 137 | // GrBatchFontCache will eventually replace GrFontCache |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 138 | fBatchFontCache = SkNEW_ARGS(GrBatchFontCache, (this)); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 139 | |
joshualitt | 0db6dfa | 2015-04-10 07:01:30 -0700 | [diff] [blame] | 140 | fTextBlobCache.reset(SkNEW_ARGS(GrTextBlobCache, (TextBlobCacheOverBudgetCB, this))); |
bsalomon@google.com | c0af317 | 2012-06-15 14:10:09 +0000 | [diff] [blame] | 141 | } |
| 142 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 143 | GrContext::~GrContext() { |
bsalomon@google.com | 733c062 | 2013-04-24 17:59:32 +0000 | [diff] [blame] | 144 | if (NULL == fGpu) { |
| 145 | return; |
| 146 | } |
| 147 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 148 | this->flush(); |
robertphillips@google.com | 5acc0e3 | 2012-05-17 12:01:02 +0000 | [diff] [blame] | 149 | |
robertphillips@google.com | 950b1b0 | 2013-10-21 17:37:28 +0000 | [diff] [blame] | 150 | for (int i = 0; i < fCleanUpData.count(); ++i) { |
| 151 | (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo); |
| 152 | } |
| 153 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 154 | SkDELETE(fResourceCache); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 155 | SkDELETE(fBatchFontCache); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 156 | SkDELETE(fFontCache); |
| 157 | SkDELETE(fDrawBuffer); |
| 158 | SkDELETE(fDrawBufferVBAllocPool); |
| 159 | SkDELETE(fDrawBufferIBAllocPool); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 160 | |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 161 | fAARectRenderer->unref(); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 162 | fOvalRenderer->unref(); |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 163 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 164 | fGpu->unref(); |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 165 | SkSafeUnref(fPathRendererChain); |
| 166 | SkSafeUnref(fSoftwarePathRenderer); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 167 | } |
| 168 | |
bsalomon | 2354f84 | 2014-07-28 13:48:36 -0700 | [diff] [blame] | 169 | void GrContext::abandonContext() { |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 170 | // abandon first to so destructors |
| 171 | // don't try to free the resources in the API. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 172 | fResourceCache->abandonAll(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 173 | |
robertphillips | e337130 | 2014-09-17 06:01:06 -0700 | [diff] [blame] | 174 | fGpu->contextAbandoned(); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 175 | |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 176 | // a path renderer may be holding onto resources that |
| 177 | // are now unusable |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 178 | SkSafeSetNull(fPathRendererChain); |
| 179 | SkSafeSetNull(fSoftwarePathRenderer); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 180 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 181 | delete fDrawBuffer; |
| 182 | fDrawBuffer = NULL; |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 183 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 184 | delete fDrawBufferVBAllocPool; |
| 185 | fDrawBufferVBAllocPool = NULL; |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 186 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 187 | delete fDrawBufferIBAllocPool; |
| 188 | fDrawBufferIBAllocPool = NULL; |
| 189 | |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 190 | fAARectRenderer->reset(); |
commit-bot@chromium.org | ef284a8 | 2013-07-11 22:29:29 +0000 | [diff] [blame] | 191 | fOvalRenderer->reset(); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 192 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 193 | fBatchFontCache->freeAll(); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 194 | fFontCache->freeAll(); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 195 | fLayerCache->freeAll(); |
joshualitt | 26ffc00 | 2015-04-16 11:24:04 -0700 | [diff] [blame^] | 196 | fTextBlobCache->freeAll(); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 197 | } |
| 198 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 199 | void GrContext::resetContext(uint32_t state) { |
| 200 | fGpu->markContextDirty(state); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | void GrContext::freeGpuResources() { |
| 204 | this->flush(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 205 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 206 | if (fDrawBuffer) { |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 207 | fDrawBuffer->purgeResources(); |
| 208 | } |
robertphillips@google.com | ff17584 | 2012-05-14 19:31:39 +0000 | [diff] [blame] | 209 | |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 210 | fAARectRenderer->reset(); |
commit-bot@chromium.org | ef284a8 | 2013-07-11 22:29:29 +0000 | [diff] [blame] | 211 | fOvalRenderer->reset(); |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 212 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 213 | fBatchFontCache->freeAll(); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 214 | fFontCache->freeAll(); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 215 | fLayerCache->freeAll(); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 216 | // a path renderer may be holding onto resources |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 217 | SkSafeSetNull(fPathRendererChain); |
| 218 | SkSafeSetNull(fSoftwarePathRenderer); |
bsalomon | 3033b9f | 2015-04-13 11:09:56 -0700 | [diff] [blame] | 219 | |
| 220 | fResourceCache->purgeAllUnlocked(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 221 | } |
| 222 | |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 223 | void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 224 | if (resourceCount) { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 225 | *resourceCount = fResourceCache->getBudgetedResourceCount(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 226 | } |
| 227 | if (resourceBytes) { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 228 | *resourceBytes = fResourceCache->getBudgetedResourceBytes(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 229 | } |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 230 | } |
| 231 | |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 232 | GrTextContext* GrContext::createTextContext(GrRenderTarget* renderTarget, |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 233 | SkGpuDevice* gpuDevice, |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 234 | const SkDeviceProperties& |
| 235 | leakyProperties, |
| 236 | bool enableDistanceFieldFonts) { |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 237 | if (fGpu->caps()->pathRenderingSupport() && renderTarget->isMultisampled()) { |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 238 | GrStencilAttachment* sb = renderTarget->renderTargetPriv().attachStencilAttachment(); |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 239 | if (sb) { |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 240 | return GrStencilAndCoverTextContext::Create(this, gpuDevice, leakyProperties); |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 241 | } |
jvanverth | 8c27a18 | 2014-10-14 08:45:50 -0700 | [diff] [blame] | 242 | } |
| 243 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 244 | #ifdef USE_BITMAP_TEXTBLOBS |
joshualitt | dbd3593 | 2015-04-02 09:19:04 -0700 | [diff] [blame] | 245 | return GrAtlasTextContext::Create(this, gpuDevice, leakyProperties); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 246 | #else |
joshualitt | 6e8cd96 | 2015-03-20 10:30:14 -0700 | [diff] [blame] | 247 | return GrDistanceFieldTextContext::Create(this, gpuDevice, leakyProperties, |
| 248 | enableDistanceFieldFonts); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 249 | #endif |
kkinnunen | c6cb56f | 2014-06-24 00:12:27 -0700 | [diff] [blame] | 250 | } |
| 251 | |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 252 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon | d042358 | 2015-02-06 08:49:24 -0800 | [diff] [blame] | 253 | enum ScratchTextureFlags { |
| 254 | kExact_ScratchTextureFlag = 0x1, |
| 255 | kNoPendingIO_ScratchTextureFlag = 0x2, |
| 256 | kNoCreate_ScratchTextureFlag = 0x4, |
| 257 | }; |
bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 258 | |
bsalomon | d2a6f4e | 2015-02-04 10:55:54 -0800 | [diff] [blame] | 259 | bool GrContext::isConfigTexturable(GrPixelConfig config) const { |
| 260 | return fGpu->caps()->isConfigTexturable(config); |
| 261 | } |
| 262 | |
| 263 | bool GrContext::npotTextureTileSupport() const { |
| 264 | return fGpu->caps()->npotTextureTileSupport(); |
| 265 | } |
| 266 | |
bsalomon | d042358 | 2015-02-06 08:49:24 -0800 | [diff] [blame] | 267 | GrTexture* GrContext::createTexture(const GrSurfaceDesc& desc, bool budgeted, const void* srcData, |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 268 | size_t rowBytes) { |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 269 | RETURN_NULL_IF_ABANDONED |
bsalomon | d042358 | 2015-02-06 08:49:24 -0800 | [diff] [blame] | 270 | if ((desc.fFlags & kRenderTarget_GrSurfaceFlag) && |
| 271 | !this->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) { |
bsalomon | d2a6f4e | 2015-02-04 10:55:54 -0800 | [diff] [blame] | 272 | return NULL; |
| 273 | } |
bsalomon | d042358 | 2015-02-06 08:49:24 -0800 | [diff] [blame] | 274 | if (!GrPixelConfigIsCompressed(desc.fConfig)) { |
| 275 | static const uint32_t kFlags = kExact_ScratchTextureFlag | |
| 276 | kNoCreate_ScratchTextureFlag; |
| 277 | if (GrTexture* texture = this->internalRefScratchTexture(desc, kFlags)) { |
| 278 | if (!srcData || texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, |
| 279 | srcData, rowBytes)) { |
| 280 | if (!budgeted) { |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 281 | texture->resourcePriv().makeUnbudgeted(); |
bsalomon | d042358 | 2015-02-06 08:49:24 -0800 | [diff] [blame] | 282 | } |
| 283 | return texture; |
| 284 | } |
| 285 | texture->unref(); |
| 286 | } |
| 287 | } |
| 288 | return fGpu->createTexture(desc, budgeted, srcData, rowBytes); |
| 289 | } |
bsalomon | d2a6f4e | 2015-02-04 10:55:54 -0800 | [diff] [blame] | 290 | |
bsalomon | d042358 | 2015-02-06 08:49:24 -0800 | [diff] [blame] | 291 | GrTexture* GrContext::refScratchTexture(const GrSurfaceDesc& desc, ScratchTexMatch match, |
| 292 | bool calledDuringFlush) { |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 293 | RETURN_NULL_IF_ABANDONED |
bsalomon | d042358 | 2015-02-06 08:49:24 -0800 | [diff] [blame] | 294 | // Currently we don't recycle compressed textures as scratch. |
| 295 | if (GrPixelConfigIsCompressed(desc.fConfig)) { |
| 296 | return NULL; |
| 297 | } else { |
| 298 | uint32_t flags = 0; |
| 299 | if (kExact_ScratchTexMatch == match) { |
| 300 | flags |= kExact_ScratchTextureFlag; |
| 301 | } |
| 302 | if (calledDuringFlush) { |
| 303 | flags |= kNoPendingIO_ScratchTextureFlag; |
| 304 | } |
| 305 | return this->internalRefScratchTexture(desc, flags); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | GrTexture* GrContext::internalRefScratchTexture(const GrSurfaceDesc& inDesc, uint32_t flags) { |
| 310 | SkASSERT(!GrPixelConfigIsCompressed(inDesc.fConfig)); |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 311 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 312 | SkTCopyOnFirstWrite<GrSurfaceDesc> desc(inDesc); |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 313 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 314 | if (fGpu->caps()->reuseScratchTextures() || (desc->fFlags & kRenderTarget_GrSurfaceFlag)) { |
bsalomon | d042358 | 2015-02-06 08:49:24 -0800 | [diff] [blame] | 315 | if (!(kExact_ScratchTextureFlag & flags)) { |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 316 | // bin by pow2 with a reasonable min |
| 317 | static const int MIN_SIZE = 16; |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 318 | GrSurfaceDesc* wdesc = desc.writable(); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 319 | wdesc->fWidth = SkTMax(MIN_SIZE, GrNextPow2(desc->fWidth)); |
| 320 | wdesc->fHeight = SkTMax(MIN_SIZE, GrNextPow2(desc->fHeight)); |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 321 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 322 | |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 323 | GrScratchKey key; |
| 324 | GrTexturePriv::ComputeScratchKey(*desc, &key); |
| 325 | uint32_t scratchFlags = 0; |
| 326 | if (kNoPendingIO_ScratchTextureFlag & flags) { |
| 327 | scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag; |
| 328 | } else if (!(desc->fFlags & kRenderTarget_GrSurfaceFlag)) { |
| 329 | // If it is not a render target then it will most likely be populated by |
| 330 | // writePixels() which will trigger a flush if the texture has pending IO. |
| 331 | scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag; |
| 332 | } |
| 333 | GrGpuResource* resource = fResourceCache->findAndRefScratchResource(key, scratchFlags); |
| 334 | if (resource) { |
| 335 | GrSurface* surface = static_cast<GrSurface*>(resource); |
| 336 | GrRenderTarget* rt = surface->asRenderTarget(); |
| 337 | if (rt && fGpu->caps()->discardRenderTargetSupport()) { |
| 338 | rt->discard(); |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 339 | } |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 340 | return surface->asTexture(); |
| 341 | } |
bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 342 | } |
| 343 | |
bsalomon | d042358 | 2015-02-06 08:49:24 -0800 | [diff] [blame] | 344 | if (!(kNoCreate_ScratchTextureFlag & flags)) { |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 345 | return fGpu->createTexture(*desc, true, NULL, 0); |
bsalomon | e167f96 | 2015-01-27 09:56:04 -0800 | [diff] [blame] | 346 | } |
bsalomon | d042358 | 2015-02-06 08:49:24 -0800 | [diff] [blame] | 347 | |
| 348 | return NULL; |
Brian Salomon | 9323b8b | 2014-10-07 15:07:38 -0400 | [diff] [blame] | 349 | } |
| 350 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 351 | void GrContext::OverBudgetCB(void* data) { |
bsalomon | 66a450f | 2014-11-13 13:19:10 -0800 | [diff] [blame] | 352 | SkASSERT(data); |
bsalomon | f21dab9 | 2014-11-13 13:33:28 -0800 | [diff] [blame] | 353 | |
bsalomon | 66a450f | 2014-11-13 13:19:10 -0800 | [diff] [blame] | 354 | GrContext* context = reinterpret_cast<GrContext*>(data); |
bsalomon | f21dab9 | 2014-11-13 13:33:28 -0800 | [diff] [blame] | 355 | |
| 356 | // Flush the InOrderDrawBuffer to possibly free up some textures |
| 357 | context->fFlushToReduceCacheSize = true; |
commit-bot@chromium.org | cae27fe | 2013-07-10 10:14:35 +0000 | [diff] [blame] | 358 | } |
| 359 | |
joshualitt | 0db6dfa | 2015-04-10 07:01:30 -0700 | [diff] [blame] | 360 | void GrContext::TextBlobCacheOverBudgetCB(void* data) { |
| 361 | SkASSERT(data); |
| 362 | |
| 363 | // Unlike the GrResourceCache, TextBlobs are drawn at the SkGpuDevice level, therefore they |
| 364 | // cannot use fFlushTorReduceCacheSize because it uses AutoCheckFlush. The solution is to move |
| 365 | // drawText calls to below the GrContext level, but this is not trivial because they call |
| 366 | // drawPath on SkGpuDevice |
| 367 | GrContext* context = reinterpret_cast<GrContext*>(data); |
| 368 | context->flush(); |
| 369 | } |
| 370 | |
bsalomon@google.com | 9195836 | 2011-06-13 17:58:13 +0000 | [diff] [blame] | 371 | int GrContext::getMaxTextureSize() const { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 372 | return SkTMin(fGpu->caps()->maxTextureSize(), fMaxTextureSizeOverride); |
bsalomon@google.com | 9195836 | 2011-06-13 17:58:13 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | int GrContext::getMaxRenderTargetSize() const { |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 376 | return fGpu->caps()->maxRenderTargetSize(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 377 | } |
| 378 | |
bsalomon@google.com | 8a70eef | 2013-03-19 13:58:55 +0000 | [diff] [blame] | 379 | int GrContext::getMaxSampleCount() const { |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 380 | return fGpu->caps()->maxSampleCount(); |
bsalomon@google.com | 8a70eef | 2013-03-19 13:58:55 +0000 | [diff] [blame] | 381 | } |
| 382 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 383 | /////////////////////////////////////////////////////////////////////////////// |
| 384 | |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 385 | GrTexture* GrContext::wrapBackendTexture(const GrBackendTextureDesc& desc) { |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 386 | RETURN_NULL_IF_ABANDONED |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 387 | return fGpu->wrapBackendTexture(desc); |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 388 | } |
| 389 | |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 390 | GrRenderTarget* GrContext::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) { |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 391 | RETURN_NULL_IF_ABANDONED |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 392 | return fGpu->wrapBackendRenderTarget(desc); |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 393 | } |
| 394 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 395 | //////////////////////////////////////////////////////////////////////////////// |
| 396 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 397 | void GrContext::clear(const SkIRect* rect, |
bsalomon@google.com | 07ea2db | 2012-08-17 14:06:49 +0000 | [diff] [blame] | 398 | const GrColor color, |
robertphillips@google.com | 56ce48a | 2013-10-31 21:44:25 +0000 | [diff] [blame] | 399 | bool canIgnoreRect, |
bsalomon | 41ebbdd | 2014-08-04 08:31:39 -0700 | [diff] [blame] | 400 | GrRenderTarget* renderTarget) { |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 401 | RETURN_IF_ABANDONED |
bsalomon | 41ebbdd | 2014-08-04 08:31:39 -0700 | [diff] [blame] | 402 | ASSERT_OWNED_RESOURCE(renderTarget); |
bsalomon | 89c6298 | 2014-11-03 12:08:42 -0800 | [diff] [blame] | 403 | SkASSERT(renderTarget); |
| 404 | |
bsalomon | f21dab9 | 2014-11-13 13:33:28 -0800 | [diff] [blame] | 405 | AutoCheckFlush acf(this); |
egdaniel | d78a168 | 2014-07-09 10:41:26 -0700 | [diff] [blame] | 406 | GR_CREATE_TRACE_MARKER_CONTEXT("GrContext::clear", this); |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 407 | GrDrawTarget* target = this->prepareToDraw(); |
bsalomon | 41ebbdd | 2014-08-04 08:31:39 -0700 | [diff] [blame] | 408 | if (NULL == target) { |
| 409 | return; |
| 410 | } |
| 411 | target->clear(rect, color, canIgnoreRect, renderTarget); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 412 | } |
| 413 | |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 414 | void GrContext::drawPaint(GrRenderTarget* rt, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 415 | const GrClip& clip, |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 416 | const GrPaint& origPaint, |
| 417 | const SkMatrix& viewMatrix) { |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 418 | RETURN_IF_ABANDONED |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 419 | // set rect to be big enough to fill the space, but not super-huge, so we |
| 420 | // don't overflow fixed-point implementations |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 421 | SkRect r; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 422 | r.setLTRB(0, 0, |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 423 | SkIntToScalar(rt->width()), |
| 424 | SkIntToScalar(rt->height())); |
bsalomon@google.com | 5dc26b9 | 2012-10-11 19:32:32 +0000 | [diff] [blame] | 425 | SkTCopyOnFirstWrite<GrPaint> paint(origPaint); |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 426 | |
| 427 | // by definition this fills the entire clip, no need for AA |
| 428 | if (paint->isAntiAlias()) { |
| 429 | paint.writable()->setAntiAlias(false); |
| 430 | } |
| 431 | |
| 432 | bool isPerspective = viewMatrix.hasPerspective(); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 433 | |
bsalomon@google.com | 4f83be8 | 2011-09-12 13:52:51 +0000 | [diff] [blame] | 434 | // We attempt to map r by the inverse matrix and draw that. mapRect will |
| 435 | // map the four corners and bound them with a new rect. This will not |
| 436 | // produce a correct result for some perspective matrices. |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 437 | if (!isPerspective) { |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 438 | SkMatrix inverse; |
| 439 | if (!viewMatrix.invert(&inverse)) { |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 440 | SkDebugf("Could not invert matrix\n"); |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 441 | return; |
| 442 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 443 | inverse.mapRect(&r); |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 444 | this->drawRect(rt, clip, *paint, viewMatrix, r); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 445 | } else { |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 446 | SkMatrix localMatrix; |
| 447 | if (!viewMatrix.invert(&localMatrix)) { |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 448 | SkDebugf("Could not invert matrix\n"); |
bsalomon@google.com | 3cbaa2d | 2012-10-12 14:51:52 +0000 | [diff] [blame] | 449 | return; |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 450 | } |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 451 | |
| 452 | AutoCheckFlush acf(this); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 453 | GrPipelineBuilder pipelineBuilder; |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 454 | GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, paint, &acf); |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 455 | if (NULL == target) { |
| 456 | return; |
| 457 | } |
| 458 | |
| 459 | GR_CREATE_TRACE_MARKER("GrContext::drawPaintWithPerspective", target); |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 460 | target->drawRect(&pipelineBuilder, |
| 461 | paint->getColor(), |
| 462 | SkMatrix::I(), |
| 463 | r, |
| 464 | NULL, |
| 465 | &localMatrix); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 466 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 467 | } |
| 468 | |
commit-bot@chromium.org | 03e3e89 | 2013-10-02 18:19:17 +0000 | [diff] [blame] | 469 | #ifdef SK_DEVELOPER |
| 470 | void GrContext::dumpFontCache() const { |
| 471 | fFontCache->dump(); |
| 472 | } |
| 473 | #endif |
| 474 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 475 | //////////////////////////////////////////////////////////////////////////////// |
| 476 | |
bsalomon | c30aaa0 | 2014-08-13 07:15:29 -0700 | [diff] [blame] | 477 | static inline bool is_irect(const SkRect& r) { |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 478 | return SkScalarIsInt(r.fLeft) && SkScalarIsInt(r.fTop) && |
| 479 | SkScalarIsInt(r.fRight) && SkScalarIsInt(r.fBottom); |
bsalomon | c30aaa0 | 2014-08-13 07:15:29 -0700 | [diff] [blame] | 480 | } |
| 481 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 482 | static bool apply_aa_to_rect(GrDrawTarget* target, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 483 | GrPipelineBuilder* pipelineBuilder, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 484 | SkRect* devBoundRect, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 485 | const SkRect& rect, |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 486 | SkScalar strokeWidth, |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 487 | const SkMatrix& combinedMatrix, |
| 488 | GrColor color) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 489 | if (pipelineBuilder->getRenderTarget()->isMultisampled()) { |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 490 | return false; |
| 491 | } |
| 492 | |
robertphillips@google.com | 4b140b5 | 2013-05-02 17:13:13 +0000 | [diff] [blame] | 493 | #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] | 494 | if (strokeWidth >= 0) { |
| 495 | #endif |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 496 | if (!combinedMatrix.preservesAxisAlignment()) { |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 497 | return false; |
| 498 | } |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 499 | |
robertphillips@google.com | 4b140b5 | 2013-05-02 17:13:13 +0000 | [diff] [blame] | 500 | #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] | 501 | } else { |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 502 | if (!combinedMatrix.preservesRightAngles()) { |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 503 | return false; |
| 504 | } |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 505 | } |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 506 | #endif |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 507 | |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 508 | combinedMatrix.mapRect(devBoundRect, rect); |
derekf | e638d1c | 2014-12-02 13:51:29 -0800 | [diff] [blame] | 509 | if (!combinedMatrix.rectStaysRect()) { |
| 510 | return true; |
| 511 | } |
| 512 | |
bsalomon | c30aaa0 | 2014-08-13 07:15:29 -0700 | [diff] [blame] | 513 | if (strokeWidth < 0) { |
| 514 | return !is_irect(*devBoundRect); |
| 515 | } |
robertphillips@google.com | 28ac96e | 2013-05-13 13:38:35 +0000 | [diff] [blame] | 516 | |
bsalomon | 9c0822a | 2014-08-11 11:07:48 -0700 | [diff] [blame] | 517 | return true; |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 518 | } |
| 519 | |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 520 | static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) { |
| 521 | return point.fX >= rect.fLeft && point.fX <= rect.fRight && |
| 522 | point.fY >= rect.fTop && point.fY <= rect.fBottom; |
| 523 | } |
| 524 | |
joshualitt | 0823bfa | 2015-02-27 10:06:15 -0800 | [diff] [blame] | 525 | class StrokeRectBatch : public GrBatch { |
| 526 | public: |
| 527 | struct Geometry { |
| 528 | GrColor fColor; |
| 529 | SkMatrix fViewMatrix; |
| 530 | SkRect fRect; |
| 531 | SkScalar fStrokeWidth; |
| 532 | }; |
| 533 | |
| 534 | static GrBatch* Create(const Geometry& geometry) { |
| 535 | return SkNEW_ARGS(StrokeRectBatch, (geometry)); |
| 536 | } |
| 537 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 538 | const char* name() const override { return "StrokeRectBatch"; } |
joshualitt | 0823bfa | 2015-02-27 10:06:15 -0800 | [diff] [blame] | 539 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 540 | void getInvariantOutputColor(GrInitInvariantOutput* out) const override { |
joshualitt | 0823bfa | 2015-02-27 10:06:15 -0800 | [diff] [blame] | 541 | // When this is called on a batch, there is only one geometry bundle |
| 542 | out->setKnownFourComponents(fGeoData[0].fColor); |
| 543 | } |
| 544 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 545 | void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override { |
joshualitt | 0823bfa | 2015-02-27 10:06:15 -0800 | [diff] [blame] | 546 | out->setKnownSingleComponent(0xff); |
| 547 | } |
| 548 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 549 | void initBatchTracker(const GrPipelineInfo& init) override { |
joshualitt | 0823bfa | 2015-02-27 10:06:15 -0800 | [diff] [blame] | 550 | // Handle any color overrides |
| 551 | if (init.fColorIgnored) { |
| 552 | fGeoData[0].fColor = GrColor_ILLEGAL; |
| 553 | } else if (GrColor_ILLEGAL != init.fOverrideColor) { |
| 554 | fGeoData[0].fColor = init.fOverrideColor; |
| 555 | } |
| 556 | |
| 557 | // setup batch properties |
| 558 | fBatch.fColorIgnored = init.fColorIgnored; |
| 559 | fBatch.fColor = fGeoData[0].fColor; |
| 560 | fBatch.fUsesLocalCoords = init.fUsesLocalCoords; |
| 561 | fBatch.fCoverageIgnored = init.fCoverageIgnored; |
| 562 | } |
| 563 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 564 | void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override { |
joshualitt | 0823bfa | 2015-02-27 10:06:15 -0800 | [diff] [blame] | 565 | SkAutoTUnref<const GrGeometryProcessor> gp( |
| 566 | GrDefaultGeoProcFactory::Create(GrDefaultGeoProcFactory::kPosition_GPType, |
| 567 | this->color(), |
| 568 | this->viewMatrix(), |
| 569 | SkMatrix::I())); |
| 570 | |
| 571 | batchTarget->initDraw(gp, pipeline); |
| 572 | |
| 573 | // TODO this is hacky, but the only way we have to initialize the GP is to use the |
| 574 | // GrPipelineInfo struct so we can generate the correct shader. Once we have GrBatch |
| 575 | // everywhere we can remove this nastiness |
| 576 | GrPipelineInfo init; |
| 577 | init.fColorIgnored = fBatch.fColorIgnored; |
| 578 | init.fOverrideColor = GrColor_ILLEGAL; |
| 579 | init.fCoverageIgnored = fBatch.fCoverageIgnored; |
| 580 | init.fUsesLocalCoords = this->usesLocalCoords(); |
| 581 | gp->initBatchTracker(batchTarget->currentBatchTracker(), init); |
| 582 | |
| 583 | size_t vertexStride = gp->getVertexStride(); |
| 584 | |
| 585 | SkASSERT(vertexStride == sizeof(GrDefaultGeoProcFactory::PositionAttr)); |
| 586 | |
| 587 | Geometry& args = fGeoData[0]; |
| 588 | |
| 589 | int vertexCount = kVertsPerHairlineRect; |
| 590 | if (args.fStrokeWidth > 0) { |
| 591 | vertexCount = kVertsPerStrokeRect; |
| 592 | } |
| 593 | |
| 594 | const GrVertexBuffer* vertexBuffer; |
| 595 | int firstVertex; |
| 596 | |
| 597 | void* vertices = batchTarget->vertexPool()->makeSpace(vertexStride, |
| 598 | vertexCount, |
| 599 | &vertexBuffer, |
| 600 | &firstVertex); |
| 601 | |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 602 | if (!vertices) { |
| 603 | SkDebugf("Could not allocate vertices\n"); |
| 604 | return; |
| 605 | } |
| 606 | |
joshualitt | 0823bfa | 2015-02-27 10:06:15 -0800 | [diff] [blame] | 607 | SkPoint* vertex = reinterpret_cast<SkPoint*>(vertices); |
| 608 | |
| 609 | GrPrimitiveType primType; |
| 610 | |
| 611 | if (args.fStrokeWidth > 0) {; |
| 612 | primType = kTriangleStrip_GrPrimitiveType; |
| 613 | args.fRect.sort(); |
| 614 | this->setStrokeRectStrip(vertex, args.fRect, args.fStrokeWidth); |
| 615 | } else { |
| 616 | // hairline |
| 617 | primType = kLineStrip_GrPrimitiveType; |
| 618 | vertex[0].set(args.fRect.fLeft, args.fRect.fTop); |
| 619 | vertex[1].set(args.fRect.fRight, args.fRect.fTop); |
| 620 | vertex[2].set(args.fRect.fRight, args.fRect.fBottom); |
| 621 | vertex[3].set(args.fRect.fLeft, args.fRect.fBottom); |
| 622 | vertex[4].set(args.fRect.fLeft, args.fRect.fTop); |
| 623 | } |
| 624 | |
| 625 | GrDrawTarget::DrawInfo drawInfo; |
| 626 | drawInfo.setPrimitiveType(primType); |
| 627 | drawInfo.setVertexBuffer(vertexBuffer); |
| 628 | drawInfo.setStartVertex(firstVertex); |
| 629 | drawInfo.setVertexCount(vertexCount); |
| 630 | drawInfo.setStartIndex(0); |
| 631 | drawInfo.setIndexCount(0); |
| 632 | drawInfo.setInstanceCount(0); |
| 633 | drawInfo.setVerticesPerInstance(0); |
| 634 | drawInfo.setIndicesPerInstance(0); |
| 635 | batchTarget->draw(drawInfo); |
| 636 | } |
| 637 | |
| 638 | SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
| 639 | |
| 640 | private: |
| 641 | StrokeRectBatch(const Geometry& geometry) { |
| 642 | this->initClassID<StrokeRectBatch>(); |
| 643 | |
| 644 | fBatch.fHairline = geometry.fStrokeWidth == 0; |
| 645 | |
| 646 | fGeoData.push_back(geometry); |
| 647 | } |
| 648 | |
| 649 | /* create a triangle strip that strokes the specified rect. There are 8 |
| 650 | unique vertices, but we repeat the last 2 to close up. Alternatively we |
| 651 | could use an indices array, and then only send 8 verts, but not sure that |
| 652 | would be faster. |
| 653 | */ |
| 654 | void setStrokeRectStrip(SkPoint verts[10], const SkRect& rect, SkScalar width) { |
| 655 | const SkScalar rad = SkScalarHalf(width); |
| 656 | // TODO we should be able to enable this assert, but we'd have to filter these draws |
| 657 | // this is a bug |
| 658 | //SkASSERT(rad < rect.width() / 2 && rad < rect.height() / 2); |
| 659 | |
| 660 | verts[0].set(rect.fLeft + rad, rect.fTop + rad); |
| 661 | verts[1].set(rect.fLeft - rad, rect.fTop - rad); |
| 662 | verts[2].set(rect.fRight - rad, rect.fTop + rad); |
| 663 | verts[3].set(rect.fRight + rad, rect.fTop - rad); |
| 664 | verts[4].set(rect.fRight - rad, rect.fBottom - rad); |
| 665 | verts[5].set(rect.fRight + rad, rect.fBottom + rad); |
| 666 | verts[6].set(rect.fLeft + rad, rect.fBottom - rad); |
| 667 | verts[7].set(rect.fLeft - rad, rect.fBottom + rad); |
| 668 | verts[8] = verts[0]; |
| 669 | verts[9] = verts[1]; |
| 670 | } |
| 671 | |
| 672 | |
| 673 | GrColor color() const { return fBatch.fColor; } |
| 674 | bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 675 | bool colorIgnored() const { return fBatch.fColorIgnored; } |
| 676 | const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } |
| 677 | bool hairline() const { return fBatch.fHairline; } |
| 678 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 679 | bool onCombineIfPossible(GrBatch* t) override { |
joshualitt | 0823bfa | 2015-02-27 10:06:15 -0800 | [diff] [blame] | 680 | // StrokeRectBatch* that = t->cast<StrokeRectBatch>(); |
| 681 | |
| 682 | // NonAA stroke rects cannot batch right now |
| 683 | // TODO make these batchable |
| 684 | return false; |
| 685 | } |
| 686 | |
| 687 | struct BatchTracker { |
| 688 | GrColor fColor; |
| 689 | bool fUsesLocalCoords; |
| 690 | bool fColorIgnored; |
| 691 | bool fCoverageIgnored; |
| 692 | bool fHairline; |
| 693 | }; |
| 694 | |
| 695 | const static int kVertsPerHairlineRect = 5; |
| 696 | const static int kVertsPerStrokeRect = 10; |
| 697 | |
| 698 | BatchTracker fBatch; |
| 699 | SkSTArray<1, Geometry, true> fGeoData; |
| 700 | }; |
| 701 | |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 702 | void GrContext::drawRect(GrRenderTarget* rt, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 703 | const GrClip& clip, |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 704 | const GrPaint& paint, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 705 | const SkMatrix& viewMatrix, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 706 | const SkRect& rect, |
bsalomon | 01c8da1 | 2014-08-04 09:21:30 -0700 | [diff] [blame] | 707 | const GrStrokeInfo* strokeInfo) { |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 708 | RETURN_IF_ABANDONED |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 709 | if (strokeInfo && strokeInfo->isDashed()) { |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 710 | SkPath path; |
| 711 | path.addRect(rect); |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 712 | this->drawPath(rt, clip, paint, viewMatrix, path, *strokeInfo); |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 713 | return; |
| 714 | } |
| 715 | |
bsalomon | f21dab9 | 2014-11-13 13:33:28 -0800 | [diff] [blame] | 716 | AutoCheckFlush acf(this); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 717 | GrPipelineBuilder pipelineBuilder; |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 718 | GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf); |
bsalomon | 41ebbdd | 2014-08-04 08:31:39 -0700 | [diff] [blame] | 719 | if (NULL == target) { |
| 720 | return; |
| 721 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 722 | |
commit-bot@chromium.org | 2a05de0 | 2014-03-25 15:17:32 +0000 | [diff] [blame] | 723 | GR_CREATE_TRACE_MARKER("GrContext::drawRect", target); |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 724 | SkScalar width = NULL == strokeInfo ? -1 : strokeInfo->getStrokeRec().getWidth(); |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 725 | |
| 726 | // Check if this is a full RT draw and can be replaced with a clear. We don't bother checking |
| 727 | // cases where the RT is fully inside a stroke. |
| 728 | if (width < 0) { |
| 729 | SkRect rtRect; |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 730 | pipelineBuilder.getRenderTarget()->getBoundsRect(&rtRect); |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 731 | SkRect clipSpaceRTRect = rtRect; |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 732 | bool checkClip = GrClip::kWideOpen_ClipType != clip.clipType(); |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 733 | if (checkClip) { |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 734 | clipSpaceRTRect.offset(SkIntToScalar(clip.origin().fX), |
| 735 | SkIntToScalar(clip.origin().fY)); |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 736 | } |
| 737 | // Does the clip contain the entire RT? |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 738 | if (!checkClip || clip.quickContains(clipSpaceRTRect)) { |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 739 | SkMatrix invM; |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 740 | if (!viewMatrix.invert(&invM)) { |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 741 | return; |
| 742 | } |
| 743 | // Does the rect bound the RT? |
| 744 | SkPoint srcSpaceRTQuad[4]; |
| 745 | invM.mapRectToQuad(srcSpaceRTQuad, rtRect); |
| 746 | if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) && |
| 747 | rect_contains_inclusive(rect, srcSpaceRTQuad[1]) && |
| 748 | rect_contains_inclusive(rect, srcSpaceRTQuad[2]) && |
| 749 | rect_contains_inclusive(rect, srcSpaceRTQuad[3])) { |
| 750 | // Will it blend? |
| 751 | GrColor clearColor; |
| 752 | if (paint.isOpaqueAndConstantColor(&clearColor)) { |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 753 | target->clear(NULL, clearColor, true, rt); |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 754 | return; |
| 755 | } |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 760 | GrColor color = paint.getColor(); |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 761 | SkRect devBoundRect; |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 762 | bool needAA = paint.isAntiAlias() && !pipelineBuilder.getRenderTarget()->isMultisampled(); |
| 763 | bool doAA = needAA && apply_aa_to_rect(target, &pipelineBuilder, &devBoundRect, rect, width, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 764 | viewMatrix, color); |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 765 | |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 766 | if (doAA) { |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 767 | if (width >= 0) { |
bsalomon | 395ef05 | 2014-11-12 11:35:22 -0800 | [diff] [blame] | 768 | const SkStrokeRec& strokeRec = strokeInfo->getStrokeRec(); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 769 | fAARectRenderer->strokeAARect(target, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 770 | &pipelineBuilder, |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 771 | color, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 772 | viewMatrix, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 773 | rect, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 774 | devBoundRect, |
| 775 | strokeRec); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 776 | } else { |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 777 | // filled AA rect |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 778 | fAARectRenderer->fillAARect(target, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 779 | &pipelineBuilder, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 780 | color, |
| 781 | viewMatrix, |
| 782 | rect, |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 783 | devBoundRect); |
bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 784 | } |
| 785 | return; |
| 786 | } |
| 787 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 788 | if (width >= 0) { |
joshualitt | 0823bfa | 2015-02-27 10:06:15 -0800 | [diff] [blame] | 789 | StrokeRectBatch::Geometry geometry; |
| 790 | geometry.fViewMatrix = viewMatrix; |
| 791 | geometry.fColor = color; |
| 792 | geometry.fRect = rect; |
| 793 | geometry.fStrokeWidth = width; |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 794 | |
joshualitt | 0823bfa | 2015-02-27 10:06:15 -0800 | [diff] [blame] | 795 | SkAutoTUnref<GrBatch> batch(StrokeRectBatch::Create(geometry)); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 796 | |
joshualitt | 0823bfa | 2015-02-27 10:06:15 -0800 | [diff] [blame] | 797 | SkRect bounds = rect; |
| 798 | SkScalar rad = SkScalarHalf(width); |
| 799 | bounds.outset(rad, rad); |
| 800 | viewMatrix.mapRect(&bounds); |
| 801 | target->drawBatch(&pipelineBuilder, batch, &bounds); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 802 | } else { |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 803 | // filled BW rect |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 804 | target->drawSimpleRect(&pipelineBuilder, color, viewMatrix, rect); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 805 | } |
| 806 | } |
| 807 | |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 808 | void GrContext::drawNonAARectToRect(GrRenderTarget* rt, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 809 | const GrClip& clip, |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 810 | const GrPaint& paint, |
joshualitt | 16b2789 | 2014-12-18 07:47:16 -0800 | [diff] [blame] | 811 | const SkMatrix& viewMatrix, |
| 812 | const SkRect& rectToDraw, |
| 813 | const SkRect& localRect, |
| 814 | const SkMatrix* localMatrix) { |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 815 | RETURN_IF_ABANDONED |
bsalomon | f21dab9 | 2014-11-13 13:33:28 -0800 | [diff] [blame] | 816 | AutoCheckFlush acf(this); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 817 | GrPipelineBuilder pipelineBuilder; |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 818 | GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf); |
bsalomon | 41ebbdd | 2014-08-04 08:31:39 -0700 | [diff] [blame] | 819 | if (NULL == target) { |
| 820 | return; |
| 821 | } |
bsalomon@google.com | 6438695 | 2013-02-08 21:22:44 +0000 | [diff] [blame] | 822 | |
commit-bot@chromium.org | 2a05de0 | 2014-03-25 15:17:32 +0000 | [diff] [blame] | 823 | GR_CREATE_TRACE_MARKER("GrContext::drawRectToRect", target); |
| 824 | |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 825 | target->drawRect(&pipelineBuilder, |
| 826 | paint.getColor(), |
| 827 | viewMatrix, |
| 828 | rectToDraw, |
| 829 | &localRect, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 830 | localMatrix); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 831 | } |
| 832 | |
joshualitt | 0823bfa | 2015-02-27 10:06:15 -0800 | [diff] [blame] | 833 | static const GrGeometryProcessor* set_vertex_attributes(bool hasLocalCoords, |
| 834 | bool hasColors, |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 835 | int* colorOffset, |
| 836 | int* texOffset, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 837 | GrColor color, |
| 838 | const SkMatrix& viewMatrix) { |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 839 | *texOffset = -1; |
| 840 | *colorOffset = -1; |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 841 | uint32_t flags = GrDefaultGeoProcFactory::kPosition_GPType; |
joshualitt | 0823bfa | 2015-02-27 10:06:15 -0800 | [diff] [blame] | 842 | if (hasLocalCoords && hasColors) { |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 843 | *colorOffset = sizeof(SkPoint); |
| 844 | *texOffset = sizeof(SkPoint) + sizeof(GrColor); |
| 845 | flags |= GrDefaultGeoProcFactory::kColor_GPType | |
| 846 | GrDefaultGeoProcFactory::kLocalCoord_GPType; |
joshualitt | 0823bfa | 2015-02-27 10:06:15 -0800 | [diff] [blame] | 847 | } else if (hasLocalCoords) { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 848 | *texOffset = sizeof(SkPoint); |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 849 | flags |= GrDefaultGeoProcFactory::kLocalCoord_GPType; |
joshualitt | 0823bfa | 2015-02-27 10:06:15 -0800 | [diff] [blame] | 850 | } else if (hasColors) { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 851 | *colorOffset = sizeof(SkPoint); |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 852 | flags |= GrDefaultGeoProcFactory::kColor_GPType; |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 853 | } |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 854 | return GrDefaultGeoProcFactory::Create(flags, color, viewMatrix, SkMatrix::I()); |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 855 | } |
| 856 | |
joshualitt | 0823bfa | 2015-02-27 10:06:15 -0800 | [diff] [blame] | 857 | class DrawVerticesBatch : public GrBatch { |
| 858 | public: |
| 859 | struct Geometry { |
| 860 | GrColor fColor; |
| 861 | SkTDArray<SkPoint> fPositions; |
| 862 | SkTDArray<uint16_t> fIndices; |
| 863 | SkTDArray<GrColor> fColors; |
| 864 | SkTDArray<SkPoint> fLocalCoords; |
| 865 | }; |
| 866 | |
| 867 | static GrBatch* Create(const Geometry& geometry, GrPrimitiveType primitiveType, |
| 868 | const SkMatrix& viewMatrix, |
| 869 | const SkPoint* positions, int vertexCount, |
| 870 | const uint16_t* indices, int indexCount, |
| 871 | const GrColor* colors, const SkPoint* localCoords) { |
| 872 | return SkNEW_ARGS(DrawVerticesBatch, (geometry, primitiveType, viewMatrix, positions, |
| 873 | vertexCount, indices, indexCount, colors, |
| 874 | localCoords)); |
| 875 | } |
| 876 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 877 | const char* name() const override { return "DrawVerticesBatch"; } |
joshualitt | 0823bfa | 2015-02-27 10:06:15 -0800 | [diff] [blame] | 878 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 879 | void getInvariantOutputColor(GrInitInvariantOutput* out) const override { |
joshualitt | 0823bfa | 2015-02-27 10:06:15 -0800 | [diff] [blame] | 880 | // When this is called on a batch, there is only one geometry bundle |
| 881 | if (this->hasColors()) { |
| 882 | out->setUnknownFourComponents(); |
| 883 | } else { |
| 884 | out->setKnownFourComponents(fGeoData[0].fColor); |
| 885 | } |
| 886 | } |
| 887 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 888 | void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override { |
joshualitt | d4c7d55 | 2015-02-27 13:00:56 -0800 | [diff] [blame] | 889 | out->setKnownSingleComponent(0xff); |
joshualitt | 0823bfa | 2015-02-27 10:06:15 -0800 | [diff] [blame] | 890 | } |
| 891 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 892 | void initBatchTracker(const GrPipelineInfo& init) override { |
joshualitt | 0823bfa | 2015-02-27 10:06:15 -0800 | [diff] [blame] | 893 | // Handle any color overrides |
| 894 | if (init.fColorIgnored) { |
| 895 | fGeoData[0].fColor = GrColor_ILLEGAL; |
| 896 | } else if (GrColor_ILLEGAL != init.fOverrideColor) { |
| 897 | fGeoData[0].fColor = init.fOverrideColor; |
| 898 | } |
| 899 | |
| 900 | // setup batch properties |
| 901 | fBatch.fColorIgnored = init.fColorIgnored; |
| 902 | fBatch.fColor = fGeoData[0].fColor; |
| 903 | fBatch.fUsesLocalCoords = init.fUsesLocalCoords; |
| 904 | fBatch.fCoverageIgnored = init.fCoverageIgnored; |
| 905 | } |
| 906 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 907 | void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override { |
joshualitt | 0823bfa | 2015-02-27 10:06:15 -0800 | [diff] [blame] | 908 | int colorOffset = -1, texOffset = -1; |
| 909 | SkAutoTUnref<const GrGeometryProcessor> gp( |
| 910 | set_vertex_attributes(this->hasLocalCoords(), this->hasColors(), &colorOffset, |
| 911 | &texOffset, this->color(), this->viewMatrix())); |
| 912 | |
| 913 | batchTarget->initDraw(gp, pipeline); |
| 914 | |
| 915 | // TODO this is hacky, but the only way we have to initialize the GP is to use the |
| 916 | // GrPipelineInfo struct so we can generate the correct shader. Once we have GrBatch |
| 917 | // everywhere we can remove this nastiness |
| 918 | GrPipelineInfo init; |
| 919 | init.fColorIgnored = fBatch.fColorIgnored; |
| 920 | init.fOverrideColor = GrColor_ILLEGAL; |
| 921 | init.fCoverageIgnored = fBatch.fCoverageIgnored; |
| 922 | init.fUsesLocalCoords = this->usesLocalCoords(); |
| 923 | gp->initBatchTracker(batchTarget->currentBatchTracker(), init); |
| 924 | |
| 925 | size_t vertexStride = gp->getVertexStride(); |
| 926 | |
| 927 | SkASSERT(vertexStride == sizeof(SkPoint) + (this->hasLocalCoords() ? sizeof(SkPoint) : 0) |
| 928 | + (this->hasColors() ? sizeof(GrColor) : 0)); |
| 929 | |
| 930 | int instanceCount = fGeoData.count(); |
| 931 | |
| 932 | const GrVertexBuffer* vertexBuffer; |
| 933 | int firstVertex; |
| 934 | |
| 935 | void* vertices = batchTarget->vertexPool()->makeSpace(vertexStride, |
| 936 | this->vertexCount(), |
| 937 | &vertexBuffer, |
| 938 | &firstVertex); |
| 939 | |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 940 | if (!vertices) { |
| 941 | SkDebugf("Could not allocate vertices\n"); |
| 942 | return; |
| 943 | } |
| 944 | |
joshualitt | 0823bfa | 2015-02-27 10:06:15 -0800 | [diff] [blame] | 945 | const GrIndexBuffer* indexBuffer; |
| 946 | int firstIndex; |
| 947 | |
| 948 | void* indices = NULL; |
| 949 | if (this->hasIndices()) { |
| 950 | indices = batchTarget->indexPool()->makeSpace(this->indexCount(), |
| 951 | &indexBuffer, |
| 952 | &firstIndex); |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 953 | |
| 954 | if (!indices) { |
| 955 | SkDebugf("Could not allocate indices\n"); |
| 956 | return; |
| 957 | } |
joshualitt | 0823bfa | 2015-02-27 10:06:15 -0800 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | int indexOffset = 0; |
| 961 | int vertexOffset = 0; |
| 962 | for (int i = 0; i < instanceCount; i++) { |
| 963 | const Geometry& args = fGeoData[i]; |
| 964 | |
| 965 | // TODO we can actually cache this interleaved and then just memcopy |
| 966 | if (this->hasIndices()) { |
| 967 | for (int j = 0; j < args.fIndices.count(); ++j, ++indexOffset) { |
| 968 | *((uint16_t*)indices + indexOffset) = args.fIndices[j] + vertexOffset; |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | for (int j = 0; j < args.fPositions.count(); ++j) { |
| 973 | *((SkPoint*)vertices) = args.fPositions[j]; |
| 974 | if (this->hasColors()) { |
| 975 | *(GrColor*)((intptr_t)vertices + colorOffset) = args.fColors[j]; |
| 976 | } |
| 977 | if (this->hasLocalCoords()) { |
| 978 | *(SkPoint*)((intptr_t)vertices + texOffset) = args.fLocalCoords[j]; |
| 979 | } |
| 980 | vertices = (void*)((intptr_t)vertices + vertexStride); |
| 981 | vertexOffset++; |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | GrDrawTarget::DrawInfo drawInfo; |
| 986 | drawInfo.setPrimitiveType(this->primitiveType()); |
| 987 | drawInfo.setVertexBuffer(vertexBuffer); |
| 988 | drawInfo.setStartVertex(firstVertex); |
| 989 | drawInfo.setVertexCount(this->vertexCount()); |
| 990 | if (this->hasIndices()) { |
| 991 | drawInfo.setIndexBuffer(indexBuffer); |
| 992 | drawInfo.setStartIndex(firstIndex); |
| 993 | drawInfo.setIndexCount(this->indexCount()); |
| 994 | } else { |
| 995 | drawInfo.setStartIndex(0); |
| 996 | drawInfo.setIndexCount(0); |
| 997 | } |
| 998 | batchTarget->draw(drawInfo); |
| 999 | } |
| 1000 | |
| 1001 | SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
| 1002 | |
| 1003 | private: |
| 1004 | DrawVerticesBatch(const Geometry& geometry, GrPrimitiveType primitiveType, |
| 1005 | const SkMatrix& viewMatrix, |
| 1006 | const SkPoint* positions, int vertexCount, |
| 1007 | const uint16_t* indices, int indexCount, |
| 1008 | const GrColor* colors, const SkPoint* localCoords) { |
| 1009 | this->initClassID<DrawVerticesBatch>(); |
| 1010 | SkASSERT(positions); |
| 1011 | |
| 1012 | fBatch.fViewMatrix = viewMatrix; |
| 1013 | Geometry& installedGeo = fGeoData.push_back(geometry); |
| 1014 | |
| 1015 | installedGeo.fPositions.append(vertexCount, positions); |
| 1016 | if (indices) { |
| 1017 | installedGeo.fIndices.append(indexCount, indices); |
| 1018 | fBatch.fHasIndices = true; |
| 1019 | } else { |
| 1020 | fBatch.fHasIndices = false; |
| 1021 | } |
| 1022 | |
| 1023 | if (colors) { |
| 1024 | installedGeo.fColors.append(vertexCount, colors); |
| 1025 | fBatch.fHasColors = true; |
| 1026 | } else { |
| 1027 | fBatch.fHasColors = false; |
| 1028 | } |
| 1029 | |
| 1030 | if (localCoords) { |
| 1031 | installedGeo.fLocalCoords.append(vertexCount, localCoords); |
| 1032 | fBatch.fHasLocalCoords = true; |
| 1033 | } else { |
| 1034 | fBatch.fHasLocalCoords = false; |
| 1035 | } |
| 1036 | fBatch.fVertexCount = vertexCount; |
| 1037 | fBatch.fIndexCount = indexCount; |
| 1038 | fBatch.fPrimitiveType = primitiveType; |
| 1039 | } |
| 1040 | |
| 1041 | GrPrimitiveType primitiveType() const { return fBatch.fPrimitiveType; } |
| 1042 | bool batchablePrimitiveType() const { |
| 1043 | return kTriangles_GrPrimitiveType == fBatch.fPrimitiveType || |
| 1044 | kLines_GrPrimitiveType == fBatch.fPrimitiveType || |
| 1045 | kPoints_GrPrimitiveType == fBatch.fPrimitiveType; |
| 1046 | } |
| 1047 | GrColor color() const { return fBatch.fColor; } |
| 1048 | bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 1049 | bool colorIgnored() const { return fBatch.fColorIgnored; } |
| 1050 | const SkMatrix& viewMatrix() const { return fBatch.fViewMatrix; } |
| 1051 | bool hasColors() const { return fBatch.fHasColors; } |
| 1052 | bool hasIndices() const { return fBatch.fHasIndices; } |
| 1053 | bool hasLocalCoords() const { return fBatch.fHasLocalCoords; } |
| 1054 | int vertexCount() const { return fBatch.fVertexCount; } |
| 1055 | int indexCount() const { return fBatch.fIndexCount; } |
| 1056 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1057 | bool onCombineIfPossible(GrBatch* t) override { |
joshualitt | 0823bfa | 2015-02-27 10:06:15 -0800 | [diff] [blame] | 1058 | DrawVerticesBatch* that = t->cast<DrawVerticesBatch>(); |
| 1059 | |
| 1060 | if (!this->batchablePrimitiveType() || this->primitiveType() != that->primitiveType()) { |
| 1061 | return false; |
| 1062 | } |
| 1063 | |
| 1064 | SkASSERT(this->usesLocalCoords() == that->usesLocalCoords()); |
| 1065 | |
| 1066 | // We currently use a uniform viewmatrix for this batch |
| 1067 | if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
| 1068 | return false; |
| 1069 | } |
| 1070 | |
| 1071 | if (this->hasColors() != that->hasColors()) { |
| 1072 | return false; |
| 1073 | } |
| 1074 | |
| 1075 | if (this->hasIndices() != that->hasIndices()) { |
| 1076 | return false; |
| 1077 | } |
| 1078 | |
| 1079 | if (this->hasLocalCoords() != that->hasLocalCoords()) { |
| 1080 | return false; |
| 1081 | } |
| 1082 | |
| 1083 | if (!this->hasColors() && this->color() != that->color()) { |
| 1084 | return false; |
| 1085 | } |
| 1086 | |
| 1087 | if (this->color() != that->color()) { |
| 1088 | fBatch.fColor = GrColor_ILLEGAL; |
| 1089 | } |
| 1090 | fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); |
| 1091 | fBatch.fVertexCount += that->vertexCount(); |
| 1092 | fBatch.fIndexCount += that->indexCount(); |
| 1093 | return true; |
| 1094 | } |
| 1095 | |
| 1096 | struct BatchTracker { |
| 1097 | GrPrimitiveType fPrimitiveType; |
| 1098 | SkMatrix fViewMatrix; |
| 1099 | GrColor fColor; |
| 1100 | bool fUsesLocalCoords; |
| 1101 | bool fColorIgnored; |
| 1102 | bool fCoverageIgnored; |
| 1103 | bool fHasColors; |
| 1104 | bool fHasIndices; |
| 1105 | bool fHasLocalCoords; |
| 1106 | int fVertexCount; |
| 1107 | int fIndexCount; |
| 1108 | }; |
| 1109 | |
| 1110 | BatchTracker fBatch; |
| 1111 | SkSTArray<1, Geometry, true> fGeoData; |
| 1112 | }; |
| 1113 | |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 1114 | void GrContext::drawVertices(GrRenderTarget* rt, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 1115 | const GrClip& clip, |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 1116 | const GrPaint& paint, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1117 | const SkMatrix& viewMatrix, |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1118 | GrPrimitiveType primitiveType, |
| 1119 | int vertexCount, |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 1120 | const SkPoint positions[], |
| 1121 | const SkPoint texCoords[], |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1122 | const GrColor colors[], |
| 1123 | const uint16_t indices[], |
| 1124 | int indexCount) { |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 1125 | RETURN_IF_ABANDONED |
bsalomon | f21dab9 | 2014-11-13 13:33:28 -0800 | [diff] [blame] | 1126 | AutoCheckFlush acf(this); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1127 | GrPipelineBuilder pipelineBuilder; |
bsalomon | f21dab9 | 2014-11-13 13:33:28 -0800 | [diff] [blame] | 1128 | GrDrawTarget::AutoReleaseGeometry geo; // must be inside AutoCheckFlush scope |
commit-bot@chromium.org | 5a56793 | 2014-01-08 21:26:09 +0000 | [diff] [blame] | 1129 | |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 1130 | GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf); |
bsalomon | 41ebbdd | 2014-08-04 08:31:39 -0700 | [diff] [blame] | 1131 | if (NULL == target) { |
| 1132 | return; |
| 1133 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1134 | |
commit-bot@chromium.org | 2a05de0 | 2014-03-25 15:17:32 +0000 | [diff] [blame] | 1135 | GR_CREATE_TRACE_MARKER("GrContext::drawVertices", target); |
| 1136 | |
joshualitt | 0823bfa | 2015-02-27 10:06:15 -0800 | [diff] [blame] | 1137 | DrawVerticesBatch::Geometry geometry; |
| 1138 | geometry.fColor = paint.getColor(); |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 1139 | |
joshualitt | 0823bfa | 2015-02-27 10:06:15 -0800 | [diff] [blame] | 1140 | SkAutoTUnref<GrBatch> batch(DrawVerticesBatch::Create(geometry, primitiveType, viewMatrix, |
| 1141 | positions, vertexCount, indices, |
| 1142 | indexCount,colors, texCoords)); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1143 | |
joshualitt | 0823bfa | 2015-02-27 10:06:15 -0800 | [diff] [blame] | 1144 | // TODO figure out bounds |
| 1145 | target->drawBatch(&pipelineBuilder, batch, NULL); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 1148 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 1149 | |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 1150 | void GrContext::drawRRect(GrRenderTarget*rt, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 1151 | const GrClip& clip, |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 1152 | const GrPaint& paint, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1153 | const SkMatrix& viewMatrix, |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1154 | const SkRRect& rrect, |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1155 | const GrStrokeInfo& strokeInfo) { |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 1156 | RETURN_IF_ABANDONED |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1157 | if (rrect.isEmpty()) { |
commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 1158 | return; |
| 1159 | } |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1160 | |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1161 | if (strokeInfo.isDashed()) { |
| 1162 | SkPath path; |
| 1163 | path.addRRect(rrect); |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 1164 | this->drawPath(rt, clip, paint, viewMatrix, path, strokeInfo); |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1165 | return; |
| 1166 | } |
| 1167 | |
bsalomon | f21dab9 | 2014-11-13 13:33:28 -0800 | [diff] [blame] | 1168 | AutoCheckFlush acf(this); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1169 | GrPipelineBuilder pipelineBuilder; |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 1170 | GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf); |
bsalomon | 41ebbdd | 2014-08-04 08:31:39 -0700 | [diff] [blame] | 1171 | if (NULL == target) { |
| 1172 | return; |
| 1173 | } |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1174 | |
commit-bot@chromium.org | 2a05de0 | 2014-03-25 15:17:32 +0000 | [diff] [blame] | 1175 | GR_CREATE_TRACE_MARKER("GrContext::drawRRect", target); |
| 1176 | |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1177 | const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec(); |
| 1178 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 1179 | GrColor color = paint.getColor(); |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 1180 | if (!fOvalRenderer->drawRRect(target, |
| 1181 | &pipelineBuilder, |
| 1182 | color, |
| 1183 | viewMatrix, |
| 1184 | paint.isAntiAlias(), |
| 1185 | rrect, |
| 1186 | strokeRec)) { |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1187 | SkPath path; |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1188 | path.addRRect(rrect); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1189 | this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(), |
| 1190 | path, strokeInfo); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | /////////////////////////////////////////////////////////////////////////////// |
| 1195 | |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 1196 | void GrContext::drawDRRect(GrRenderTarget* rt, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 1197 | const GrClip& clip, |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 1198 | const GrPaint& paint, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1199 | const SkMatrix& viewMatrix, |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1200 | const SkRRect& outer, |
| 1201 | const SkRRect& inner) { |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 1202 | RETURN_IF_ABANDONED |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1203 | if (outer.isEmpty()) { |
| 1204 | return; |
| 1205 | } |
| 1206 | |
bsalomon | f21dab9 | 2014-11-13 13:33:28 -0800 | [diff] [blame] | 1207 | AutoCheckFlush acf(this); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1208 | GrPipelineBuilder pipelineBuilder; |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 1209 | GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf); |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1210 | |
| 1211 | GR_CREATE_TRACE_MARKER("GrContext::drawDRRect", target); |
| 1212 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 1213 | GrColor color = paint.getColor(); |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 1214 | if (!fOvalRenderer->drawDRRect(target, |
| 1215 | &pipelineBuilder, |
| 1216 | color, |
| 1217 | viewMatrix, |
| 1218 | paint.isAntiAlias(), |
| 1219 | outer, |
| 1220 | inner)) { |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1221 | SkPath path; |
| 1222 | path.addRRect(inner); |
| 1223 | path.addRRect(outer); |
| 1224 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 1225 | |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1226 | GrStrokeInfo fillRec(SkStrokeRec::kFill_InitStyle); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1227 | this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(), |
| 1228 | path, fillRec); |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | /////////////////////////////////////////////////////////////////////////////// |
| 1233 | |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 1234 | void GrContext::drawOval(GrRenderTarget* rt, |
| 1235 | const GrClip& clip, |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 1236 | const GrPaint& paint, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1237 | const SkMatrix& viewMatrix, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 1238 | const SkRect& oval, |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1239 | const GrStrokeInfo& strokeInfo) { |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 1240 | RETURN_IF_ABANDONED |
commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 1241 | if (oval.isEmpty()) { |
| 1242 | return; |
| 1243 | } |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 1244 | |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1245 | if (strokeInfo.isDashed()) { |
| 1246 | SkPath path; |
| 1247 | path.addOval(oval); |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 1248 | this->drawPath(rt, clip, paint, viewMatrix, path, strokeInfo); |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1249 | return; |
| 1250 | } |
| 1251 | |
bsalomon | f21dab9 | 2014-11-13 13:33:28 -0800 | [diff] [blame] | 1252 | AutoCheckFlush acf(this); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1253 | GrPipelineBuilder pipelineBuilder; |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 1254 | GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf); |
bsalomon | 41ebbdd | 2014-08-04 08:31:39 -0700 | [diff] [blame] | 1255 | if (NULL == target) { |
| 1256 | return; |
| 1257 | } |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 1258 | |
commit-bot@chromium.org | 2a05de0 | 2014-03-25 15:17:32 +0000 | [diff] [blame] | 1259 | GR_CREATE_TRACE_MARKER("GrContext::drawOval", target); |
| 1260 | |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1261 | const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec(); |
| 1262 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 1263 | GrColor color = paint.getColor(); |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 1264 | if (!fOvalRenderer->drawOval(target, |
| 1265 | &pipelineBuilder, |
| 1266 | color, |
| 1267 | viewMatrix, |
| 1268 | paint.isAntiAlias(), |
| 1269 | oval, |
| 1270 | strokeRec)) { |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 1271 | SkPath path; |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 1272 | path.addOval(oval); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1273 | this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(), |
| 1274 | path, strokeInfo); |
skia.committer@gmail.com | 98ded84 | 2013-01-23 07:06:17 +0000 | [diff] [blame] | 1275 | } |
bsalomon@google.com | 150d284 | 2012-01-12 20:19:56 +0000 | [diff] [blame] | 1276 | } |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1277 | |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1278 | // Can 'path' be drawn as a pair of filled nested rectangles? |
| 1279 | static bool is_nested_rects(GrDrawTarget* target, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1280 | GrPipelineBuilder* pipelineBuilder, |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 1281 | GrColor color, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1282 | const SkMatrix& viewMatrix, |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1283 | const SkPath& path, |
| 1284 | const SkStrokeRec& stroke, |
bsalomon | 9c0822a | 2014-08-11 11:07:48 -0700 | [diff] [blame] | 1285 | SkRect rects[2]) { |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1286 | SkASSERT(stroke.isFillStyle()); |
| 1287 | |
| 1288 | if (path.isInverseFillType()) { |
| 1289 | return false; |
| 1290 | } |
| 1291 | |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1292 | // TODO: this restriction could be lifted if we were willing to apply |
| 1293 | // the matrix to all the points individually rather than just to the rect |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1294 | if (!viewMatrix.preservesAxisAlignment()) { |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1295 | return false; |
| 1296 | } |
| 1297 | |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1298 | SkPath::Direction dirs[2]; |
caryclark | 95bc5f3 | 2015-04-08 08:34:15 -0700 | [diff] [blame] | 1299 | if (!path.isNestedFillRects(rects, dirs)) { |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1300 | return false; |
| 1301 | } |
| 1302 | |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 1303 | if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) { |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1304 | // The two rects need to be wound opposite to each other |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 1305 | return false; |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1306 | } |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1307 | |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 1308 | // Right now, nested rects where the margin is not the same width |
| 1309 | // all around do not render correctly |
| 1310 | const SkScalar* outer = rects[0].asScalars(); |
| 1311 | const SkScalar* inner = rects[1].asScalars(); |
| 1312 | |
robertphillips | 183e985 | 2014-10-21 11:25:37 -0700 | [diff] [blame] | 1313 | bool allEq = true; |
| 1314 | |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 1315 | SkScalar margin = SkScalarAbs(outer[0] - inner[0]); |
robertphillips | 183e985 | 2014-10-21 11:25:37 -0700 | [diff] [blame] | 1316 | bool allGoE1 = margin >= SK_Scalar1; |
| 1317 | |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 1318 | for (int i = 1; i < 4; ++i) { |
| 1319 | SkScalar temp = SkScalarAbs(outer[i] - inner[i]); |
robertphillips | 183e985 | 2014-10-21 11:25:37 -0700 | [diff] [blame] | 1320 | if (temp < SK_Scalar1) { |
| 1321 | allGoE1 = false; |
| 1322 | } |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 1323 | if (!SkScalarNearlyEqual(margin, temp)) { |
robertphillips | 183e985 | 2014-10-21 11:25:37 -0700 | [diff] [blame] | 1324 | allEq = false; |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 1325 | } |
| 1326 | } |
| 1327 | |
robertphillips | 183e985 | 2014-10-21 11:25:37 -0700 | [diff] [blame] | 1328 | return allEq || allGoE1; |
robertphillips@google.com | 8d3c640 | 2013-08-20 12:11:31 +0000 | [diff] [blame] | 1329 | } |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1330 | |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 1331 | void GrContext::drawPath(GrRenderTarget* rt, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 1332 | const GrClip& clip, |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 1333 | const GrPaint& paint, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1334 | const SkMatrix& viewMatrix, |
| 1335 | const SkPath& path, |
| 1336 | const GrStrokeInfo& strokeInfo) { |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 1337 | RETURN_IF_ABANDONED |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 1338 | if (path.isEmpty()) { |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 1339 | if (path.isInverseFillType()) { |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 1340 | this->drawPaint(rt, clip, paint, viewMatrix); |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 1341 | } |
| 1342 | return; |
| 1343 | } |
| 1344 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 1345 | GrColor color = paint.getColor(); |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1346 | if (strokeInfo.isDashed()) { |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 1347 | SkPoint pts[2]; |
| 1348 | if (path.isLine(pts)) { |
bsalomon | f21dab9 | 2014-11-13 13:33:28 -0800 | [diff] [blame] | 1349 | AutoCheckFlush acf(this); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1350 | GrPipelineBuilder pipelineBuilder; |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 1351 | GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf); |
bsalomon | 41ebbdd | 2014-08-04 08:31:39 -0700 | [diff] [blame] | 1352 | if (NULL == target) { |
| 1353 | return; |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 1354 | } |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 1355 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1356 | if (GrDashingEffect::DrawDashLine(fGpu, target, &pipelineBuilder, color, viewMatrix, |
| 1357 | pts, paint, strokeInfo)) { |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 1358 | return; |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 1359 | } |
| 1360 | } |
| 1361 | |
| 1362 | // Filter dashed path into new path with the dashing applied |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1363 | const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo(); |
| 1364 | SkTLazy<SkPath> effectPath; |
| 1365 | GrStrokeInfo newStrokeInfo(strokeInfo, false); |
| 1366 | SkStrokeRec* stroke = newStrokeInfo.getStrokeRecPtr(); |
| 1367 | if (SkDashPath::FilterDashPath(effectPath.init(), path, stroke, NULL, info)) { |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 1368 | this->drawPath(rt, clip, paint, viewMatrix, *effectPath.get(), newStrokeInfo); |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1369 | return; |
| 1370 | } |
| 1371 | |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 1372 | this->drawPath(rt, clip, paint, viewMatrix, path, newStrokeInfo); |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1373 | return; |
| 1374 | } |
| 1375 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 1376 | // 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] | 1377 | // Scratch textures can be recycled after they are returned to the texture |
| 1378 | // cache. This presents a potential hazard for buffered drawing. However, |
| 1379 | // the writePixels that uploads to the scratch will perform a flush so we're |
| 1380 | // OK. |
bsalomon | f21dab9 | 2014-11-13 13:33:28 -0800 | [diff] [blame] | 1381 | AutoCheckFlush acf(this); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1382 | GrPipelineBuilder pipelineBuilder; |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 1383 | GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf); |
bsalomon | 41ebbdd | 2014-08-04 08:31:39 -0700 | [diff] [blame] | 1384 | if (NULL == target) { |
| 1385 | return; |
| 1386 | } |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 1387 | |
egdaniel | 93a37bc | 2014-07-21 13:47:57 -0700 | [diff] [blame] | 1388 | 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] | 1389 | |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1390 | const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec(); |
| 1391 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1392 | bool useCoverageAA = paint.isAntiAlias() && |
| 1393 | !pipelineBuilder.getRenderTarget()->isMultisampled(); |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 1394 | |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1395 | if (useCoverageAA && strokeRec.getWidth() < 0 && !path.isConvex()) { |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1396 | // 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] | 1397 | SkRect rects[2]; |
| 1398 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1399 | if (is_nested_rects(target, &pipelineBuilder, color, viewMatrix, path, strokeRec, rects)) { |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 1400 | fAARectRenderer->fillAANestedRects(target, &pipelineBuilder, color, viewMatrix, rects); |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1401 | return; |
| 1402 | } |
| 1403 | } |
| 1404 | |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 1405 | SkRect ovalRect; |
| 1406 | bool isOval = path.isOval(&ovalRect); |
| 1407 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1408 | if (!isOval || path.isInverseFillType() || |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 1409 | !fOvalRenderer->drawOval(target, |
| 1410 | &pipelineBuilder, |
| 1411 | color, |
| 1412 | viewMatrix, |
| 1413 | paint.isAntiAlias(), |
| 1414 | ovalRect, |
| 1415 | strokeRec)) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1416 | this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(), |
| 1417 | path, strokeInfo); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 1418 | } |
| 1419 | } |
| 1420 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 1421 | void GrContext::internalDrawPath(GrDrawTarget* target, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1422 | GrPipelineBuilder* pipelineBuilder, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1423 | const SkMatrix& viewMatrix, |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 1424 | GrColor color, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 1425 | bool useAA, |
| 1426 | const SkPath& path, |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1427 | const GrStrokeInfo& strokeInfo) { |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 1428 | RETURN_IF_ABANDONED |
commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 1429 | SkASSERT(!path.isEmpty()); |
commit-bot@chromium.org | 8131283 | 2013-03-22 18:34:09 +0000 | [diff] [blame] | 1430 | |
commit-bot@chromium.org | 2a05de0 | 2014-03-25 15:17:32 +0000 | [diff] [blame] | 1431 | GR_CREATE_TRACE_MARKER("GrContext::internalDrawPath", target); |
| 1432 | |
| 1433 | |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 1434 | // An Assumption here is that path renderer would use some form of tweaking |
| 1435 | // the src color (either the input alpha or in the frag shader) to implement |
| 1436 | // aa. If we have some future driver-mojo path AA that can do the right |
| 1437 | // 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] | 1438 | bool useCoverageAA = useAA && |
egdaniel | 0bdeec9 | 2015-02-23 12:12:54 -0800 | [diff] [blame] | 1439 | !pipelineBuilder->getRenderTarget()->isMultisampled(); |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 1440 | |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 1441 | |
| 1442 | GrPathRendererChain::DrawType type = |
| 1443 | useCoverageAA ? GrPathRendererChain::kColorAntiAlias_DrawType : |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 1444 | GrPathRendererChain::kColor_DrawType; |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 1445 | |
robertphillips@google.com | eb928ea | 2013-01-08 13:45:09 +0000 | [diff] [blame] | 1446 | const SkPath* pathPtr = &path; |
commit-bot@chromium.org | f0c41e2 | 2014-01-14 18:42:34 +0000 | [diff] [blame] | 1447 | SkTLazy<SkPath> tmpPath; |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 1448 | SkTCopyOnFirstWrite<SkStrokeRec> stroke(strokeInfo.getStrokeRec()); |
robertphillips@google.com | eb928ea | 2013-01-08 13:45:09 +0000 | [diff] [blame] | 1449 | |
| 1450 | // Try a 1st time without stroking the path and without allowing the SW renderer |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1451 | GrPathRenderer* pr = this->getPathRenderer(target, pipelineBuilder, viewMatrix, *pathPtr, |
| 1452 | *stroke, false, type); |
robertphillips@google.com | eb928ea | 2013-01-08 13:45:09 +0000 | [diff] [blame] | 1453 | |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 1454 | if (NULL == pr) { |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 1455 | if (!GrPathRenderer::IsStrokeHairlineOrEquivalent(*stroke, viewMatrix, NULL)) { |
robertphillips@google.com | eb928ea | 2013-01-08 13:45:09 +0000 | [diff] [blame] | 1456 | // It didn't work the 1st time, so try again with the stroked path |
egdaniel | f103cd8 | 2015-04-13 13:06:46 -0700 | [diff] [blame] | 1457 | stroke.writable()->setResScale(SkScalarAbs(viewMatrix.getMaxScale())); |
commit-bot@chromium.org | f0c41e2 | 2014-01-14 18:42:34 +0000 | [diff] [blame] | 1458 | if (stroke->applyToPath(tmpPath.init(), *pathPtr)) { |
| 1459 | pathPtr = tmpPath.get(); |
| 1460 | stroke.writable()->setFillStyle(); |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 1461 | if (pathPtr->isEmpty()) { |
| 1462 | return; |
| 1463 | } |
robertphillips@google.com | eb928ea | 2013-01-08 13:45:09 +0000 | [diff] [blame] | 1464 | } |
| 1465 | } |
commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 1466 | |
robertphillips@google.com | eb928ea | 2013-01-08 13:45:09 +0000 | [diff] [blame] | 1467 | // This time, allow SW renderer |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1468 | pr = this->getPathRenderer(target, pipelineBuilder, viewMatrix, *pathPtr, *stroke, true, |
| 1469 | type); |
robertphillips@google.com | eb928ea | 2013-01-08 13:45:09 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 1472 | if (NULL == pr) { |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 1473 | #ifdef SK_DEBUG |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 1474 | SkDebugf("Unable to find path renderer compatible with path.\n"); |
bsalomon@google.com | 1983f39 | 2011-10-10 15:17:58 +0000 | [diff] [blame] | 1475 | #endif |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 1476 | return; |
| 1477 | } |
| 1478 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1479 | pr->drawPath(target, pipelineBuilder, color, viewMatrix, *pathPtr, *stroke, useCoverageAA); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1480 | } |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1481 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1482 | //////////////////////////////////////////////////////////////////////////////// |
| 1483 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 1484 | void GrContext::flush(int flagsBitfield) { |
robertphillips@google.com | e7db8d6 | 2013-07-04 11:48:52 +0000 | [diff] [blame] | 1485 | if (NULL == fDrawBuffer) { |
| 1486 | return; |
| 1487 | } |
| 1488 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 1489 | if (kDiscard_FlushBit & flagsBitfield) { |
| 1490 | fDrawBuffer->reset(); |
| 1491 | } else { |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 1492 | fDrawBuffer->flush(); |
junov@google.com | 53a5584 | 2011-06-08 22:55:10 +0000 | [diff] [blame] | 1493 | } |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 1494 | fResourceCache->notifyFlushOccurred(); |
bsalomon | f21dab9 | 2014-11-13 13:33:28 -0800 | [diff] [blame] | 1495 | fFlushToReduceCacheSize = false; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1496 | } |
| 1497 | |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 1498 | bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes, |
| 1499 | const void* inPixels, size_t outRowBytes, void* outPixels) { |
| 1500 | SkSrcPixelInfo srcPI; |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 1501 | if (!GrPixelConfig2ColorAndProfileType(srcConfig, &srcPI.fColorType, NULL)) { |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1502 | return false; |
| 1503 | } |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 1504 | srcPI.fAlphaType = kUnpremul_SkAlphaType; |
| 1505 | srcPI.fPixels = inPixels; |
| 1506 | srcPI.fRowBytes = inRowBytes; |
| 1507 | |
| 1508 | SkDstPixelInfo dstPI; |
| 1509 | dstPI.fColorType = srcPI.fColorType; |
| 1510 | dstPI.fAlphaType = kPremul_SkAlphaType; |
| 1511 | dstPI.fPixels = outPixels; |
| 1512 | dstPI.fRowBytes = outRowBytes; |
| 1513 | |
| 1514 | return srcPI.convertPixelsTo(&dstPI, width, height); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1515 | } |
| 1516 | |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 1517 | bool GrContext::writeSurfacePixels(GrSurface* surface, |
| 1518 | int left, int top, int width, int height, |
| 1519 | GrPixelConfig srcConfig, const void* buffer, size_t rowBytes, |
| 1520 | uint32_t pixelOpsFlags) { |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 1521 | RETURN_FALSE_IF_ABANDONED |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 1522 | { |
| 1523 | GrTexture* texture = NULL; |
| 1524 | if (!(kUnpremul_PixelOpsFlag & pixelOpsFlags) && (texture = surface->asTexture()) && |
| 1525 | fGpu->canWriteTexturePixels(texture, srcConfig)) { |
| 1526 | |
| 1527 | if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && |
| 1528 | surface->surfacePriv().hasPendingIO()) { |
| 1529 | this->flush(); |
| 1530 | } |
| 1531 | return fGpu->writeTexturePixels(texture, left, top, width, height, |
| 1532 | srcConfig, buffer, rowBytes); |
| 1533 | // Don't need to check kFlushWrites_PixelOp here, we just did a direct write so the |
| 1534 | // upload is already flushed. |
| 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | // If we didn't do a direct texture write then we upload the pixels to a texture and draw. |
| 1539 | GrRenderTarget* renderTarget = surface->asRenderTarget(); |
| 1540 | if (NULL == renderTarget) { |
| 1541 | return false; |
| 1542 | } |
| 1543 | |
| 1544 | // We ignore the preferred config unless it is a R/B swap of the src config. In that case |
| 1545 | // we will upload the original src data to a scratch texture but we will spoof it as the swapped |
| 1546 | // config. This scratch will then have R and B swapped. We correct for this by swapping again |
| 1547 | // when drawing the scratch to the dst using a conversion effect. |
| 1548 | bool swapRAndB = false; |
| 1549 | GrPixelConfig writeConfig = srcConfig; |
| 1550 | if (GrPixelConfigSwapRAndB(srcConfig) == |
| 1551 | fGpu->preferredWritePixelsConfig(srcConfig, renderTarget->config())) { |
| 1552 | writeConfig = GrPixelConfigSwapRAndB(srcConfig); |
| 1553 | swapRAndB = true; |
| 1554 | } |
| 1555 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 1556 | GrSurfaceDesc desc; |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 1557 | desc.fWidth = width; |
| 1558 | desc.fHeight = height; |
| 1559 | desc.fConfig = writeConfig; |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 1560 | SkAutoTUnref<GrTexture> texture(this->refScratchTexture(desc, kApprox_ScratchTexMatch)); |
| 1561 | if (!texture) { |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 1562 | return false; |
| 1563 | } |
| 1564 | |
| 1565 | SkAutoTUnref<const GrFragmentProcessor> fp; |
| 1566 | SkMatrix textureMatrix; |
| 1567 | textureMatrix.setIDiv(texture->width(), texture->height()); |
| 1568 | |
| 1569 | // allocate a tmp buffer and sw convert the pixels to premul |
| 1570 | SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0); |
| 1571 | |
| 1572 | if (kUnpremul_PixelOpsFlag & pixelOpsFlags) { |
| 1573 | if (!GrPixelConfigIs8888(srcConfig)) { |
| 1574 | return false; |
| 1575 | } |
| 1576 | fp.reset(this->createUPMToPMEffect(texture, swapRAndB, textureMatrix)); |
| 1577 | // handle the unpremul step on the CPU if we couldn't create an effect to do it. |
| 1578 | if (NULL == fp) { |
| 1579 | size_t tmpRowBytes = 4 * width; |
| 1580 | tmpPixels.reset(width * height); |
| 1581 | if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes, |
| 1582 | tmpPixels.get())) { |
| 1583 | return false; |
| 1584 | } |
| 1585 | rowBytes = tmpRowBytes; |
| 1586 | buffer = tmpPixels.get(); |
| 1587 | } |
| 1588 | } |
| 1589 | if (NULL == fp) { |
| 1590 | fp.reset(GrConfigConversionEffect::Create(texture, |
| 1591 | swapRAndB, |
| 1592 | GrConfigConversionEffect::kNone_PMConversion, |
| 1593 | textureMatrix)); |
| 1594 | } |
| 1595 | |
| 1596 | // Even if the client told us not to flush, we still flush here. The client may have known that |
| 1597 | // writes to the original surface caused no data hazards, but they can't know that the scratch |
| 1598 | // we just got is safe. |
| 1599 | if (texture->surfacePriv().hasPendingIO()) { |
| 1600 | this->flush(); |
| 1601 | } |
| 1602 | if (!fGpu->writeTexturePixels(texture, 0, 0, width, height, |
| 1603 | writeConfig, buffer, rowBytes)) { |
| 1604 | return false; |
| 1605 | } |
| 1606 | |
| 1607 | SkMatrix matrix; |
| 1608 | matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top)); |
| 1609 | |
| 1610 | // This function can be called in the midst of drawing another object (e.g., when uploading a |
| 1611 | // SW-rasterized clip while issuing a draw). So we push the current geometry state before |
| 1612 | // drawing a rect to the render target. |
| 1613 | // The bracket ensures we pop the stack if we wind up flushing below. |
| 1614 | { |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 1615 | GrDrawTarget* drawTarget = this->prepareToDraw(); |
| 1616 | if (!drawTarget) { |
| 1617 | return false; |
| 1618 | } |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 1619 | GrDrawTarget::AutoGeometryPush agp(drawTarget); |
| 1620 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1621 | GrPipelineBuilder pipelineBuilder; |
| 1622 | pipelineBuilder.addColorProcessor(fp); |
| 1623 | pipelineBuilder.setRenderTarget(renderTarget); |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 1624 | drawTarget->drawSimpleRect(&pipelineBuilder, |
| 1625 | GrColor_WHITE, |
| 1626 | matrix, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1627 | SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height))); |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 1628 | } |
| 1629 | |
| 1630 | if (kFlushWrites_PixelOp & pixelOpsFlags) { |
| 1631 | this->flushSurfaceWrites(surface); |
| 1632 | } |
| 1633 | |
| 1634 | return true; |
| 1635 | } |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 1636 | |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 1637 | // toggles between RGBA and BGRA |
| 1638 | static SkColorType toggle_colortype32(SkColorType ct) { |
| 1639 | if (kRGBA_8888_SkColorType == ct) { |
| 1640 | return kBGRA_8888_SkColorType; |
| 1641 | } else { |
| 1642 | SkASSERT(kBGRA_8888_SkColorType == ct); |
| 1643 | return kRGBA_8888_SkColorType; |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 1644 | } |
| 1645 | } |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1646 | |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 1647 | bool GrContext::readRenderTargetPixels(GrRenderTarget* target, |
| 1648 | int left, int top, int width, int height, |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 1649 | GrPixelConfig dstConfig, void* buffer, size_t rowBytes, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 1650 | uint32_t flags) { |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 1651 | RETURN_FALSE_IF_ABANDONED |
bsalomon@google.com | bc4b654 | 2011-11-19 13:56:11 +0000 | [diff] [blame] | 1652 | ASSERT_OWNED_RESOURCE(target); |
bsalomon | 89c6298 | 2014-11-03 12:08:42 -0800 | [diff] [blame] | 1653 | SkASSERT(target); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1654 | |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 1655 | if (!(kDontFlush_PixelOpsFlag & flags) && target->surfacePriv().hasPendingWrite()) { |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1656 | this->flush(); |
| 1657 | } |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1658 | |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1659 | // 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] | 1660 | |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1661 | // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll |
| 1662 | // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read. |
| 1663 | bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top, |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 1664 | width, height, dstConfig, |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1665 | rowBytes); |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 1666 | // We ignore the preferred config if it is different than our config unless it is an R/B swap. |
| 1667 | // In that case we'll perform an R and B swap while drawing to a scratch texture of the swapped |
| 1668 | // config. Then we will call readPixels on the scratch with the swapped config. The swaps during |
| 1669 | // the draw cancels out the fact that we call readPixels with a config that is R/B swapped from |
| 1670 | // dstConfig. |
| 1671 | GrPixelConfig readConfig = dstConfig; |
| 1672 | bool swapRAndB = false; |
commit-bot@chromium.org | 5d1d79a | 2013-05-24 18:52:52 +0000 | [diff] [blame] | 1673 | if (GrPixelConfigSwapRAndB(dstConfig) == |
| 1674 | fGpu->preferredReadPixelsConfig(dstConfig, target->config())) { |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 1675 | readConfig = GrPixelConfigSwapRAndB(readConfig); |
| 1676 | swapRAndB = true; |
| 1677 | } |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1678 | |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 1679 | bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1680 | |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 1681 | if (unpremul && !GrPixelConfigIs8888(dstConfig)) { |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1682 | // The unpremul flag is only allowed for these two configs. |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1683 | return false; |
| 1684 | } |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1685 | |
bsalomon | 191bcc0 | 2014-11-14 11:31:13 -0800 | [diff] [blame] | 1686 | SkAutoTUnref<GrTexture> tempTexture; |
| 1687 | |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1688 | // If the src is a texture and we would have to do conversions after read pixels, we instead |
| 1689 | // do the conversions by drawing the src to a scratch texture. If we handle any of the |
| 1690 | // conversions in the draw we set the corresponding bool to false so that we don't reapply it |
| 1691 | // on the read back pixels. |
| 1692 | GrTexture* src = target->asTexture(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1693 | if (src && (swapRAndB || unpremul || flipY)) { |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 1694 | // Make the scratch a render so we can read its pixels. |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 1695 | GrSurfaceDesc desc; |
| 1696 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 1697 | desc.fWidth = width; |
| 1698 | desc.fHeight = height; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1699 | desc.fConfig = readConfig; |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 1700 | desc.fOrigin = kTopLeft_GrSurfaceOrigin; |
bsalomon@google.com | c4ff22a | 2011-11-10 21:56:21 +0000 | [diff] [blame] | 1701 | |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 1702 | // 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] | 1703 | // match the passed rect. However, if we see many different size rectangles we will trash |
| 1704 | // our texture cache and pay the cost of creating and destroying many textures. So, we only |
| 1705 | // 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] | 1706 | ScratchTexMatch match = kApprox_ScratchTexMatch; |
| 1707 | if (0 == left && |
| 1708 | 0 == top && |
| 1709 | target->width() == width && |
| 1710 | target->height() == height && |
| 1711 | fGpu->fullReadPixelsIsFasterThanPartial()) { |
| 1712 | match = kExact_ScratchTexMatch; |
| 1713 | } |
bsalomon | 191bcc0 | 2014-11-14 11:31:13 -0800 | [diff] [blame] | 1714 | tempTexture.reset(this->refScratchTexture(desc, match)); |
| 1715 | if (tempTexture) { |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 1716 | // compute a matrix to perform the draw |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 1717 | SkMatrix textureMatrix; |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 1718 | textureMatrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top); |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 1719 | textureMatrix.postIDiv(src->width(), src->height()); |
| 1720 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1721 | SkAutoTUnref<const GrFragmentProcessor> fp; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1722 | if (unpremul) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1723 | fp.reset(this->createPMToUPMEffect(src, swapRAndB, textureMatrix)); |
| 1724 | if (fp) { |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 1725 | 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] | 1726 | } |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1727 | } |
| 1728 | // If we failed to create a PM->UPM effect and have no other conversions to perform then |
| 1729 | // there is no longer any point to using the scratch. |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1730 | if (fp || flipY || swapRAndB) { |
| 1731 | if (!fp) { |
| 1732 | fp.reset(GrConfigConversionEffect::Create( |
| 1733 | src, swapRAndB, GrConfigConversionEffect::kNone_PMConversion, |
| 1734 | textureMatrix)); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1735 | } |
| 1736 | swapRAndB = false; // we will handle the swap in the draw. |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1737 | |
robertphillips@google.com | 13f181f | 2013-03-02 12:02:08 +0000 | [diff] [blame] | 1738 | // We protect the existing geometry here since it may not be |
| 1739 | // clear to the caller that a draw operation (i.e., drawSimpleRect) |
| 1740 | // can be invoked in this method |
joshualitt | 5c55fef | 2014-10-31 14:04:35 -0700 | [diff] [blame] | 1741 | { |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 1742 | GrDrawTarget::AutoGeometryPush agp(fDrawBuffer); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1743 | GrPipelineBuilder pipelineBuilder; |
joshualitt | 5c55fef | 2014-10-31 14:04:35 -0700 | [diff] [blame] | 1744 | SkASSERT(fp); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1745 | pipelineBuilder.addColorProcessor(fp); |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 1746 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1747 | pipelineBuilder.setRenderTarget(tempTexture->asRenderTarget()); |
joshualitt | 5c55fef | 2014-10-31 14:04:35 -0700 | [diff] [blame] | 1748 | SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)); |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 1749 | fDrawBuffer->drawSimpleRect(&pipelineBuilder, |
| 1750 | GrColor_WHITE, |
| 1751 | SkMatrix::I(), |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1752 | rect); |
joshualitt | 5c55fef | 2014-10-31 14:04:35 -0700 | [diff] [blame] | 1753 | // we want to read back from the scratch's origin |
| 1754 | left = 0; |
| 1755 | top = 0; |
bsalomon | 191bcc0 | 2014-11-14 11:31:13 -0800 | [diff] [blame] | 1756 | target = tempTexture->asRenderTarget(); |
joshualitt | 5c55fef | 2014-10-31 14:04:35 -0700 | [diff] [blame] | 1757 | } |
| 1758 | this->flushSurfaceWrites(target); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1759 | } |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 1760 | } |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1761 | } |
joshualitt | 5c55fef | 2014-10-31 14:04:35 -0700 | [diff] [blame] | 1762 | |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1763 | if (!fGpu->readPixels(target, |
| 1764 | left, top, width, height, |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 1765 | readConfig, buffer, rowBytes)) { |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1766 | return false; |
| 1767 | } |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 1768 | // 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] | 1769 | if (unpremul || swapRAndB) { |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 1770 | SkDstPixelInfo dstPI; |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 1771 | if (!GrPixelConfig2ColorAndProfileType(dstConfig, &dstPI.fColorType, NULL)) { |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 1772 | return false; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1773 | } |
reed@google.com | 7111d46 | 2014-03-25 16:20:24 +0000 | [diff] [blame] | 1774 | dstPI.fAlphaType = kUnpremul_SkAlphaType; |
| 1775 | dstPI.fPixels = buffer; |
| 1776 | dstPI.fRowBytes = rowBytes; |
| 1777 | |
| 1778 | SkSrcPixelInfo srcPI; |
| 1779 | srcPI.fColorType = swapRAndB ? toggle_colortype32(dstPI.fColorType) : dstPI.fColorType; |
| 1780 | srcPI.fAlphaType = kPremul_SkAlphaType; |
| 1781 | srcPI.fPixels = buffer; |
| 1782 | srcPI.fRowBytes = rowBytes; |
| 1783 | |
| 1784 | return srcPI.convertPixelsTo(&dstPI, width, height); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1785 | } |
| 1786 | return true; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1787 | } |
| 1788 | |
bsalomon | 87a94eb | 2014-11-03 14:28:32 -0800 | [diff] [blame] | 1789 | void GrContext::prepareSurfaceForExternalRead(GrSurface* surface) { |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 1790 | RETURN_IF_ABANDONED |
bsalomon | 87a94eb | 2014-11-03 14:28:32 -0800 | [diff] [blame] | 1791 | SkASSERT(surface); |
| 1792 | ASSERT_OWNED_RESOURCE(surface); |
| 1793 | if (surface->surfacePriv().hasPendingIO()) { |
| 1794 | this->flush(); |
| 1795 | } |
| 1796 | GrRenderTarget* rt = surface->asRenderTarget(); |
| 1797 | if (fGpu && rt) { |
| 1798 | fGpu->resolveRenderTarget(rt); |
bsalomon | 41ebbdd | 2014-08-04 08:31:39 -0700 | [diff] [blame] | 1799 | } |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 1800 | } |
| 1801 | |
bsalomon | 41ebbdd | 2014-08-04 08:31:39 -0700 | [diff] [blame] | 1802 | void GrContext::discardRenderTarget(GrRenderTarget* renderTarget) { |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 1803 | RETURN_IF_ABANDONED |
bsalomon | 41ebbdd | 2014-08-04 08:31:39 -0700 | [diff] [blame] | 1804 | SkASSERT(renderTarget); |
| 1805 | ASSERT_OWNED_RESOURCE(renderTarget); |
bsalomon | f21dab9 | 2014-11-13 13:33:28 -0800 | [diff] [blame] | 1806 | AutoCheckFlush acf(this); |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 1807 | GrDrawTarget* target = this->prepareToDraw(); |
bsalomon | 41ebbdd | 2014-08-04 08:31:39 -0700 | [diff] [blame] | 1808 | if (NULL == target) { |
| 1809 | return; |
| 1810 | } |
| 1811 | target->discard(renderTarget); |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 1812 | } |
| 1813 | |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 1814 | void GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect, |
| 1815 | const SkIPoint& dstPoint, uint32_t pixelOpsFlags) { |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 1816 | RETURN_IF_ABANDONED |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 1817 | if (NULL == src || NULL == dst) { |
| 1818 | return; |
| 1819 | } |
bsalomon | e3d4bf2 | 2014-09-23 09:15:03 -0700 | [diff] [blame] | 1820 | ASSERT_OWNED_RESOURCE(src); |
junov | 2bb5210 | 2014-09-29 10:18:59 -0700 | [diff] [blame] | 1821 | ASSERT_OWNED_RESOURCE(dst); |
Brian Salomon | 34a9895 | 2014-09-24 11:41:24 -0400 | [diff] [blame] | 1822 | |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 1823 | // Since we're going to the draw target and not GPU, no need to check kNoFlush |
| 1824 | // here. |
junov | 96c118e | 2014-09-26 13:09:47 -0700 | [diff] [blame] | 1825 | |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 1826 | GrDrawTarget* target = this->prepareToDraw(); |
junov | 96c118e | 2014-09-26 13:09:47 -0700 | [diff] [blame] | 1827 | if (NULL == target) { |
| 1828 | return; |
| 1829 | } |
junov | 96c118e | 2014-09-26 13:09:47 -0700 | [diff] [blame] | 1830 | target->copySurface(dst, src, srcRect, dstPoint); |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 1831 | |
| 1832 | if (kFlushWrites_PixelOp & pixelOpsFlags) { |
| 1833 | this->flush(); |
| 1834 | } |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 1835 | } |
| 1836 | |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 1837 | void GrContext::flushSurfaceWrites(GrSurface* surface) { |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 1838 | RETURN_IF_ABANDONED |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 1839 | if (surface->surfacePriv().hasPendingWrite()) { |
| 1840 | this->flush(); |
| 1841 | } |
| 1842 | } |
| 1843 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1844 | GrDrawTarget* GrContext::prepareToDraw(GrPipelineBuilder* pipelineBuilder, |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 1845 | GrRenderTarget* rt, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 1846 | const GrClip& clip, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 1847 | const GrPaint* paint, |
| 1848 | const AutoCheckFlush* acf) { |
joshualitt | 5f5a8d7 | 2015-02-25 14:09:45 -0800 | [diff] [blame] | 1849 | if (NULL == fGpu || NULL == fDrawBuffer) { |
bsalomon | 41ebbdd | 2014-08-04 08:31:39 -0700 | [diff] [blame] | 1850 | return NULL; |
| 1851 | } |
| 1852 | |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 1853 | ASSERT_OWNED_RESOURCE(rt); |
| 1854 | SkASSERT(rt && paint && acf); |
| 1855 | pipelineBuilder->setFromPaint(*paint, rt, clip); |
| 1856 | return fDrawBuffer; |
| 1857 | } |
| 1858 | |
| 1859 | GrDrawTarget* GrContext::prepareToDraw() { |
| 1860 | if (NULL == fGpu) { |
| 1861 | return NULL; |
bsalomon@google.com | 07ea2db | 2012-08-17 14:06:49 +0000 | [diff] [blame] | 1862 | } |
joshualitt | 5c55fef | 2014-10-31 14:04:35 -0700 | [diff] [blame] | 1863 | return fDrawBuffer; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 1866 | /* |
| 1867 | * This method finds a path renderer that can draw the specified path on |
| 1868 | * the provided target. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1869 | * 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] | 1870 | * can be individually allowed/disallowed via the "allowSW" boolean. |
| 1871 | */ |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 1872 | GrPathRenderer* GrContext::getPathRenderer(const GrDrawTarget* target, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1873 | const GrPipelineBuilder* pipelineBuilder, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1874 | const SkMatrix& viewMatrix, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 1875 | const SkPath& path, |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 1876 | const SkStrokeRec& stroke, |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 1877 | bool allowSW, |
| 1878 | GrPathRendererChain::DrawType drawType, |
| 1879 | GrPathRendererChain::StencilSupport* stencilSupport) { |
| 1880 | |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 1881 | if (NULL == fPathRendererChain) { |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 1882 | fPathRendererChain = SkNEW_ARGS(GrPathRendererChain, (this)); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 1883 | } |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 1884 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 1885 | GrPathRenderer* pr = fPathRendererChain->getPathRenderer(target, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 1886 | pipelineBuilder, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1887 | viewMatrix, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 1888 | path, |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 1889 | stroke, |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 1890 | drawType, |
| 1891 | stencilSupport); |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 1892 | |
| 1893 | if (NULL == pr && allowSW) { |
| 1894 | if (NULL == fSoftwarePathRenderer) { |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 1895 | fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this)); |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 1896 | } |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 1897 | pr = fSoftwarePathRenderer; |
| 1898 | } |
| 1899 | |
| 1900 | return pr; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 1901 | } |
| 1902 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1903 | //////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | 6b7938f | 2013-10-15 14:18:16 +0000 | [diff] [blame] | 1904 | bool GrContext::isConfigRenderable(GrPixelConfig config, bool withMSAA) const { |
| 1905 | return fGpu->caps()->isConfigRenderable(config, withMSAA); |
robertphillips@google.com | 99a5ac0 | 2012-04-10 19:26:38 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
commit-bot@chromium.org | b471a32 | 2014-03-10 07:40:03 +0000 | [diff] [blame] | 1908 | int GrContext::getRecommendedSampleCount(GrPixelConfig config, |
| 1909 | SkScalar dpi) const { |
| 1910 | if (!this->isConfigRenderable(config, true)) { |
| 1911 | return 0; |
| 1912 | } |
| 1913 | int chosenSampleCount = 0; |
| 1914 | if (fGpu->caps()->pathRenderingSupport()) { |
| 1915 | if (dpi >= 250.0f) { |
| 1916 | chosenSampleCount = 4; |
| 1917 | } else { |
| 1918 | chosenSampleCount = 16; |
| 1919 | } |
| 1920 | } |
| 1921 | return chosenSampleCount <= fGpu->caps()->maxSampleCount() ? |
| 1922 | chosenSampleCount : 0; |
| 1923 | } |
| 1924 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 1925 | void GrContext::setupDrawBuffer() { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 1926 | SkASSERT(NULL == fDrawBuffer); |
| 1927 | SkASSERT(NULL == fDrawBufferVBAllocPool); |
| 1928 | SkASSERT(NULL == fDrawBufferIBAllocPool); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 1929 | |
bsalomon@google.com | de6ac2d | 2011-02-25 21:50:42 +0000 | [diff] [blame] | 1930 | fDrawBufferVBAllocPool = |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 1931 | SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false, |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1932 | DRAW_BUFFER_VBPOOL_BUFFER_SIZE, |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 1933 | DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS)); |
bsalomon@google.com | de6ac2d | 2011-02-25 21:50:42 +0000 | [diff] [blame] | 1934 | fDrawBufferIBAllocPool = |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 1935 | SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false, |
bsalomon@google.com | de6ac2d | 2011-02-25 21:50:42 +0000 | [diff] [blame] | 1936 | DRAW_BUFFER_IBPOOL_BUFFER_SIZE, |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 1937 | DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS)); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1938 | |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 1939 | fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu, |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 1940 | fDrawBufferVBAllocPool, |
| 1941 | fDrawBufferIBAllocPool)); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1942 | } |
| 1943 | |
bsalomon@google.com | 21c10c5 | 2013-06-13 17:44:07 +0000 | [diff] [blame] | 1944 | GrDrawTarget* GrContext::getTextTarget() { |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 1945 | return this->prepareToDraw(); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1946 | } |
| 1947 | |
| 1948 | const GrIndexBuffer* GrContext::getQuadIndexBuffer() const { |
| 1949 | return fGpu->getQuadIndexBuffer(); |
| 1950 | } |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 1951 | |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1952 | namespace { |
| 1953 | void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) { |
| 1954 | GrConfigConversionEffect::PMConversion pmToUPM; |
| 1955 | GrConfigConversionEffect::PMConversion upmToPM; |
| 1956 | GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM); |
| 1957 | *pmToUPMValue = pmToUPM; |
| 1958 | *upmToPMValue = upmToPM; |
| 1959 | } |
| 1960 | } |
| 1961 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1962 | const GrFragmentProcessor* GrContext::createPMToUPMEffect(GrTexture* texture, |
| 1963 | bool swapRAndB, |
| 1964 | const SkMatrix& matrix) { |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1965 | if (!fDidTestPMConversions) { |
| 1966 | test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion); |
bsalomon@google.com | d0f3f68 | 2012-08-28 13:08:14 +0000 | [diff] [blame] | 1967 | fDidTestPMConversions = true; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1968 | } |
| 1969 | GrConfigConversionEffect::PMConversion pmToUPM = |
| 1970 | static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion); |
| 1971 | if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) { |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 1972 | return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, matrix); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1973 | } else { |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 1974 | return NULL; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1975 | } |
| 1976 | } |
| 1977 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1978 | const GrFragmentProcessor* GrContext::createUPMToPMEffect(GrTexture* texture, |
| 1979 | bool swapRAndB, |
| 1980 | const SkMatrix& matrix) { |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1981 | if (!fDidTestPMConversions) { |
| 1982 | test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion); |
bsalomon@google.com | d0f3f68 | 2012-08-28 13:08:14 +0000 | [diff] [blame] | 1983 | fDidTestPMConversions = true; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1984 | } |
| 1985 | GrConfigConversionEffect::PMConversion upmToPM = |
| 1986 | static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion); |
| 1987 | if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) { |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 1988 | return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, matrix); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1989 | } else { |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 1990 | return NULL; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1991 | } |
| 1992 | } |
| 1993 | |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 1994 | ////////////////////////////////////////////////////////////////////////////// |
| 1995 | |
| 1996 | void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const { |
| 1997 | if (maxTextures) { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 1998 | *maxTextures = fResourceCache->getMaxResourceCount(); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 1999 | } |
| 2000 | if (maxTextureBytes) { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 2001 | *maxTextureBytes = fResourceCache->getMaxResourceBytes(); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 2002 | } |
| 2003 | } |
| 2004 | |
| 2005 | void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 2006 | fResourceCache->setLimits(maxTextures, maxTextureBytes); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 2007 | } |
| 2008 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 2009 | void GrContext::addResourceToCache(const GrUniqueKey& key, GrGpuResource* resource) { |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 2010 | ASSERT_OWNED_RESOURCE(resource); |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 2011 | if (!resource) { |
| 2012 | return; |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 2013 | } |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 2014 | resource->resourcePriv().setUniqueKey(key); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 2015 | } |
| 2016 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 2017 | bool GrContext::isResourceInCache(const GrUniqueKey& key) const { |
| 2018 | return fResourceCache->hasUniqueKey(key); |
commit-bot@chromium.org | 95a2b0e | 2014-05-05 19:21:16 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 2021 | GrGpuResource* GrContext::findAndRefCachedResource(const GrUniqueKey& key) { |
| 2022 | return fResourceCache->findAndRefUniqueResource(key); |
commit-bot@chromium.org | 95a2b0e | 2014-05-05 19:21:16 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 2025 | ////////////////////////////////////////////////////////////////////////////// |
| 2026 | |
egdaniel | bbcb38d | 2014-06-19 10:19:29 -0700 | [diff] [blame] | 2027 | void GrContext::addGpuTraceMarker(const GrGpuTraceMarker* marker) { |
| 2028 | fGpu->addGpuTraceMarker(marker); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 2029 | if (fDrawBuffer) { |
egdaniel | bbcb38d | 2014-06-19 10:19:29 -0700 | [diff] [blame] | 2030 | fDrawBuffer->addGpuTraceMarker(marker); |
| 2031 | } |
| 2032 | } |
| 2033 | |
| 2034 | void GrContext::removeGpuTraceMarker(const GrGpuTraceMarker* marker) { |
| 2035 | fGpu->removeGpuTraceMarker(marker); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 2036 | if (fDrawBuffer) { |
egdaniel | bbcb38d | 2014-06-19 10:19:29 -0700 | [diff] [blame] | 2037 | fDrawBuffer->removeGpuTraceMarker(marker); |
| 2038 | } |
| 2039 | } |
| 2040 | |