1b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel/*
2b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel * Copyright 2014 Google Inc.
3b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel *
4b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel * Use of this source code is governed by a BSD-style license that can be
5b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel * found in the LICENSE file.
6b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel */
7b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel
8b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel#include "GrProcOptInfo.h"
9b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel
104d8da81562852e0ff7e18b66ee1cebd50ad81ee8joshualitt#include "GrBatch.h"
119513143efa734bef0c1a0c7f945022572dbc8518egdaniel#include "GrFragmentProcessor.h"
128d95ffa497091d0c9c7cda099684c7bca6714a17egdaniel#include "GrFragmentStage.h"
139513143efa734bef0c1a0c7f945022572dbc8518egdaniel#include "GrGeometryProcessor.h"
14b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel
154d8da81562852e0ff7e18b66ee1cebd50ad81ee8joshualittvoid GrProcOptInfo::calcColorWithBatch(const GrBatch* batch,
164d8da81562852e0ff7e18b66ee1cebd50ad81ee8joshualitt                                       const GrFragmentStage* stages,
174d8da81562852e0ff7e18b66ee1cebd50ad81ee8joshualitt                                       int stageCount) {
184d8da81562852e0ff7e18b66ee1cebd50ad81ee8joshualitt    GrInitInvariantOutput out;
194d8da81562852e0ff7e18b66ee1cebd50ad81ee8joshualitt    batch->getInvariantOutputColor(&out);
204d8da81562852e0ff7e18b66ee1cebd50ad81ee8joshualitt    fInOut.reset(out);
214d8da81562852e0ff7e18b66ee1cebd50ad81ee8joshualitt    this->internalCalc(stages, stageCount, batch->willReadFragmentPosition());
224d8da81562852e0ff7e18b66ee1cebd50ad81ee8joshualitt}
234d8da81562852e0ff7e18b66ee1cebd50ad81ee8joshualitt
244d8da81562852e0ff7e18b66ee1cebd50ad81ee8joshualittvoid GrProcOptInfo::calcCoverageWithBatch(const GrBatch* batch,
254d8da81562852e0ff7e18b66ee1cebd50ad81ee8joshualitt                                          const GrFragmentStage* stages,
264d8da81562852e0ff7e18b66ee1cebd50ad81ee8joshualitt                                          int stageCount) {
274d8da81562852e0ff7e18b66ee1cebd50ad81ee8joshualitt    GrInitInvariantOutput out;
284d8da81562852e0ff7e18b66ee1cebd50ad81ee8joshualitt    batch->getInvariantOutputCoverage(&out);
294d8da81562852e0ff7e18b66ee1cebd50ad81ee8joshualitt    fInOut.reset(out);
304d8da81562852e0ff7e18b66ee1cebd50ad81ee8joshualitt    this->internalCalc(stages, stageCount, batch->willReadFragmentPosition());
314d8da81562852e0ff7e18b66ee1cebd50ad81ee8joshualitt}
324d8da81562852e0ff7e18b66ee1cebd50ad81ee8joshualitt
3356995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualittvoid GrProcOptInfo::calcColorWithPrimProc(const GrPrimitiveProcessor* primProc,
3456995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt                                          const GrFragmentStage* stages,
3556995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt                                          int stageCount) {
3656995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt    GrInitInvariantOutput out;
3756995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt    primProc->getInvariantOutputColor(&out);
3856995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt    fInOut.reset(out);
3956995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt    this->internalCalc(stages, stageCount, primProc->willReadFragmentPosition());
4056995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt}
4156995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt
4256995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualittvoid GrProcOptInfo::calcCoverageWithPrimProc(const GrPrimitiveProcessor* primProc,
4356995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt                                             const GrFragmentStage* stages,
4456995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt                                             int stageCount) {
4556995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt    GrInitInvariantOutput out;
4656995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt    primProc->getInvariantOutputCoverage(&out);
4756995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt    fInOut.reset(out);
4856995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt    this->internalCalc(stages, stageCount, primProc->willReadFragmentPosition());
4956995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt}
5056995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt
51b6cbc38702dc4a1540a17181cbdab71060c92ce7egdanielvoid GrProcOptInfo::calcWithInitialValues(const GrFragmentStage* stages,
52b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel                                          int stageCount,
53b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel                                          GrColor startColor,
54b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel                                          GrColorComponentFlags flags,
5556995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt                                          bool areCoverageStages) {
5656995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt    GrInitInvariantOutput out;
5756995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt    out.fIsSingleComponent = areCoverageStages;
5856995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt    out.fColor = startColor;
5956995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt    out.fValidFlags = flags;
6056995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt    fInOut.reset(out);
6156995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt    this->internalCalc(stages, stageCount, false);
6256995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt}
6356995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt
6456995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualittvoid GrProcOptInfo::internalCalc(const GrFragmentStage* stages,
6556995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt                                 int stageCount,
6656995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt                                 bool initWillReadFragmentPosition) {
67b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel    fFirstEffectStageIndex = 0;
68b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel    fInputColorIsUsed = true;
6956995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt    fInputColor = fInOut.color();
7056995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt    fReadsFragPosition = initWillReadFragmentPosition;
71b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel
72b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel    for (int i = 0; i < stageCount; ++i) {
7340d4bd8dafe9ed844f29a3de3d8965672248ee73joshualitt        const GrFragmentProcessor* processor = stages[i].processor();
74b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel        fInOut.resetWillUseInputColor();
75b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel        processor->computeInvariantOutput(&fInOut);
7656995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt        SkDEBUGCODE(fInOut.validate());
77b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel        if (!fInOut.willUseInputColor()) {
78b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel            fFirstEffectStageIndex = i;
79b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel            fInputColorIsUsed = false;
809513143efa734bef0c1a0c7f945022572dbc8518egdaniel            // Reset these since we don't care if previous stages read these values
8156995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt            fReadsFragPosition = initWillReadFragmentPosition;
82b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel        }
839513143efa734bef0c1a0c7f945022572dbc8518egdaniel        if (processor->willReadFragmentPosition()) {
849513143efa734bef0c1a0c7f945022572dbc8518egdaniel            fReadsFragPosition = true;
859513143efa734bef0c1a0c7f945022572dbc8518egdaniel        }
86b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel        if (kRGBA_GrColorComponentFlags == fInOut.validFlags()) {
87b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel            fFirstEffectStageIndex = i + 1;
88b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel            fInputColor = fInOut.color();
89b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel            fInputColorIsUsed = true;
90b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel            // Since we are clearing all previous color stages we are in a state where we have found
91b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel            // zero stages that don't multiply the inputColor.
92b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel            fInOut.resetNonMulStageFound();
939513143efa734bef0c1a0c7f945022572dbc8518egdaniel            // Reset these since we don't care if previous stages read these values
9456995b5cc00c9c83bd5fcf86bca9a67e939a96cbjoshualitt            fReadsFragPosition = initWillReadFragmentPosition;
95b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel        }
96b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel    }
97b6cbc38702dc4a1540a17181cbdab71060c92ce7egdaniel}
98