Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/ccpr/GrCCPerFlushResources.h" |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/private/GrRecordingContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/gpu/GrClip.h" |
| 12 | #include "src/gpu/GrMemoryPool.h" |
| 13 | #include "src/gpu/GrOnFlushResourceProvider.h" |
| 14 | #include "src/gpu/GrRecordingContextPriv.h" |
| 15 | #include "src/gpu/GrRenderTargetContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/gpu/GrSurfaceContextPriv.h" |
| 17 | #include "src/gpu/ccpr/GrCCPathCache.h" |
| 18 | #include "src/gpu/ccpr/GrGSCoverageProcessor.h" |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 19 | #include "src/gpu/ccpr/GrSampleMaskProcessor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "src/gpu/ccpr/GrVSCoverageProcessor.h" |
Michael Ludwig | 663afe5 | 2019-06-03 16:46:19 -0400 | [diff] [blame] | 21 | #include "src/gpu/geometry/GrShape.h" |
Mike Klein | 456c405 | 2019-12-05 15:30:35 -0600 | [diff] [blame] | 22 | #include <algorithm> |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 23 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 24 | using CoverageType = GrCCAtlas::CoverageType; |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 25 | using FillBatchID = GrCCFiller::BatchID; |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 26 | using StrokeBatchID = GrCCStroker::BatchID; |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 27 | using PathInstance = GrCCPathProcessor::Instance; |
| 28 | |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 29 | static constexpr int kFillIdx = GrCCPerFlushResourceSpecs::kFillIdx; |
| 30 | static constexpr int kStrokeIdx = GrCCPerFlushResourceSpecs::kStrokeIdx; |
| 31 | |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 32 | namespace { |
| 33 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 34 | // Base class for an Op that renders a CCPR atlas. |
| 35 | class AtlasOp : public GrDrawOp { |
| 36 | public: |
| 37 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 38 | GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, |
| 39 | bool hasMixedSampledCoverage, GrClampType) override { |
Chris Dalton | 4b62aed | 2019-01-15 11:53:00 -0700 | [diff] [blame] | 40 | return GrProcessorSet::EmptySetAnalysis(); |
Brian Osman | 532b3f9 | 2018-07-11 10:02:07 -0400 | [diff] [blame] | 41 | } |
Michael Ludwig | 28b0c5d | 2019-12-19 14:51:00 -0500 | [diff] [blame] | 42 | CombineResult onCombineIfPossible(GrOp* other, GrRecordingContext::Arenas*, |
| 43 | const GrCaps&) override { |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 44 | // We will only make multiple copy ops if they have different source proxies. |
| 45 | // TODO: make use of texture chaining. |
| 46 | return CombineResult::kCannotCombine; |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 47 | } |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 48 | |
| 49 | protected: |
| 50 | AtlasOp(uint32_t classID, sk_sp<const GrCCPerFlushResources> resources, |
| 51 | const SkISize& drawBounds) |
| 52 | : GrDrawOp(classID) |
| 53 | , fResources(std::move(resources)) { |
| 54 | this->setBounds(SkRect::MakeIWH(drawBounds.width(), drawBounds.height()), |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 55 | GrOp::HasAABloat::kNo, GrOp::IsHairline::kNo); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | const sk_sp<const GrCCPerFlushResources> fResources; |
Robert Phillips | c655c3a | 2020-03-18 13:23:45 -0400 | [diff] [blame] | 59 | |
| 60 | private: |
| 61 | void onPrePrepare(GrRecordingContext*, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 62 | const GrSurfaceProxyView* writeView, |
Robert Phillips | c655c3a | 2020-03-18 13:23:45 -0400 | [diff] [blame] | 63 | GrAppliedClip*, |
| 64 | const GrXferProcessor::DstProxyView&) final {} |
| 65 | void onPrepare(GrOpFlushState*) final {} |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 66 | }; |
| 67 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 68 | // Copies paths from a cached coverage count or msaa atlas into an 8-bit literal-coverage atlas. |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 69 | class CopyAtlasOp : public AtlasOp { |
| 70 | public: |
| 71 | DEFINE_OP_CLASS_ID |
| 72 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 73 | static std::unique_ptr<GrDrawOp> Make( |
| 74 | GrRecordingContext* context, sk_sp<const GrCCPerFlushResources> resources, |
| 75 | sk_sp<GrTextureProxy> copyProxy, int baseInstance, int endInstance, |
| 76 | const SkISize& drawBounds) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 77 | GrOpMemoryPool* pool = context->priv().opMemoryPool(); |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 78 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 79 | return pool->allocate<CopyAtlasOp>(std::move(resources), std::move(copyProxy), baseInstance, |
| 80 | endInstance, drawBounds); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | const char* name() const override { return "CopyAtlasOp (CCPR)"; } |
Chris Dalton | 7eb5c0f | 2019-05-23 15:15:47 -0600 | [diff] [blame] | 84 | |
| 85 | void visitProxies(const VisitProxyFunc& fn) const override { |
| 86 | fn(fSrcProxy.get(), GrMipMapped::kNo); |
| 87 | } |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 88 | |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 89 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 90 | SkASSERT(fSrcProxy); |
Chris Dalton | 39ca973 | 2020-03-10 10:34:17 -0600 | [diff] [blame] | 91 | SkASSERT(fSrcProxy->isInstantiated()); |
Chris Dalton | f91b755 | 2019-04-29 16:21:18 -0600 | [diff] [blame] | 92 | |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 93 | auto coverageMode = GrCCAtlas::CoverageTypeToPathCoverageMode( |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 94 | fResources->renderedPathCoverageType()); |
Greg Daniel | e810d83 | 2020-02-07 17:20:56 -0500 | [diff] [blame] | 95 | GrColorType ct = GrCCAtlas::CoverageTypeToColorType(fResources->renderedPathCoverageType()); |
Chris Dalton | 39ca973 | 2020-03-10 10:34:17 -0600 | [diff] [blame] | 96 | GrSwizzle swizzle = flushState->caps().getReadSwizzle(fSrcProxy->backendFormat(), ct); |
| 97 | GrCCPathProcessor pathProc(coverageMode, fSrcProxy->peekTexture(), swizzle, |
Greg Daniel | e810d83 | 2020-02-07 17:20:56 -0500 | [diff] [blame] | 98 | GrCCAtlas::kTextureOrigin); |
Chris Dalton | 46d0f9a | 2019-04-24 19:34:54 -0400 | [diff] [blame] | 99 | |
Greg Daniel | 2c3398d | 2019-06-19 11:58:01 -0400 | [diff] [blame] | 100 | GrPipeline pipeline(GrScissorTest::kDisabled, SkBlendMode::kSrc, |
Brian Salomon | 982f546 | 2020-03-30 12:52:33 -0400 | [diff] [blame] | 101 | flushState->drawOpArgs().writeSwizzle()); |
Chris Dalton | f91b755 | 2019-04-29 16:21:18 -0600 | [diff] [blame] | 102 | |
Chris Dalton | 39ca973 | 2020-03-10 10:34:17 -0600 | [diff] [blame] | 103 | pathProc.drawPaths(flushState, pipeline, *fSrcProxy, *fResources, fBaseInstance, |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 104 | fEndInstance, this->bounds()); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | private: |
| 108 | friend class ::GrOpMemoryPool; // for ctor |
| 109 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 110 | CopyAtlasOp(sk_sp<const GrCCPerFlushResources> resources, sk_sp<GrTextureProxy> srcProxy, |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 111 | int baseInstance, int endInstance, const SkISize& drawBounds) |
| 112 | : AtlasOp(ClassID(), std::move(resources), drawBounds) |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 113 | , fSrcProxy(srcProxy) |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 114 | , fBaseInstance(baseInstance) |
| 115 | , fEndInstance(endInstance) { |
| 116 | } |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 117 | sk_sp<GrTextureProxy> fSrcProxy; |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 118 | const int fBaseInstance; |
| 119 | const int fEndInstance; |
| 120 | }; |
| 121 | |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 122 | // Renders coverage counts to a CCPR atlas using the resources' pre-filled GrCCPathParser. |
Chris Dalton | 2c5e011 | 2019-03-29 13:14:18 -0500 | [diff] [blame] | 123 | template<typename ProcessorType> class RenderAtlasOp : public AtlasOp { |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 124 | public: |
| 125 | DEFINE_OP_CLASS_ID |
| 126 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 127 | static std::unique_ptr<GrDrawOp> Make( |
| 128 | GrRecordingContext* context, sk_sp<const GrCCPerFlushResources> resources, |
| 129 | FillBatchID fillBatchID, StrokeBatchID strokeBatchID, const SkISize& drawBounds) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 130 | GrOpMemoryPool* pool = context->priv().opMemoryPool(); |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 131 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 132 | return pool->allocate<RenderAtlasOp>( |
| 133 | std::move(resources), fillBatchID, strokeBatchID, drawBounds); |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | // GrDrawOp interface. |
| 137 | const char* name() const override { return "RenderAtlasOp (CCPR)"; } |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 138 | |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 139 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
Chris Dalton | 2c5e011 | 2019-03-29 13:14:18 -0500 | [diff] [blame] | 140 | ProcessorType proc; |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 141 | GrPipeline pipeline(GrScissorTest::kEnabled, SkBlendMode::kPlus, |
Brian Salomon | 982f546 | 2020-03-30 12:52:33 -0400 | [diff] [blame] | 142 | flushState->drawOpArgs().writeSwizzle()); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 143 | fResources->filler().drawFills(flushState, &proc, pipeline, fFillBatchID, fDrawBounds); |
Chris Dalton | 2c5e011 | 2019-03-29 13:14:18 -0500 | [diff] [blame] | 144 | fResources->stroker().drawStrokes(flushState, &proc, fStrokeBatchID, fDrawBounds); |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | private: |
| 148 | friend class ::GrOpMemoryPool; // for ctor |
| 149 | |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 150 | RenderAtlasOp(sk_sp<const GrCCPerFlushResources> resources, FillBatchID fillBatchID, |
| 151 | StrokeBatchID strokeBatchID, const SkISize& drawBounds) |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 152 | : AtlasOp(ClassID(), std::move(resources), drawBounds) |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 153 | , fFillBatchID(fillBatchID) |
| 154 | , fStrokeBatchID(strokeBatchID) |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 155 | , fDrawBounds(SkIRect::MakeWH(drawBounds.width(), drawBounds.height())) { |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 156 | } |
| 157 | |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 158 | const FillBatchID fFillBatchID; |
| 159 | const StrokeBatchID fStrokeBatchID; |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 160 | const SkIRect fDrawBounds; |
| 161 | }; |
| 162 | |
| 163 | } |
| 164 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 165 | static int inst_buffer_count(const GrCCPerFlushResourceSpecs& specs) { |
| 166 | return specs.fNumCachedPaths + |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 167 | // Copies get two instances per draw: 1 copy + 1 draw. |
| 168 | (specs.fNumCopiedPaths[kFillIdx] + specs.fNumCopiedPaths[kStrokeIdx]) * 2 + |
| 169 | specs.fNumRenderedPaths[kFillIdx] + specs.fNumRenderedPaths[kStrokeIdx]; |
| 170 | // No clips in instance buffers. |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 171 | } |
| 172 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 173 | GrCCPerFlushResources::GrCCPerFlushResources( |
| 174 | GrOnFlushResourceProvider* onFlushRP, CoverageType coverageType, |
| 175 | const GrCCPerFlushResourceSpecs& specs) |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 176 | // Overallocate by one point so we can call Sk4f::Store at the final SkPoint in the array. |
| 177 | // (See transform_path_pts below.) |
| 178 | // FIXME: instead use built-in instructions to write only the first two lanes of an Sk4f. |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 179 | : fLocalDevPtsBuffer(std::max(specs.fRenderedPathStats[kFillIdx].fMaxPointsPerPath, |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 180 | specs.fRenderedPathStats[kStrokeIdx].fMaxPointsPerPath) + 1) |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 181 | , fFiller((CoverageType::kFP16_CoverageCount == coverageType) |
| 182 | ? GrCCFiller::Algorithm::kCoverageCount |
| 183 | : GrCCFiller::Algorithm::kStencilWindingCount, |
| 184 | specs.fNumRenderedPaths[kFillIdx] + specs.fNumClipPaths, |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 185 | specs.fRenderedPathStats[kFillIdx].fNumTotalSkPoints, |
| 186 | specs.fRenderedPathStats[kFillIdx].fNumTotalSkVerbs, |
| 187 | specs.fRenderedPathStats[kFillIdx].fNumTotalConicWeights) |
| 188 | , fStroker(specs.fNumRenderedPaths[kStrokeIdx], |
| 189 | specs.fRenderedPathStats[kStrokeIdx].fNumTotalSkPoints, |
| 190 | specs.fRenderedPathStats[kStrokeIdx].fNumTotalSkVerbs) |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 191 | , fCopyAtlasStack(CoverageType::kA8_LiteralCoverage, specs.fCopyAtlasSpecs, |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 192 | onFlushRP->caps()) |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 193 | , fRenderedAtlasStack(coverageType, specs.fRenderedAtlasSpecs, onFlushRP->caps()) |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 194 | , fIndexBuffer(GrCCPathProcessor::FindIndexBuffer(onFlushRP)) |
| 195 | , fVertexBuffer(GrCCPathProcessor::FindVertexBuffer(onFlushRP)) |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 196 | , fNextCopyInstanceIdx(0) |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 197 | , fNextPathInstanceIdx( |
| 198 | specs.fNumCopiedPaths[kFillIdx] + specs.fNumCopiedPaths[kStrokeIdx]) { |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 199 | if (!fIndexBuffer) { |
| 200 | SkDebugf("WARNING: failed to allocate CCPR index buffer. No paths will be drawn.\n"); |
| 201 | return; |
| 202 | } |
| 203 | if (!fVertexBuffer) { |
| 204 | SkDebugf("WARNING: failed to allocate CCPR vertex buffer. No paths will be drawn.\n"); |
| 205 | return; |
| 206 | } |
Chris Dalton | a6adc5c | 2020-03-25 12:41:17 -0600 | [diff] [blame] | 207 | fPathInstanceBuffer.resetAndMapBuffer(onFlushRP, |
| 208 | inst_buffer_count(specs) * sizeof(PathInstance)); |
| 209 | if (!fPathInstanceBuffer.gpuBuffer()) { |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 210 | SkDebugf("WARNING: failed to allocate CCPR instance buffer. No paths will be drawn.\n"); |
| 211 | return; |
| 212 | } |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 213 | |
| 214 | if (CoverageType::kA8_Multisample == coverageType) { |
| 215 | int numRenderedPaths = |
| 216 | specs.fNumRenderedPaths[kFillIdx] + specs.fNumRenderedPaths[kStrokeIdx] + |
| 217 | specs.fNumClipPaths; |
Chris Dalton | a6adc5c | 2020-03-25 12:41:17 -0600 | [diff] [blame] | 218 | fStencilResolveBuffer.resetAndMapBuffer( |
| 219 | onFlushRP, numRenderedPaths * sizeof(GrStencilAtlasOp::ResolveRectInstance)); |
| 220 | if (!fStencilResolveBuffer.gpuBuffer()) { |
| 221 | SkDebugf("WARNING: failed to allocate CCPR stencil resolve buffer. " |
| 222 | "No paths will be drawn.\n"); |
| 223 | return; |
| 224 | } |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 225 | SkDEBUGCODE(fEndStencilResolveInstance = numRenderedPaths); |
| 226 | } |
| 227 | |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 228 | SkDEBUGCODE(fEndCopyInstance = |
| 229 | specs.fNumCopiedPaths[kFillIdx] + specs.fNumCopiedPaths[kStrokeIdx]); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 230 | SkDEBUGCODE(fEndPathInstance = inst_buffer_count(specs)); |
| 231 | } |
| 232 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 233 | void GrCCPerFlushResources::upgradeEntryToLiteralCoverageAtlas( |
| 234 | GrCCPathCache* pathCache, GrOnFlushResourceProvider* onFlushRP, GrCCPathCacheEntry* entry, |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 235 | GrFillRule fillRule) { |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 236 | using ReleaseAtlasResult = GrCCPathCacheEntry::ReleaseAtlasResult; |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 237 | SkASSERT(this->isMapped()); |
| 238 | SkASSERT(fNextCopyInstanceIdx < fEndCopyInstance); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 239 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 240 | const GrCCCachedAtlas* cachedAtlas = entry->cachedAtlas(); |
| 241 | SkASSERT(cachedAtlas); |
| 242 | SkASSERT(cachedAtlas->getOnFlushProxy()); |
| 243 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 244 | if (CoverageType::kA8_LiteralCoverage == cachedAtlas->coverageType()) { |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 245 | // This entry has already been upgraded to literal coverage. The path must have been drawn |
| 246 | // multiple times during the flush. |
| 247 | SkDEBUGCODE(--fEndCopyInstance); |
| 248 | return; |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 249 | } |
| 250 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 251 | SkIVector newAtlasOffset; |
| 252 | if (GrCCAtlas* retiredAtlas = fCopyAtlasStack.addRect(entry->devIBounds(), &newAtlasOffset)) { |
| 253 | // We did not fit in the previous copy atlas and it was retired. We will render the ranges |
| 254 | // up until fCopyPathRanges.count() into the retired atlas during finalize(). |
| 255 | retiredAtlas->setFillBatchID(fCopyPathRanges.count()); |
| 256 | fCurrCopyAtlasRangesIdx = fCopyPathRanges.count(); |
| 257 | } |
| 258 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 259 | this->recordCopyPathInstance( |
| 260 | *entry, newAtlasOffset, fillRule, sk_ref_sp(cachedAtlas->getOnFlushProxy())); |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 261 | |
| 262 | sk_sp<GrTexture> previousAtlasTexture = |
| 263 | sk_ref_sp(cachedAtlas->getOnFlushProxy()->peekTexture()); |
| 264 | GrCCAtlas* newAtlas = &fCopyAtlasStack.current(); |
| 265 | if (ReleaseAtlasResult::kDidInvalidateFromCache == |
| 266 | entry->upgradeToLiteralCoverageAtlas(pathCache, onFlushRP, newAtlas, newAtlasOffset)) { |
| 267 | // This texture just got booted out of the cache. Keep it around, in case we might be able |
| 268 | // to recycle it for a new atlas. We can recycle it because copying happens before rendering |
| 269 | // new paths, and every path from the atlas that we're planning to use this flush will be |
| 270 | // copied to a new atlas. We'll never copy some and leave others. |
| 271 | fRecyclableAtlasTextures.push_back(std::move(previousAtlasTexture)); |
| 272 | } |
| 273 | } |
| 274 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 275 | void GrCCPerFlushResources::recordCopyPathInstance( |
| 276 | const GrCCPathCacheEntry& entry, const SkIVector& newAtlasOffset, GrFillRule fillRule, |
| 277 | sk_sp<GrTextureProxy> srcProxy) { |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 278 | SkASSERT(fNextCopyInstanceIdx < fEndCopyInstance); |
| 279 | |
| 280 | // Write the instance at the back of the array. |
| 281 | int currentInstanceIdx = fNextCopyInstanceIdx++; |
Brian Osman | c6444d2 | 2019-01-09 16:30:12 -0500 | [diff] [blame] | 282 | constexpr uint64_t kWhite = (((uint64_t) SK_Half1) << 0) | |
| 283 | (((uint64_t) SK_Half1) << 16) | |
| 284 | (((uint64_t) SK_Half1) << 32) | |
| 285 | (((uint64_t) SK_Half1) << 48); |
Chris Dalton | a6adc5c | 2020-03-25 12:41:17 -0600 | [diff] [blame] | 286 | fPathInstanceBuffer[currentInstanceIdx].set(entry, newAtlasOffset, kWhite, fillRule); |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 287 | |
| 288 | // Percolate the instance forward until it's contiguous with other instances that share the same |
| 289 | // proxy. |
| 290 | for (int i = fCopyPathRanges.count() - 1; i >= fCurrCopyAtlasRangesIdx; --i) { |
| 291 | if (fCopyPathRanges[i].fSrcProxy == srcProxy) { |
| 292 | ++fCopyPathRanges[i].fCount; |
| 293 | return; |
| 294 | } |
| 295 | int rangeFirstInstanceIdx = currentInstanceIdx - fCopyPathRanges[i].fCount; |
Chris Dalton | a6adc5c | 2020-03-25 12:41:17 -0600 | [diff] [blame] | 296 | std::swap(fPathInstanceBuffer[rangeFirstInstanceIdx], |
| 297 | fPathInstanceBuffer[currentInstanceIdx]); |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 298 | currentInstanceIdx = rangeFirstInstanceIdx; |
| 299 | } |
| 300 | |
Mike Klein | 456c405 | 2019-12-05 15:30:35 -0600 | [diff] [blame] | 301 | // An instance with this particular proxy did not yet exist in the array. Add a range for it, |
| 302 | // first moving any later ranges back to make space for it at fCurrCopyAtlasRangesIdx. |
| 303 | fCopyPathRanges.push_back(); |
| 304 | std::move_backward(fCopyPathRanges.begin() + fCurrCopyAtlasRangesIdx, |
| 305 | fCopyPathRanges.end() - 1, |
| 306 | fCopyPathRanges.end()); |
| 307 | fCopyPathRanges[fCurrCopyAtlasRangesIdx] = {std::move(srcProxy), 1}; |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 308 | } |
| 309 | |
Chris Dalton | 8610e9c | 2019-05-09 11:07:10 -0600 | [diff] [blame] | 310 | static bool transform_path_pts( |
| 311 | const SkMatrix& m, const SkPath& path, const SkAutoSTArray<32, SkPoint>& outDevPts, |
| 312 | GrOctoBounds* octoBounds) { |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 313 | const SkPoint* pts = SkPathPriv::PointData(path); |
| 314 | int numPts = path.countPoints(); |
| 315 | SkASSERT(numPts + 1 <= outDevPts.count()); |
| 316 | SkASSERT(numPts); |
| 317 | |
| 318 | // m45 transforms path points into "45 degree" device space. A bounding box in this space gives |
| 319 | // the circumscribing octagon's diagonals. We could use SK_ScalarRoot2Over2, but an orthonormal |
| 320 | // transform is not necessary as long as the shader uses the correct inverse. |
| 321 | SkMatrix m45; |
| 322 | m45.setSinCos(1, 1); |
| 323 | m45.preConcat(m); |
| 324 | |
| 325 | // X,Y,T are two parallel view matrices that accumulate two bounding boxes as they map points: |
| 326 | // device-space bounds and "45 degree" device-space bounds (| 1 -1 | * devCoords). |
| 327 | // | 1 1 | |
| 328 | Sk4f X = Sk4f(m.getScaleX(), m.getSkewY(), m45.getScaleX(), m45.getSkewY()); |
| 329 | Sk4f Y = Sk4f(m.getSkewX(), m.getScaleY(), m45.getSkewX(), m45.getScaleY()); |
| 330 | Sk4f T = Sk4f(m.getTranslateX(), m.getTranslateY(), m45.getTranslateX(), m45.getTranslateY()); |
| 331 | |
| 332 | // Map the path's points to device space and accumulate bounding boxes. |
| 333 | Sk4f devPt = SkNx_fma(Y, Sk4f(pts[0].y()), T); |
| 334 | devPt = SkNx_fma(X, Sk4f(pts[0].x()), devPt); |
| 335 | Sk4f topLeft = devPt; |
| 336 | Sk4f bottomRight = devPt; |
| 337 | |
| 338 | // Store all 4 values [dev.x, dev.y, dev45.x, dev45.y]. We are only interested in the first two, |
| 339 | // and will overwrite [dev45.x, dev45.y] with the next point. This is why the dst buffer must |
| 340 | // be at least one larger than the number of points. |
| 341 | devPt.store(&outDevPts[0]); |
| 342 | |
| 343 | for (int i = 1; i < numPts; ++i) { |
| 344 | devPt = SkNx_fma(Y, Sk4f(pts[i].y()), T); |
| 345 | devPt = SkNx_fma(X, Sk4f(pts[i].x()), devPt); |
| 346 | topLeft = Sk4f::Min(topLeft, devPt); |
| 347 | bottomRight = Sk4f::Max(bottomRight, devPt); |
| 348 | devPt.store(&outDevPts[i]); |
| 349 | } |
| 350 | |
Chris Dalton | ce038dc | 2018-09-14 14:14:49 -0600 | [diff] [blame] | 351 | if (!(Sk4f(0) == topLeft*0).allTrue() || !(Sk4f(0) == bottomRight*0).allTrue()) { |
| 352 | // The bounds are infinite or NaN. |
| 353 | return false; |
| 354 | } |
| 355 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 356 | SkPoint topLeftPts[2], bottomRightPts[2]; |
| 357 | topLeft.store(topLeftPts); |
| 358 | bottomRight.store(bottomRightPts); |
Chris Dalton | 8610e9c | 2019-05-09 11:07:10 -0600 | [diff] [blame] | 359 | |
| 360 | const SkRect& devBounds = SkRect::MakeLTRB( |
| 361 | topLeftPts[0].x(), topLeftPts[0].y(), bottomRightPts[0].x(), bottomRightPts[0].y()); |
| 362 | const SkRect& devBounds45 = SkRect::MakeLTRB( |
| 363 | topLeftPts[1].x(), topLeftPts[1].y(), bottomRightPts[1].x(), bottomRightPts[1].y()); |
| 364 | |
| 365 | octoBounds->set(devBounds, devBounds45); |
Chris Dalton | ce038dc | 2018-09-14 14:14:49 -0600 | [diff] [blame] | 366 | return true; |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 367 | } |
| 368 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 369 | GrCCAtlas* GrCCPerFlushResources::renderShapeInAtlas( |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 370 | const SkIRect& clipIBounds, const SkMatrix& m, const GrShape& shape, float strokeDevWidth, |
Chris Dalton | 8610e9c | 2019-05-09 11:07:10 -0600 | [diff] [blame] | 371 | GrOctoBounds* octoBounds, SkIRect* devIBounds, SkIVector* devToAtlasOffset) { |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 372 | SkASSERT(this->isMapped()); |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 373 | SkASSERT(fNextPathInstanceIdx < fEndPathInstance); |
| 374 | |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 375 | SkPath path; |
| 376 | shape.asPath(&path); |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 377 | if (path.isEmpty()) { |
| 378 | SkDEBUGCODE(--fEndPathInstance); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 379 | SkDEBUGCODE(--fEndStencilResolveInstance); |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 380 | return nullptr; |
| 381 | } |
Chris Dalton | 8610e9c | 2019-05-09 11:07:10 -0600 | [diff] [blame] | 382 | if (!transform_path_pts(m, path, fLocalDevPtsBuffer, octoBounds)) { |
Chris Dalton | ce038dc | 2018-09-14 14:14:49 -0600 | [diff] [blame] | 383 | // The transformed path had infinite or NaN bounds. |
| 384 | SkDEBUGCODE(--fEndPathInstance); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 385 | SkDEBUGCODE(--fEndStencilResolveInstance); |
Chris Dalton | ce038dc | 2018-09-14 14:14:49 -0600 | [diff] [blame] | 386 | return nullptr; |
| 387 | } |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 388 | |
| 389 | const SkStrokeRec& stroke = shape.style().strokeRec(); |
| 390 | if (!stroke.isFillStyle()) { |
Chris Dalton | 8610e9c | 2019-05-09 11:07:10 -0600 | [diff] [blame] | 391 | float r = SkStrokeRec::GetInflationRadius( |
| 392 | stroke.getJoin(), stroke.getMiter(), stroke.getCap(), strokeDevWidth); |
| 393 | octoBounds->outset(r); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 394 | } |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 395 | |
Chris Dalton | a6fcb76 | 2019-05-13 08:57:53 -0600 | [diff] [blame] | 396 | GrScissorTest enableScissorInAtlas; |
| 397 | if (clipIBounds.contains(octoBounds->bounds())) { |
| 398 | enableScissorInAtlas = GrScissorTest::kDisabled; |
| 399 | } else if (octoBounds->clip(clipIBounds)) { |
| 400 | enableScissorInAtlas = GrScissorTest::kEnabled; |
| 401 | } else { |
| 402 | // The clip and octo bounds do not intersect. Draw nothing. |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 403 | SkDEBUGCODE(--fEndPathInstance); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 404 | SkDEBUGCODE(--fEndStencilResolveInstance); |
Chris Dalton | a6fcb76 | 2019-05-13 08:57:53 -0600 | [diff] [blame] | 405 | return nullptr; |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 406 | } |
Chris Dalton | a6fcb76 | 2019-05-13 08:57:53 -0600 | [diff] [blame] | 407 | octoBounds->roundOut(devIBounds); |
| 408 | SkASSERT(clipIBounds.contains(*devIBounds)); |
| 409 | |
| 410 | this->placeRenderedPathInAtlas(*devIBounds, enableScissorInAtlas, devToAtlasOffset); |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 411 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 412 | GrFillRule fillRule; |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 413 | if (stroke.isFillStyle()) { |
| 414 | SkASSERT(0 == strokeDevWidth); |
Chris Dalton | a6fcb76 | 2019-05-13 08:57:53 -0600 | [diff] [blame] | 415 | fFiller.parseDeviceSpaceFill(path, fLocalDevPtsBuffer.begin(), enableScissorInAtlas, |
| 416 | *devIBounds, *devToAtlasOffset); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 417 | fillRule = GrFillRuleForSkPath(path); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 418 | } else { |
| 419 | // Stroke-and-fill is not yet supported. |
| 420 | SkASSERT(SkStrokeRec::kStroke_Style == stroke.getStyle() || stroke.isHairlineStyle()); |
| 421 | SkASSERT(!stroke.isHairlineStyle() || 1 == strokeDevWidth); |
Chris Dalton | a6fcb76 | 2019-05-13 08:57:53 -0600 | [diff] [blame] | 422 | fStroker.parseDeviceSpaceStroke( |
| 423 | path, fLocalDevPtsBuffer.begin(), stroke, strokeDevWidth, enableScissorInAtlas, |
| 424 | *devIBounds, *devToAtlasOffset); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 425 | fillRule = GrFillRule::kNonzero; |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 426 | } |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 427 | |
| 428 | if (GrCCAtlas::CoverageType::kA8_Multisample == this->renderedPathCoverageType()) { |
| 429 | this->recordStencilResolveInstance(*devIBounds, *devToAtlasOffset, fillRule); |
| 430 | } |
| 431 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 432 | return &fRenderedAtlasStack.current(); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 433 | } |
| 434 | |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 435 | const GrCCAtlas* GrCCPerFlushResources::renderDeviceSpacePathInAtlas( |
| 436 | const SkIRect& clipIBounds, const SkPath& devPath, const SkIRect& devPathIBounds, |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 437 | GrFillRule fillRule, SkIVector* devToAtlasOffset) { |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 438 | SkASSERT(this->isMapped()); |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 439 | |
| 440 | if (devPath.isEmpty()) { |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 441 | SkDEBUGCODE(--fEndStencilResolveInstance); |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 442 | return nullptr; |
| 443 | } |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 444 | |
Chris Dalton | a6fcb76 | 2019-05-13 08:57:53 -0600 | [diff] [blame] | 445 | GrScissorTest enableScissorInAtlas; |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 446 | SkIRect clippedPathIBounds; |
Chris Dalton | a6fcb76 | 2019-05-13 08:57:53 -0600 | [diff] [blame] | 447 | if (clipIBounds.contains(devPathIBounds)) { |
| 448 | clippedPathIBounds = devPathIBounds; |
| 449 | enableScissorInAtlas = GrScissorTest::kDisabled; |
| 450 | } else if (clippedPathIBounds.intersect(clipIBounds, devPathIBounds)) { |
| 451 | enableScissorInAtlas = GrScissorTest::kEnabled; |
| 452 | } else { |
| 453 | // The clip and path bounds do not intersect. Draw nothing. |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 454 | SkDEBUGCODE(--fEndStencilResolveInstance); |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 455 | return nullptr; |
| 456 | } |
| 457 | |
Chris Dalton | a6fcb76 | 2019-05-13 08:57:53 -0600 | [diff] [blame] | 458 | this->placeRenderedPathInAtlas(clippedPathIBounds, enableScissorInAtlas, devToAtlasOffset); |
| 459 | fFiller.parseDeviceSpaceFill(devPath, SkPathPriv::PointData(devPath), enableScissorInAtlas, |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 460 | clippedPathIBounds, *devToAtlasOffset); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 461 | |
| 462 | // In MSAA mode we also record an internal draw instance that will be used to resolve stencil |
| 463 | // winding values to coverage when the atlas is generated. |
| 464 | if (GrCCAtlas::CoverageType::kA8_Multisample == this->renderedPathCoverageType()) { |
| 465 | this->recordStencilResolveInstance(clippedPathIBounds, *devToAtlasOffset, fillRule); |
| 466 | } |
| 467 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 468 | return &fRenderedAtlasStack.current(); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 469 | } |
| 470 | |
Chris Dalton | a6fcb76 | 2019-05-13 08:57:53 -0600 | [diff] [blame] | 471 | void GrCCPerFlushResources::placeRenderedPathInAtlas( |
| 472 | const SkIRect& clippedPathIBounds, GrScissorTest scissorTest, SkIVector* devToAtlasOffset) { |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 473 | if (GrCCAtlas* retiredAtlas = |
Chris Dalton | a6fcb76 | 2019-05-13 08:57:53 -0600 | [diff] [blame] | 474 | fRenderedAtlasStack.addRect(clippedPathIBounds, devToAtlasOffset)) { |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 475 | // We did not fit in the previous coverage count atlas and it was retired. Close the path |
| 476 | // parser's current batch (which does not yet include the path we just parsed). We will |
| 477 | // render this batch into the retired atlas during finalize(). |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 478 | retiredAtlas->setFillBatchID(fFiller.closeCurrentBatch()); |
| 479 | retiredAtlas->setStrokeBatchID(fStroker.closeCurrentBatch()); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 480 | retiredAtlas->setEndStencilResolveInstance(fNextStencilResolveInstanceIdx); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 481 | } |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 482 | } |
| 483 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 484 | void GrCCPerFlushResources::recordStencilResolveInstance( |
| 485 | const SkIRect& clippedPathIBounds, const SkIVector& devToAtlasOffset, GrFillRule fillRule) { |
| 486 | SkASSERT(GrCCAtlas::CoverageType::kA8_Multisample == this->renderedPathCoverageType()); |
| 487 | SkASSERT(fNextStencilResolveInstanceIdx < fEndStencilResolveInstance); |
| 488 | |
Brian Salomon | 9bd947d | 2019-10-03 14:57:07 -0400 | [diff] [blame] | 489 | SkIRect atlasIBounds = clippedPathIBounds.makeOffset(devToAtlasOffset); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 490 | if (GrFillRule::kEvenOdd == fillRule) { |
| 491 | // Make even/odd fills counterclockwise. The resolve draw uses two-sided stencil, with |
| 492 | // "nonzero" settings in front and "even/odd" settings in back. |
| 493 | std::swap(atlasIBounds.fLeft, atlasIBounds.fRight); |
| 494 | } |
Chris Dalton | a6adc5c | 2020-03-25 12:41:17 -0600 | [diff] [blame] | 495 | fStencilResolveBuffer[fNextStencilResolveInstanceIdx++] = { |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 496 | (int16_t)atlasIBounds.left(), (int16_t)atlasIBounds.top(), |
| 497 | (int16_t)atlasIBounds.right(), (int16_t)atlasIBounds.bottom()}; |
| 498 | } |
| 499 | |
Chris Dalton | c4b4735 | 2019-08-23 10:10:36 -0600 | [diff] [blame] | 500 | bool GrCCPerFlushResources::finalize(GrOnFlushResourceProvider* onFlushRP) { |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 501 | SkASSERT(this->isMapped()); |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 502 | SkASSERT(fNextPathInstanceIdx == fEndPathInstance); |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 503 | SkASSERT(fNextCopyInstanceIdx == fEndCopyInstance); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 504 | SkASSERT(GrCCAtlas::CoverageType::kA8_Multisample != this->renderedPathCoverageType() || |
| 505 | fNextStencilResolveInstanceIdx == fEndStencilResolveInstance); |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 506 | |
Chris Dalton | a6adc5c | 2020-03-25 12:41:17 -0600 | [diff] [blame] | 507 | fPathInstanceBuffer.unmapBuffer(); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 508 | |
Chris Dalton | a6adc5c | 2020-03-25 12:41:17 -0600 | [diff] [blame] | 509 | if (fStencilResolveBuffer.gpuBuffer()) { |
| 510 | fStencilResolveBuffer.unmapBuffer(); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 511 | } |
| 512 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 513 | if (!fCopyAtlasStack.empty()) { |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 514 | fCopyAtlasStack.current().setFillBatchID(fCopyPathRanges.count()); |
| 515 | fCurrCopyAtlasRangesIdx = fCopyPathRanges.count(); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 516 | } |
| 517 | if (!fRenderedAtlasStack.empty()) { |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 518 | fRenderedAtlasStack.current().setFillBatchID(fFiller.closeCurrentBatch()); |
| 519 | fRenderedAtlasStack.current().setStrokeBatchID(fStroker.closeCurrentBatch()); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 520 | fRenderedAtlasStack.current().setEndStencilResolveInstance(fNextStencilResolveInstanceIdx); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 521 | } |
| 522 | |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 523 | // Build the GPU buffers to render path coverage counts. (This must not happen until after the |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 524 | // final calls to fFiller/fStroker.closeCurrentBatch().) |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 525 | if (!fFiller.prepareToDraw(onFlushRP)) { |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 526 | return false; |
| 527 | } |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 528 | if (!fStroker.prepareToDraw(onFlushRP)) { |
| 529 | return false; |
| 530 | } |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 531 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 532 | // Draw the copies from coverage count or msaa atlas(es) into 8-bit cached atlas(es). |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 533 | int copyRangeIdx = 0; |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 534 | int baseCopyInstance = 0; |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame] | 535 | for (GrCCAtlas& atlas : fCopyAtlasStack.atlases()) { |
| 536 | int endCopyRange = atlas.getFillBatchID(); |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 537 | SkASSERT(endCopyRange > copyRangeIdx); |
| 538 | |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame] | 539 | auto rtc = atlas.instantiate(onFlushRP); |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 540 | for (; copyRangeIdx < endCopyRange; ++copyRangeIdx) { |
| 541 | const CopyPathRange& copyRange = fCopyPathRanges[copyRangeIdx]; |
| 542 | int endCopyInstance = baseCopyInstance + copyRange.fCount; |
| 543 | if (rtc) { |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 544 | auto op = CopyAtlasOp::Make( |
| 545 | rtc->surfPriv().getContext(), sk_ref_sp(this), copyRange.fSrcProxy, |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame] | 546 | baseCopyInstance, endCopyInstance, atlas.drawBounds()); |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 547 | rtc->addDrawOp(GrNoClip(), std::move(op)); |
| 548 | } |
| 549 | baseCopyInstance = endCopyInstance; |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 550 | } |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 551 | } |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 552 | SkASSERT(fCopyPathRanges.count() == copyRangeIdx); |
| 553 | SkASSERT(fNextCopyInstanceIdx == baseCopyInstance); |
| 554 | SkASSERT(baseCopyInstance == fEndCopyInstance); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 555 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 556 | // Render the coverage count atlas(es). |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 557 | int baseStencilResolveInstance = 0; |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame] | 558 | for (GrCCAtlas& atlas : fRenderedAtlasStack.atlases()) { |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 559 | // Copies will be finished by the time we get to rendering new atlases. See if we can |
| 560 | // recycle any previous invalidated atlas textures instead of creating new ones. |
Chris Dalton | afde18f | 2018-06-22 12:44:19 -0600 | [diff] [blame] | 561 | sk_sp<GrTexture> backingTexture; |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 562 | for (sk_sp<GrTexture>& texture : fRecyclableAtlasTextures) { |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame] | 563 | if (texture && atlas.currentHeight() == texture->height() && |
| 564 | atlas.currentWidth() == texture->width()) { |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 565 | backingTexture = skstd::exchange(texture, nullptr); |
| 566 | break; |
| 567 | } |
Chris Dalton | afde18f | 2018-06-22 12:44:19 -0600 | [diff] [blame] | 568 | } |
| 569 | |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame] | 570 | if (auto rtc = atlas.instantiate(onFlushRP, std::move(backingTexture))) { |
Chris Dalton | 2c5e011 | 2019-03-29 13:14:18 -0500 | [diff] [blame] | 571 | std::unique_ptr<GrDrawOp> op; |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 572 | if (CoverageType::kA8_Multisample == fRenderedAtlasStack.coverageType()) { |
| 573 | op = GrStencilAtlasOp::Make( |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame] | 574 | rtc->surfPriv().getContext(), sk_ref_sp(this), atlas.getFillBatchID(), |
| 575 | atlas.getStrokeBatchID(), baseStencilResolveInstance, |
| 576 | atlas.getEndStencilResolveInstance(), atlas.drawBounds()); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 577 | } else if (onFlushRP->caps()->shaderCaps()->geometryShaderSupport()) { |
Chris Dalton | 2c5e011 | 2019-03-29 13:14:18 -0500 | [diff] [blame] | 578 | op = RenderAtlasOp<GrGSCoverageProcessor>::Make( |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame] | 579 | rtc->surfPriv().getContext(), sk_ref_sp(this), atlas.getFillBatchID(), |
| 580 | atlas.getStrokeBatchID(), atlas.drawBounds()); |
Chris Dalton | 2c5e011 | 2019-03-29 13:14:18 -0500 | [diff] [blame] | 581 | } else { |
| 582 | op = RenderAtlasOp<GrVSCoverageProcessor>::Make( |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame] | 583 | rtc->surfPriv().getContext(), sk_ref_sp(this), atlas.getFillBatchID(), |
| 584 | atlas.getStrokeBatchID(), atlas.drawBounds()); |
Chris Dalton | 2c5e011 | 2019-03-29 13:14:18 -0500 | [diff] [blame] | 585 | } |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 586 | rtc->addDrawOp(GrNoClip(), std::move(op)); |
Greg Daniel | 46e366a | 2019-12-16 14:38:36 -0500 | [diff] [blame] | 587 | if (rtc->asSurfaceProxy()->requiresManualMSAAResolve()) { |
| 588 | onFlushRP->addTextureResolveTask(sk_ref_sp(rtc->asTextureProxy()), |
Chris Dalton | 4ece96d | 2019-08-30 11:26:39 -0600 | [diff] [blame] | 589 | GrSurfaceProxy::ResolveFlags::kMSAA); |
| 590 | } |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 591 | } |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 592 | |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame] | 593 | SkASSERT(atlas.getEndStencilResolveInstance() >= baseStencilResolveInstance); |
| 594 | baseStencilResolveInstance = atlas.getEndStencilResolveInstance(); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 595 | } |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 596 | SkASSERT(GrCCAtlas::CoverageType::kA8_Multisample != this->renderedPathCoverageType() || |
| 597 | baseStencilResolveInstance == fEndStencilResolveInstance); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 598 | |
| 599 | return true; |
| 600 | } |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 601 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 602 | void GrCCPerFlushResourceSpecs::cancelCopies() { |
| 603 | // Convert copies to cached draws. |
| 604 | fNumCachedPaths += fNumCopiedPaths[kFillIdx] + fNumCopiedPaths[kStrokeIdx]; |
| 605 | fNumCopiedPaths[kFillIdx] = fNumCopiedPaths[kStrokeIdx] = 0; |
| 606 | fCopyPathStats[kFillIdx] = fCopyPathStats[kStrokeIdx] = GrCCRenderedPathStats(); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 607 | fCopyAtlasSpecs = GrCCAtlas::Specs(); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 608 | } |