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 | |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 11 | #include <array> |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 12 | #include "GrCaps.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 13 | #include "GrGeometryProcessor.h" |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 14 | #include "GrPipeline.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 15 | #include "SkPath.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 16 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 17 | class GrCCPathCacheEntry; |
| 18 | class GrCCPerFlushResources; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 19 | class GrOnFlushResourceProvider; |
Chris Dalton | d925f2d | 2018-05-07 19:19:06 -0600 | [diff] [blame] | 20 | class GrOpFlushState; |
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. |
Brian Osman | c6444d2 | 2019-01-09 16:30:12 -0500 | [diff] [blame] | 59 | uint64_t fColor; // Color always stored as 4 x fp16 |
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, |
Brian Osman | c6444d2 | 2019-01-09 16:30:12 -0500 | [diff] [blame] | 62 | const SkIVector& devToAtlasOffset, uint64_t, DoEvenOddFill = DoEvenOddFill::kNo); |
| 63 | void set(const GrCCPathCacheEntry&, const SkIVector& shift, uint64_t, |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 64 | DoEvenOddFill = DoEvenOddFill::kNo); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 65 | }; |
| 66 | |
Brian Osman | c6444d2 | 2019-01-09 16:30:12 -0500 | [diff] [blame] | 67 | GR_STATIC_ASSERT(4 * 12 == sizeof(Instance)); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 68 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 69 | static sk_sp<const GrGpuBuffer> FindVertexBuffer(GrOnFlushResourceProvider*); |
| 70 | static sk_sp<const GrGpuBuffer> FindIndexBuffer(GrOnFlushResourceProvider*); |
Chris Dalton | 5d2de08 | 2017-12-19 10:40:23 -0700 | [diff] [blame] | 71 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 72 | GrCCPathProcessor(const 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"; } |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 76 | const SkISize& atlasSize() const { return fAtlasSize; } |
| 77 | GrSurfaceOrigin atlasOrigin() const { return fAtlasOrigin; } |
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 { |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 80 | int idx = static_cast<int>(attribID); |
| 81 | SkASSERT(idx >= 0 && idx < static_cast<int>(SK_ARRAY_COUNT(kInstanceAttribs))); |
| 82 | return kInstanceAttribs[idx]; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 83 | } |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 84 | const Attribute& getEdgeNormsAttrib() const { return kEdgeNormsAttrib; } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 85 | |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 86 | void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {} |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 87 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override; |
| 88 | |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 89 | void drawPaths(GrOpFlushState*, const GrPipeline&, const GrPipeline::FixedDynamicState*, |
| 90 | const GrCCPerFlushResources&, int baseInstance, int endInstance, |
| 91 | const SkRect& bounds) const; |
Chris Dalton | d925f2d | 2018-05-07 19:19:06 -0600 | [diff] [blame] | 92 | |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 93 | private: |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 94 | const TextureSampler& onTextureSampler(int) const override { return fAtlasAccess; } |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 95 | |
Chris Dalton | a3e9271 | 2017-12-04 11:45:51 -0700 | [diff] [blame] | 96 | const TextureSampler fAtlasAccess; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 97 | SkISize fAtlasSize; |
| 98 | GrSurfaceOrigin fAtlasOrigin; |
| 99 | |
Chris Dalton | 1c54894 | 2018-05-22 13:09:48 -0600 | [diff] [blame] | 100 | SkMatrix fLocalMatrix; |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 101 | static constexpr Attribute kInstanceAttribs[kNumInstanceAttribs] = { |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 102 | {"devbounds", kFloat4_GrVertexAttribType, kFloat4_GrSLType}, |
| 103 | {"devbounds45", kFloat4_GrVertexAttribType, kFloat4_GrSLType}, |
| 104 | {"dev_to_atlas_offset", kInt2_GrVertexAttribType, kInt2_GrSLType}, |
Brian Osman | c6444d2 | 2019-01-09 16:30:12 -0500 | [diff] [blame] | 105 | {"color", kHalf4_GrVertexAttribType, kHalf4_GrSLType} |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 106 | }; |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 107 | static constexpr Attribute kEdgeNormsAttrib = {"edge_norms", kFloat4_GrVertexAttribType, |
| 108 | kFloat4_GrSLType}; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 109 | |
| 110 | typedef GrGeometryProcessor INHERITED; |
| 111 | }; |
| 112 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 113 | inline void GrCCPathProcessor::Instance::set(const SkRect& devBounds, const SkRect& devBounds45, |
Brian Osman | c6444d2 | 2019-01-09 16:30:12 -0500 | [diff] [blame] | 114 | const SkIVector& devToAtlasOffset, uint64_t color, |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 115 | DoEvenOddFill doEvenOddFill) { |
| 116 | if (DoEvenOddFill::kYes == doEvenOddFill) { |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 117 | // "right < left" indicates even-odd fill type. |
| 118 | fDevBounds.setLTRB(devBounds.fRight, devBounds.fTop, devBounds.fLeft, devBounds.fBottom); |
| 119 | } else { |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 120 | fDevBounds = devBounds; |
| 121 | } |
| 122 | fDevBounds45 = devBounds45; |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 123 | fDevToAtlasOffset = devToAtlasOffset; |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 124 | fColor = color; |
| 125 | } |
| 126 | |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 127 | #endif |