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