blob: 534f08d237a869ef0088417b0e898ab9e8b7743b [file] [log] [blame]
Chris Dalton1a325d22017-07-14 15:17:41 -06001/*
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 Dalton383a2ef2018-01-08 17:21:41 -05008#ifndef GrCCPathProcessor_DEFINED
9#define GrCCPathProcessor_DEFINED
Chris Dalton1a325d22017-07-14 15:17:41 -060010
Brian Salomon49348902018-06-26 09:12:38 -040011#include <array>
Chris Dalton27059d32018-01-23 14:06:50 -070012#include "GrCaps.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060013#include "GrGeometryProcessor.h"
Brian Salomon49348902018-06-26 09:12:38 -040014#include "GrPipeline.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060015#include "SkPath.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060016
Chris Dalton4da70192018-06-18 09:51:36 -060017class GrCCPathCacheEntry;
18class GrCCPerFlushResources;
Chris Dalton1a325d22017-07-14 15:17:41 -060019class GrOnFlushResourceProvider;
Chris Daltond925f2d2018-05-07 19:19:06 -060020class GrOpFlushState;
Chris Dalton1a325d22017-07-14 15:17:41 -060021
22/**
Chris Dalton383a2ef2018-01-08 17:21:41 -050023 * This class draws AA paths using the coverage count masks produced by GrCCCoverageProcessor.
Chris Dalton1a325d22017-07-14 15:17:41 -060024 *
25 * Paths are drawn as bloated octagons, and coverage is derived from the coverage count mask and
26 * fill rule.
27 *
Chris Daltond925f2d2018-05-07 19:19:06 -060028 * 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 Dalton1a325d22017-07-14 15:17:41 -060031 */
Chris Dalton383a2ef2018-01-08 17:21:41 -050032class GrCCPathProcessor : public GrGeometryProcessor {
Chris Dalton1a325d22017-07-14 15:17:41 -060033public:
Chris Dalton1a325d22017-07-14 15:17:41 -060034 enum class InstanceAttribs {
35 kDevBounds,
36 kDevBounds45,
Chris Dalton9414c962018-06-14 10:14:50 -060037 kDevToAtlasOffset,
Chris Dalton1a325d22017-07-14 15:17:41 -060038 kColor
39 };
40 static constexpr int kNumInstanceAttribs = 1 + (int)InstanceAttribs::kColor;
41
Chris Dalton4da70192018-06-18 09:51:36 -060042 // 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 Dalton1a325d22017-07-14 15:17:41 -060054 struct Instance {
Chris Dalton9414c962018-06-14 10:14:50 -060055 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 Osmanc6444d22019-01-09 16:30:12 -050059 uint64_t fColor; // Color always stored as 4 x fp16
Chris Dalton1a325d22017-07-14 15:17:41 -060060
Chris Dalton4da70192018-06-18 09:51:36 -060061 void set(const SkRect& devBounds, const SkRect& devBounds45,
Brian Osmanc6444d22019-01-09 16:30:12 -050062 const SkIVector& devToAtlasOffset, uint64_t, DoEvenOddFill = DoEvenOddFill::kNo);
63 void set(const GrCCPathCacheEntry&, const SkIVector& shift, uint64_t,
Chris Dalton4da70192018-06-18 09:51:36 -060064 DoEvenOddFill = DoEvenOddFill::kNo);
Chris Dalton1a325d22017-07-14 15:17:41 -060065 };
66
Brian Osmanc6444d22019-01-09 16:30:12 -050067 GR_STATIC_ASSERT(4 * 12 == sizeof(Instance));
Chris Dalton1a325d22017-07-14 15:17:41 -060068
Brian Salomondbf70722019-02-07 11:31:24 -050069 static sk_sp<const GrGpuBuffer> FindVertexBuffer(GrOnFlushResourceProvider*);
70 static sk_sp<const GrGpuBuffer> FindIndexBuffer(GrOnFlushResourceProvider*);
Chris Dalton5d2de082017-12-19 10:40:23 -070071
Brian Salomon7eae3e02018-08-07 14:02:38 +000072 GrCCPathProcessor(const GrTextureProxy* atlas,
Chris Dalton1c548942018-05-22 13:09:48 -060073 const SkMatrix& viewMatrixIfUsingLocalCoords = SkMatrix::I());
Chris Dalton1a325d22017-07-14 15:17:41 -060074
Chris Dalton383a2ef2018-01-08 17:21:41 -050075 const char* name() const override { return "GrCCPathProcessor"; }
Brian Salomon7eae3e02018-08-07 14:02:38 +000076 const SkISize& atlasSize() const { return fAtlasSize; }
77 GrSurfaceOrigin atlasOrigin() const { return fAtlasOrigin; }
Chris Dalton1c548942018-05-22 13:09:48 -060078 const SkMatrix& localMatrix() const { return fLocalMatrix; }
Chris Dalton1a325d22017-07-14 15:17:41 -060079 const Attribute& getInstanceAttrib(InstanceAttribs attribID) const {
Brian Salomon92be2f72018-06-19 14:33:47 -040080 int idx = static_cast<int>(attribID);
81 SkASSERT(idx >= 0 && idx < static_cast<int>(SK_ARRAY_COUNT(kInstanceAttribs)));
82 return kInstanceAttribs[idx];
Chris Dalton1a325d22017-07-14 15:17:41 -060083 }
Brian Salomon92be2f72018-06-19 14:33:47 -040084 const Attribute& getEdgeNormsAttrib() const { return kEdgeNormsAttrib; }
Chris Dalton1a325d22017-07-14 15:17:41 -060085
Chris Daltondaef06a2018-05-23 17:11:09 -060086 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
Chris Dalton1a325d22017-07-14 15:17:41 -060087 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override;
88
Brian Salomon49348902018-06-26 09:12:38 -040089 void drawPaths(GrOpFlushState*, const GrPipeline&, const GrPipeline::FixedDynamicState*,
90 const GrCCPerFlushResources&, int baseInstance, int endInstance,
91 const SkRect& bounds) const;
Chris Daltond925f2d2018-05-07 19:19:06 -060092
Chris Dalton1a325d22017-07-14 15:17:41 -060093private:
Brian Salomonf7dcd762018-07-30 14:48:15 -040094 const TextureSampler& onTextureSampler(int) const override { return fAtlasAccess; }
Brian Salomon92be2f72018-06-19 14:33:47 -040095
Chris Daltona3e92712017-12-04 11:45:51 -070096 const TextureSampler fAtlasAccess;
Brian Salomon7eae3e02018-08-07 14:02:38 +000097 SkISize fAtlasSize;
98 GrSurfaceOrigin fAtlasOrigin;
99
Chris Dalton1c548942018-05-22 13:09:48 -0600100 SkMatrix fLocalMatrix;
Brian Salomon92be2f72018-06-19 14:33:47 -0400101 static constexpr Attribute kInstanceAttribs[kNumInstanceAttribs] = {
Brian Osmand4c29702018-09-14 16:16:55 -0400102 {"devbounds", kFloat4_GrVertexAttribType, kFloat4_GrSLType},
103 {"devbounds45", kFloat4_GrVertexAttribType, kFloat4_GrSLType},
104 {"dev_to_atlas_offset", kInt2_GrVertexAttribType, kInt2_GrSLType},
Brian Osmanc6444d22019-01-09 16:30:12 -0500105 {"color", kHalf4_GrVertexAttribType, kHalf4_GrSLType}
Brian Salomon92be2f72018-06-19 14:33:47 -0400106 };
Brian Osmand4c29702018-09-14 16:16:55 -0400107 static constexpr Attribute kEdgeNormsAttrib = {"edge_norms", kFloat4_GrVertexAttribType,
108 kFloat4_GrSLType};
Chris Dalton1a325d22017-07-14 15:17:41 -0600109
110 typedef GrGeometryProcessor INHERITED;
111};
112
Chris Dalton4da70192018-06-18 09:51:36 -0600113inline void GrCCPathProcessor::Instance::set(const SkRect& devBounds, const SkRect& devBounds45,
Brian Osmanc6444d22019-01-09 16:30:12 -0500114 const SkIVector& devToAtlasOffset, uint64_t color,
Chris Dalton4da70192018-06-18 09:51:36 -0600115 DoEvenOddFill doEvenOddFill) {
116 if (DoEvenOddFill::kYes == doEvenOddFill) {
Chris Daltondaef06a2018-05-23 17:11:09 -0600117 // "right < left" indicates even-odd fill type.
118 fDevBounds.setLTRB(devBounds.fRight, devBounds.fTop, devBounds.fLeft, devBounds.fBottom);
119 } else {
Chris Daltondaef06a2018-05-23 17:11:09 -0600120 fDevBounds = devBounds;
121 }
122 fDevBounds45 = devBounds45;
Chris Dalton9414c962018-06-14 10:14:50 -0600123 fDevToAtlasOffset = devToAtlasOffset;
Chris Daltondaef06a2018-05-23 17:11:09 -0600124 fColor = color;
125}
126
Chris Dalton1a325d22017-07-14 15:17:41 -0600127#endif