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