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, |
| 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 | |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 228 | static bool view_matrix_ok_for_aa_fill_rect(const SkMatrix& viewMatrix) { |
| 229 | return viewMatrix.preservesRightAngles(); |
| 230 | } |
| 231 | |
| 232 | static bool should_apply_coverage_aa(const GrPaint& paint, GrRenderTarget* rt) { |
| 233 | return paint.isAntiAlias() && !rt->isUnifiedMultisampled(); |
| 234 | } |
| 235 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 236 | void GrDrawContext::drawRect(const GrClip& clip, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 237 | const GrPaint& paint, |
| 238 | const SkMatrix& viewMatrix, |
| 239 | const SkRect& rect, |
| 240 | const GrStrokeInfo* strokeInfo) { |
| 241 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 242 | SkDEBUGCODE(this->validate();) |
| 243 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 244 | if (strokeInfo && strokeInfo->isDashed()) { |
| 245 | SkPath path; |
| 246 | path.setIsVolatile(true); |
| 247 | path.addRect(rect); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 248 | this->drawPath(clip, paint, viewMatrix, path, *strokeInfo); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 249 | return; |
| 250 | } |
| 251 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 252 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 253 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 254 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
joshualitt | 7b670db | 2015-07-09 13:25:02 -0700 | [diff] [blame] | 255 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 256 | SkScalar width = nullptr == strokeInfo ? -1 : strokeInfo->getWidth(); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 257 | |
| 258 | // Check if this is a full RT draw and can be replaced with a clear. We don't bother checking |
| 259 | // cases where the RT is fully inside a stroke. |
| 260 | if (width < 0) { |
| 261 | SkRect rtRect; |
| 262 | pipelineBuilder.getRenderTarget()->getBoundsRect(&rtRect); |
| 263 | SkRect clipSpaceRTRect = rtRect; |
| 264 | bool checkClip = GrClip::kWideOpen_ClipType != clip.clipType(); |
| 265 | if (checkClip) { |
| 266 | clipSpaceRTRect.offset(SkIntToScalar(clip.origin().fX), |
| 267 | SkIntToScalar(clip.origin().fY)); |
| 268 | } |
| 269 | // Does the clip contain the entire RT? |
| 270 | if (!checkClip || clip.quickContains(clipSpaceRTRect)) { |
| 271 | SkMatrix invM; |
| 272 | if (!viewMatrix.invert(&invM)) { |
| 273 | return; |
| 274 | } |
| 275 | // Does the rect bound the RT? |
| 276 | SkPoint srcSpaceRTQuad[4]; |
| 277 | invM.mapRectToQuad(srcSpaceRTQuad, rtRect); |
| 278 | if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) && |
| 279 | rect_contains_inclusive(rect, srcSpaceRTQuad[1]) && |
| 280 | rect_contains_inclusive(rect, srcSpaceRTQuad[2]) && |
| 281 | rect_contains_inclusive(rect, srcSpaceRTQuad[3])) { |
| 282 | // Will it blend? |
| 283 | GrColor clearColor; |
cdalton | 1fa4572 | 2015-06-02 10:43:39 -0700 | [diff] [blame] | 284 | if (paint.isConstantBlendedColor(&clearColor)) { |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 285 | this->getDrawTarget()->clear(nullptr, clearColor, true, fRenderTarget); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 286 | return; |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | GrColor color = paint.getColor(); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 293 | bool needAA = should_apply_coverage_aa(paint, pipelineBuilder.getRenderTarget()); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 294 | |
joshualitt | 9be0b00 | 2015-08-19 11:50:51 -0700 | [diff] [blame] | 295 | // The fill path can handle rotation but not skew |
| 296 | // 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] | 297 | // None of our AA draw rect calls can handle perspective yet |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 298 | bool canApplyAA = width >=0 ? viewMatrix.rectStaysRect() : |
| 299 | view_matrix_ok_for_aa_fill_rect(viewMatrix); |
joshualitt | 9be0b00 | 2015-08-19 11:50:51 -0700 | [diff] [blame] | 300 | |
| 301 | if (needAA && canApplyAA) { |
robertphillips | fac9ceb | 2015-09-03 08:32:08 -0700 | [diff] [blame] | 302 | SkASSERT(!viewMatrix.hasPerspective()); |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 303 | SkAutoTUnref<GrDrawBatch> batch; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 304 | if (width >= 0) { |
joshualitt | 10cae83 | 2015-09-22 12:50:33 -0700 | [diff] [blame] | 305 | batch.reset(GrRectBatchFactory::CreateAAStroke(color, viewMatrix, rect, *strokeInfo)); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 306 | } else { |
joshualitt | 10cae83 | 2015-09-22 12:50:33 -0700 | [diff] [blame] | 307 | SkRect devBoundRect; |
| 308 | viewMatrix.mapRect(&devBoundRect, rect); |
joshualitt | d2b23e0 | 2015-08-21 10:53:34 -0700 | [diff] [blame] | 309 | batch.reset(GrRectBatchFactory::CreateAAFill(color, viewMatrix, rect, devBoundRect)); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 310 | } |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 311 | this->getDrawTarget()->drawBatch(pipelineBuilder, batch); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 312 | return; |
| 313 | } |
| 314 | |
| 315 | if (width >= 0) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 316 | // 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] | 317 | bool snapToPixelCenters = (0 == width && !fRenderTarget->isUnifiedMultisampled()); |
joshualitt | d2b23e0 | 2015-08-21 10:53:34 -0700 | [diff] [blame] | 318 | SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAStroke( |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 319 | color, viewMatrix, rect, width, snapToPixelCenters)); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 320 | |
| 321 | // Depending on sub-pixel coordinates and the particular GPU, we may lose a corner of |
| 322 | // hairline rects. We jam all the vertices to pixel centers to avoid this, but not when MSAA |
| 323 | // is enabled because it can cause ugly artifacts. |
| 324 | pipelineBuilder.setState(GrPipelineBuilder::kSnapVerticesToPixelCenters_Flag, |
| 325 | snapToPixelCenters); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 326 | this->getDrawTarget()->drawBatch(pipelineBuilder, batch); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 327 | } else { |
| 328 | // filled BW rect |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 329 | this->getDrawTarget()->drawNonAARect(pipelineBuilder, color, viewMatrix, rect); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
bsalomon | a2e69fc | 2015-11-05 10:41:43 -0800 | [diff] [blame] | 333 | void GrDrawContext::fillRectToRect(const GrClip& clip, |
| 334 | const GrPaint& paint, |
| 335 | const SkMatrix& viewMatrix, |
| 336 | const SkRect& rectToDraw, |
| 337 | const SkRect& localRect) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 338 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 339 | SkDEBUGCODE(this->validate();) |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 340 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 341 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 342 | |
| 343 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 344 | if (should_apply_coverage_aa(paint, fRenderTarget) && |
| 345 | view_matrix_ok_for_aa_fill_rect(viewMatrix)) { |
| 346 | SkAutoTUnref<GrDrawBatch> batch(GrAAFillRectBatch::CreateWithLocalRect( |
| 347 | paint.getColor(), viewMatrix, rectToDraw, localRect)); |
| 348 | if (batch) { |
| 349 | this->drawBatch(&pipelineBuilder, batch); |
| 350 | } |
| 351 | } else { |
| 352 | this->getDrawTarget()->drawNonAARect(pipelineBuilder, |
| 353 | paint.getColor(), |
| 354 | viewMatrix, |
| 355 | rectToDraw, |
| 356 | localRect); |
| 357 | } |
joshualitt | b6b513b | 2015-08-21 10:25:18 -0700 | [diff] [blame] | 358 | } |
| 359 | |
bsalomon | a2e69fc | 2015-11-05 10:41:43 -0800 | [diff] [blame] | 360 | void GrDrawContext::fillRectWithLocalMatrix(const GrClip& clip, |
| 361 | const GrPaint& paint, |
| 362 | const SkMatrix& viewMatrix, |
| 363 | const SkRect& rectToDraw, |
| 364 | const SkMatrix& localMatrix) { |
joshualitt | b6b513b | 2015-08-21 10:25:18 -0700 | [diff] [blame] | 365 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 366 | SkDEBUGCODE(this->validate();) |
joshualitt | b6b513b | 2015-08-21 10:25:18 -0700 | [diff] [blame] | 367 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 368 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 369 | |
| 370 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 371 | if (should_apply_coverage_aa(paint, pipelineBuilder.getRenderTarget()) && |
| 372 | view_matrix_ok_for_aa_fill_rect(viewMatrix)) { |
| 373 | SkAutoTUnref<GrDrawBatch> batch(GrAAFillRectBatch::Create( |
| 374 | paint.getColor(), viewMatrix, localMatrix, rectToDraw)); |
| 375 | this->drawBatch(&pipelineBuilder, batch); |
| 376 | } else { |
| 377 | this->getDrawTarget()->drawNonAARect(pipelineBuilder, |
| 378 | paint.getColor(), |
| 379 | viewMatrix, |
| 380 | rectToDraw, |
| 381 | localMatrix); |
| 382 | } |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 383 | } |
| 384 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 385 | void GrDrawContext::drawVertices(const GrClip& clip, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 386 | const GrPaint& paint, |
| 387 | const SkMatrix& viewMatrix, |
| 388 | GrPrimitiveType primitiveType, |
| 389 | int vertexCount, |
| 390 | const SkPoint positions[], |
| 391 | const SkPoint texCoords[], |
| 392 | const GrColor colors[], |
| 393 | const uint16_t indices[], |
| 394 | int indexCount) { |
| 395 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 396 | SkDEBUGCODE(this->validate();) |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 397 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 398 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 399 | |
| 400 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
joshualitt | 7b670db | 2015-07-09 13:25:02 -0700 | [diff] [blame] | 401 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 402 | // TODO clients should give us bounds |
| 403 | SkRect bounds; |
| 404 | if (!bounds.setBoundsCheck(positions, vertexCount)) { |
| 405 | SkDebugf("drawVertices call empty bounds\n"); |
| 406 | return; |
| 407 | } |
| 408 | |
| 409 | viewMatrix.mapRect(&bounds); |
| 410 | |
| 411 | // 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^] | 412 | // snapping. We also do this for the "hair" primitive types: lines and points since they have |
| 413 | // a 1 pixel thickness in device space. |
| 414 | if (!paint.isAntiAlias() || GrIsPrimTypeLines(primitiveType) || |
| 415 | kPoints_GrPrimitiveType == primitiveType) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 416 | bounds.outset(0.5f, 0.5f); |
| 417 | } |
| 418 | |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 419 | GrDrawVerticesBatch::Geometry geometry; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 420 | geometry.fColor = paint.getColor(); |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 421 | SkAutoTUnref<GrDrawBatch> batch(GrDrawVerticesBatch::Create(geometry, primitiveType, viewMatrix, |
| 422 | positions, vertexCount, indices, |
| 423 | indexCount, colors, texCoords, |
| 424 | bounds)); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 425 | |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 426 | this->getDrawTarget()->drawBatch(pipelineBuilder, batch); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | /////////////////////////////////////////////////////////////////////////////// |
| 430 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 431 | void GrDrawContext::drawAtlas(const GrClip& clip, |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 432 | const GrPaint& paint, |
| 433 | const SkMatrix& viewMatrix, |
| 434 | int spriteCount, |
| 435 | const SkRSXform xform[], |
| 436 | const SkRect texRect[], |
| 437 | const SkColor colors[]) { |
| 438 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 439 | SkDEBUGCODE(this->validate();) |
| 440 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 441 | AutoCheckFlush acf(fDrawingManager); |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 442 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 443 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 444 | |
jvanverth | 14b8803 | 2015-08-07 12:18:54 -0700 | [diff] [blame] | 445 | GrDrawAtlasBatch::Geometry geometry; |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 446 | geometry.fColor = paint.getColor(); |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 447 | SkAutoTUnref<GrDrawBatch> batch(GrDrawAtlasBatch::Create(geometry, viewMatrix, spriteCount, |
| 448 | xform, texRect, colors)); |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 449 | |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 450 | this->getDrawTarget()->drawBatch(pipelineBuilder, batch); |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | /////////////////////////////////////////////////////////////////////////////// |
| 454 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 455 | void GrDrawContext::drawRRect(const GrClip& clip, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 456 | const GrPaint& paint, |
| 457 | const SkMatrix& viewMatrix, |
| 458 | const SkRRect& rrect, |
| 459 | const GrStrokeInfo& strokeInfo) { |
| 460 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 461 | SkDEBUGCODE(this->validate();) |
| 462 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 463 | if (rrect.isEmpty()) { |
| 464 | return; |
| 465 | } |
| 466 | |
| 467 | if (strokeInfo.isDashed()) { |
| 468 | SkPath path; |
| 469 | path.setIsVolatile(true); |
| 470 | path.addRRect(rrect); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 471 | this->drawPath(clip, paint, viewMatrix, path, strokeInfo); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 472 | return; |
| 473 | } |
| 474 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 475 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 476 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 477 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 478 | GrColor color = paint.getColor(); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 479 | |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 480 | if (!GrOvalRenderer::DrawRRect(this->getDrawTarget(), |
joshualitt | ae3d63a | 2015-07-13 08:44:06 -0700 | [diff] [blame] | 481 | pipelineBuilder, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 482 | color, |
| 483 | viewMatrix, |
| 484 | paint.isAntiAlias(), |
| 485 | rrect, |
| 486 | strokeInfo)) { |
| 487 | SkPath path; |
| 488 | path.setIsVolatile(true); |
| 489 | path.addRRect(rrect); |
robertphillips | b83bec5 | 2015-10-23 09:38:03 -0700 | [diff] [blame] | 490 | this->internalDrawPath(&pipelineBuilder, viewMatrix, color, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 491 | paint.isAntiAlias(), path, strokeInfo); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | /////////////////////////////////////////////////////////////////////////////// |
| 496 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 497 | void GrDrawContext::drawDRRect(const GrClip& clip, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 498 | const GrPaint& paint, |
| 499 | const SkMatrix& viewMatrix, |
| 500 | const SkRRect& outer, |
| 501 | const SkRRect& inner) { |
| 502 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 503 | SkDEBUGCODE(this->validate();) |
| 504 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 505 | if (outer.isEmpty()) { |
| 506 | return; |
| 507 | } |
| 508 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 509 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 510 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 511 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 512 | GrColor color = paint.getColor(); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 513 | if (!GrOvalRenderer::DrawDRRect(this->getDrawTarget(), |
joshualitt | ae3d63a | 2015-07-13 08:44:06 -0700 | [diff] [blame] | 514 | pipelineBuilder, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 515 | color, |
| 516 | viewMatrix, |
| 517 | paint.isAntiAlias(), |
| 518 | outer, |
| 519 | inner)) { |
| 520 | SkPath path; |
| 521 | path.setIsVolatile(true); |
| 522 | path.addRRect(inner); |
| 523 | path.addRRect(outer); |
| 524 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 525 | |
| 526 | GrStrokeInfo fillRec(SkStrokeRec::kFill_InitStyle); |
robertphillips | b83bec5 | 2015-10-23 09:38:03 -0700 | [diff] [blame] | 527 | this->internalDrawPath(&pipelineBuilder, viewMatrix, color, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 528 | paint.isAntiAlias(), path, fillRec); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | /////////////////////////////////////////////////////////////////////////////// |
| 533 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 534 | void GrDrawContext::drawOval(const GrClip& clip, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 535 | const GrPaint& paint, |
| 536 | const SkMatrix& viewMatrix, |
| 537 | const SkRect& oval, |
| 538 | const GrStrokeInfo& strokeInfo) { |
| 539 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 540 | SkDEBUGCODE(this->validate();) |
| 541 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 542 | if (oval.isEmpty()) { |
| 543 | return; |
| 544 | } |
| 545 | |
| 546 | if (strokeInfo.isDashed()) { |
| 547 | SkPath path; |
| 548 | path.setIsVolatile(true); |
| 549 | path.addOval(oval); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 550 | this->drawPath(clip, paint, viewMatrix, path, strokeInfo); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 551 | return; |
| 552 | } |
| 553 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 554 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 555 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 556 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 557 | GrColor color = paint.getColor(); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 558 | |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 559 | if (!GrOvalRenderer::DrawOval(this->getDrawTarget(), |
joshualitt | ae3d63a | 2015-07-13 08:44:06 -0700 | [diff] [blame] | 560 | pipelineBuilder, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 561 | color, |
| 562 | viewMatrix, |
| 563 | paint.isAntiAlias(), |
| 564 | oval, |
| 565 | strokeInfo)) { |
| 566 | SkPath path; |
| 567 | path.setIsVolatile(true); |
| 568 | path.addOval(oval); |
robertphillips | b83bec5 | 2015-10-23 09:38:03 -0700 | [diff] [blame] | 569 | this->internalDrawPath(&pipelineBuilder, viewMatrix, color, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 570 | paint.isAntiAlias(), path, strokeInfo); |
| 571 | } |
| 572 | } |
| 573 | |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 574 | void GrDrawContext::drawImageNine(const GrClip& clip, |
| 575 | const GrPaint& paint, |
| 576 | const SkMatrix& viewMatrix, |
| 577 | int imageWidth, |
| 578 | int imageHeight, |
| 579 | const SkIRect& center, |
| 580 | const SkRect& dst) { |
| 581 | RETURN_IF_ABANDONED |
| 582 | SkDEBUGCODE(this->validate();) |
| 583 | |
| 584 | AutoCheckFlush acf(fDrawingManager); |
| 585 | |
| 586 | SkAutoTUnref<GrDrawBatch> batch(GrNinePatch::CreateNonAA(paint.getColor(), viewMatrix, |
| 587 | imageWidth, imageHeight, |
| 588 | center, dst)); |
| 589 | |
| 590 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
| 591 | this->getDrawTarget()->drawBatch(pipelineBuilder, batch); |
| 592 | } |
| 593 | |
| 594 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 595 | // Can 'path' be drawn as a pair of filled nested rectangles? |
joshualitt | f9c5db2 | 2015-07-10 11:31:01 -0700 | [diff] [blame] | 596 | static bool is_nested_rects(const SkMatrix& viewMatrix, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 597 | const SkPath& path, |
| 598 | const SkStrokeRec& stroke, |
| 599 | SkRect rects[2]) { |
| 600 | SkASSERT(stroke.isFillStyle()); |
| 601 | |
| 602 | if (path.isInverseFillType()) { |
| 603 | return false; |
| 604 | } |
| 605 | |
| 606 | // TODO: this restriction could be lifted if we were willing to apply |
| 607 | // the matrix to all the points individually rather than just to the rect |
| 608 | if (!viewMatrix.preservesAxisAlignment()) { |
| 609 | return false; |
| 610 | } |
| 611 | |
| 612 | SkPath::Direction dirs[2]; |
| 613 | if (!path.isNestedFillRects(rects, dirs)) { |
| 614 | return false; |
| 615 | } |
| 616 | |
| 617 | if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) { |
| 618 | // The two rects need to be wound opposite to each other |
| 619 | return false; |
| 620 | } |
| 621 | |
| 622 | // Right now, nested rects where the margin is not the same width |
| 623 | // all around do not render correctly |
| 624 | const SkScalar* outer = rects[0].asScalars(); |
| 625 | const SkScalar* inner = rects[1].asScalars(); |
| 626 | |
| 627 | bool allEq = true; |
| 628 | |
| 629 | SkScalar margin = SkScalarAbs(outer[0] - inner[0]); |
| 630 | bool allGoE1 = margin >= SK_Scalar1; |
| 631 | |
| 632 | for (int i = 1; i < 4; ++i) { |
| 633 | SkScalar temp = SkScalarAbs(outer[i] - inner[i]); |
| 634 | if (temp < SK_Scalar1) { |
| 635 | allGoE1 = false; |
| 636 | } |
| 637 | if (!SkScalarNearlyEqual(margin, temp)) { |
| 638 | allEq = false; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | return allEq || allGoE1; |
| 643 | } |
| 644 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 645 | void GrDrawContext::drawBatch(const GrClip& clip, |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 646 | const GrPaint& paint, GrDrawBatch* batch) { |
joshualitt | b7ee1bf | 2015-08-10 11:59:02 -0700 | [diff] [blame] | 647 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 648 | SkDEBUGCODE(this->validate();) |
joshualitt | b7ee1bf | 2015-08-10 11:59:02 -0700 | [diff] [blame] | 649 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 650 | AutoCheckFlush acf(fDrawingManager); |
joshualitt | b7ee1bf | 2015-08-10 11:59:02 -0700 | [diff] [blame] | 651 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 652 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 653 | this->getDrawTarget()->drawBatch(pipelineBuilder, batch); |
joshualitt | b7ee1bf | 2015-08-10 11:59:02 -0700 | [diff] [blame] | 654 | } |
| 655 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 656 | void GrDrawContext::drawPath(const GrClip& clip, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 657 | const GrPaint& paint, |
| 658 | const SkMatrix& viewMatrix, |
| 659 | const SkPath& path, |
| 660 | const GrStrokeInfo& strokeInfo) { |
| 661 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 662 | SkDEBUGCODE(this->validate();) |
| 663 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 664 | if (path.isEmpty()) { |
| 665 | if (path.isInverseFillType()) { |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 666 | this->drawPaint(clip, paint, viewMatrix); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 667 | } |
| 668 | return; |
| 669 | } |
| 670 | |
| 671 | GrColor color = paint.getColor(); |
| 672 | |
| 673 | // Note that internalDrawPath may sw-rasterize the path into a scratch texture. |
| 674 | // Scratch textures can be recycled after they are returned to the texture |
| 675 | // cache. This presents a potential hazard for buffered drawing. However, |
| 676 | // the writePixels that uploads to the scratch will perform a flush so we're |
| 677 | // OK. |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 678 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 679 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 680 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 681 | if (!strokeInfo.isDashed()) { |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 682 | bool useCoverageAA = should_apply_coverage_aa(paint, pipelineBuilder.getRenderTarget()); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 683 | |
| 684 | if (useCoverageAA && strokeInfo.getWidth() < 0 && !path.isConvex()) { |
| 685 | // Concave AA paths are expensive - try to avoid them for special cases |
| 686 | SkRect rects[2]; |
| 687 | |
joshualitt | f9c5db2 | 2015-07-10 11:31:01 -0700 | [diff] [blame] | 688 | if (is_nested_rects(viewMatrix, path, strokeInfo, rects)) { |
joshualitt | d2b23e0 | 2015-08-21 10:53:34 -0700 | [diff] [blame] | 689 | SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateAAFillNestedRects( |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 690 | color, viewMatrix, rects)); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 691 | this->getDrawTarget()->drawBatch(pipelineBuilder, batch); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 692 | return; |
| 693 | } |
| 694 | } |
| 695 | SkRect ovalRect; |
| 696 | bool isOval = path.isOval(&ovalRect); |
| 697 | |
| 698 | if (isOval && !path.isInverseFillType()) { |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 699 | if (GrOvalRenderer::DrawOval(this->getDrawTarget(), |
joshualitt | ae3d63a | 2015-07-13 08:44:06 -0700 | [diff] [blame] | 700 | pipelineBuilder, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 701 | color, |
| 702 | viewMatrix, |
| 703 | paint.isAntiAlias(), |
| 704 | ovalRect, |
| 705 | strokeInfo)) { |
| 706 | return; |
| 707 | } |
| 708 | } |
| 709 | } |
robertphillips | b83bec5 | 2015-10-23 09:38:03 -0700 | [diff] [blame] | 710 | this->internalDrawPath(&pipelineBuilder, viewMatrix, color, |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 711 | paint.isAntiAlias(), path, strokeInfo); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 712 | } |
| 713 | |
robertphillips | b83bec5 | 2015-10-23 09:38:03 -0700 | [diff] [blame] | 714 | void GrDrawContext::internalDrawPath(GrPipelineBuilder* pipelineBuilder, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 715 | const SkMatrix& viewMatrix, |
| 716 | GrColor color, |
| 717 | bool useAA, |
| 718 | const SkPath& path, |
| 719 | const GrStrokeInfo& strokeInfo) { |
| 720 | RETURN_IF_ABANDONED |
| 721 | SkASSERT(!path.isEmpty()); |
| 722 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 723 | // An Assumption here is that path renderer would use some form of tweaking |
| 724 | // the src color (either the input alpha or in the frag shader) to implement |
| 725 | // aa. If we have some future driver-mojo path AA that can do the right |
| 726 | // thing WRT to the blend then we'll need some query on the PR. |
| 727 | bool useCoverageAA = useAA && |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 728 | !pipelineBuilder->getRenderTarget()->isUnifiedMultisampled(); |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 729 | bool isStencilDisabled = pipelineBuilder->getStencil().isDisabled(); |
| 730 | bool isStencilBufferMSAA = pipelineBuilder->getRenderTarget()->isStencilBufferMultisampled(); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 731 | |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 732 | const GrPathRendererChain::DrawType type = |
| 733 | useCoverageAA ? GrPathRendererChain::kColorAntiAlias_DrawType |
| 734 | : GrPathRendererChain::kColor_DrawType; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 735 | |
| 736 | const SkPath* pathPtr = &path; |
| 737 | SkTLazy<SkPath> tmpPath; |
| 738 | const GrStrokeInfo* strokeInfoPtr = &strokeInfo; |
| 739 | |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 740 | GrPathRenderer::CanDrawPathArgs canDrawArgs; |
| 741 | canDrawArgs.fShaderCaps = fDrawingManager->getContext()->caps()->shaderCaps(); |
| 742 | canDrawArgs.fViewMatrix = &viewMatrix; |
| 743 | canDrawArgs.fPath = pathPtr; |
| 744 | canDrawArgs.fStroke = strokeInfoPtr; |
| 745 | canDrawArgs.fAntiAlias = useCoverageAA; |
| 746 | canDrawArgs.fIsStencilDisabled = isStencilDisabled; |
| 747 | canDrawArgs.fIsStencilBufferMSAA = isStencilBufferMSAA; |
| 748 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 749 | // 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] | 750 | GrPathRenderer* pr = fDrawingManager->getPathRenderer(canDrawArgs, false, type); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 751 | |
| 752 | GrStrokeInfo dashlessStrokeInfo(strokeInfo, false); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 753 | if (nullptr == pr && strokeInfo.isDashed()) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 754 | // It didn't work above, so try again with dashed stroke converted to a dashless stroke. |
| 755 | if (!strokeInfo.applyDashToPath(tmpPath.init(), &dashlessStrokeInfo, *pathPtr)) { |
| 756 | return; |
| 757 | } |
| 758 | pathPtr = tmpPath.get(); |
| 759 | if (pathPtr->isEmpty()) { |
| 760 | return; |
| 761 | } |
| 762 | strokeInfoPtr = &dashlessStrokeInfo; |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 763 | |
| 764 | canDrawArgs.fPath = pathPtr; |
| 765 | canDrawArgs.fStroke = strokeInfoPtr; |
| 766 | |
| 767 | pr = fDrawingManager->getPathRenderer(canDrawArgs, false, type); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 768 | } |
| 769 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 770 | if (nullptr == pr) { |
| 771 | if (!GrPathRenderer::IsStrokeHairlineOrEquivalent(*strokeInfoPtr, viewMatrix, nullptr) && |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 772 | !strokeInfoPtr->isFillStyle()) { |
| 773 | // It didn't work above, so try again with stroke converted to a fill. |
| 774 | if (!tmpPath.isValid()) { |
| 775 | tmpPath.init(); |
| 776 | } |
| 777 | dashlessStrokeInfo.setResScale(SkScalarAbs(viewMatrix.getMaxScale())); |
| 778 | if (!dashlessStrokeInfo.applyToPath(tmpPath.get(), *pathPtr)) { |
| 779 | return; |
| 780 | } |
| 781 | pathPtr = tmpPath.get(); |
| 782 | if (pathPtr->isEmpty()) { |
| 783 | return; |
| 784 | } |
| 785 | dashlessStrokeInfo.setFillStyle(); |
| 786 | strokeInfoPtr = &dashlessStrokeInfo; |
| 787 | } |
| 788 | |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 789 | canDrawArgs.fPath = pathPtr; |
| 790 | canDrawArgs.fStroke = strokeInfoPtr; |
| 791 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 792 | // This time, allow SW renderer |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 793 | pr = fDrawingManager->getPathRenderer(canDrawArgs, true, type); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 794 | } |
| 795 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 796 | if (nullptr == pr) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 797 | #ifdef SK_DEBUG |
| 798 | SkDebugf("Unable to find path renderer compatible with path.\n"); |
| 799 | #endif |
| 800 | return; |
| 801 | } |
| 802 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 803 | GrPathRenderer::DrawPathArgs args; |
robertphillips | b83bec5 | 2015-10-23 09:38:03 -0700 | [diff] [blame] | 804 | args.fTarget = this->getDrawTarget(); |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 805 | args.fResourceProvider = fDrawingManager->getContext()->resourceProvider(); |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 806 | args.fPipelineBuilder = pipelineBuilder; |
| 807 | args.fColor = color; |
| 808 | args.fViewMatrix = &viewMatrix; |
| 809 | args.fPath = pathPtr; |
| 810 | args.fStroke = strokeInfoPtr; |
| 811 | args.fAntiAlias = useCoverageAA; |
| 812 | pr->drawPath(args); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 813 | } |
| 814 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 815 | void GrDrawContext::drawBatch(GrPipelineBuilder* pipelineBuilder, GrDrawBatch* batch) { |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 816 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 817 | SkDEBUGCODE(this->validate();) |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 818 | |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 819 | this->getDrawTarget()->drawBatch(*pipelineBuilder, batch); |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 820 | } |