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#ifndef GrFragmentProcessor_DEFINED
9#define GrFragmentProcessor_DEFINED
10
11#include "GrProcessor.h"
12
13class GrCoordTransform;
14class GrGLSLCaps;
15class GrGLFragmentProcessor;
16class GrProcessorKeyBuilder;
17
18/** Provides custom fragment shader code. Fragment processors receive an input color (vec4f) and
19    produce an output color. They may reference textures and uniforms. They may use
20    GrCoordTransforms to receive a transformation of the local coordinates that map from local space
21    to the fragment being processed.
22 */
23class GrFragmentProcessor : public GrProcessor {
24public:
25    GrFragmentProcessor()
26        : INHERITED()
27        , fWillUseInputColor(true)
28        , fUsesLocalCoords(false) {}
29
30    /** Implemented using GLFragmentProcessor::GenKey as described in this class's comment. */
31    virtual void getGLProcessorKey(const GrGLSLCaps& caps,
32                                   GrProcessorKeyBuilder* b) const = 0;
33
34    /** Returns a new instance of the appropriate *GL* implementation class
35        for the given GrFragmentProcessor; caller is responsible for deleting
36        the object. */
37    virtual GrGLFragmentProcessor* createGLInstance() const = 0;
38
39    /** Human-meaningful string to identify this GrFragmentProcessor; may be embedded
40        in generated shader code. */
41    virtual const char* name() const = 0;
42
43    int numTransforms() const { return fCoordTransforms.count(); }
44
45    /** Returns the coordinate transformation at index. index must be valid according to
46        numTransforms(). */
47    const GrCoordTransform& coordTransform(int index) const { return *fCoordTransforms[index]; }
48
49    const SkTArray<const GrCoordTransform*, true>& coordTransforms() const {
50        return fCoordTransforms;
51    }
52
53    /** Will this prceossor read the source color value? */
54    bool willUseInputColor() const { return fWillUseInputColor; }
55
56    /** Do any of the coordtransforms for this processor require local coords? */
57    bool usesLocalCoords() const { return fUsesLocalCoords; }
58
59    /** Returns true if this and other processor conservatively draw identically. It can only return
60        true when the two processor are of the same subclass (i.e. they return the same object from
61        from getFactory()).
62
63        A return value of true from isEqual() should not be used to test whether the processor would
64        generate the same shader code. To test for identical code generation use getGLProcessorKey*/
65    bool isEqual(const GrFragmentProcessor& that) const {
66        if (this->classID() != that.classID() ||
67            !this->hasSameTransforms(that) ||
68            !this->hasSameTextureAccesses(that)) {
69            return false;
70        }
71        return this->onIsEqual(that);
72    }
73
74    /**
75     * This function is used to perform optimizations. When called the invarientOuput param
76     * indicate whether the input components to this processor in the FS will have known values.
77     * In inout the validFlags member is a bitfield of GrColorComponentFlags. The isSingleComponent
78     * member indicates whether the input will be 1 or 4 bytes. The function updates the members of
79     * inout to indicate known values of its output. A component of the color member only has
80     * meaning if the corresponding bit in validFlags is set.
81     */
82    void computeInvariantOutput(GrInvariantOutput* inout) const;
83
84protected:
85    /**
86     * Fragment Processor subclasses call this from their constructor to register coordinate
87     * transformations. Coord transforms provide a mechanism for a processor to receive coordinates
88     * in their FS code. The matrix expresses a transformation from local space. For a given
89     * fragment the matrix will be applied to the local coordinate that maps to the fragment.
90     *
91     * When the transformation has perspective, the transformed coordinates will have
92     * 3 components. Otherwise they'll have 2.
93     *
94     * This must only be called from the constructor because GrProcessors are immutable. The
95     * processor subclass manages the lifetime of the transformations (this function only stores a
96     * pointer). The GrCoordTransform is typically a member field of the GrProcessor subclass.
97     *
98     * A processor subclass that has multiple methods of construction should always add its coord
99     * transforms in a consistent order. The non-virtual implementation of isEqual() automatically
100     * compares transforms and will assume they line up across the two processor instances.
101     */
102    void addCoordTransform(const GrCoordTransform*);
103
104    /**
105     * If the prceossor will generate a result that does not depend on the input color value then it
106     * must call this function from its constructor. Otherwise, when its generated backend-specific
107     * code might fail during variable binding due to unused variables.
108     */
109    void setWillNotUseInputColor() { fWillUseInputColor = false; }
110
111    /**
112     * Subclass implements this to support getConstantColorComponents(...).
113     */
114    virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const = 0;
115
116private:
117    /**
118     * Subclass implements this to support isEqual(). It will only be called if it is known that
119     * the two processors are of the same subclass (i.e. they return the same object from
120     * getFactory()). The processor subclass should not compare its coord transforms as that will
121     * be performed automatically in the non-virtual isEqual().
122     */
123    virtual bool onIsEqual(const GrFragmentProcessor&) const = 0;
124
125    bool hasSameTransforms(const GrFragmentProcessor&) const;
126
127    SkSTArray<4, const GrCoordTransform*, true>  fCoordTransforms;
128    bool                                         fWillUseInputColor;
129    bool                                         fUsesLocalCoords;
130
131    typedef GrProcessor INHERITED;
132};
133
134#endif
135