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 | |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 8 | #ifndef GrProcessorAnalysis_DEFINED |
| 9 | #define GrProcessorAnalysis_DEFINED |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 10 | |
Brian Osman | e3f543c | 2018-11-02 12:52:10 -0400 | [diff] [blame] | 11 | #include "SkColorData.h" |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 12 | |
| 13 | class GrDrawOp; |
| 14 | class GrFragmentProcessor; |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 15 | |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 16 | class GrProcessorAnalysisColor { |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 17 | public: |
| 18 | enum class Opaque { |
| 19 | kNo, |
| 20 | kYes, |
| 21 | }; |
| 22 | |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 23 | constexpr GrProcessorAnalysisColor(Opaque opaque = Opaque::kNo) |
Brian Osman | 1be2b7c | 2018-10-29 16:07:15 -0400 | [diff] [blame] | 24 | : fFlags(opaque == Opaque::kYes ? kIsOpaque_Flag : 0) |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 25 | , fColor(SK_PMColor4fTRANSPARENT) {} |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 26 | |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 27 | GrProcessorAnalysisColor(const SkPMColor4f& color) { this->setToConstant(color); } |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 28 | |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 29 | void setToConstant(const SkPMColor4f& color) { |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 30 | fColor = color; |
Brian Osman | 1be2b7c | 2018-10-29 16:07:15 -0400 | [diff] [blame] | 31 | if (color.isOpaque()) { |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 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 | |
Michael Ludwig | ca91e1f | 2018-12-10 10:44:44 -0500 | [diff] [blame] | 42 | bool isUnknown() const { return SkToBool(fFlags == 0); } |
| 43 | |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 44 | bool isOpaque() const { return SkToBool(kIsOpaque_Flag & fFlags); } |
| 45 | |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 46 | bool isConstant(SkPMColor4f* color = nullptr) const { |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 47 | if (kColorIsKnown_Flag & fFlags) { |
Brian Salomon | 3185384 | 2017-03-28 16:32:05 -0400 | [diff] [blame] | 48 | if (color) { |
| 49 | *color = fColor; |
| 50 | } |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 51 | return true; |
| 52 | } |
| 53 | return false; |
| 54 | } |
| 55 | |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 56 | bool operator==(const GrProcessorAnalysisColor& that) const { |
Brian Salomon | 1c6025c | 2017-03-29 14:25:04 -0400 | [diff] [blame] | 57 | if (fFlags != that.fFlags) { |
| 58 | return false; |
| 59 | } |
| 60 | return (kColorIsKnown_Flag & fFlags) ? fColor == that.fColor : true; |
| 61 | } |
| 62 | |
Brian Salomon | 13071c5 | 2017-03-29 21:28:20 -0400 | [diff] [blame] | 63 | /** The returned value reflects the common properties of the two inputs. */ |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 64 | static GrProcessorAnalysisColor Combine(const GrProcessorAnalysisColor& a, |
| 65 | const GrProcessorAnalysisColor& b) { |
| 66 | GrProcessorAnalysisColor result; |
Brian Salomon | 13071c5 | 2017-03-29 21:28:20 -0400 | [diff] [blame] | 67 | uint32_t commonFlags = a.fFlags & b.fFlags; |
| 68 | if ((kColorIsKnown_Flag & commonFlags) && a.fColor == b.fColor) { |
| 69 | result.fColor = a.fColor; |
| 70 | result.fFlags = a.fFlags; |
| 71 | } else if (kIsOpaque_Flag & commonFlags) { |
| 72 | result.fFlags = kIsOpaque_Flag; |
| 73 | } |
| 74 | return result; |
| 75 | } |
| 76 | |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 77 | private: |
| 78 | enum Flags { |
| 79 | kColorIsKnown_Flag = 0x1, |
| 80 | kIsOpaque_Flag = 0x2, |
| 81 | }; |
| 82 | uint32_t fFlags; |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 83 | SkPMColor4f fColor; |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 84 | }; |
| 85 | |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 86 | enum class GrProcessorAnalysisCoverage { kNone, kSingleChannel, kLCD }; |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * GrColorFragmentProcessorAnalysis gathers invariant data from a set of color fragment processor. |
| 90 | * It is used to recognize optimizations that can simplify the generated shader or make blending |
| 91 | * more effecient. |
| 92 | */ |
| 93 | class GrColorFragmentProcessorAnalysis { |
| 94 | public: |
Brian Salomon | 650ced0 | 2017-07-20 16:46:46 -0400 | [diff] [blame] | 95 | GrColorFragmentProcessorAnalysis() = delete; |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 96 | |
Brian Salomon | 650ced0 | 2017-07-20 16:46:46 -0400 | [diff] [blame] | 97 | GrColorFragmentProcessorAnalysis(const GrProcessorAnalysisColor& input, |
| 98 | const GrFragmentProcessor* const* processors, |
| 99 | int cnt); |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 100 | |
| 101 | bool isOpaque() const { return fIsOpaque; } |
| 102 | |
| 103 | /** |
| 104 | * Are all the fragment processors compatible with conflating coverage with color prior to the |
Brian Salomon | 28207df | 2017-06-05 12:25:13 -0400 | [diff] [blame] | 105 | * the first fragment processor. This result assumes that processors that should be eliminated |
| 106 | * as indicated by initialProcessorsToEliminate() are in fact eliminated. |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 107 | */ |
| 108 | bool allProcessorsCompatibleWithCoverageAsAlpha() const { |
Brian Salomon | 650ced0 | 2017-07-20 16:46:46 -0400 | [diff] [blame] | 109 | return fCompatibleWithCoverageAsAlpha; |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
Brian Salomon | 28207df | 2017-06-05 12:25:13 -0400 | [diff] [blame] | 113 | * Do any of the fragment processors require local coords. This result assumes that |
| 114 | * processors that should be eliminated as indicated by initialProcessorsToEliminate() are in |
| 115 | * fact eliminated. |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 116 | */ |
| 117 | bool usesLocalCoords() const { return fUsesLocalCoords; } |
| 118 | |
| 119 | /** |
| 120 | * If we detected that the result after the first N processors is a known color then we |
| 121 | * eliminate those N processors and replace the GrDrawOp's color input to the GrPipeline with |
| 122 | * the known output of the Nth processor, so that the Nth+1 fragment processor (or the XP if |
| 123 | * there are only N processors) sees its expected input. If this returns 0 then there are no |
| 124 | * processors to eliminate. |
| 125 | */ |
Brian Osman | 1be2b7c | 2018-10-29 16:07:15 -0400 | [diff] [blame] | 126 | int initialProcessorsToEliminate(SkPMColor4f* newPipelineInputColor) const { |
Brian Salomon | 650ced0 | 2017-07-20 16:46:46 -0400 | [diff] [blame] | 127 | if (fProcessorsToEliminate > 0) { |
Brian Osman | 1be2b7c | 2018-10-29 16:07:15 -0400 | [diff] [blame] | 128 | *newPipelineInputColor = fLastKnownOutputColor; |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 129 | } |
Brian Salomon | 650ced0 | 2017-07-20 16:46:46 -0400 | [diff] [blame] | 130 | return fProcessorsToEliminate; |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 131 | } |
| 132 | |
Brian Salomon | 650ced0 | 2017-07-20 16:46:46 -0400 | [diff] [blame] | 133 | /** |
| 134 | * Provides known information about the last processor's output color. |
| 135 | */ |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 136 | GrProcessorAnalysisColor outputColor() const { |
Brian Salomon | 650ced0 | 2017-07-20 16:46:46 -0400 | [diff] [blame] | 137 | if (fKnowOutputColor) { |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 138 | return fLastKnownOutputColor; |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 139 | } |
Brian Salomon | 650ced0 | 2017-07-20 16:46:46 -0400 | [diff] [blame] | 140 | return fIsOpaque ? GrProcessorAnalysisColor::Opaque::kYes |
| 141 | : GrProcessorAnalysisColor::Opaque::kNo; |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | private: |
Brian Salomon | 650ced0 | 2017-07-20 16:46:46 -0400 | [diff] [blame] | 145 | bool fIsOpaque; |
| 146 | bool fCompatibleWithCoverageAsAlpha; |
| 147 | bool fUsesLocalCoords; |
| 148 | bool fKnowOutputColor; |
| 149 | int fProcessorsToEliminate; |
Brian Osman | 1d5b598 | 2018-10-01 13:41:39 -0400 | [diff] [blame] | 150 | SkPMColor4f fLastKnownOutputColor; |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | #endif |