joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 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 "GrGeometryProcessor.h" |
| 12 | |
| 13 | class GrDrawState; |
| 14 | |
| 15 | /* |
| 16 | * A factory for creating default Geometry Processors which simply multiply position by the uniform |
| 17 | * view matrix and wire through color, coverage, UV coords if requested. Right now this is only |
| 18 | * used in the creation of optimized draw states because adding default GPs to the drawstate can |
| 19 | * interfere with batching due to updating the drawstate. |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 20 | */ |
joshualitt | 7a0d697 | 2015-07-28 10:20:08 -0700 | [diff] [blame] | 21 | namespace GrDefaultGeoProcFactory { |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 22 | // Structs for adding vertex attributes |
| 23 | struct PositionAttr { |
| 24 | SkPoint fPosition; |
| 25 | }; |
| 26 | |
| 27 | struct PositionCoverageAttr { |
| 28 | SkPoint fPosition; |
| 29 | GrColor fCoverage; |
| 30 | }; |
| 31 | |
| 32 | struct PositionColorAttr { |
| 33 | SkPoint fPosition; |
| 34 | SkColor fColor; |
| 35 | }; |
| 36 | |
| 37 | struct PositionColorCoverageAttr { |
| 38 | SkPoint fPosition; |
| 39 | SkColor fColor; |
| 40 | GrColor fCoverage; |
| 41 | }; |
| 42 | |
| 43 | struct PositionLocalCoordAttr { |
| 44 | SkPoint fPosition; |
| 45 | SkPoint fLocalCoord; |
| 46 | }; |
| 47 | |
| 48 | struct PositionLocalCoordCoverageAttr { |
| 49 | SkPoint fPosition; |
| 50 | SkPoint fLocalCoord; |
| 51 | GrColor fCoverage; |
| 52 | }; |
| 53 | |
| 54 | struct PositionColorLocalCoordAttr { |
| 55 | SkPoint fPosition; |
| 56 | GrColor fColor; |
| 57 | SkPoint fLocalCoord; |
| 58 | }; |
| 59 | |
| 60 | struct PositionColorLocalCoordCoverage { |
| 61 | SkPoint fPosition; |
| 62 | GrColor fColor; |
| 63 | SkPoint fLocalCoord; |
| 64 | GrColor fCoverage; |
| 65 | }; |
| 66 | |
joshualitt | e9d6095 | 2015-07-27 12:13:14 -0700 | [diff] [blame] | 67 | struct Color { |
| 68 | enum Type { |
| 69 | kNone_Type, |
| 70 | kUniform_Type, |
| 71 | kAttribute_Type, |
| 72 | }; |
| 73 | Color(GrColor color) : fType(kUniform_Type), fColor(color) {} |
| 74 | Color(Type type) : fType(type), fColor(GrColor_ILLEGAL) { |
| 75 | SkASSERT(type != kUniform_Type); |
| 76 | |
| 77 | // TODO This is temporary |
| 78 | if (kAttribute_Type == type) { |
| 79 | fColor = GrColor_WHITE; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | Type fType; |
| 84 | GrColor fColor; |
| 85 | }; |
| 86 | |
| 87 | struct Coverage { |
| 88 | enum Type { |
| 89 | kNone_Type, |
| 90 | kSolid_Type, |
| 91 | kUniform_Type, |
| 92 | kAttribute_Type, |
| 93 | }; |
| 94 | Coverage(uint8_t coverage) : fType(kUniform_Type), fCoverage(coverage) {} |
| 95 | Coverage(Type type) : fType(type), fCoverage(0xff) { |
| 96 | SkASSERT(type != kUniform_Type); |
| 97 | } |
| 98 | |
| 99 | Type fType; |
| 100 | uint8_t fCoverage; |
| 101 | }; |
| 102 | |
| 103 | struct LocalCoords { |
| 104 | enum Type { |
joshualitt | 0d986d8 | 2015-07-28 10:01:18 -0700 | [diff] [blame] | 105 | kUnused_Type, |
joshualitt | e9d6095 | 2015-07-27 12:13:14 -0700 | [diff] [blame] | 106 | kUsePosition_Type, |
| 107 | kHasExplicit_Type, |
joshualitt | b2aa7cb | 2015-08-05 11:05:22 -0700 | [diff] [blame] | 108 | kHasTransformed_Type, |
joshualitt | e9d6095 | 2015-07-27 12:13:14 -0700 | [diff] [blame] | 109 | }; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 110 | LocalCoords(Type type) : fType(type), fMatrix(nullptr) {} |
joshualitt | 0d986d8 | 2015-07-28 10:01:18 -0700 | [diff] [blame] | 111 | LocalCoords(Type type, const SkMatrix* matrix) : fType(type), fMatrix(matrix) { |
| 112 | SkASSERT(kUnused_Type != type); |
| 113 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 114 | bool hasLocalMatrix() const { return nullptr != fMatrix; } |
joshualitt | 0d986d8 | 2015-07-28 10:01:18 -0700 | [diff] [blame] | 115 | |
| 116 | Type fType; |
| 117 | const SkMatrix* fMatrix; |
joshualitt | e9d6095 | 2015-07-27 12:13:14 -0700 | [diff] [blame] | 118 | }; |
| 119 | |
joshualitt | 7a0d697 | 2015-07-28 10:20:08 -0700 | [diff] [blame] | 120 | const GrGeometryProcessor* Create(const Color&, |
| 121 | const Coverage&, |
| 122 | const LocalCoords&, |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 123 | const SkMatrix& viewMatrix); |
joshualitt | e9d6095 | 2015-07-27 12:13:14 -0700 | [diff] [blame] | 124 | |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 125 | /* |
joshualitt | 0d986d8 | 2015-07-28 10:01:18 -0700 | [diff] [blame] | 126 | * Use this factory to create a GrGeometryProcessor that expects a device space vertex position |
| 127 | * attribute. The view matrix must still be provided to compute correctly transformed |
| 128 | * coordinates for GrFragmentProcessors. It may fail if the view matrix is not invertible. |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 129 | */ |
joshualitt | 7a0d697 | 2015-07-28 10:20:08 -0700 | [diff] [blame] | 130 | const GrGeometryProcessor* CreateForDeviceSpace(const Color&, |
| 131 | const Coverage&, |
| 132 | const LocalCoords&, |
| 133 | const SkMatrix& viewMatrix); |
joshualitt | 0d986d8 | 2015-07-28 10:01:18 -0700 | [diff] [blame] | 134 | |
joshualitt | 7a0d697 | 2015-07-28 10:20:08 -0700 | [diff] [blame] | 135 | inline size_t DefaultVertexStride() { return sizeof(PositionAttr); } |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | #endif |