Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [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 GrPipelineAnalysis_DEFINED |
| 9 | #define GrPipelineAnalysis_DEFINED |
| 10 | |
| 11 | #include "GrColor.h" |
| 12 | |
| 13 | class GrDrawOp; |
| 14 | class GrFragmentProcessor; |
| 15 | class GrPrimitiveProcessor; |
| 16 | |
| 17 | class GrPipelineAnalysisColor { |
| 18 | public: |
| 19 | enum class Opaque { |
| 20 | kNo, |
| 21 | kYes, |
| 22 | }; |
| 23 | |
| 24 | GrPipelineAnalysisColor(Opaque opaque = Opaque::kNo) |
| 25 | : fFlags(opaque == Opaque::kYes ? kIsOpaque_Flag : 0) {} |
| 26 | |
| 27 | GrPipelineAnalysisColor(GrColor color) { this->setToConstant(color); } |
| 28 | |
| 29 | void setToConstant(GrColor color) { |
| 30 | fColor = color; |
| 31 | if (GrColorIsOpaque(color)) { |
| 32 | fFlags = kColorIsKnown_Flag | kIsOpaque_Flag; |
| 33 | } else { |
| 34 | fFlags = kColorIsKnown_Flag; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | void setToUnknown() { fFlags = 0; } |
| 39 | |
| 40 | void setToUnknownOpaque() { fFlags = kIsOpaque_Flag; } |
| 41 | |
| 42 | bool isOpaque() const { return SkToBool(kIsOpaque_Flag & fFlags); } |
| 43 | |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 44 | bool isConstant(GrColor* color = nullptr) const { |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 45 | if (kColorIsKnown_Flag & fFlags) { |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 46 | if (color) { |
| 47 | *color = fColor; |
| 48 | } |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 49 | return true; |
| 50 | } |
| 51 | return false; |
| 52 | } |
| 53 | |
Brian Salomon | 1c6025c | 2017-03-29 14:25:04 -0400 | [diff] [blame] | 54 | bool operator==(const GrPipelineAnalysisColor& that) const { |
| 55 | if (fFlags != that.fFlags) { |
| 56 | return false; |
| 57 | } |
| 58 | return (kColorIsKnown_Flag & fFlags) ? fColor == that.fColor : true; |
| 59 | } |
| 60 | |
Brian Salomon | 13071c5 | 2017-03-29 21:28:20 -0400 | [diff] [blame^] | 61 | /** The returned value reflects the common properties of the two inputs. */ |
| 62 | static GrPipelineAnalysisColor Combine(const GrPipelineAnalysisColor& a, |
| 63 | const GrPipelineAnalysisColor& b) { |
| 64 | GrPipelineAnalysisColor result; |
| 65 | uint32_t commonFlags = a.fFlags & b.fFlags; |
| 66 | if ((kColorIsKnown_Flag & commonFlags) && a.fColor == b.fColor) { |
| 67 | result.fColor = a.fColor; |
| 68 | result.fFlags = a.fFlags; |
| 69 | } else if (kIsOpaque_Flag & commonFlags) { |
| 70 | result.fFlags = kIsOpaque_Flag; |
| 71 | } |
| 72 | return result; |
| 73 | } |
| 74 | |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 75 | private: |
| 76 | enum Flags { |
| 77 | kColorIsKnown_Flag = 0x1, |
| 78 | kIsOpaque_Flag = 0x2, |
| 79 | }; |
| 80 | uint32_t fFlags; |
| 81 | GrColor fColor; |
| 82 | }; |
| 83 | |
| 84 | enum class GrPipelineAnalysisCoverage { kNone, kSingleChannel, kLCD }; |
| 85 | |
| 86 | /** |
| 87 | * GrColorFragmentProcessorAnalysis gathers invariant data from a set of color fragment processor. |
| 88 | * It is used to recognize optimizations that can simplify the generated shader or make blending |
| 89 | * more effecient. |
| 90 | */ |
| 91 | class GrColorFragmentProcessorAnalysis { |
| 92 | public: |
| 93 | GrColorFragmentProcessorAnalysis() = default; |
| 94 | |
| 95 | GrColorFragmentProcessorAnalysis(const GrPipelineAnalysisColor& input) |
| 96 | : GrColorFragmentProcessorAnalysis() { |
| 97 | fAllProcessorsCompatibleWithCoverageAsAlpha = true; |
| 98 | fIsOpaque = input.isOpaque(); |
| 99 | GrColor color; |
| 100 | if (input.isConstant(&color)) { |
| 101 | fLastKnownOutputColor = GrColor4f::FromGrColor(color); |
| 102 | fProcessorsVisitedWithKnownOutput = 0; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void reset(const GrPipelineAnalysisColor& input) { |
| 107 | *this = GrColorFragmentProcessorAnalysis(input); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Runs through a series of processors and updates calculated values. This can be called |
| 112 | * repeatedly for cases when the sequence of processors is not in a contiguous array. |
| 113 | */ |
| 114 | void analyzeProcessors(const GrFragmentProcessor* const* processors, int cnt); |
| 115 | |
| 116 | bool isOpaque() const { return fIsOpaque; } |
| 117 | |
| 118 | /** |
| 119 | * Are all the fragment processors compatible with conflating coverage with color prior to the |
| 120 | * the first fragment processor. This result does not consider processors that should be |
| 121 | * eliminated as indicated by initialProcessorsToEliminate(). |
| 122 | */ |
| 123 | bool allProcessorsCompatibleWithCoverageAsAlpha() const { |
| 124 | return fAllProcessorsCompatibleWithCoverageAsAlpha; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Do any of the fragment processors require local coords. This result does not consider |
| 129 | * processors that should be eliminated as indicated by initialProcessorsToEliminate(). |
| 130 | */ |
| 131 | bool usesLocalCoords() const { return fUsesLocalCoords; } |
| 132 | |
| 133 | /** |
| 134 | * If we detected that the result after the first N processors is a known color then we |
| 135 | * eliminate those N processors and replace the GrDrawOp's color input to the GrPipeline with |
| 136 | * the known output of the Nth processor, so that the Nth+1 fragment processor (or the XP if |
| 137 | * there are only N processors) sees its expected input. If this returns 0 then there are no |
| 138 | * processors to eliminate. |
| 139 | */ |
| 140 | int initialProcessorsToEliminate(GrColor* newPipelineInputColor) const { |
| 141 | if (fProcessorsVisitedWithKnownOutput > 0) { |
| 142 | *newPipelineInputColor = fLastKnownOutputColor.toGrColor(); |
| 143 | } |
| 144 | return SkTMax(0, fProcessorsVisitedWithKnownOutput); |
| 145 | } |
| 146 | |
| 147 | int initialProcessorsToEliminate(GrColor4f* newPipelineInputColor) const { |
| 148 | if (fProcessorsVisitedWithKnownOutput > 0) { |
| 149 | *newPipelineInputColor = fLastKnownOutputColor; |
| 150 | } |
| 151 | return SkTMax(0, fProcessorsVisitedWithKnownOutput); |
| 152 | } |
| 153 | |
| 154 | GrPipelineAnalysisColor outputColor() const { |
| 155 | if (fProcessorsVisitedWithKnownOutput != fTotalProcessorsVisited) { |
| 156 | return GrPipelineAnalysisColor(fIsOpaque ? GrPipelineAnalysisColor::Opaque::kYes |
| 157 | : GrPipelineAnalysisColor::Opaque::kNo); |
| 158 | } |
| 159 | return GrPipelineAnalysisColor(fLastKnownOutputColor.toGrColor()); |
| 160 | } |
| 161 | |
| 162 | private: |
| 163 | int fTotalProcessorsVisited = 0; |
| 164 | // negative one means even the color is unknown before adding the first processor. |
| 165 | int fProcessorsVisitedWithKnownOutput = -1; |
| 166 | bool fIsOpaque = false; |
| 167 | bool fAllProcessorsCompatibleWithCoverageAsAlpha = true; |
| 168 | bool fUsesLocalCoords = false; |
| 169 | GrColor4f fLastKnownOutputColor; |
| 170 | }; |
| 171 | |
| 172 | #endif |