blob: 0e2a22d159e45d7c74e389d5005b63488dde26e3 [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>
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkPath.h"
13#include "src/gpu/GrCaps.h"
14#include "src/gpu/GrGeometryProcessor.h"
15#include "src/gpu/GrPipeline.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 Dalton4da70192018-06-18 09:51:36 -060034 // Helper to offset the 45-degree bounding box returned by GrCCPathParser::parsePath().
35 static SkRect MakeOffset45(const SkRect& devBounds45, float dx, float dy) {
36 // devBounds45 is in "| 1 -1 | * devCoords" space.
37 // | 1 1 |
38 return devBounds45.makeOffset(dx - dy, dx + dy);
39 }
40
41 enum class DoEvenOddFill : bool {
42 kNo = false,
43 kYes = true
44 };
45
Chris Dalton1a325d22017-07-14 15:17:41 -060046 struct Instance {
Chris Dalton9414c962018-06-14 10:14:50 -060047 SkRect fDevBounds; // "right < left" indicates even-odd fill type.
48 SkRect fDevBounds45; // Bounding box in "| 1 -1 | * devCoords" space.
49 // | 1 1 |
50 SkIVector fDevToAtlasOffset; // Translation from device space to location in atlas.
Brian Osmanc6444d22019-01-09 16:30:12 -050051 uint64_t fColor; // Color always stored as 4 x fp16
Chris Dalton1a325d22017-07-14 15:17:41 -060052
Chris Dalton4da70192018-06-18 09:51:36 -060053 void set(const SkRect& devBounds, const SkRect& devBounds45,
Brian Osmanc6444d22019-01-09 16:30:12 -050054 const SkIVector& devToAtlasOffset, uint64_t, DoEvenOddFill = DoEvenOddFill::kNo);
55 void set(const GrCCPathCacheEntry&, const SkIVector& shift, uint64_t,
Chris Dalton4da70192018-06-18 09:51:36 -060056 DoEvenOddFill = DoEvenOddFill::kNo);
Chris Dalton1a325d22017-07-14 15:17:41 -060057 };
58
Brian Osmanc6444d22019-01-09 16:30:12 -050059 GR_STATIC_ASSERT(4 * 12 == sizeof(Instance));
Chris Dalton1a325d22017-07-14 15:17:41 -060060
Brian Salomondbf70722019-02-07 11:31:24 -050061 static sk_sp<const GrGpuBuffer> FindVertexBuffer(GrOnFlushResourceProvider*);
62 static sk_sp<const GrGpuBuffer> FindIndexBuffer(GrOnFlushResourceProvider*);
Chris Dalton5d2de082017-12-19 10:40:23 -070063
Chris Dalton2185f392019-04-25 17:44:41 +000064 GrCCPathProcessor(const GrTextureProxy* atlas,
Chris Dalton1c548942018-05-22 13:09:48 -060065 const SkMatrix& viewMatrixIfUsingLocalCoords = SkMatrix::I());
Chris Dalton1a325d22017-07-14 15:17:41 -060066
Chris Dalton383a2ef2018-01-08 17:21:41 -050067 const char* name() const override { return "GrCCPathProcessor"; }
Chris Dalton2185f392019-04-25 17:44:41 +000068 const SkISize& atlasSize() const { return fAtlasSize; }
69 GrSurfaceOrigin atlasOrigin() const { return fAtlasOrigin; }
70 const SkMatrix& localMatrix() const { return fLocalMatrix; }
71
Chris Daltondaef06a2018-05-23 17:11:09 -060072 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
Chris Dalton1a325d22017-07-14 15:17:41 -060073 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override;
74
Brian Salomon49348902018-06-26 09:12:38 -040075 void drawPaths(GrOpFlushState*, const GrPipeline&, const GrPipeline::FixedDynamicState*,
76 const GrCCPerFlushResources&, int baseInstance, int endInstance,
77 const SkRect& bounds) const;
Chris Daltond925f2d2018-05-07 19:19:06 -060078
Chris Dalton1a325d22017-07-14 15:17:41 -060079private:
Brian Salomonf7dcd762018-07-30 14:48:15 -040080 const TextureSampler& onTextureSampler(int) const override { return fAtlasAccess; }
Brian Salomon92be2f72018-06-19 14:33:47 -040081
Chris Daltona3e92712017-12-04 11:45:51 -070082 const TextureSampler fAtlasAccess;
Brian Salomon7eae3e02018-08-07 14:02:38 +000083 SkISize fAtlasSize;
84 GrSurfaceOrigin fAtlasOrigin;
85
Chris Dalton1c548942018-05-22 13:09:48 -060086 SkMatrix fLocalMatrix;
Chris Dalton377b18b2019-04-18 13:33:50 -060087 static constexpr Attribute kInstanceAttribs[] = {
Brian Osmand4c29702018-09-14 16:16:55 -040088 {"devbounds", kFloat4_GrVertexAttribType, kFloat4_GrSLType},
89 {"devbounds45", kFloat4_GrVertexAttribType, kFloat4_GrSLType},
90 {"dev_to_atlas_offset", kInt2_GrVertexAttribType, kInt2_GrSLType},
Brian Osmanc6444d22019-01-09 16:30:12 -050091 {"color", kHalf4_GrVertexAttribType, kHalf4_GrSLType}
Brian Salomon92be2f72018-06-19 14:33:47 -040092 };
Chris Dalton377b18b2019-04-18 13:33:50 -060093 static constexpr int kColorAttribIdx = 3;
94 static constexpr Attribute kCornersAttrib =
95 {"corners", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
96
97 class Impl;
Chris Dalton1a325d22017-07-14 15:17:41 -060098
99 typedef GrGeometryProcessor INHERITED;
100};
101
Chris Dalton377b18b2019-04-18 13:33:50 -0600102inline void GrCCPathProcessor::Instance::set(
103 const SkRect& devBounds, const SkRect& devBounds45, const SkIVector& devToAtlasOffset,
104 uint64_t color, DoEvenOddFill doEvenOddFill) {
105 if (DoEvenOddFill::kNo == doEvenOddFill) {
106 // We cover "nonzero" paths with clockwise triangles, which is the default result from
107 // normal bounding boxes.
Chris Daltondaef06a2018-05-23 17:11:09 -0600108 fDevBounds = devBounds;
Chris Dalton377b18b2019-04-18 13:33:50 -0600109 fDevBounds45 = devBounds45;
110 } else {
111 // We cover "even/odd" paths with counterclockwise triangles. Reorder the bounding box
112 // vertices so the output is flipped horizontally.
113 fDevBounds.setLTRB(devBounds.fRight, devBounds.fTop, devBounds.fLeft, devBounds.fBottom);
114 fDevBounds45.setLTRB(
115 devBounds45.fBottom, devBounds45.fRight, devBounds45.fTop, devBounds45.fLeft);
Chris Daltondaef06a2018-05-23 17:11:09 -0600116 }
Chris Dalton9414c962018-06-14 10:14:50 -0600117 fDevToAtlasOffset = devToAtlasOffset;
Chris Daltondaef06a2018-05-23 17:11:09 -0600118 fColor = color;
119}
120
Chris Dalton1a325d22017-07-14 15:17:41 -0600121#endif