blob: 416cdb1ab16e0d19dfa4e34701c525099119eeab [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 Dalton8610e9c2019-05-09 11:07:10 -060016#include "src/gpu/ccpr/GrOctoBounds.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060017
Chris Dalton4da70192018-06-18 09:51:36 -060018class GrCCPathCacheEntry;
19class GrCCPerFlushResources;
Chris Dalton1a325d22017-07-14 15:17:41 -060020class GrOnFlushResourceProvider;
Chris Daltond925f2d2018-05-07 19:19:06 -060021class GrOpFlushState;
Chris Dalton1a325d22017-07-14 15:17:41 -060022
23/**
Chris Dalton383a2ef2018-01-08 17:21:41 -050024 * This class draws AA paths using the coverage count masks produced by GrCCCoverageProcessor.
Chris Dalton1a325d22017-07-14 15:17:41 -060025 *
26 * Paths are drawn as bloated octagons, and coverage is derived from the coverage count mask and
27 * fill rule.
28 *
Chris Daltond925f2d2018-05-07 19:19:06 -060029 * To draw paths, the caller must set up an instance buffer as detailed below, then call drawPaths()
30 * providing its own instance buffer alongside the buffers found by calling FindIndexBuffer/
31 * FindVertexBuffer.
Chris Dalton1a325d22017-07-14 15:17:41 -060032 */
Chris Dalton383a2ef2018-01-08 17:21:41 -050033class GrCCPathProcessor : public GrGeometryProcessor {
Chris Dalton1a325d22017-07-14 15:17:41 -060034public:
Chris Dalton4da70192018-06-18 09:51:36 -060035 enum class DoEvenOddFill : bool {
36 kNo = false,
37 kYes = true
38 };
39
Chris Dalton1a325d22017-07-14 15:17:41 -060040 struct Instance {
Chris Dalton9414c962018-06-14 10:14:50 -060041 SkRect fDevBounds; // "right < left" indicates even-odd fill type.
Chris Dalton8610e9c2019-05-09 11:07:10 -060042 SkRect fDevBounds45; // Bounding box in "| 1 -1 | * devCoords" space. See GrOctoBounds.
Chris Dalton9414c962018-06-14 10:14:50 -060043 // | 1 1 |
44 SkIVector fDevToAtlasOffset; // Translation from device space to location in atlas.
Brian Osmanc6444d22019-01-09 16:30:12 -050045 uint64_t fColor; // Color always stored as 4 x fp16
Chris Dalton1a325d22017-07-14 15:17:41 -060046
Chris Dalton8610e9c2019-05-09 11:07:10 -060047 void set(const GrOctoBounds&, const SkIVector& devToAtlasOffset, uint64_t,
48 DoEvenOddFill = DoEvenOddFill::kNo);
Brian Osmanc6444d22019-01-09 16:30:12 -050049 void set(const GrCCPathCacheEntry&, const SkIVector& shift, uint64_t,
Chris Dalton4da70192018-06-18 09:51:36 -060050 DoEvenOddFill = DoEvenOddFill::kNo);
Chris Dalton1a325d22017-07-14 15:17:41 -060051 };
52
Brian Osmanc6444d22019-01-09 16:30:12 -050053 GR_STATIC_ASSERT(4 * 12 == sizeof(Instance));
Chris Dalton1a325d22017-07-14 15:17:41 -060054
Brian Salomondbf70722019-02-07 11:31:24 -050055 static sk_sp<const GrGpuBuffer> FindVertexBuffer(GrOnFlushResourceProvider*);
56 static sk_sp<const GrGpuBuffer> FindIndexBuffer(GrOnFlushResourceProvider*);
Chris Dalton5d2de082017-12-19 10:40:23 -070057
Chris Daltonf91b7552019-04-29 16:21:18 -060058 GrCCPathProcessor(const GrTexture* atlasTexture, GrSurfaceOrigin atlasOrigin,
Chris Dalton1c548942018-05-22 13:09:48 -060059 const SkMatrix& viewMatrixIfUsingLocalCoords = SkMatrix::I());
Chris Dalton1a325d22017-07-14 15:17:41 -060060
Chris Dalton383a2ef2018-01-08 17:21:41 -050061 const char* name() const override { return "GrCCPathProcessor"; }
Chris Daltondaef06a2018-05-23 17:11:09 -060062 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
Chris Dalton1a325d22017-07-14 15:17:41 -060063 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override;
64
Brian Salomon49348902018-06-26 09:12:38 -040065 void drawPaths(GrOpFlushState*, const GrPipeline&, const GrPipeline::FixedDynamicState*,
66 const GrCCPerFlushResources&, int baseInstance, int endInstance,
67 const SkRect& bounds) const;
Chris Daltond925f2d2018-05-07 19:19:06 -060068
Chris Dalton1a325d22017-07-14 15:17:41 -060069private:
Brian Salomonf7dcd762018-07-30 14:48:15 -040070 const TextureSampler& onTextureSampler(int) const override { return fAtlasAccess; }
Brian Salomon92be2f72018-06-19 14:33:47 -040071
Chris Daltona3e92712017-12-04 11:45:51 -070072 const TextureSampler fAtlasAccess;
Brian Salomon7eae3e02018-08-07 14:02:38 +000073 SkISize fAtlasSize;
74 GrSurfaceOrigin fAtlasOrigin;
75
Chris Dalton1c548942018-05-22 13:09:48 -060076 SkMatrix fLocalMatrix;
Chris Dalton377b18b2019-04-18 13:33:50 -060077 static constexpr Attribute kInstanceAttribs[] = {
Brian Osmand4c29702018-09-14 16:16:55 -040078 {"devbounds", kFloat4_GrVertexAttribType, kFloat4_GrSLType},
79 {"devbounds45", kFloat4_GrVertexAttribType, kFloat4_GrSLType},
80 {"dev_to_atlas_offset", kInt2_GrVertexAttribType, kInt2_GrSLType},
Brian Osmanc6444d22019-01-09 16:30:12 -050081 {"color", kHalf4_GrVertexAttribType, kHalf4_GrSLType}
Brian Salomon92be2f72018-06-19 14:33:47 -040082 };
Chris Dalton377b18b2019-04-18 13:33:50 -060083 static constexpr int kColorAttribIdx = 3;
84 static constexpr Attribute kCornersAttrib =
85 {"corners", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
86
87 class Impl;
Chris Dalton1a325d22017-07-14 15:17:41 -060088
89 typedef GrGeometryProcessor INHERITED;
90};
91
Chris Dalton377b18b2019-04-18 13:33:50 -060092inline void GrCCPathProcessor::Instance::set(
Chris Dalton8610e9c2019-05-09 11:07:10 -060093 const GrOctoBounds& octoBounds, const SkIVector& devToAtlasOffset, uint64_t color,
94 DoEvenOddFill doEvenOddFill) {
Chris Dalton377b18b2019-04-18 13:33:50 -060095 if (DoEvenOddFill::kNo == doEvenOddFill) {
96 // We cover "nonzero" paths with clockwise triangles, which is the default result from
Chris Dalton8610e9c2019-05-09 11:07:10 -060097 // normal octo bounds.
98 fDevBounds = octoBounds.bounds();
99 fDevBounds45 = octoBounds.bounds45();
Chris Dalton377b18b2019-04-18 13:33:50 -0600100 } else {
Chris Dalton8610e9c2019-05-09 11:07:10 -0600101 // We cover "even/odd" paths with counterclockwise triangles. Here we reorder the bounding
102 // box vertices so the output is flipped horizontally.
103 fDevBounds.setLTRB(
104 octoBounds.right(), octoBounds.top(), octoBounds.left(), octoBounds.bottom());
Chris Dalton377b18b2019-04-18 13:33:50 -0600105 fDevBounds45.setLTRB(
Chris Dalton8610e9c2019-05-09 11:07:10 -0600106 octoBounds.bottom45(), octoBounds.right45(), octoBounds.top45(),
107 octoBounds.left45());
Chris Daltondaef06a2018-05-23 17:11:09 -0600108 }
Chris Dalton9414c962018-06-14 10:14:50 -0600109 fDevToAtlasOffset = devToAtlasOffset;
Chris Daltondaef06a2018-05-23 17:11:09 -0600110 fColor = color;
111}
112
Chris Dalton1a325d22017-07-14 15:17:41 -0600113#endif