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