Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 8 | #ifndef GrCCPathProcessor_DEFINED |
| 9 | #define GrCCPathProcessor_DEFINED |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 10 | |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 11 | #include "GrCaps.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 12 | #include "GrGeometryProcessor.h" |
| 13 | #include "SkPath.h" |
| 14 | #include <array> |
| 15 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 16 | class GrCCPathCacheEntry; |
| 17 | class GrCCPerFlushResources; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 18 | class GrOnFlushResourceProvider; |
Chris Dalton | d925f2d | 2018-05-07 19:19:06 -0600 | [diff] [blame] | 19 | class GrOpFlushState; |
| 20 | class GrPipeline; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 21 | |
| 22 | /** |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 23 | * This class draws AA paths using the coverage count masks produced by GrCCCoverageProcessor. |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 24 | * |
| 25 | * Paths are drawn as bloated octagons, and coverage is derived from the coverage count mask and |
| 26 | * fill rule. |
| 27 | * |
Chris Dalton | d925f2d | 2018-05-07 19:19:06 -0600 | [diff] [blame] | 28 | * To draw paths, the caller must set up an instance buffer as detailed below, then call drawPaths() |
| 29 | * providing its own instance buffer alongside the buffers found by calling FindIndexBuffer/ |
| 30 | * FindVertexBuffer. |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 31 | */ |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 32 | class GrCCPathProcessor : public GrGeometryProcessor { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 33 | public: |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 34 | enum class InstanceAttribs { |
| 35 | kDevBounds, |
| 36 | kDevBounds45, |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 37 | kDevToAtlasOffset, |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 38 | kColor |
| 39 | }; |
| 40 | static constexpr int kNumInstanceAttribs = 1 + (int)InstanceAttribs::kColor; |
| 41 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 42 | // Helper to offset the 45-degree bounding box returned by GrCCPathParser::parsePath(). |
| 43 | static SkRect MakeOffset45(const SkRect& devBounds45, float dx, float dy) { |
| 44 | // devBounds45 is in "| 1 -1 | * devCoords" space. |
| 45 | // | 1 1 | |
| 46 | return devBounds45.makeOffset(dx - dy, dx + dy); |
| 47 | } |
| 48 | |
| 49 | enum class DoEvenOddFill : bool { |
| 50 | kNo = false, |
| 51 | kYes = true |
| 52 | }; |
| 53 | |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 54 | struct Instance { |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 55 | SkRect fDevBounds; // "right < left" indicates even-odd fill type. |
| 56 | SkRect fDevBounds45; // Bounding box in "| 1 -1 | * devCoords" space. |
| 57 | // | 1 1 | |
| 58 | SkIVector fDevToAtlasOffset; // Translation from device space to location in atlas. |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 59 | GrColor fColor; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 60 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 61 | void set(const SkRect& devBounds, const SkRect& devBounds45, |
| 62 | const SkIVector& devToAtlasOffset, GrColor, DoEvenOddFill = DoEvenOddFill::kNo); |
| 63 | void set(const GrCCPathCacheEntry&, const SkIVector& shift, GrColor, |
| 64 | DoEvenOddFill = DoEvenOddFill::kNo); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 65 | }; |
| 66 | |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 67 | GR_STATIC_ASSERT(4 * 11 == sizeof(Instance)); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 68 | |
Chris Dalton | 5d2de08 | 2017-12-19 10:40:23 -0700 | [diff] [blame] | 69 | static sk_sp<const GrBuffer> FindVertexBuffer(GrOnFlushResourceProvider*); |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 70 | static sk_sp<const GrBuffer> FindIndexBuffer(GrOnFlushResourceProvider*); |
Chris Dalton | 5d2de08 | 2017-12-19 10:40:23 -0700 | [diff] [blame] | 71 | |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 72 | GrCCPathProcessor(GrResourceProvider*, sk_sp<GrTextureProxy> atlas, |
Chris Dalton | 1c54894 | 2018-05-22 13:09:48 -0600 | [diff] [blame] | 73 | const SkMatrix& viewMatrixIfUsingLocalCoords = SkMatrix::I()); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 74 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 75 | const char* name() const override { return "GrCCPathProcessor"; } |
Robert Phillips | e44ef10 | 2017-07-21 15:37:19 -0400 | [diff] [blame] | 76 | const GrSurfaceProxy* atlasProxy() const { return fAtlasAccess.proxy(); } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 77 | const GrTexture* atlas() const { return fAtlasAccess.peekTexture(); } |
Chris Dalton | 1c54894 | 2018-05-22 13:09:48 -0600 | [diff] [blame] | 78 | const SkMatrix& localMatrix() const { return fLocalMatrix; } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 79 | const Attribute& getInstanceAttrib(InstanceAttribs attribID) const { |
Mike Klein | 5045e50 | 2018-06-19 01:40:57 +0000 | [diff] [blame] | 80 | const Attribute& attrib = this->getAttrib((int)attribID); |
| 81 | SkASSERT(Attribute::InputRate::kPerInstance == attrib.inputRate()); |
| 82 | return attrib; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 83 | } |
Mike Klein | 5045e50 | 2018-06-19 01:40:57 +0000 | [diff] [blame] | 84 | const Attribute& getEdgeNormsAttrib() const { |
| 85 | SkASSERT(1 + kNumInstanceAttribs == this->numAttribs()); |
| 86 | const Attribute& attrib = this->getAttrib(kNumInstanceAttribs); |
| 87 | SkASSERT(Attribute::InputRate::kPerVertex == attrib.inputRate()); |
| 88 | return attrib; |
| 89 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 90 | |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 91 | void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {} |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 92 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override; |
| 93 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 94 | void drawPaths(GrOpFlushState*, const GrPipeline&, const GrCCPerFlushResources&, |
| 95 | int baseInstance, int endInstance, const SkRect& bounds) const; |
Chris Dalton | d925f2d | 2018-05-07 19:19:06 -0600 | [diff] [blame] | 96 | |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 97 | private: |
Chris Dalton | a3e9271 | 2017-12-04 11:45:51 -0700 | [diff] [blame] | 98 | const TextureSampler fAtlasAccess; |
Chris Dalton | 1c54894 | 2018-05-22 13:09:48 -0600 | [diff] [blame] | 99 | SkMatrix fLocalMatrix; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 100 | |
| 101 | typedef GrGeometryProcessor INHERITED; |
| 102 | }; |
| 103 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 104 | inline void GrCCPathProcessor::Instance::set(const SkRect& devBounds, const SkRect& devBounds45, |
| 105 | const SkIVector& devToAtlasOffset, GrColor color, |
| 106 | DoEvenOddFill doEvenOddFill) { |
| 107 | if (DoEvenOddFill::kYes == doEvenOddFill) { |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 108 | // "right < left" indicates even-odd fill type. |
| 109 | fDevBounds.setLTRB(devBounds.fRight, devBounds.fTop, devBounds.fLeft, devBounds.fBottom); |
| 110 | } else { |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 111 | fDevBounds = devBounds; |
| 112 | } |
| 113 | fDevBounds45 = devBounds45; |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 114 | fDevToAtlasOffset = devToAtlasOffset; |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 115 | fColor = color; |
| 116 | } |
| 117 | |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 118 | #endif |