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