robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2015 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. |
| 7 | */ |
| 8 | |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 9 | #include "GrAtlasTextContext.h" |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 10 | #include "GrBatchTest.h" |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 11 | #include "GrColor.h" |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 12 | #include "GrDrawContext.h" |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 13 | #include "GrDrawingManager.h" |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 14 | #include "GrOvalRenderer.h" |
| 15 | #include "GrPathRenderer.h" |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 16 | #include "GrRenderTarget.h" |
| 17 | #include "GrRenderTargetPriv.h" |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 18 | #include "GrResourceProvider.h" |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 19 | #include "GrStencilAndCoverTextContext.h" |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 20 | #include "SkSurfacePriv.h" |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 21 | |
joshualitt | 7441782 | 2015-08-07 11:42:16 -0700 | [diff] [blame] | 22 | #include "batches/GrBatch.h" |
jvanverth | 14b8803 | 2015-08-07 12:18:54 -0700 | [diff] [blame] | 23 | #include "batches/GrDrawAtlasBatch.h" |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 24 | #include "batches/GrDrawVerticesBatch.h" |
joshualitt | 7fc2a26 | 2015-08-10 10:30:14 -0700 | [diff] [blame] | 25 | #include "batches/GrRectBatchFactory.h" |
joshualitt | 7441782 | 2015-08-07 11:42:16 -0700 | [diff] [blame] | 26 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 27 | #define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == fDrawingManager->getContext()) |
| 28 | #define RETURN_IF_ABANDONED if (fDrawingManager->abandoned()) { return; } |
| 29 | #define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->abandoned()) { return false; } |
| 30 | #define RETURN_NULL_IF_ABANDONED if (fDrawingManager->abandoned()) { return nullptr; } |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 31 | |
| 32 | class AutoCheckFlush { |
| 33 | public: |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 34 | AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) { |
| 35 | SkASSERT(fDrawingManager); |
| 36 | } |
| 37 | ~AutoCheckFlush() { fDrawingManager->getContext()->flushIfNecessary(); } |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 38 | |
| 39 | private: |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 40 | GrDrawingManager* fDrawingManager; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 41 | }; |
| 42 | |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 43 | // In MDB mode the reffing of the 'getLastDrawTarget' call's result allows in-progress |
| 44 | // drawTargets to be picked up and added to by drawContexts lower in the call |
| 45 | // stack. When this occurs with a closed drawTarget, a new one will be allocated |
| 46 | // when the drawContext attempts to use it (via getDrawTarget). |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 47 | GrDrawContext::GrDrawContext(GrDrawingManager* drawingMgr, |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 48 | GrRenderTarget* rt, |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 49 | const SkSurfaceProps* surfaceProps) |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 50 | : fDrawingManager(drawingMgr) |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 51 | , fRenderTarget(rt) |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 52 | , fDrawTarget(SkSafeRef(rt->getLastDrawTarget())) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 53 | , fTextContext(nullptr) |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 54 | , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps)) { |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 55 | SkDEBUGCODE(this->validate();) |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 56 | } |
| 57 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 58 | #ifdef SK_DEBUG |
| 59 | void GrDrawContext::validate() const { |
| 60 | SkASSERT(fRenderTarget); |
| 61 | ASSERT_OWNED_RESOURCE(fRenderTarget); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 62 | |
| 63 | if (fDrawTarget && !fDrawTarget->isClosed()) { |
| 64 | SkASSERT(fRenderTarget->getLastDrawTarget() == fDrawTarget); |
| 65 | } |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 66 | } |
| 67 | #endif |
| 68 | |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 69 | GrDrawContext::~GrDrawContext() { |
| 70 | SkSafeUnref(fDrawTarget); |
| 71 | } |
| 72 | |
| 73 | GrDrawTarget* GrDrawContext::getDrawTarget() { |
| 74 | SkDEBUGCODE(this->validate();) |
| 75 | |
| 76 | if (!fDrawTarget || fDrawTarget->isClosed()) { |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 77 | fDrawTarget = fDrawingManager->newDrawTarget(fRenderTarget); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 78 | fRenderTarget->setLastDrawTarget(fDrawTarget); |
| 79 | } |
| 80 | |
| 81 | return fDrawTarget; |
| 82 | } |
| 83 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 84 | void GrDrawContext::copySurface(GrSurface* src, const SkIRect& srcRect, const SkIPoint& dstPoint) { |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 85 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 86 | SkDEBUGCODE(this->validate();) |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 87 | |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 88 | this->getDrawTarget()->copySurface(fRenderTarget, src, srcRect, dstPoint); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 89 | } |
| 90 | |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 91 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 92 | void GrDrawContext::drawText(const GrClip& clip, const GrPaint& grPaint, |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 93 | const SkPaint& skPaint, |
| 94 | const SkMatrix& viewMatrix, |
| 95 | const char text[], size_t byteLength, |
| 96 | SkScalar x, SkScalar y, const SkIRect& clipBounds) { |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 97 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 98 | SkDEBUGCODE(this->validate();) |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 99 | |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 100 | if (!fTextContext) { |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 101 | fTextContext = fDrawingManager->textContext(fSurfaceProps, fRenderTarget); |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 102 | } |
| 103 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 104 | fTextContext->drawText(this, fRenderTarget, clip, grPaint, skPaint, viewMatrix, |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 105 | text, byteLength, x, y, clipBounds); |
| 106 | |
| 107 | } |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 108 | void GrDrawContext::drawPosText(const GrClip& clip, const GrPaint& grPaint, |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 109 | const SkPaint& skPaint, |
| 110 | const SkMatrix& viewMatrix, |
| 111 | const char text[], size_t byteLength, |
| 112 | const SkScalar pos[], int scalarsPerPosition, |
| 113 | const SkPoint& offset, const SkIRect& clipBounds) { |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 114 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 115 | SkDEBUGCODE(this->validate();) |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 116 | |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 117 | if (!fTextContext) { |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 118 | fTextContext = fDrawingManager->textContext(fSurfaceProps, fRenderTarget); |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 119 | } |
| 120 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 121 | fTextContext->drawPosText(this, fRenderTarget, clip, grPaint, skPaint, viewMatrix, text, byteLength, |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 122 | pos, scalarsPerPosition, offset, clipBounds); |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 123 | |
| 124 | } |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 125 | void GrDrawContext::drawTextBlob(const GrClip& clip, const SkPaint& skPaint, |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 126 | const SkMatrix& viewMatrix, const SkTextBlob* blob, |
| 127 | SkScalar x, SkScalar y, |
| 128 | SkDrawFilter* filter, const SkIRect& clipBounds) { |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 129 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 130 | SkDEBUGCODE(this->validate();) |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 131 | |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 132 | if (!fTextContext) { |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 133 | fTextContext = fDrawingManager->textContext(fSurfaceProps, fRenderTarget); |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 134 | } |
| 135 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 136 | fTextContext->drawTextBlob(this, fRenderTarget, |
robertphillips | f6703fa | 2015-09-01 05:36:47 -0700 | [diff] [blame] | 137 | clip, skPaint, viewMatrix, blob, x, y, filter, clipBounds); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 138 | } |
| 139 | |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 140 | void GrDrawContext::drawPathsFromRange(const GrPipelineBuilder* pipelineBuilder, |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame] | 141 | const SkMatrix& viewMatrix, |
| 142 | const SkMatrix& localMatrix, |
| 143 | GrColor color, |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 144 | GrPathRange* range, |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 145 | GrPathRangeDraw* draw, |
| 146 | int /*GrPathRendering::FillType*/ fill) { |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 147 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 148 | SkDEBUGCODE(this->validate();) |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 149 | |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 150 | this->getDrawTarget()->drawPathsFromRange(*pipelineBuilder, viewMatrix, localMatrix, color, |
| 151 | range, draw, (GrPathRendering::FillType) fill); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 152 | } |
| 153 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 154 | void GrDrawContext::discard() { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 155 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 156 | SkDEBUGCODE(this->validate();) |
| 157 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 158 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 159 | this->getDrawTarget()->discard(fRenderTarget); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 160 | } |
| 161 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 162 | void GrDrawContext::clear(const SkIRect* rect, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 163 | const GrColor color, |
| 164 | bool canIgnoreRect) { |
| 165 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 166 | SkDEBUGCODE(this->validate();) |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 167 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 168 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 169 | this->getDrawTarget()->clear(rect, color, canIgnoreRect, fRenderTarget); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 173 | void GrDrawContext::drawPaint(const GrClip& clip, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 174 | const GrPaint& origPaint, |
| 175 | const SkMatrix& viewMatrix) { |
| 176 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 177 | SkDEBUGCODE(this->validate();) |
| 178 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 179 | // set rect to be big enough to fill the space, but not super-huge, so we |
| 180 | // don't overflow fixed-point implementations |
| 181 | SkRect r; |
| 182 | r.setLTRB(0, 0, |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 183 | SkIntToScalar(fRenderTarget->width()), |
| 184 | SkIntToScalar(fRenderTarget->height())); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 185 | SkTCopyOnFirstWrite<GrPaint> paint(origPaint); |
| 186 | |
| 187 | // by definition this fills the entire clip, no need for AA |
| 188 | if (paint->isAntiAlias()) { |
| 189 | paint.writable()->setAntiAlias(false); |
| 190 | } |
| 191 | |
| 192 | bool isPerspective = viewMatrix.hasPerspective(); |
| 193 | |
| 194 | // We attempt to map r by the inverse matrix and draw that. mapRect will |
| 195 | // map the four corners and bound them with a new rect. This will not |
| 196 | // produce a correct result for some perspective matrices. |
| 197 | if (!isPerspective) { |
| 198 | SkMatrix inverse; |
| 199 | if (!viewMatrix.invert(&inverse)) { |
| 200 | SkDebugf("Could not invert matrix\n"); |
| 201 | return; |
| 202 | } |
| 203 | inverse.mapRect(&r); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 204 | this->drawRect(clip, *paint, viewMatrix, r); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 205 | } else { |
| 206 | SkMatrix localMatrix; |
| 207 | if (!viewMatrix.invert(&localMatrix)) { |
| 208 | SkDebugf("Could not invert matrix\n"); |
| 209 | return; |
| 210 | } |
| 211 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 212 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 213 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 214 | GrPipelineBuilder pipelineBuilder(*paint, fRenderTarget, clip); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 215 | this->getDrawTarget()->drawNonAARect(pipelineBuilder, |
| 216 | paint->getColor(), |
| 217 | SkMatrix::I(), |
| 218 | r, |
| 219 | localMatrix); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 223 | static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) { |
| 224 | return point.fX >= rect.fLeft && point.fX <= rect.fRight && |
| 225 | point.fY >= rect.fTop && point.fY <= rect.fBottom; |
| 226 | } |
| 227 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 228 | void GrDrawContext::drawRect(const GrClip& clip, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 229 | const GrPaint& paint, |
| 230 | const SkMatrix& viewMatrix, |
| 231 | const SkRect& rect, |
| 232 | const GrStrokeInfo* strokeInfo) { |
| 233 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 234 | SkDEBUGCODE(this->validate();) |
| 235 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 236 | if (strokeInfo && strokeInfo->isDashed()) { |
| 237 | SkPath path; |
| 238 | path.setIsVolatile(true); |
| 239 | path.addRect(rect); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 240 | this->drawPath(clip, paint, viewMatrix, path, *strokeInfo); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 241 | return; |
| 242 | } |
| 243 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 244 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 245 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 246 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
joshualitt | 7b670db | 2015-07-09 13:25:02 -0700 | [diff] [blame] | 247 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 248 | SkScalar width = nullptr == strokeInfo ? -1 : strokeInfo->getWidth(); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 249 | |
| 250 | // Check if this is a full RT draw and can be replaced with a clear. We don't bother checking |
| 251 | // cases where the RT is fully inside a stroke. |
| 252 | if (width < 0) { |
| 253 | SkRect rtRect; |
| 254 | pipelineBuilder.getRenderTarget()->getBoundsRect(&rtRect); |
| 255 | SkRect clipSpaceRTRect = rtRect; |
| 256 | bool checkClip = GrClip::kWideOpen_ClipType != clip.clipType(); |
| 257 | if (checkClip) { |
| 258 | clipSpaceRTRect.offset(SkIntToScalar(clip.origin().fX), |
| 259 | SkIntToScalar(clip.origin().fY)); |
| 260 | } |
| 261 | // Does the clip contain the entire RT? |
| 262 | if (!checkClip || clip.quickContains(clipSpaceRTRect)) { |
| 263 | SkMatrix invM; |
| 264 | if (!viewMatrix.invert(&invM)) { |
| 265 | return; |
| 266 | } |
| 267 | // Does the rect bound the RT? |
| 268 | SkPoint srcSpaceRTQuad[4]; |
| 269 | invM.mapRectToQuad(srcSpaceRTQuad, rtRect); |
| 270 | if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) && |
| 271 | rect_contains_inclusive(rect, srcSpaceRTQuad[1]) && |
| 272 | rect_contains_inclusive(rect, srcSpaceRTQuad[2]) && |
| 273 | rect_contains_inclusive(rect, srcSpaceRTQuad[3])) { |
| 274 | // Will it blend? |
| 275 | GrColor clearColor; |
cdalton | 1fa4572 | 2015-06-02 10:43:39 -0700 | [diff] [blame] | 276 | if (paint.isConstantBlendedColor(&clearColor)) { |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 277 | this->getDrawTarget()->clear(nullptr, clearColor, true, fRenderTarget); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 278 | return; |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | GrColor color = paint.getColor(); |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 285 | bool needAA = paint.isAntiAlias() && |
| 286 | !pipelineBuilder.getRenderTarget()->isUnifiedMultisampled(); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 287 | |
joshualitt | 9be0b00 | 2015-08-19 11:50:51 -0700 | [diff] [blame] | 288 | // The fill path can handle rotation but not skew |
| 289 | // The stroke path needs the rect to remain axis aligned (no rotation or skew) |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 290 | // None of our AA draw rect calls can handle perspective yet |
joshualitt | 9be0b00 | 2015-08-19 11:50:51 -0700 | [diff] [blame] | 291 | bool canApplyAA = width >=0 ? viewMatrix.rectStaysRect() : viewMatrix.preservesRightAngles(); |
| 292 | |
| 293 | if (needAA && canApplyAA) { |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 294 | SkASSERT(!viewMatrix.hasPerspective()); |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 295 | SkAutoTUnref<GrDrawBatch> batch; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 296 | if (width >= 0) { |
joshualitt | 10cae83 | 2015-09-22 12:50:33 -0700 | [diff] [blame] | 297 | batch.reset(GrRectBatchFactory::CreateAAStroke(color, viewMatrix, rect, *strokeInfo)); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 298 | } else { |
joshualitt | 10cae83 | 2015-09-22 12:50:33 -0700 | [diff] [blame] | 299 | SkRect devBoundRect; |
| 300 | viewMatrix.mapRect(&devBoundRect, rect); |
joshualitt | d2b23e0 | 2015-08-21 10:53:34 -0700 | [diff] [blame] | 301 | batch.reset(GrRectBatchFactory::CreateAAFill(color, viewMatrix, rect, devBoundRect)); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 302 | } |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 303 | this->getDrawTarget()->drawBatch(pipelineBuilder, batch); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 304 | return; |
| 305 | } |
| 306 | |
| 307 | if (width >= 0) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 308 | // Non-AA hairlines are snapped to pixel centers to make which pixels are hit deterministic |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 309 | bool snapToPixelCenters = (0 == width && !fRenderTarget->isUnifiedMultisampled()); |
joshualitt | d2b23e0 | 2015-08-21 10:53:34 -0700 | [diff] [blame] | 310 | SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAStroke( |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 311 | color, viewMatrix, rect, width, snapToPixelCenters)); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 312 | |
| 313 | // Depending on sub-pixel coordinates and the particular GPU, we may lose a corner of |
| 314 | // hairline rects. We jam all the vertices to pixel centers to avoid this, but not when MSAA |
| 315 | // is enabled because it can cause ugly artifacts. |
| 316 | pipelineBuilder.setState(GrPipelineBuilder::kSnapVerticesToPixelCenters_Flag, |
| 317 | snapToPixelCenters); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 318 | this->getDrawTarget()->drawBatch(pipelineBuilder, batch); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 319 | } else { |
| 320 | // filled BW rect |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 321 | this->getDrawTarget()->drawNonAARect(pipelineBuilder, color, viewMatrix, rect); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 325 | void GrDrawContext::drawNonAARectToRect(const GrClip& clip, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 326 | const GrPaint& paint, |
| 327 | const SkMatrix& viewMatrix, |
| 328 | const SkRect& rectToDraw, |
joshualitt | b6b513b | 2015-08-21 10:25:18 -0700 | [diff] [blame] | 329 | const SkRect& localRect) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 330 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 331 | SkDEBUGCODE(this->validate();) |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 332 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 333 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 334 | |
| 335 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 336 | this->getDrawTarget()->drawNonAARect(pipelineBuilder, |
| 337 | paint.getColor(), |
| 338 | viewMatrix, |
| 339 | rectToDraw, |
| 340 | localRect); |
joshualitt | b6b513b | 2015-08-21 10:25:18 -0700 | [diff] [blame] | 341 | } |
| 342 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 343 | void GrDrawContext::drawNonAARectWithLocalMatrix(const GrClip& clip, |
joshualitt | b6b513b | 2015-08-21 10:25:18 -0700 | [diff] [blame] | 344 | const GrPaint& paint, |
| 345 | const SkMatrix& viewMatrix, |
| 346 | const SkRect& rectToDraw, |
| 347 | const SkMatrix& localMatrix) { |
| 348 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 349 | SkDEBUGCODE(this->validate();) |
joshualitt | b6b513b | 2015-08-21 10:25:18 -0700 | [diff] [blame] | 350 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 351 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 352 | |
| 353 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 354 | this->getDrawTarget()->drawNonAARect(pipelineBuilder, |
| 355 | paint.getColor(), |
| 356 | viewMatrix, |
| 357 | rectToDraw, |
| 358 | localMatrix); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 359 | } |
| 360 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 361 | void GrDrawContext::drawVertices(const GrClip& clip, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 362 | const GrPaint& paint, |
| 363 | const SkMatrix& viewMatrix, |
| 364 | GrPrimitiveType primitiveType, |
| 365 | int vertexCount, |
| 366 | const SkPoint positions[], |
| 367 | const SkPoint texCoords[], |
| 368 | const GrColor colors[], |
| 369 | const uint16_t indices[], |
| 370 | int indexCount) { |
| 371 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 372 | SkDEBUGCODE(this->validate();) |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 373 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 374 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 375 | |
| 376 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
joshualitt | 7b670db | 2015-07-09 13:25:02 -0700 | [diff] [blame] | 377 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 378 | // TODO clients should give us bounds |
| 379 | SkRect bounds; |
| 380 | if (!bounds.setBoundsCheck(positions, vertexCount)) { |
| 381 | SkDebugf("drawVertices call empty bounds\n"); |
| 382 | return; |
| 383 | } |
| 384 | |
| 385 | viewMatrix.mapRect(&bounds); |
| 386 | |
| 387 | // If we don't have AA then we outset for a half pixel in each direction to account for |
| 388 | // snapping |
| 389 | if (!paint.isAntiAlias()) { |
| 390 | bounds.outset(0.5f, 0.5f); |
| 391 | } |
| 392 | |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 393 | GrDrawVerticesBatch::Geometry geometry; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 394 | geometry.fColor = paint.getColor(); |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 395 | SkAutoTUnref<GrDrawBatch> batch(GrDrawVerticesBatch::Create(geometry, primitiveType, viewMatrix, |
| 396 | positions, vertexCount, indices, |
| 397 | indexCount, colors, texCoords, |
| 398 | bounds)); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 399 | |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 400 | this->getDrawTarget()->drawBatch(pipelineBuilder, batch); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | /////////////////////////////////////////////////////////////////////////////// |
| 404 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 405 | void GrDrawContext::drawAtlas(const GrClip& clip, |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 406 | const GrPaint& paint, |
| 407 | const SkMatrix& viewMatrix, |
| 408 | int spriteCount, |
| 409 | const SkRSXform xform[], |
| 410 | const SkRect texRect[], |
| 411 | const SkColor colors[]) { |
| 412 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 413 | SkDEBUGCODE(this->validate();) |
| 414 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 415 | AutoCheckFlush acf(fDrawingManager); |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 416 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 417 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 418 | |
jvanverth | 14b8803 | 2015-08-07 12:18:54 -0700 | [diff] [blame] | 419 | GrDrawAtlasBatch::Geometry geometry; |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 420 | geometry.fColor = paint.getColor(); |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 421 | SkAutoTUnref<GrDrawBatch> batch(GrDrawAtlasBatch::Create(geometry, viewMatrix, spriteCount, |
| 422 | xform, texRect, colors)); |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 423 | |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 424 | this->getDrawTarget()->drawBatch(pipelineBuilder, batch); |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | /////////////////////////////////////////////////////////////////////////////// |
| 428 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 429 | void GrDrawContext::drawRRect(const GrClip& clip, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 430 | const GrPaint& paint, |
| 431 | const SkMatrix& viewMatrix, |
| 432 | const SkRRect& rrect, |
| 433 | const GrStrokeInfo& strokeInfo) { |
| 434 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 435 | SkDEBUGCODE(this->validate();) |
| 436 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 437 | if (rrect.isEmpty()) { |
| 438 | return; |
| 439 | } |
| 440 | |
| 441 | if (strokeInfo.isDashed()) { |
| 442 | SkPath path; |
| 443 | path.setIsVolatile(true); |
| 444 | path.addRRect(rrect); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 445 | this->drawPath(clip, paint, viewMatrix, path, strokeInfo); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 446 | return; |
| 447 | } |
| 448 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 449 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 450 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 451 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 452 | GrColor color = paint.getColor(); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 453 | |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 454 | if (!GrOvalRenderer::DrawRRect(this->getDrawTarget(), |
joshualitt | ae3d63a | 2015-07-13 08:44:06 -0700 | [diff] [blame] | 455 | pipelineBuilder, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 456 | color, |
| 457 | viewMatrix, |
| 458 | paint.isAntiAlias(), |
| 459 | rrect, |
| 460 | strokeInfo)) { |
| 461 | SkPath path; |
| 462 | path.setIsVolatile(true); |
| 463 | path.addRRect(rrect); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 464 | this->internalDrawPath(this->getDrawTarget(), &pipelineBuilder, viewMatrix, color, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 465 | paint.isAntiAlias(), path, strokeInfo); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | /////////////////////////////////////////////////////////////////////////////// |
| 470 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 471 | void GrDrawContext::drawDRRect(const GrClip& clip, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 472 | const GrPaint& paint, |
| 473 | const SkMatrix& viewMatrix, |
| 474 | const SkRRect& outer, |
| 475 | const SkRRect& inner) { |
| 476 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 477 | SkDEBUGCODE(this->validate();) |
| 478 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 479 | if (outer.isEmpty()) { |
| 480 | return; |
| 481 | } |
| 482 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 483 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 484 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 485 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 486 | GrColor color = paint.getColor(); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 487 | if (!GrOvalRenderer::DrawDRRect(this->getDrawTarget(), |
joshualitt | ae3d63a | 2015-07-13 08:44:06 -0700 | [diff] [blame] | 488 | pipelineBuilder, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 489 | color, |
| 490 | viewMatrix, |
| 491 | paint.isAntiAlias(), |
| 492 | outer, |
| 493 | inner)) { |
| 494 | SkPath path; |
| 495 | path.setIsVolatile(true); |
| 496 | path.addRRect(inner); |
| 497 | path.addRRect(outer); |
| 498 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 499 | |
| 500 | GrStrokeInfo fillRec(SkStrokeRec::kFill_InitStyle); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 501 | this->internalDrawPath(this->getDrawTarget(), &pipelineBuilder, viewMatrix, color, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 502 | paint.isAntiAlias(), path, fillRec); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | /////////////////////////////////////////////////////////////////////////////// |
| 507 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 508 | void GrDrawContext::drawOval(const GrClip& clip, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 509 | const GrPaint& paint, |
| 510 | const SkMatrix& viewMatrix, |
| 511 | const SkRect& oval, |
| 512 | const GrStrokeInfo& strokeInfo) { |
| 513 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 514 | SkDEBUGCODE(this->validate();) |
| 515 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 516 | if (oval.isEmpty()) { |
| 517 | return; |
| 518 | } |
| 519 | |
| 520 | if (strokeInfo.isDashed()) { |
| 521 | SkPath path; |
| 522 | path.setIsVolatile(true); |
| 523 | path.addOval(oval); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 524 | this->drawPath(clip, paint, viewMatrix, path, strokeInfo); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 525 | return; |
| 526 | } |
| 527 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 528 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 529 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 530 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 531 | GrColor color = paint.getColor(); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 532 | |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 533 | if (!GrOvalRenderer::DrawOval(this->getDrawTarget(), |
joshualitt | ae3d63a | 2015-07-13 08:44:06 -0700 | [diff] [blame] | 534 | pipelineBuilder, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 535 | color, |
| 536 | viewMatrix, |
| 537 | paint.isAntiAlias(), |
| 538 | oval, |
| 539 | strokeInfo)) { |
| 540 | SkPath path; |
| 541 | path.setIsVolatile(true); |
| 542 | path.addOval(oval); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 543 | this->internalDrawPath(this->getDrawTarget(), &pipelineBuilder, viewMatrix, color, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 544 | paint.isAntiAlias(), path, strokeInfo); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | // Can 'path' be drawn as a pair of filled nested rectangles? |
joshualitt | f9c5db2 | 2015-07-10 11:31:01 -0700 | [diff] [blame] | 549 | static bool is_nested_rects(const SkMatrix& viewMatrix, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 550 | const SkPath& path, |
| 551 | const SkStrokeRec& stroke, |
| 552 | SkRect rects[2]) { |
| 553 | SkASSERT(stroke.isFillStyle()); |
| 554 | |
| 555 | if (path.isInverseFillType()) { |
| 556 | return false; |
| 557 | } |
| 558 | |
| 559 | // TODO: this restriction could be lifted if we were willing to apply |
| 560 | // the matrix to all the points individually rather than just to the rect |
| 561 | if (!viewMatrix.preservesAxisAlignment()) { |
| 562 | return false; |
| 563 | } |
| 564 | |
| 565 | SkPath::Direction dirs[2]; |
| 566 | if (!path.isNestedFillRects(rects, dirs)) { |
| 567 | return false; |
| 568 | } |
| 569 | |
| 570 | if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) { |
| 571 | // The two rects need to be wound opposite to each other |
| 572 | return false; |
| 573 | } |
| 574 | |
| 575 | // Right now, nested rects where the margin is not the same width |
| 576 | // all around do not render correctly |
| 577 | const SkScalar* outer = rects[0].asScalars(); |
| 578 | const SkScalar* inner = rects[1].asScalars(); |
| 579 | |
| 580 | bool allEq = true; |
| 581 | |
| 582 | SkScalar margin = SkScalarAbs(outer[0] - inner[0]); |
| 583 | bool allGoE1 = margin >= SK_Scalar1; |
| 584 | |
| 585 | for (int i = 1; i < 4; ++i) { |
| 586 | SkScalar temp = SkScalarAbs(outer[i] - inner[i]); |
| 587 | if (temp < SK_Scalar1) { |
| 588 | allGoE1 = false; |
| 589 | } |
| 590 | if (!SkScalarNearlyEqual(margin, temp)) { |
| 591 | allEq = false; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | return allEq || allGoE1; |
| 596 | } |
| 597 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 598 | void GrDrawContext::drawBatch(const GrClip& clip, |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 599 | const GrPaint& paint, GrDrawBatch* batch) { |
joshualitt | b7ee1bf | 2015-08-10 11:59:02 -0700 | [diff] [blame] | 600 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 601 | SkDEBUGCODE(this->validate();) |
joshualitt | b7ee1bf | 2015-08-10 11:59:02 -0700 | [diff] [blame] | 602 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 603 | AutoCheckFlush acf(fDrawingManager); |
joshualitt | b7ee1bf | 2015-08-10 11:59:02 -0700 | [diff] [blame] | 604 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 605 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 606 | this->getDrawTarget()->drawBatch(pipelineBuilder, batch); |
joshualitt | b7ee1bf | 2015-08-10 11:59:02 -0700 | [diff] [blame] | 607 | } |
| 608 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 609 | void GrDrawContext::drawPath(const GrClip& clip, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 610 | const GrPaint& paint, |
| 611 | const SkMatrix& viewMatrix, |
| 612 | const SkPath& path, |
| 613 | const GrStrokeInfo& strokeInfo) { |
| 614 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 615 | SkDEBUGCODE(this->validate();) |
| 616 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 617 | if (path.isEmpty()) { |
| 618 | if (path.isInverseFillType()) { |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 619 | this->drawPaint(clip, paint, viewMatrix); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 620 | } |
| 621 | return; |
| 622 | } |
| 623 | |
| 624 | GrColor color = paint.getColor(); |
| 625 | |
| 626 | // Note that internalDrawPath may sw-rasterize the path into a scratch texture. |
| 627 | // Scratch textures can be recycled after they are returned to the texture |
| 628 | // cache. This presents a potential hazard for buffered drawing. However, |
| 629 | // the writePixels that uploads to the scratch will perform a flush so we're |
| 630 | // OK. |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 631 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 632 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 633 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 634 | if (!strokeInfo.isDashed()) { |
| 635 | bool useCoverageAA = paint.isAntiAlias() && |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 636 | !pipelineBuilder.getRenderTarget()->isUnifiedMultisampled(); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 637 | |
| 638 | if (useCoverageAA && strokeInfo.getWidth() < 0 && !path.isConvex()) { |
| 639 | // Concave AA paths are expensive - try to avoid them for special cases |
| 640 | SkRect rects[2]; |
| 641 | |
joshualitt | f9c5db2 | 2015-07-10 11:31:01 -0700 | [diff] [blame] | 642 | if (is_nested_rects(viewMatrix, path, strokeInfo, rects)) { |
joshualitt | d2b23e0 | 2015-08-21 10:53:34 -0700 | [diff] [blame] | 643 | SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateAAFillNestedRects( |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 644 | color, viewMatrix, rects)); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 645 | this->getDrawTarget()->drawBatch(pipelineBuilder, batch); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 646 | return; |
| 647 | } |
| 648 | } |
| 649 | SkRect ovalRect; |
| 650 | bool isOval = path.isOval(&ovalRect); |
| 651 | |
| 652 | if (isOval && !path.isInverseFillType()) { |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 653 | if (GrOvalRenderer::DrawOval(this->getDrawTarget(), |
joshualitt | ae3d63a | 2015-07-13 08:44:06 -0700 | [diff] [blame] | 654 | pipelineBuilder, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 655 | color, |
| 656 | viewMatrix, |
| 657 | paint.isAntiAlias(), |
| 658 | ovalRect, |
| 659 | strokeInfo)) { |
| 660 | return; |
| 661 | } |
| 662 | } |
| 663 | } |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 664 | this->internalDrawPath(this->getDrawTarget(), &pipelineBuilder, viewMatrix, color, |
| 665 | paint.isAntiAlias(), path, strokeInfo); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | void GrDrawContext::internalDrawPath(GrDrawTarget* target, |
| 669 | GrPipelineBuilder* pipelineBuilder, |
| 670 | const SkMatrix& viewMatrix, |
| 671 | GrColor color, |
| 672 | bool useAA, |
| 673 | const SkPath& path, |
| 674 | const GrStrokeInfo& strokeInfo) { |
| 675 | RETURN_IF_ABANDONED |
| 676 | SkASSERT(!path.isEmpty()); |
| 677 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 678 | // An Assumption here is that path renderer would use some form of tweaking |
| 679 | // the src color (either the input alpha or in the frag shader) to implement |
| 680 | // aa. If we have some future driver-mojo path AA that can do the right |
| 681 | // thing WRT to the blend then we'll need some query on the PR. |
| 682 | bool useCoverageAA = useAA && |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 683 | !pipelineBuilder->getRenderTarget()->isUnifiedMultisampled(); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 684 | |
| 685 | |
| 686 | GrPathRendererChain::DrawType type = |
| 687 | useCoverageAA ? GrPathRendererChain::kColorAntiAlias_DrawType : |
| 688 | GrPathRendererChain::kColor_DrawType; |
| 689 | |
| 690 | const SkPath* pathPtr = &path; |
| 691 | SkTLazy<SkPath> tmpPath; |
| 692 | const GrStrokeInfo* strokeInfoPtr = &strokeInfo; |
| 693 | |
| 694 | // Try a 1st time without stroking the path and without allowing the SW renderer |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 695 | GrPathRenderer* pr = fDrawingManager->getContext()->getPathRenderer(target, pipelineBuilder, |
| 696 | viewMatrix, *pathPtr, |
| 697 | *strokeInfoPtr, false, |
| 698 | type); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 699 | |
| 700 | GrStrokeInfo dashlessStrokeInfo(strokeInfo, false); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 701 | if (nullptr == pr && strokeInfo.isDashed()) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 702 | // It didn't work above, so try again with dashed stroke converted to a dashless stroke. |
| 703 | if (!strokeInfo.applyDashToPath(tmpPath.init(), &dashlessStrokeInfo, *pathPtr)) { |
| 704 | return; |
| 705 | } |
| 706 | pathPtr = tmpPath.get(); |
| 707 | if (pathPtr->isEmpty()) { |
| 708 | return; |
| 709 | } |
| 710 | strokeInfoPtr = &dashlessStrokeInfo; |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 711 | pr = fDrawingManager->getContext()->getPathRenderer(target, pipelineBuilder, viewMatrix, |
| 712 | *pathPtr, *strokeInfoPtr, |
| 713 | false, type); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 714 | } |
| 715 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 716 | if (nullptr == pr) { |
| 717 | if (!GrPathRenderer::IsStrokeHairlineOrEquivalent(*strokeInfoPtr, viewMatrix, nullptr) && |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 718 | !strokeInfoPtr->isFillStyle()) { |
| 719 | // It didn't work above, so try again with stroke converted to a fill. |
| 720 | if (!tmpPath.isValid()) { |
| 721 | tmpPath.init(); |
| 722 | } |
| 723 | dashlessStrokeInfo.setResScale(SkScalarAbs(viewMatrix.getMaxScale())); |
| 724 | if (!dashlessStrokeInfo.applyToPath(tmpPath.get(), *pathPtr)) { |
| 725 | return; |
| 726 | } |
| 727 | pathPtr = tmpPath.get(); |
| 728 | if (pathPtr->isEmpty()) { |
| 729 | return; |
| 730 | } |
| 731 | dashlessStrokeInfo.setFillStyle(); |
| 732 | strokeInfoPtr = &dashlessStrokeInfo; |
| 733 | } |
| 734 | |
| 735 | // This time, allow SW renderer |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 736 | pr = fDrawingManager->getContext()->getPathRenderer(target, pipelineBuilder, viewMatrix, |
| 737 | *pathPtr, *strokeInfoPtr, |
| 738 | true, type); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 739 | } |
| 740 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 741 | if (nullptr == pr) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 742 | #ifdef SK_DEBUG |
| 743 | SkDebugf("Unable to find path renderer compatible with path.\n"); |
| 744 | #endif |
| 745 | return; |
| 746 | } |
| 747 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 748 | GrPathRenderer::DrawPathArgs args; |
| 749 | args.fTarget = target; |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 750 | args.fResourceProvider = fDrawingManager->getContext()->resourceProvider(); |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 751 | args.fPipelineBuilder = pipelineBuilder; |
| 752 | args.fColor = color; |
| 753 | args.fViewMatrix = &viewMatrix; |
| 754 | args.fPath = pathPtr; |
| 755 | args.fStroke = strokeInfoPtr; |
| 756 | args.fAntiAlias = useCoverageAA; |
| 757 | pr->drawPath(args); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 758 | } |
| 759 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 760 | void GrDrawContext::drawBatch(GrPipelineBuilder* pipelineBuilder, GrDrawBatch* batch) { |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 761 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 762 | SkDEBUGCODE(this->validate();) |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 763 | |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 764 | this->getDrawTarget()->drawBatch(*pipelineBuilder, batch); |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 765 | } |