Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [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 GrCCStroker_DEFINED |
| 9 | #define GrCCStroker_DEFINED |
| 10 | |
Chris Dalton | 39ca973 | 2020-03-10 10:34:17 -0600 | [diff] [blame] | 11 | #include "include/private/GrTypesPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/private/SkNx.h" |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame^] | 13 | #include "src/gpu/GrTAllocator.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/gpu/ccpr/GrCCStrokeGeometry.h" |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 15 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 16 | class GrGpuBuffer; |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 17 | class GrCCCoverageProcessor; |
| 18 | class GrOnFlushResourceProvider; |
| 19 | class GrOpFlushState; |
| 20 | class GrPipeline; |
| 21 | class GrPrimitiveProcessor; |
| 22 | class SkMatrix; |
| 23 | class SkPath; |
| 24 | class SkStrokeRec; |
| 25 | |
| 26 | /** |
| 27 | * This class parses stroked SkPaths into a GPU instance buffer, then issues calls to draw their |
| 28 | * coverage counts. |
| 29 | */ |
| 30 | class GrCCStroker { |
| 31 | public: |
| 32 | GrCCStroker(int numPaths, int numSkPoints, int numSkVerbs) |
| 33 | : fGeometry(numSkPoints, numSkVerbs), fPathInfos(numPaths) {} |
| 34 | |
| 35 | // Parses a device-space SkPath into the current batch, using the SkPath's original verbs with |
| 36 | // 'deviceSpacePts', and the SkStrokeRec's original settings with 'strokeDevWidth'. Accepts an |
| 37 | // optional post-device-space translate for placement in an atlas. |
| 38 | // |
| 39 | // Strokes intended as hairlines must have a strokeDevWidth of 1. Non-hairline strokes can only |
| 40 | // be drawn with rigid body transforms; affine transformation of the stroke lines themselves is |
| 41 | // not yet supported. |
| 42 | void parseDeviceSpaceStroke(const SkPath&, const SkPoint* deviceSpacePts, const SkStrokeRec&, |
| 43 | float strokeDevWidth, GrScissorTest, |
| 44 | const SkIRect& clippedDevIBounds, |
| 45 | const SkIVector& devToAtlasOffset); |
| 46 | |
| 47 | using BatchID = int; |
| 48 | |
| 49 | // Compiles the outstanding parsed paths into a batch, and returns an ID that can be used to |
| 50 | // draw their strokes in the future. |
| 51 | BatchID closeCurrentBatch(); |
| 52 | |
| 53 | // Builds an internal GPU buffer and prepares for calls to drawStrokes(). Caller must close the |
| 54 | // current batch before calling this method, and cannot parse new paths afer. |
| 55 | bool prepareToDraw(GrOnFlushResourceProvider*); |
| 56 | |
| 57 | // Called after prepareToDraw(). Draws the given batch of path strokes. |
Chris Dalton | 2c5e011 | 2019-03-29 13:14:18 -0500 | [diff] [blame] | 58 | void drawStrokes( |
| 59 | GrOpFlushState*, GrCCCoverageProcessor*, BatchID, const SkIRect& drawBounds) const; |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 60 | |
| 61 | private: |
| 62 | static constexpr int kNumScissorModes = 2; |
| 63 | static constexpr BatchID kEmptyBatchID = -1; |
| 64 | using Verb = GrCCStrokeGeometry::Verb; |
| 65 | using InstanceTallies = GrCCStrokeGeometry::InstanceTallies; |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame^] | 66 | using InstanceTalliesAllocator = GrTAllocator<InstanceTallies, 128>; |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 67 | |
| 68 | // Every kBeginPath verb has a corresponding PathInfo entry. |
| 69 | struct PathInfo { |
| 70 | SkIVector fDevToAtlasOffset; |
| 71 | float fStrokeRadius; |
| 72 | GrScissorTest fScissorTest; |
| 73 | }; |
| 74 | |
| 75 | // Defines a sub-batch of stroke instances that have a scissor test and the same scissor rect. |
| 76 | // Start indices are deduced by looking at the previous ScissorSubBatch. |
| 77 | struct ScissorSubBatch { |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame^] | 78 | ScissorSubBatch(InstanceTalliesAllocator* alloc, const InstanceTallies& startIndices, |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 79 | const SkIRect& scissor) |
| 80 | : fEndInstances(&alloc->emplace_back(startIndices)), fScissor(scissor) {} |
| 81 | InstanceTallies* fEndInstances; |
| 82 | SkIRect fScissor; |
| 83 | }; |
| 84 | |
| 85 | // Defines a batch of stroke instances that can be drawn with drawStrokes(). Start indices are |
| 86 | // deduced by looking at the previous Batch in the list. |
| 87 | struct Batch { |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame^] | 88 | Batch(InstanceTalliesAllocator* alloc, const InstanceTallies& startNonScissorIndices, |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 89 | int startScissorSubBatch) |
| 90 | : fNonScissorEndInstances(&alloc->emplace_back(startNonScissorIndices)) |
| 91 | , fEndScissorSubBatch(startScissorSubBatch) {} |
| 92 | InstanceTallies* fNonScissorEndInstances; |
| 93 | int fEndScissorSubBatch; |
| 94 | }; |
| 95 | |
| 96 | class InstanceBufferBuilder; |
| 97 | |
Chris Dalton | 39ca973 | 2020-03-10 10:34:17 -0600 | [diff] [blame] | 98 | // Draws a batch of strokes by chopping them into "2^numSegmentsLog2" linear segments each. |
| 99 | void drawLog2Strokes(int numSegmentsLog2, GrOpFlushState*, const GrPrimitiveProcessor&, |
| 100 | const GrPipeline&, const Batch&, const InstanceTallies* startIndices[2], |
| 101 | int startScissorSubBatch, const SkIRect& drawBounds) const; |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 102 | |
| 103 | template<int GrCCStrokeGeometry::InstanceTallies::* InstanceType> |
| 104 | void drawConnectingGeometry(GrOpFlushState*, const GrPipeline&, |
| 105 | const GrCCCoverageProcessor&, const Batch&, |
| 106 | const InstanceTallies* startIndices[2], int startScissorSubBatch, |
| 107 | const SkIRect& drawBounds) const; |
| 108 | |
| 109 | GrCCStrokeGeometry fGeometry; |
| 110 | SkSTArray<32, PathInfo> fPathInfos; |
| 111 | SkSTArray<32, Batch> fBatches; |
| 112 | SkSTArray<32, ScissorSubBatch> fScissorSubBatches; |
| 113 | int fMaxNumScissorSubBatches = 0; |
| 114 | bool fHasOpenBatch = false; |
| 115 | |
| 116 | const InstanceTallies fZeroTallies = InstanceTallies(); |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame^] | 117 | InstanceTalliesAllocator fTalliesAllocator; |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 118 | const InstanceTallies* fInstanceCounts[kNumScissorModes] = {&fZeroTallies, &fZeroTallies}; |
| 119 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 120 | sk_sp<GrGpuBuffer> fInstanceBuffer; |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 121 | // The indices stored in batches are relative to these base instances. |
| 122 | InstanceTallies fBaseInstances[kNumScissorModes]; |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | #endif |