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 "GrProcOptInfo.h"
9
10#include "GrGeometryProcessor.h"
11
12#include "batches/GrDrawBatch.h"
13
14void GrProcOptInfo::calcWithInitialValues(const GrFragmentProcessor * const processors[],
15                                          int cnt,
16                                          GrColor startColor,
17                                          GrColorComponentFlags flags,
18                                          bool areCoverageStages,
19                                          bool isLCD) {
20    GrInitInvariantOutput out;
21    out.fIsSingleComponent = areCoverageStages;
22    out.fColor = startColor;
23    out.fValidFlags = flags;
24    out.fIsLCDCoverage = isLCD;
25    fInOut.reset(out);
26    this->internalCalc(processors, cnt, false);
27}
28
29void GrProcOptInfo::initUsingInvariantOutput(GrInitInvariantOutput invOutput) {
30    fInOut.reset(invOutput);
31}
32
33void GrProcOptInfo::completeCalculations(const GrFragmentProcessor * const processors[], int cnt) {
34    this->internalCalc(processors, cnt, false);
35}
36
37void GrProcOptInfo::internalCalc(const GrFragmentProcessor* const processors[],
38                                 int cnt,
39                                 bool initWillReadFragmentPosition) {
40    fFirstEffectiveProcessorIndex = 0;
41    fInputColorIsUsed = true;
42    fInputColor = fInOut.color();
43    fReadsFragPosition = initWillReadFragmentPosition;
44
45    for (int i = 0; i < cnt; ++i) {
46        const GrFragmentProcessor* processor = processors[i];
47        fInOut.resetWillUseInputColor();
48        processor->computeInvariantOutput(&fInOut);
49        SkDEBUGCODE(fInOut.validate());
50        if (!fInOut.willUseInputColor()) {
51            fFirstEffectiveProcessorIndex = i;
52            fInputColorIsUsed = false;
53            // Reset these since we don't care if previous stages read these values
54            fReadsFragPosition = initWillReadFragmentPosition;
55        }
56        if (processor->willReadFragmentPosition()) {
57            fReadsFragPosition = true;
58        }
59        if (kRGBA_GrColorComponentFlags == fInOut.validFlags()) {
60            fFirstEffectiveProcessorIndex = i + 1;
61            fInputColor = fInOut.color();
62            fInputColorIsUsed = true;
63            // Since we are clearing all previous color stages we are in a state where we have found
64            // zero stages that don't multiply the inputColor.
65            fInOut.resetNonMulStageFound();
66            // Reset these since we don't care if previous stages read these values
67            fReadsFragPosition = initWillReadFragmentPosition;
68        }
69    }
70}
71