robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 8 | #include "GrBatchTest.h" |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 9 | #include "GrColor.h" |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 10 | #include "GrDrawContext.h" |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 11 | #include "GrDrawContextPriv.h" |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 12 | #include "GrDrawingManager.h" |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 13 | #include "GrOvalRenderer.h" |
| 14 | #include "GrPathRenderer.h" |
robertphillips | 5fa7f30 | 2016-07-21 09:21:04 -0700 | [diff] [blame] | 15 | #include "GrPipelineBuilder.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" |
robertphillips | 9199a9f | 2016-07-13 07:48:43 -0700 | [diff] [blame] | 22 | #include "batches/GrClearBatch.h" |
jvanverth | 14b8803 | 2015-08-07 12:18:54 -0700 | [diff] [blame] | 23 | #include "batches/GrDrawAtlasBatch.h" |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 24 | #include "batches/GrDrawVerticesBatch.h" |
joshualitt | 7fc2a26 | 2015-08-10 10:30:14 -0700 | [diff] [blame] | 25 | #include "batches/GrRectBatchFactory.h" |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 26 | #include "batches/GrNinePatch.h" // TODO Factory |
joshualitt | 7441782 | 2015-08-07 11:42:16 -0700 | [diff] [blame] | 27 | |
robertphillips | 0009589 | 2016-02-29 13:50:40 -0800 | [diff] [blame] | 28 | #include "effects/GrRRectEffect.h" |
| 29 | |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 30 | #include "instanced/InstancedRendering.h" |
| 31 | |
joshualitt | e804292 | 2015-12-11 06:11:21 -0800 | [diff] [blame] | 32 | #include "text/GrAtlasTextContext.h" |
| 33 | #include "text/GrStencilAndCoverTextContext.h" |
| 34 | |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 35 | #include "../private/GrAuditTrail.h" |
| 36 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 37 | #define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == fDrawingManager->getContext()) |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 38 | #define ASSERT_SINGLE_OWNER \ |
joshualitt | de8dc7e | 2016-01-08 10:09:13 -0800 | [diff] [blame] | 39 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);) |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 40 | #define ASSERT_SINGLE_OWNER_PRIV \ |
| 41 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fDrawContext->fSingleOwner);) |
robertphillips | 7761d61 | 2016-05-16 09:14:53 -0700 | [diff] [blame] | 42 | #define RETURN_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return; } |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 43 | #define RETURN_IF_ABANDONED_PRIV if (fDrawContext->fDrawingManager->wasAbandoned()) { return; } |
robertphillips | 7761d61 | 2016-05-16 09:14:53 -0700 | [diff] [blame] | 44 | #define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return false; } |
| 45 | #define RETURN_FALSE_IF_ABANDONED_PRIV if (fDrawContext->fDrawingManager->wasAbandoned()) { return false; } |
| 46 | #define RETURN_NULL_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return nullptr; } |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 47 | |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 48 | using gr_instanced::InstancedRendering; |
| 49 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 50 | class AutoCheckFlush { |
| 51 | public: |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 52 | AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) { |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 53 | SkASSERT(fDrawingManager); |
| 54 | } |
| 55 | ~AutoCheckFlush() { fDrawingManager->getContext()->flushIfNecessary(); } |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 56 | |
| 57 | private: |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 58 | GrDrawingManager* fDrawingManager; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
robertphillips | 7761d61 | 2016-05-16 09:14:53 -0700 | [diff] [blame] | 61 | bool GrDrawContext::wasAbandoned() const { |
| 62 | return fDrawingManager->wasAbandoned(); |
| 63 | } |
| 64 | |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 65 | // In MDB mode the reffing of the 'getLastDrawTarget' call's result allows in-progress |
| 66 | // drawTargets to be picked up and added to by drawContexts lower in the call |
| 67 | // stack. When this occurs with a closed drawTarget, a new one will be allocated |
| 68 | // when the drawContext attempts to use it (via getDrawTarget). |
joshualitt | 1b39f43 | 2016-02-11 06:46:52 -0800 | [diff] [blame] | 69 | GrDrawContext::GrDrawContext(GrContext* context, |
| 70 | GrDrawingManager* drawingMgr, |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 71 | sk_sp<GrRenderTarget> rt, |
brianosman | dfe4f2e | 2016-07-21 13:28:36 -0700 | [diff] [blame] | 72 | sk_sp<SkColorSpace> colorSpace, |
joshualitt | de8dc7e | 2016-01-08 10:09:13 -0800 | [diff] [blame] | 73 | const SkSurfaceProps* surfaceProps, |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 74 | GrAuditTrail* auditTrail, |
joshualitt | de8dc7e | 2016-01-08 10:09:13 -0800 | [diff] [blame] | 75 | GrSingleOwner* singleOwner) |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 76 | : fDrawingManager(drawingMgr) |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 77 | , fRenderTarget(std::move(rt)) |
| 78 | , fDrawTarget(SkSafeRef(fRenderTarget->getLastDrawTarget())) |
joshualitt | 1b39f43 | 2016-02-11 06:46:52 -0800 | [diff] [blame] | 79 | , fContext(context) |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 80 | , fInstancedPipelineInfo(fRenderTarget.get()) |
brianosman | dfe4f2e | 2016-07-21 13:28:36 -0700 | [diff] [blame] | 81 | , fColorSpace(std::move(colorSpace)) |
joshualitt | de8dc7e | 2016-01-08 10:09:13 -0800 | [diff] [blame] | 82 | , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps)) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 83 | , fAuditTrail(auditTrail) |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 84 | #ifdef SK_DEBUG |
| 85 | , fSingleOwner(singleOwner) |
| 86 | #endif |
| 87 | { |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 88 | SkDEBUGCODE(this->validate();) |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 89 | } |
| 90 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 91 | #ifdef SK_DEBUG |
| 92 | void GrDrawContext::validate() const { |
| 93 | SkASSERT(fRenderTarget); |
| 94 | ASSERT_OWNED_RESOURCE(fRenderTarget); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 95 | |
| 96 | if (fDrawTarget && !fDrawTarget->isClosed()) { |
| 97 | SkASSERT(fRenderTarget->getLastDrawTarget() == fDrawTarget); |
| 98 | } |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 99 | } |
| 100 | #endif |
| 101 | |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 102 | GrDrawContext::~GrDrawContext() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 103 | ASSERT_SINGLE_OWNER |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 104 | SkSafeUnref(fDrawTarget); |
| 105 | } |
| 106 | |
| 107 | GrDrawTarget* GrDrawContext::getDrawTarget() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 108 | ASSERT_SINGLE_OWNER |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 109 | SkDEBUGCODE(this->validate();) |
| 110 | |
| 111 | if (!fDrawTarget || fDrawTarget->isClosed()) { |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 112 | fDrawTarget = fDrawingManager->newDrawTarget(fRenderTarget.get()); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | return fDrawTarget; |
| 116 | } |
| 117 | |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 118 | bool GrDrawContext::copySurface(GrSurface* src, const SkIRect& srcRect, const SkIPoint& dstPoint) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 119 | ASSERT_SINGLE_OWNER |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 120 | RETURN_FALSE_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::copySurface"); |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 123 | |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 124 | return this->getDrawTarget()->copySurface(fRenderTarget.get(), src, srcRect, dstPoint); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 125 | } |
| 126 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 127 | void GrDrawContext::drawText(const GrClip& clip, const GrPaint& grPaint, |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 128 | const SkPaint& skPaint, |
| 129 | const SkMatrix& viewMatrix, |
| 130 | const char text[], size_t byteLength, |
| 131 | SkScalar x, SkScalar y, const SkIRect& clipBounds) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 132 | ASSERT_SINGLE_OWNER |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 133 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 134 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 135 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::drawText"); |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 136 | |
joshualitt | 96880d9 | 2016-02-16 10:36:53 -0800 | [diff] [blame] | 137 | if (!fAtlasTextContext) { |
joshualitt | 8db8678 | 2016-02-17 05:40:00 -0800 | [diff] [blame] | 138 | fAtlasTextContext.reset(GrAtlasTextContext::Create()); |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 139 | } |
| 140 | |
joshualitt | 96880d9 | 2016-02-16 10:36:53 -0800 | [diff] [blame] | 141 | fAtlasTextContext->drawText(fContext, this, clip, grPaint, skPaint, viewMatrix, fSurfaceProps, |
| 142 | text, byteLength, x, y, clipBounds); |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 143 | } |
robertphillips | caef345 | 2015-11-11 13:18:11 -0800 | [diff] [blame] | 144 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 145 | void GrDrawContext::drawPosText(const GrClip& clip, const GrPaint& grPaint, |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 146 | const SkPaint& skPaint, |
| 147 | const SkMatrix& viewMatrix, |
| 148 | const char text[], size_t byteLength, |
| 149 | const SkScalar pos[], int scalarsPerPosition, |
| 150 | const SkPoint& offset, const SkIRect& clipBounds) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 151 | ASSERT_SINGLE_OWNER |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 152 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 153 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 154 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::drawPosText"); |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 155 | |
joshualitt | 96880d9 | 2016-02-16 10:36:53 -0800 | [diff] [blame] | 156 | if (!fAtlasTextContext) { |
joshualitt | 8db8678 | 2016-02-17 05:40:00 -0800 | [diff] [blame] | 157 | fAtlasTextContext.reset(GrAtlasTextContext::Create()); |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 158 | } |
| 159 | |
joshualitt | 96880d9 | 2016-02-16 10:36:53 -0800 | [diff] [blame] | 160 | fAtlasTextContext->drawPosText(fContext, this, clip, grPaint, skPaint, viewMatrix, |
| 161 | fSurfaceProps, text, byteLength, pos, scalarsPerPosition, |
| 162 | offset, clipBounds); |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 163 | |
| 164 | } |
robertphillips | caef345 | 2015-11-11 13:18:11 -0800 | [diff] [blame] | 165 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 166 | void GrDrawContext::drawTextBlob(const GrClip& clip, const SkPaint& skPaint, |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 167 | const SkMatrix& viewMatrix, const SkTextBlob* blob, |
| 168 | SkScalar x, SkScalar y, |
| 169 | SkDrawFilter* filter, const SkIRect& clipBounds) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 170 | ASSERT_SINGLE_OWNER |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 171 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 172 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 173 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::drawTextBlob"); |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 174 | |
joshualitt | 96880d9 | 2016-02-16 10:36:53 -0800 | [diff] [blame] | 175 | if (!fAtlasTextContext) { |
joshualitt | 8db8678 | 2016-02-17 05:40:00 -0800 | [diff] [blame] | 176 | fAtlasTextContext.reset(GrAtlasTextContext::Create()); |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 177 | } |
| 178 | |
joshualitt | 96880d9 | 2016-02-16 10:36:53 -0800 | [diff] [blame] | 179 | fAtlasTextContext->drawTextBlob(fContext, this, clip, skPaint, viewMatrix, fSurfaceProps, blob, |
| 180 | x, y, filter, clipBounds); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 181 | } |
| 182 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 183 | void GrDrawContext::discard() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 184 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 185 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 186 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 187 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::discard"); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 188 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 189 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 190 | this->getDrawTarget()->discard(fRenderTarget.get()); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 191 | } |
| 192 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 193 | void GrDrawContext::clear(const SkIRect* rect, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 194 | const GrColor color, |
| 195 | bool canIgnoreRect) { |
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::clear"); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 200 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 201 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | 9199a9f | 2016-07-13 07:48:43 -0700 | [diff] [blame] | 202 | |
| 203 | const SkIRect rtRect = SkIRect::MakeWH(this->width(), this->height()); |
| 204 | SkIRect clippedRect; |
| 205 | if (!rect || |
| 206 | (canIgnoreRect && fContext->caps()->fullClearIsFree()) || |
| 207 | rect->contains(rtRect)) { |
| 208 | rect = &rtRect; |
| 209 | } else { |
| 210 | clippedRect = *rect; |
| 211 | if (!clippedRect.intersect(rtRect)) { |
| 212 | return; |
| 213 | } |
| 214 | rect = &clippedRect; |
| 215 | } |
| 216 | |
| 217 | if (fContext->caps()->useDrawInsteadOfClear()) { |
| 218 | // This works around a driver bug with clear by drawing a rect instead. |
| 219 | // The driver will ignore a clear if it is the only thing rendered to a |
| 220 | // target before the target is read. |
| 221 | if (rect == &rtRect) { |
| 222 | this->discard(); |
| 223 | } |
| 224 | |
| 225 | GrPaint paint; |
| 226 | paint.setColor4f(GrColor4f::FromGrColor(color)); |
| 227 | paint.setXPFactory(GrPorterDuffXPFactory::Make(SkXfermode::kSrc_Mode)); |
| 228 | |
| 229 | this->drawRect(GrNoClip(), paint, SkMatrix::I(), SkRect::Make(*rect)); |
| 230 | } else { |
| 231 | sk_sp<GrBatch> batch = GrClearBatch::Make(*rect, color, this->accessRenderTarget()); |
| 232 | this->getDrawTarget()->addBatch(std::move(batch)); |
| 233 | } |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 237 | void GrDrawContext::drawPaint(const GrClip& clip, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 238 | const GrPaint& origPaint, |
| 239 | const SkMatrix& viewMatrix) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 240 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 241 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 242 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 243 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::drawPaint"); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 244 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 245 | // set rect to be big enough to fill the space, but not super-huge, so we |
| 246 | // don't overflow fixed-point implementations |
| 247 | SkRect r; |
| 248 | r.setLTRB(0, 0, |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 249 | SkIntToScalar(fRenderTarget->width()), |
| 250 | SkIntToScalar(fRenderTarget->height())); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 251 | SkTCopyOnFirstWrite<GrPaint> paint(origPaint); |
| 252 | |
| 253 | // by definition this fills the entire clip, no need for AA |
| 254 | if (paint->isAntiAlias()) { |
| 255 | paint.writable()->setAntiAlias(false); |
| 256 | } |
| 257 | |
| 258 | bool isPerspective = viewMatrix.hasPerspective(); |
| 259 | |
| 260 | // We attempt to map r by the inverse matrix and draw that. mapRect will |
| 261 | // map the four corners and bound them with a new rect. This will not |
| 262 | // produce a correct result for some perspective matrices. |
| 263 | if (!isPerspective) { |
| 264 | SkMatrix inverse; |
| 265 | if (!viewMatrix.invert(&inverse)) { |
| 266 | SkDebugf("Could not invert matrix\n"); |
| 267 | return; |
| 268 | } |
| 269 | inverse.mapRect(&r); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 270 | this->drawRect(clip, *paint, viewMatrix, r); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 271 | } else { |
| 272 | SkMatrix localMatrix; |
| 273 | if (!viewMatrix.invert(&localMatrix)) { |
| 274 | SkDebugf("Could not invert matrix\n"); |
| 275 | return; |
| 276 | } |
| 277 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 278 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 279 | |
robertphillips | 4430239 | 2016-07-08 14:43:03 -0700 | [diff] [blame] | 280 | this->drawNonAAFilledRect(clip, *paint, SkMatrix::I(), r, nullptr, &localMatrix, nullptr); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 284 | static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) { |
| 285 | return point.fX >= rect.fLeft && point.fX <= rect.fRight && |
| 286 | point.fY >= rect.fTop && point.fY <= rect.fBottom; |
| 287 | } |
| 288 | |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 289 | static bool view_matrix_ok_for_aa_fill_rect(const SkMatrix& viewMatrix) { |
| 290 | return viewMatrix.preservesRightAngles(); |
| 291 | } |
| 292 | |
csmartdalton | ecbc12b | 2016-06-08 10:08:43 -0700 | [diff] [blame] | 293 | static bool should_apply_coverage_aa(const GrPaint& paint, GrRenderTarget* rt, |
| 294 | bool* useHWAA = nullptr) { |
| 295 | if (!paint.isAntiAlias()) { |
| 296 | if (useHWAA) { |
| 297 | *useHWAA = false; |
| 298 | } |
| 299 | return false; |
| 300 | } else { |
| 301 | if (useHWAA) { |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 302 | *useHWAA = rt->isUnifiedMultisampled(); |
csmartdalton | ecbc12b | 2016-06-08 10:08:43 -0700 | [diff] [blame] | 303 | } |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 304 | return !rt->isUnifiedMultisampled(); |
csmartdalton | ecbc12b | 2016-06-08 10:08:43 -0700 | [diff] [blame] | 305 | } |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 306 | } |
| 307 | |
csmartdalton | 97f6cd5 | 2016-07-13 13:37:08 -0700 | [diff] [blame] | 308 | // Attempts to crop a rect and optional local rect to the clip boundaries. |
| 309 | // Returns false if the draw can be skipped entirely. |
| 310 | static bool crop_filled_rect(const GrRenderTarget* rt, const GrClip& clip, |
| 311 | const SkMatrix& viewMatrix, SkRect* rect, |
| 312 | SkRect* localRect = nullptr) { |
| 313 | if (!viewMatrix.rectStaysRect()) { |
| 314 | return true; |
| 315 | } |
| 316 | |
| 317 | SkMatrix inverseViewMatrix; |
| 318 | if (!viewMatrix.invert(&inverseViewMatrix)) { |
| 319 | return false; |
| 320 | } |
| 321 | |
| 322 | SkIRect clipDevBounds; |
| 323 | SkRect clipBounds; |
| 324 | SkASSERT(inverseViewMatrix.rectStaysRect()); |
| 325 | |
| 326 | clip.getConservativeBounds(rt->width(), rt->height(), &clipDevBounds); |
| 327 | inverseViewMatrix.mapRect(&clipBounds, SkRect::Make(clipDevBounds)); |
| 328 | |
| 329 | if (localRect) { |
| 330 | if (!rect->intersects(clipBounds)) { |
| 331 | return false; |
| 332 | } |
| 333 | const SkScalar dx = localRect->width() / rect->width(); |
| 334 | const SkScalar dy = localRect->height() / rect->height(); |
| 335 | if (clipBounds.fLeft > rect->fLeft) { |
| 336 | localRect->fLeft += (clipBounds.fLeft - rect->fLeft) * dx; |
| 337 | rect->fLeft = clipBounds.fLeft; |
| 338 | } |
| 339 | if (clipBounds.fTop > rect->fTop) { |
| 340 | localRect->fTop += (clipBounds.fTop - rect->fTop) * dy; |
| 341 | rect->fTop = clipBounds.fTop; |
| 342 | } |
| 343 | if (clipBounds.fRight < rect->fRight) { |
| 344 | localRect->fRight -= (rect->fRight - clipBounds.fRight) * dx; |
| 345 | rect->fRight = clipBounds.fRight; |
| 346 | } |
| 347 | if (clipBounds.fBottom < rect->fBottom) { |
| 348 | localRect->fBottom -= (rect->fBottom - clipBounds.fBottom) * dy; |
| 349 | rect->fBottom = clipBounds.fBottom; |
| 350 | } |
| 351 | return true; |
| 352 | } |
| 353 | |
| 354 | return rect->intersect(clipBounds); |
| 355 | } |
| 356 | |
robertphillips | 4430239 | 2016-07-08 14:43:03 -0700 | [diff] [blame] | 357 | bool GrDrawContext::drawFilledRect(const GrClip& clip, |
| 358 | const GrPaint& paint, |
| 359 | const SkMatrix& viewMatrix, |
| 360 | const SkRect& rect, |
| 361 | const GrUserStencilSettings* ss) { |
csmartdalton | 97f6cd5 | 2016-07-13 13:37:08 -0700 | [diff] [blame] | 362 | SkRect croppedRect = rect; |
| 363 | if (!crop_filled_rect(fRenderTarget.get(), clip, viewMatrix, &croppedRect)) { |
| 364 | return true; |
| 365 | } |
robertphillips | 4430239 | 2016-07-08 14:43:03 -0700 | [diff] [blame] | 366 | |
| 367 | SkAutoTUnref<GrDrawBatch> batch; |
| 368 | bool useHWAA; |
| 369 | |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 370 | if (InstancedRendering* ir = this->getDrawTarget()->instancedRendering()) { |
csmartdalton | 97f6cd5 | 2016-07-13 13:37:08 -0700 | [diff] [blame] | 371 | batch.reset(ir->recordRect(croppedRect, viewMatrix, paint.getColor(), |
robertphillips | 4430239 | 2016-07-08 14:43:03 -0700 | [diff] [blame] | 372 | paint.isAntiAlias(), fInstancedPipelineInfo, |
| 373 | &useHWAA)); |
| 374 | if (batch) { |
| 375 | GrPipelineBuilder pipelineBuilder(paint, useHWAA); |
| 376 | if (ss) { |
| 377 | pipelineBuilder.setUserStencil(ss); |
| 378 | } |
| 379 | this->getDrawTarget()->drawBatch(pipelineBuilder, this, clip, batch); |
| 380 | return true; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 381 | } |
| 382 | } |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 383 | |
robertphillips | 4430239 | 2016-07-08 14:43:03 -0700 | [diff] [blame] | 384 | if (should_apply_coverage_aa(paint, fRenderTarget.get(), &useHWAA)) { |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 385 | // The fill path can handle rotation but not skew. |
| 386 | if (view_matrix_ok_for_aa_fill_rect(viewMatrix)) { |
| 387 | SkRect devBoundRect; |
csmartdalton | 97f6cd5 | 2016-07-13 13:37:08 -0700 | [diff] [blame] | 388 | viewMatrix.mapRect(&devBoundRect, croppedRect); |
robertphillips | 4430239 | 2016-07-08 14:43:03 -0700 | [diff] [blame] | 389 | |
| 390 | batch.reset(GrRectBatchFactory::CreateAAFill(paint.getColor(), viewMatrix, |
csmartdalton | 97f6cd5 | 2016-07-13 13:37:08 -0700 | [diff] [blame] | 391 | croppedRect, devBoundRect)); |
robertphillips | 4430239 | 2016-07-08 14:43:03 -0700 | [diff] [blame] | 392 | if (batch) { |
| 393 | GrPipelineBuilder pipelineBuilder(paint, useHWAA); |
| 394 | if (ss) { |
| 395 | pipelineBuilder.setUserStencil(ss); |
| 396 | } |
| 397 | this->getDrawTarget()->drawBatch(pipelineBuilder, this, clip, batch); |
| 398 | return true; |
| 399 | } |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 400 | } |
| 401 | } else { |
csmartdalton | 97f6cd5 | 2016-07-13 13:37:08 -0700 | [diff] [blame] | 402 | this->drawNonAAFilledRect(clip, paint, viewMatrix, croppedRect, nullptr, nullptr, ss); |
robertphillips | 4430239 | 2016-07-08 14:43:03 -0700 | [diff] [blame] | 403 | return true; |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 404 | } |
| 405 | |
robertphillips | 4430239 | 2016-07-08 14:43:03 -0700 | [diff] [blame] | 406 | return false; |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 407 | } |
| 408 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 409 | void GrDrawContext::drawRect(const GrClip& clip, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 410 | const GrPaint& paint, |
| 411 | const SkMatrix& viewMatrix, |
| 412 | const SkRect& rect, |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 413 | const GrStyle* style) { |
| 414 | if (!style) { |
| 415 | style = &GrStyle::SimpleFill(); |
| 416 | } |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 417 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 418 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 419 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 420 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::drawRect"); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 421 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 422 | // Path effects should've been devolved to a path in SkGpuDevice |
| 423 | SkASSERT(!style->pathEffect()); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 424 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 425 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 426 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 427 | const SkStrokeRec& stroke = style->strokeRec(); |
robertphillips | 3ab14ca | 2016-07-10 11:49:39 -0700 | [diff] [blame] | 428 | if (stroke.getStyle() == SkStrokeRec::kFill_Style) { |
| 429 | |
| 430 | if (!fContext->caps()->useDrawInsteadOfClear()) { |
| 431 | // Check if this is a full RT draw and can be replaced with a clear. We don't bother |
| 432 | // checking cases where the RT is fully inside a stroke. |
| 433 | SkRect rtRect; |
| 434 | fRenderTarget->getBoundsRect(&rtRect); |
| 435 | // Does the clip contain the entire RT? |
| 436 | if (clip.quickContains(rtRect)) { |
| 437 | SkMatrix invM; |
| 438 | if (!viewMatrix.invert(&invM)) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 439 | return; |
| 440 | } |
robertphillips | 3ab14ca | 2016-07-10 11:49:39 -0700 | [diff] [blame] | 441 | // Does the rect bound the RT? |
| 442 | SkPoint srcSpaceRTQuad[4]; |
| 443 | invM.mapRectToQuad(srcSpaceRTQuad, rtRect); |
| 444 | if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) && |
| 445 | rect_contains_inclusive(rect, srcSpaceRTQuad[1]) && |
| 446 | rect_contains_inclusive(rect, srcSpaceRTQuad[2]) && |
| 447 | rect_contains_inclusive(rect, srcSpaceRTQuad[3])) { |
| 448 | // Will it blend? |
| 449 | GrColor clearColor; |
| 450 | if (paint.isConstantBlendedColor(&clearColor)) { |
robertphillips | 9199a9f | 2016-07-13 07:48:43 -0700 | [diff] [blame] | 451 | this->clear(nullptr, clearColor, true); |
robertphillips | 3ab14ca | 2016-07-10 11:49:39 -0700 | [diff] [blame] | 452 | return; |
| 453 | } |
| 454 | } |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 455 | } |
| 456 | } |
robertphillips | 4430239 | 2016-07-08 14:43:03 -0700 | [diff] [blame] | 457 | |
| 458 | if (this->drawFilledRect(clip, paint, viewMatrix, rect, nullptr)) { |
| 459 | return; |
| 460 | } |
bsalomon | a7d85ba | 2016-07-06 11:54:59 -0700 | [diff] [blame] | 461 | } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style || |
| 462 | stroke.getStyle() == SkStrokeRec::kHairline_Style) { |
| 463 | if ((!rect.width() || !rect.height()) && |
| 464 | SkStrokeRec::kHairline_Style != stroke.getStyle()) { |
| 465 | SkScalar r = stroke.getWidth() / 2; |
| 466 | // TODO: Move these stroke->fill fallbacks to GrShape? |
| 467 | switch (stroke.getJoin()) { |
| 468 | case SkPaint::kMiter_Join: |
| 469 | this->drawRect(clip, paint, viewMatrix, |
| 470 | {rect.fLeft - r, rect.fTop - r, |
| 471 | rect.fRight + r, rect.fBottom + r}, |
| 472 | &GrStyle::SimpleFill()); |
| 473 | return; |
| 474 | case SkPaint::kRound_Join: |
| 475 | // Raster draws nothing when both dimensions are empty. |
| 476 | if (rect.width() || rect.height()){ |
| 477 | SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r); |
| 478 | this->drawRRect(clip, paint, viewMatrix, rrect, GrStyle::SimpleFill()); |
| 479 | return; |
| 480 | } |
| 481 | case SkPaint::kBevel_Join: |
| 482 | if (!rect.width()) { |
| 483 | this->drawRect(clip, paint, viewMatrix, |
| 484 | {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom}, |
| 485 | &GrStyle::SimpleFill()); |
| 486 | } else { |
| 487 | this->drawRect(clip, paint, viewMatrix, |
| 488 | {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r}, |
| 489 | &GrStyle::SimpleFill()); |
| 490 | } |
| 491 | return; |
| 492 | } |
| 493 | } |
robertphillips | 4430239 | 2016-07-08 14:43:03 -0700 | [diff] [blame] | 494 | |
| 495 | bool useHWAA; |
| 496 | bool snapToPixelCenters = false; |
| 497 | SkAutoTUnref<GrDrawBatch> batch; |
| 498 | |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 499 | GrColor color = paint.getColor(); |
csmartdalton | ecbc12b | 2016-06-08 10:08:43 -0700 | [diff] [blame] | 500 | if (should_apply_coverage_aa(paint, fRenderTarget.get(), &useHWAA)) { |
cdalton | bb53948 | 2016-01-04 09:48:25 -0800 | [diff] [blame] | 501 | // The stroke path needs the rect to remain axis aligned (no rotation or skew). |
| 502 | if (viewMatrix.rectStaysRect()) { |
bsalomon | a7d85ba | 2016-07-06 11:54:59 -0700 | [diff] [blame] | 503 | batch.reset(GrRectBatchFactory::CreateAAStroke(color, viewMatrix, rect, stroke)); |
cdalton | bb53948 | 2016-01-04 09:48:25 -0800 | [diff] [blame] | 504 | } |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 505 | } else { |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 506 | // Depending on sub-pixel coordinates and the particular GPU, we may lose a corner of |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 507 | // hairline rects. We jam all the vertices to pixel centers to avoid this, but not |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 508 | // when MSAA is enabled because it can cause ugly artifacts. |
bsalomon | a7d85ba | 2016-07-06 11:54:59 -0700 | [diff] [blame] | 509 | snapToPixelCenters = stroke.getStyle() == SkStrokeRec::kHairline_Style && |
| 510 | !fRenderTarget->isUnifiedMultisampled(); |
| 511 | batch.reset(GrRectBatchFactory::CreateNonAAStroke(color, viewMatrix, rect, |
| 512 | stroke, snapToPixelCenters)); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 513 | } |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 514 | |
robertphillips | 4430239 | 2016-07-08 14:43:03 -0700 | [diff] [blame] | 515 | if (batch) { |
| 516 | GrPipelineBuilder pipelineBuilder(paint, useHWAA); |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 517 | |
robertphillips | 4430239 | 2016-07-08 14:43:03 -0700 | [diff] [blame] | 518 | if (snapToPixelCenters) { |
| 519 | pipelineBuilder.setState(GrPipelineBuilder::kSnapVerticesToPixelCenters_Flag, |
| 520 | snapToPixelCenters); |
| 521 | } |
| 522 | |
| 523 | this->getDrawTarget()->drawBatch(pipelineBuilder, this, clip, batch); |
| 524 | return; |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 525 | } |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 526 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 527 | |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 528 | SkPath path; |
| 529 | path.setIsVolatile(true); |
| 530 | path.addRect(rect); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 531 | this->internalDrawPath(clip, paint, viewMatrix, path, *style); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 532 | } |
| 533 | |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 534 | void GrDrawContextPriv::clearStencilClip(const SkIRect& rect, bool insideClip) { |
| 535 | ASSERT_SINGLE_OWNER_PRIV |
| 536 | RETURN_IF_ABANDONED_PRIV |
| 537 | SkDEBUGCODE(fDrawContext->validate();) |
| 538 | GR_AUDIT_TRAIL_AUTO_FRAME(fDrawContext->fAuditTrail, "GrDrawContextPriv::clearStencilClip"); |
| 539 | |
| 540 | AutoCheckFlush acf(fDrawContext->fDrawingManager); |
| 541 | fDrawContext->getDrawTarget()->clearStencilClip(rect, insideClip, |
| 542 | fDrawContext->accessRenderTarget()); |
| 543 | } |
| 544 | |
robertphillips | 59cf61a | 2016-07-13 09:18:21 -0700 | [diff] [blame] | 545 | void GrDrawContextPriv::stencilPath(const GrClip& clip, |
| 546 | const GrUserStencilSettings* ss, |
| 547 | bool useHWAA, |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 548 | const SkMatrix& viewMatrix, |
robertphillips | 59cf61a | 2016-07-13 09:18:21 -0700 | [diff] [blame] | 549 | const GrPath* path) { |
| 550 | fDrawContext->getDrawTarget()->stencilPath(fDrawContext, clip, ss, useHWAA, viewMatrix, path); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | void GrDrawContextPriv::stencilRect(const GrFixedClip& clip, |
| 554 | const GrUserStencilSettings* ss, |
csmartdalton | 656dbe4 | 2016-06-10 12:32:57 -0700 | [diff] [blame] | 555 | bool useHWAA, |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 556 | const SkMatrix& viewMatrix, |
| 557 | const SkRect& rect) { |
| 558 | ASSERT_SINGLE_OWNER_PRIV |
| 559 | RETURN_IF_ABANDONED_PRIV |
| 560 | SkDEBUGCODE(fDrawContext->validate();) |
| 561 | GR_AUDIT_TRAIL_AUTO_FRAME(fDrawContext->fAuditTrail, "GrDrawContext::stencilRect"); |
| 562 | |
| 563 | AutoCheckFlush acf(fDrawContext->fDrawingManager); |
| 564 | |
| 565 | GrPaint paint; |
csmartdalton | 656dbe4 | 2016-06-10 12:32:57 -0700 | [diff] [blame] | 566 | paint.setAntiAlias(useHWAA); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 567 | paint.setXPFactory(GrDisableColorXPFactory::Make()); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 568 | |
csmartdalton | 656dbe4 | 2016-06-10 12:32:57 -0700 | [diff] [blame] | 569 | SkASSERT(!useHWAA || fDrawContext->isStencilBufferMultisampled()); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 570 | |
robertphillips | 4430239 | 2016-07-08 14:43:03 -0700 | [diff] [blame] | 571 | fDrawContext->drawFilledRect(clip, paint, viewMatrix, rect, ss); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 572 | } |
| 573 | |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 574 | bool GrDrawContextPriv::drawAndStencilRect(const GrFixedClip& clip, |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 575 | const GrUserStencilSettings* ss, |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 576 | SkRegion::Op op, |
| 577 | bool invert, |
| 578 | bool doAA, |
| 579 | const SkMatrix& viewMatrix, |
| 580 | const SkRect& rect) { |
| 581 | ASSERT_SINGLE_OWNER_PRIV |
| 582 | RETURN_FALSE_IF_ABANDONED_PRIV |
| 583 | SkDEBUGCODE(fDrawContext->validate();) |
robertphillips | 55fdccc | 2016-06-06 06:16:20 -0700 | [diff] [blame] | 584 | GR_AUDIT_TRAIL_AUTO_FRAME(fDrawContext->fAuditTrail, "GrDrawContext::drawAndStencilRect"); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 585 | |
| 586 | AutoCheckFlush acf(fDrawContext->fDrawingManager); |
| 587 | |
| 588 | GrPaint paint; |
| 589 | paint.setAntiAlias(doAA); |
| 590 | paint.setCoverageSetOpXPFactory(op, invert); |
| 591 | |
robertphillips | 4430239 | 2016-07-08 14:43:03 -0700 | [diff] [blame] | 592 | if (fDrawContext->drawFilledRect(clip, paint, viewMatrix, rect, ss)) { |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 593 | return true; |
| 594 | } |
| 595 | |
| 596 | SkPath path; |
| 597 | path.setIsVolatile(true); |
| 598 | path.addRect(rect); |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 599 | return this->drawAndStencilPath(clip, ss, op, invert, doAA, viewMatrix, path); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 600 | } |
| 601 | |
bsalomon | a2e69fc | 2015-11-05 10:41:43 -0800 | [diff] [blame] | 602 | void GrDrawContext::fillRectToRect(const GrClip& clip, |
| 603 | const GrPaint& paint, |
| 604 | const SkMatrix& viewMatrix, |
| 605 | const SkRect& rectToDraw, |
| 606 | const SkRect& localRect) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 607 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 608 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 609 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 610 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::fillRectToRect"); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 611 | |
csmartdalton | 97f6cd5 | 2016-07-13 13:37:08 -0700 | [diff] [blame] | 612 | SkRect croppedRect = rectToDraw; |
| 613 | SkRect croppedLocalRect = localRect; |
| 614 | if (!crop_filled_rect(fRenderTarget.get(), clip, viewMatrix, &croppedRect, &croppedLocalRect)) { |
| 615 | return; |
| 616 | } |
| 617 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 618 | AutoCheckFlush acf(fDrawingManager); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 619 | bool useHWAA; |
| 620 | |
| 621 | if (InstancedRendering* ir = this->getDrawTarget()->instancedRendering()) { |
csmartdalton | fc49d56 | 2016-07-26 17:05:47 -0700 | [diff] [blame^] | 622 | SkAutoTUnref<GrDrawBatch> batch(ir->recordRect(croppedRect, viewMatrix, paint.getColor(), |
| 623 | croppedLocalRect, paint.isAntiAlias(), |
| 624 | fInstancedPipelineInfo, &useHWAA)); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 625 | if (batch) { |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 626 | GrPipelineBuilder pipelineBuilder(paint, useHWAA); |
| 627 | this->getDrawTarget()->drawBatch(pipelineBuilder, this, clip, batch); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 628 | return; |
| 629 | } |
| 630 | } |
| 631 | |
csmartdalton | fc49d56 | 2016-07-26 17:05:47 -0700 | [diff] [blame^] | 632 | if (!should_apply_coverage_aa(paint, fRenderTarget.get(), &useHWAA)) { |
csmartdalton | 97f6cd5 | 2016-07-13 13:37:08 -0700 | [diff] [blame] | 633 | this->drawNonAAFilledRect(clip, paint, viewMatrix, croppedRect, &croppedLocalRect, |
robertphillips | 4430239 | 2016-07-08 14:43:03 -0700 | [diff] [blame] | 634 | nullptr, nullptr); |
csmartdalton | fc49d56 | 2016-07-26 17:05:47 -0700 | [diff] [blame^] | 635 | return; |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 636 | } |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 637 | |
csmartdalton | fc49d56 | 2016-07-26 17:05:47 -0700 | [diff] [blame^] | 638 | if (view_matrix_ok_for_aa_fill_rect(viewMatrix)) { |
| 639 | SkAutoTUnref<GrDrawBatch> batch(GrAAFillRectBatch::CreateWithLocalRect(paint.getColor(), |
| 640 | viewMatrix, |
| 641 | croppedRect, |
| 642 | croppedLocalRect)); |
| 643 | GrPipelineBuilder pipelineBuilder(paint, useHWAA); |
| 644 | this->drawBatch(pipelineBuilder, clip, batch); |
| 645 | return; |
| 646 | } |
| 647 | |
| 648 | SkMatrix viewAndUnLocalMatrix; |
| 649 | if (!viewAndUnLocalMatrix.setRectToRect(localRect, rectToDraw, SkMatrix::kFill_ScaleToFit)) { |
| 650 | SkDebugf("fillRectToRect called with empty local matrix.\n"); |
| 651 | return; |
| 652 | } |
| 653 | viewAndUnLocalMatrix.postConcat(viewMatrix); |
| 654 | |
| 655 | SkPath path; |
| 656 | path.setIsVolatile(true); |
| 657 | path.addRect(localRect); |
| 658 | this->internalDrawPath(clip, paint, viewAndUnLocalMatrix, path, GrStyle()); |
joshualitt | b6b513b | 2015-08-21 10:25:18 -0700 | [diff] [blame] | 659 | } |
| 660 | |
bsalomon | a2e69fc | 2015-11-05 10:41:43 -0800 | [diff] [blame] | 661 | void GrDrawContext::fillRectWithLocalMatrix(const GrClip& clip, |
| 662 | const GrPaint& paint, |
| 663 | const SkMatrix& viewMatrix, |
| 664 | const SkRect& rectToDraw, |
| 665 | const SkMatrix& localMatrix) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 666 | ASSERT_SINGLE_OWNER |
joshualitt | b6b513b | 2015-08-21 10:25:18 -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::fillRectWithLocalMatrix"); |
joshualitt | b6b513b | 2015-08-21 10:25:18 -0700 | [diff] [blame] | 670 | |
csmartdalton | 97f6cd5 | 2016-07-13 13:37:08 -0700 | [diff] [blame] | 671 | SkRect croppedRect = rectToDraw; |
| 672 | if (!crop_filled_rect(fRenderTarget.get(), clip, viewMatrix, &croppedRect)) { |
| 673 | return; |
| 674 | } |
| 675 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 676 | AutoCheckFlush acf(fDrawingManager); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 677 | bool useHWAA; |
| 678 | |
| 679 | if (InstancedRendering* ir = this->getDrawTarget()->instancedRendering()) { |
csmartdalton | fc49d56 | 2016-07-26 17:05:47 -0700 | [diff] [blame^] | 680 | SkAutoTUnref<GrDrawBatch> batch(ir->recordRect(croppedRect, viewMatrix, paint.getColor(), |
| 681 | localMatrix, paint.isAntiAlias(), |
| 682 | fInstancedPipelineInfo, &useHWAA)); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 683 | if (batch) { |
| 684 | GrPipelineBuilder pipelineBuilder(paint, useHWAA); |
| 685 | this->getDrawTarget()->drawBatch(pipelineBuilder, this, clip, batch); |
| 686 | return; |
| 687 | } |
| 688 | } |
| 689 | |
csmartdalton | fc49d56 | 2016-07-26 17:05:47 -0700 | [diff] [blame^] | 690 | if (!should_apply_coverage_aa(paint, fRenderTarget.get(), &useHWAA)) { |
csmartdalton | 97f6cd5 | 2016-07-13 13:37:08 -0700 | [diff] [blame] | 691 | this->drawNonAAFilledRect(clip, paint, viewMatrix, croppedRect, nullptr, |
robertphillips | 4430239 | 2016-07-08 14:43:03 -0700 | [diff] [blame] | 692 | &localMatrix, nullptr); |
csmartdalton | fc49d56 | 2016-07-26 17:05:47 -0700 | [diff] [blame^] | 693 | return; |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 694 | } |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 695 | |
csmartdalton | fc49d56 | 2016-07-26 17:05:47 -0700 | [diff] [blame^] | 696 | if (view_matrix_ok_for_aa_fill_rect(viewMatrix)) { |
| 697 | SkAutoTUnref<GrDrawBatch> batch(GrAAFillRectBatch::Create(paint.getColor(), viewMatrix, |
| 698 | localMatrix, croppedRect)); |
| 699 | GrPipelineBuilder pipelineBuilder(paint, useHWAA); |
| 700 | this->getDrawTarget()->drawBatch(pipelineBuilder, this, clip, batch); |
| 701 | return; |
| 702 | } |
| 703 | |
| 704 | SkMatrix viewAndUnLocalMatrix; |
| 705 | if (!localMatrix.invert(&viewAndUnLocalMatrix)) { |
| 706 | SkDebugf("fillRectWithLocalMatrix called with degenerate local matrix.\n"); |
| 707 | return; |
| 708 | } |
| 709 | viewAndUnLocalMatrix.postConcat(viewMatrix); |
| 710 | |
| 711 | SkPath path; |
| 712 | path.setIsVolatile(true); |
| 713 | path.addRect(rectToDraw); |
| 714 | path.transform(localMatrix); |
| 715 | this->internalDrawPath(clip, paint, viewAndUnLocalMatrix, path, GrStyle()); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 716 | } |
| 717 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 718 | void GrDrawContext::drawVertices(const GrClip& clip, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 719 | const GrPaint& paint, |
| 720 | const SkMatrix& viewMatrix, |
| 721 | GrPrimitiveType primitiveType, |
| 722 | int vertexCount, |
| 723 | const SkPoint positions[], |
| 724 | const SkPoint texCoords[], |
| 725 | const GrColor colors[], |
| 726 | const uint16_t indices[], |
| 727 | int indexCount) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 728 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 729 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 730 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 731 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::drawVertices"); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 732 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 733 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 734 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 735 | // TODO clients should give us bounds |
| 736 | SkRect bounds; |
| 737 | if (!bounds.setBoundsCheck(positions, vertexCount)) { |
| 738 | SkDebugf("drawVertices call empty bounds\n"); |
| 739 | return; |
| 740 | } |
| 741 | |
| 742 | viewMatrix.mapRect(&bounds); |
| 743 | |
| 744 | // 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] | 745 | // snapping. We also do this for the "hair" primitive types: lines and points since they have |
| 746 | // a 1 pixel thickness in device space. |
| 747 | if (!paint.isAntiAlias() || GrIsPrimTypeLines(primitiveType) || |
| 748 | kPoints_GrPrimitiveType == primitiveType) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 749 | bounds.outset(0.5f, 0.5f); |
| 750 | } |
| 751 | |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 752 | SkAutoTUnref<GrDrawBatch> batch(new GrDrawVerticesBatch(paint.getColor(), |
| 753 | primitiveType, viewMatrix, positions, |
| 754 | vertexCount, indices, indexCount, |
| 755 | colors, texCoords, bounds)); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 756 | |
csmartdalton | ecbc12b | 2016-06-08 10:08:43 -0700 | [diff] [blame] | 757 | GrPipelineBuilder pipelineBuilder(paint, this->mustUseHWAA(paint)); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 758 | this->getDrawTarget()->drawBatch(pipelineBuilder, this, clip, batch); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | /////////////////////////////////////////////////////////////////////////////// |
| 762 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 763 | void GrDrawContext::drawAtlas(const GrClip& clip, |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 764 | const GrPaint& paint, |
| 765 | const SkMatrix& viewMatrix, |
| 766 | int spriteCount, |
| 767 | const SkRSXform xform[], |
| 768 | const SkRect texRect[], |
| 769 | const SkColor colors[]) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 770 | ASSERT_SINGLE_OWNER |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 771 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 772 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 773 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::drawAtlas"); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 774 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 775 | AutoCheckFlush acf(fDrawingManager); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 776 | |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 777 | SkAutoTUnref<GrDrawBatch> batch(new GrDrawAtlasBatch(paint.getColor(), viewMatrix, spriteCount, |
| 778 | xform, texRect, colors)); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 779 | |
csmartdalton | ecbc12b | 2016-06-08 10:08:43 -0700 | [diff] [blame] | 780 | GrPipelineBuilder pipelineBuilder(paint, this->mustUseHWAA(paint)); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 781 | this->getDrawTarget()->drawBatch(pipelineBuilder, this, clip, batch); |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | /////////////////////////////////////////////////////////////////////////////// |
| 785 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 786 | void GrDrawContext::drawRRect(const GrClip& clip, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 787 | const GrPaint& paint, |
| 788 | const SkMatrix& viewMatrix, |
| 789 | const SkRRect& rrect, |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 790 | const GrStyle& style) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 791 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 792 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 793 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 794 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::drawRRect"); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 795 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 796 | if (rrect.isEmpty()) { |
| 797 | return; |
| 798 | } |
| 799 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 800 | SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice |
ksakamoto | ec7f2ac | 2016-07-05 03:54:53 -0700 | [diff] [blame] | 801 | |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 802 | AutoCheckFlush acf(fDrawingManager); |
| 803 | const SkStrokeRec stroke = style.strokeRec(); |
csmartdalton | ecbc12b | 2016-06-08 10:08:43 -0700 | [diff] [blame] | 804 | bool useHWAA; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 805 | |
| 806 | if (this->getDrawTarget()->instancedRendering() && stroke.isFillStyle()) { |
| 807 | InstancedRendering* ir = this->getDrawTarget()->instancedRendering(); |
| 808 | SkAutoTUnref<GrDrawBatch> batch(ir->recordRRect(rrect, viewMatrix, paint.getColor(), |
| 809 | paint.isAntiAlias(), fInstancedPipelineInfo, |
| 810 | &useHWAA)); |
| 811 | if (batch) { |
| 812 | GrPipelineBuilder pipelineBuilder(paint, useHWAA); |
| 813 | this->getDrawTarget()->drawBatch(pipelineBuilder, this, clip, batch); |
| 814 | return; |
| 815 | } |
| 816 | } |
| 817 | |
csmartdalton | ecbc12b | 2016-06-08 10:08:43 -0700 | [diff] [blame] | 818 | if (should_apply_coverage_aa(paint, fRenderTarget.get(), &useHWAA)) { |
robertphillips | b56f927 | 2016-02-25 11:03:52 -0800 | [diff] [blame] | 819 | GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps(); |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 820 | SkAutoTUnref<GrDrawBatch> batch(GrOvalRenderer::CreateRRectBatch(paint.getColor(), |
robertphillips | b56f927 | 2016-02-25 11:03:52 -0800 | [diff] [blame] | 821 | viewMatrix, |
| 822 | rrect, |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 823 | stroke, |
robertphillips | b56f927 | 2016-02-25 11:03:52 -0800 | [diff] [blame] | 824 | shaderCaps)); |
| 825 | if (batch) { |
csmartdalton | ecbc12b | 2016-06-08 10:08:43 -0700 | [diff] [blame] | 826 | GrPipelineBuilder pipelineBuilder(paint, useHWAA); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 827 | this->getDrawTarget()->drawBatch(pipelineBuilder, this, clip, batch); |
robertphillips | b56f927 | 2016-02-25 11:03:52 -0800 | [diff] [blame] | 828 | return; |
| 829 | } |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 830 | } |
robertphillips | b56f927 | 2016-02-25 11:03:52 -0800 | [diff] [blame] | 831 | |
| 832 | SkPath path; |
| 833 | path.setIsVolatile(true); |
| 834 | path.addRRect(rrect); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 835 | this->internalDrawPath(clip, paint, viewMatrix, path, style); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 836 | } |
| 837 | |
robertphillips | 0009589 | 2016-02-29 13:50:40 -0800 | [diff] [blame] | 838 | bool GrDrawContext::drawFilledDRRect(const GrClip& clip, |
| 839 | const GrPaint& paintIn, |
| 840 | const SkMatrix& viewMatrix, |
| 841 | const SkRRect& origOuter, |
| 842 | const SkRRect& origInner) { |
| 843 | SkASSERT(!origInner.isEmpty()); |
| 844 | SkASSERT(!origOuter.isEmpty()); |
| 845 | |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 846 | if (InstancedRendering* ir = this->getDrawTarget()->instancedRendering()) { |
| 847 | bool useHWAA; |
| 848 | SkAutoTUnref<GrDrawBatch> batch(ir->recordDRRect(origOuter, origInner, viewMatrix, |
| 849 | paintIn.getColor(), paintIn.isAntiAlias(), |
| 850 | fInstancedPipelineInfo, &useHWAA)); |
| 851 | if (batch) { |
| 852 | GrPipelineBuilder pipelineBuilder(paintIn, useHWAA); |
| 853 | this->getDrawTarget()->drawBatch(pipelineBuilder, this, clip, batch); |
| 854 | return true; |
| 855 | } |
| 856 | } |
| 857 | |
robertphillips | 0009589 | 2016-02-29 13:50:40 -0800 | [diff] [blame] | 858 | bool applyAA = paintIn.isAntiAlias() && !fRenderTarget->isUnifiedMultisampled(); |
| 859 | |
| 860 | GrPrimitiveEdgeType innerEdgeType = applyAA ? kInverseFillAA_GrProcessorEdgeType : |
| 861 | kInverseFillBW_GrProcessorEdgeType; |
| 862 | GrPrimitiveEdgeType outerEdgeType = applyAA ? kFillAA_GrProcessorEdgeType : |
| 863 | kFillBW_GrProcessorEdgeType; |
| 864 | |
| 865 | SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter); |
| 866 | SkMatrix inverseVM; |
| 867 | if (!viewMatrix.isIdentity()) { |
| 868 | if (!origInner.transform(viewMatrix, inner.writable())) { |
| 869 | return false; |
| 870 | } |
| 871 | if (!origOuter.transform(viewMatrix, outer.writable())) { |
| 872 | return false; |
| 873 | } |
| 874 | if (!viewMatrix.invert(&inverseVM)) { |
| 875 | return false; |
| 876 | } |
| 877 | } else { |
| 878 | inverseVM.reset(); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 879 | } |
robertphillips | 0009589 | 2016-02-29 13:50:40 -0800 | [diff] [blame] | 880 | |
| 881 | GrPaint grPaint(paintIn); |
| 882 | grPaint.setAntiAlias(false); |
| 883 | |
| 884 | // TODO these need to be a geometry processors |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 885 | sk_sp<GrFragmentProcessor> innerEffect(GrRRectEffect::Make(innerEdgeType, *inner)); |
robertphillips | 0009589 | 2016-02-29 13:50:40 -0800 | [diff] [blame] | 886 | if (!innerEffect) { |
| 887 | return false; |
| 888 | } |
| 889 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 890 | sk_sp<GrFragmentProcessor> outerEffect(GrRRectEffect::Make(outerEdgeType, *outer)); |
robertphillips | 0009589 | 2016-02-29 13:50:40 -0800 | [diff] [blame] | 891 | if (!outerEffect) { |
| 892 | return false; |
| 893 | } |
| 894 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 895 | grPaint.addCoverageFragmentProcessor(std::move(innerEffect)); |
| 896 | grPaint.addCoverageFragmentProcessor(std::move(outerEffect)); |
robertphillips | 0009589 | 2016-02-29 13:50:40 -0800 | [diff] [blame] | 897 | |
| 898 | SkRect bounds = outer->getBounds(); |
| 899 | if (applyAA) { |
| 900 | bounds.outset(SK_ScalarHalf, SK_ScalarHalf); |
| 901 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 902 | |
robertphillips | 0009589 | 2016-02-29 13:50:40 -0800 | [diff] [blame] | 903 | this->fillRectWithLocalMatrix(clip, grPaint, SkMatrix::I(), bounds, inverseVM); |
| 904 | return true; |
| 905 | } |
| 906 | |
| 907 | void GrDrawContext::drawDRRect(const GrClip& clip, |
| 908 | const GrPaint& paint, |
| 909 | const SkMatrix& viewMatrix, |
| 910 | const SkRRect& outer, |
| 911 | const SkRRect& inner) { |
| 912 | ASSERT_SINGLE_OWNER |
| 913 | RETURN_IF_ABANDONED |
| 914 | SkDEBUGCODE(this->validate();) |
| 915 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::drawDRRect"); |
| 916 | |
| 917 | SkASSERT(!outer.isEmpty()); |
| 918 | SkASSERT(!inner.isEmpty()); |
| 919 | |
| 920 | AutoCheckFlush acf(fDrawingManager); |
| 921 | |
| 922 | if (this->drawFilledDRRect(clip, paint, viewMatrix, outer, inner)) { |
| 923 | return; |
| 924 | } |
| 925 | |
| 926 | SkPath path; |
| 927 | path.setIsVolatile(true); |
| 928 | path.addRRect(inner); |
| 929 | path.addRRect(outer); |
| 930 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 931 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 932 | this->internalDrawPath(clip, paint, viewMatrix, path, GrStyle::SimpleFill()); |
robertphillips | 0009589 | 2016-02-29 13:50:40 -0800 | [diff] [blame] | 933 | } |
| 934 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 935 | /////////////////////////////////////////////////////////////////////////////// |
| 936 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 937 | void GrDrawContext::drawOval(const GrClip& clip, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 938 | const GrPaint& paint, |
| 939 | const SkMatrix& viewMatrix, |
| 940 | const SkRect& oval, |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 941 | const GrStyle& style) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 942 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 943 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 944 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 945 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::drawOval"); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 946 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 947 | if (oval.isEmpty()) { |
| 948 | return; |
| 949 | } |
| 950 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 951 | SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 952 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 953 | AutoCheckFlush acf(fDrawingManager); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 954 | const SkStrokeRec& stroke = style.strokeRec(); |
csmartdalton | ecbc12b | 2016-06-08 10:08:43 -0700 | [diff] [blame] | 955 | bool useHWAA; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 956 | |
| 957 | if (this->getDrawTarget()->instancedRendering() && stroke.isFillStyle()) { |
| 958 | InstancedRendering* ir = this->getDrawTarget()->instancedRendering(); |
| 959 | SkAutoTUnref<GrDrawBatch> batch(ir->recordOval(oval, viewMatrix, paint.getColor(), |
| 960 | paint.isAntiAlias(), fInstancedPipelineInfo, |
| 961 | &useHWAA)); |
| 962 | if (batch) { |
| 963 | GrPipelineBuilder pipelineBuilder(paint, useHWAA); |
| 964 | this->getDrawTarget()->drawBatch(pipelineBuilder, this, clip, batch); |
| 965 | return; |
| 966 | } |
| 967 | } |
| 968 | |
csmartdalton | ecbc12b | 2016-06-08 10:08:43 -0700 | [diff] [blame] | 969 | if (should_apply_coverage_aa(paint, fRenderTarget.get(), &useHWAA)) { |
robertphillips | b56f927 | 2016-02-25 11:03:52 -0800 | [diff] [blame] | 970 | GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps(); |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 971 | SkAutoTUnref<GrDrawBatch> batch(GrOvalRenderer::CreateOvalBatch(paint.getColor(), |
robertphillips | b56f927 | 2016-02-25 11:03:52 -0800 | [diff] [blame] | 972 | viewMatrix, |
| 973 | oval, |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 974 | stroke, |
robertphillips | b56f927 | 2016-02-25 11:03:52 -0800 | [diff] [blame] | 975 | shaderCaps)); |
| 976 | if (batch) { |
csmartdalton | ecbc12b | 2016-06-08 10:08:43 -0700 | [diff] [blame] | 977 | GrPipelineBuilder pipelineBuilder(paint, useHWAA); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 978 | this->getDrawTarget()->drawBatch(pipelineBuilder, this, clip, batch); |
robertphillips | b56f927 | 2016-02-25 11:03:52 -0800 | [diff] [blame] | 979 | return; |
| 980 | } |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 981 | } |
robertphillips | b56f927 | 2016-02-25 11:03:52 -0800 | [diff] [blame] | 982 | |
| 983 | SkPath path; |
| 984 | path.setIsVolatile(true); |
| 985 | path.addOval(oval); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 986 | this->internalDrawPath(clip, paint, viewMatrix, path, style); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 987 | } |
| 988 | |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 989 | void GrDrawContext::drawImageNine(const GrClip& clip, |
| 990 | const GrPaint& paint, |
| 991 | const SkMatrix& viewMatrix, |
| 992 | int imageWidth, |
| 993 | int imageHeight, |
| 994 | const SkIRect& center, |
| 995 | const SkRect& dst) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 996 | ASSERT_SINGLE_OWNER |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 997 | RETURN_IF_ABANDONED |
| 998 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 999 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::drawImageNine"); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 1000 | |
| 1001 | AutoCheckFlush acf(fDrawingManager); |
| 1002 | |
| 1003 | SkAutoTUnref<GrDrawBatch> batch(GrNinePatch::CreateNonAA(paint.getColor(), viewMatrix, |
| 1004 | imageWidth, imageHeight, |
| 1005 | center, dst)); |
| 1006 | |
csmartdalton | ecbc12b | 2016-06-08 10:08:43 -0700 | [diff] [blame] | 1007 | GrPipelineBuilder pipelineBuilder(paint, this->mustUseHWAA(paint)); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 1008 | this->getDrawTarget()->drawBatch(pipelineBuilder, this, clip, batch); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 1009 | } |
| 1010 | |
robertphillips | 8c523e0 | 2016-07-26 07:41:00 -0700 | [diff] [blame] | 1011 | void GrDrawContext::prepareForExternalIO() { |
| 1012 | ASSERT_SINGLE_OWNER |
| 1013 | RETURN_IF_ABANDONED |
| 1014 | SkDEBUGCODE(this->validate();) |
| 1015 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::prepareForExternalIO"); |
| 1016 | |
| 1017 | ASSERT_OWNED_RESOURCE(fRenderTarget); |
| 1018 | |
| 1019 | fDrawingManager->getContext()->prepareSurfaceForExternalIO(fRenderTarget.get()); |
| 1020 | } |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 1021 | |
robertphillips | 4430239 | 2016-07-08 14:43:03 -0700 | [diff] [blame] | 1022 | void GrDrawContext::drawNonAAFilledRect(const GrClip& clip, |
| 1023 | const GrPaint& paint, |
| 1024 | const SkMatrix& viewMatrix, |
| 1025 | const SkRect& rect, |
| 1026 | const SkRect* localRect, |
| 1027 | const SkMatrix* localMatrix, |
| 1028 | const GrUserStencilSettings* ss) { |
| 1029 | SkAutoTUnref<GrDrawBatch> batch( |
| 1030 | GrRectBatchFactory::CreateNonAAFill(paint.getColor(), viewMatrix, rect, localRect, |
| 1031 | localMatrix)); |
| 1032 | GrPipelineBuilder pipelineBuilder(paint, this->mustUseHWAA(paint)); |
| 1033 | if (ss) { |
| 1034 | pipelineBuilder.setUserStencil(ss); |
| 1035 | } |
| 1036 | this->getDrawTarget()->drawBatch(pipelineBuilder, this, clip, batch); |
| 1037 | } |
| 1038 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1039 | // Can 'path' be drawn as a pair of filled nested rectangles? |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 1040 | static bool fills_as_nested_rects(const SkMatrix& viewMatrix, const SkPath& path, SkRect rects[2]) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1041 | |
| 1042 | if (path.isInverseFillType()) { |
| 1043 | return false; |
| 1044 | } |
| 1045 | |
| 1046 | // TODO: this restriction could be lifted if we were willing to apply |
| 1047 | // the matrix to all the points individually rather than just to the rect |
robertphillips | 0e7029e | 2015-11-30 05:45:06 -0800 | [diff] [blame] | 1048 | if (!viewMatrix.rectStaysRect()) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1049 | return false; |
| 1050 | } |
| 1051 | |
| 1052 | SkPath::Direction dirs[2]; |
| 1053 | if (!path.isNestedFillRects(rects, dirs)) { |
| 1054 | return false; |
| 1055 | } |
| 1056 | |
| 1057 | if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) { |
| 1058 | // The two rects need to be wound opposite to each other |
| 1059 | return false; |
| 1060 | } |
| 1061 | |
| 1062 | // Right now, nested rects where the margin is not the same width |
| 1063 | // all around do not render correctly |
| 1064 | const SkScalar* outer = rects[0].asScalars(); |
| 1065 | const SkScalar* inner = rects[1].asScalars(); |
| 1066 | |
| 1067 | bool allEq = true; |
| 1068 | |
| 1069 | SkScalar margin = SkScalarAbs(outer[0] - inner[0]); |
| 1070 | bool allGoE1 = margin >= SK_Scalar1; |
| 1071 | |
| 1072 | for (int i = 1; i < 4; ++i) { |
| 1073 | SkScalar temp = SkScalarAbs(outer[i] - inner[i]); |
| 1074 | if (temp < SK_Scalar1) { |
| 1075 | allGoE1 = false; |
| 1076 | } |
| 1077 | if (!SkScalarNearlyEqual(margin, temp)) { |
| 1078 | allEq = false; |
| 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | return allEq || allGoE1; |
| 1083 | } |
| 1084 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 1085 | void GrDrawContext::drawPath(const GrClip& clip, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1086 | const GrPaint& paint, |
| 1087 | const SkMatrix& viewMatrix, |
| 1088 | const SkPath& path, |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 1089 | const GrStyle& style) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1090 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1091 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 1092 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 1093 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::drawPath"); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 1094 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1095 | if (path.isEmpty()) { |
| 1096 | if (path.isInverseFillType()) { |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 1097 | this->drawPaint(clip, paint, viewMatrix); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1098 | } |
| 1099 | return; |
| 1100 | } |
| 1101 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 1102 | AutoCheckFlush acf(fDrawingManager); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1103 | |
csmartdalton | ecbc12b | 2016-06-08 10:08:43 -0700 | [diff] [blame] | 1104 | bool useHWAA; |
| 1105 | if (should_apply_coverage_aa(paint, fRenderTarget.get(), &useHWAA) && !style.pathEffect()) { |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 1106 | if (style.isSimpleFill() && !path.isConvex()) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1107 | // Concave AA paths are expensive - try to avoid them for special cases |
| 1108 | SkRect rects[2]; |
| 1109 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 1110 | if (fills_as_nested_rects(viewMatrix, path, rects)) { |
joshualitt | d2b23e0 | 2015-08-21 10:53:34 -0700 | [diff] [blame] | 1111 | SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateAAFillNestedRects( |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 1112 | paint.getColor(), viewMatrix, rects)); |
bsalomon | 40ef485 | 2016-05-02 13:22:13 -0700 | [diff] [blame] | 1113 | if (batch) { |
csmartdalton | ecbc12b | 2016-06-08 10:08:43 -0700 | [diff] [blame] | 1114 | GrPipelineBuilder pipelineBuilder(paint, useHWAA); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 1115 | this->getDrawTarget()->drawBatch(pipelineBuilder, this, clip, batch); |
bsalomon | 40ef485 | 2016-05-02 13:22:13 -0700 | [diff] [blame] | 1116 | } |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1117 | return; |
| 1118 | } |
| 1119 | } |
| 1120 | SkRect ovalRect; |
| 1121 | bool isOval = path.isOval(&ovalRect); |
| 1122 | |
| 1123 | if (isOval && !path.isInverseFillType()) { |
robertphillips | 0cc2f85 | 2016-02-24 13:36:56 -0800 | [diff] [blame] | 1124 | GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps(); |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 1125 | SkAutoTUnref<GrDrawBatch> batch(GrOvalRenderer::CreateOvalBatch(paint.getColor(), |
robertphillips | 0cc2f85 | 2016-02-24 13:36:56 -0800 | [diff] [blame] | 1126 | viewMatrix, |
robertphillips | 0cc2f85 | 2016-02-24 13:36:56 -0800 | [diff] [blame] | 1127 | ovalRect, |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 1128 | style.strokeRec(), |
robertphillips | 0cc2f85 | 2016-02-24 13:36:56 -0800 | [diff] [blame] | 1129 | shaderCaps)); |
| 1130 | if (batch) { |
csmartdalton | ecbc12b | 2016-06-08 10:08:43 -0700 | [diff] [blame] | 1131 | GrPipelineBuilder pipelineBuilder(paint, useHWAA); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 1132 | this->getDrawTarget()->drawBatch(pipelineBuilder, this, clip, batch); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1133 | return; |
| 1134 | } |
| 1135 | } |
| 1136 | } |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 1137 | |
| 1138 | // Note that internalDrawPath may sw-rasterize the path into a scratch texture. |
| 1139 | // Scratch textures can be recycled after they are returned to the texture |
| 1140 | // cache. This presents a potential hazard for buffered drawing. However, |
| 1141 | // the writePixels that uploads to the scratch will perform a flush so we're |
| 1142 | // OK. |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 1143 | this->internalDrawPath(clip, paint, viewMatrix, path, style); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1144 | } |
| 1145 | |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 1146 | bool GrDrawContextPriv::drawAndStencilPath(const GrFixedClip& clip, |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 1147 | const GrUserStencilSettings* ss, |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 1148 | SkRegion::Op op, |
| 1149 | bool invert, |
| 1150 | bool doAA, |
| 1151 | const SkMatrix& viewMatrix, |
| 1152 | const SkPath& path) { |
| 1153 | ASSERT_SINGLE_OWNER_PRIV |
| 1154 | RETURN_FALSE_IF_ABANDONED_PRIV |
| 1155 | SkDEBUGCODE(fDrawContext->validate();) |
| 1156 | GR_AUDIT_TRAIL_AUTO_FRAME(fDrawContext->fAuditTrail, "GrDrawContext::drawPath"); |
| 1157 | |
| 1158 | if (path.isEmpty() && path.isInverseFillType()) { |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 1159 | this->drawAndStencilRect(clip, ss, op, invert, false, SkMatrix::I(), |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 1160 | SkRect::MakeIWH(fDrawContext->width(), |
| 1161 | fDrawContext->height())); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 1162 | return true; |
| 1163 | } |
| 1164 | |
| 1165 | AutoCheckFlush acf(fDrawContext->fDrawingManager); |
| 1166 | |
| 1167 | // An Assumption here is that path renderer would use some form of tweaking |
| 1168 | // the src color (either the input alpha or in the frag shader) to implement |
| 1169 | // aa. If we have some future driver-mojo path AA that can do the right |
| 1170 | // thing WRT to the blend then we'll need some query on the PR. |
| 1171 | bool useCoverageAA = doAA && !fDrawContext->fRenderTarget->isUnifiedMultisampled(); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 1172 | bool hasUserStencilSettings = !ss->isUnused(); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 1173 | bool isStencilBufferMSAA = fDrawContext->fRenderTarget->isStencilBufferMultisampled(); |
| 1174 | |
| 1175 | const GrPathRendererChain::DrawType type = |
| 1176 | useCoverageAA ? GrPathRendererChain::kColorAntiAlias_DrawType |
| 1177 | : GrPathRendererChain::kColor_DrawType; |
| 1178 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 1179 | GrShape shape(path, GrStyle::SimpleFill()); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 1180 | GrPathRenderer::CanDrawPathArgs canDrawArgs; |
| 1181 | canDrawArgs.fShaderCaps = fDrawContext->fDrawingManager->getContext()->caps()->shaderCaps(); |
| 1182 | canDrawArgs.fViewMatrix = &viewMatrix; |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 1183 | canDrawArgs.fShape = &shape; |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 1184 | canDrawArgs.fAntiAlias = useCoverageAA; |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 1185 | canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings; |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 1186 | canDrawArgs.fIsStencilBufferMSAA = isStencilBufferMSAA; |
| 1187 | |
| 1188 | // Don't allow the SW renderer |
| 1189 | GrPathRenderer* pr = fDrawContext->fDrawingManager->getPathRenderer(canDrawArgs, false, type); |
| 1190 | if (!pr) { |
| 1191 | return false; |
| 1192 | } |
| 1193 | |
| 1194 | GrPaint paint; |
| 1195 | paint.setCoverageSetOpXPFactory(op, invert); |
| 1196 | |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 1197 | GrPathRenderer::DrawPathArgs args; |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 1198 | args.fResourceProvider = fDrawContext->fDrawingManager->getContext()->resourceProvider(); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 1199 | args.fPaint = &paint; |
| 1200 | args.fUserStencilSettings = ss; |
| 1201 | args.fDrawContext = fDrawContext; |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 1202 | args.fClip = &clip; |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 1203 | args.fViewMatrix = &viewMatrix; |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 1204 | args.fShape = &shape; |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 1205 | args.fAntiAlias = useCoverageAA; |
brianosman | 0e3c554 | 2016-04-13 13:56:21 -0700 | [diff] [blame] | 1206 | args.fGammaCorrect = fDrawContext->isGammaCorrect(); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 1207 | pr->drawPath(args); |
| 1208 | return true; |
| 1209 | } |
| 1210 | |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 1211 | void GrDrawContext::internalDrawPath(const GrClip& clip, |
| 1212 | const GrPaint& paint, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1213 | const SkMatrix& viewMatrix, |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 1214 | const SkPath& path, |
| 1215 | const GrStyle& style) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1216 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1217 | RETURN_IF_ABANDONED |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 1218 | SkASSERT(!path.isEmpty()); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1219 | |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 1220 | bool useCoverageAA = should_apply_coverage_aa(paint, fRenderTarget.get()); |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 1221 | constexpr bool kHasUserStencilSettings = false; |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 1222 | bool isStencilBufferMSAA = fRenderTarget->isStencilBufferMultisampled(); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1223 | |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 1224 | const GrPathRendererChain::DrawType type = |
| 1225 | useCoverageAA ? GrPathRendererChain::kColorAntiAlias_DrawType |
| 1226 | : GrPathRendererChain::kColor_DrawType; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1227 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 1228 | GrShape shape(path, style); |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 1229 | if (shape.isEmpty()) { |
| 1230 | return; |
| 1231 | } |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 1232 | GrPathRenderer::CanDrawPathArgs canDrawArgs; |
| 1233 | canDrawArgs.fShaderCaps = fDrawingManager->getContext()->caps()->shaderCaps(); |
| 1234 | canDrawArgs.fViewMatrix = &viewMatrix; |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 1235 | canDrawArgs.fShape = &shape; |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 1236 | canDrawArgs.fAntiAlias = useCoverageAA; |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 1237 | canDrawArgs.fHasUserStencilSettings = kHasUserStencilSettings; |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 1238 | canDrawArgs.fIsStencilBufferMSAA = isStencilBufferMSAA; |
| 1239 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 1240 | // Try a 1st time without applying any of the style to the geometry (and barring sw) |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 1241 | GrPathRenderer* pr = fDrawingManager->getPathRenderer(canDrawArgs, false, type); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 1242 | SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1243 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 1244 | if (!pr && shape.style().pathEffect()) { |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 1245 | // It didn't work above, so try again with the path effect applied. |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 1246 | shape = shape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale); |
| 1247 | if (shape.isEmpty()) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1248 | return; |
| 1249 | } |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 1250 | pr = fDrawingManager->getPathRenderer(canDrawArgs, false, type); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1251 | } |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 1252 | if (!pr) { |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 1253 | if (shape.style().applies()) { |
| 1254 | shape = shape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale); |
| 1255 | if (shape.isEmpty()) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1256 | return; |
| 1257 | } |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 1258 | } |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1259 | // This time, allow SW renderer |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 1260 | pr = fDrawingManager->getPathRenderer(canDrawArgs, true, type); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1261 | } |
| 1262 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 1263 | if (!pr) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1264 | #ifdef SK_DEBUG |
| 1265 | SkDebugf("Unable to find path renderer compatible with path.\n"); |
| 1266 | #endif |
| 1267 | return; |
| 1268 | } |
| 1269 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 1270 | GrPathRenderer::DrawPathArgs args; |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 1271 | args.fResourceProvider = fDrawingManager->getContext()->resourceProvider(); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 1272 | args.fPaint = &paint; |
| 1273 | args.fUserStencilSettings = &GrUserStencilSettings::kUnused; |
| 1274 | args.fDrawContext = this; |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 1275 | args.fClip = &clip; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 1276 | args.fViewMatrix = &viewMatrix; |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 1277 | args.fShape = canDrawArgs.fShape; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 1278 | args.fAntiAlias = useCoverageAA; |
brianosman | 0e3c554 | 2016-04-13 13:56:21 -0700 | [diff] [blame] | 1279 | args.fGammaCorrect = this->isGammaCorrect(); |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 1280 | pr->drawPath(args); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1281 | } |
| 1282 | |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 1283 | void GrDrawContext::drawBatch(const GrPipelineBuilder& pipelineBuilder, const GrClip& clip, |
| 1284 | GrDrawBatch* batch) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1285 | ASSERT_SINGLE_OWNER |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 1286 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 1287 | SkDEBUGCODE(this->validate();) |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 1288 | GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::drawBatch"); |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 1289 | |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 1290 | this->getDrawTarget()->drawBatch(pipelineBuilder, this, clip, batch); |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 1291 | } |