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