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