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