1 /*
2  * Copyright 2014 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 GrDefaultGeoProcFactory_DEFINED
9 #define GrDefaultGeoProcFactory_DEFINED
10 
11 #include "GrColorSpaceXform.h"
12 #include "GrGeometryProcessor.h"
13 
14 /*
15  * A factory for creating default Geometry Processors which simply multiply position by the uniform
16  * view matrix and wire through color, coverage, UV coords if requested.
17  */
18 namespace GrDefaultGeoProcFactory {
19     // Structs for adding vertex attributes
20     struct PositionAttr {
21         SkPoint fPosition;
22     };
23 
24     struct PositionCoverageAttr {
25         SkPoint fPosition;
26         float   fCoverage;
27     };
28 
29     struct PositionColorAttr {
30         SkPoint fPosition;
31         SkColor fColor;
32     };
33 
34     struct PositionColorCoverageAttr {
35         SkPoint fPosition;
36         SkColor fColor;
37         float   fCoverage;
38     };
39 
40     struct PositionLocalCoordAttr {
41         SkPoint fPosition;
42         SkPoint fLocalCoord;
43     };
44 
45     struct PositionLocalCoordCoverageAttr {
46         SkPoint fPosition;
47         SkPoint fLocalCoord;
48         float   fCoverage;
49     };
50 
51     struct PositionColorLocalCoordAttr {
52         SkPoint fPosition;
53         GrColor fColor;
54         SkPoint fLocalCoord;
55     };
56 
57     struct PositionColorLocalCoordCoverage {
58         SkPoint fPosition;
59         GrColor fColor;
60         SkPoint fLocalCoord;
61         float   fCoverage;
62     };
63 
64     struct Color {
65         enum Type {
66             kPremulGrColorUniform_Type,
67             kPremulGrColorAttribute_Type,
68             kUnpremulSkColorAttribute_Type,
69         };
70         explicit Color(GrColor color)
71                 : fType(kPremulGrColorUniform_Type)
72                 , fColor(color)
73                 , fLinearize(false)
74                 , fColorSpaceXform(nullptr) {}
75         Color(Type type)
76                 : fType(type)
77                 , fColor(GrColor_ILLEGAL)
78                 , fLinearize(false)
79                 , fColorSpaceXform(nullptr) {
80             SkASSERT(type != kPremulGrColorUniform_Type);
81         }
82 
83         Type fType;
84         GrColor fColor;
85 
86         // These options only apply to SkColor. Any GrColors are assumed to have been color managed
87         // during paint conversion.
88         bool fLinearize;
89         sk_sp<GrColorSpaceXform> fColorSpaceXform;
90     };
91 
92     struct Coverage {
93         enum Type {
94             kSolid_Type,
95             kUniform_Type,
96             kAttribute_Type,
97         };
98         explicit Coverage(uint8_t coverage) : fType(kUniform_Type), fCoverage(coverage) {}
99         Coverage(Type type) : fType(type), fCoverage(0xff) {
100             SkASSERT(type != kUniform_Type);
101         }
102 
103         Type fType;
104         uint8_t fCoverage;
105     };
106 
107     struct LocalCoords {
108         enum Type {
109             kUnused_Type,
110             kUsePosition_Type,
111             kHasExplicit_Type,
112             kHasTransformed_Type,
113         };
114         LocalCoords(Type type) : fType(type), fMatrix(nullptr) {}
115         LocalCoords(Type type, const SkMatrix* matrix) : fType(type), fMatrix(matrix) {
116             SkASSERT(kUnused_Type != type);
117         }
118         bool hasLocalMatrix() const { return nullptr != fMatrix; }
119 
120         Type fType;
121         const SkMatrix* fMatrix;
122     };
123 
124     sk_sp<GrGeometryProcessor> Make(const Color&,
125                                     const Coverage&,
126                                     const LocalCoords&,
127                                     const SkMatrix& viewMatrix);
128 
129     /*
130      * Use this factory to create a GrGeometryProcessor that expects a device space vertex position
131      * attribute. The view matrix must still be provided to compute correctly transformed
132      * coordinates for GrFragmentProcessors. It may fail if the view matrix is not invertible.
133      */
134     sk_sp<GrGeometryProcessor> MakeForDeviceSpace(const Color&,
135                                                   const Coverage&,
136                                                   const LocalCoords&,
137                                                   const SkMatrix& viewMatrix);
138 };
139 
140 #endif
141