blob: a74455bc8e361c3a8dc8d493e0f0f79e4c90c9c7 [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
8#ifndef GrCCPRPathProcessor_DEFINED
9#define GrCCPRPathProcessor_DEFINED
10
11#include "GrGeometryProcessor.h"
12#include "SkPath.h"
13#include <array>
14
15class GrOnFlushResourceProvider;
16class GrShaderCaps;
17
18/**
19 * This class draws AA paths using the coverage count masks produced by GrCCPRCoverageProcessor.
20 *
21 * Paths are drawn as bloated octagons, and coverage is derived from the coverage count mask and
22 * fill rule.
23 *
24 * The caller must set up an instance buffer as detailed below, then draw indexed-instanced
25 * triangles using the index and vertex buffers provided by this class.
26 */
27class GrCCPRPathProcessor : public GrGeometryProcessor {
28public:
29 static constexpr int kPerInstanceIndexCount = 6 * 3;
30 static sk_sp<GrBuffer> FindOrMakeIndexBuffer(GrOnFlushResourceProvider*);
31 static sk_sp<GrBuffer> FindOrMakeVertexBuffer(GrOnFlushResourceProvider*);
32
33 enum class InstanceAttribs {
34 kDevBounds,
35 kDevBounds45,
36 kViewMatrix, // FIXME: This causes a lot of duplication. It could move to a texel buffer.
37 kViewTranslate,
38 kAtlasOffset,
39 kColor
40 };
41 static constexpr int kNumInstanceAttribs = 1 + (int)InstanceAttribs::kColor;
42
43 struct Instance {
44 SkRect fDevBounds;
45 SkRect fDevBounds45; // Bounding box in "| 1 -1 | * devCoords" space.
46 // | 1 1 |
47 std::array<float, 4> fViewMatrix; // {kScaleX, kSkewy, kSkewX, kScaleY}
48 std::array<float, 2> fViewTranslate;
49 std::array<int32_t, 2> fAtlasOffset;
50 uint32_t fColor;
51
52 GR_STATIC_ASSERT(SK_SCALAR_IS_FLOAT);
53 };
54
55 GR_STATIC_ASSERT(4 * 17 == sizeof(Instance)); // FIXME: 4 * 16 by making fAtlasOffset int16_t's.
56
57 GrCCPRPathProcessor(GrResourceProvider*, sk_sp<GrTextureProxy> atlas, SkPath::FillType,
58 const GrShaderCaps&);
59
60 const char* name() const override { return "GrCCPRPathProcessor"; }
Chris Dalton1a325d22017-07-14 15:17:41 -060061 const GrTexture* atlas() const { return fAtlasAccess.peekTexture(); }
62 SkPath::FillType fillType() const { return fFillType; }
63 const Attribute& getInstanceAttrib(InstanceAttribs attribID) const {
64 const Attribute& attrib = this->getAttrib((int)attribID);
65 SkASSERT(Attribute::InputRate::kPerInstance == attrib.fInputRate);
66 return attrib;
67 }
68 const Attribute& getEdgeNormsAttrib() const {
69 SkASSERT(1 + kNumInstanceAttribs == this->numAttribs());
70 const Attribute& attrib = this->getAttrib(kNumInstanceAttribs);
71 SkASSERT(Attribute::InputRate::kPerVertex == attrib.fInputRate);
72 return attrib;
73 }
74
75 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
76 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override;
77
78private:
79 const SkPath::FillType fFillType;
80 TextureSampler fAtlasAccess;
81
82 typedef GrGeometryProcessor INHERITED;
83};
84
85#endif