egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [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 | #include "GrOptDrawState.h" |
| 9 | |
| 10 | #include "GrDrawState.h" |
| 11 | |
egdaniel | 8a4c103 | 2014-09-16 07:18:54 -0700 | [diff] [blame^] | 12 | GrOptDrawState::GrOptDrawState(const GrDrawState& drawState) : INHERITED(drawState) { |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 13 | fColor = drawState.getColor(); |
| 14 | fCoverage = drawState.getCoverage(); |
| 15 | fViewMatrix = drawState.getViewMatrix(); |
| 16 | fBlendConstant = drawState.getBlendConstant(); |
| 17 | fFlagBits = drawState.getFlagBits(); |
| 18 | fVAPtr = drawState.getVertexAttribs(); |
| 19 | fVACount = drawState.getVertexAttribCount(); |
| 20 | fVAStride = drawState.getVertexStride(); |
| 21 | fStencilSettings = drawState.getStencil(); |
| 22 | fDrawFace = drawState.getDrawFace(); |
egdaniel | 8a4c103 | 2014-09-16 07:18:54 -0700 | [diff] [blame^] | 23 | |
| 24 | fBlendOptFlags = drawState.getBlendOpts(false, &fSrcBlend, &fDstBlend); |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 25 | |
| 26 | memcpy(fFixedFunctionVertexAttribIndices, |
| 27 | drawState.getFixedFunctionVertexAttribIndices(), |
| 28 | sizeof(fFixedFunctionVertexAttribIndices)); |
| 29 | |
| 30 | fInputColorIsUsed = true; |
| 31 | fInputCoverageIsUsed = true; |
| 32 | |
| 33 | if (drawState.hasGeometryProcessor()) { |
| 34 | fGeometryProcessor.reset(SkNEW_ARGS(GrEffectStage, (*drawState.getGeometryProcessor()))); |
| 35 | } else { |
| 36 | fGeometryProcessor.reset(NULL); |
| 37 | } |
| 38 | |
| 39 | this->copyEffectiveColorStages(drawState); |
| 40 | this->copyEffectiveCoverageStages(drawState); |
| 41 | }; |
| 42 | |
| 43 | void GrOptDrawState::removeFixedFunctionVertexAttribs(uint8_t removeVAFlag) { |
| 44 | int numToRemove = 0; |
| 45 | uint8_t maskCheck = 0x1; |
| 46 | // Count the number of vertex attributes that we will actually remove |
| 47 | for (int i = 0; i < kGrFixedFunctionVertexAttribBindingCnt; ++i) { |
| 48 | if ((maskCheck & removeVAFlag) && -1 != fFixedFunctionVertexAttribIndices[i]) { |
| 49 | ++numToRemove; |
| 50 | } |
| 51 | maskCheck <<= 1; |
| 52 | } |
| 53 | fOptVA.reset(fVACount - numToRemove); |
| 54 | |
| 55 | GrVertexAttrib* dst = fOptVA.get(); |
| 56 | const GrVertexAttrib* src = fVAPtr; |
| 57 | |
| 58 | for (int i = 0, newIdx = 0; i < fVACount; ++i, ++src) { |
| 59 | const GrVertexAttrib& currAttrib = *src; |
| 60 | if (currAttrib.fBinding < kGrFixedFunctionVertexAttribBindingCnt) { |
| 61 | uint8_t maskCheck = 0x1 << currAttrib.fBinding; |
| 62 | if (maskCheck & removeVAFlag) { |
| 63 | SkASSERT(-1 != fFixedFunctionVertexAttribIndices[currAttrib.fBinding]); |
| 64 | fFixedFunctionVertexAttribIndices[currAttrib.fBinding] = -1; |
| 65 | continue; |
| 66 | } |
| 67 | } |
| 68 | memcpy(dst, src, sizeof(GrVertexAttrib)); |
egdaniel | 8a4c103 | 2014-09-16 07:18:54 -0700 | [diff] [blame^] | 69 | fFixedFunctionVertexAttribIndices[currAttrib.fBinding] = newIdx; |
egdaniel | 3658f38 | 2014-09-15 07:01:59 -0700 | [diff] [blame] | 70 | ++newIdx; |
| 71 | ++dst; |
| 72 | } |
| 73 | fVACount -= numToRemove; |
| 74 | fVAPtr = fOptVA.get(); |
| 75 | } |
| 76 | |
| 77 | void GrOptDrawState::copyEffectiveColorStages(const GrDrawState& ds) { |
| 78 | int firstColorStage = 0; |
| 79 | |
| 80 | // Set up color and flags for ConstantColorComponent checks |
| 81 | GrColor color; |
| 82 | uint32_t validComponentFlags; |
| 83 | if (!this->hasColorVertexAttribute()) { |
| 84 | color = ds.getColor(); |
| 85 | validComponentFlags = kRGBA_GrColorComponentFlags; |
| 86 | } else { |
| 87 | if (ds.vertexColorsAreOpaque()) { |
| 88 | color = 0xFF << GrColor_SHIFT_A; |
| 89 | validComponentFlags = kA_GrColorComponentFlag; |
| 90 | } else { |
| 91 | validComponentFlags = 0; |
| 92 | color = 0; // not strictly necessary but we get false alarms from tools about uninit. |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | for (int i = 0; i < ds.numColorStages(); ++i) { |
| 97 | const GrEffect* effect = ds.getColorStage(i).getEffect(); |
| 98 | if (!effect->willUseInputColor()) { |
| 99 | firstColorStage = i; |
| 100 | fInputColorIsUsed = false; |
| 101 | } |
| 102 | effect->getConstantColorComponents(&color, &validComponentFlags); |
| 103 | if (kRGBA_GrColorComponentFlags == validComponentFlags) { |
| 104 | firstColorStage = i + 1; |
| 105 | fColor = color; |
| 106 | fInputColorIsUsed = true; |
| 107 | this->removeFixedFunctionVertexAttribs(0x1 << kColor_GrVertexAttribBinding); |
| 108 | } |
| 109 | } |
| 110 | if (firstColorStage < ds.numColorStages()) { |
| 111 | fColorStages.reset(&ds.getColorStage(firstColorStage), |
| 112 | ds.numColorStages() - firstColorStage); |
| 113 | } else { |
| 114 | fColorStages.reset(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | void GrOptDrawState::copyEffectiveCoverageStages(const GrDrawState& ds) { |
| 119 | int firstCoverageStage = 0; |
| 120 | |
| 121 | // We do not try to optimize out constantColor coverage effects here. It is extremely rare |
| 122 | // to have a coverage effect that returns a constant value for all four channels. Thus we |
| 123 | // save having to make extra virtual calls by not checking for it. |
| 124 | |
| 125 | // Don't do any optimizations on coverage stages. It should not be the case where we do not use |
| 126 | // input coverage in an effect |
| 127 | #ifdef OptCoverageStages |
| 128 | for (int i = 0; i < ds.numCoverageStages(); ++i) { |
| 129 | const GrEffect* effect = ds.getCoverageStage(i).getEffect(); |
| 130 | if (!effect->willUseInputColor()) { |
| 131 | firstCoverageStage = i; |
| 132 | fInputCoverageIsUsed = false; |
| 133 | } |
| 134 | } |
| 135 | #endif |
| 136 | if (ds.numCoverageStages() > 0) { |
| 137 | fCoverageStages.reset(&ds.getCoverageStage(firstCoverageStage), |
| 138 | ds.numCoverageStages() - firstCoverageStage); |
| 139 | } else { |
| 140 | fCoverageStages.reset(); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | bool GrOptDrawState::operator== (const GrOptDrawState& that) const { |
| 145 | return this->isEqual(that); |
| 146 | } |
| 147 | |