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