blob: 642cbdc9f1aec04102447ad3b4769e28cb1627e5 [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 Daltonf91b7552019-04-29 16:21:18 -060064 GrCCPathProcessor(const GrTexture* atlasTexture, GrSurfaceOrigin atlasOrigin,
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 Daltondaef06a2018-05-23 17:11:09 -060068 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
Chris Dalton1a325d22017-07-14 15:17:41 -060069 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override;
70
Brian Salomon49348902018-06-26 09:12:38 -040071 void drawPaths(GrOpFlushState*, const GrPipeline&, const GrPipeline::FixedDynamicState*,
72 const GrCCPerFlushResources&, int baseInstance, int endInstance,
73 const SkRect& bounds) const;
Chris Daltond925f2d2018-05-07 19:19:06 -060074
Chris Dalton1a325d22017-07-14 15:17:41 -060075private:
Brian Salomonf7dcd762018-07-30 14:48:15 -040076 const TextureSampler& onTextureSampler(int) const override { return fAtlasAccess; }
Brian Salomon92be2f72018-06-19 14:33:47 -040077
Chris Daltona3e92712017-12-04 11:45:51 -070078 const TextureSampler fAtlasAccess;
Brian Salomon7eae3e02018-08-07 14:02:38 +000079 SkISize fAtlasSize;
80 GrSurfaceOrigin fAtlasOrigin;
81
Chris Dalton1c548942018-05-22 13:09:48 -060082 SkMatrix fLocalMatrix;
Chris Dalton377b18b2019-04-18 13:33:50 -060083 static constexpr Attribute kInstanceAttribs[] = {
Brian Osmand4c29702018-09-14 16:16:55 -040084 {"devbounds", kFloat4_GrVertexAttribType, kFloat4_GrSLType},
85 {"devbounds45", kFloat4_GrVertexAttribType, kFloat4_GrSLType},
86 {"dev_to_atlas_offset", kInt2_GrVertexAttribType, kInt2_GrSLType},
Brian Osmanc6444d22019-01-09 16:30:12 -050087 {"color", kHalf4_GrVertexAttribType, kHalf4_GrSLType}
Brian Salomon92be2f72018-06-19 14:33:47 -040088 };
Chris Dalton377b18b2019-04-18 13:33:50 -060089 static constexpr int kColorAttribIdx = 3;
90 static constexpr Attribute kCornersAttrib =
91 {"corners", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
92
93 class Impl;
Chris Dalton1a325d22017-07-14 15:17:41 -060094
95 typedef GrGeometryProcessor INHERITED;
96};
97
Chris Dalton377b18b2019-04-18 13:33:50 -060098inline void GrCCPathProcessor::Instance::set(
99 const SkRect& devBounds, const SkRect& devBounds45, const SkIVector& devToAtlasOffset,
100 uint64_t color, DoEvenOddFill doEvenOddFill) {
101 if (DoEvenOddFill::kNo == doEvenOddFill) {
102 // We cover "nonzero" paths with clockwise triangles, which is the default result from
103 // normal bounding boxes.
Chris Daltondaef06a2018-05-23 17:11:09 -0600104 fDevBounds = devBounds;
Chris Dalton377b18b2019-04-18 13:33:50 -0600105 fDevBounds45 = devBounds45;
106 } else {
107 // We cover "even/odd" paths with counterclockwise triangles. Reorder the bounding box
108 // vertices so the output is flipped horizontally.
109 fDevBounds.setLTRB(devBounds.fRight, devBounds.fTop, devBounds.fLeft, devBounds.fBottom);
110 fDevBounds45.setLTRB(
111 devBounds45.fBottom, devBounds45.fRight, devBounds45.fTop, devBounds45.fLeft);
Chris Daltondaef06a2018-05-23 17:11:09 -0600112 }
Chris Dalton9414c962018-06-14 10:14:50 -0600113 fDevToAtlasOffset = devToAtlasOffset;
Chris Daltondaef06a2018-05-23 17:11:09 -0600114 fColor = color;
115}
116
Chris Dalton1a325d22017-07-14 15:17:41 -0600117#endif