18072caa80384292858d31ae34b7e19768875866bjoshualitt/*
28072caa80384292858d31ae34b7e19768875866bjoshualitt * Copyright 2013 Google Inc.
38072caa80384292858d31ae34b7e19768875866bjoshualitt *
48072caa80384292858d31ae34b7e19768875866bjoshualitt * Use of this source code is governed by a BSD-style license that can be
58072caa80384292858d31ae34b7e19768875866bjoshualitt * found in the LICENSE file.
68072caa80384292858d31ae34b7e19768875866bjoshualitt */
78072caa80384292858d31ae34b7e19768875866bjoshualitt
88072caa80384292858d31ae34b7e19768875866bjoshualitt#ifndef GrPrimitiveProcessor_DEFINED
98072caa80384292858d31ae34b7e19768875866bjoshualitt#define GrPrimitiveProcessor_DEFINED
108072caa80384292858d31ae34b7e19768875866bjoshualitt
118072caa80384292858d31ae34b7e19768875866bjoshualitt#include "GrColor.h"
128072caa80384292858d31ae34b7e19768875866bjoshualitt#include "GrProcessor.h"
138072caa80384292858d31ae34b7e19768875866bjoshualitt#include "GrShaderVar.h"
148072caa80384292858d31ae34b7e19768875866bjoshualitt
158072caa80384292858d31ae34b7e19768875866bjoshualitt/*
168072caa80384292858d31ae34b7e19768875866bjoshualitt * The GrPrimitiveProcessor represents some kind of geometric primitive.  This includes the shape
178072caa80384292858d31ae34b7e19768875866bjoshualitt * of the primitive and the inherent color of the primitive.  The GrPrimitiveProcessor is
188072caa80384292858d31ae34b7e19768875866bjoshualitt * responsible for providing a color and coverage input into the Ganesh rendering pipeline.  Through
198072caa80384292858d31ae34b7e19768875866bjoshualitt * optimization, Ganesh may decide a different color, no color, and / or no coverage are required
208072caa80384292858d31ae34b7e19768875866bjoshualitt * from the GrPrimitiveProcessor, so the GrPrimitiveProcessor must be able to support this
2109d994ecb30de2e62a31af2c16307af31fe0e0b3Brian Salomon * functionality.
228072caa80384292858d31ae34b7e19768875866bjoshualitt *
238072caa80384292858d31ae34b7e19768875866bjoshualitt * There are two feedback loops between the GrFragmentProcessors, the GrXferProcessor, and the
24cb30bb2cb727e276792812c6390547dba474c831Brian Salomon * GrPrimitiveProcessor. These loops run on the CPU and to determine known properties of the final
25cb30bb2cb727e276792812c6390547dba474c831Brian Salomon * color and coverage inputs to the GrXferProcessor in order to perform optimizations that preserve
26cb30bb2cb727e276792812c6390547dba474c831Brian Salomon * correctness. The GrDrawOp seeds these loops with initial color and coverage, in its
275298dc8bf30f580f551d130346c007efaf4b2098Brian Salomon * getFragmentProcessorAnalysisInputs implementation. These seed values are processed by the
285298dc8bf30f580f551d130346c007efaf4b2098Brian Salomon * subsequent
2992aee3d6857386f2b5b8e1148e680a7b58e9b1fcBrian Salomon * stages of the rendering pipeline and the output is then fed back into the GrDrawOp in
3092aee3d6857386f2b5b8e1148e680a7b58e9b1fcBrian Salomon * the applyPipelineOptimizations call, where the op can use the information to inform decisions
3192aee3d6857386f2b5b8e1148e680a7b58e9b1fcBrian Salomon * about GrPrimitiveProcessor creation.
328072caa80384292858d31ae34b7e19768875866bjoshualitt */
338072caa80384292858d31ae34b7e19768875866bjoshualitt
34e659a581f63fdccb64dce2dc8a478cf56831feeaegdanielclass GrGLSLPrimitiveProcessor;
358072caa80384292858d31ae34b7e19768875866bjoshualitt
368072caa80384292858d31ae34b7e19768875866bjoshualittstruct GrInitInvariantOutput;
378072caa80384292858d31ae34b7e19768875866bjoshualitt
388072caa80384292858d31ae34b7e19768875866bjoshualitt/*
3992aee3d6857386f2b5b8e1148e680a7b58e9b1fcBrian Salomon * This class allows the GrPipeline to communicate information about the pipeline to a GrOp which
4092aee3d6857386f2b5b8e1148e680a7b58e9b1fcBrian Salomon * inform its decisions for GrPrimitiveProcessor setup. These are not properly part of the pipeline
4192aee3d6857386f2b5b8e1148e680a7b58e9b1fcBrian Salomon * because they reflect the specific inputs that the op provided to perform the analysis (e.g. that
4292aee3d6857386f2b5b8e1148e680a7b58e9b1fcBrian Salomon * the GrGeometryProcessor would output an opaque color).
4392aee3d6857386f2b5b8e1148e680a7b58e9b1fcBrian Salomon *
4492aee3d6857386f2b5b8e1148e680a7b58e9b1fcBrian Salomon * The pipeline analysis that produced this may have decided to elide some GrProcessors. However,
4592aee3d6857386f2b5b8e1148e680a7b58e9b1fcBrian Salomon * those elisions may depend upon changing the color output by the GrGeometryProcessor used by the
4692aee3d6857386f2b5b8e1148e680a7b58e9b1fcBrian Salomon * GrDrawOp. The op must check getOverrideColorIfSet() for this.
478072caa80384292858d31ae34b7e19768875866bjoshualitt */
4892aee3d6857386f2b5b8e1148e680a7b58e9b1fcBrian Salomonclass GrPipelineOptimizations {
497765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomonpublic:
507765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon    /** Does the pipeline require access to (implicit or explicit) local coordinates? */
517765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon    bool readsLocalCoords() const {
52c699873ac7c6b21bbca96053cdb9720c80f69916bsalomon        return SkToBool(kReadsLocalCoords_Flag & fFlags);
537765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon    }
547765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon
557765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon    /** Does the pipeline allow the GrPrimitiveProcessor to combine color and coverage into one
567765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon        color output ? */
577765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon    bool canTweakAlphaForCoverage() const {
58c699873ac7c6b21bbca96053cdb9720c80f69916bsalomon        return SkToBool(kCanTweakAlphaForCoverage_Flag & fFlags);
597765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon    }
607765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon
617765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon    /** Does the pipeline require the GrPrimitiveProcessor to specify a specific color (and if
627765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon        so get the color)? */
637765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon    bool getOverrideColorIfSet(GrColor* overrideColor) const {
64c699873ac7c6b21bbca96053cdb9720c80f69916bsalomon        if (SkToBool(kUseOverrideColor_Flag & fFlags)) {
657765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon            if (overrideColor) {
667765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon                *overrideColor = fOverrideColor;
677765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon            }
687765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon            return true;
697765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon        }
707765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon        return false;
717765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon    }
727765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon
737765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomonprivate:
747765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon    enum {
757765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon        // If this is not set the primitive processor need not produce local coordinates
76bfd5183b9e039b50fb33441d1f90130b8eced80aBrian Salomon        kReadsLocalCoords_Flag = 0x1,
777765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon
787765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon        // If this flag is set then the primitive processor may produce color*coverage as
797765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon        // its color output (and not output a separate coverage).
80bfd5183b9e039b50fb33441d1f90130b8eced80aBrian Salomon        kCanTweakAlphaForCoverage_Flag = 0x2,
817765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon
827765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon        // If this flag is set the GrPrimitiveProcessor must produce fOverrideColor as its
837765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon        // output color. If not set fOverrideColor is to be ignored.
84bfd5183b9e039b50fb33441d1f90130b8eced80aBrian Salomon        kUseOverrideColor_Flag = 0x4,
857765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon    };
867765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon
877765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon    uint32_t    fFlags;
887765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon    GrColor     fOverrideColor;
897765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon
907765a477ee3ca5e2c6ada1e16c31dfaae2079240bsalomon    friend class GrPipeline; // To initialize this
918072caa80384292858d31ae34b7e19768875866bjoshualitt};
928072caa80384292858d31ae34b7e19768875866bjoshualitt
938072caa80384292858d31ae34b7e19768875866bjoshualitt/*
948072caa80384292858d31ae34b7e19768875866bjoshualitt * GrPrimitiveProcessor defines an interface which all subclasses must implement.  All
958072caa80384292858d31ae34b7e19768875866bjoshualitt * GrPrimitiveProcessors must proivide seed color and coverage for the Ganesh color / coverage
968072caa80384292858d31ae34b7e19768875866bjoshualitt * pipelines, and they must provide some notion of equality
978072caa80384292858d31ae34b7e19768875866bjoshualitt */
988072caa80384292858d31ae34b7e19768875866bjoshualittclass GrPrimitiveProcessor : public GrProcessor {
998072caa80384292858d31ae34b7e19768875866bjoshualittpublic:
1008072caa80384292858d31ae34b7e19768875866bjoshualitt    // Only the GrGeometryProcessor subclass actually has a geo shader or vertex attributes, but
1018072caa80384292858d31ae34b7e19768875866bjoshualitt    // we put these calls on the base class to prevent having to cast
1028072caa80384292858d31ae34b7e19768875866bjoshualitt    virtual bool willUseGeoShader() const = 0;
1038072caa80384292858d31ae34b7e19768875866bjoshualitt
1048072caa80384292858d31ae34b7e19768875866bjoshualitt    struct Attribute {
1058072caa80384292858d31ae34b7e19768875866bjoshualitt        Attribute()
10696fcdcc219d2a0d3579719b84b28bede76efba64halcanary            : fName(nullptr)
1078072caa80384292858d31ae34b7e19768875866bjoshualitt            , fType(kFloat_GrVertexAttribType)
1088072caa80384292858d31ae34b7e19768875866bjoshualitt            , fOffset(0) {}
1096cb807bf99ac0f8f166e1790f91bcb3afbfb5458bsalomon        Attribute(const char* name, GrVertexAttribType type, GrSLPrecision precision)
1108072caa80384292858d31ae34b7e19768875866bjoshualitt            : fName(name)
1118072caa80384292858d31ae34b7e19768875866bjoshualitt            , fType(type)
112f2539d50f911914af0f80f0092ff8c654869e650senorblanco            , fOffset(SkAlign4(GrVertexAttribTypeSize(type)))
113f2539d50f911914af0f80f0092ff8c654869e650senorblanco            , fPrecision(precision) {}
1148072caa80384292858d31ae34b7e19768875866bjoshualitt        const char* fName;
1158072caa80384292858d31ae34b7e19768875866bjoshualitt        GrVertexAttribType fType;
1168072caa80384292858d31ae34b7e19768875866bjoshualitt        size_t fOffset;
117f2539d50f911914af0f80f0092ff8c654869e650senorblanco        GrSLPrecision fPrecision;
1188072caa80384292858d31ae34b7e19768875866bjoshualitt    };
1198072caa80384292858d31ae34b7e19768875866bjoshualitt
1207dbd45d2c7427d2c679d6507435d2f0220bf64efbsalomon    int numAttribs() const { return fAttribs.count(); }
1217dbd45d2c7427d2c679d6507435d2f0220bf64efbsalomon    const Attribute& getAttrib(int index) const { return fAttribs[index]; }
1228072caa80384292858d31ae34b7e19768875866bjoshualitt
1238072caa80384292858d31ae34b7e19768875866bjoshualitt    // Returns the vertex stride of the GP.  A common use case is to request geometry from a
124f2361d2d93c200cd4555b5e8ecea4531801abaaaRobert Phillips    // GrOpList based off of the stride, and to populate this memory using an implicit array of
1258072caa80384292858d31ae34b7e19768875866bjoshualitt    // structs.  In this case, it is best to assert the vertexstride == sizeof(VertexStruct).
1268072caa80384292858d31ae34b7e19768875866bjoshualitt    size_t getVertexStride() const { return fVertexStride; }
1278072caa80384292858d31ae34b7e19768875866bjoshualitt
1288072caa80384292858d31ae34b7e19768875866bjoshualitt    /**
129a7f4c435bc1dcd845990a5515828bbe8cccfab41wangyix     * Computes a transformKey from an array of coord transforms. Will only look at the first
130a7f4c435bc1dcd845990a5515828bbe8cccfab41wangyix     * <numCoords> transforms in the array.
131a7f4c435bc1dcd845990a5515828bbe8cccfab41wangyix     *
132a7f4c435bc1dcd845990a5515828bbe8cccfab41wangyix     * TODO: A better name for this function  would be "compute" instead of "get".
1338072caa80384292858d31ae34b7e19768875866bjoshualitt     */
134a7f4c435bc1dcd845990a5515828bbe8cccfab41wangyix    uint32_t getTransformKey(const SkTArray<const GrCoordTransform*, true>& coords,
135a7f4c435bc1dcd845990a5515828bbe8cccfab41wangyix                             int numCoords) const;
1368072caa80384292858d31ae34b7e19768875866bjoshualitt
1378072caa80384292858d31ae34b7e19768875866bjoshualitt    /**
1388072caa80384292858d31ae34b7e19768875866bjoshualitt     * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this geometry
1398072caa80384292858d31ae34b7e19768875866bjoshualitt     * processor's GL backend implementation.
140a7f4c435bc1dcd845990a5515828bbe8cccfab41wangyix     *
141a7f4c435bc1dcd845990a5515828bbe8cccfab41wangyix     * TODO: A better name for this function  would be "compute" instead of "get".
1428072caa80384292858d31ae34b7e19768875866bjoshualitt     */
14394efbf51f5a88d9e8aa961d3fbe38c5e335d6108Brian Salomon    virtual void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const = 0;
1448072caa80384292858d31ae34b7e19768875866bjoshualitt
1458072caa80384292858d31ae34b7e19768875866bjoshualitt
1468072caa80384292858d31ae34b7e19768875866bjoshualitt    /** Returns a new instance of the appropriate *GL* implementation class
1478072caa80384292858d31ae34b7e19768875866bjoshualitt        for the given GrProcessor; caller is responsible for deleting
1488072caa80384292858d31ae34b7e19768875866bjoshualitt        the object. */
14994efbf51f5a88d9e8aa961d3fbe38c5e335d6108Brian Salomon    virtual GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const = 0;
1508072caa80384292858d31ae34b7e19768875866bjoshualitt
1512279325d539700ee3da29d6e874b3b3ce1dcf49cethannicholas    virtual bool isPathRendering() const { return false; }
1528072caa80384292858d31ae34b7e19768875866bjoshualitt
1532279325d539700ee3da29d6e874b3b3ce1dcf49cethannicholas    /**
1542279325d539700ee3da29d6e874b3b3ce1dcf49cethannicholas     * If non-null, overrides the dest color returned by GrGLSLFragmentShaderBuilder::dstColor().
1552279325d539700ee3da29d6e874b3b3ce1dcf49cethannicholas     */
1562279325d539700ee3da29d6e874b3b3ce1dcf49cethannicholas    virtual const char* getDestColorOverride() const { return nullptr; }
1579d524f22bfde5dc3dc8f48e1be39bdebd3bb0304halcanary
15828ef445d2e55ada7a45fd74e9248b4f22b16e061ethannicholas    virtual float getSampleShading() const {
15928ef445d2e55ada7a45fd74e9248b4f22b16e061ethannicholas        return 0.0;
16028ef445d2e55ada7a45fd74e9248b4f22b16e061ethannicholas    }
16128ef445d2e55ada7a45fd74e9248b4f22b16e061ethannicholas
1629b03e7b29d963ea333a66dc5353e94f6391eb899dvonbeck    /* Sub-class should override and return true if this primitive processor implements the distance
1639b03e7b29d963ea333a66dc5353e94f6391eb899dvonbeck     * vector field, a field of vectors to the nearest point in the edge of the shape.  */
1649b03e7b29d963ea333a66dc5353e94f6391eb899dvonbeck    virtual bool implementsDistanceVector() const { return false; }
1659b03e7b29d963ea333a66dc5353e94f6391eb899dvonbeck
1668072caa80384292858d31ae34b7e19768875866bjoshualittprotected:
1677dbd45d2c7427d2c679d6507435d2f0220bf64efbsalomon    GrPrimitiveProcessor() : fVertexStride(0) {}
1688072caa80384292858d31ae34b7e19768875866bjoshualitt
1697dbd45d2c7427d2c679d6507435d2f0220bf64efbsalomon    enum { kPreallocAttribCnt = 8 };
1707dbd45d2c7427d2c679d6507435d2f0220bf64efbsalomon    SkSTArray<kPreallocAttribCnt, Attribute> fAttribs;
1718072caa80384292858d31ae34b7e19768875866bjoshualitt    size_t fVertexStride;
1728072caa80384292858d31ae34b7e19768875866bjoshualitt
1738072caa80384292858d31ae34b7e19768875866bjoshualittprivate:
174fc6c37b981daeece7474ce61070c707c37eefa62Mike Klein    void notifyRefCntIsZero() const final {}
1758072caa80384292858d31ae34b7e19768875866bjoshualitt    virtual bool hasExplicitLocalCoords() const = 0;
1768072caa80384292858d31ae34b7e19768875866bjoshualitt
1778072caa80384292858d31ae34b7e19768875866bjoshualitt    typedef GrProcessor INHERITED;
1788072caa80384292858d31ae34b7e19768875866bjoshualitt};
1798072caa80384292858d31ae34b7e19768875866bjoshualitt
1808072caa80384292858d31ae34b7e19768875866bjoshualitt#endif
181