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 | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 9 | #include "GrBatchTest.h" |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 10 | #include "GrColor.h" |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 11 | #include "GrDrawContext.h" |
robertphillips | 86c6075 | 2016-03-02 08:43:13 -0800 | [diff] [blame^] | 12 | #include "GrDrawContextPriv.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 | 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 | 14b8803 | 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 | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 25 | #include "batches/GrNinePatch.h" // TODO Factory |
joshualitt | 7441782 | 2015-08-07 11:42:16 -0700 | [diff] [blame] | 26 | |
robertphillips | 0009589 | 2016-02-29 13:50:40 -0800 | [diff] [blame] | 27 | #include "effects/GrRRectEffect.h" |
| 28 | |
joshualitt | e804292 | 2015-12-11 06:11:21 -0800 | [diff] [blame] | 29 | #include "text/GrAtlasTextContext.h" |
| 30 | #include "text/GrStencilAndCoverTextContext.h" |
| 31 | |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 32 | #include "../private/GrAuditTrail.h" |
| 33 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 34 | #define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == fDrawingManager->getContext()) |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 35 | #define ASSERT_SINGLE_OWNER \ |
joshualitt | de8dc7e | 2016-01-08 10:09:13 -0800 | [diff] [blame] | 36 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);) |
robertphillips | 86c6075 | 2016-03-02 08:43:13 -0800 | [diff] [blame^] | 37 | #define ASSERT_SINGLE_OWNER_PRIV \ |
| 38 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fDrawContext->fSingleOwner);) |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 39 | #define RETURN_IF_ABANDONED if (fDrawingManager->abandoned()) { return; } |
| 40 | #define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->abandoned()) { return false; } |
robertphillips | 86c6075 | 2016-03-02 08:43:13 -0800 | [diff] [blame^] | 41 | #define RETURN_FALSE_IF_ABANDONED_PRIV if (fDrawContext->fDrawingManager->abandoned()) { return false; } |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 42 | #define RETURN_NULL_IF_ABANDONED if (fDrawingManager->abandoned()) { return nullptr; } |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 43 | |
| 44 | class AutoCheckFlush { |
| 45 | public: |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 46 | AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) { |
| 47 | SkASSERT(fDrawingManager); |
| 48 | } |
| 49 | ~AutoCheckFlush() { fDrawingManager->getContext()->flushIfNecessary(); } |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 50 | |
| 51 | private: |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 52 | GrDrawingManager* fDrawingManager; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 55 | // In MDB mode the reffing of the 'getLastDrawTarget' call's result allows in-progress |
| 56 | // drawTargets to be picked up and added to by drawContexts lower in the call |
| 57 | // stack. When this occurs with a closed drawTarget, a new one will be allocated |
| 58 | // when the drawContext attempts to use it (via getDrawTarget). |
joshualitt | 1b39f43 | 2016-02-11 06:46:52 -0800 | [diff] [blame] | 59 | GrDrawContext::GrDrawContext(GrContext* context, |
| 60 | GrDrawingManager* drawingMgr, |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 61 | GrRenderTarget* rt, |
joshualitt | de8dc7e | 2016-01-08 10:09:13 -0800 | [diff] [blame] | 62 | const SkSurfaceProps* surfaceProps, |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 63 | GrAuditTrail* auditTrail, |
joshualitt | de8dc7e | 2016-01-08 10:09:13 -0800 | [diff] [blame] | 64 | GrSingleOwner* singleOwner) |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 65 | : fDrawingManager(drawingMgr) |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 66 | , fRenderTarget(rt) |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 67 | , fDrawTarget(SkSafeRef(rt->getLastDrawTarget())) |
joshualitt | 1b39f43 | 2016-02-11 06:46:52 -0800 | [diff] [blame] | 68 | , fContext(context) |
joshualitt | de8dc7e | 2016-01-08 10:09:13 -0800 | [diff] [blame] | 69 | , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps)) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 70 | , fAuditTrail(auditTrail) |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 71 | #ifdef SK_DEBUG |
| 72 | , fSingleOwner(singleOwner) |
| 73 | #endif |
| 74 | { |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 75 | SkDEBUGCODE(this->validate();) |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 76 | } |
| 77 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 78 | #ifdef SK_DEBUG |
| 79 | void GrDrawContext::validate() const { |
| 80 | SkASSERT(fRenderTarget); |
| 81 | ASSERT_OWNED_RESOURCE(fRenderTarget); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 82 | |
| 83 | if (fDrawTarget && !fDrawTarget->isClosed()) { |
| 84 | SkASSERT(fRenderTarget->getLastDrawTarget() == fDrawTarget); |
| 85 | } |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 86 | } |
| 87 | #endif |
| 88 | |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 89 | GrDrawContext::~GrDrawContext() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 90 | ASSERT_SINGLE_OWNER |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 91 | SkSafeUnref(fDrawTarget); |
| 92 | } |
| 93 | |
| 94 | GrDrawTarget* GrDrawContext::getDrawTarget() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 95 | ASSERT_SINGLE_OWNER |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 96 | SkDEBUGCODE(this->validate();) |
| 97 | |
| 98 | if (!fDrawTarget || fDrawTarget->isClosed()) { |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 99 | fDrawTarget = fDrawingManager->newDrawTarget(fRenderTarget); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | return fDrawTarget; |
| 103 | } |
| 104 | |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 105 | bool GrDrawContext::copySurface(GrSurface* src, const SkIRect& srcRect, const SkIPoint& dstPoint) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 106 | ASSERT_SINGLE_OWNER |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 107 | RETURN_FALSE_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 108 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 109 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::copySurface"); |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 110 | |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 111 | return this->getDrawTarget()->copySurface(fRenderTarget, src, srcRect, dstPoint); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 112 | } |
| 113 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 114 | void GrDrawContext::drawText(const GrClip& clip, const GrPaint& grPaint, |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 115 | const SkPaint& skPaint, |
| 116 | const SkMatrix& viewMatrix, |
| 117 | const char text[], size_t byteLength, |
| 118 | SkScalar x, SkScalar y, const SkIRect& clipBounds) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 119 | ASSERT_SINGLE_OWNER |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 120 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 121 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 122 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::drawText"); |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 123 | |
joshualitt | 96880d9 | 2016-02-16 10:36:53 -0800 | [diff] [blame] | 124 | if (!fAtlasTextContext) { |
joshualitt | 8db8678 | 2016-02-17 05:40:00 -0800 | [diff] [blame] | 125 | fAtlasTextContext.reset(GrAtlasTextContext::Create()); |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 126 | } |
| 127 | |
joshualitt | 96880d9 | 2016-02-16 10:36:53 -0800 | [diff] [blame] | 128 | fAtlasTextContext->drawText(fContext, this, clip, grPaint, skPaint, viewMatrix, fSurfaceProps, |
| 129 | text, byteLength, x, y, clipBounds); |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 130 | } |
robertphillips | caef345 | 2015-11-11 13:18:11 -0800 | [diff] [blame] | 131 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 132 | void GrDrawContext::drawPosText(const GrClip& clip, const GrPaint& grPaint, |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 133 | const SkPaint& skPaint, |
| 134 | const SkMatrix& viewMatrix, |
| 135 | const char text[], size_t byteLength, |
| 136 | const SkScalar pos[], int scalarsPerPosition, |
| 137 | const SkPoint& offset, const SkIRect& clipBounds) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 138 | ASSERT_SINGLE_OWNER |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 139 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 140 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 141 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::drawPosText"); |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 142 | |
joshualitt | 96880d9 | 2016-02-16 10:36:53 -0800 | [diff] [blame] | 143 | if (!fAtlasTextContext) { |
joshualitt | 8db8678 | 2016-02-17 05:40:00 -0800 | [diff] [blame] | 144 | fAtlasTextContext.reset(GrAtlasTextContext::Create()); |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 145 | } |
| 146 | |
joshualitt | 96880d9 | 2016-02-16 10:36:53 -0800 | [diff] [blame] | 147 | fAtlasTextContext->drawPosText(fContext, this, clip, grPaint, skPaint, viewMatrix, |
| 148 | fSurfaceProps, text, byteLength, pos, scalarsPerPosition, |
| 149 | offset, clipBounds); |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 150 | |
| 151 | } |
robertphillips | caef345 | 2015-11-11 13:18:11 -0800 | [diff] [blame] | 152 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 153 | void GrDrawContext::drawTextBlob(const GrClip& clip, const SkPaint& skPaint, |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 154 | const SkMatrix& viewMatrix, const SkTextBlob* blob, |
| 155 | SkScalar x, SkScalar y, |
| 156 | SkDrawFilter* filter, const SkIRect& clipBounds) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 157 | ASSERT_SINGLE_OWNER |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 158 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 159 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 160 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::drawTextBlob"); |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 161 | |
joshualitt | 96880d9 | 2016-02-16 10:36:53 -0800 | [diff] [blame] | 162 | if (!fAtlasTextContext) { |
joshualitt | 8db8678 | 2016-02-17 05:40:00 -0800 | [diff] [blame] | 163 | fAtlasTextContext.reset(GrAtlasTextContext::Create()); |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 164 | } |
| 165 | |
joshualitt | 96880d9 | 2016-02-16 10:36:53 -0800 | [diff] [blame] | 166 | fAtlasTextContext->drawTextBlob(fContext, this, clip, skPaint, viewMatrix, fSurfaceProps, blob, |
| 167 | x, y, filter, clipBounds); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 168 | } |
| 169 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 170 | void GrDrawContext::discard() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 171 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 172 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 173 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 174 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::discard"); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 175 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 176 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 177 | this->getDrawTarget()->discard(fRenderTarget); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 178 | } |
| 179 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 180 | void GrDrawContext::clear(const SkIRect* rect, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 181 | const GrColor color, |
| 182 | bool canIgnoreRect) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 183 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 184 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 185 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 186 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::clear"); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 187 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 188 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 189 | this->getDrawTarget()->clear(rect, color, canIgnoreRect, fRenderTarget); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 193 | void GrDrawContext::drawPaint(const GrClip& clip, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 194 | const GrPaint& origPaint, |
| 195 | const SkMatrix& viewMatrix) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 196 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 197 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 198 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 199 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::drawPaint"); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 200 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 201 | // set rect to be big enough to fill the space, but not super-huge, so we |
| 202 | // don't overflow fixed-point implementations |
| 203 | SkRect r; |
| 204 | r.setLTRB(0, 0, |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 205 | SkIntToScalar(fRenderTarget->width()), |
| 206 | SkIntToScalar(fRenderTarget->height())); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 207 | SkTCopyOnFirstWrite<GrPaint> paint(origPaint); |
| 208 | |
| 209 | // by definition this fills the entire clip, no need for AA |
| 210 | if (paint->isAntiAlias()) { |
| 211 | paint.writable()->setAntiAlias(false); |
| 212 | } |
| 213 | |
| 214 | bool isPerspective = viewMatrix.hasPerspective(); |
| 215 | |
| 216 | // We attempt to map r by the inverse matrix and draw that. mapRect will |
| 217 | // map the four corners and bound them with a new rect. This will not |
| 218 | // produce a correct result for some perspective matrices. |
| 219 | if (!isPerspective) { |
| 220 | SkMatrix inverse; |
| 221 | if (!viewMatrix.invert(&inverse)) { |
| 222 | SkDebugf("Could not invert matrix\n"); |
| 223 | return; |
| 224 | } |
| 225 | inverse.mapRect(&r); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 226 | this->drawRect(clip, *paint, viewMatrix, r); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 227 | } else { |
| 228 | SkMatrix localMatrix; |
| 229 | if (!viewMatrix.invert(&localMatrix)) { |
| 230 | SkDebugf("Could not invert matrix\n"); |
| 231 | return; |
| 232 | } |
| 233 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 234 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 235 | |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 236 | SkAutoTUnref<GrDrawBatch> batch( |
| 237 | GrRectBatchFactory::CreateNonAAFill(paint->getColor(), SkMatrix::I(), r, nullptr, |
| 238 | &localMatrix)); |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 239 | GrPipelineBuilder pipelineBuilder(*paint, fRenderTarget, clip); |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 240 | this->getDrawTarget()->drawBatch(pipelineBuilder, batch); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 244 | static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) { |
| 245 | return point.fX >= rect.fLeft && point.fX <= rect.fRight && |
| 246 | point.fY >= rect.fTop && point.fY <= rect.fBottom; |
| 247 | } |
| 248 | |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 249 | static bool view_matrix_ok_for_aa_fill_rect(const SkMatrix& viewMatrix) { |
| 250 | return viewMatrix.preservesRightAngles(); |
| 251 | } |
| 252 | |
| 253 | static bool should_apply_coverage_aa(const GrPaint& paint, GrRenderTarget* rt) { |
| 254 | return paint.isAntiAlias() && !rt->isUnifiedMultisampled(); |
| 255 | } |
| 256 | |
robertphillips | 86c6075 | 2016-03-02 08:43:13 -0800 | [diff] [blame^] | 257 | GrDrawBatch* GrDrawContext::getFillRectBatch(const GrPaint& paint, |
| 258 | const SkMatrix& viewMatrix, |
| 259 | const SkRect& rect) { |
| 260 | |
| 261 | GrDrawBatch* batch = nullptr; |
| 262 | if (should_apply_coverage_aa(paint, fRenderTarget)) { |
| 263 | // The fill path can handle rotation but not skew. |
| 264 | if (view_matrix_ok_for_aa_fill_rect(viewMatrix)) { |
| 265 | SkRect devBoundRect; |
| 266 | viewMatrix.mapRect(&devBoundRect, rect); |
| 267 | batch = GrRectBatchFactory::CreateAAFill(paint.getColor(), viewMatrix, |
| 268 | rect, devBoundRect); |
| 269 | } |
| 270 | } else { |
| 271 | // filled BW rect |
| 272 | batch = GrRectBatchFactory::CreateNonAAFill(paint.getColor(), viewMatrix, rect, |
| 273 | nullptr, nullptr); |
| 274 | } |
| 275 | |
| 276 | return batch; |
| 277 | } |
| 278 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 279 | void GrDrawContext::drawRect(const GrClip& clip, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 280 | const GrPaint& paint, |
| 281 | const SkMatrix& viewMatrix, |
| 282 | const SkRect& rect, |
| 283 | const GrStrokeInfo* strokeInfo) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 284 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 285 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 286 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 287 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::drawRect"); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 288 | |
robertphillips | ff55b49 | 2015-11-24 07:56:59 -0800 | [diff] [blame] | 289 | // Dashing should've been devolved to a path in SkGpuDevice |
| 290 | SkASSERT(!strokeInfo || !strokeInfo->isDashed()); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 291 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 292 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 293 | |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 294 | SkScalar width = !strokeInfo ? -1 : strokeInfo->getWidth(); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 295 | |
| 296 | // Check if this is a full RT draw and can be replaced with a clear. We don't bother checking |
| 297 | // cases where the RT is fully inside a stroke. |
| 298 | if (width < 0) { |
| 299 | SkRect rtRect; |
robertphillips | 954cbc1 | 2015-12-02 10:33:46 -0800 | [diff] [blame] | 300 | fRenderTarget->getBoundsRect(&rtRect); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 301 | SkRect clipSpaceRTRect = rtRect; |
| 302 | bool checkClip = GrClip::kWideOpen_ClipType != clip.clipType(); |
| 303 | if (checkClip) { |
| 304 | clipSpaceRTRect.offset(SkIntToScalar(clip.origin().fX), |
| 305 | SkIntToScalar(clip.origin().fY)); |
| 306 | } |
| 307 | // Does the clip contain the entire RT? |
| 308 | if (!checkClip || clip.quickContains(clipSpaceRTRect)) { |
| 309 | SkMatrix invM; |
| 310 | if (!viewMatrix.invert(&invM)) { |
| 311 | return; |
| 312 | } |
| 313 | // Does the rect bound the RT? |
| 314 | SkPoint srcSpaceRTQuad[4]; |
| 315 | invM.mapRectToQuad(srcSpaceRTQuad, rtRect); |
| 316 | if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) && |
| 317 | rect_contains_inclusive(rect, srcSpaceRTQuad[1]) && |
| 318 | rect_contains_inclusive(rect, srcSpaceRTQuad[2]) && |
| 319 | rect_contains_inclusive(rect, srcSpaceRTQuad[3])) { |
| 320 | // Will it blend? |
| 321 | GrColor clearColor; |
cdalton | 1fa4572 | 2015-06-02 10:43:39 -0700 | [diff] [blame] | 322 | if (paint.isConstantBlendedColor(&clearColor)) { |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 323 | this->getDrawTarget()->clear(nullptr, clearColor, true, fRenderTarget); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 324 | return; |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 330 | bool snapToPixelCenters = false; |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 331 | SkAutoTUnref<GrDrawBatch> batch; |
robertphillips | 86c6075 | 2016-03-02 08:43:13 -0800 | [diff] [blame^] | 332 | if (width < 0) { |
| 333 | batch.reset(this->getFillRectBatch(paint, viewMatrix, rect)); |
| 334 | } else { |
| 335 | GrColor color = paint.getColor(); |
| 336 | |
| 337 | if (should_apply_coverage_aa(paint, fRenderTarget)) { |
cdalton | bb53948 | 2016-01-04 09:48:25 -0800 | [diff] [blame] | 338 | // The stroke path needs the rect to remain axis aligned (no rotation or skew). |
| 339 | if (viewMatrix.rectStaysRect()) { |
robertphillips | 86c6075 | 2016-03-02 08:43:13 -0800 | [diff] [blame^] | 340 | batch.reset(GrRectBatchFactory::CreateAAStroke(color, viewMatrix, rect, |
cdalton | bb53948 | 2016-01-04 09:48:25 -0800 | [diff] [blame] | 341 | *strokeInfo)); |
| 342 | } |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 343 | } else { |
robertphillips | 86c6075 | 2016-03-02 08:43:13 -0800 | [diff] [blame^] | 344 | // Non-AA hairlines are snapped to pixel centers to make which pixels are hit |
| 345 | // deterministic |
| 346 | snapToPixelCenters = (0 == width && !fRenderTarget->isUnifiedMultisampled()); |
| 347 | batch.reset(GrRectBatchFactory::CreateNonAAStroke(color, viewMatrix, rect, |
| 348 | width, snapToPixelCenters)); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 349 | |
robertphillips | 86c6075 | 2016-03-02 08:43:13 -0800 | [diff] [blame^] | 350 | // Depending on sub-pixel coordinates and the particular GPU, we may lose a corner of |
| 351 | // hairline rects. We jam all the vertices to pixel centers to avoid this, but not |
| 352 | // when MSAA is enabled because it can cause ugly artifacts. |
| 353 | } |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 354 | } |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 355 | |
| 356 | if (batch) { |
| 357 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
| 358 | |
| 359 | if (snapToPixelCenters) { |
| 360 | pipelineBuilder.setState(GrPipelineBuilder::kSnapVerticesToPixelCenters_Flag, |
| 361 | snapToPixelCenters); |
| 362 | } |
| 363 | |
| 364 | this->getDrawTarget()->drawBatch(pipelineBuilder, batch); |
| 365 | return; |
| 366 | } |
| 367 | |
| 368 | SkPath path; |
| 369 | path.setIsVolatile(true); |
| 370 | path.addRect(rect); |
| 371 | this->internalDrawPath(clip, paint, viewMatrix, path, |
| 372 | strokeInfo ? *strokeInfo : GrStrokeInfo::FillInfo()); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 373 | } |
| 374 | |
robertphillips | 86c6075 | 2016-03-02 08:43:13 -0800 | [diff] [blame^] | 375 | bool GrDrawContextPriv::drawAndStencilRect(const SkIRect* scissorRect, |
| 376 | const GrStencilSettings& ss, |
| 377 | SkRegion::Op op, |
| 378 | bool invert, |
| 379 | bool doAA, |
| 380 | const SkMatrix& viewMatrix, |
| 381 | const SkRect& rect) { |
| 382 | ASSERT_SINGLE_OWNER_PRIV |
| 383 | RETURN_FALSE_IF_ABANDONED_PRIV |
| 384 | SkDEBUGCODE(fDrawContext->validate();) |
| 385 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::stencilRect"); |
| 386 | |
| 387 | AutoCheckFlush acf(fDrawContext->fDrawingManager); |
| 388 | |
| 389 | GrPaint paint; |
| 390 | paint.setAntiAlias(doAA); |
| 391 | paint.setCoverageSetOpXPFactory(op, invert); |
| 392 | |
| 393 | SkAutoTUnref<GrDrawBatch> batch(fDrawContext->getFillRectBatch(paint, viewMatrix, rect)); |
| 394 | if (batch) { |
| 395 | GrPipelineBuilder pipelineBuilder(paint, fDrawContext->fRenderTarget, GrClip::WideOpen()); |
| 396 | pipelineBuilder.setStencil(ss); |
| 397 | |
| 398 | fDrawContext->getDrawTarget()->drawBatch(pipelineBuilder, batch, scissorRect); |
| 399 | return true; |
| 400 | } |
| 401 | |
| 402 | SkPath path; |
| 403 | path.setIsVolatile(true); |
| 404 | path.addRect(rect); |
| 405 | return this->drawAndStencilPath(scissorRect, ss, op, invert, doAA, viewMatrix, path); |
| 406 | } |
| 407 | |
bsalomon | a2e69fc | 2015-11-05 10:41:43 -0800 | [diff] [blame] | 408 | void GrDrawContext::fillRectToRect(const GrClip& clip, |
| 409 | const GrPaint& paint, |
| 410 | const SkMatrix& viewMatrix, |
| 411 | const SkRect& rectToDraw, |
| 412 | const SkRect& localRect) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 413 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 414 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 415 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 416 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::fillRectToRect"); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 417 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 418 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 419 | |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 420 | SkAutoTUnref<GrDrawBatch> batch; |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 421 | if (should_apply_coverage_aa(paint, fRenderTarget) && |
| 422 | view_matrix_ok_for_aa_fill_rect(viewMatrix)) { |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 423 | batch.reset(GrAAFillRectBatch::CreateWithLocalRect(paint.getColor(), viewMatrix, rectToDraw, |
| 424 | localRect)); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 425 | } else { |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 426 | batch.reset(GrRectBatchFactory::CreateNonAAFill(paint.getColor(), viewMatrix, rectToDraw, |
| 427 | &localRect, nullptr)); |
| 428 | } |
| 429 | |
| 430 | if (batch) { |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 431 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 432 | this->drawBatch(&pipelineBuilder, batch); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 433 | } |
joshualitt | b6b513b | 2015-08-21 10:25:18 -0700 | [diff] [blame] | 434 | } |
| 435 | |
bsalomon | a2e69fc | 2015-11-05 10:41:43 -0800 | [diff] [blame] | 436 | void GrDrawContext::fillRectWithLocalMatrix(const GrClip& clip, |
| 437 | const GrPaint& paint, |
| 438 | const SkMatrix& viewMatrix, |
| 439 | const SkRect& rectToDraw, |
| 440 | const SkMatrix& localMatrix) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 441 | ASSERT_SINGLE_OWNER |
joshualitt | b6b513b | 2015-08-21 10:25:18 -0700 | [diff] [blame] | 442 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 443 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 444 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::fillRectWithLocalMatrix"); |
joshualitt | b6b513b | 2015-08-21 10:25:18 -0700 | [diff] [blame] | 445 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 446 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 447 | |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 448 | SkAutoTUnref<GrDrawBatch> batch; |
robertphillips | 954cbc1 | 2015-12-02 10:33:46 -0800 | [diff] [blame] | 449 | if (should_apply_coverage_aa(paint, fRenderTarget) && |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 450 | view_matrix_ok_for_aa_fill_rect(viewMatrix)) { |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 451 | batch.reset(GrAAFillRectBatch::Create(paint.getColor(), viewMatrix, localMatrix, |
| 452 | rectToDraw)); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 453 | } else { |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 454 | batch.reset(GrRectBatchFactory::CreateNonAAFill(paint.getColor(), viewMatrix, rectToDraw, |
| 455 | nullptr, &localMatrix)); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 456 | } |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 457 | |
| 458 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 459 | this->getDrawTarget()->drawBatch(pipelineBuilder, batch); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 460 | } |
| 461 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 462 | void GrDrawContext::drawVertices(const GrClip& clip, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 463 | const GrPaint& paint, |
| 464 | const SkMatrix& viewMatrix, |
| 465 | GrPrimitiveType primitiveType, |
| 466 | int vertexCount, |
| 467 | const SkPoint positions[], |
| 468 | const SkPoint texCoords[], |
| 469 | const GrColor colors[], |
| 470 | const uint16_t indices[], |
| 471 | int indexCount) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 472 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 473 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 474 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 475 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::drawVertices"); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 476 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 477 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 478 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 479 | // TODO clients should give us bounds |
| 480 | SkRect bounds; |
| 481 | if (!bounds.setBoundsCheck(positions, vertexCount)) { |
| 482 | SkDebugf("drawVertices call empty bounds\n"); |
| 483 | return; |
| 484 | } |
| 485 | |
| 486 | viewMatrix.mapRect(&bounds); |
| 487 | |
| 488 | // 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] | 489 | // snapping. We also do this for the "hair" primitive types: lines and points since they have |
| 490 | // a 1 pixel thickness in device space. |
| 491 | if (!paint.isAntiAlias() || GrIsPrimTypeLines(primitiveType) || |
| 492 | kPoints_GrPrimitiveType == primitiveType) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 493 | bounds.outset(0.5f, 0.5f); |
| 494 | } |
| 495 | |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 496 | GrDrawVerticesBatch::Geometry geometry; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 497 | geometry.fColor = paint.getColor(); |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 498 | SkAutoTUnref<GrDrawBatch> batch(GrDrawVerticesBatch::Create(geometry, primitiveType, viewMatrix, |
| 499 | positions, vertexCount, indices, |
| 500 | indexCount, colors, texCoords, |
| 501 | bounds)); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 502 | |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 503 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 504 | this->getDrawTarget()->drawBatch(pipelineBuilder, batch); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | /////////////////////////////////////////////////////////////////////////////// |
| 508 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 509 | void GrDrawContext::drawAtlas(const GrClip& clip, |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 510 | const GrPaint& paint, |
| 511 | const SkMatrix& viewMatrix, |
| 512 | int spriteCount, |
| 513 | const SkRSXform xform[], |
| 514 | const SkRect texRect[], |
| 515 | const SkColor colors[]) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 516 | ASSERT_SINGLE_OWNER |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 517 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 518 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 519 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::drawAtlas"); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 520 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 521 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 522 | |
jvanverth | 14b8803 | 2015-08-07 12:18:54 -0700 | [diff] [blame] | 523 | GrDrawAtlasBatch::Geometry geometry; |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 524 | geometry.fColor = paint.getColor(); |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 525 | SkAutoTUnref<GrDrawBatch> batch(GrDrawAtlasBatch::Create(geometry, viewMatrix, spriteCount, |
| 526 | xform, texRect, colors)); |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 527 | |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 528 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 529 | this->getDrawTarget()->drawBatch(pipelineBuilder, batch); |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | /////////////////////////////////////////////////////////////////////////////// |
| 533 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 534 | void GrDrawContext::drawRRect(const GrClip& clip, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 535 | const GrPaint& paint, |
| 536 | const SkMatrix& viewMatrix, |
| 537 | const SkRRect& rrect, |
| 538 | const GrStrokeInfo& strokeInfo) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 539 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 540 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 541 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 542 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::drawRRect"); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 543 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 544 | if (rrect.isEmpty()) { |
| 545 | return; |
| 546 | } |
| 547 | |
robertphillips | 514450c | 2015-11-24 05:36:02 -0800 | [diff] [blame] | 548 | SkASSERT(!strokeInfo.isDashed()); // this should've been devolved to a path in SkGpuDevice |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 549 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 550 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 551 | |
robertphillips | b56f927 | 2016-02-25 11:03:52 -0800 | [diff] [blame] | 552 | if (should_apply_coverage_aa(paint, fRenderTarget)) { |
| 553 | GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps(); |
| 554 | |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 555 | SkAutoTUnref<GrDrawBatch> batch(GrOvalRenderer::CreateRRectBatch(paint.getColor(), |
robertphillips | b56f927 | 2016-02-25 11:03:52 -0800 | [diff] [blame] | 556 | viewMatrix, |
| 557 | rrect, |
| 558 | strokeInfo, |
| 559 | shaderCaps)); |
| 560 | if (batch) { |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 561 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
robertphillips | b56f927 | 2016-02-25 11:03:52 -0800 | [diff] [blame] | 562 | this->getDrawTarget()->drawBatch(pipelineBuilder, batch); |
| 563 | return; |
| 564 | } |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 565 | } |
robertphillips | b56f927 | 2016-02-25 11:03:52 -0800 | [diff] [blame] | 566 | |
| 567 | SkPath path; |
| 568 | path.setIsVolatile(true); |
| 569 | path.addRRect(rrect); |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 570 | this->internalDrawPath(clip, paint, viewMatrix, path, strokeInfo); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 571 | } |
| 572 | |
robertphillips | 0009589 | 2016-02-29 13:50:40 -0800 | [diff] [blame] | 573 | bool GrDrawContext::drawFilledDRRect(const GrClip& clip, |
| 574 | const GrPaint& paintIn, |
| 575 | const SkMatrix& viewMatrix, |
| 576 | const SkRRect& origOuter, |
| 577 | const SkRRect& origInner) { |
| 578 | SkASSERT(!origInner.isEmpty()); |
| 579 | SkASSERT(!origOuter.isEmpty()); |
| 580 | |
| 581 | bool applyAA = paintIn.isAntiAlias() && !fRenderTarget->isUnifiedMultisampled(); |
| 582 | |
| 583 | GrPrimitiveEdgeType innerEdgeType = applyAA ? kInverseFillAA_GrProcessorEdgeType : |
| 584 | kInverseFillBW_GrProcessorEdgeType; |
| 585 | GrPrimitiveEdgeType outerEdgeType = applyAA ? kFillAA_GrProcessorEdgeType : |
| 586 | kFillBW_GrProcessorEdgeType; |
| 587 | |
| 588 | SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter); |
| 589 | SkMatrix inverseVM; |
| 590 | if (!viewMatrix.isIdentity()) { |
| 591 | if (!origInner.transform(viewMatrix, inner.writable())) { |
| 592 | return false; |
| 593 | } |
| 594 | if (!origOuter.transform(viewMatrix, outer.writable())) { |
| 595 | return false; |
| 596 | } |
| 597 | if (!viewMatrix.invert(&inverseVM)) { |
| 598 | return false; |
| 599 | } |
| 600 | } else { |
| 601 | inverseVM.reset(); |
| 602 | } |
| 603 | |
| 604 | GrPaint grPaint(paintIn); |
| 605 | grPaint.setAntiAlias(false); |
| 606 | |
| 607 | // TODO these need to be a geometry processors |
| 608 | SkAutoTUnref<GrFragmentProcessor> innerEffect(GrRRectEffect::Create(innerEdgeType, *inner)); |
| 609 | if (!innerEffect) { |
| 610 | return false; |
| 611 | } |
| 612 | |
| 613 | SkAutoTUnref<GrFragmentProcessor> outerEffect(GrRRectEffect::Create(outerEdgeType, *outer)); |
| 614 | if (!outerEffect) { |
| 615 | return false; |
| 616 | } |
| 617 | |
| 618 | grPaint.addCoverageFragmentProcessor(innerEffect); |
| 619 | grPaint.addCoverageFragmentProcessor(outerEffect); |
| 620 | |
| 621 | SkRect bounds = outer->getBounds(); |
| 622 | if (applyAA) { |
| 623 | bounds.outset(SK_ScalarHalf, SK_ScalarHalf); |
| 624 | } |
| 625 | |
| 626 | this->fillRectWithLocalMatrix(clip, grPaint, SkMatrix::I(), bounds, inverseVM); |
| 627 | return true; |
| 628 | } |
| 629 | |
| 630 | void GrDrawContext::drawDRRect(const GrClip& clip, |
| 631 | const GrPaint& paint, |
| 632 | const SkMatrix& viewMatrix, |
| 633 | const SkRRect& outer, |
| 634 | const SkRRect& inner) { |
| 635 | ASSERT_SINGLE_OWNER |
| 636 | RETURN_IF_ABANDONED |
| 637 | SkDEBUGCODE(this->validate();) |
| 638 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::drawDRRect"); |
| 639 | |
| 640 | SkASSERT(!outer.isEmpty()); |
| 641 | SkASSERT(!inner.isEmpty()); |
| 642 | |
| 643 | AutoCheckFlush acf(fDrawingManager); |
| 644 | |
| 645 | if (this->drawFilledDRRect(clip, paint, viewMatrix, outer, inner)) { |
| 646 | return; |
| 647 | } |
| 648 | |
| 649 | SkPath path; |
| 650 | path.setIsVolatile(true); |
| 651 | path.addRRect(inner); |
| 652 | path.addRRect(outer); |
| 653 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 654 | |
| 655 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 656 | this->internalDrawPath(clip, paint, viewMatrix, path, GrStrokeInfo::FillInfo()); |
robertphillips | 0009589 | 2016-02-29 13:50:40 -0800 | [diff] [blame] | 657 | } |
| 658 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 659 | /////////////////////////////////////////////////////////////////////////////// |
| 660 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 661 | void GrDrawContext::drawOval(const GrClip& clip, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 662 | const GrPaint& paint, |
| 663 | const SkMatrix& viewMatrix, |
| 664 | const SkRect& oval, |
| 665 | const GrStrokeInfo& strokeInfo) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 666 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 667 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 668 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 669 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::drawOval"); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 670 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 671 | if (oval.isEmpty()) { |
| 672 | return; |
| 673 | } |
| 674 | |
robertphillips | 514450c | 2015-11-24 05:36:02 -0800 | [diff] [blame] | 675 | SkASSERT(!strokeInfo.isDashed()); // this should've been devolved to a path in SkGpuDevice |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 676 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 677 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 678 | |
robertphillips | b56f927 | 2016-02-25 11:03:52 -0800 | [diff] [blame] | 679 | if (should_apply_coverage_aa(paint, fRenderTarget)) { |
| 680 | GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps(); |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 681 | SkAutoTUnref<GrDrawBatch> batch(GrOvalRenderer::CreateOvalBatch(paint.getColor(), |
robertphillips | b56f927 | 2016-02-25 11:03:52 -0800 | [diff] [blame] | 682 | viewMatrix, |
| 683 | oval, |
| 684 | strokeInfo, |
| 685 | shaderCaps)); |
| 686 | if (batch) { |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 687 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
robertphillips | b56f927 | 2016-02-25 11:03:52 -0800 | [diff] [blame] | 688 | this->getDrawTarget()->drawBatch(pipelineBuilder, batch); |
| 689 | return; |
| 690 | } |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 691 | } |
robertphillips | b56f927 | 2016-02-25 11:03:52 -0800 | [diff] [blame] | 692 | |
| 693 | SkPath path; |
| 694 | path.setIsVolatile(true); |
| 695 | path.addOval(oval); |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 696 | this->internalDrawPath(clip, paint, viewMatrix, path, strokeInfo); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 697 | } |
| 698 | |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 699 | void GrDrawContext::drawImageNine(const GrClip& clip, |
| 700 | const GrPaint& paint, |
| 701 | const SkMatrix& viewMatrix, |
| 702 | int imageWidth, |
| 703 | int imageHeight, |
| 704 | const SkIRect& center, |
| 705 | const SkRect& dst) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 706 | ASSERT_SINGLE_OWNER |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 707 | RETURN_IF_ABANDONED |
| 708 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 709 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::drawImageNine"); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 710 | |
| 711 | AutoCheckFlush acf(fDrawingManager); |
| 712 | |
| 713 | SkAutoTUnref<GrDrawBatch> batch(GrNinePatch::CreateNonAA(paint.getColor(), viewMatrix, |
| 714 | imageWidth, imageHeight, |
| 715 | center, dst)); |
| 716 | |
| 717 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
| 718 | this->getDrawTarget()->drawBatch(pipelineBuilder, batch); |
| 719 | } |
| 720 | |
| 721 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 722 | // Can 'path' be drawn as a pair of filled nested rectangles? |
joshualitt | f9c5db2 | 2015-07-10 11:31:01 -0700 | [diff] [blame] | 723 | static bool is_nested_rects(const SkMatrix& viewMatrix, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 724 | const SkPath& path, |
| 725 | const SkStrokeRec& stroke, |
| 726 | SkRect rects[2]) { |
| 727 | SkASSERT(stroke.isFillStyle()); |
| 728 | |
| 729 | if (path.isInverseFillType()) { |
| 730 | return false; |
| 731 | } |
| 732 | |
| 733 | // TODO: this restriction could be lifted if we were willing to apply |
| 734 | // the matrix to all the points individually rather than just to the rect |
robertphillips | 0e7029e | 2015-11-30 05:45:06 -0800 | [diff] [blame] | 735 | if (!viewMatrix.rectStaysRect()) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 736 | return false; |
| 737 | } |
| 738 | |
| 739 | SkPath::Direction dirs[2]; |
| 740 | if (!path.isNestedFillRects(rects, dirs)) { |
| 741 | return false; |
| 742 | } |
| 743 | |
| 744 | if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) { |
| 745 | // The two rects need to be wound opposite to each other |
| 746 | return false; |
| 747 | } |
| 748 | |
| 749 | // Right now, nested rects where the margin is not the same width |
| 750 | // all around do not render correctly |
| 751 | const SkScalar* outer = rects[0].asScalars(); |
| 752 | const SkScalar* inner = rects[1].asScalars(); |
| 753 | |
| 754 | bool allEq = true; |
| 755 | |
| 756 | SkScalar margin = SkScalarAbs(outer[0] - inner[0]); |
| 757 | bool allGoE1 = margin >= SK_Scalar1; |
| 758 | |
| 759 | for (int i = 1; i < 4; ++i) { |
| 760 | SkScalar temp = SkScalarAbs(outer[i] - inner[i]); |
| 761 | if (temp < SK_Scalar1) { |
| 762 | allGoE1 = false; |
| 763 | } |
| 764 | if (!SkScalarNearlyEqual(margin, temp)) { |
| 765 | allEq = false; |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | return allEq || allGoE1; |
| 770 | } |
| 771 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 772 | void GrDrawContext::drawBatch(const GrClip& clip, |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 773 | const GrPaint& paint, GrDrawBatch* batch) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 774 | ASSERT_SINGLE_OWNER |
joshualitt | b7ee1bf | 2015-08-10 11:59:02 -0700 | [diff] [blame] | 775 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 776 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 777 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::drawBatch"); |
joshualitt | b7ee1bf | 2015-08-10 11:59:02 -0700 | [diff] [blame] | 778 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 779 | AutoCheckFlush acf(fDrawingManager); |
joshualitt | b7ee1bf | 2015-08-10 11:59:02 -0700 | [diff] [blame] | 780 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 781 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 782 | this->getDrawTarget()->drawBatch(pipelineBuilder, batch); |
joshualitt | b7ee1bf | 2015-08-10 11:59:02 -0700 | [diff] [blame] | 783 | } |
| 784 | |
cdalton | 8ff8d24 | 2015-12-08 10:20:32 -0800 | [diff] [blame] | 785 | void GrDrawContext::drawPathBatch(const GrPipelineBuilder& pipelineBuilder, |
| 786 | GrDrawPathBatchBase* batch) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 787 | ASSERT_SINGLE_OWNER |
cdalton | 8ff8d24 | 2015-12-08 10:20:32 -0800 | [diff] [blame] | 788 | RETURN_IF_ABANDONED |
| 789 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 790 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::drawPathBatch"); |
cdalton | 8ff8d24 | 2015-12-08 10:20:32 -0800 | [diff] [blame] | 791 | |
| 792 | AutoCheckFlush acf(fDrawingManager); |
| 793 | |
| 794 | this->getDrawTarget()->drawPathBatch(pipelineBuilder, batch); |
| 795 | } |
| 796 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 797 | void GrDrawContext::drawPath(const GrClip& clip, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 798 | const GrPaint& paint, |
| 799 | const SkMatrix& viewMatrix, |
| 800 | const SkPath& path, |
| 801 | const GrStrokeInfo& strokeInfo) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 802 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 803 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 804 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 805 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::drawPath"); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 806 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 807 | if (path.isEmpty()) { |
| 808 | if (path.isInverseFillType()) { |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 809 | this->drawPaint(clip, paint, viewMatrix); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 810 | } |
| 811 | return; |
| 812 | } |
| 813 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 814 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 815 | |
robertphillips | b56f927 | 2016-02-25 11:03:52 -0800 | [diff] [blame] | 816 | if (should_apply_coverage_aa(paint, fRenderTarget) && !strokeInfo.isDashed()) { |
| 817 | if (strokeInfo.getWidth() < 0 && !path.isConvex()) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 818 | // Concave AA paths are expensive - try to avoid them for special cases |
| 819 | SkRect rects[2]; |
| 820 | |
joshualitt | f9c5db2 | 2015-07-10 11:31:01 -0700 | [diff] [blame] | 821 | if (is_nested_rects(viewMatrix, path, strokeInfo, rects)) { |
joshualitt | d2b23e0 | 2015-08-21 10:53:34 -0700 | [diff] [blame] | 822 | SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateAAFillNestedRects( |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 823 | paint.getColor(), viewMatrix, rects)); |
| 824 | |
| 825 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 826 | this->getDrawTarget()->drawBatch(pipelineBuilder, batch); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 827 | return; |
| 828 | } |
| 829 | } |
| 830 | SkRect ovalRect; |
| 831 | bool isOval = path.isOval(&ovalRect); |
| 832 | |
| 833 | if (isOval && !path.isInverseFillType()) { |
robertphillips | 0cc2f85 | 2016-02-24 13:36:56 -0800 | [diff] [blame] | 834 | GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps(); |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 835 | SkAutoTUnref<GrDrawBatch> batch(GrOvalRenderer::CreateOvalBatch(paint.getColor(), |
robertphillips | 0cc2f85 | 2016-02-24 13:36:56 -0800 | [diff] [blame] | 836 | viewMatrix, |
robertphillips | 0cc2f85 | 2016-02-24 13:36:56 -0800 | [diff] [blame] | 837 | ovalRect, |
| 838 | strokeInfo, |
| 839 | shaderCaps)); |
| 840 | if (batch) { |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 841 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
robertphillips | 0cc2f85 | 2016-02-24 13:36:56 -0800 | [diff] [blame] | 842 | this->getDrawTarget()->drawBatch(pipelineBuilder, batch); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 843 | return; |
| 844 | } |
| 845 | } |
| 846 | } |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 847 | |
| 848 | // Note that internalDrawPath may sw-rasterize the path into a scratch texture. |
| 849 | // Scratch textures can be recycled after they are returned to the texture |
| 850 | // cache. This presents a potential hazard for buffered drawing. However, |
| 851 | // the writePixels that uploads to the scratch will perform a flush so we're |
| 852 | // OK. |
| 853 | this->internalDrawPath(clip, paint, viewMatrix, path, strokeInfo); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 854 | } |
| 855 | |
robertphillips | 86c6075 | 2016-03-02 08:43:13 -0800 | [diff] [blame^] | 856 | bool GrDrawContextPriv::drawAndStencilPath(const SkIRect* scissorRect, |
| 857 | const GrStencilSettings& ss, |
| 858 | SkRegion::Op op, |
| 859 | bool invert, |
| 860 | bool doAA, |
| 861 | const SkMatrix& viewMatrix, |
| 862 | const SkPath& path) { |
| 863 | ASSERT_SINGLE_OWNER_PRIV |
| 864 | RETURN_FALSE_IF_ABANDONED_PRIV |
| 865 | SkDEBUGCODE(fDrawContext->validate();) |
| 866 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::drawPath"); |
| 867 | |
| 868 | if (path.isEmpty() && path.isInverseFillType()) { |
| 869 | this->drawAndStencilRect(scissorRect, ss, op, invert, false, SkMatrix::I(), |
| 870 | SkRect::MakeIWH(fDrawContext->fRenderTarget->width(), |
| 871 | fDrawContext->fRenderTarget->height())); |
| 872 | return true; |
| 873 | } |
| 874 | |
| 875 | AutoCheckFlush acf(fDrawContext->fDrawingManager); |
| 876 | |
| 877 | // An Assumption here is that path renderer would use some form of tweaking |
| 878 | // the src color (either the input alpha or in the frag shader) to implement |
| 879 | // aa. If we have some future driver-mojo path AA that can do the right |
| 880 | // thing WRT to the blend then we'll need some query on the PR. |
| 881 | bool useCoverageAA = doAA && !fDrawContext->fRenderTarget->isUnifiedMultisampled(); |
| 882 | bool isStencilDisabled = true; |
| 883 | bool isStencilBufferMSAA = fDrawContext->fRenderTarget->isStencilBufferMultisampled(); |
| 884 | |
| 885 | const GrPathRendererChain::DrawType type = |
| 886 | useCoverageAA ? GrPathRendererChain::kColorAntiAlias_DrawType |
| 887 | : GrPathRendererChain::kColor_DrawType; |
| 888 | |
| 889 | GrPathRenderer::CanDrawPathArgs canDrawArgs; |
| 890 | canDrawArgs.fShaderCaps = fDrawContext->fDrawingManager->getContext()->caps()->shaderCaps(); |
| 891 | canDrawArgs.fViewMatrix = &viewMatrix; |
| 892 | canDrawArgs.fPath = &path; |
| 893 | canDrawArgs.fStroke = &GrStrokeInfo::FillInfo(); |
| 894 | canDrawArgs.fAntiAlias = useCoverageAA; |
| 895 | canDrawArgs.fIsStencilDisabled = isStencilDisabled; |
| 896 | canDrawArgs.fIsStencilBufferMSAA = isStencilBufferMSAA; |
| 897 | |
| 898 | // Don't allow the SW renderer |
| 899 | GrPathRenderer* pr = fDrawContext->fDrawingManager->getPathRenderer(canDrawArgs, false, type); |
| 900 | if (!pr) { |
| 901 | return false; |
| 902 | } |
| 903 | |
| 904 | GrPaint paint; |
| 905 | paint.setCoverageSetOpXPFactory(op, invert); |
| 906 | |
| 907 | // TODO: it is unfortunate that we have to convert this to a GrClip to |
| 908 | // call drawPath. |
| 909 | GrClip clip; |
| 910 | if (scissorRect) { |
| 911 | clip.setIRect(*scissorRect); |
| 912 | } |
| 913 | |
| 914 | GrPipelineBuilder pipelineBuilder(paint, fDrawContext->fRenderTarget, clip); |
| 915 | pipelineBuilder.setStencil(ss); |
| 916 | |
| 917 | GrPathRenderer::DrawPathArgs args; |
| 918 | args.fTarget = fDrawContext->getDrawTarget(); |
| 919 | args.fResourceProvider = fDrawContext->fDrawingManager->getContext()->resourceProvider(); |
| 920 | args.fPipelineBuilder = &pipelineBuilder; |
| 921 | args.fColor = GrColor_WHITE; |
| 922 | args.fViewMatrix = &viewMatrix; |
| 923 | args.fPath = &path; |
| 924 | args.fStroke = &GrStrokeInfo::FillInfo(); |
| 925 | args.fAntiAlias = useCoverageAA; |
| 926 | pr->drawPath(args); |
| 927 | return true; |
| 928 | } |
| 929 | |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 930 | void GrDrawContext::internalDrawPath(const GrClip& clip, |
| 931 | const GrPaint& paint, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 932 | const SkMatrix& viewMatrix, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 933 | const SkPath& path, |
| 934 | const GrStrokeInfo& strokeInfo) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 935 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 936 | RETURN_IF_ABANDONED |
| 937 | SkASSERT(!path.isEmpty()); |
| 938 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 939 | // An Assumption here is that path renderer would use some form of tweaking |
| 940 | // the src color (either the input alpha or in the frag shader) to implement |
| 941 | // aa. If we have some future driver-mojo path AA that can do the right |
| 942 | // thing WRT to the blend then we'll need some query on the PR. |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 943 | bool useCoverageAA = should_apply_coverage_aa(paint, fRenderTarget); |
| 944 | const bool isStencilDisabled = true; |
| 945 | bool isStencilBufferMSAA = fRenderTarget->isStencilBufferMultisampled(); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 946 | |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 947 | const GrPathRendererChain::DrawType type = |
| 948 | useCoverageAA ? GrPathRendererChain::kColorAntiAlias_DrawType |
| 949 | : GrPathRendererChain::kColor_DrawType; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 950 | |
| 951 | const SkPath* pathPtr = &path; |
| 952 | SkTLazy<SkPath> tmpPath; |
| 953 | const GrStrokeInfo* strokeInfoPtr = &strokeInfo; |
| 954 | |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 955 | GrPathRenderer::CanDrawPathArgs canDrawArgs; |
| 956 | canDrawArgs.fShaderCaps = fDrawingManager->getContext()->caps()->shaderCaps(); |
| 957 | canDrawArgs.fViewMatrix = &viewMatrix; |
| 958 | canDrawArgs.fPath = pathPtr; |
| 959 | canDrawArgs.fStroke = strokeInfoPtr; |
| 960 | canDrawArgs.fAntiAlias = useCoverageAA; |
| 961 | canDrawArgs.fIsStencilDisabled = isStencilDisabled; |
| 962 | canDrawArgs.fIsStencilBufferMSAA = isStencilBufferMSAA; |
| 963 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 964 | // 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] | 965 | GrPathRenderer* pr = fDrawingManager->getPathRenderer(canDrawArgs, false, type); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 966 | |
| 967 | GrStrokeInfo dashlessStrokeInfo(strokeInfo, false); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 968 | if (nullptr == pr && strokeInfo.isDashed()) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 969 | // It didn't work above, so try again with dashed stroke converted to a dashless stroke. |
| 970 | if (!strokeInfo.applyDashToPath(tmpPath.init(), &dashlessStrokeInfo, *pathPtr)) { |
| 971 | return; |
| 972 | } |
| 973 | pathPtr = tmpPath.get(); |
| 974 | if (pathPtr->isEmpty()) { |
| 975 | return; |
| 976 | } |
| 977 | strokeInfoPtr = &dashlessStrokeInfo; |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 978 | |
| 979 | canDrawArgs.fPath = pathPtr; |
| 980 | canDrawArgs.fStroke = strokeInfoPtr; |
| 981 | |
| 982 | pr = fDrawingManager->getPathRenderer(canDrawArgs, false, type); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 983 | } |
| 984 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 985 | if (nullptr == pr) { |
| 986 | if (!GrPathRenderer::IsStrokeHairlineOrEquivalent(*strokeInfoPtr, viewMatrix, nullptr) && |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 987 | !strokeInfoPtr->isFillStyle()) { |
| 988 | // It didn't work above, so try again with stroke converted to a fill. |
| 989 | if (!tmpPath.isValid()) { |
| 990 | tmpPath.init(); |
| 991 | } |
| 992 | dashlessStrokeInfo.setResScale(SkScalarAbs(viewMatrix.getMaxScale())); |
| 993 | if (!dashlessStrokeInfo.applyToPath(tmpPath.get(), *pathPtr)) { |
| 994 | return; |
| 995 | } |
| 996 | pathPtr = tmpPath.get(); |
| 997 | if (pathPtr->isEmpty()) { |
| 998 | return; |
| 999 | } |
| 1000 | dashlessStrokeInfo.setFillStyle(); |
| 1001 | strokeInfoPtr = &dashlessStrokeInfo; |
| 1002 | } |
| 1003 | |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 1004 | canDrawArgs.fPath = pathPtr; |
| 1005 | canDrawArgs.fStroke = strokeInfoPtr; |
| 1006 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1007 | // This time, allow SW renderer |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 1008 | pr = fDrawingManager->getPathRenderer(canDrawArgs, true, type); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1011 | if (nullptr == pr) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1012 | #ifdef SK_DEBUG |
| 1013 | SkDebugf("Unable to find path renderer compatible with path.\n"); |
| 1014 | #endif |
| 1015 | return; |
| 1016 | } |
| 1017 | |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 1018 | GrPipelineBuilder pipelineBuilder(paint, fRenderTarget, clip); |
| 1019 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 1020 | GrPathRenderer::DrawPathArgs args; |
robertphillips | b83bec5 | 2015-10-23 09:38:03 -0700 | [diff] [blame] | 1021 | args.fTarget = this->getDrawTarget(); |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 1022 | args.fResourceProvider = fDrawingManager->getContext()->resourceProvider(); |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 1023 | args.fPipelineBuilder = &pipelineBuilder; |
| 1024 | args.fColor = paint.getColor(); |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 1025 | args.fViewMatrix = &viewMatrix; |
| 1026 | args.fPath = pathPtr; |
| 1027 | args.fStroke = strokeInfoPtr; |
| 1028 | args.fAntiAlias = useCoverageAA; |
| 1029 | pr->drawPath(args); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 1032 | void GrDrawContext::drawBatch(GrPipelineBuilder* pipelineBuilder, GrDrawBatch* batch) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1033 | ASSERT_SINGLE_OWNER |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 1034 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 1035 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 1036 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::drawBatch"); |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 1037 | |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 1038 | this->getDrawTarget()->drawBatch(*pipelineBuilder, batch); |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 1039 | } |