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