blob: d50778726b2cff8de50699d6ea9ef05cd63c228e [file] [log] [blame]
Chris Dalton5ba36ba2018-05-09 01:08:38 -06001/*
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 Daltond7e22272018-05-23 10:17:17 -060011#include "GrNonAtomicRef.h"
Chris Dalton5ba36ba2018-05-09 01:08:38 -060012#include "ccpr/GrCCAtlas.h"
Chris Daltone1639692018-08-20 14:00:30 -060013#include "ccpr/GrCCFiller.h"
Chris Dalton09a7bb22018-08-31 19:53:15 +080014#include "ccpr/GrCCStroker.h"
Chris Dalton5ba36ba2018-05-09 01:08:38 -060015#include "ccpr/GrCCPathProcessor.h"
16
Chris Dalton351e80c2019-01-06 22:51:00 -070017class GrCCPathCache;
Chris Dalton4da70192018-06-18 09:51:36 -060018class GrCCPathCacheEntry;
19class GrOnFlushResourceProvider;
Chris Dalton09a7bb22018-08-31 19:53:15 +080020class GrShape;
21
22/**
23 * This struct counts values that help us preallocate buffers for rendered path geometry.
24 */
25struct 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 Dalton4da70192018-06-18 09:51:36 -060033
Chris Dalton5ba36ba2018-05-09 01:08:38 -060034/**
Chris Dalton42c21152018-06-13 15:28:19 -060035 * This struct encapsulates the minimum and desired requirements for the GPU resources required by
36 * CCPR in a given flush.
37 */
38struct GrCCPerFlushResourceSpecs {
Chris Dalton09a7bb22018-08-31 19:53:15 +080039 static constexpr int kFillIdx = 0;
40 static constexpr int kStrokeIdx = 1;
41
Chris Dalton4da70192018-06-18 09:51:36 -060042 int fNumCachedPaths = 0;
43
Chris Dalton09a7bb22018-08-31 19:53:15 +080044 int fNumCopiedPaths[2] = {0, 0};
45 GrCCRenderedPathStats fCopyPathStats[2];
Chris Dalton4da70192018-06-18 09:51:36 -060046 GrCCAtlas::Specs fCopyAtlasSpecs;
47
Chris Dalton09a7bb22018-08-31 19:53:15 +080048 int fNumRenderedPaths[2] = {0, 0};
Chris Dalton42c21152018-06-13 15:28:19 -060049 int fNumClipPaths = 0;
Chris Dalton09a7bb22018-08-31 19:53:15 +080050 GrCCRenderedPathStats fRenderedPathStats[2];
Chris Dalton4da70192018-06-18 09:51:36 -060051 GrCCAtlas::Specs fRenderedAtlasSpecs;
Chris Dalton42c21152018-06-13 15:28:19 -060052
Chris Dalton4da70192018-06-18 09:51:36 -060053 bool isEmpty() const {
Chris Dalton09a7bb22018-08-31 19:53:15 +080054 return 0 == fNumCachedPaths + fNumCopiedPaths[kFillIdx] + fNumCopiedPaths[kStrokeIdx] +
55 fNumRenderedPaths[kFillIdx] + fNumRenderedPaths[kStrokeIdx] + fNumClipPaths;
Chris Dalton4da70192018-06-18 09:51:36 -060056 }
Chris Dalton351e80c2019-01-06 22:51:00 -070057 // Converts the copies to normal cached draws.
58 void cancelCopies();
Chris Dalton42c21152018-06-13 15:28:19 -060059};
60
61/**
Chris Daltond7e22272018-05-23 10:17:17 -060062 * 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 Dalton5ba36ba2018-05-09 01:08:38 -060065 */
Chris Daltond7e22272018-05-23 10:17:17 -060066class GrCCPerFlushResources : public GrNonAtomicRef<GrCCPerFlushResources> {
Chris Dalton5ba36ba2018-05-09 01:08:38 -060067public:
Chris Dalton42c21152018-06-13 15:28:19 -060068 GrCCPerFlushResources(GrOnFlushResourceProvider*, const GrCCPerFlushResourceSpecs&);
Chris Dalton5ba36ba2018-05-09 01:08:38 -060069
70 bool isMapped() const { return SkToBool(fPathInstanceData); }
71
Chris Dalton351e80c2019-01-06 22:51:00 -070072 // 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 Dalton4da70192018-06-18 09:51:36 -060076
Chris Dalton09a7bb22018-08-31 19:53:15 +080077 // These two methods render a path into a temporary coverage count atlas. See
Chris Dalton351e80c2019-01-06 22:51:00 -070078 // GrCCPathProcessor::Instance for a description of the outputs.
Chris Dalton09a7bb22018-08-31 19:53:15 +080079 //
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 Dalton351e80c2019-01-06 22:51:00 -070082 GrCCAtlas* renderShapeInAtlas(const SkIRect& clipIBounds, const SkMatrix&, const GrShape&,
83 float strokeDevWidth, SkRect* devBounds, SkRect* devBounds45,
84 SkIRect* devIBounds, SkIVector* devToAtlasOffset);
Chris Dalton9414c962018-06-14 10:14:50 -060085 const GrCCAtlas* renderDeviceSpacePathInAtlas(const SkIRect& clipIBounds, const SkPath& devPath,
86 const SkIRect& devPathIBounds,
87 SkIVector* devToAtlasOffset);
Chris Dalton5ba36ba2018-05-09 01:08:38 -060088
Chris Dalton9414c962018-06-14 10:14:50 -060089 // Returns the index in instanceBuffer() of the next instance that will be added by
90 // appendDrawPathInstance().
Chris Daltondaef06a2018-05-23 17:11:09 -060091 int nextPathInstanceIdx() const { return fNextPathInstanceIdx; }
Chris Dalton5ba36ba2018-05-09 01:08:38 -060092
Chris Dalton9414c962018-06-14 10:14:50 -060093 // 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 Dalton5ba36ba2018-05-09 01:08:38 -0600101
Chris Dalton351e80c2019-01-06 22:51:00 -0700102 // Finishes off the GPU buffers and renders the atlas(es).
103 bool finalize(GrOnFlushResourceProvider*, SkTArray<sk_sp<GrRenderTargetContext>>* out);
Chris Dalton9414c962018-06-14 10:14:50 -0600104
105 // Accessors used by draw calls, once the resources have been finalized.
Chris Daltone1639692018-08-20 14:00:30 -0600106 const GrCCFiller& filler() const { SkASSERT(!this->isMapped()); return fFiller; }
Chris Dalton09a7bb22018-08-31 19:53:15 +0800107 const GrCCStroker& stroker() const { SkASSERT(!this->isMapped()); return fStroker; }
Brian Salomondbf70722019-02-07 11:31:24 -0500108 sk_sp<const GrGpuBuffer> refIndexBuffer() const {
Brian Salomon12d22642019-01-29 14:38:50 -0500109 SkASSERT(!this->isMapped());
110 return fIndexBuffer;
111 }
Brian Salomondbf70722019-02-07 11:31:24 -0500112 sk_sp<const GrGpuBuffer> refVertexBuffer() const {
Brian Salomon12d22642019-01-29 14:38:50 -0500113 SkASSERT(!this->isMapped());
114 return fVertexBuffer;
115 }
Brian Salomondbf70722019-02-07 11:31:24 -0500116 sk_sp<const GrGpuBuffer> refInstanceBuffer() const {
Brian Salomon12d22642019-01-29 14:38:50 -0500117 SkASSERT(!this->isMapped());
118 return fInstanceBuffer;
119 }
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600120
121private:
Chris Dalton351e80c2019-01-06 22:51:00 -0700122 void recordCopyPathInstance(const GrCCPathCacheEntry&, const SkIVector& newAtlasOffset,
123 GrCCPathProcessor::DoEvenOddFill, sk_sp<GrTextureProxy> srcProxy);
Chris Daltone1639692018-08-20 14:00:30 -0600124 bool placeRenderedPathInAtlas(const SkIRect& clipIBounds, const SkIRect& pathIBounds,
125 GrScissorTest*, SkIRect* clippedPathIBounds,
126 SkIVector* devToAtlasOffset);
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600127
Chris Daltone1639692018-08-20 14:00:30 -0600128 const SkAutoSTArray<32, SkPoint> fLocalDevPtsBuffer;
129 GrCCFiller fFiller;
Chris Dalton09a7bb22018-08-31 19:53:15 +0800130 GrCCStroker fStroker;
Chris Dalton4da70192018-06-18 09:51:36 -0600131 GrCCAtlasStack fCopyAtlasStack;
132 GrCCAtlasStack fRenderedAtlasStack;
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600133
Brian Salomondbf70722019-02-07 11:31:24 -0500134 const sk_sp<const GrGpuBuffer> fIndexBuffer;
135 const sk_sp<const GrGpuBuffer> fVertexBuffer;
136 const sk_sp<GrGpuBuffer> fInstanceBuffer;
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600137
138 GrCCPathProcessor::Instance* fPathInstanceData = nullptr;
Chris Dalton4da70192018-06-18 09:51:36 -0600139 int fNextCopyInstanceIdx;
140 SkDEBUGCODE(int fEndCopyInstance);
141 int fNextPathInstanceIdx;
Chris Dalton9414c962018-06-14 10:14:50 -0600142 SkDEBUGCODE(int fEndPathInstance);
Chris Dalton351e80c2019-01-06 22:51:00 -0700143
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
164public:
165 const GrTexture* testingOnly_frontCopyAtlasTexture() const;
166 const GrTexture* testingOnly_frontRenderedAtlasTexture() const;
Chris Dalton5ba36ba2018-05-09 01:08:38 -0600167};
168
Chris Dalton09a7bb22018-08-31 19:53:15 +0800169inline 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 Dalton5ba36ba2018-05-09 01:08:38 -0600176#endif