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