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