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 | |
| 8 | #ifndef GrCCPerFlushResources_DEFINED |
| 9 | #define GrCCPerFlushResources_DEFINED |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/gpu/GrNonAtomicRef.h" |
| 12 | #include "src/gpu/ccpr/GrCCAtlas.h" |
| 13 | #include "src/gpu/ccpr/GrCCFiller.h" |
| 14 | #include "src/gpu/ccpr/GrCCPathProcessor.h" |
| 15 | #include "src/gpu/ccpr/GrCCStroker.h" |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 16 | #include "src/gpu/ccpr/GrStencilAtlasOp.h" |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 17 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 18 | class GrCCPathCache; |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 19 | class GrCCPathCacheEntry; |
Chris Dalton | 8610e9c | 2019-05-09 11:07:10 -0600 | [diff] [blame] | 20 | class GrOctoBounds; |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 21 | class GrOnFlushResourceProvider; |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 22 | class GrShape; |
| 23 | |
| 24 | /** |
| 25 | * This struct counts values that help us preallocate buffers for rendered path geometry. |
| 26 | */ |
| 27 | struct GrCCRenderedPathStats { |
| 28 | int fMaxPointsPerPath = 0; |
| 29 | int fNumTotalSkPoints = 0; |
| 30 | int fNumTotalSkVerbs = 0; |
| 31 | int fNumTotalConicWeights = 0; |
| 32 | |
| 33 | void statPath(const SkPath&); |
| 34 | }; |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 35 | |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 36 | /** |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 37 | * This struct encapsulates the minimum and desired requirements for the GPU resources required by |
| 38 | * CCPR in a given flush. |
| 39 | */ |
| 40 | struct GrCCPerFlushResourceSpecs { |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 41 | static constexpr int kFillIdx = 0; |
| 42 | static constexpr int kStrokeIdx = 1; |
| 43 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 44 | int fNumCachedPaths = 0; |
| 45 | |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 46 | int fNumCopiedPaths[2] = {0, 0}; |
| 47 | GrCCRenderedPathStats fCopyPathStats[2]; |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 48 | GrCCAtlas::Specs fCopyAtlasSpecs; |
| 49 | |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 50 | int fNumRenderedPaths[2] = {0, 0}; |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 51 | int fNumClipPaths = 0; |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 52 | GrCCRenderedPathStats fRenderedPathStats[2]; |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 53 | GrCCAtlas::Specs fRenderedAtlasSpecs; |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 54 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 55 | bool isEmpty() const { |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 56 | return 0 == fNumCachedPaths + fNumCopiedPaths[kFillIdx] + fNumCopiedPaths[kStrokeIdx] + |
| 57 | fNumRenderedPaths[kFillIdx] + fNumRenderedPaths[kStrokeIdx] + fNumClipPaths; |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 58 | } |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 59 | // Converts the copies to normal cached draws. |
| 60 | void cancelCopies(); |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | /** |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 64 | * This class wraps all the GPU resources that CCPR builds at flush time. It is allocated in CCPR's |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame^] | 65 | * preFlush() method, and referenced by all the GrCCPerOpsTaskPaths objects that are being flushed. |
| 66 | * It is deleted in postFlush() once all the flushing GrCCPerOpsTaskPaths objects are deleted. |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 67 | */ |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 68 | class GrCCPerFlushResources : public GrNonAtomicRef<GrCCPerFlushResources> { |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 69 | public: |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 70 | GrCCPerFlushResources( |
| 71 | GrOnFlushResourceProvider*, GrCCAtlas::CoverageType,const GrCCPerFlushResourceSpecs&); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 72 | |
| 73 | bool isMapped() const { return SkToBool(fPathInstanceData); } |
| 74 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 75 | GrCCAtlas::CoverageType renderedPathCoverageType() const { |
| 76 | return fRenderedAtlasStack.coverageType(); |
| 77 | } |
| 78 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 79 | // Copies a coverage-counted path out of the given texture proxy, and into a cached, 8-bit, |
| 80 | // literal coverage atlas. Updates the cache entry to reference the new atlas. |
| 81 | void upgradeEntryToLiteralCoverageAtlas(GrCCPathCache*, GrOnFlushResourceProvider*, |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 82 | GrCCPathCacheEntry*, GrFillRule); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 83 | |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 84 | // These two methods render a path into a temporary coverage count atlas. See |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 85 | // GrCCPathProcessor::Instance for a description of the outputs. |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 86 | // |
| 87 | // strokeDevWidth must be 0 for fills, 1 for hairlines, or the stroke width in device-space |
| 88 | // pixels for non-hairline strokes (implicitly requiring a rigid-body transform). |
Chris Dalton | 8610e9c | 2019-05-09 11:07:10 -0600 | [diff] [blame] | 89 | GrCCAtlas* renderShapeInAtlas( |
| 90 | const SkIRect& clipIBounds, const SkMatrix&, const GrShape&, float strokeDevWidth, |
| 91 | GrOctoBounds*, SkIRect* devIBounds, SkIVector* devToAtlasOffset); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 92 | const GrCCAtlas* renderDeviceSpacePathInAtlas( |
| 93 | const SkIRect& clipIBounds, const SkPath& devPath, const SkIRect& devPathIBounds, |
| 94 | GrFillRule fillRule, SkIVector* devToAtlasOffset); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 95 | |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 96 | // Returns the index in instanceBuffer() of the next instance that will be added by |
| 97 | // appendDrawPathInstance(). |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 98 | int nextPathInstanceIdx() const { return fNextPathInstanceIdx; } |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 99 | |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 100 | // Appends an instance to instanceBuffer() that will draw a path to the destination render |
| 101 | // target. The caller is responsible to call set() on the returned instance, to keep track of |
| 102 | // its atlas and index (see nextPathInstanceIdx()), and to issue the actual draw call. |
| 103 | GrCCPathProcessor::Instance& appendDrawPathInstance() { |
| 104 | SkASSERT(this->isMapped()); |
| 105 | SkASSERT(fNextPathInstanceIdx < fEndPathInstance); |
| 106 | return fPathInstanceData[fNextPathInstanceIdx++]; |
| 107 | } |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 108 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 109 | // Finishes off the GPU buffers and renders the atlas(es). |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 110 | bool finalize(GrOnFlushResourceProvider*, |
| 111 | SkTArray<std::unique_ptr<GrRenderTargetContext>>* out); |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 112 | |
| 113 | // Accessors used by draw calls, once the resources have been finalized. |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 114 | const GrCCFiller& filler() const { SkASSERT(!this->isMapped()); return fFiller; } |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 115 | const GrCCStroker& stroker() const { SkASSERT(!this->isMapped()); return fStroker; } |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 116 | sk_sp<const GrGpuBuffer> refIndexBuffer() const { |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 117 | SkASSERT(!this->isMapped()); |
| 118 | return fIndexBuffer; |
| 119 | } |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 120 | sk_sp<const GrGpuBuffer> refVertexBuffer() const { |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 121 | SkASSERT(!this->isMapped()); |
| 122 | return fVertexBuffer; |
| 123 | } |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 124 | sk_sp<const GrGpuBuffer> refInstanceBuffer() const { |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 125 | SkASSERT(!this->isMapped()); |
| 126 | return fInstanceBuffer; |
| 127 | } |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 128 | sk_sp<const GrGpuBuffer> refStencilResolveBuffer() const { |
| 129 | SkASSERT(!this->isMapped()); |
| 130 | return fStencilResolveBuffer; |
| 131 | } |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 132 | |
| 133 | private: |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 134 | void recordCopyPathInstance(const GrCCPathCacheEntry&, const SkIVector& newAtlasOffset, |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 135 | GrFillRule, sk_sp<GrTextureProxy> srcProxy); |
| 136 | void placeRenderedPathInAtlas( |
| 137 | const SkIRect& clippedPathIBounds, GrScissorTest, SkIVector* devToAtlasOffset); |
| 138 | |
| 139 | // In MSAA mode we record an additional instance per path that draws a rectangle on top of its |
| 140 | // corresponding path in the atlas and resolves stencil winding values to coverage. |
| 141 | void recordStencilResolveInstance( |
| 142 | const SkIRect& clippedPathIBounds, const SkIVector& devToAtlasOffset, GrFillRule); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 143 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 144 | const SkAutoSTArray<32, SkPoint> fLocalDevPtsBuffer; |
| 145 | GrCCFiller fFiller; |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 146 | GrCCStroker fStroker; |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 147 | GrCCAtlasStack fCopyAtlasStack; |
| 148 | GrCCAtlasStack fRenderedAtlasStack; |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 149 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 150 | const sk_sp<const GrGpuBuffer> fIndexBuffer; |
| 151 | const sk_sp<const GrGpuBuffer> fVertexBuffer; |
| 152 | const sk_sp<GrGpuBuffer> fInstanceBuffer; |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 153 | |
| 154 | GrCCPathProcessor::Instance* fPathInstanceData = nullptr; |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 155 | int fNextCopyInstanceIdx; |
| 156 | SkDEBUGCODE(int fEndCopyInstance); |
| 157 | int fNextPathInstanceIdx; |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 158 | int fBasePathInstanceIdx; |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 159 | SkDEBUGCODE(int fEndPathInstance); |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 160 | |
| 161 | // Represents a range of copy-path instances that all share the same source proxy. (i.e. Draw |
| 162 | // instances that copy a path mask from a 16-bit coverage count atlas into an 8-bit literal |
| 163 | // coverage atlas.) |
| 164 | struct CopyPathRange { |
| 165 | CopyPathRange() = default; |
| 166 | CopyPathRange(sk_sp<GrTextureProxy> srcProxy, int count) |
| 167 | : fSrcProxy(std::move(srcProxy)), fCount(count) {} |
| 168 | sk_sp<GrTextureProxy> fSrcProxy; |
| 169 | int fCount; |
| 170 | }; |
| 171 | |
| 172 | SkSTArray<4, CopyPathRange> fCopyPathRanges; |
| 173 | int fCurrCopyAtlasRangesIdx = 0; |
| 174 | |
| 175 | // This is a list of coverage count atlas textures that have been invalidated due to us copying |
| 176 | // their paths into new 8-bit literal coverage atlases. Since copying is finished by the time |
| 177 | // we begin rendering new atlases, we can recycle these textures for the rendered atlases rather |
| 178 | // than allocating new texture objects upon instantiation. |
| 179 | SkSTArray<2, sk_sp<GrTexture>> fRecyclableAtlasTextures; |
| 180 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 181 | // Used in MSAA mode make an intermediate draw that resolves stencil winding values to coverage. |
| 182 | sk_sp<GrGpuBuffer> fStencilResolveBuffer; |
| 183 | GrStencilAtlasOp::ResolveRectInstance* fStencilResolveInstanceData = nullptr; |
| 184 | int fNextStencilResolveInstanceIdx = 0; |
| 185 | SkDEBUGCODE(int fEndStencilResolveInstance); |
| 186 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 187 | public: |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 188 | #ifdef SK_DEBUG |
| 189 | void debugOnly_didReuseRenderedPath() { |
| 190 | if (GrCCAtlas::CoverageType::kA8_Multisample == this->renderedPathCoverageType()) { |
| 191 | --fEndStencilResolveInstance; |
| 192 | } |
| 193 | } |
| 194 | #endif |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 195 | const GrTexture* testingOnly_frontCopyAtlasTexture() const; |
| 196 | const GrTexture* testingOnly_frontRenderedAtlasTexture() const; |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 197 | }; |
| 198 | |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 199 | inline void GrCCRenderedPathStats::statPath(const SkPath& path) { |
| 200 | fMaxPointsPerPath = SkTMax(fMaxPointsPerPath, path.countPoints()); |
| 201 | fNumTotalSkPoints += path.countPoints(); |
| 202 | fNumTotalSkVerbs += path.countVerbs(); |
| 203 | fNumTotalConicWeights += SkPathPriv::ConicWeightCnt(path); |
| 204 | } |
| 205 | |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 206 | #endif |