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