GrSingleTextureEffect.h revision 8abb370aca280516f4861c6c942ec453aad018fa
1/*
2 * Copyright 2012 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 GrSingleTextureEffect_DEFINED
9#define GrSingleTextureEffect_DEFINED
10
11#include "GrFragmentProcessor.h"
12#include "GrColorSpaceXform.h"
13#include "GrCoordTransform.h"
14#include "GrInvariantOutput.h"
15#include "SkMatrix.h"
16
17class GrTexture;
18
19/**
20 * A base class for effects that draw a single texture with a texture matrix. This effect has no
21 * backend implementations. One must be provided by the subclass.
22 */
23class GrSingleTextureEffect : public GrFragmentProcessor {
24public:
25    ~GrSingleTextureEffect() override;
26
27    SkString dumpInfo() const override {
28        SkString str;
29        str.appendf("Texture: %d", fTextureAccess.getTexture()->uniqueID());
30        return str;
31    }
32
33protected:
34    /** unfiltered, clamp mode */
35    GrSingleTextureEffect(GrTexture*, sk_sp<GrColorSpaceXform>, const SkMatrix&,
36                          GrCoordSet = kLocal_GrCoordSet);
37    /** clamp mode */
38    GrSingleTextureEffect(GrTexture*, sk_sp<GrColorSpaceXform>, const SkMatrix&,
39                          GrTextureParams::FilterMode filterMode, GrCoordSet = kLocal_GrCoordSet);
40    GrSingleTextureEffect(GrTexture*,
41                          sk_sp<GrColorSpaceXform>,
42                          const SkMatrix&,
43                          const GrTextureParams&,
44                          GrCoordSet = kLocal_GrCoordSet);
45
46    /**
47     * Can be used as a helper to implement subclass onComputeInvariantOutput(). It assumes that
48     * the subclass output color will be a modulation of the input color with a value read from the
49     * texture.
50     */
51    void updateInvariantOutputForModulation(GrInvariantOutput* inout) const {
52        if (GrPixelConfigIsAlphaOnly(this->texture(0)->config())) {
53            inout->mulByUnknownSingleComponent();
54        } else if (GrPixelConfigIsOpaque(this->texture(0)->config())) {
55            inout->mulByUnknownOpaqueFourComponents();
56        } else {
57            inout->mulByUnknownFourComponents();
58        }
59    }
60
61private:
62    GrCoordTransform fCoordTransform;
63    GrTextureAccess  fTextureAccess;
64    sk_sp<GrColorSpaceXform> fColorSpaceXform;
65
66    typedef GrFragmentProcessor INHERITED;
67};
68
69#endif
70