blob: 3845d32bbd65cfb544e3f92c3a1903c9e217ae46 [file] [log] [blame]
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001/*
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 GrConfigConversionEffect_DEFINED
9#define GrConfigConversionEffect_DEFINED
10
11#include "GrSingleTextureEffect.h"
12
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000013class GrEffectStage;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000014class GrGLConfigConversionEffect;
15
16/**
17 * This class is used to perform config conversions. Clients may want to read/write data that is
18 * unpremultiplied. Also on some systems reading/writing BGRA or RGBA is faster. In those cases we
19 * read/write using the faster path and perform an R/B swap in the shader if the client data is in
20 * the slower config.
21 */
22class GrConfigConversionEffect : public GrSingleTextureEffect {
23public:
24 /**
25 * The PM->UPM or UPM->PM conversions to apply.
26 */
27 enum PMConversion {
28 kNone_PMConversion = 0,
29 kMulByAlpha_RoundUp_PMConversion,
30 kMulByAlpha_RoundDown_PMConversion,
31 kDivByAlpha_RoundUp_PMConversion,
32 kDivByAlpha_RoundDown_PMConversion,
33
34 kPMConversionCnt
35 };
36
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000037 // Installs an effect in the GrEffectStage to perform a config conversion.
38 static bool InstallEffect(GrTexture*,
39 bool swapRedAndBlue,
40 PMConversion pmConversion,
bsalomon@google.comb9086a02012-11-01 18:02:54 +000041 const SkMatrix& matrix,
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000042 GrEffectStage* stage);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000043
44 static const char* Name() { return "Config Conversion"; }
bsalomon@google.com422e81a2012-10-25 14:11:03 +000045 typedef GrGLConfigConversionEffect GLEffect;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000046
bsalomon@google.com396e61f2012-10-25 19:00:29 +000047 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000048
49 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000050
51 bool swapsRedAndBlue() const { return fSwapRedAndBlue; }
52 PMConversion pmConversion() const { return fPMConversion; }
53
54 // This function determines whether it is possible to choose PM->UPM and UPM->PM conversions
55 // for which in any PM->UPM->PM->UPM sequence the two UPM values are the same. This means that
56 // if pixels are read back to a UPM buffer, written back to PM to the GPU, and read back again
57 // both reads will produce the same result. This test is quite expensive and should not be run
58 // multiple times for a given context.
59 static void TestForPreservingPMConversions(GrContext* context,
60 PMConversion* PMToUPMRule,
61 PMConversion* UPMToPMRule);
62
63private:
64 GrConfigConversionEffect(GrTexture*,
65 bool swapRedAndBlue,
bsalomon@google.comb1456d72012-11-02 18:23:45 +000066 PMConversion pmConversion,
67 const SkMatrix& matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000068
bsalomon@google.com8a252f72013-01-22 20:35:13 +000069 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000070
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000071 bool fSwapRedAndBlue;
72 PMConversion fPMConversion;
73
bsalomon@google.comf271cc72012-10-24 19:35:13 +000074 GR_DECLARE_EFFECT_TEST;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000075
76 typedef GrSingleTextureEffect INHERITED;
77};
78
79#endif