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