blob: 10ecf54c758c25e938cb9b1b7fe549f016a9e727 [file] [log] [blame]
egdaniel378092f2014-12-03 10:40:13 -08001/*
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 GrXferProcessor_DEFINED
9#define GrXferProcessor_DEFINED
10
11#include "GrColor.h"
12#include "GrFragmentProcessor.h"
13#include "GrTypes.h"
14#include "SkXfermode.h"
15
16/**
17 * GrXferProcessor is responsible for implementing the xfer mode that blends the src color and dst
18 * color. It does this by emitting fragment shader code and controlling the fixed-function blend
19 * state. The inputs to its shader code are the final computed src color and fractional pixel
20 * coverage. The GrXferProcessor's shader code writes the fragment shader output color that goes
21 * into the fixed-function blend. When dual-source blending is available, it may also write a
22 * seconday fragment shader output color. When allowed by the backend API, the GrXferProcessor may
23 * read the destination color. The GrXferProcessor is responsible for setting the blend coefficients
24 * and blend constant color.
25 *
26 * A GrXferProcessor is never installed directly into our draw state, but instead is created from a
27 * GrXPFactory once we have finalized the state of our draw.
28 */
29class GrXferProcessor : public GrFragmentProcessor {
30private:
31
32 typedef GrFragmentProcessor INHERITED;
33};
34
35/**
36 * We install a GrXPFactory (XPF) early on in the pipeline before all the final draw information is
37 * known (e.g. whether there is fractional pixel coverage, will coverage be 1 or 4 channel, is the
38 * draw opaque, etc.). Once the state of the draw is finalized, we use the XPF along with all the
39 * draw information to create a GrXferProcessor (XP) which can implement the desired blending for
40 * the draw.
41 *
42 * Before the XP is created, the XPF is able to answer queries about what functionality the XPs it
43 * creates will have. For example, can it create an XP that supports RGB coverage or will the XP
44 * blend with the destination color.
45 */
egdanielc016fb82014-12-03 11:41:54 -080046class GrXPFactory : public SkRefCnt {
egdaniel378092f2014-12-03 10:40:13 -080047public:
48 virtual const GrXferProcessor* createXferProcessor() const = 0;
49
50 /**
51 * This function returns true if the GrXferProcessor generated from this factory will be able to
52 * correctly blend when using RGB coverage. The knownColor and knownColorFlags represent the
53 * final computed color from the color stages.
54 */
55 virtual bool supportsRGBCoverage(GrColor knownColor, uint32_t knownColorFlags) const = 0;
56
egdaniel915187b2014-12-05 12:58:28 -080057 bool isEqual(const GrXPFactory& that) const {
58 if (this->classID() != that.classID()) {
59 return false;
60 }
61 return this->onIsEqual(that);
62 }
63
64 /**
65 * Helper for down-casting to a GrXPFactory subclass
66 */
67 template <typename T> const T& cast() const { return *static_cast<const T*>(this); }
68
69 uint32_t classID() const { SkASSERT(kIllegalXPFClassID != fClassID); return fClassID; }
70
71protected:
72 GrXPFactory() : fClassID(kIllegalXPFClassID) {}
73
74 template <typename XPF_SUBCLASS> void initClassID() {
75 static uint32_t kClassID = GenClassID();
76 fClassID = kClassID;
77 }
78
79 uint32_t fClassID;
80
egdaniel378092f2014-12-03 10:40:13 -080081private:
egdaniel915187b2014-12-05 12:58:28 -080082 virtual bool onIsEqual(const GrXPFactory&) const = 0;
83
84 static uint32_t GenClassID() {
85 // fCurrXPFactoryID has been initialized to kIllegalXPFactoryID. The
86 // atomic inc returns the old value not the incremented value. So we add
87 // 1 to the returned value.
88 uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&gCurrXPFClassID)) + 1;
89 if (!id) {
90 SkFAIL("This should never wrap as it should only be called once for each GrXPFactory "
91 "subclass.");
92 }
93 return id;
94 }
95
96 enum {
97 kIllegalXPFClassID = 0,
98 };
99 static int32_t gCurrXPFClassID;
100
egdaniel378092f2014-12-03 10:40:13 -0800101 typedef GrProgramElement INHERITED;
102};
103
104#endif
105