blob: 511f59541f97b5937b3739077dfc947059b4021e [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
13class GrGLConfigConversionEffect;
14
15/**
16 * This class is used to perform config conversions. Clients may want to read/write data that is
17 * unpremultiplied. Also on some systems reading/writing BGRA or RGBA is faster. In those cases we
18 * read/write using the faster path and perform an R/B swap in the shader if the client data is in
19 * the slower config.
20 */
21class GrConfigConversionEffect : public GrSingleTextureEffect {
22public:
23 /**
24 * The PM->UPM or UPM->PM conversions to apply.
25 */
26 enum PMConversion {
27 kNone_PMConversion = 0,
28 kMulByAlpha_RoundUp_PMConversion,
29 kMulByAlpha_RoundDown_PMConversion,
30 kDivByAlpha_RoundUp_PMConversion,
31 kDivByAlpha_RoundDown_PMConversion,
32
33 kPMConversionCnt
34 };
35
36 // This will fail if the config is not 8888 and a PM conversion is requested.
bsalomon@google.coma469c282012-10-24 18:28:34 +000037 static GrEffect* Create(GrTexture*,
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000038 bool swapRedAndBlue,
39 PMConversion pmConversion = kNone_PMConversion);
40
41 static const char* Name() { return "Config Conversion"; }
42 typedef GrGLConfigConversionEffect GLProgramStage;
43
44 virtual const GrProgramStageFactory& getFactory() const SK_OVERRIDE;
bsalomon@google.coma469c282012-10-24 18:28:34 +000045 virtual bool isEqual(const GrEffect&) const SK_OVERRIDE;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000046
47 bool swapsRedAndBlue() const { return fSwapRedAndBlue; }
48 PMConversion pmConversion() const { return fPMConversion; }
49
50 // This function determines whether it is possible to choose PM->UPM and UPM->PM conversions
51 // for which in any PM->UPM->PM->UPM sequence the two UPM values are the same. This means that
52 // if pixels are read back to a UPM buffer, written back to PM to the GPU, and read back again
53 // both reads will produce the same result. This test is quite expensive and should not be run
54 // multiple times for a given context.
55 static void TestForPreservingPMConversions(GrContext* context,
56 PMConversion* PMToUPMRule,
57 PMConversion* UPMToPMRule);
58
59private:
60 GrConfigConversionEffect(GrTexture*,
61 bool swapRedAndBlue,
62 PMConversion pmConversion);
63
64 bool fSwapRedAndBlue;
65 PMConversion fPMConversion;
66
bsalomon@google.comf271cc72012-10-24 19:35:13 +000067 GR_DECLARE_EFFECT_TEST;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000068
69 typedef GrSingleTextureEffect INHERITED;
70};
71
72#endif