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 | |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 8 | #include "src/gpu/GrRenderTargetContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "include/core/SkDrawable.h" |
| 10 | #include "include/gpu/GrBackendSemaphore.h" |
| 11 | #include "include/gpu/GrRenderTarget.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/private/GrRecordingContext.h" |
| 13 | #include "include/private/SkShadowFlags.h" |
| 14 | #include "include/utils/SkShadowUtils.h" |
Brian Salomon | cd734f6 | 2019-05-10 16:32:54 -0400 | [diff] [blame] | 15 | #include "src/core/SkAutoPixmapStorage.h" |
| 16 | #include "src/core/SkConvertPixels.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/core/SkDrawShadowInfo.h" |
| 18 | #include "src/core/SkGlyphRunPainter.h" |
| 19 | #include "src/core/SkLatticeIter.h" |
| 20 | #include "src/core/SkMatrixPriv.h" |
| 21 | #include "src/core/SkRRectPriv.h" |
| 22 | #include "src/core/SkSurfacePriv.h" |
| 23 | #include "src/gpu/GrAppliedClip.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 24 | #include "src/gpu/GrAuditTrail.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "src/gpu/GrBlurUtils.h" |
| 26 | #include "src/gpu/GrCaps.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 27 | #include "src/gpu/GrColor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 28 | #include "src/gpu/GrContextPriv.h" |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 29 | #include "src/gpu/GrDataUtils.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 30 | #include "src/gpu/GrDrawingManager.h" |
| 31 | #include "src/gpu/GrFixedClip.h" |
| 32 | #include "src/gpu/GrGpuResourcePriv.h" |
| 33 | #include "src/gpu/GrMemoryPool.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 34 | #include "src/gpu/GrOpList.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 35 | #include "src/gpu/GrPathRenderer.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 36 | #include "src/gpu/GrRecordingContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 37 | #include "src/gpu/GrRenderTargetContextPriv.h" |
| 38 | #include "src/gpu/GrResourceProvider.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 39 | #include "src/gpu/GrStencilAttachment.h" |
| 40 | #include "src/gpu/GrStyle.h" |
| 41 | #include "src/gpu/GrTracing.h" |
| 42 | #include "src/gpu/SkGr.h" |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 43 | #include "src/gpu/effects/GrBicubicEffect.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 44 | #include "src/gpu/effects/GrRRectEffect.h" |
| 45 | #include "src/gpu/effects/GrTextureDomain.h" |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 46 | #include "src/gpu/effects/generated/GrColorMatrixFragmentProcessor.h" |
Michael Ludwig | fd4f4df | 2019-05-29 09:51:09 -0400 | [diff] [blame] | 47 | #include "src/gpu/geometry/GrQuad.h" |
Michael Ludwig | 6132820 | 2019-06-19 14:48:58 +0000 | [diff] [blame] | 48 | #include "src/gpu/geometry/GrQuadUtils.h" |
Michael Ludwig | 663afe5 | 2019-06-03 16:46:19 -0400 | [diff] [blame] | 49 | #include "src/gpu/geometry/GrShape.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 50 | #include "src/gpu/ops/GrAtlasTextOp.h" |
| 51 | #include "src/gpu/ops/GrClearOp.h" |
| 52 | #include "src/gpu/ops/GrClearStencilClipOp.h" |
| 53 | #include "src/gpu/ops/GrDebugMarkerOp.h" |
| 54 | #include "src/gpu/ops/GrDrawAtlasOp.h" |
| 55 | #include "src/gpu/ops/GrDrawOp.h" |
| 56 | #include "src/gpu/ops/GrDrawVerticesOp.h" |
| 57 | #include "src/gpu/ops/GrDrawableOp.h" |
| 58 | #include "src/gpu/ops/GrFillRRectOp.h" |
| 59 | #include "src/gpu/ops/GrFillRectOp.h" |
| 60 | #include "src/gpu/ops/GrLatticeOp.h" |
| 61 | #include "src/gpu/ops/GrOp.h" |
| 62 | #include "src/gpu/ops/GrOvalOpFactory.h" |
| 63 | #include "src/gpu/ops/GrRegionOp.h" |
| 64 | #include "src/gpu/ops/GrSemaphoreOp.h" |
| 65 | #include "src/gpu/ops/GrShadowRRectOp.h" |
| 66 | #include "src/gpu/ops/GrStencilPathOp.h" |
| 67 | #include "src/gpu/ops/GrStrokeRectOp.h" |
| 68 | #include "src/gpu/ops/GrTextureOp.h" |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 69 | #include "src/gpu/ops/GrTransferFromOp.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 70 | #include "src/gpu/text/GrTextContext.h" |
| 71 | #include "src/gpu/text/GrTextTarget.h" |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 72 | |
Herb Derby | c1b482c | 2018-08-09 15:02:27 -0400 | [diff] [blame] | 73 | class GrRenderTargetContext::TextTarget : public GrTextTarget { |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 74 | public: |
| 75 | TextTarget(GrRenderTargetContext* renderTargetContext) |
Herb Derby | c1b482c | 2018-08-09 15:02:27 -0400 | [diff] [blame] | 76 | : GrTextTarget(renderTargetContext->width(), renderTargetContext->height(), |
Robert Phillips | 7e90be9 | 2019-02-15 12:22:59 -0500 | [diff] [blame] | 77 | renderTargetContext->colorSpaceInfo()) |
Herb Derby | 74c6ed3 | 2018-07-28 18:07:54 -0400 | [diff] [blame] | 78 | , fRenderTargetContext(renderTargetContext) |
Herb Derby | 6595687 | 2018-08-21 16:55:04 -0400 | [diff] [blame] | 79 | , fGlyphPainter{*renderTargetContext}{} |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 80 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 81 | void addDrawOp(const GrClip& clip, std::unique_ptr<GrAtlasTextOp> op) override { |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 82 | fRenderTargetContext->addDrawOp(clip, std::move(op)); |
| 83 | } |
| 84 | |
Robert Phillips | 46a1338 | 2018-08-23 13:53:01 -0400 | [diff] [blame] | 85 | void drawShape(const GrClip& clip, const SkPaint& paint, |
| 86 | const SkMatrix& viewMatrix, const GrShape& shape) override { |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 87 | GrBlurUtils::drawShapeWithMaskFilter(fRenderTargetContext->fContext, fRenderTargetContext, |
| 88 | clip, paint, viewMatrix, shape); |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void makeGrPaint(GrMaskFormat maskFormat, const SkPaint& skPaint, const SkMatrix& viewMatrix, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 92 | GrPaint* grPaint) override { |
Robert Phillips | 6989370 | 2019-02-22 11:16:30 -0500 | [diff] [blame] | 93 | auto context = fRenderTargetContext->fContext; |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 94 | const GrColorSpaceInfo& colorSpaceInfo = fRenderTargetContext->colorSpaceInfo(); |
| 95 | if (kARGB_GrMaskFormat == maskFormat) { |
| 96 | SkPaintToGrPaintWithPrimitiveColor(context, colorSpaceInfo, skPaint, grPaint); |
| 97 | } else { |
| 98 | SkPaintToGrPaint(context, colorSpaceInfo, skPaint, viewMatrix, grPaint); |
| 99 | } |
| 100 | } |
| 101 | |
Robert Phillips | 6989370 | 2019-02-22 11:16:30 -0500 | [diff] [blame] | 102 | GrRecordingContext* getContext() override { |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 103 | return fRenderTargetContext->fContext; |
| 104 | } |
| 105 | |
Herb Derby | 6595687 | 2018-08-21 16:55:04 -0400 | [diff] [blame] | 106 | SkGlyphRunListPainter* glyphPainter() override { |
| 107 | return &fGlyphPainter; |
Herb Derby | 74c6ed3 | 2018-07-28 18:07:54 -0400 | [diff] [blame] | 108 | } |
| 109 | |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 110 | private: |
| 111 | GrRenderTargetContext* fRenderTargetContext; |
Herb Derby | 6595687 | 2018-08-21 16:55:04 -0400 | [diff] [blame] | 112 | SkGlyphRunListPainter fGlyphPainter; |
Herb Derby | 74c6ed3 | 2018-07-28 18:07:54 -0400 | [diff] [blame] | 113 | |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 114 | }; |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 115 | |
Robert Phillips | 7215283 | 2017-01-25 17:31:35 -0500 | [diff] [blame] | 116 | #define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext()) |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 117 | #define ASSERT_SINGLE_OWNER \ |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 118 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());) |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 119 | #define ASSERT_SINGLE_OWNER_PRIV \ |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 120 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fRenderTargetContext->singleOwner());) |
Robert Phillips | 6989370 | 2019-02-22 11:16:30 -0500 | [diff] [blame] | 121 | #define RETURN_IF_ABANDONED if (fContext->priv().abandoned()) { return; } |
| 122 | #define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return; } |
| 123 | #define RETURN_FALSE_IF_ABANDONED if (fContext->priv().abandoned()) { return false; } |
| 124 | #define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return false; } |
| 125 | #define RETURN_NULL_IF_ABANDONED if (fContext->priv().abandoned()) { return nullptr; } |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 126 | |
Brian Salomon | e225b56 | 2017-06-14 13:00:03 -0400 | [diff] [blame] | 127 | ////////////////////////////////////////////////////////////////////////////// |
| 128 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 129 | class AutoCheckFlush { |
| 130 | public: |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 131 | AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) { |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 132 | SkASSERT(fDrawingManager); |
| 133 | } |
bsalomon | b77a907 | 2016-09-07 10:02:04 -0700 | [diff] [blame] | 134 | ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); } |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 135 | |
| 136 | private: |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 137 | GrDrawingManager* fDrawingManager; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 138 | }; |
| 139 | |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 140 | // In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 141 | // GrOpLists to be picked up and added to by renderTargetContexts lower in the call |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 142 | // stack. When this occurs with a closed GrOpList, a new one will be allocated |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 143 | // when the renderTargetContext attempts to use it (via getOpList). |
Robert Phillips | 6989370 | 2019-02-22 11:16:30 -0500 | [diff] [blame] | 144 | GrRenderTargetContext::GrRenderTargetContext(GrRecordingContext* context, |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 145 | sk_sp<GrRenderTargetProxy> rtp, |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 146 | GrColorType colorType, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 147 | sk_sp<SkColorSpace> colorSpace, |
| 148 | const SkSurfaceProps* surfaceProps, |
Robert Phillips | 941d144 | 2017-06-14 16:37:02 -0400 | [diff] [blame] | 149 | bool managedOpList) |
Brian Salomon | bd3d8d3 | 2019-07-02 09:16:28 -0400 | [diff] [blame] | 150 | : GrSurfaceContext(context, colorType, kPremul_SkAlphaType, std::move(colorSpace)) |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 151 | , fRenderTargetProxy(std::move(rtp)) |
| 152 | , fOpList(sk_ref_sp(fRenderTargetProxy->getLastRenderTargetOpList())) |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 153 | , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps)) |
| 154 | , fManagedOpList(managedOpList) { |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 155 | fTextTarget.reset(new TextTarget(this)); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 156 | SkDEBUGCODE(this->validate();) |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 157 | } |
| 158 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 159 | #ifdef SK_DEBUG |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 160 | void GrRenderTargetContext::validate() const { |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 161 | SkASSERT(fRenderTargetProxy); |
| 162 | fRenderTargetProxy->validate(fContext); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 163 | |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 164 | if (fOpList && !fOpList->isClosed()) { |
Robert Phillips | dc83b89 | 2017-04-13 12:23:54 -0400 | [diff] [blame] | 165 | SkASSERT(fRenderTargetProxy->getLastOpList() == fOpList.get()); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 166 | } |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 167 | } |
| 168 | #endif |
| 169 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 170 | GrRenderTargetContext::~GrRenderTargetContext() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 171 | ASSERT_SINGLE_OWNER |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Chris Dalton | 7d6748e | 2019-03-13 00:34:52 -0600 | [diff] [blame] | 174 | inline GrAAType GrRenderTargetContext::chooseAAType(GrAA aa) { |
Chris Dalton | 7d6748e | 2019-03-13 00:34:52 -0600 | [diff] [blame] | 175 | if (GrAA::kNo == aa) { |
| 176 | // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect |
| 177 | // that. |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 178 | if (this->numSamples() > 1 && !this->caps()->multisampleDisableSupport()) { |
Chris Dalton | 7d6748e | 2019-03-13 00:34:52 -0600 | [diff] [blame] | 179 | return GrAAType::kMSAA; |
| 180 | } |
| 181 | return GrAAType::kNone; |
| 182 | } |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 183 | return (this->numSamples() > 1) ? GrAAType::kMSAA : GrAAType::kCoverage; |
Chris Dalton | 7d6748e | 2019-03-13 00:34:52 -0600 | [diff] [blame] | 184 | } |
| 185 | |
Robert Phillips | f200a90 | 2017-01-30 13:27:37 -0500 | [diff] [blame] | 186 | GrTextureProxy* GrRenderTargetContext::asTextureProxy() { |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 187 | return fRenderTargetProxy->asTextureProxy(); |
| 188 | } |
| 189 | |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 190 | const GrTextureProxy* GrRenderTargetContext::asTextureProxy() const { |
| 191 | return fRenderTargetProxy->asTextureProxy(); |
| 192 | } |
| 193 | |
Robert Phillips | f200a90 | 2017-01-30 13:27:37 -0500 | [diff] [blame] | 194 | sk_sp<GrTextureProxy> GrRenderTargetContext::asTextureProxyRef() { |
| 195 | return sk_ref_sp(fRenderTargetProxy->asTextureProxy()); |
| 196 | } |
| 197 | |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 198 | GrMipMapped GrRenderTargetContext::mipMapped() const { |
| 199 | if (const GrTextureProxy* proxy = this->asTextureProxy()) { |
| 200 | return proxy->mipMapped(); |
| 201 | } |
| 202 | return GrMipMapped::kNo; |
| 203 | } |
| 204 | |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 205 | GrRenderTargetOpList* GrRenderTargetContext::getRTOpList() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 206 | ASSERT_SINGLE_OWNER |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 207 | SkDEBUGCODE(this->validate();) |
| 208 | |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 209 | if (!fOpList || fOpList->isClosed()) { |
Michael Ludwig | 6e17f1d | 2019-05-15 14:00:20 +0000 | [diff] [blame] | 210 | fOpList = this->drawingManager()->newRTOpList(fRenderTargetProxy, fManagedOpList); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Robert Phillips | dc83b89 | 2017-04-13 12:23:54 -0400 | [diff] [blame] | 213 | return fOpList.get(); |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 216 | GrOpList* GrRenderTargetContext::getOpList() { |
| 217 | return this->getRTOpList(); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Herb Derby | cddab25 | 2018-07-16 11:19:04 -0400 | [diff] [blame] | 220 | void GrRenderTargetContext::drawGlyphRunList( |
| 221 | const GrClip& clip, const SkMatrix& viewMatrix, |
Robert Phillips | e4643cc | 2018-08-14 13:01:29 -0400 | [diff] [blame] | 222 | const SkGlyphRunList& blob) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 223 | ASSERT_SINGLE_OWNER |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 224 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 225 | SkDEBUGCODE(this->validate();) |
Herb Derby | cddab25 | 2018-07-16 11:19:04 -0400 | [diff] [blame] | 226 | GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawGlyphRunList", fContext); |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 227 | |
Greg Daniel | be7fc46 | 2019-01-03 16:40:42 -0500 | [diff] [blame] | 228 | // Drawing text can cause us to do inline uploads. This is not supported for wrapped vulkan |
| 229 | // secondary command buffers because it would require stopping and starting a render pass which |
| 230 | // we don't have access to. |
| 231 | if (this->wrapsVkSecondaryCB()) { |
| 232 | return; |
| 233 | } |
| 234 | |
Herb Derby | 26cbe51 | 2018-05-24 14:39:01 -0400 | [diff] [blame] | 235 | GrTextContext* atlasTextContext = this->drawingManager()->getTextContext(); |
Herb Derby | cddab25 | 2018-07-16 11:19:04 -0400 | [diff] [blame] | 236 | atlasTextContext->drawGlyphRunList(fContext, fTextTarget.get(), clip, viewMatrix, |
Robert Phillips | e4643cc | 2018-08-14 13:01:29 -0400 | [diff] [blame] | 237 | fSurfaceProps, blob); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 240 | void GrRenderTargetContext::discard() { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 241 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 242 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 243 | SkDEBUGCODE(this->validate();) |
Robert Phillips | 6b47c7d | 2017-08-29 07:24:09 -0400 | [diff] [blame] | 244 | GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 245 | |
Robert Phillips | 7215283 | 2017-01-25 17:31:35 -0500 | [diff] [blame] | 246 | AutoCheckFlush acf(this->drawingManager()); |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 247 | |
Robert Phillips | 380b90c | 2017-08-30 07:41:07 -0400 | [diff] [blame] | 248 | this->getRTOpList()->discard(); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 251 | void GrRenderTargetContext::clear(const SkIRect* rect, |
Brian Osman | 9a9baae | 2018-11-05 15:06:26 -0500 | [diff] [blame] | 252 | const SkPMColor4f& color, |
Chris Dalton | 344e903 | 2017-12-11 15:42:09 -0700 | [diff] [blame] | 253 | CanClearFullscreen canClearFullscreen) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 254 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 255 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 256 | SkDEBUGCODE(this->validate();) |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 257 | GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 258 | |
Robert Phillips | 7215283 | 2017-01-25 17:31:35 -0500 | [diff] [blame] | 259 | AutoCheckFlush acf(this->drawingManager()); |
Chris Dalton | 344e903 | 2017-12-11 15:42:09 -0700 | [diff] [blame] | 260 | this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color, |
| 261 | canClearFullscreen); |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 262 | } |
robertphillips | 9199a9f | 2016-07-13 07:48:43 -0700 | [diff] [blame] | 263 | |
Michael Ludwig | c39d0c8 | 2019-01-15 10:03:43 -0500 | [diff] [blame] | 264 | void GrRenderTargetContextPriv::clear(const GrFixedClip& clip, |
| 265 | const SkPMColor4f& color, |
| 266 | CanClearFullscreen canClearFullscreen) { |
| 267 | ASSERT_SINGLE_OWNER_PRIV |
| 268 | RETURN_IF_ABANDONED_PRIV |
| 269 | SkDEBUGCODE(fRenderTargetContext->validate();) |
| 270 | GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clear", |
| 271 | fRenderTargetContext->fContext); |
| 272 | |
| 273 | AutoCheckFlush acf(fRenderTargetContext->drawingManager()); |
| 274 | fRenderTargetContext->internalClear(clip, color, canClearFullscreen); |
| 275 | } |
| 276 | |
| 277 | static void clear_to_grpaint(const SkPMColor4f& color, GrPaint* paint) { |
| 278 | paint->setColor4f(color); |
| 279 | if (color.isOpaque()) { |
| 280 | // Can just rely on the src-over blend mode to do the right thing |
| 281 | paint->setPorterDuffXPFactory(SkBlendMode::kSrcOver); |
| 282 | } else { |
| 283 | // A clear overwrites the prior color, so even if it's transparent, it behaves as if it |
| 284 | // were src blended |
| 285 | paint->setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | void GrRenderTargetContext::internalClear(const GrFixedClip& clip, |
| 290 | const SkPMColor4f& color, |
| 291 | CanClearFullscreen canClearFullscreen) { |
| 292 | bool isFull = false; |
| 293 | if (!clip.hasWindowRectangles()) { |
Robert Phillips | 0e35ce2 | 2019-04-05 10:57:28 -0400 | [diff] [blame] | 294 | // TODO: wrt the shouldInitializeTextures path, it would be more performant to |
| 295 | // only clear the entire target if we knew it had not been cleared before. As |
| 296 | // is this could end up doing a lot of redundant clears. |
Michael Ludwig | c39d0c8 | 2019-01-15 10:03:43 -0500 | [diff] [blame] | 297 | isFull = !clip.scissorEnabled() || |
| 298 | (CanClearFullscreen::kYes == canClearFullscreen && |
Robert Phillips | 0e35ce2 | 2019-04-05 10:57:28 -0400 | [diff] [blame] | 299 | (this->caps()->preferFullscreenClears() || this->caps()->shouldInitializeTextures())) || |
Michael Ludwig | c39d0c8 | 2019-01-15 10:03:43 -0500 | [diff] [blame] | 300 | clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height())); |
| 301 | } |
| 302 | |
| 303 | if (isFull) { |
Chris Dalton | 6b98280 | 2019-06-27 13:53:46 -0600 | [diff] [blame] | 304 | GrRenderTargetOpList* opList = this->getRTOpList(); |
| 305 | if (opList->resetForFullscreenClear(this->canDiscardPreviousOpsOnFullClear()) && |
Michael Ludwig | c39d0c8 | 2019-01-15 10:03:43 -0500 | [diff] [blame] | 306 | !this->caps()->performColorClearsAsDraws()) { |
| 307 | // The op list was emptied and native clears are allowed, so just use the load op |
Chris Dalton | 6b98280 | 2019-06-27 13:53:46 -0600 | [diff] [blame] | 308 | opList->setColorLoadOp(GrLoadOp::kClear, color); |
Michael Ludwig | c39d0c8 | 2019-01-15 10:03:43 -0500 | [diff] [blame] | 309 | return; |
| 310 | } else { |
| 311 | // Will use an op for the clear, reset the load op to discard since the op will |
| 312 | // blow away the color buffer contents |
Chris Dalton | 6b98280 | 2019-06-27 13:53:46 -0600 | [diff] [blame] | 313 | opList->setColorLoadOp(GrLoadOp::kDiscard); |
Michael Ludwig | c39d0c8 | 2019-01-15 10:03:43 -0500 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | // Must add an op to the list (either because we couldn't use a load op, or because the |
| 317 | // clear load op isn't supported) |
| 318 | if (this->caps()->performColorClearsAsDraws()) { |
| 319 | SkRect rtRect = SkRect::MakeWH(this->width(), this->height()); |
| 320 | GrPaint paint; |
| 321 | clear_to_grpaint(color, &paint); |
| 322 | this->addDrawOp(GrFixedClip::Disabled(), |
Michael Ludwig | aa1b6b3 | 2019-05-29 14:43:13 -0400 | [diff] [blame] | 323 | GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(), |
| 324 | rtRect)); |
Michael Ludwig | c39d0c8 | 2019-01-15 10:03:43 -0500 | [diff] [blame] | 325 | } else { |
Chris Dalton | 6b98280 | 2019-06-27 13:53:46 -0600 | [diff] [blame] | 326 | opList->addOp(GrClearOp::Make(fContext, SkIRect::MakeEmpty(), color, |
| 327 | /* fullscreen */ true), *this->caps()); |
Michael Ludwig | c39d0c8 | 2019-01-15 10:03:43 -0500 | [diff] [blame] | 328 | } |
| 329 | } else { |
| 330 | if (this->caps()->performPartialClearsAsDraws()) { |
| 331 | // performPartialClearsAsDraws() also returns true if any clear has to be a draw. |
Michael Ludwig | c39d0c8 | 2019-01-15 10:03:43 -0500 | [diff] [blame] | 332 | GrPaint paint; |
| 333 | clear_to_grpaint(color, &paint); |
| 334 | |
Michael Ludwig | 64b28a7 | 2019-05-28 12:02:00 -0400 | [diff] [blame] | 335 | this->addDrawOp(clip, |
Michael Ludwig | aa1b6b3 | 2019-05-29 14:43:13 -0400 | [diff] [blame] | 336 | GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(), |
| 337 | SkRect::Make(clip.scissorRect()))); |
Michael Ludwig | c39d0c8 | 2019-01-15 10:03:43 -0500 | [diff] [blame] | 338 | } else { |
| 339 | std::unique_ptr<GrOp> op(GrClearOp::Make(fContext, clip, color, |
| 340 | this->asSurfaceProxy())); |
| 341 | // This version of the clear op factory can return null if the clip doesn't intersect |
| 342 | // with the surface proxy's boundary |
| 343 | if (!op) { |
| 344 | return; |
| 345 | } |
| 346 | this->getRTOpList()->addOp(std::move(op), *this->caps()); |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | |
Brian Osman | 9a9baae | 2018-11-05 15:06:26 -0500 | [diff] [blame] | 351 | void GrRenderTargetContextPriv::absClear(const SkIRect* clearRect, const SkPMColor4f& color) { |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 352 | ASSERT_SINGLE_OWNER_PRIV |
| 353 | RETURN_IF_ABANDONED_PRIV |
| 354 | SkDEBUGCODE(fRenderTargetContext->validate();) |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 355 | GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "absClear", |
| 356 | fRenderTargetContext->fContext); |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 357 | |
Robert Phillips | 7215283 | 2017-01-25 17:31:35 -0500 | [diff] [blame] | 358 | AutoCheckFlush acf(fRenderTargetContext->drawingManager()); |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 359 | |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 360 | SkIRect rtRect = SkIRect::MakeWH(fRenderTargetContext->fRenderTargetProxy->worstCaseWidth(), |
| 361 | fRenderTargetContext->fRenderTargetProxy->worstCaseHeight()); |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 362 | |
| 363 | if (clearRect) { |
| 364 | if (clearRect->contains(rtRect)) { |
| 365 | clearRect = nullptr; // full screen |
| 366 | } else { |
| 367 | if (!rtRect.intersect(*clearRect)) { |
| 368 | return; |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | // TODO: in a post-MDB world this should be handled at the OpList level. |
Michael Ludwig | c39d0c8 | 2019-01-15 10:03:43 -0500 | [diff] [blame] | 374 | // This makes sure to always add an op to the list, instead of marking the clear as a load op. |
| 375 | // This code follows very similar logic to internalClear() below, but critical differences are |
| 376 | // highlighted in line related to absClear()'s unique behavior. |
| 377 | if (clearRect) { |
| 378 | if (fRenderTargetContext->caps()->performPartialClearsAsDraws()) { |
Ethan Nicholas | 56d19a5 | 2018-10-15 11:26:20 -0400 | [diff] [blame] | 379 | GrPaint paint; |
Michael Ludwig | c39d0c8 | 2019-01-15 10:03:43 -0500 | [diff] [blame] | 380 | clear_to_grpaint(color, &paint); |
Michael Ludwig | aa1b6b3 | 2019-05-29 14:43:13 -0400 | [diff] [blame] | 381 | |
Michael Ludwig | c39d0c8 | 2019-01-15 10:03:43 -0500 | [diff] [blame] | 382 | // Use the disabled clip; the rect geometry already matches the clear rectangle and |
| 383 | // if it were added to a scissor, that would be intersected with the logical surface |
| 384 | // bounds and not the worst case dimensions required here. |
Michael Ludwig | 4a0cf50 | 2019-05-30 12:54:09 -0400 | [diff] [blame] | 385 | fRenderTargetContext->addDrawOp( |
| 386 | GrFixedClip::Disabled(), |
Michael Ludwig | aa1b6b3 | 2019-05-29 14:43:13 -0400 | [diff] [blame] | 387 | GrFillRectOp::MakeNonAARect(fRenderTargetContext->fContext, std::move(paint), |
| 388 | SkMatrix::I(), SkRect::Make(rtRect))); |
Michael Ludwig | c39d0c8 | 2019-01-15 10:03:43 -0500 | [diff] [blame] | 389 | } else { |
| 390 | // Must use the ClearOp factory that takes a boolean (false) instead of a surface |
| 391 | // proxy. The surface proxy variant would intersect the clip rect with its logical |
| 392 | // bounds, which is not desired in this special case. |
| 393 | fRenderTargetContext->getRTOpList()->addOp( |
| 394 | GrClearOp::Make(fRenderTargetContext->fContext, rtRect, color, |
| 395 | /* fullscreen */ false), |
| 396 | *fRenderTargetContext->caps()); |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 397 | } |
Michael Ludwig | c39d0c8 | 2019-01-15 10:03:43 -0500 | [diff] [blame] | 398 | } else { |
| 399 | // Reset the oplist like in internalClear(), but do not rely on a load op for the clear |
Chris Dalton | 6b98280 | 2019-06-27 13:53:46 -0600 | [diff] [blame] | 400 | fRenderTargetContext->getRTOpList()->resetForFullscreenClear( |
| 401 | fRenderTargetContext->canDiscardPreviousOpsOnFullClear()); |
Michael Ludwig | c39d0c8 | 2019-01-15 10:03:43 -0500 | [diff] [blame] | 402 | fRenderTargetContext->getRTOpList()->setColorLoadOp(GrLoadOp::kDiscard); |
| 403 | |
| 404 | if (fRenderTargetContext->caps()->performColorClearsAsDraws()) { |
| 405 | // This draws a quad covering the worst case dimensions instead of just the logical |
| 406 | // width and height like in internalClear(). |
| 407 | GrPaint paint; |
| 408 | clear_to_grpaint(color, &paint); |
Michael Ludwig | 4a0cf50 | 2019-05-30 12:54:09 -0400 | [diff] [blame] | 409 | fRenderTargetContext->addDrawOp( |
| 410 | GrFixedClip::Disabled(), |
Michael Ludwig | aa1b6b3 | 2019-05-29 14:43:13 -0400 | [diff] [blame] | 411 | GrFillRectOp::MakeNonAARect(fRenderTargetContext->fContext, std::move(paint), |
| 412 | SkMatrix::I(), SkRect::Make(rtRect))); |
Michael Ludwig | c39d0c8 | 2019-01-15 10:03:43 -0500 | [diff] [blame] | 413 | } else { |
| 414 | // Nothing special about this path in absClear compared to internalClear() |
| 415 | fRenderTargetContext->getRTOpList()->addOp( |
| 416 | GrClearOp::Make(fRenderTargetContext->fContext, SkIRect::MakeEmpty(), color, |
| 417 | /* fullscreen */ true), |
| 418 | *fRenderTargetContext->caps()); |
Ethan Nicholas | 56d19a5 | 2018-10-15 11:26:20 -0400 | [diff] [blame] | 419 | } |
robertphillips | 9199a9f | 2016-07-13 07:48:43 -0700 | [diff] [blame] | 420 | } |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 423 | void GrRenderTargetContext::drawPaint(const GrClip& clip, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 424 | GrPaint&& paint, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 425 | const SkMatrix& viewMatrix) { |
Michael Ludwig | 6132820 | 2019-06-19 14:48:58 +0000 | [diff] [blame] | 426 | // Start with the render target, since that is the maximum content we could possibly fill. |
| 427 | // drawFilledQuad() will automatically restrict it to clip bounds for us if possible. |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 428 | SkRect r = fRenderTargetProxy->getBoundsRect(); |
Michael Ludwig | 3d6390e | 2018-10-09 11:45:16 -0400 | [diff] [blame] | 429 | if (!paint.numTotalFragmentProcessors()) { |
Michael Ludwig | 6132820 | 2019-06-19 14:48:58 +0000 | [diff] [blame] | 430 | // The paint is trivial so we won't need to use local coordinates, so skip calculating the |
| 431 | // inverse view matrix. |
| 432 | this->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, r); |
| 433 | } else { |
| 434 | // Use the inverse view matrix to arrive at appropriate local coordinates for the paint. |
| 435 | SkMatrix localMatrix; |
| 436 | if (!viewMatrix.invert(&localMatrix)) { |
| 437 | return; |
Michael Ludwig | 3d6390e | 2018-10-09 11:45:16 -0400 | [diff] [blame] | 438 | } |
Michael Ludwig | 6132820 | 2019-06-19 14:48:58 +0000 | [diff] [blame] | 439 | this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, |
| 440 | localMatrix); |
bsalomon | cb31e51 | 2016-08-26 10:48:19 -0700 | [diff] [blame] | 441 | } |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Michael Ludwig | 6132820 | 2019-06-19 14:48:58 +0000 | [diff] [blame] | 444 | enum class GrRenderTargetContext::QuadOptimization { |
| 445 | // The rect to draw doesn't intersect clip or render target, so no draw op should be added |
| 446 | kDiscarded, |
| 447 | // The rect to draw was converted to some other op and appended to the oplist, so no additional |
| 448 | // op is necessary. Currently this can convert it to a clear op or a rrect op. Only valid if |
| 449 | // a constColor is provided. |
| 450 | kSubmitted, |
| 451 | // The clip was folded into the device quad, with updated edge flags and local coords, and |
| 452 | // caller is responsible for adding an appropriate op. |
| 453 | kClipApplied, |
| 454 | // No change to clip, but quad updated to better fit clip/render target, and caller is |
| 455 | // responsible for adding an appropriate op. |
| 456 | kCropped |
| 457 | }; |
Michael Ludwig | 61a1651 | 2019-01-15 11:15:13 -0500 | [diff] [blame] | 458 | |
Michael Ludwig | ed71b7e | 2019-06-21 13:47:02 -0400 | [diff] [blame] | 459 | static bool make_vertex_finite(float* value) { |
| 460 | if (SkScalarIsNaN(*value)) { |
| 461 | return false; |
| 462 | } |
| 463 | |
| 464 | if (!SkScalarIsFinite(*value)) { |
| 465 | // +/- infinity at this point. Don't use exactly SK_ScalarMax so that we have some precision |
| 466 | // left when calculating crops. |
| 467 | static constexpr float kNearInfinity = SK_ScalarMax / 4.f; |
| 468 | *value = *value < 0.f ? -kNearInfinity : kNearInfinity; |
| 469 | } |
| 470 | |
| 471 | return true; |
| 472 | } |
| 473 | |
Michael Ludwig | 6132820 | 2019-06-19 14:48:58 +0000 | [diff] [blame] | 474 | GrRenderTargetContext::QuadOptimization GrRenderTargetContext::attemptQuadOptimization( |
Michael Ludwig | e08b443 | 2019-06-19 18:00:48 -0400 | [diff] [blame] | 475 | const GrClip& clip, const SkPMColor4f* constColor, |
| 476 | const GrUserStencilSettings* stencilSettings, GrAA* aa, GrQuadAAFlags* edgeFlags, |
| 477 | GrQuad* deviceQuad, GrQuad* localQuad) { |
Michael Ludwig | 6132820 | 2019-06-19 14:48:58 +0000 | [diff] [blame] | 478 | // Optimization requirements: |
| 479 | // 1. kDiscard applies when clip bounds and quad bounds do not intersect |
| 480 | // 2. kClear applies when constColor and final geom is pixel aligned rect; |
| 481 | // pixel aligned rect requires rect clip and (rect quad or quad covers clip) |
| 482 | // 3. kRRect applies when constColor and rrect clip and quad covers clip |
| 483 | // 4. kExplicitClip applies when rect clip and (rect quad or quad covers clip) |
| 484 | // 5. kCropped applies when rect quad (currently) |
| 485 | // 6. kNone always applies |
| 486 | GrQuadAAFlags newFlags = *edgeFlags; |
Brian Salomon | 7694b90 | 2019-06-18 21:00:21 +0000 | [diff] [blame] | 487 | |
Michael Ludwig | e08b443 | 2019-06-19 18:00:48 -0400 | [diff] [blame] | 488 | SkRect rtRect; |
| 489 | if (stencilSettings) { |
| 490 | // Must use worst case bounds so that stencil buffer updates on approximately sized render |
| 491 | // targets don't get corrupted. |
| 492 | rtRect = SkRect::MakeWH(fRenderTargetProxy->worstCaseWidth(), |
| 493 | fRenderTargetProxy->worstCaseHeight()); |
| 494 | } else { |
| 495 | // Use the logical size of the render target, which allows for "fullscreen" clears even if |
| 496 | // the render target has an approximate backing fit |
| 497 | rtRect = SkRect::MakeWH(this->width(), this->height()); |
| 498 | } |
| 499 | |
Michael Ludwig | 6132820 | 2019-06-19 14:48:58 +0000 | [diff] [blame] | 500 | SkRect drawBounds = deviceQuad->bounds(); |
| 501 | if (constColor) { |
| 502 | // Don't bother updating local coordinates when the paint will ignore them anyways |
| 503 | localQuad = nullptr; |
Michael Ludwig | ed71b7e | 2019-06-21 13:47:02 -0400 | [diff] [blame] | 504 | // If the device quad is not finite, coerce into a finite quad. This is acceptable since it |
| 505 | // will be cropped to the finite 'clip' or render target and there is no local space mapping |
| 506 | if (!deviceQuad->isFinite()) { |
| 507 | for (int i = 0; i < 4; ++i) { |
| 508 | if (!make_vertex_finite(deviceQuad->xs() + i) || |
| 509 | !make_vertex_finite(deviceQuad->ys() + i) || |
| 510 | !make_vertex_finite(deviceQuad->ws() + i)) { |
| 511 | // Discard if we see a nan |
| 512 | return QuadOptimization::kDiscarded; |
| 513 | } |
| 514 | } |
| 515 | SkASSERT(deviceQuad->isFinite()); |
| 516 | } |
| 517 | } else { |
| 518 | // CropToRect requires the quads to be finite. If they are not finite and we have local |
| 519 | // coordinates, the mapping from local space to device space is poorly defined so drop it |
| 520 | if (!deviceQuad->isFinite()) { |
| 521 | return QuadOptimization::kDiscarded; |
| 522 | } |
Brian Salomon | 7694b90 | 2019-06-18 21:00:21 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Michael Ludwig | 6132820 | 2019-06-19 14:48:58 +0000 | [diff] [blame] | 525 | // If the quad is entirely off screen, it doesn't matter what the clip does |
| 526 | if (!rtRect.intersects(drawBounds)) { |
| 527 | return QuadOptimization::kDiscarded; |
| 528 | } |
Brian Salomon | 7694b90 | 2019-06-18 21:00:21 +0000 | [diff] [blame] | 529 | |
Michael Ludwig | 6132820 | 2019-06-19 14:48:58 +0000 | [diff] [blame] | 530 | // Check if clip can be represented as a rounded rect (initialize as if clip fully contained |
| 531 | // the render target). |
| 532 | SkRRect clipRRect = SkRRect::MakeRect(rtRect); |
Michael Ludwig | e08b443 | 2019-06-19 18:00:48 -0400 | [diff] [blame] | 533 | // We initialize clipAA to *aa when there are stencil settings so that we don't artificially |
| 534 | // encounter mixed-aa edges (not allowed for stencil), but we want to start as non-AA for |
| 535 | // regular draws so that if we fully cover the render target, that can stop being anti-aliased. |
| 536 | GrAA clipAA = stencilSettings ? *aa : GrAA::kNo; |
Michael Ludwig | 6132820 | 2019-06-19 14:48:58 +0000 | [diff] [blame] | 537 | bool axisAlignedClip = true; |
Brian Salomon | 7694b90 | 2019-06-18 21:00:21 +0000 | [diff] [blame] | 538 | if (!clip.quickContains(rtRect)) { |
Michael Ludwig | 6132820 | 2019-06-19 14:48:58 +0000 | [diff] [blame] | 539 | if (!clip.isRRect(rtRect, &clipRRect, &clipAA)) { |
| 540 | axisAlignedClip = false; |
Brian Salomon | 7694b90 | 2019-06-18 21:00:21 +0000 | [diff] [blame] | 541 | } |
Brian Salomon | 7694b90 | 2019-06-18 21:00:21 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Michael Ludwig | 6132820 | 2019-06-19 14:48:58 +0000 | [diff] [blame] | 544 | // If the clip rrect is valid (i.e. axis-aligned), we can potentially combine it with the |
| 545 | // draw geometry so that no clip is needed when drawing. |
Michael Ludwig | e08b443 | 2019-06-19 18:00:48 -0400 | [diff] [blame] | 546 | if (axisAlignedClip && (!stencilSettings || clipAA == *aa)) { |
Michael Ludwig | 6132820 | 2019-06-19 14:48:58 +0000 | [diff] [blame] | 547 | // Tighten clip bounds (if clipRRect.isRect() is true, clipBounds now holds the intersection |
| 548 | // of the render target and the clip rect) |
| 549 | SkRect clipBounds = rtRect; |
| 550 | if (!clipBounds.intersect(clipRRect.rect()) || !clipBounds.intersects(drawBounds)) { |
| 551 | return QuadOptimization::kDiscarded; |
Brian Salomon | 7694b90 | 2019-06-18 21:00:21 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Michael Ludwig | 6132820 | 2019-06-19 14:48:58 +0000 | [diff] [blame] | 554 | if (clipRRect.isRect()) { |
| 555 | // No rounded corners, so the kClear and kExplicitClip optimizations are possible |
| 556 | if (GrQuadUtils::CropToRect(clipBounds, clipAA, &newFlags, deviceQuad, localQuad)) { |
| 557 | if (constColor && deviceQuad->quadType() == GrQuad::Type::kAxisAligned) { |
| 558 | // Clear optimization is possible |
| 559 | drawBounds = deviceQuad->bounds(); |
| 560 | if (drawBounds.contains(rtRect)) { |
| 561 | // Fullscreen clear |
| 562 | this->clear(nullptr, *constColor, CanClearFullscreen::kYes); |
| 563 | return QuadOptimization::kSubmitted; |
| 564 | } else if (GrClip::IsPixelAligned(drawBounds) && |
| 565 | drawBounds.width() > 256 && drawBounds.height() > 256) { |
| 566 | // Scissor + clear (round shouldn't do anything since we are pixel aligned) |
| 567 | SkIRect scissorRect; |
| 568 | drawBounds.round(&scissorRect); |
| 569 | this->clear(&scissorRect, *constColor, CanClearFullscreen::kNo); |
| 570 | return QuadOptimization::kSubmitted; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | // Update overall AA setting. |
| 575 | *edgeFlags = newFlags; |
| 576 | if (*aa == GrAA::kNo && clipAA == GrAA::kYes && |
| 577 | newFlags != GrQuadAAFlags::kNone) { |
| 578 | // The clip was anti-aliased and now the draw needs to be upgraded to AA to |
| 579 | // properly reflect the smooth edge of the clip. |
| 580 | *aa = GrAA::kYes; |
| 581 | } |
| 582 | // We intentionally do not downgrade AA here because we don't know if we need to |
| 583 | // preserve MSAA (see GrQuadAAFlags docs). But later in the pipeline, the ops can |
| 584 | // use GrResolveAATypeForQuad() to turn off coverage AA when all flags are off. |
| 585 | |
| 586 | // deviceQuad is exactly the intersection of original quad and clip, so it can be |
| 587 | // drawn with no clip (submitted by caller) |
| 588 | return QuadOptimization::kClipApplied; |
| 589 | } else { |
| 590 | // The quads have been updated to better fit the clip bounds, but can't get rid of |
| 591 | // the clip entirely |
| 592 | return QuadOptimization::kCropped; |
| 593 | } |
| 594 | } else if (constColor) { |
| 595 | // Rounded corners and constant filled color (limit ourselves to solid colors because |
| 596 | // there is no way to use custom local coordinates with drawRRect). |
| 597 | if (GrQuadUtils::CropToRect(clipBounds, clipAA, &newFlags, deviceQuad, localQuad) && |
| 598 | deviceQuad->quadType() == GrQuad::Type::kAxisAligned && |
| 599 | deviceQuad->bounds().contains(clipBounds)) { |
| 600 | // Since the cropped quad became a rectangle which covered the bounds of the rrect, |
| 601 | // we can draw the rrect directly and ignore the edge flags |
| 602 | GrPaint paint; |
| 603 | clear_to_grpaint(*constColor, &paint); |
| 604 | this->drawRRect(GrFixedClip::Disabled(), std::move(paint), clipAA, SkMatrix::I(), |
| 605 | clipRRect, GrStyle::SimpleFill()); |
| 606 | return QuadOptimization::kSubmitted; |
| 607 | } else { |
| 608 | // The quad has been updated to better fit clip bounds, but can't remove the clip |
| 609 | return QuadOptimization::kCropped; |
| 610 | } |
Brian Salomon | 7694b90 | 2019-06-18 21:00:21 +0000 | [diff] [blame] | 611 | } |
Brian Salomon | 7694b90 | 2019-06-18 21:00:21 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Michael Ludwig | 6132820 | 2019-06-19 14:48:58 +0000 | [diff] [blame] | 614 | // Crop the quad to the conservative bounds of the clip. |
| 615 | SkIRect clipDevBounds; |
| 616 | clip.getConservativeBounds(rtRect.width(), rtRect.height(), &clipDevBounds); |
| 617 | SkRect clipBounds = SkRect::Make(clipDevBounds); |
| 618 | |
| 619 | // One final check for discarding, since we may have gone here directly due to a complex clip |
| 620 | if (!clipBounds.intersects(drawBounds)) { |
| 621 | return QuadOptimization::kDiscarded; |
Brian Salomon | 7694b90 | 2019-06-18 21:00:21 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Michael Ludwig | 6132820 | 2019-06-19 14:48:58 +0000 | [diff] [blame] | 624 | // Even if this were to return true, the crop rect does not exactly match the clip, so can not |
| 625 | // report explicit-clip. Since these edges aren't visible, don't update the final edge flags. |
| 626 | GrQuadUtils::CropToRect(clipBounds, clipAA, &newFlags, deviceQuad, localQuad); |
| 627 | |
| 628 | return QuadOptimization::kCropped; |
Brian Salomon | 7694b90 | 2019-06-18 21:00:21 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Michael Ludwig | 6132820 | 2019-06-19 14:48:58 +0000 | [diff] [blame] | 631 | void GrRenderTargetContext::drawFilledQuad(const GrClip& clip, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 632 | GrPaint&& paint, |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 633 | GrAA aa, |
Michael Ludwig | 6132820 | 2019-06-19 14:48:58 +0000 | [diff] [blame] | 634 | GrQuadAAFlags edgeFlags, |
| 635 | const GrQuad& deviceQuad, |
| 636 | const GrQuad& localQuad, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 637 | const GrUserStencilSettings* ss) { |
Michael Ludwig | 6132820 | 2019-06-19 14:48:58 +0000 | [diff] [blame] | 638 | ASSERT_SINGLE_OWNER |
| 639 | RETURN_IF_ABANDONED |
| 640 | SkDEBUGCODE(this->validate();) |
| 641 | GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFilledQuad", fContext); |
Michael Ludwig | 61a1651 | 2019-01-15 11:15:13 -0500 | [diff] [blame] | 642 | |
Michael Ludwig | 6132820 | 2019-06-19 14:48:58 +0000 | [diff] [blame] | 643 | AutoCheckFlush acf(this->drawingManager()); |
| 644 | |
| 645 | SkPMColor4f* constColor = nullptr; |
| 646 | SkPMColor4f paintColor; |
| 647 | if (!ss && !paint.numCoverageFragmentProcessors() && |
| 648 | paint.isConstantBlendedColor(&paintColor)) { |
| 649 | // Only consider clears/rrects when it's easy to guarantee 100% fill with single color |
| 650 | constColor = &paintColor; |
Michael Ludwig | 61a1651 | 2019-01-15 11:15:13 -0500 | [diff] [blame] | 651 | } |
| 652 | |
Michael Ludwig | 6132820 | 2019-06-19 14:48:58 +0000 | [diff] [blame] | 653 | GrQuad croppedDeviceQuad = deviceQuad; |
| 654 | GrQuad croppedLocalQuad = localQuad; |
Michael Ludwig | e08b443 | 2019-06-19 18:00:48 -0400 | [diff] [blame] | 655 | QuadOptimization opt = this->attemptQuadOptimization(clip, constColor, ss, &aa, &edgeFlags, |
| 656 | &croppedDeviceQuad, &croppedLocalQuad); |
Michael Ludwig | 6132820 | 2019-06-19 14:48:58 +0000 | [diff] [blame] | 657 | if (opt >= QuadOptimization::kClipApplied) { |
| 658 | // These optimizations require caller to add an op themselves |
| 659 | const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled() |
| 660 | : clip; |
| 661 | GrAAType aaType = ss ? (aa == GrAA::kYes ? GrAAType::kMSAA : GrAAType::kNone) |
| 662 | : this->chooseAAType(aa); |
| 663 | this->addDrawOp(finalClip, GrFillRectOp::Make(fContext, std::move(paint), aaType, edgeFlags, |
| 664 | croppedDeviceQuad, croppedLocalQuad, ss)); |
csmartdalton | 97f6cd5 | 2016-07-13 13:37:08 -0700 | [diff] [blame] | 665 | } |
Michael Ludwig | 6132820 | 2019-06-19 14:48:58 +0000 | [diff] [blame] | 666 | // All other optimization levels were completely handled inside attempt(), so no extra op needed |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 667 | } |
| 668 | |
Michael Ludwig | aee26ea | 2019-07-08 16:22:48 +0000 | [diff] [blame] | 669 | void GrRenderTargetContext::drawTexturedQuad(const GrClip& clip, |
| 670 | sk_sp<GrTextureProxy> proxy, |
| 671 | sk_sp<GrColorSpaceXform> textureXform, |
| 672 | GrSamplerState::Filter filter, |
| 673 | const SkPMColor4f& color, |
| 674 | SkBlendMode blendMode, |
| 675 | GrAA aa, |
| 676 | GrQuadAAFlags edgeFlags, |
| 677 | const GrQuad& deviceQuad, |
| 678 | const GrQuad& localQuad, |
| 679 | const SkRect* domain) { |
| 680 | ASSERT_SINGLE_OWNER |
| 681 | RETURN_IF_ABANDONED |
| 682 | SkDEBUGCODE(this->validate();) |
| 683 | GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexturedQuad", fContext); |
| 684 | |
| 685 | AutoCheckFlush acf(this->drawingManager()); |
| 686 | |
| 687 | // Functionally this is very similar to drawFilledQuad except that there's no constColor to |
| 688 | // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp. |
| 689 | GrQuad croppedDeviceQuad = deviceQuad; |
| 690 | GrQuad croppedLocalQuad = localQuad; |
| 691 | QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr, nullptr, &aa, &edgeFlags, |
| 692 | &croppedDeviceQuad, &croppedLocalQuad); |
| 693 | |
| 694 | SkASSERT(opt != QuadOptimization::kSubmitted); |
| 695 | if (opt != QuadOptimization::kDiscarded) { |
| 696 | // And the texture op if not discarded |
| 697 | const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled() |
| 698 | : clip; |
| 699 | GrAAType aaType = this->chooseAAType(aa); |
| 700 | // Use the provided domain, although hypothetically we could detect that the cropped local |
| 701 | // quad is sufficiently inside the domain and the constraint could be dropped. |
| 702 | this->addDrawOp(finalClip, GrTextureOp::Make(fContext, std::move(proxy), |
| 703 | std::move(textureXform), filter, color, |
| 704 | blendMode, aaType, edgeFlags, |
| 705 | croppedDeviceQuad, croppedLocalQuad, domain)); |
| 706 | } |
| 707 | } |
| 708 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 709 | void GrRenderTargetContext::drawRect(const GrClip& clip, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 710 | GrPaint&& paint, |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 711 | GrAA aa, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 712 | const SkMatrix& viewMatrix, |
| 713 | const SkRect& rect, |
| 714 | const GrStyle* style) { |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 715 | if (!style) { |
| 716 | style = &GrStyle::SimpleFill(); |
| 717 | } |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 718 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 719 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 720 | SkDEBUGCODE(this->validate();) |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 721 | GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 722 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 723 | // Path effects should've been devolved to a path in SkGpuDevice |
| 724 | SkASSERT(!style->pathEffect()); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 725 | |
Robert Phillips | 7215283 | 2017-01-25 17:31:35 -0500 | [diff] [blame] | 726 | AutoCheckFlush acf(this->drawingManager()); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 727 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 728 | const SkStrokeRec& stroke = style->strokeRec(); |
Robert Phillips | 8c8b046 | 2018-08-24 16:18:03 -0400 | [diff] [blame] | 729 | if (stroke.getStyle() == SkStrokeRec::kFill_Style) { |
Michael Ludwig | 6132820 | 2019-06-19 14:48:58 +0000 | [diff] [blame] | 730 | // Fills the rect, using rect as its own local coordinates |
| 731 | this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect); |
Michael Ludwig | 61a1651 | 2019-01-15 11:15:13 -0500 | [diff] [blame] | 732 | return; |
bsalomon | a7d85ba | 2016-07-06 11:54:59 -0700 | [diff] [blame] | 733 | } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style || |
| 734 | stroke.getStyle() == SkStrokeRec::kHairline_Style) { |
| 735 | if ((!rect.width() || !rect.height()) && |
| 736 | SkStrokeRec::kHairline_Style != stroke.getStyle()) { |
| 737 | SkScalar r = stroke.getWidth() / 2; |
| 738 | // TODO: Move these stroke->fill fallbacks to GrShape? |
| 739 | switch (stroke.getJoin()) { |
| 740 | case SkPaint::kMiter_Join: |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 741 | this->drawRect( |
| 742 | clip, std::move(paint), aa, viewMatrix, |
| 743 | {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r}, |
| 744 | &GrStyle::SimpleFill()); |
bsalomon | a7d85ba | 2016-07-06 11:54:59 -0700 | [diff] [blame] | 745 | return; |
| 746 | case SkPaint::kRound_Join: |
| 747 | // Raster draws nothing when both dimensions are empty. |
| 748 | if (rect.width() || rect.height()){ |
| 749 | SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 750 | this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, |
| 751 | GrStyle::SimpleFill()); |
bsalomon | a7d85ba | 2016-07-06 11:54:59 -0700 | [diff] [blame] | 752 | return; |
| 753 | } |
| 754 | case SkPaint::kBevel_Join: |
| 755 | if (!rect.width()) { |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 756 | this->drawRect(clip, std::move(paint), aa, viewMatrix, |
bsalomon | a7d85ba | 2016-07-06 11:54:59 -0700 | [diff] [blame] | 757 | {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom}, |
| 758 | &GrStyle::SimpleFill()); |
| 759 | } else { |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 760 | this->drawRect(clip, std::move(paint), aa, viewMatrix, |
bsalomon | a7d85ba | 2016-07-06 11:54:59 -0700 | [diff] [blame] | 761 | {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r}, |
| 762 | &GrStyle::SimpleFill()); |
| 763 | } |
| 764 | return; |
| 765 | } |
| 766 | } |
robertphillips | 4430239 | 2016-07-08 14:43:03 -0700 | [diff] [blame] | 767 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 768 | std::unique_ptr<GrDrawOp> op; |
robertphillips | 4430239 | 2016-07-08 14:43:03 -0700 | [diff] [blame] | 769 | |
Chris Dalton | 7d6748e | 2019-03-13 00:34:52 -0600 | [diff] [blame] | 770 | GrAAType aaType = this->chooseAAType(aa); |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 771 | op = GrStrokeRectOp::Make(fContext, std::move(paint), aaType, viewMatrix, rect, stroke); |
| 772 | // op may be null if the stroke is not supported or if using coverage aa and the view matrix |
| 773 | // does not preserve rectangles. |
Brian Salomon | 42521e8 | 2016-12-07 16:44:58 -0500 | [diff] [blame] | 774 | if (op) { |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 775 | this->addDrawOp(clip, std::move(op)); |
robertphillips | 4430239 | 2016-07-08 14:43:03 -0700 | [diff] [blame] | 776 | return; |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 777 | } |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 778 | } |
Mike Klein | 1688507 | 2018-12-11 09:54:31 -0500 | [diff] [blame] | 779 | assert_alive(paint); |
Brian Salomon | 2fad74a | 2017-12-20 13:28:55 -0500 | [diff] [blame] | 780 | this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(rect, *style)); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 781 | } |
| 782 | |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 783 | void GrRenderTargetContext::drawQuadSet(const GrClip& clip, GrPaint&& paint, GrAA aa, |
| 784 | const SkMatrix& viewMatrix, const QuadSetEntry quads[], |
| 785 | int cnt) { |
Chris Dalton | 7d6748e | 2019-03-13 00:34:52 -0600 | [diff] [blame] | 786 | GrAAType aaType = this->chooseAAType(aa); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 787 | this->addDrawOp(clip, GrFillRectOp::MakeSet(fContext, std::move(paint), aaType, viewMatrix, |
| 788 | quads, cnt)); |
| 789 | } |
| 790 | |
Robert Phillips | ec2249f | 2016-11-09 08:54:35 -0500 | [diff] [blame] | 791 | int GrRenderTargetContextPriv::maxWindowRectangles() const { |
| 792 | return fRenderTargetContext->fRenderTargetProxy->maxWindowRectangles( |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 793 | *fRenderTargetContext->caps()); |
Robert Phillips | ec2249f | 2016-11-09 08:54:35 -0500 | [diff] [blame] | 794 | } |
| 795 | |
Chris Dalton | 6b98280 | 2019-06-27 13:53:46 -0600 | [diff] [blame] | 796 | GrRenderTargetOpList::CanDiscardPreviousOps GrRenderTargetContext::canDiscardPreviousOpsOnFullClear( |
| 797 | ) const { |
| 798 | #if GR_TEST_UTILS |
| 799 | if (fPreserveOpsOnFullClear_TestingOnly) { |
| 800 | return GrRenderTargetOpList::CanDiscardPreviousOps::kNo; |
| 801 | } |
| 802 | #endif |
| 803 | // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops |
| 804 | // would normally be overwritten. The one exception is if the render target context is marked as |
| 805 | // needing a stencil buffer then there may be a prior op that writes to the stencil buffer. |
| 806 | // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get |
| 807 | // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond |
| 808 | // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002). |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 809 | return GrRenderTargetOpList::CanDiscardPreviousOps(!fNumStencilSamples); |
Chris Dalton | 6b98280 | 2019-06-27 13:53:46 -0600 | [diff] [blame] | 810 | } |
| 811 | |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 812 | void GrRenderTargetContext::setNeedsStencil(bool multisampled) { |
| 813 | // Don't clear stencil until after we've changed fNumStencilSamples. This ensures we don't loop |
Chris Dalton | 6b98280 | 2019-06-27 13:53:46 -0600 | [diff] [blame] | 814 | // forever in the event that there are driver bugs and we need to clear as a draw. |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 815 | bool needsStencilClear = !fNumStencilSamples; |
Chris Dalton | 6b98280 | 2019-06-27 13:53:46 -0600 | [diff] [blame] | 816 | |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 817 | int numRequiredSamples = this->numSamples(); |
| 818 | if (multisampled && 1 == numRequiredSamples) { |
| 819 | // The caller has requested a multisampled stencil buffer on a non-MSAA render target. Use |
| 820 | // mixed samples. |
| 821 | SkASSERT(fRenderTargetProxy->canUseMixedSamples(*this->caps())); |
| 822 | numRequiredSamples = this->caps()->internalMultisampleCount( |
Brian Salomon | bd3d8d3 | 2019-07-02 09:16:28 -0400 | [diff] [blame] | 823 | this->asSurfaceProxy()->config()); |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 824 | } |
| 825 | SkASSERT(numRequiredSamples > 0); |
| 826 | |
| 827 | if (numRequiredSamples > fNumStencilSamples) { |
| 828 | fNumStencilSamples = numRequiredSamples; |
| 829 | fRenderTargetProxy->setNeedsStencil(fNumStencilSamples); |
| 830 | } |
Chris Dalton | 6b98280 | 2019-06-27 13:53:46 -0600 | [diff] [blame] | 831 | |
| 832 | if (needsStencilClear) { |
| 833 | if (this->caps()->performStencilClearsAsDraws()) { |
| 834 | // There is a driver bug with clearing stencil. We must use an op to manually clear the |
| 835 | // stencil buffer before the op that required 'setNeedsStencil'. |
| 836 | this->internalStencilClear(GrFixedClip::Disabled(), /* inside mask */ false); |
| 837 | } else { |
| 838 | // Setting the clear stencil load op is preferable. On non-tilers, this lets the flush |
| 839 | // code note when the instantiated stencil buffer is already clear and skip the clear |
| 840 | // altogether. And on tilers, loading the stencil buffer cleared is even faster than |
| 841 | // preserving the previous contents. |
| 842 | this->getRTOpList()->setStencilLoadOp(GrLoadOp::kClear); |
| 843 | } |
| 844 | } |
| 845 | } |
| 846 | |
Jim Van Verth | 6a40abc | 2017-11-02 16:56:09 +0000 | [diff] [blame] | 847 | void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) { |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 848 | ASSERT_SINGLE_OWNER_PRIV |
| 849 | RETURN_IF_ABANDONED_PRIV |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 850 | SkDEBUGCODE(fRenderTargetContext->validate();) |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 851 | GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip", |
| 852 | fRenderTargetContext->fContext); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 853 | |
Robert Phillips | 7215283 | 2017-01-25 17:31:35 -0500 | [diff] [blame] | 854 | AutoCheckFlush acf(fRenderTargetContext->drawingManager()); |
Robert Phillips | b9a02a1 | 2017-04-06 11:08:40 -0400 | [diff] [blame] | 855 | |
Michael Ludwig | c39d0c8 | 2019-01-15 10:03:43 -0500 | [diff] [blame] | 856 | fRenderTargetContext->internalStencilClear(clip, insideStencilMask); |
| 857 | } |
| 858 | |
| 859 | void GrRenderTargetContext::internalStencilClear(const GrFixedClip& clip, bool insideStencilMask) { |
| 860 | if (this->caps()->performStencilClearsAsDraws()) { |
| 861 | const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask); |
| 862 | SkRect rtRect = SkRect::MakeWH(this->width(), this->height()); |
| 863 | |
| 864 | // Configure the paint to have no impact on the color buffer |
| 865 | GrPaint paint; |
Michael Ludwig | 0cb2fde | 2019-05-28 13:14:41 -0400 | [diff] [blame] | 866 | paint.setXPFactory(GrDisableColorXPFactory::Get()); |
Michael Ludwig | aa1b6b3 | 2019-05-29 14:43:13 -0400 | [diff] [blame] | 867 | this->addDrawOp(clip, GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(), |
| 868 | rtRect, ss)); |
Michael Ludwig | c39d0c8 | 2019-01-15 10:03:43 -0500 | [diff] [blame] | 869 | } else { |
| 870 | std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(fContext, clip, insideStencilMask, |
| 871 | fRenderTargetProxy.get())); |
| 872 | if (!op) { |
| 873 | return; |
| 874 | } |
| 875 | this->getRTOpList()->addOp(std::move(op), *this->caps()); |
Robert Phillips | e60ad62 | 2016-11-17 10:22:48 -0500 | [diff] [blame] | 876 | } |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 877 | } |
| 878 | |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 879 | void GrRenderTargetContextPriv::stencilPath(const GrHardClip& clip, |
Chris Dalton | 09e5689 | 2019-03-13 00:22:01 -0600 | [diff] [blame] | 880 | GrAA doStencilMSAA, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 881 | const SkMatrix& viewMatrix, |
| 882 | const GrPath* path) { |
Brian Salomon | 467921e | 2017-03-06 16:17:12 -0500 | [diff] [blame] | 883 | ASSERT_SINGLE_OWNER_PRIV |
| 884 | RETURN_IF_ABANDONED_PRIV |
| 885 | SkDEBUGCODE(fRenderTargetContext->validate();) |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 886 | GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath", |
| 887 | fRenderTargetContext->fContext); |
Brian Salomon | 467921e | 2017-03-06 16:17:12 -0500 | [diff] [blame] | 888 | |
Brian Salomon | 467921e | 2017-03-06 16:17:12 -0500 | [diff] [blame] | 889 | // TODO: extract portions of checkDraw that are relevant to path stenciling. |
| 890 | SkASSERT(path); |
| 891 | SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport()); |
| 892 | |
| 893 | // FIXME: Use path bounds instead of this WAR once |
| 894 | // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved. |
| 895 | SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height()); |
| 896 | |
| 897 | // Setup clip |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 898 | GrAppliedHardClip appliedClip; |
| 899 | if (!clip.apply(fRenderTargetContext->width(), fRenderTargetContext->height(), &appliedClip, |
| 900 | &bounds)) { |
Brian Salomon | 467921e | 2017-03-06 16:17:12 -0500 | [diff] [blame] | 901 | return; |
| 902 | } |
| 903 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 904 | std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext, |
| 905 | viewMatrix, |
Chris Dalton | 09e5689 | 2019-03-13 00:22:01 -0600 | [diff] [blame] | 906 | GrAA::kYes == doStencilMSAA, |
Brian Salomon | 467921e | 2017-03-06 16:17:12 -0500 | [diff] [blame] | 907 | path->getFillType(), |
| 908 | appliedClip.hasStencilClip(), |
Brian Salomon | 467921e | 2017-03-06 16:17:12 -0500 | [diff] [blame] | 909 | appliedClip.scissorState(), |
Brian Salomon | 467921e | 2017-03-06 16:17:12 -0500 | [diff] [blame] | 910 | path); |
Robert Phillips | b9a02a1 | 2017-04-06 11:08:40 -0400 | [diff] [blame] | 911 | if (!op) { |
| 912 | return; |
| 913 | } |
Brian Salomon | 97180af | 2017-03-14 13:42:58 -0400 | [diff] [blame] | 914 | op->setClippedBounds(bounds); |
Chris Dalton | 6b98280 | 2019-06-27 13:53:46 -0600 | [diff] [blame] | 915 | |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 916 | fRenderTargetContext->setNeedsStencil(GrAA::kYes == doStencilMSAA); |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 917 | fRenderTargetContext->getRTOpList()->addOp(std::move(op), *fRenderTargetContext->caps()); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 918 | } |
| 919 | |
Brian Salomon | d7065e7 | 2018-10-12 11:42:02 -0400 | [diff] [blame] | 920 | void GrRenderTargetContext::drawTextureSet(const GrClip& clip, const TextureSetEntry set[], int cnt, |
Michael Ludwig | a3c45c7 | 2019-01-17 17:26:48 -0500 | [diff] [blame] | 921 | GrSamplerState::Filter filter, SkBlendMode mode, |
Michael Ludwig | 31ba718 | 2019-04-03 10:38:06 -0400 | [diff] [blame] | 922 | GrAA aa, SkCanvas::SrcRectConstraint constraint, |
| 923 | const SkMatrix& viewMatrix, |
Brian Osman | 3d139a4 | 2018-11-19 10:42:10 -0500 | [diff] [blame] | 924 | sk_sp<GrColorSpaceXform> texXform) { |
Brian Salomon | d7065e7 | 2018-10-12 11:42:02 -0400 | [diff] [blame] | 925 | ASSERT_SINGLE_OWNER |
| 926 | RETURN_IF_ABANDONED |
| 927 | SkDEBUGCODE(this->validate();) |
| 928 | GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureSet", fContext); |
Michael Ludwig | a3c45c7 | 2019-01-17 17:26:48 -0500 | [diff] [blame] | 929 | |
Michael Ludwig | a3c45c7 | 2019-01-17 17:26:48 -0500 | [diff] [blame] | 930 | if (mode != SkBlendMode::kSrcOver || |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 931 | !fContext->priv().caps()->dynamicStateArrayGeometryProcessorTextureSupport()) { |
Michael Ludwig | 22429f9 | 2019-06-27 10:44:48 -0400 | [diff] [blame] | 932 | // Draw one at a time since the bulk API doesn't support non src-over blending, or the |
| 933 | // backend can't support the bulk geometry processor yet. |
Michael Ludwig | 7ae2ab5 | 2019-03-05 16:00:20 -0500 | [diff] [blame] | 934 | SkMatrix ctm; |
Michael Ludwig | a3c45c7 | 2019-01-17 17:26:48 -0500 | [diff] [blame] | 935 | for (int i = 0; i < cnt; ++i) { |
Michael Ludwig | d54ca8f | 2019-02-13 13:25:21 -0500 | [diff] [blame] | 936 | float alpha = set[i].fAlpha; |
Michael Ludwig | 7ae2ab5 | 2019-03-05 16:00:20 -0500 | [diff] [blame] | 937 | ctm = viewMatrix; |
| 938 | if (set[i].fPreViewMatrix) { |
| 939 | ctm.preConcat(*set[i].fPreViewMatrix); |
| 940 | } |
| 941 | |
Michael Ludwig | aee26ea | 2019-07-08 16:22:48 +0000 | [diff] [blame] | 942 | GrQuad quad, srcQuad; |
| 943 | if (set[i].fDstClipQuad) { |
| 944 | quad = GrQuad::MakeFromSkQuad(set[i].fDstClipQuad, ctm); |
| 945 | |
| 946 | SkPoint srcPts[4]; |
| 947 | GrMapRectPoints(set[i].fDstRect, set[i].fSrcRect, set[i].fDstClipQuad, srcPts, 4); |
| 948 | srcQuad = GrQuad::MakeFromSkQuad(srcPts, SkMatrix::I()); |
Michael Ludwig | ce62dec | 2019-02-19 11:48:46 -0500 | [diff] [blame] | 949 | } else { |
Michael Ludwig | aee26ea | 2019-07-08 16:22:48 +0000 | [diff] [blame] | 950 | quad = GrQuad::MakeFromRect(set[i].fDstRect, ctm); |
| 951 | srcQuad = GrQuad(set[i].fSrcRect); |
Michael Ludwig | ce62dec | 2019-02-19 11:48:46 -0500 | [diff] [blame] | 952 | } |
Michael Ludwig | aee26ea | 2019-07-08 16:22:48 +0000 | [diff] [blame] | 953 | |
| 954 | const SkRect* domain = constraint == SkCanvas::kStrict_SrcRectConstraint |
| 955 | ? &set[i].fSrcRect : nullptr; |
| 956 | this->drawTexturedQuad(clip, set[i].fProxy, texXform, filter, |
| 957 | {alpha, alpha, alpha, alpha}, mode, aa, set[i].fAAFlags, |
| 958 | quad, srcQuad, domain); |
Michael Ludwig | a3c45c7 | 2019-01-17 17:26:48 -0500 | [diff] [blame] | 959 | } |
| 960 | } else { |
| 961 | // Can use a single op, avoiding GrPaint creation, and can batch across proxies |
Michael Ludwig | d54ca8f | 2019-02-13 13:25:21 -0500 | [diff] [blame] | 962 | AutoCheckFlush acf(this->drawingManager()); |
Chris Dalton | 7d6748e | 2019-03-13 00:34:52 -0600 | [diff] [blame] | 963 | GrAAType aaType = this->chooseAAType(aa); |
Michael Ludwig | 31ba718 | 2019-04-03 10:38:06 -0400 | [diff] [blame] | 964 | auto op = GrTextureOp::MakeSet(fContext, set, cnt, filter, aaType, constraint, viewMatrix, |
Michael Ludwig | 009b92e | 2019-02-15 16:03:53 -0500 | [diff] [blame] | 965 | std::move(texXform)); |
Michael Ludwig | a3c45c7 | 2019-01-17 17:26:48 -0500 | [diff] [blame] | 966 | this->addDrawOp(clip, std::move(op)); |
| 967 | } |
Brian Salomon | d7065e7 | 2018-10-12 11:42:02 -0400 | [diff] [blame] | 968 | } |
| 969 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 970 | void GrRenderTargetContext::drawVertices(const GrClip& clip, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 971 | GrPaint&& paint, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 972 | const SkMatrix& viewMatrix, |
Brian Osman | ae0c50c | 2017-05-25 16:56:34 -0400 | [diff] [blame] | 973 | sk_sp<SkVertices> vertices, |
Ruiqi Mao | c97a339 | 2018-08-15 10:44:19 -0400 | [diff] [blame] | 974 | const SkVertices::Bone bones[], |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 975 | int boneCount, |
Brian Osman | ae0c50c | 2017-05-25 16:56:34 -0400 | [diff] [blame] | 976 | GrPrimitiveType* overridePrimType) { |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 977 | ASSERT_SINGLE_OWNER |
| 978 | RETURN_IF_ABANDONED |
| 979 | SkDEBUGCODE(this->validate();) |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 980 | GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext); |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 981 | |
| 982 | AutoCheckFlush acf(this->drawingManager()); |
| 983 | |
| 984 | SkASSERT(vertices); |
Chris Dalton | 7d6748e | 2019-03-13 00:34:52 -0600 | [diff] [blame] | 985 | GrAAType aaType = this->chooseAAType(GrAA::kNo); |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 986 | std::unique_ptr<GrDrawOp> op = GrDrawVerticesOp::Make( |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 987 | fContext, std::move(paint), std::move(vertices), bones, boneCount, viewMatrix, aaType, |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 988 | this->colorSpaceInfo().refColorSpaceXformFromSRGB(), overridePrimType); |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 989 | this->addDrawOp(clip, std::move(op)); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | /////////////////////////////////////////////////////////////////////////////// |
| 993 | |
Brian Osman | 4d92b89 | 2019-03-24 00:53:23 +0000 | [diff] [blame] | 994 | void GrRenderTargetContext::drawAtlas(const GrClip& clip, |
| 995 | GrPaint&& paint, |
| 996 | const SkMatrix& viewMatrix, |
| 997 | int spriteCount, |
| 998 | const SkRSXform xform[], |
| 999 | const SkRect texRect[], |
| 1000 | const SkColor colors[]) { |
| 1001 | ASSERT_SINGLE_OWNER |
| 1002 | RETURN_IF_ABANDONED |
| 1003 | SkDEBUGCODE(this->validate();) |
| 1004 | GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext); |
| 1005 | |
| 1006 | AutoCheckFlush acf(this->drawingManager()); |
| 1007 | |
| 1008 | GrAAType aaType = this->chooseAAType(GrAA::kNo); |
| 1009 | std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix, |
| 1010 | aaType, spriteCount, xform, texRect, colors); |
| 1011 | this->addDrawOp(clip, std::move(op)); |
| 1012 | } |
| 1013 | |
| 1014 | /////////////////////////////////////////////////////////////////////////////// |
| 1015 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1016 | void GrRenderTargetContext::drawRRect(const GrClip& origClip, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 1017 | GrPaint&& paint, |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 1018 | GrAA aa, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1019 | const SkMatrix& viewMatrix, |
| 1020 | const SkRRect& rrect, |
| 1021 | const GrStyle& style) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1022 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1023 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 1024 | SkDEBUGCODE(this->validate();) |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 1025 | GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext); |
Robert Phillips | 8529080 | 2018-07-02 13:14:28 -0400 | [diff] [blame] | 1026 | |
| 1027 | const SkStrokeRec& stroke = style.strokeRec(); |
| 1028 | if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1029 | return; |
| 1030 | } |
| 1031 | |
bsalomon | 7f0d9f3 | 2016-08-15 14:49:10 -0700 | [diff] [blame] | 1032 | GrNoClip noclip; |
| 1033 | const GrClip* clip = &origClip; |
| 1034 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 1035 | // The Android framework frequently clips rrects to themselves where the clip is non-aa and the |
Brian Salomon | 42521e8 | 2016-12-07 16:44:58 -0500 | [diff] [blame] | 1036 | // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it |
bsalomon | 7f0d9f3 | 2016-08-15 14:49:10 -0700 | [diff] [blame] | 1037 | // doesn't detect that the clip can be ignored (modulo antialiasing). The following test |
| 1038 | // attempts to mitigate the stencil clip cost but will only help when the entire clip stack |
Michael Ludwig | 2839884 | 2019-03-25 10:24:24 -0400 | [diff] [blame] | 1039 | // can be ignored. We'd prefer to fix this in the framework by removing the clips calls. This |
| 1040 | // only works for filled rrects since the stroke width outsets beyond the rrect itself. |
bsalomon | 7f0d9f3 | 2016-08-15 14:49:10 -0700 | [diff] [blame] | 1041 | SkRRect devRRect; |
Michael Ludwig | 2839884 | 2019-03-25 10:24:24 -0400 | [diff] [blame] | 1042 | if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.transform(viewMatrix, &devRRect) && |
| 1043 | clip->quickContains(devRRect)) { |
bsalomon | 7f0d9f3 | 2016-08-15 14:49:10 -0700 | [diff] [blame] | 1044 | clip = &noclip; |
| 1045 | } |
| 1046 | #endif |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 1047 | SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice |
ksakamoto | ec7f2ac | 2016-07-05 03:54:53 -0700 | [diff] [blame] | 1048 | |
Robert Phillips | 7215283 | 2017-01-25 17:31:35 -0500 | [diff] [blame] | 1049 | AutoCheckFlush acf(this->drawingManager()); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 1050 | |
Chris Dalton | 7d6748e | 2019-03-13 00:34:52 -0600 | [diff] [blame] | 1051 | GrAAType aaType = this->chooseAAType(aa); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 1052 | |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 1053 | std::unique_ptr<GrDrawOp> op; |
Jim Van Verth | 64b8589 | 2019-06-17 12:01:46 -0400 | [diff] [blame] | 1054 | if (GrAAType::kCoverage == aaType && rrect.isSimple() && |
| 1055 | rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY && |
| 1056 | viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) { |
| 1057 | // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory |
| 1058 | // to avoid perf regressions on some platforms. |
| 1059 | assert_alive(paint); |
| 1060 | op = GrOvalOpFactory::MakeCircularRRectOp( |
| 1061 | fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps()); |
| 1062 | } |
| 1063 | if (!op && style.isSimpleFill()) { |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 1064 | assert_alive(paint); |
| 1065 | op = GrFillRRectOp::Make( |
| 1066 | fContext, aaType, viewMatrix, rrect, *this->caps(), std::move(paint)); |
| 1067 | } |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 1068 | if (!op && GrAAType::kCoverage == aaType) { |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 1069 | assert_alive(paint); |
| 1070 | op = GrOvalOpFactory::MakeRRectOp( |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 1071 | fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps()); |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 1072 | } |
| 1073 | if (op) { |
| 1074 | this->addDrawOp(*clip, std::move(op)); |
| 1075 | return; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1076 | } |
robertphillips | b56f927 | 2016-02-25 11:03:52 -0800 | [diff] [blame] | 1077 | |
Mike Klein | 1688507 | 2018-12-11 09:54:31 -0500 | [diff] [blame] | 1078 | assert_alive(paint); |
Brian Salomon | 2fad74a | 2017-12-20 13:28:55 -0500 | [diff] [blame] | 1079 | this->drawShapeUsingPathRenderer(*clip, std::move(paint), aa, viewMatrix, |
| 1080 | GrShape(rrect, style)); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 1083 | /////////////////////////////////////////////////////////////////////////////// |
| 1084 | |
Jim Van Verth | 3af1af9 | 2017-05-18 15:06:54 -0400 | [diff] [blame] | 1085 | static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) { |
| 1086 | SkPoint3 result; |
| 1087 | m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX); |
| 1088 | result.fZ = pt.fZ; |
| 1089 | return result; |
| 1090 | } |
| 1091 | |
| 1092 | bool GrRenderTargetContext::drawFastShadow(const GrClip& clip, |
Jim Van Verth | 3af1af9 | 2017-05-18 15:06:54 -0400 | [diff] [blame] | 1093 | const SkMatrix& viewMatrix, |
| 1094 | const SkPath& path, |
| 1095 | const SkDrawShadowRec& rec) { |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 1096 | ASSERT_SINGLE_OWNER |
Robert Phillips | 6a6de56 | 2019-02-15 15:19:15 -0500 | [diff] [blame] | 1097 | if (fContext->priv().abandoned()) { |
Jim Van Verth | 3af1af9 | 2017-05-18 15:06:54 -0400 | [diff] [blame] | 1098 | return true; |
| 1099 | } |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 1100 | SkDEBUGCODE(this->validate();) |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 1101 | GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext); |
Jim Van Verth | 3af1af9 | 2017-05-18 15:06:54 -0400 | [diff] [blame] | 1102 | |
| 1103 | // check z plane |
| 1104 | bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) || |
| 1105 | !SkScalarNearlyZero(rec.fZPlaneParams.fY)); |
| 1106 | bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag); |
| 1107 | if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) { |
| 1108 | return false; |
| 1109 | } |
| 1110 | |
| 1111 | SkRRect rrect; |
| 1112 | SkRect rect; |
| 1113 | // we can only handle rects, circles, and rrects with circular corners |
Mike Reed | 242135a | 2018-02-22 13:41:39 -0500 | [diff] [blame] | 1114 | bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) && |
Jim Van Verth | 3af1af9 | 2017-05-18 15:06:54 -0400 | [diff] [blame] | 1115 | rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero; |
| 1116 | if (!isRRect && |
| 1117 | path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) && |
| 1118 | rect.width() > SK_ScalarNearlyZero) { |
| 1119 | rrect.setOval(rect); |
| 1120 | isRRect = true; |
| 1121 | } |
| 1122 | if (!isRRect && path.isRect(&rect)) { |
| 1123 | rrect.setRect(rect); |
| 1124 | isRRect = true; |
| 1125 | } |
| 1126 | |
| 1127 | if (!isRRect) { |
| 1128 | return false; |
| 1129 | } |
| 1130 | |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 1131 | if (rrect.isEmpty()) { |
Jim Van Verth | 3af1af9 | 2017-05-18 15:06:54 -0400 | [diff] [blame] | 1132 | return true; |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 1133 | } |
| 1134 | |
Robert Phillips | 7215283 | 2017-01-25 17:31:35 -0500 | [diff] [blame] | 1135 | AutoCheckFlush acf(this->drawingManager()); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 1136 | |
Jim Van Verth | 3af1af9 | 2017-05-18 15:06:54 -0400 | [diff] [blame] | 1137 | // transform light |
| 1138 | SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos); |
| 1139 | |
| 1140 | // 1/scale |
| 1141 | SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ? |
| 1142 | SkScalarInvert(viewMatrix[SkMatrix::kMScaleX]) : |
| 1143 | sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] + |
| 1144 | viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]); |
| 1145 | |
| 1146 | SkScalar occluderHeight = rec.fZPlaneParams.fZ; |
Jim Van Verth | 3af1af9 | 2017-05-18 15:06:54 -0400 | [diff] [blame] | 1147 | bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag); |
| 1148 | |
Jim Van Verth | b1b80f7 | 2018-01-18 15:19:13 -0500 | [diff] [blame] | 1149 | if (SkColorGetA(rec.fAmbientColor) > 0) { |
Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 1150 | SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight); |
| 1151 | const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight); |
| 1152 | const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha; |
Jim Van Verth | 3af1af9 | 2017-05-18 15:06:54 -0400 | [diff] [blame] | 1153 | |
| 1154 | // Outset the shadow rrect to the border of the penumbra |
| 1155 | SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale; |
| 1156 | SkRRect ambientRRect; |
| 1157 | SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset); |
| 1158 | // If the rrect was an oval then its outset will also be one. |
| 1159 | // We set it explicitly to avoid errors. |
| 1160 | if (rrect.isOval()) { |
| 1161 | ambientRRect = SkRRect::MakeOval(outsetRect); |
| 1162 | } else { |
Mike Reed | 242135a | 2018-02-22 13:41:39 -0500 | [diff] [blame] | 1163 | SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset; |
Jim Van Verth | 3af1af9 | 2017-05-18 15:06:54 -0400 | [diff] [blame] | 1164 | ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad); |
| 1165 | } |
| 1166 | |
Jim Van Verth | b1b80f7 | 2018-01-18 15:19:13 -0500 | [diff] [blame] | 1167 | GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor); |
Jim Van Verth | 3af1af9 | 2017-05-18 15:06:54 -0400 | [diff] [blame] | 1168 | if (transparent) { |
| 1169 | // set a large inset to force a fill |
| 1170 | devSpaceInsetWidth = ambientRRect.width(); |
| 1171 | } |
Jim Van Verth | 39e7165 | 2018-04-23 18:08:45 +0000 | [diff] [blame] | 1172 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 1173 | std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext, |
| 1174 | ambientColor, |
| 1175 | viewMatrix, |
Brian Salomon | 0596909 | 2017-07-13 11:20:51 -0400 | [diff] [blame] | 1176 | ambientRRect, |
| 1177 | devSpaceAmbientBlur, |
Jim Van Verth | fb18639 | 2018-09-11 11:37:46 -0400 | [diff] [blame] | 1178 | devSpaceInsetWidth); |
Robert Phillips | e576378 | 2019-04-17 14:38:24 -0400 | [diff] [blame] | 1179 | if (op) { |
| 1180 | this->addDrawOp(clip, std::move(op)); |
| 1181 | } |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 1182 | } |
Jim Van Verth | 3af1af9 | 2017-05-18 15:06:54 -0400 | [diff] [blame] | 1183 | |
Jim Van Verth | b1b80f7 | 2018-01-18 15:19:13 -0500 | [diff] [blame] | 1184 | if (SkColorGetA(rec.fSpotColor) > 0) { |
Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 1185 | SkScalar devSpaceSpotBlur; |
| 1186 | SkScalar spotScale; |
| 1187 | SkVector spotOffset; |
| 1188 | SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY, |
| 1189 | devLightPos.fZ, rec.fLightRadius, |
| 1190 | &devSpaceSpotBlur, &spotScale, &spotOffset); |
| 1191 | // handle scale of radius due to CTM |
Jim Van Verth | 3af1af9 | 2017-05-18 15:06:54 -0400 | [diff] [blame] | 1192 | const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale; |
| 1193 | |
Jim Van Verth | 3af1af9 | 2017-05-18 15:06:54 -0400 | [diff] [blame] | 1194 | // Adjust translate for the effect of the scale. |
| 1195 | spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX]; |
| 1196 | spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY]; |
| 1197 | // This offset is in dev space, need to transform it into source space. |
| 1198 | SkMatrix ctmInverse; |
| 1199 | if (viewMatrix.invert(&ctmInverse)) { |
| 1200 | ctmInverse.mapPoints(&spotOffset, 1); |
| 1201 | } else { |
| 1202 | // Since the matrix is a similarity, this should never happen, but just in case... |
| 1203 | SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n"); |
| 1204 | SkASSERT(false); |
| 1205 | } |
| 1206 | |
| 1207 | // Compute the transformed shadow rrect |
| 1208 | SkRRect spotShadowRRect; |
| 1209 | SkMatrix shadowTransform; |
| 1210 | shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY); |
| 1211 | rrect.transform(shadowTransform, &spotShadowRRect); |
Mike Reed | 242135a | 2018-02-22 13:41:39 -0500 | [diff] [blame] | 1212 | SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX; |
Jim Van Verth | 3af1af9 | 2017-05-18 15:06:54 -0400 | [diff] [blame] | 1213 | |
| 1214 | // Compute the insetWidth |
Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 1215 | SkScalar blurOutset = srcSpaceSpotBlur; |
Jim Van Verth | 3af1af9 | 2017-05-18 15:06:54 -0400 | [diff] [blame] | 1216 | SkScalar insetWidth = blurOutset; |
| 1217 | if (transparent) { |
| 1218 | // If transparent, just do a fill |
| 1219 | insetWidth += spotShadowRRect.width(); |
| 1220 | } else { |
| 1221 | // For shadows, instead of using a stroke we specify an inset from the penumbra |
| 1222 | // border. We want to extend this inset area so that it meets up with the caster |
| 1223 | // geometry. The inset geometry will by default already be inset by the blur width. |
| 1224 | // |
| 1225 | // We compare the min and max corners inset by the radius between the original |
| 1226 | // rrect and the shadow rrect. The distance between the two plus the difference |
| 1227 | // between the scaled radius and the original radius gives the distance from the |
| 1228 | // transformed shadow shape to the original shape in that corner. The max |
| 1229 | // of these gives the maximum distance we need to cover. |
| 1230 | // |
| 1231 | // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to |
| 1232 | // that to get the full insetWidth. |
| 1233 | SkScalar maxOffset; |
| 1234 | if (rrect.isRect()) { |
| 1235 | // Manhattan distance works better for rects |
| 1236 | maxOffset = SkTMax(SkTMax(SkTAbs(spotShadowRRect.rect().fLeft - |
| 1237 | rrect.rect().fLeft), |
| 1238 | SkTAbs(spotShadowRRect.rect().fTop - |
| 1239 | rrect.rect().fTop)), |
| 1240 | SkTMax(SkTAbs(spotShadowRRect.rect().fRight - |
| 1241 | rrect.rect().fRight), |
| 1242 | SkTAbs(spotShadowRRect.rect().fBottom - |
| 1243 | rrect.rect().fBottom))); |
| 1244 | } else { |
Mike Reed | 242135a | 2018-02-22 13:41:39 -0500 | [diff] [blame] | 1245 | SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX; |
Jim Van Verth | 3af1af9 | 2017-05-18 15:06:54 -0400 | [diff] [blame] | 1246 | SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft - |
| 1247 | rrect.rect().fLeft + dr, |
| 1248 | spotShadowRRect.rect().fTop - |
| 1249 | rrect.rect().fTop + dr); |
| 1250 | SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight - |
| 1251 | rrect.rect().fRight - dr, |
| 1252 | spotShadowRRect.rect().fBottom - |
| 1253 | rrect.rect().fBottom - dr); |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 1254 | maxOffset = SkScalarSqrt(SkTMax(SkPointPriv::LengthSqd(upperLeftOffset), |
| 1255 | SkPointPriv::LengthSqd(lowerRightOffset))) + dr; |
Jim Van Verth | 3af1af9 | 2017-05-18 15:06:54 -0400 | [diff] [blame] | 1256 | } |
Jim Van Verth | 4c8c1e8 | 2018-04-23 17:14:48 -0400 | [diff] [blame] | 1257 | insetWidth += SkTMax(blurOutset, maxOffset); |
Jim Van Verth | 3af1af9 | 2017-05-18 15:06:54 -0400 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | // Outset the shadow rrect to the border of the penumbra |
| 1261 | SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset); |
| 1262 | if (spotShadowRRect.isOval()) { |
| 1263 | spotShadowRRect = SkRRect::MakeOval(outsetRect); |
| 1264 | } else { |
| 1265 | SkScalar outsetRad = spotRadius + blurOutset; |
| 1266 | spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad); |
| 1267 | } |
| 1268 | |
Jim Van Verth | b1b80f7 | 2018-01-18 15:19:13 -0500 | [diff] [blame] | 1269 | GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor); |
Jim Van Verth | 34d6e4b | 2017-06-09 11:09:03 -0400 | [diff] [blame] | 1270 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 1271 | std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext, |
| 1272 | spotColor, |
| 1273 | viewMatrix, |
Brian Salomon | 0596909 | 2017-07-13 11:20:51 -0400 | [diff] [blame] | 1274 | spotShadowRRect, |
Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 1275 | 2.0f * devSpaceSpotBlur, |
Brian Salomon | 0596909 | 2017-07-13 11:20:51 -0400 | [diff] [blame] | 1276 | insetWidth); |
Robert Phillips | e576378 | 2019-04-17 14:38:24 -0400 | [diff] [blame] | 1277 | if (op) { |
| 1278 | this->addDrawOp(clip, std::move(op)); |
| 1279 | } |
Jim Van Verth | 3af1af9 | 2017-05-18 15:06:54 -0400 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | return true; |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | /////////////////////////////////////////////////////////////////////////////// |
| 1286 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1287 | bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 1288 | GrPaint&& paint, |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 1289 | GrAA aa, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1290 | const SkMatrix& viewMatrix, |
| 1291 | const SkRRect& origOuter, |
| 1292 | const SkRRect& origInner) { |
robertphillips | 0009589 | 2016-02-29 13:50:40 -0800 | [diff] [blame] | 1293 | SkASSERT(!origInner.isEmpty()); |
| 1294 | SkASSERT(!origOuter.isEmpty()); |
| 1295 | |
Brian Salomon | 6574921 | 2017-12-01 16:01:47 -0500 | [diff] [blame] | 1296 | SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter); |
| 1297 | |
Chris Dalton | 7d6748e | 2019-03-13 00:34:52 -0600 | [diff] [blame] | 1298 | GrAAType aaType = this->chooseAAType(aa); |
Brian Salomon | 45839f9 | 2017-12-04 09:02:35 -0500 | [diff] [blame] | 1299 | |
| 1300 | if (GrAAType::kMSAA == aaType) { |
| 1301 | return false; |
| 1302 | } |
| 1303 | |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 1304 | if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner) |
| 1305 | && SkRRectPriv::IsCircle(*outer)) { |
Brian Salomon | 6574921 | 2017-12-01 16:01:47 -0500 | [diff] [blame] | 1306 | auto outerR = outer->width() / 2.f; |
| 1307 | auto innerR = inner->width() / 2.f; |
| 1308 | auto cx = outer->getBounds().fLeft + outerR; |
| 1309 | auto cy = outer->getBounds().fTop + outerR; |
| 1310 | if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) && |
| 1311 | SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) { |
| 1312 | auto avgR = (innerR + outerR) / 2.f; |
| 1313 | auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR); |
| 1314 | SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle); |
| 1315 | stroke.setStrokeStyle(outerR - innerR); |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 1316 | auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 1317 | circleBounds, GrStyle(stroke, nullptr), |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1318 | this->caps()->shaderCaps()); |
Brian Salomon | 6574921 | 2017-12-01 16:01:47 -0500 | [diff] [blame] | 1319 | if (op) { |
| 1320 | this->addDrawOp(clip, std::move(op)); |
| 1321 | return true; |
| 1322 | } |
Mike Klein | 1688507 | 2018-12-11 09:54:31 -0500 | [diff] [blame] | 1323 | assert_alive(paint); |
Brian Salomon | 6574921 | 2017-12-01 16:01:47 -0500 | [diff] [blame] | 1324 | } |
| 1325 | } |
| 1326 | |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 1327 | GrClipEdgeType innerEdgeType, outerEdgeType; |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 1328 | if (GrAAType::kCoverage == aaType) { |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame] | 1329 | innerEdgeType = GrClipEdgeType::kInverseFillAA; |
| 1330 | outerEdgeType = GrClipEdgeType::kFillAA; |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 1331 | } else { |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame] | 1332 | innerEdgeType = GrClipEdgeType::kInverseFillBW; |
| 1333 | outerEdgeType = GrClipEdgeType::kFillBW; |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 1334 | } |
robertphillips | 0009589 | 2016-02-29 13:50:40 -0800 | [diff] [blame] | 1335 | |
robertphillips | 0009589 | 2016-02-29 13:50:40 -0800 | [diff] [blame] | 1336 | SkMatrix inverseVM; |
| 1337 | if (!viewMatrix.isIdentity()) { |
| 1338 | if (!origInner.transform(viewMatrix, inner.writable())) { |
| 1339 | return false; |
| 1340 | } |
| 1341 | if (!origOuter.transform(viewMatrix, outer.writable())) { |
| 1342 | return false; |
| 1343 | } |
| 1344 | if (!viewMatrix.invert(&inverseVM)) { |
| 1345 | return false; |
| 1346 | } |
| 1347 | } else { |
| 1348 | inverseVM.reset(); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 1349 | } |
robertphillips | 0009589 | 2016-02-29 13:50:40 -0800 | [diff] [blame] | 1350 | |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 1351 | const auto& caps = *this->caps()->shaderCaps(); |
robertphillips | 0009589 | 2016-02-29 13:50:40 -0800 | [diff] [blame] | 1352 | // TODO these need to be a geometry processors |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 1353 | auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps); |
robertphillips | 0009589 | 2016-02-29 13:50:40 -0800 | [diff] [blame] | 1354 | if (!innerEffect) { |
| 1355 | return false; |
| 1356 | } |
| 1357 | |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 1358 | auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps); |
robertphillips | 0009589 | 2016-02-29 13:50:40 -0800 | [diff] [blame] | 1359 | if (!outerEffect) { |
| 1360 | return false; |
| 1361 | } |
| 1362 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 1363 | paint.addCoverageFragmentProcessor(std::move(innerEffect)); |
| 1364 | paint.addCoverageFragmentProcessor(std::move(outerEffect)); |
robertphillips | 0009589 | 2016-02-29 13:50:40 -0800 | [diff] [blame] | 1365 | |
| 1366 | SkRect bounds = outer->getBounds(); |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 1367 | if (GrAAType::kCoverage == aaType) { |
robertphillips | 0009589 | 2016-02-29 13:50:40 -0800 | [diff] [blame] | 1368 | bounds.outset(SK_ScalarHalf, SK_ScalarHalf); |
| 1369 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 1370 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 1371 | this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds, |
| 1372 | inverseVM); |
robertphillips | 0009589 | 2016-02-29 13:50:40 -0800 | [diff] [blame] | 1373 | return true; |
| 1374 | } |
| 1375 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1376 | void GrRenderTargetContext::drawDRRect(const GrClip& clip, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 1377 | GrPaint&& paint, |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 1378 | GrAA aa, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1379 | const SkMatrix& viewMatrix, |
| 1380 | const SkRRect& outer, |
| 1381 | const SkRRect& inner) { |
robertphillips | 0009589 | 2016-02-29 13:50:40 -0800 | [diff] [blame] | 1382 | ASSERT_SINGLE_OWNER |
| 1383 | RETURN_IF_ABANDONED |
| 1384 | SkDEBUGCODE(this->validate();) |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 1385 | GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext); |
robertphillips | 0009589 | 2016-02-29 13:50:40 -0800 | [diff] [blame] | 1386 | |
| 1387 | SkASSERT(!outer.isEmpty()); |
| 1388 | SkASSERT(!inner.isEmpty()); |
| 1389 | |
Robert Phillips | 7215283 | 2017-01-25 17:31:35 -0500 | [diff] [blame] | 1390 | AutoCheckFlush acf(this->drawingManager()); |
robertphillips | 0009589 | 2016-02-29 13:50:40 -0800 | [diff] [blame] | 1391 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 1392 | if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) { |
robertphillips | 0009589 | 2016-02-29 13:50:40 -0800 | [diff] [blame] | 1393 | return; |
| 1394 | } |
Mike Klein | 1688507 | 2018-12-11 09:54:31 -0500 | [diff] [blame] | 1395 | assert_alive(paint); |
robertphillips | 0009589 | 2016-02-29 13:50:40 -0800 | [diff] [blame] | 1396 | |
| 1397 | SkPath path; |
| 1398 | path.setIsVolatile(true); |
| 1399 | path.addRRect(inner); |
| 1400 | path.addRRect(outer); |
| 1401 | path.setFillType(SkPath::kEvenOdd_FillType); |
Brian Salomon | 2fad74a | 2017-12-20 13:28:55 -0500 | [diff] [blame] | 1402 | this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(path)); |
robertphillips | 0009589 | 2016-02-29 13:50:40 -0800 | [diff] [blame] | 1403 | } |
| 1404 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1405 | /////////////////////////////////////////////////////////////////////////////// |
| 1406 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1407 | void GrRenderTargetContext::drawRegion(const GrClip& clip, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 1408 | GrPaint&& paint, |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 1409 | GrAA aa, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1410 | const SkMatrix& viewMatrix, |
| 1411 | const SkRegion& region, |
Stan Iliev | 73d8fd9 | 2017-08-02 15:36:24 -0400 | [diff] [blame] | 1412 | const GrStyle& style, |
| 1413 | const GrUserStencilSettings* ss) { |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 1414 | ASSERT_SINGLE_OWNER |
| 1415 | RETURN_IF_ABANDONED |
| 1416 | SkDEBUGCODE(this->validate();) |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 1417 | GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 1418 | |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 1419 | if (GrAA::kYes == aa) { |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 1420 | // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix |
Brian Salomon | c57c7c9 | 2016-12-06 14:47:34 -0500 | [diff] [blame] | 1421 | // to see whether aa is really required. |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 1422 | if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) && |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 1423 | SkScalarIsInt(viewMatrix.getTranslateX()) && |
| 1424 | SkScalarIsInt(viewMatrix.getTranslateY())) { |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 1425 | aa = GrAA::kNo; |
| 1426 | } |
Brian Salomon | c57c7c9 | 2016-12-06 14:47:34 -0500 | [diff] [blame] | 1427 | } |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 1428 | bool complexStyle = !style.isSimpleFill(); |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 1429 | if (complexStyle || GrAA::kYes == aa) { |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 1430 | SkPath path; |
| 1431 | region.getBoundaryPath(&path); |
Robert Phillips | 46a1338 | 2018-08-23 13:53:01 -0400 | [diff] [blame] | 1432 | path.setIsVolatile(true); |
| 1433 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 1434 | return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 1435 | } |
| 1436 | |
Chris Dalton | 7d6748e | 2019-03-13 00:34:52 -0600 | [diff] [blame] | 1437 | GrAAType aaType = this->chooseAAType(GrAA::kNo); |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 1438 | std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region, |
| 1439 | aaType, ss); |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 1440 | this->addDrawOp(clip, std::move(op)); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 1441 | } |
| 1442 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1443 | void GrRenderTargetContext::drawOval(const GrClip& clip, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 1444 | GrPaint&& paint, |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 1445 | GrAA aa, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1446 | const SkMatrix& viewMatrix, |
| 1447 | const SkRect& oval, |
| 1448 | const GrStyle& style) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1449 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1450 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 1451 | SkDEBUGCODE(this->validate();) |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 1452 | GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 1453 | |
Robert Phillips | 7484d20 | 2018-07-03 09:09:08 -0400 | [diff] [blame] | 1454 | const SkStrokeRec& stroke = style.strokeRec(); |
| 1455 | |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1456 | if (oval.isEmpty() && !style.pathEffect()) { |
Robert Phillips | 7484d20 | 2018-07-03 09:09:08 -0400 | [diff] [blame] | 1457 | if (stroke.getStyle() == SkStrokeRec::kFill_Style) { |
| 1458 | return; |
| 1459 | } |
| 1460 | |
| 1461 | this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style); |
Brian Salomon | 62e4f3d | 2018-04-20 13:54:11 -0400 | [diff] [blame] | 1462 | return; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1463 | } |
| 1464 | |
Robert Phillips | 7215283 | 2017-01-25 17:31:35 -0500 | [diff] [blame] | 1465 | AutoCheckFlush acf(this->drawingManager()); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 1466 | |
Chris Dalton | 7d6748e | 2019-03-13 00:34:52 -0600 | [diff] [blame] | 1467 | GrAAType aaType = this->chooseAAType(aa); |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 1468 | |
| 1469 | std::unique_ptr<GrDrawOp> op; |
Jim Van Verth | d7871cc | 2019-06-27 13:08:04 -0400 | [diff] [blame] | 1470 | if (GrAAType::kCoverage == aaType && oval.width() > SK_ScalarNearlyZero && |
| 1471 | oval.width() == oval.height() && viewMatrix.isSimilarity()) { |
Jim Van Verth | 64b8589 | 2019-06-17 12:01:46 -0400 | [diff] [blame] | 1472 | // We don't draw true circles as round rects in coverage mode, because it can |
| 1473 | // cause perf regressions on some platforms as compared to the dedicated circle Op. |
| 1474 | assert_alive(paint); |
| 1475 | op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style, |
| 1476 | this->caps()->shaderCaps()); |
| 1477 | } |
| 1478 | if (!op && style.isSimpleFill()) { |
Chris Dalton | 82eb9e7 | 2019-03-21 14:26:39 -0600 | [diff] [blame] | 1479 | // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate |
| 1480 | // the arc equation. This same special geometry and fragment branch also turn out to be a |
| 1481 | // substantial optimization for drawing ovals (namely, by not evaluating the arc equation |
| 1482 | // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw |
| 1483 | // ovals the exact same way we do round rects. |
Jim Van Verth | 64b8589 | 2019-06-17 12:01:46 -0400 | [diff] [blame] | 1484 | assert_alive(paint); |
| 1485 | op = GrFillRRectOp::Make(fContext, aaType, viewMatrix, SkRRect::MakeOval(oval), |
| 1486 | *this->caps(), std::move(paint)); |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 1487 | } |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 1488 | if (!op && GrAAType::kCoverage == aaType) { |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 1489 | assert_alive(paint); |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 1490 | op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style, |
| 1491 | this->caps()->shaderCaps()); |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 1492 | } |
| 1493 | if (op) { |
| 1494 | this->addDrawOp(clip, std::move(op)); |
| 1495 | return; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1496 | } |
robertphillips | b56f927 | 2016-02-25 11:03:52 -0800 | [diff] [blame] | 1497 | |
Mike Klein | 1688507 | 2018-12-11 09:54:31 -0500 | [diff] [blame] | 1498 | assert_alive(paint); |
Brian Salomon | 5209d7f | 2018-04-20 16:52:42 -0400 | [diff] [blame] | 1499 | this->drawShapeUsingPathRenderer( |
| 1500 | clip, std::move(paint), aa, viewMatrix, |
| 1501 | GrShape(SkRRect::MakeOval(oval), SkPath::kCW_Direction, 2, false, style)); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1502 | } |
| 1503 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1504 | void GrRenderTargetContext::drawArc(const GrClip& clip, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 1505 | GrPaint&& paint, |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 1506 | GrAA aa, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1507 | const SkMatrix& viewMatrix, |
| 1508 | const SkRect& oval, |
| 1509 | SkScalar startAngle, |
| 1510 | SkScalar sweepAngle, |
| 1511 | bool useCenter, |
| 1512 | const GrStyle& style) { |
Robert Phillips | f1d0ced | 2017-02-10 09:31:01 -0500 | [diff] [blame] | 1513 | ASSERT_SINGLE_OWNER |
| 1514 | RETURN_IF_ABANDONED |
| 1515 | SkDEBUGCODE(this->validate();) |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 1516 | GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext); |
Robert Phillips | f1d0ced | 2017-02-10 09:31:01 -0500 | [diff] [blame] | 1517 | |
| 1518 | AutoCheckFlush acf(this->drawingManager()); |
| 1519 | |
Chris Dalton | 7d6748e | 2019-03-13 00:34:52 -0600 | [diff] [blame] | 1520 | GrAAType aaType = this->chooseAAType(aa); |
Greg Daniel | 2655ede | 2019-04-10 00:49:28 +0000 | [diff] [blame] | 1521 | if (GrAAType::kCoverage == aaType) { |
| 1522 | const GrShaderCaps* shaderCaps = this->caps()->shaderCaps(); |
| 1523 | std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext, |
| 1524 | std::move(paint), |
| 1525 | viewMatrix, |
| 1526 | oval, |
| 1527 | startAngle, |
| 1528 | sweepAngle, |
| 1529 | useCenter, |
| 1530 | style, |
| 1531 | shaderCaps); |
| 1532 | if (op) { |
| 1533 | this->addDrawOp(clip, std::move(op)); |
| 1534 | return; |
| 1535 | } |
| 1536 | assert_alive(paint); |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 1537 | } |
Brian Salomon | e494940 | 2018-04-26 15:22:04 -0400 | [diff] [blame] | 1538 | this->drawShapeUsingPathRenderer( |
| 1539 | clip, std::move(paint), aa, viewMatrix, |
| 1540 | GrShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style)); |
bsalomon | 4f3a0ca | 2016-08-22 13:14:26 -0700 | [diff] [blame] | 1541 | } |
| 1542 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1543 | void GrRenderTargetContext::drawImageLattice(const GrClip& clip, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 1544 | GrPaint&& paint, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1545 | const SkMatrix& viewMatrix, |
Brian Salomon | 2a943df | 2018-05-04 13:43:19 -0400 | [diff] [blame] | 1546 | sk_sp<GrTextureProxy> image, |
| 1547 | sk_sp<GrColorSpaceXform> csxf, |
| 1548 | GrSamplerState::Filter filter, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1549 | std::unique_ptr<SkLatticeIter> iter, |
| 1550 | const SkRect& dst) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 1551 | ASSERT_SINGLE_OWNER |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 1552 | RETURN_IF_ABANDONED |
| 1553 | SkDEBUGCODE(this->validate();) |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 1554 | GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 1555 | |
Robert Phillips | 7215283 | 2017-01-25 17:31:35 -0500 | [diff] [blame] | 1556 | AutoCheckFlush acf(this->drawingManager()); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 1557 | |
Brian Salomon | 2a943df | 2018-05-04 13:43:19 -0400 | [diff] [blame] | 1558 | std::unique_ptr<GrDrawOp> op = |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 1559 | GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(image), |
| 1560 | std::move(csxf), filter, std::move(iter), dst); |
Brian Salomon | 815486c | 2017-07-11 08:52:13 -0400 | [diff] [blame] | 1561 | this->addDrawOp(clip, std::move(op)); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 1562 | } |
| 1563 | |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 1564 | void GrRenderTargetContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable, |
| 1565 | const SkRect& bounds) { |
| 1566 | std::unique_ptr<GrOp> op(GrDrawableOp::Make(fContext, std::move(drawable), bounds)); |
| 1567 | SkASSERT(op); |
| 1568 | this->getRTOpList()->addOp(std::move(op), *this->caps()); |
| 1569 | } |
| 1570 | |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1571 | sk_sp<GrRenderTargetContext> GrRenderTargetContext::rescale(const SkImageInfo& info, |
| 1572 | const SkIRect& srcRect, |
| 1573 | SkSurface::RescaleGamma rescaleGamma, |
| 1574 | SkFilterQuality rescaleQuality) { |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 1575 | auto direct = fContext->priv().asDirectContext(); |
| 1576 | if (!direct) { |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1577 | return nullptr; |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 1578 | } |
| 1579 | if (fRenderTargetProxy->wrapsVkSecondaryCB()) { |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1580 | return nullptr; |
| 1581 | } |
| 1582 | |
Brian Salomon | 4d03689 | 2019-07-02 15:10:58 -0400 | [diff] [blame] | 1583 | // We rescale by drawing and don't currently support drawing to a kUnpremul destination. |
| 1584 | if (info.alphaType() == kUnpremul_SkAlphaType) { |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1585 | return nullptr; |
| 1586 | } |
| 1587 | |
| 1588 | int srcW = srcRect.width(); |
| 1589 | int srcH = srcRect.height(); |
| 1590 | int srcX = srcRect.fLeft; |
| 1591 | int srcY = srcRect.fTop; |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 1592 | sk_sp<GrTextureProxy> texProxy = sk_ref_sp(fRenderTargetProxy->asTextureProxy()); |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1593 | SkCanvas::SrcRectConstraint constraint = SkCanvas::kStrict_SrcRectConstraint; |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 1594 | if (!texProxy) { |
| 1595 | texProxy = GrSurfaceProxy::Copy(fContext, fRenderTargetProxy.get(), GrMipMapped::kNo, |
| 1596 | srcRect, SkBackingFit::kApprox, SkBudgeted::kNo); |
| 1597 | if (!texProxy) { |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1598 | return nullptr; |
| 1599 | } |
| 1600 | srcX = 0; |
| 1601 | srcY = 0; |
| 1602 | constraint = SkCanvas::kFast_SrcRectConstraint; |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | float sx = (float)info.width() / srcW; |
| 1606 | float sy = (float)info.height() / srcH; |
| 1607 | |
| 1608 | // How many bilerp/bicubic steps to do in X and Y. + means upscaling, - means downscaling. |
| 1609 | int stepsX; |
| 1610 | int stepsY; |
| 1611 | if (rescaleQuality > kNone_SkFilterQuality) { |
| 1612 | stepsX = static_cast<int>((sx > 1.f) ? std::ceil(std::log2f(sx)) |
| 1613 | : std::floor(std::log2f(sx))); |
| 1614 | stepsY = static_cast<int>((sy > 1.f) ? std::ceil(std::log2f(sy)) |
| 1615 | : std::floor(std::log2f(sy))); |
| 1616 | } else { |
| 1617 | stepsX = sx != 1.f; |
| 1618 | stepsY = sy != 1.f; |
| 1619 | } |
| 1620 | SkASSERT(stepsX || stepsY); |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 1621 | auto currRTC = sk_ref_sp(this); |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1622 | // Assume we should ignore the rescale linear request if the surface has no color space since |
| 1623 | // it's unclear how we'd linearize from an unknown color space. |
Brian Salomon | 4d03689 | 2019-07-02 15:10:58 -0400 | [diff] [blame] | 1624 | if (rescaleGamma == SkSurface::RescaleGamma::kLinear && this->colorSpaceInfo().colorSpace() && |
| 1625 | !this->colorSpaceInfo().colorSpace()->gammaIsLinear()) { |
| 1626 | auto cs = this->colorSpaceInfo().colorSpace()->makeLinearGamma(); |
Brian Salomon | 4d03689 | 2019-07-02 15:10:58 -0400 | [diff] [blame] | 1627 | auto xform = GrColorSpaceXform::Make(this->colorSpaceInfo().colorSpace(), |
| 1628 | this->colorSpaceInfo().alphaType(), |
| 1629 | cs.get(), kPremul_SkAlphaType); |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1630 | // We'll fall back to kRGBA_8888 if half float not supported. |
| 1631 | auto linearRTC = fContext->priv().makeDeferredRenderTargetContextWithFallback( |
Brian Salomon | 27ae52c | 2019-07-03 11:27:44 -0400 | [diff] [blame] | 1632 | SkBackingFit::kExact, srcW, srcH, GrColorType::kRGBA_F16, cs, 1, GrMipMapped::kNo, |
| 1633 | kTopLeft_GrSurfaceOrigin); |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1634 | if (!linearRTC) { |
| 1635 | return nullptr; |
| 1636 | } |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 1637 | linearRTC->drawTexture(GrNoClip(), texProxy, |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1638 | GrSamplerState::Filter::kNearest, SkBlendMode::kSrc, |
| 1639 | SK_PMColor4fWHITE, SkRect::Make(srcRect), SkRect::MakeWH(srcW, srcH), |
| 1640 | GrAA::kNo, GrQuadAAFlags::kNone, constraint, SkMatrix::I(), |
| 1641 | std::move(xform)); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 1642 | texProxy = linearRTC->asTextureProxyRef(); |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 1643 | currRTC = std::move(linearRTC); |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1644 | srcX = 0; |
| 1645 | srcY = 0; |
| 1646 | constraint = SkCanvas::kFast_SrcRectConstraint; |
| 1647 | } |
| 1648 | while (stepsX || stepsY) { |
| 1649 | int nextW = info.width(); |
| 1650 | int nextH = info.height(); |
| 1651 | if (stepsX < 0) { |
| 1652 | nextW = info.width() << (-stepsX - 1); |
| 1653 | stepsX++; |
| 1654 | } else if (stepsX != 0) { |
| 1655 | if (stepsX > 1) { |
| 1656 | nextW = srcW * 2; |
| 1657 | } |
| 1658 | --stepsX; |
| 1659 | } |
| 1660 | if (stepsY < 0) { |
| 1661 | nextH = info.height() << (-stepsY - 1); |
| 1662 | stepsY++; |
| 1663 | } else if (stepsY != 0) { |
| 1664 | if (stepsY > 1) { |
| 1665 | nextH = srcH * 2; |
| 1666 | } |
| 1667 | --stepsY; |
| 1668 | } |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 1669 | GrColorType colorType = currRTC->colorSpaceInfo().colorType(); |
Brian Salomon | 4d03689 | 2019-07-02 15:10:58 -0400 | [diff] [blame] | 1670 | auto cs = currRTC->colorSpaceInfo().refColorSpace(); |
Brian Salomon | 900dda8 | 2019-06-03 13:12:55 -0400 | [diff] [blame] | 1671 | sk_sp<GrColorSpaceXform> xform; |
Brian Salomon | 4d03689 | 2019-07-02 15:10:58 -0400 | [diff] [blame] | 1672 | auto prevAlphaType = currRTC->colorSpaceInfo().alphaType(); |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1673 | if (!stepsX && !stepsY) { |
| 1674 | // Might as well fold conversion to final info in the last step. |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1675 | cs = info.refColorSpace(); |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 1676 | colorType = SkColorTypeToGrColorType(info.colorType()); |
Brian Salomon | 4d03689 | 2019-07-02 15:10:58 -0400 | [diff] [blame] | 1677 | xform = GrColorSpaceXform::Make(currRTC->colorSpaceInfo().colorSpace(), |
| 1678 | currRTC->colorSpaceInfo().alphaType(), |
| 1679 | cs.get(), info.alphaType()); |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1680 | } |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 1681 | currRTC = fContext->priv().makeDeferredRenderTargetContextWithFallback( |
Brian Salomon | 27ae52c | 2019-07-03 11:27:44 -0400 | [diff] [blame] | 1682 | SkBackingFit::kExact, nextW, nextH, colorType, std::move(cs), 1, GrMipMapped::kNo, |
| 1683 | kTopLeft_GrSurfaceOrigin); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 1684 | if (!currRTC) { |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1685 | return nullptr; |
| 1686 | } |
| 1687 | auto dstRect = SkRect::MakeWH(nextW, nextH); |
| 1688 | if (rescaleQuality == kHigh_SkFilterQuality) { |
| 1689 | SkMatrix matrix; |
| 1690 | matrix.setScaleTranslate((float)srcW / nextW, (float)srcH / nextH, srcX, srcY); |
| 1691 | std::unique_ptr<GrFragmentProcessor> fp; |
Brian Salomon | a86fc7a | 2019-05-28 20:42:58 -0400 | [diff] [blame] | 1692 | auto dir = GrBicubicEffect::Direction::kXY; |
| 1693 | if (nextW == srcW) { |
| 1694 | dir = GrBicubicEffect::Direction::kY; |
| 1695 | } else if (nextH == srcH) { |
| 1696 | dir = GrBicubicEffect::Direction::kX; |
| 1697 | } |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 1698 | if (srcW != texProxy->width() || srcH != texProxy->height()) { |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1699 | auto domain = GrTextureDomain::MakeTexelDomain( |
| 1700 | SkIRect::MakeXYWH(srcX, srcY, srcW, srcH), GrTextureDomain::kClamp_Mode); |
Brian Salomon | 4d03689 | 2019-07-02 15:10:58 -0400 | [diff] [blame] | 1701 | fp = GrBicubicEffect::Make(texProxy, matrix, domain, dir, prevAlphaType); |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1702 | } else { |
Brian Salomon | 4d03689 | 2019-07-02 15:10:58 -0400 | [diff] [blame] | 1703 | fp = GrBicubicEffect::Make(texProxy, matrix, dir, prevAlphaType); |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1704 | } |
Brian Salomon | 900dda8 | 2019-06-03 13:12:55 -0400 | [diff] [blame] | 1705 | if (xform) { |
| 1706 | fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform)); |
| 1707 | } |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1708 | GrPaint paint; |
| 1709 | paint.addColorFragmentProcessor(std::move(fp)); |
| 1710 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Michael Ludwig | 6132820 | 2019-06-19 14:48:58 +0000 | [diff] [blame] | 1711 | currRTC->fillRectToRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), |
| 1712 | dstRect, dstRect); |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1713 | } else { |
| 1714 | auto filter = rescaleQuality == kNone_SkFilterQuality ? GrSamplerState::Filter::kNearest |
| 1715 | : GrSamplerState::Filter::kBilerp; |
| 1716 | auto srcSubset = SkRect::MakeXYWH(srcX, srcY, srcW, srcH); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 1717 | currRTC->drawTexture(GrNoClip(), texProxy, filter, SkBlendMode::kSrc, SK_PMColor4fWHITE, |
| 1718 | srcSubset, dstRect, GrAA::kNo, GrQuadAAFlags::kNone, constraint, |
| 1719 | SkMatrix::I(), std::move(xform)); |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1720 | } |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 1721 | texProxy = currRTC->asTextureProxyRef(); |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1722 | srcX = srcY = 0; |
| 1723 | srcW = nextW; |
| 1724 | srcH = nextH; |
| 1725 | constraint = SkCanvas::kFast_SrcRectConstraint; |
| 1726 | } |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 1727 | SkASSERT(currRTC); |
| 1728 | return currRTC; |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1729 | } |
| 1730 | |
| 1731 | void GrRenderTargetContext::asyncRescaleAndReadPixels( |
| 1732 | const SkImageInfo& info, const SkIRect& srcRect, SkSurface::RescaleGamma rescaleGamma, |
| 1733 | SkFilterQuality rescaleQuality, ReadPixelsCallback callback, ReadPixelsContext context) { |
| 1734 | auto direct = fContext->priv().asDirectContext(); |
| 1735 | if (!direct) { |
| 1736 | callback(context, nullptr, 0); |
| 1737 | return; |
| 1738 | } |
| 1739 | if (fRenderTargetProxy->wrapsVkSecondaryCB()) { |
| 1740 | callback(context, nullptr, 0); |
| 1741 | return; |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 1742 | } |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1743 | auto dstCT = SkColorTypeToGrColorType(info.colorType()); |
| 1744 | bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height(); |
Greg Daniel | c6dc5cf | 2019-07-17 16:02:00 -0400 | [diff] [blame^] | 1745 | auto colorTypeOfFinalContext = this->colorSpaceInfo().colorType(); |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1746 | GrPixelConfig configOfFinalContext = fRenderTargetProxy->config(); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 1747 | auto backendFormatOfFinalContext = fRenderTargetProxy->backendFormat(); |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1748 | if (needsRescale) { |
Greg Daniel | c6dc5cf | 2019-07-17 16:02:00 -0400 | [diff] [blame^] | 1749 | colorTypeOfFinalContext = dstCT; |
Greg Daniel | 627d053 | 2019-07-08 16:48:14 -0400 | [diff] [blame] | 1750 | backendFormatOfFinalContext = this->caps()->getBackendFormatFromColorType(dstCT); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 1751 | configOfFinalContext = this->caps()->getConfigFromBackendFormat(backendFormatOfFinalContext, |
Greg Daniel | 627d053 | 2019-07-08 16:48:14 -0400 | [diff] [blame] | 1752 | dstCT); |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1753 | } |
Greg Daniel | c6dc5cf | 2019-07-17 16:02:00 -0400 | [diff] [blame^] | 1754 | auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext, |
| 1755 | configOfFinalContext, |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 1756 | backendFormatOfFinalContext, dstCT); |
| 1757 | // Fail if we can't read from the source surface's color type. |
| 1758 | if (readInfo.fColorType == GrColorType::kUnknown) { |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1759 | callback(context, nullptr, 0); |
| 1760 | return; |
| 1761 | } |
Brian Salomon | 624f906 | 2019-07-02 14:23:00 -0400 | [diff] [blame] | 1762 | // Fail if read color type does not have all of dstCT's color channels and those missing color |
| 1763 | // channels are in the src. |
| 1764 | uint32_t dstComponents = GrColorTypeComponentFlags(dstCT); |
| 1765 | uint32_t legalReadComponents = GrColorTypeComponentFlags(readInfo.fColorType); |
| 1766 | uint32_t srcComponents = GrColorTypeComponentFlags(this->colorSpaceInfo().colorType()); |
| 1767 | if ((~legalReadComponents & dstComponents) & srcComponents) { |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1768 | callback(context, nullptr, 0); |
| 1769 | return; |
| 1770 | } |
| 1771 | |
| 1772 | sk_sp<GrRenderTargetContext> rtc; |
| 1773 | int x = srcRect.fLeft; |
| 1774 | int y = srcRect.fTop; |
| 1775 | if (needsRescale) { |
| 1776 | rtc = this->rescale(info, srcRect, rescaleGamma, rescaleQuality); |
| 1777 | if (!rtc) { |
| 1778 | callback(context, nullptr, 0); |
| 1779 | return; |
| 1780 | } |
| 1781 | SkASSERT(SkColorSpace::Equals(rtc->colorSpaceInfo().colorSpace(), info.colorSpace())); |
| 1782 | SkASSERT(rtc->origin() == kTopLeft_GrSurfaceOrigin); |
| 1783 | x = y = 0; |
| 1784 | } else { |
| 1785 | sk_sp<GrColorSpaceXform> xform = |
Brian Salomon | 4d03689 | 2019-07-02 15:10:58 -0400 | [diff] [blame] | 1786 | GrColorSpaceXform::Make(this->colorSpaceInfo().colorSpace(), |
| 1787 | this->colorSpaceInfo().alphaType(), |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1788 | info.colorSpace(), info.alphaType()); |
| 1789 | // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion. |
| 1790 | if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) { |
Brian Salomon | 4d03689 | 2019-07-02 15:10:58 -0400 | [diff] [blame] | 1791 | // We flip or color convert by drawing and we don't currently support drawing to |
| 1792 | // kPremul. |
| 1793 | if (info.alphaType() == kUnpremul_SkAlphaType) { |
| 1794 | callback(context, nullptr, 0); |
| 1795 | return; |
| 1796 | } |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1797 | sk_sp<GrTextureProxy> texProxy = sk_ref_sp(fRenderTargetProxy->asTextureProxy()); |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1798 | SkRect srcRectToDraw = SkRect::Make(srcRect); |
| 1799 | // If the src is not texturable first try to make a copy to a texture. |
| 1800 | if (!texProxy) { |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 1801 | texProxy = GrSurfaceProxy::Copy(fContext, fRenderTargetProxy.get(), |
| 1802 | GrMipMapped::kNo, srcRect, SkBackingFit::kApprox, |
| 1803 | SkBudgeted::kNo); |
| 1804 | if (!texProxy) { |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1805 | callback(context, nullptr, 0); |
| 1806 | return; |
| 1807 | } |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1808 | srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height()); |
| 1809 | } |
| 1810 | rtc = direct->priv().makeDeferredRenderTargetContext( |
Brian Salomon | 27ae52c | 2019-07-03 11:27:44 -0400 | [diff] [blame] | 1811 | SkBackingFit::kApprox, srcRect.width(), srcRect.height(), |
| 1812 | this->colorSpaceInfo().colorType(), info.refColorSpace(), 1, GrMipMapped::kNo, |
| 1813 | kTopLeft_GrSurfaceOrigin); |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1814 | if (!rtc) { |
| 1815 | callback(context, nullptr, 0); |
| 1816 | return; |
| 1817 | } |
| 1818 | rtc->drawTexture(GrNoClip(), std::move(texProxy), GrSamplerState::Filter::kNearest, |
| 1819 | SkBlendMode::kSrc, SK_PMColor4fWHITE, srcRectToDraw, |
| 1820 | SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo, |
| 1821 | GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint, SkMatrix::I(), |
| 1822 | std::move(xform)); |
| 1823 | x = y = 0; |
| 1824 | } else { |
| 1825 | rtc = sk_ref_sp(this); |
| 1826 | } |
| 1827 | } |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 1828 | return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()), |
| 1829 | info.colorType(), callback, context); |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1830 | } |
| 1831 | |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 1832 | GrRenderTargetContext::PixelTransferResult GrRenderTargetContext::transferPixels( |
| 1833 | GrColorType dstCT, const SkIRect& rect) { |
| 1834 | SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width()); |
| 1835 | SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height()); |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1836 | auto direct = fContext->priv().asDirectContext(); |
| 1837 | if (!direct) { |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 1838 | return {}; |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1839 | } |
| 1840 | if (fRenderTargetProxy->wrapsVkSecondaryCB()) { |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 1841 | return {}; |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1842 | } |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 1843 | auto supportedRead = this->caps()->supportedReadPixelsColorType( |
Greg Daniel | c6dc5cf | 2019-07-17 16:02:00 -0400 | [diff] [blame^] | 1844 | this->colorSpaceInfo().colorType(), fRenderTargetProxy->config(), |
| 1845 | fRenderTargetProxy->backendFormat(), dstCT); |
Brian Salomon | 624f906 | 2019-07-02 14:23:00 -0400 | [diff] [blame] | 1846 | // Fail if read color type does not have all of dstCT's color channels and those missing color |
| 1847 | // channels are in the src. |
| 1848 | uint32_t dstComponents = GrColorTypeComponentFlags(dstCT); |
| 1849 | uint32_t legalReadComponents = GrColorTypeComponentFlags(supportedRead.fColorType); |
| 1850 | uint32_t srcComponents = GrColorTypeComponentFlags(this->colorSpaceInfo().colorType()); |
| 1851 | if ((~legalReadComponents & dstComponents) & srcComponents) { |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 1852 | return {}; |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 1853 | } |
| 1854 | |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1855 | if (!this->caps()->transferBufferSupport() || |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 1856 | !this->caps()->transferFromOffsetAlignment(supportedRead.fColorType)) { |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 1857 | return {}; |
| 1858 | } |
| 1859 | |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 1860 | size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width(); |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 1861 | size_t size = rowBytes * rect.height(); |
| 1862 | auto buffer = direct->priv().resourceProvider()->createBuffer( |
| 1863 | size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern); |
| 1864 | if (!buffer) { |
| 1865 | return {}; |
| 1866 | } |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 1867 | auto srcRect = rect; |
| 1868 | bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin; |
| 1869 | if (flip) { |
| 1870 | srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight, |
| 1871 | this->height() - rect.fTop); |
| 1872 | } |
| 1873 | auto op = GrTransferFromOp::Make(fContext, srcRect, supportedRead.fColorType, buffer, 0); |
| 1874 | this->getRTOpList()->addOp(std::move(op), *this->caps()); |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 1875 | PixelTransferResult result; |
| 1876 | result.fTransferBuffer = std::move(buffer); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 1877 | if (supportedRead.fColorType != dstCT || supportedRead.fSwizzle != GrSwizzle("rgba") || flip) { |
| 1878 | result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead]( |
| 1879 | void* dst, const void* src) { |
Brian Salomon | 4d03689 | 2019-07-02 15:10:58 -0400 | [diff] [blame] | 1880 | // We're using kPremul here for src and dst simply because we don't want any alpha type |
| 1881 | // conversions. |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 1882 | GrPixelInfo srcInfo(supportedRead.fColorType, kPremul_SkAlphaType, nullptr, w, h); |
| 1883 | GrPixelInfo dstInfo(dstCT, kPremul_SkAlphaType, nullptr, w, h); |
| 1884 | GrConvertPixels(dstInfo, dst, dstInfo.minRowBytes(), |
| 1885 | srcInfo, src, srcInfo.minRowBytes(), |
| 1886 | /* flipY = */ false, supportedRead.fSwizzle); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 1887 | }; |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 1888 | } |
| 1889 | return result; |
| 1890 | } |
| 1891 | |
| 1892 | void GrRenderTargetContext::asyncReadPixels(const SkIRect& rect, SkColorType colorType, |
| 1893 | ReadPixelsCallback callback, |
| 1894 | ReadPixelsContext context) { |
| 1895 | SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width()); |
| 1896 | SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height()); |
| 1897 | |
| 1898 | auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect); |
| 1899 | |
| 1900 | if (!transferResult.fTransferBuffer) { |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1901 | SkAutoPixmapStorage pm; |
Brian Salomon | 4d03689 | 2019-07-02 15:10:58 -0400 | [diff] [blame] | 1902 | auto ii = SkImageInfo::Make(rect.width(), rect.height(), colorType, |
| 1903 | this->colorSpaceInfo().alphaType(), |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 1904 | this->colorSpaceInfo().refColorSpace()); |
| 1905 | pm.alloc(ii); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 1906 | if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) { |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1907 | callback(context, nullptr, 0); |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 1908 | } |
Brian Salomon | 031b0ba | 2019-05-23 11:05:26 -0400 | [diff] [blame] | 1909 | callback(context, pm.addr(), pm.rowBytes()); |
| 1910 | return; |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 1911 | } |
Brian Salomon | cd734f6 | 2019-05-10 16:32:54 -0400 | [diff] [blame] | 1912 | |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 1913 | struct FinishContext { |
| 1914 | ReadPixelsCallback* fClientCallback; |
| 1915 | ReadPixelsContext fClientContext; |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 1916 | int fW, fH; |
| 1917 | SkColorType fColorType; |
| 1918 | PixelTransferResult fTransferResult; |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 1919 | }; |
| 1920 | // Assumption is that the caller would like to flush. We could take a parameter or require an |
| 1921 | // explicit flush from the caller. We'd have to have a way to defer attaching the finish |
| 1922 | // callback to GrGpu until after the next flush that flushes our op list, though. |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 1923 | auto* finishContext = new FinishContext{callback, context, rect.width(), |
| 1924 | rect.height(), colorType, std::move(transferResult)}; |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 1925 | auto finishCallback = [](GrGpuFinishedContext c) { |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 1926 | const auto* context = reinterpret_cast<const FinishContext*>(c); |
| 1927 | const void* data = context->fTransferResult.fTransferBuffer->map(); |
Brian Salomon | cd734f6 | 2019-05-10 16:32:54 -0400 | [diff] [blame] | 1928 | if (!data) { |
| 1929 | (*context->fClientCallback)(context->fClientContext, nullptr, 0); |
| 1930 | delete context; |
| 1931 | return; |
| 1932 | } |
Brian Salomon | 4d03689 | 2019-07-02 15:10:58 -0400 | [diff] [blame] | 1933 | std::unique_ptr<char[]> tmp; |
| 1934 | size_t rowBytes = context->fW * SkColorTypeBytesPerPixel(context->fColorType); |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 1935 | if (context->fTransferResult.fPixelConverter) { |
Brian Salomon | 4d03689 | 2019-07-02 15:10:58 -0400 | [diff] [blame] | 1936 | tmp.reset(new char[rowBytes * context->fH]); |
| 1937 | context->fTransferResult.fPixelConverter(tmp.get(), data); |
| 1938 | data = tmp.get(); |
Brian Salomon | cd734f6 | 2019-05-10 16:32:54 -0400 | [diff] [blame] | 1939 | } |
Brian Salomon | 4d03689 | 2019-07-02 15:10:58 -0400 | [diff] [blame] | 1940 | (*context->fClientCallback)(context->fClientContext, data, rowBytes); |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 1941 | delete context; |
| 1942 | }; |
| 1943 | GrFlushInfo flushInfo; |
| 1944 | flushInfo.fFinishedContext = finishContext; |
| 1945 | flushInfo.fFinishedProc = finishCallback; |
| 1946 | this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo); |
| 1947 | } |
| 1948 | |
| 1949 | void GrRenderTargetContext::asyncRescaleAndReadPixelsYUV420( |
| 1950 | SkYUVColorSpace yuvColorSpace, sk_sp<SkColorSpace> dstColorSpace, const SkIRect& srcRect, |
| 1951 | int dstW, int dstH, RescaleGamma rescaleGamma, SkFilterQuality rescaleQuality, |
| 1952 | ReadPixelsCallbackYUV420 callback, ReadPixelsContext context) { |
| 1953 | SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width()); |
| 1954 | SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height()); |
| 1955 | SkASSERT((dstW % 2 == 0) && (dstH % 2 == 0)); |
| 1956 | auto direct = fContext->priv().asDirectContext(); |
| 1957 | if (!direct) { |
| 1958 | callback(context, nullptr, nullptr); |
| 1959 | return; |
| 1960 | } |
| 1961 | if (fRenderTargetProxy->wrapsVkSecondaryCB()) { |
| 1962 | callback(context, nullptr, nullptr); |
| 1963 | return; |
| 1964 | } |
| 1965 | if (dstW & 0x1) { |
| 1966 | return; |
| 1967 | } |
| 1968 | int x = srcRect.fLeft; |
| 1969 | int y = srcRect.fTop; |
| 1970 | auto rtc = sk_ref_sp(this); |
| 1971 | bool needsRescale = srcRect.width() != dstW || srcRect.height() != dstH; |
| 1972 | if (needsRescale) { |
Brian Salomon | 4d03689 | 2019-07-02 15:10:58 -0400 | [diff] [blame] | 1973 | // We assume the caller wants kPremul. There is no way to indicate a preference. |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 1974 | auto info = SkImageInfo::Make(dstW, dstH, kRGBA_8888_SkColorType, kPremul_SkAlphaType, |
| 1975 | dstColorSpace); |
| 1976 | // TODO: Incorporate the YUV conversion into last pass of rescaling. |
| 1977 | rtc = this->rescale(info, srcRect, rescaleGamma, rescaleQuality); |
| 1978 | if (!rtc) { |
| 1979 | callback(context, nullptr, nullptr); |
| 1980 | return; |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 1981 | } |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 1982 | SkASSERT(SkColorSpace::Equals(rtc->colorSpaceInfo().colorSpace(), info.colorSpace())); |
| 1983 | SkASSERT(rtc->origin() == kTopLeft_GrSurfaceOrigin); |
| 1984 | x = y = 0; |
| 1985 | } else { |
Brian Salomon | 4d03689 | 2019-07-02 15:10:58 -0400 | [diff] [blame] | 1986 | // We assume the caller wants kPremul. There is no way to indicate a preference. |
| 1987 | sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make( |
| 1988 | this->colorSpaceInfo().colorSpace(), this->colorSpaceInfo().alphaType(), |
| 1989 | dstColorSpace.get(), kPremul_SkAlphaType); |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 1990 | if (xform) { |
| 1991 | sk_sp<GrTextureProxy> texProxy = this->asTextureProxyRef(); |
| 1992 | // TODO: Do something if the input is not a texture already. |
| 1993 | if (!texProxy) { |
| 1994 | callback(context, nullptr, nullptr); |
| 1995 | return; |
| 1996 | } |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 1997 | SkRect srcRectToDraw = SkRect::Make(srcRect); |
| 1998 | rtc = direct->priv().makeDeferredRenderTargetContext( |
Brian Salomon | 27ae52c | 2019-07-03 11:27:44 -0400 | [diff] [blame] | 1999 | SkBackingFit::kApprox, dstW, dstH, this->colorSpaceInfo().colorType(), |
| 2000 | dstColorSpace, 1, GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin); |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 2001 | if (!rtc) { |
| 2002 | callback(context, nullptr, nullptr); |
| 2003 | return; |
| 2004 | } |
| 2005 | rtc->drawTexture(GrNoClip(), std::move(texProxy), GrSamplerState::Filter::kNearest, |
| 2006 | SkBlendMode::kSrc, SK_PMColor4fWHITE, srcRectToDraw, |
| 2007 | SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo, |
| 2008 | GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint, SkMatrix::I(), |
| 2009 | std::move(xform)); |
| 2010 | x = y = 0; |
| 2011 | } |
| 2012 | } |
| 2013 | auto srcProxy = rtc->asTextureProxyRef(); |
| 2014 | // TODO: Do something if the input is not a texture already. |
| 2015 | if (!srcProxy) { |
| 2016 | callback(context, nullptr, nullptr); |
| 2017 | return; |
| 2018 | } |
Brian Salomon | 27ae52c | 2019-07-03 11:27:44 -0400 | [diff] [blame] | 2019 | auto yRTC = direct->priv().makeDeferredRenderTargetContextWithFallback( |
| 2020 | SkBackingFit::kApprox, dstW, dstH, GrColorType::kAlpha_8, dstColorSpace, 1, |
| 2021 | GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin); |
| 2022 | auto uRTC = direct->priv().makeDeferredRenderTargetContextWithFallback( |
| 2023 | SkBackingFit::kApprox, dstW / 2, dstH / 2, GrColorType::kAlpha_8, dstColorSpace, 1, |
| 2024 | GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin); |
| 2025 | auto vRTC = direct->priv().makeDeferredRenderTargetContextWithFallback( |
| 2026 | SkBackingFit::kApprox, dstW / 2, dstH / 2, GrColorType::kAlpha_8, dstColorSpace, 1, |
| 2027 | GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin); |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 2028 | if (!yRTC || !uRTC || !vRTC) { |
| 2029 | callback(context, nullptr, nullptr); |
| 2030 | return; |
| 2031 | } |
| 2032 | |
| 2033 | static constexpr float kRec601M[] { |
| 2034 | 65.481f / 255, 128.553f / 255, 24.966f / 255, 16.f / 255, // y |
| 2035 | -37.797f / 255, -74.203f / 255, 112.0f / 255, 128.f / 255, // u |
| 2036 | 112.f / 255, -93.786f / 255, -18.214f / 255, 128.f / 255, // v |
| 2037 | }; |
| 2038 | static constexpr float kRec709M[] { |
| 2039 | 45.5594f / 255, 156.6288f / 255, 15.8118f / 255, 16.f / 255, // y |
| 2040 | -25.6642f / 255, -86.3358f / 255, 112.f / 255, 128.f / 255, // u |
| 2041 | 112.f / 255, -101.7303f / 255, -10.2697f / 255, 128.f / 255, // v |
| 2042 | }; |
| 2043 | static constexpr float kJpegM[] { |
| 2044 | 0.299f , 0.587f , 0.114f , 0.f / 255, // y |
| 2045 | -0.168736f, -0.331264f, 0.5f , 128.f / 255, // u |
| 2046 | 0.5f , -0.418688f, -0.081312f, 128.f / 255, // v |
| 2047 | }; |
| 2048 | static constexpr float kIM[] { |
| 2049 | 1.f, 0.f, 0.f, 0.f, |
| 2050 | 0.f, 1.f, 0.f, 0.f, |
| 2051 | 0.f, 0.f, 1.f, 0.f, |
| 2052 | }; |
| 2053 | const float* baseM = kIM; |
| 2054 | switch (yuvColorSpace) { |
| 2055 | case kRec601_SkYUVColorSpace: |
| 2056 | baseM = kRec601M; |
| 2057 | break; |
| 2058 | case kRec709_SkYUVColorSpace: |
| 2059 | baseM = kRec709M; |
| 2060 | break; |
| 2061 | case kJPEG_SkYUVColorSpace: |
| 2062 | baseM = kJpegM; |
| 2063 | break; |
| 2064 | case kIdentity_SkYUVColorSpace: |
| 2065 | baseM = kIM; |
| 2066 | break; |
| 2067 | } |
| 2068 | // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost? |
| 2069 | |
| 2070 | auto texMatrix = SkMatrix::MakeTrans(x, y); |
| 2071 | |
Michael Ludwig | 6132820 | 2019-06-19 14:48:58 +0000 | [diff] [blame] | 2072 | SkRect dstRectY = SkRect::MakeWH(dstW, dstH); |
| 2073 | SkRect dstRectUV = SkRect::MakeWH(dstW / 2, dstH / 2); |
| 2074 | |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 2075 | // This matrix generates (r,g,b,a) = (0, 0, 0, y) |
| 2076 | float yM[20]; |
Brian Salomon | df586b7 | 2019-06-11 13:26:37 -0400 | [diff] [blame] | 2077 | std::fill_n(yM, 15, 0.f); |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 2078 | yM[15] = baseM[0]; yM[16] = baseM[1]; yM[17] = baseM[2]; yM[18] = 0; yM[19] = baseM[3]; |
| 2079 | GrPaint yPaint; |
| 2080 | yPaint.addColorTextureProcessor(srcProxy, texMatrix); |
| 2081 | auto yFP = GrColorMatrixFragmentProcessor::Make(yM, false, true, false); |
| 2082 | yPaint.addColorFragmentProcessor(std::move(yFP)); |
| 2083 | yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Michael Ludwig | 6132820 | 2019-06-19 14:48:58 +0000 | [diff] [blame] | 2084 | yRTC->fillRectToRect(GrNoClip(), std::move(yPaint), GrAA::kNo, SkMatrix::I(), |
| 2085 | dstRectY, dstRectY); |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 2086 | auto yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8, |
| 2087 | SkIRect::MakeWH(yRTC->width(), yRTC->height())); |
| 2088 | if (!yTransfer.fTransferBuffer) { |
| 2089 | callback(context, nullptr, nullptr); |
| 2090 | return; |
| 2091 | } |
| 2092 | |
| 2093 | texMatrix.preScale(2.f, 2.f); |
| 2094 | // This matrix generates (r,g,b,a) = (0, 0, 0, u) |
| 2095 | float uM[20]; |
Brian Salomon | df586b7 | 2019-06-11 13:26:37 -0400 | [diff] [blame] | 2096 | std::fill_n(uM, 15, 0.f); |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 2097 | uM[15] = baseM[4]; uM[16] = baseM[5]; uM[17] = baseM[6]; uM[18] = 0; uM[19] = baseM[7]; |
| 2098 | GrPaint uPaint; |
| 2099 | uPaint.addColorTextureProcessor(srcProxy, texMatrix, GrSamplerState::ClampBilerp()); |
| 2100 | auto uFP = GrColorMatrixFragmentProcessor::Make(uM, false, true, false); |
| 2101 | uPaint.addColorFragmentProcessor(std::move(uFP)); |
| 2102 | uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Michael Ludwig | 6132820 | 2019-06-19 14:48:58 +0000 | [diff] [blame] | 2103 | uRTC->fillRectToRect(GrNoClip(), std::move(uPaint), GrAA::kNo, SkMatrix::I(), |
| 2104 | dstRectUV, dstRectUV); |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 2105 | auto uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8, |
| 2106 | SkIRect::MakeWH(uRTC->width(), uRTC->height())); |
| 2107 | if (!uTransfer.fTransferBuffer) { |
| 2108 | callback(context, nullptr, nullptr); |
| 2109 | return; |
| 2110 | } |
| 2111 | |
| 2112 | // This matrix generates (r,g,b,a) = (0, 0, 0, v) |
| 2113 | float vM[20]; |
Brian Salomon | df586b7 | 2019-06-11 13:26:37 -0400 | [diff] [blame] | 2114 | std::fill_n(vM, 15, 0.f); |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 2115 | vM[15] = baseM[8]; vM[16] = baseM[9]; vM[17] = baseM[10]; vM[18] = 0; vM[19] = baseM[11]; |
| 2116 | GrPaint vPaint; |
| 2117 | vPaint.addColorTextureProcessor(srcProxy, texMatrix, GrSamplerState::ClampBilerp()); |
| 2118 | auto vFP = GrColorMatrixFragmentProcessor::Make(vM, false, true, false); |
| 2119 | vPaint.addColorFragmentProcessor(std::move(vFP)); |
| 2120 | vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Michael Ludwig | 6132820 | 2019-06-19 14:48:58 +0000 | [diff] [blame] | 2121 | vRTC->fillRectToRect(GrNoClip(), std::move(vPaint), GrAA::kNo, SkMatrix::I(), |
| 2122 | dstRectUV, dstRectUV); |
Brian Salomon | 024bd00 | 2019-06-11 11:38:16 -0400 | [diff] [blame] | 2123 | auto vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8, |
| 2124 | SkIRect::MakeWH(vRTC->width(), vRTC->height())); |
| 2125 | if (!vTransfer.fTransferBuffer) { |
| 2126 | callback(context, nullptr, nullptr); |
| 2127 | return; |
| 2128 | } |
| 2129 | |
| 2130 | struct FinishContext { |
| 2131 | ReadPixelsCallbackYUV420* fClientCallback; |
| 2132 | ReadPixelsContext fClientContext; |
| 2133 | int fW, fH; |
| 2134 | PixelTransferResult fYTransfer; |
| 2135 | PixelTransferResult fUTransfer; |
| 2136 | PixelTransferResult fVTransfer; |
| 2137 | }; |
| 2138 | // Assumption is that the caller would like to flush. We could take a parameter or require an |
| 2139 | // explicit flush from the caller. We'd have to have a way to defer attaching the finish |
| 2140 | // callback to GrGpu until after the next flush that flushes our op list, though. |
| 2141 | auto* finishContext = new FinishContext{callback, |
| 2142 | context, |
| 2143 | dstW, |
| 2144 | dstH, |
| 2145 | std::move(yTransfer), |
| 2146 | std::move(uTransfer), |
| 2147 | std::move(vTransfer)}; |
| 2148 | auto finishCallback = [](GrGpuFinishedContext c) { |
| 2149 | const auto* context = reinterpret_cast<const FinishContext*>(c); |
| 2150 | const void* y = context->fYTransfer.fTransferBuffer->map(); |
| 2151 | const void* u = context->fUTransfer.fTransferBuffer->map(); |
| 2152 | const void* v = context->fVTransfer.fTransferBuffer->map(); |
| 2153 | if (!y || !u || !v) { |
| 2154 | if (y) { |
| 2155 | context->fYTransfer.fTransferBuffer->unmap(); |
| 2156 | } |
| 2157 | if (u) { |
| 2158 | context->fUTransfer.fTransferBuffer->unmap(); |
| 2159 | } |
| 2160 | if (v) { |
| 2161 | context->fVTransfer.fTransferBuffer->unmap(); |
| 2162 | } |
| 2163 | (*context->fClientCallback)(context->fClientContext, nullptr, 0); |
| 2164 | delete context; |
| 2165 | return; |
| 2166 | } |
| 2167 | size_t w = SkToSizeT(context->fW); |
| 2168 | size_t h = SkToSizeT(context->fH); |
| 2169 | std::unique_ptr<uint8_t[]> yTemp; |
| 2170 | if (context->fYTransfer.fPixelConverter) { |
| 2171 | yTemp.reset(new uint8_t[w * h]); |
| 2172 | context->fYTransfer.fPixelConverter(yTemp.get(), y); |
| 2173 | y = yTemp.get(); |
| 2174 | } |
| 2175 | std::unique_ptr<uint8_t[]> uTemp; |
| 2176 | if (context->fUTransfer.fPixelConverter) { |
| 2177 | uTemp.reset(new uint8_t[w / 2 * h / 2]); |
| 2178 | context->fUTransfer.fPixelConverter(uTemp.get(), u); |
| 2179 | u = uTemp.get(); |
| 2180 | } |
| 2181 | std::unique_ptr<uint8_t[]> vTemp; |
| 2182 | if (context->fVTransfer.fPixelConverter) { |
| 2183 | vTemp.reset(new uint8_t[w / 2 * h / 2]); |
| 2184 | context->fVTransfer.fPixelConverter(vTemp.get(), v); |
| 2185 | v = vTemp.get(); |
| 2186 | } |
| 2187 | const void* data[] = {y, u, v}; |
| 2188 | size_t rowBytes[] = {w, w / 2, w / 2}; |
| 2189 | (*context->fClientCallback)(context->fClientContext, data, rowBytes); |
| 2190 | context->fYTransfer.fTransferBuffer->unmap(); |
| 2191 | context->fUTransfer.fTransferBuffer->unmap(); |
| 2192 | context->fVTransfer.fTransferBuffer->unmap(); |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 2193 | delete context; |
| 2194 | }; |
| 2195 | GrFlushInfo flushInfo; |
| 2196 | flushInfo.fFinishedContext = finishContext; |
| 2197 | flushInfo.fFinishedProc = finishCallback; |
| 2198 | this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo); |
Brian Salomon | ab32f65 | 2019-05-10 14:24:50 -0400 | [diff] [blame] | 2199 | } |
| 2200 | |
Greg Daniel | e6bfb7d | 2019-04-17 15:26:11 -0400 | [diff] [blame] | 2201 | GrSemaphoresSubmitted GrRenderTargetContext::flush(SkSurface::BackendSurfaceAccess access, |
| 2202 | const GrFlushInfo& info) { |
robertphillips | 8c523e0 | 2016-07-26 07:41:00 -0700 | [diff] [blame] | 2203 | ASSERT_SINGLE_OWNER |
Robert Phillips | 6a6de56 | 2019-02-15 15:19:15 -0500 | [diff] [blame] | 2204 | if (fContext->priv().abandoned()) { |
Robert Phillips | a9162df | 2019-02-11 14:12:03 -0500 | [diff] [blame] | 2205 | return GrSemaphoresSubmitted::kNo; |
| 2206 | } |
robertphillips | 8c523e0 | 2016-07-26 07:41:00 -0700 | [diff] [blame] | 2207 | SkDEBUGCODE(this->validate();) |
Robert Phillips | 15c9142 | 2019-05-07 16:54:48 -0400 | [diff] [blame] | 2208 | GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext); |
robertphillips | 8c523e0 | 2016-07-26 07:41:00 -0700 | [diff] [blame] | 2209 | |
Greg Daniel | e6bfb7d | 2019-04-17 15:26:11 -0400 | [diff] [blame] | 2210 | return this->drawingManager()->flushSurface(fRenderTargetProxy.get(), access, info); |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 2211 | } |
| 2212 | |
Greg Daniel | c64ee46 | 2017-06-15 16:59:49 -0400 | [diff] [blame] | 2213 | bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores, |
Robert Phillips | bc4994a | 2019-02-14 08:36:56 -0500 | [diff] [blame] | 2214 | const GrBackendSemaphore waitSemaphores[]) { |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 2215 | ASSERT_SINGLE_OWNER |
Greg Daniel | c64ee46 | 2017-06-15 16:59:49 -0400 | [diff] [blame] | 2216 | RETURN_FALSE_IF_ABANDONED |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 2217 | SkDEBUGCODE(this->validate();) |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 2218 | GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext); |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 2219 | |
| 2220 | AutoCheckFlush acf(this->drawingManager()); |
| 2221 | |
Brian Salomon | 9ff5acb | 2019-05-08 09:04:47 -0400 | [diff] [blame] | 2222 | if (numSemaphores && !this->caps()->semaphoreSupport()) { |
Greg Daniel | c64ee46 | 2017-06-15 16:59:49 -0400 | [diff] [blame] | 2223 | return false; |
| 2224 | } |
| 2225 | |
Robert Phillips | bc4994a | 2019-02-14 08:36:56 -0500 | [diff] [blame] | 2226 | auto direct = fContext->priv().asDirectContext(); |
| 2227 | if (!direct) { |
| 2228 | return false; |
| 2229 | } |
| 2230 | |
| 2231 | auto resourceProvider = direct->priv().resourceProvider(); |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 2232 | |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 2233 | for (int i = 0; i < numSemaphores; ++i) { |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 2234 | sk_sp<GrSemaphore> sema = resourceProvider->wrapBackendSemaphore( |
Greg Daniel | 17b7c05 | 2018-01-09 13:55:33 -0500 | [diff] [blame] | 2235 | waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait, |
| 2236 | kAdopt_GrWrapOwnership); |
Robert Phillips | bc4994a | 2019-02-14 08:36:56 -0500 | [diff] [blame] | 2237 | std::unique_ptr<GrOp> waitOp(GrSemaphoreOp::MakeWait(fContext, std::move(sema), |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 2238 | fRenderTargetProxy.get())); |
Greg Daniel | cb32415 | 2019-02-25 11:36:53 -0500 | [diff] [blame] | 2239 | this->getRTOpList()->addWaitOp(std::move(waitOp), *this->caps()); |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 2240 | } |
Greg Daniel | c64ee46 | 2017-06-15 16:59:49 -0400 | [diff] [blame] | 2241 | return true; |
robertphillips | 8c523e0 | 2016-07-26 07:41:00 -0700 | [diff] [blame] | 2242 | } |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 2243 | |
Robert Phillips | 65a88fa | 2017-08-08 08:36:22 -0400 | [diff] [blame] | 2244 | void GrRenderTargetContext::insertEventMarker(const SkString& str) { |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 2245 | std::unique_ptr<GrOp> op(GrDebugMarkerOp::Make(fContext, fRenderTargetProxy.get(), str)); |
Robert Phillips | 65a88fa | 2017-08-08 08:36:22 -0400 | [diff] [blame] | 2246 | this->getRTOpList()->addOp(std::move(op), *this->caps()); |
| 2247 | } |
| 2248 | |
Robert Phillips | be9aff2 | 2019-02-15 11:33:22 -0500 | [diff] [blame] | 2249 | const GrCaps* GrRenderTargetContext::caps() const { |
| 2250 | return fContext->priv().caps(); |
| 2251 | } |
| 2252 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 2253 | void GrRenderTargetContext::drawPath(const GrClip& clip, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 2254 | GrPaint&& paint, |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 2255 | GrAA aa, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 2256 | const SkMatrix& viewMatrix, |
Brian Salomon | 40b77a6 | 2017-12-22 11:25:52 -0500 | [diff] [blame] | 2257 | const SkPath& path, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 2258 | const GrStyle& style) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 2259 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 2260 | RETURN_IF_ABANDONED |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 2261 | SkDEBUGCODE(this->validate();) |
Robert Phillips | 20390c3 | 2018-08-17 11:01:03 -0400 | [diff] [blame] | 2262 | GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext); |
| 2263 | |
Brian Salomon | 40b77a6 | 2017-12-22 11:25:52 -0500 | [diff] [blame] | 2264 | GrShape shape(path, style); |
Robert Phillips | 20390c3 | 2018-08-17 11:01:03 -0400 | [diff] [blame] | 2265 | |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 2266 | this->drawShape(clip, std::move(paint), aa, viewMatrix, shape); |
Robert Phillips | 20390c3 | 2018-08-17 11:01:03 -0400 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | void GrRenderTargetContext::drawShape(const GrClip& clip, |
| 2270 | GrPaint&& paint, |
| 2271 | GrAA aa, |
| 2272 | const SkMatrix& viewMatrix, |
| 2273 | const GrShape& shape) { |
| 2274 | ASSERT_SINGLE_OWNER |
| 2275 | RETURN_IF_ABANDONED |
| 2276 | SkDEBUGCODE(this->validate();) |
| 2277 | GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext); |
| 2278 | |
Brian Salomon | 2fad74a | 2017-12-20 13:28:55 -0500 | [diff] [blame] | 2279 | if (shape.isEmpty()) { |
| 2280 | if (shape.inverseFilled()) { |
| 2281 | this->drawPaint(clip, std::move(paint), viewMatrix); |
| 2282 | } |
| 2283 | return; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 2284 | } |
| 2285 | |
Robert Phillips | 7215283 | 2017-01-25 17:31:35 -0500 | [diff] [blame] | 2286 | AutoCheckFlush acf(this->drawingManager()); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 2287 | |
Brian Salomon | 2fad74a | 2017-12-20 13:28:55 -0500 | [diff] [blame] | 2288 | if (!shape.style().hasPathEffect()) { |
Chris Dalton | 7d6748e | 2019-03-13 00:34:52 -0600 | [diff] [blame] | 2289 | GrAAType aaType = this->chooseAAType(aa); |
Brian Salomon | 2fad74a | 2017-12-20 13:28:55 -0500 | [diff] [blame] | 2290 | SkRRect rrect; |
| 2291 | // We can ignore the starting point and direction since there is no path effect. |
| 2292 | bool inverted; |
| 2293 | if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) { |
| 2294 | if (rrect.isRect()) { |
| 2295 | this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(), |
| 2296 | &shape.style()); |
| 2297 | return; |
| 2298 | } else if (rrect.isOval()) { |
| 2299 | this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style()); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 2300 | return; |
| 2301 | } |
Brian Salomon | 2fad74a | 2017-12-20 13:28:55 -0500 | [diff] [blame] | 2302 | this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style()); |
| 2303 | return; |
Robert Phillips | 73653b4 | 2018-08-22 12:42:42 -0400 | [diff] [blame] | 2304 | } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() && |
| 2305 | viewMatrix.rectStaysRect()) { |
| 2306 | // TODO: the rectStaysRect restriction could be lifted if we were willing to apply |
| 2307 | // the matrix to all the points individually rather than just to the rect |
| 2308 | SkRect rects[2]; |
| 2309 | if (shape.asNestedRects(rects)) { |
| 2310 | // Concave AA paths are expensive - try to avoid them for special cases |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 2311 | std::unique_ptr<GrDrawOp> op = GrStrokeRectOp::MakeNested( |
Robert Phillips | 73653b4 | 2018-08-22 12:42:42 -0400 | [diff] [blame] | 2312 | fContext, std::move(paint), viewMatrix, rects); |
| 2313 | if (op) { |
| 2314 | this->addDrawOp(clip, std::move(op)); |
| 2315 | } |
| 2316 | // Returning here indicates that there is nothing to draw in this case. |
| 2317 | return; |
| 2318 | } |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 2319 | } |
| 2320 | } |
robertphillips | 4bc3181 | 2016-03-01 12:22:49 -0800 | [diff] [blame] | 2321 | |
Brian Salomon | 2fad74a | 2017-12-20 13:28:55 -0500 | [diff] [blame] | 2322 | this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 2323 | } |
| 2324 | |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 2325 | bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip& clip, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 2326 | const GrUserStencilSettings* ss, |
| 2327 | SkRegion::Op op, |
| 2328 | bool invert, |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 2329 | GrAA aa, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 2330 | const SkMatrix& viewMatrix, |
| 2331 | const SkPath& path) { |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 2332 | ASSERT_SINGLE_OWNER_PRIV |
| 2333 | RETURN_FALSE_IF_ABANDONED_PRIV |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 2334 | SkDEBUGCODE(fRenderTargetContext->validate();) |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 2335 | GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath", |
| 2336 | fRenderTargetContext->fContext); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 2337 | |
| 2338 | if (path.isEmpty() && path.isInverseFillType()) { |
Michael Ludwig | aa1b6b3 | 2019-05-29 14:43:13 -0400 | [diff] [blame] | 2339 | GrPaint paint; |
| 2340 | paint.setCoverageSetOpXPFactory(op, invert); |
| 2341 | this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(), |
| 2342 | SkRect::MakeIWH(fRenderTargetContext->width(), |
| 2343 | fRenderTargetContext->height())); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 2344 | return true; |
| 2345 | } |
| 2346 | |
Robert Phillips | 7215283 | 2017-01-25 17:31:35 -0500 | [diff] [blame] | 2347 | AutoCheckFlush acf(fRenderTargetContext->drawingManager()); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 2348 | |
| 2349 | // An Assumption here is that path renderer would use some form of tweaking |
| 2350 | // the src color (either the input alpha or in the frag shader) to implement |
| 2351 | // aa. If we have some future driver-mojo path AA that can do the right |
| 2352 | // thing WRT to the blend then we'll need some query on the PR. |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 2353 | GrAAType aaType = fRenderTargetContext->chooseAAType(aa); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 2354 | bool hasUserStencilSettings = !ss->isUnused(); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 2355 | |
Chris Dalton | db91c6e | 2017-09-08 16:25:08 -0600 | [diff] [blame] | 2356 | SkIRect clipConservativeBounds; |
| 2357 | clip.getConservativeBounds(fRenderTargetContext->width(), fRenderTargetContext->height(), |
| 2358 | &clipConservativeBounds, nullptr); |
| 2359 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 2360 | GrShape shape(path, GrStyle::SimpleFill()); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 2361 | GrPathRenderer::CanDrawPathArgs canDrawArgs; |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 2362 | canDrawArgs.fCaps = fRenderTargetContext->caps(); |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 2363 | canDrawArgs.fProxy = fRenderTargetContext->proxy(); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 2364 | canDrawArgs.fViewMatrix = &viewMatrix; |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 2365 | canDrawArgs.fShape = &shape; |
Chris Dalton | db91c6e | 2017-09-08 16:25:08 -0600 | [diff] [blame] | 2366 | canDrawArgs.fClipConservativeBounds = &clipConservativeBounds; |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 2367 | canDrawArgs.fAAType = aaType; |
Greg Daniel | be7fc46 | 2019-01-03 16:40:42 -0500 | [diff] [blame] | 2368 | SkASSERT(!fRenderTargetContext->wrapsVkSecondaryCB()); |
| 2369 | canDrawArgs.fTargetIsWrappedVkSecondaryCB = false; |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 2370 | canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings; |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 2371 | |
| 2372 | // Don't allow the SW renderer |
Robert Phillips | 7215283 | 2017-01-25 17:31:35 -0500 | [diff] [blame] | 2373 | GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer( |
Brian Salomon | 36aa176 | 2016-12-10 13:24:02 -0500 | [diff] [blame] | 2374 | canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 2375 | if (!pr) { |
| 2376 | return false; |
| 2377 | } |
| 2378 | |
| 2379 | GrPaint paint; |
| 2380 | paint.setCoverageSetOpXPFactory(op, invert); |
| 2381 | |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 2382 | GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(), |
| 2383 | std::move(paint), |
| 2384 | ss, |
| 2385 | fRenderTargetContext, |
| 2386 | &clip, |
| 2387 | &clipConservativeBounds, |
| 2388 | &viewMatrix, |
| 2389 | &shape, |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 2390 | aaType, |
Brian Osman | 34ec374 | 2018-07-03 10:40:57 -0400 | [diff] [blame] | 2391 | fRenderTargetContext->colorSpaceInfo().isLinearlyBlended()}; |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 2392 | pr->drawPath(args); |
| 2393 | return true; |
| 2394 | } |
| 2395 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 2396 | SkBudgeted GrRenderTargetContextPriv::isBudgeted() const { |
robertphillips | 714712b | 2016-08-04 06:20:45 -0700 | [diff] [blame] | 2397 | ASSERT_SINGLE_OWNER_PRIV |
| 2398 | |
Robert Phillips | 6a6de56 | 2019-02-15 15:19:15 -0500 | [diff] [blame] | 2399 | if (fRenderTargetContext->fContext->priv().abandoned()) { |
robertphillips | 714712b | 2016-08-04 06:20:45 -0700 | [diff] [blame] | 2400 | return SkBudgeted::kNo; |
| 2401 | } |
| 2402 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 2403 | SkDEBUGCODE(fRenderTargetContext->validate();) |
robertphillips | 714712b | 2016-08-04 06:20:45 -0700 | [diff] [blame] | 2404 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 2405 | return fRenderTargetContext->fRenderTargetProxy->isBudgeted(); |
robertphillips | 714712b | 2016-08-04 06:20:45 -0700 | [diff] [blame] | 2406 | } |
| 2407 | |
Brian Salomon | 2fad74a | 2017-12-20 13:28:55 -0500 | [diff] [blame] | 2408 | void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip& clip, |
| 2409 | GrPaint&& paint, |
| 2410 | GrAA aa, |
| 2411 | const SkMatrix& viewMatrix, |
| 2412 | const GrShape& originalShape) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 2413 | ASSERT_SINGLE_OWNER |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 2414 | RETURN_IF_ABANDONED |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 2415 | GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext); |
| 2416 | |
Jim Van Verth | f86073a | 2018-10-02 13:05:38 -0400 | [diff] [blame] | 2417 | if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) { |
| 2418 | return; |
| 2419 | } |
| 2420 | |
Chris Dalton | db91c6e | 2017-09-08 16:25:08 -0600 | [diff] [blame] | 2421 | SkIRect clipConservativeBounds; |
| 2422 | clip.getConservativeBounds(this->width(), this->height(), &clipConservativeBounds, nullptr); |
| 2423 | |
Brian Salomon | 2fad74a | 2017-12-20 13:28:55 -0500 | [diff] [blame] | 2424 | GrShape tempShape; |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 2425 | GrAAType aaType = this->chooseAAType(aa); |
Chris Dalton | 09e5689 | 2019-03-13 00:22:01 -0600 | [diff] [blame] | 2426 | |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 2427 | GrPathRenderer::CanDrawPathArgs canDrawArgs; |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 2428 | canDrawArgs.fCaps = this->caps(); |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 2429 | canDrawArgs.fProxy = this->proxy(); |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 2430 | canDrawArgs.fViewMatrix = &viewMatrix; |
Brian Salomon | 2fad74a | 2017-12-20 13:28:55 -0500 | [diff] [blame] | 2431 | canDrawArgs.fShape = &originalShape; |
Chris Dalton | db91c6e | 2017-09-08 16:25:08 -0600 | [diff] [blame] | 2432 | canDrawArgs.fClipConservativeBounds = &clipConservativeBounds; |
Greg Daniel | be7fc46 | 2019-01-03 16:40:42 -0500 | [diff] [blame] | 2433 | canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB(); |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 2434 | canDrawArgs.fHasUserStencilSettings = false; |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 2435 | |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 2436 | GrPathRenderer* pr; |
Brian Salomon | 82125e9 | 2016-12-10 09:35:48 -0500 | [diff] [blame] | 2437 | static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor; |
Brian Salomon | 2fad74a | 2017-12-20 13:28:55 -0500 | [diff] [blame] | 2438 | if (originalShape.isEmpty() && !originalShape.inverseFilled()) { |
Brian Salomon | 1e5d0ca | 2017-12-14 10:50:19 -0500 | [diff] [blame] | 2439 | return; |
| 2440 | } |
| 2441 | |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 2442 | canDrawArgs.fAAType = aaType; |
Brian Salomon | 1e5d0ca | 2017-12-14 10:50:19 -0500 | [diff] [blame] | 2443 | |
| 2444 | // Try a 1st time without applying any of the style to the geometry (and barring sw) |
| 2445 | pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType); |
| 2446 | SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix); |
| 2447 | |
Brian Salomon | 2fad74a | 2017-12-20 13:28:55 -0500 | [diff] [blame] | 2448 | if (!pr && originalShape.style().pathEffect()) { |
Brian Salomon | 1e5d0ca | 2017-12-14 10:50:19 -0500 | [diff] [blame] | 2449 | // It didn't work above, so try again with the path effect applied. |
Brian Salomon | 2fad74a | 2017-12-20 13:28:55 -0500 | [diff] [blame] | 2450 | tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale); |
| 2451 | if (tempShape.isEmpty()) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 2452 | return; |
| 2453 | } |
Brian Salomon | 2fad74a | 2017-12-20 13:28:55 -0500 | [diff] [blame] | 2454 | canDrawArgs.fShape = &tempShape; |
Robert Phillips | 7215283 | 2017-01-25 17:31:35 -0500 | [diff] [blame] | 2455 | pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType); |
Brian Salomon | 1e5d0ca | 2017-12-14 10:50:19 -0500 | [diff] [blame] | 2456 | } |
| 2457 | if (!pr) { |
Brian Salomon | 2fad74a | 2017-12-20 13:28:55 -0500 | [diff] [blame] | 2458 | if (canDrawArgs.fShape->style().applies()) { |
| 2459 | tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, |
| 2460 | styleScale); |
| 2461 | if (tempShape.isEmpty()) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 2462 | return; |
| 2463 | } |
Brian Salomon | 2fad74a | 2017-12-20 13:28:55 -0500 | [diff] [blame] | 2464 | canDrawArgs.fShape = &tempShape; |
Brian Salomon | e7df0bb | 2018-05-07 14:44:57 -0400 | [diff] [blame] | 2465 | // This time, allow SW renderer |
| 2466 | pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType); |
| 2467 | } else { |
| 2468 | pr = this->drawingManager()->getSoftwarePathRenderer(); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 2469 | } |
Brian Salomon | 1e5d0ca | 2017-12-14 10:50:19 -0500 | [diff] [blame] | 2470 | } |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 2471 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 2472 | if (!pr) { |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 2473 | #ifdef SK_DEBUG |
| 2474 | SkDebugf("Unable to find path renderer compatible with path.\n"); |
| 2475 | #endif |
| 2476 | return; |
| 2477 | } |
| 2478 | |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 2479 | GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(), |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 2480 | std::move(paint), |
| 2481 | &GrUserStencilSettings::kUnused, |
| 2482 | this, |
| 2483 | &clip, |
Chris Dalton | db91c6e | 2017-09-08 16:25:08 -0600 | [diff] [blame] | 2484 | &clipConservativeBounds, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 2485 | &viewMatrix, |
Brian Salomon | 2fad74a | 2017-12-20 13:28:55 -0500 | [diff] [blame] | 2486 | canDrawArgs.fShape, |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 2487 | aaType, |
Brian Osman | 34ec374 | 2018-07-03 10:40:57 -0400 | [diff] [blame] | 2488 | this->colorSpaceInfo().isLinearlyBlended()}; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 2489 | pr->drawPath(args); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 2490 | } |
| 2491 | |
Brian Salomon | 467921e | 2017-03-06 16:17:12 -0500 | [diff] [blame] | 2492 | static void op_bounds(SkRect* bounds, const GrOp* op) { |
| 2493 | *bounds = op->bounds(); |
| 2494 | if (op->hasZeroArea()) { |
| 2495 | if (op->hasAABloat()) { |
| 2496 | bounds->outset(0.5f, 0.5f); |
| 2497 | } else { |
| 2498 | // We don't know which way the particular GPU will snap lines or points at integer |
| 2499 | // coords. So we ensure that the bounds is large enough for either snap. |
| 2500 | SkRect before = *bounds; |
| 2501 | bounds->roundOut(bounds); |
| 2502 | if (bounds->fLeft == before.fLeft) { |
| 2503 | bounds->fLeft -= 1; |
| 2504 | } |
| 2505 | if (bounds->fTop == before.fTop) { |
| 2506 | bounds->fTop -= 1; |
| 2507 | } |
| 2508 | if (bounds->fRight == before.fRight) { |
| 2509 | bounds->fRight += 1; |
| 2510 | } |
| 2511 | if (bounds->fBottom == before.fBottom) { |
| 2512 | bounds->fBottom += 1; |
| 2513 | } |
| 2514 | } |
| 2515 | } |
| 2516 | } |
| 2517 | |
Brian Salomon | 348a037 | 2018-10-31 10:42:18 -0400 | [diff] [blame] | 2518 | void GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op, |
| 2519 | const std::function<WillAddOpFn>& willAddFn) { |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 2520 | ASSERT_SINGLE_OWNER |
Robert Phillips | 6989370 | 2019-02-22 11:16:30 -0500 | [diff] [blame] | 2521 | if (fContext->priv().abandoned()) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 2522 | fContext->priv().opMemoryPool()->release(std::move(op)); |
Brian Salomon | 348a037 | 2018-10-31 10:42:18 -0400 | [diff] [blame] | 2523 | return; |
Robert Phillips | c013892 | 2017-03-08 11:50:55 -0500 | [diff] [blame] | 2524 | } |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 2525 | SkDEBUGCODE(this->validate();) |
Ethan Nicholas | 029b22c | 2018-10-18 16:49:56 -0400 | [diff] [blame] | 2526 | SkDEBUGCODE(op->fAddDrawOpCalled = true;) |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 2527 | GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext); |
robertphillips | 2d70dcb | 2015-10-06 07:38:23 -0700 | [diff] [blame] | 2528 | |
Brian Salomon | 467921e | 2017-03-06 16:17:12 -0500 | [diff] [blame] | 2529 | // Setup clip |
| 2530 | SkRect bounds; |
| 2531 | op_bounds(&bounds, op.get()); |
Brian Salomon | 97180af | 2017-03-14 13:42:58 -0400 | [diff] [blame] | 2532 | GrAppliedClip appliedClip; |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 2533 | GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags(); |
Chris Dalton | 6b98280 | 2019-06-27 13:53:46 -0600 | [diff] [blame] | 2534 | bool usesHWAA = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA; |
| 2535 | bool usesStencil = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil; |
| 2536 | |
| 2537 | if (usesStencil) { |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 2538 | this->setNeedsStencil(usesHWAA); |
Chris Dalton | 6b98280 | 2019-06-27 13:53:46 -0600 | [diff] [blame] | 2539 | } |
| 2540 | |
| 2541 | if (!clip.apply(fContext, this, usesHWAA, usesStencil, &appliedClip, &bounds)) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 2542 | fContext->priv().opMemoryPool()->release(std::move(op)); |
Brian Salomon | 348a037 | 2018-10-31 10:42:18 -0400 | [diff] [blame] | 2543 | return; |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 2544 | } |
| 2545 | |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 2546 | SkASSERT((!usesStencil && !appliedClip.hasStencilClip()) || (fNumStencilSamples > 0)); |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 2547 | |
Brian Salomon | bd3d8d3 | 2019-07-02 09:16:28 -0400 | [diff] [blame] | 2548 | GrClampType clampType = GrColorTypeClampType(this->colorSpaceInfo().colorType()); |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 2549 | // MIXED SAMPLES TODO: If we start using mixed samples for clips we will need to check the clip |
| 2550 | // here as well. |
| 2551 | bool hasMixedSampledCoverage = (usesHWAA && this->numSamples() <= 1); |
| 2552 | #ifdef SK_DEBUG |
| 2553 | if (hasMixedSampledCoverage) { |
| 2554 | SkASSERT(usesStencil); |
| 2555 | SkASSERT(fRenderTargetProxy->canUseMixedSamples(*this->caps())); |
| 2556 | } |
| 2557 | #endif |
Brian Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame] | 2558 | GrProcessorSet::Analysis analysis = op->finalize( |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 2559 | *this->caps(), &appliedClip, hasMixedSampledCoverage, clampType); |
| 2560 | |
| 2561 | GrXferProcessor::DstProxy dstProxy; |
Chris Dalton | 945ee65 | 2019-01-23 09:10:36 -0700 | [diff] [blame] | 2562 | if (analysis.requiresDstTexture()) { |
Brian Salomon | 09181ef | 2018-11-14 13:39:51 -0500 | [diff] [blame] | 2563 | if (!this->setupDstProxy(this->asRenderTargetProxy(), clip, *op, &dstProxy)) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 2564 | fContext->priv().opMemoryPool()->release(std::move(op)); |
Brian Salomon | 348a037 | 2018-10-31 10:42:18 -0400 | [diff] [blame] | 2565 | return; |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 2566 | } |
| 2567 | } |
| 2568 | |
| 2569 | op->setClippedBounds(bounds); |
Brian Salomon | 348a037 | 2018-10-31 10:42:18 -0400 | [diff] [blame] | 2570 | auto opList = this->getRTOpList(); |
| 2571 | if (willAddFn) { |
| 2572 | willAddFn(op.get(), opList->uniqueID()); |
| 2573 | } |
Chris Dalton | 945ee65 | 2019-01-23 09:10:36 -0700 | [diff] [blame] | 2574 | opList->addDrawOp(std::move(op), analysis, std::move(appliedClip), dstProxy, *this->caps()); |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 2575 | } |
| 2576 | |
Robert Phillips | bb581ce | 2017-05-29 15:05:15 -0400 | [diff] [blame] | 2577 | bool GrRenderTargetContext::setupDstProxy(GrRenderTargetProxy* rtProxy, const GrClip& clip, |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 2578 | const GrOp& op, GrXferProcessor::DstProxy* dstProxy) { |
Greg Daniel | be7fc46 | 2019-01-03 16:40:42 -0500 | [diff] [blame] | 2579 | // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we |
| 2580 | // don't actually have a VkImage to make a copy of. Additionally we don't have the power to |
| 2581 | // start and stop the render pass in order to make the copy. |
| 2582 | if (rtProxy->wrapsVkSecondaryCB()) { |
| 2583 | return false; |
| 2584 | } |
| 2585 | |
Brian Salomon | 467921e | 2017-03-06 16:17:12 -0500 | [diff] [blame] | 2586 | if (this->caps()->textureBarrierSupport()) { |
Robert Phillips | bf25d43 | 2017-04-07 10:08:53 -0400 | [diff] [blame] | 2587 | if (GrTextureProxy* texProxy = rtProxy->asTextureProxy()) { |
Brian Salomon | 467921e | 2017-03-06 16:17:12 -0500 | [diff] [blame] | 2588 | // The render target is a texture, so we can read from it directly in the shader. The XP |
| 2589 | // will be responsible to detect this situation and request a texture barrier. |
Robert Phillips | bb581ce | 2017-05-29 15:05:15 -0400 | [diff] [blame] | 2590 | dstProxy->setProxy(sk_ref_sp(texProxy)); |
| 2591 | dstProxy->setOffset(0, 0); |
Robert Phillips | bf25d43 | 2017-04-07 10:08:53 -0400 | [diff] [blame] | 2592 | return true; |
Brian Salomon | 467921e | 2017-03-06 16:17:12 -0500 | [diff] [blame] | 2593 | } |
| 2594 | } |
| 2595 | |
Robert Phillips | bf25d43 | 2017-04-07 10:08:53 -0400 | [diff] [blame] | 2596 | SkIRect copyRect = SkIRect::MakeWH(rtProxy->width(), rtProxy->height()); |
Brian Salomon | 467921e | 2017-03-06 16:17:12 -0500 | [diff] [blame] | 2597 | |
Eric Karl | 7448088 | 2017-04-03 14:49:05 -0700 | [diff] [blame] | 2598 | SkIRect clippedRect; |
Robert Phillips | bf25d43 | 2017-04-07 10:08:53 -0400 | [diff] [blame] | 2599 | clip.getConservativeBounds(rtProxy->width(), rtProxy->height(), &clippedRect); |
Brian Salomon | 09181ef | 2018-11-14 13:39:51 -0500 | [diff] [blame] | 2600 | SkRect opBounds = op.bounds(); |
| 2601 | // If the op has aa bloating or is a infinitely thin geometry (hairline) outset the bounds by |
| 2602 | // 0.5 pixels. |
| 2603 | if (op.hasAABloat() || op.hasZeroArea()) { |
| 2604 | opBounds.outset(0.5f, 0.5f); |
| 2605 | // An antialiased/hairline draw can sometimes bleed outside of the clips bounds. For |
| 2606 | // performance we may ignore the clip when the draw is entirely inside the clip is float |
| 2607 | // space but will hit pixels just outside the clip when actually rasterizing. |
| 2608 | clippedRect.outset(1, 1); |
| 2609 | clippedRect.intersect(SkIRect::MakeWH(rtProxy->width(), rtProxy->height())); |
| 2610 | } |
| 2611 | SkIRect opIBounds; |
| 2612 | opBounds.roundOut(&opIBounds); |
| 2613 | if (!clippedRect.intersect(opIBounds)) { |
Brian Salomon | 467921e | 2017-03-06 16:17:12 -0500 | [diff] [blame] | 2614 | #ifdef SK_DEBUG |
Robert Phillips | bf25d43 | 2017-04-07 10:08:53 -0400 | [diff] [blame] | 2615 | GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw."); |
Brian Salomon | 467921e | 2017-03-06 16:17:12 -0500 | [diff] [blame] | 2616 | #endif |
Robert Phillips | bf25d43 | 2017-04-07 10:08:53 -0400 | [diff] [blame] | 2617 | return false; |
Brian Salomon | 467921e | 2017-03-06 16:17:12 -0500 | [diff] [blame] | 2618 | } |
| 2619 | |
| 2620 | // MSAA consideration: When there is support for reading MSAA samples in the shader we could |
| 2621 | // have per-sample dst values by making the copy multisampled. |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 2622 | // TODO: We don't really use renderability or GrSurfaceDesc. Remove them from caps function? |
Brian Salomon | 467921e | 2017-03-06 16:17:12 -0500 | [diff] [blame] | 2623 | GrSurfaceDesc desc; |
Eric Karl | 7448088 | 2017-04-03 14:49:05 -0700 | [diff] [blame] | 2624 | bool rectsMustMatch = false; |
| 2625 | bool disallowSubrect = false; |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 2626 | GrRenderable renderable = GrRenderable::kNo; |
| 2627 | if (!this->caps()->initDescForDstCopy(rtProxy, &desc, &renderable, &rectsMustMatch, |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 2628 | &disallowSubrect)) { |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 2629 | renderable = GrRenderable::kYes; |
Robert Phillips | bf25d43 | 2017-04-07 10:08:53 -0400 | [diff] [blame] | 2630 | desc.fConfig = rtProxy->config(); |
Brian Salomon | 467921e | 2017-03-06 16:17:12 -0500 | [diff] [blame] | 2631 | } |
| 2632 | |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 2633 | desc.fIsProtected = rtProxy->isProtected() ? GrProtected::kYes : GrProtected::kNo; |
| 2634 | |
Eric Karl | 7448088 | 2017-04-03 14:49:05 -0700 | [diff] [blame] | 2635 | if (!disallowSubrect) { |
| 2636 | copyRect = clippedRect; |
| 2637 | } |
Brian Salomon | 467921e | 2017-03-06 16:17:12 -0500 | [diff] [blame] | 2638 | |
Robert Phillips | bf25d43 | 2017-04-07 10:08:53 -0400 | [diff] [blame] | 2639 | SkIPoint dstPoint, dstOffset; |
| 2640 | SkBackingFit fit; |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 2641 | GrSurfaceProxy::RectsMustMatch matchRects; |
Eric Karl | 7448088 | 2017-04-03 14:49:05 -0700 | [diff] [blame] | 2642 | if (rectsMustMatch) { |
Robert Phillips | bf25d43 | 2017-04-07 10:08:53 -0400 | [diff] [blame] | 2643 | desc.fWidth = rtProxy->width(); |
| 2644 | desc.fHeight = rtProxy->height(); |
Eric Karl | 7448088 | 2017-04-03 14:49:05 -0700 | [diff] [blame] | 2645 | dstPoint = {copyRect.fLeft, copyRect.fTop}; |
| 2646 | dstOffset = {0, 0}; |
Robert Phillips | bf25d43 | 2017-04-07 10:08:53 -0400 | [diff] [blame] | 2647 | fit = SkBackingFit::kExact; |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 2648 | matchRects = GrSurfaceProxy::RectsMustMatch::kYes; |
Eric Karl | 7448088 | 2017-04-03 14:49:05 -0700 | [diff] [blame] | 2649 | } else { |
| 2650 | desc.fWidth = copyRect.width(); |
| 2651 | desc.fHeight = copyRect.height(); |
| 2652 | dstPoint = {0, 0}; |
| 2653 | dstOffset = {copyRect.fLeft, copyRect.fTop}; |
Robert Phillips | bf25d43 | 2017-04-07 10:08:53 -0400 | [diff] [blame] | 2654 | fit = SkBackingFit::kApprox; |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 2655 | matchRects = GrSurfaceProxy::RectsMustMatch::kNo; |
Eric Karl | 7448088 | 2017-04-03 14:49:05 -0700 | [diff] [blame] | 2656 | } |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 2657 | sk_sp<GrTextureProxy> newProxy = GrSurfaceProxy::Copy(fContext, rtProxy, GrMipMapped::kNo, |
| 2658 | copyRect, fit, SkBudgeted::kYes, |
| 2659 | matchRects); |
| 2660 | SkASSERT(newProxy); |
Robert Phillips | bf25d43 | 2017-04-07 10:08:53 -0400 | [diff] [blame] | 2661 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 2662 | dstProxy->setProxy(std::move(newProxy)); |
Robert Phillips | bb581ce | 2017-05-29 15:05:15 -0400 | [diff] [blame] | 2663 | dstProxy->setOffset(dstOffset); |
Robert Phillips | bf25d43 | 2017-04-07 10:08:53 -0400 | [diff] [blame] | 2664 | return true; |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 2665 | } |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 2666 | |
| 2667 | bool GrRenderTargetContext::blitTexture(GrTextureProxy* src, const SkIRect& srcRect, |
| 2668 | const SkIPoint& dstPoint) { |
| 2669 | SkIRect clippedSrcRect; |
| 2670 | SkIPoint clippedDstPoint; |
| 2671 | if (!GrClipSrcRectAndDstPoint(this->asSurfaceProxy()->isize(), src->isize(), srcRect, dstPoint, |
| 2672 | &clippedSrcRect, &clippedDstPoint)) { |
| 2673 | return false; |
| 2674 | } |
| 2675 | |
| 2676 | GrPaint paint; |
| 2677 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 2678 | auto fp = GrSimpleTextureEffect::Make(sk_ref_sp(src->asTextureProxy()), |
| 2679 | SkMatrix::I()); |
| 2680 | if (!fp) { |
| 2681 | return false; |
| 2682 | } |
| 2683 | paint.addColorFragmentProcessor(std::move(fp)); |
| 2684 | |
| 2685 | this->fillRectToRect( |
| 2686 | GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), |
| 2687 | SkRect::MakeXYWH(clippedDstPoint.fX, clippedDstPoint.fY, clippedSrcRect.width(), |
| 2688 | clippedSrcRect.height()), |
| 2689 | SkRect::Make(clippedSrcRect)); |
| 2690 | return true; |
| 2691 | } |
| 2692 | |