blob: 8c65dce50616189f6ccf7c1b957573a622651f1b [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
joshualitta5305a12014-10-10 17:47:00 -070013class GrFragmentStage;
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
joshualittb0a8a372014-09-23 09:50:21 -070037 // Installs an effect in the GrProcessorStage to perform a config conversion.
38 static const GrFragmentProcessor* Create(GrTexture*, bool swapRedAndBlue, PMConversion,
39 const SkMatrix&);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000040
41 static const char* Name() { return "Config Conversion"; }
joshualittb0a8a372014-09-23 09:50:21 -070042 typedef GrGLConfigConversionEffect GLProcessor;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000043
joshualittb0a8a372014-09-23 09:50:21 -070044 virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERRIDE;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000045
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000046 bool swapsRedAndBlue() const { return fSwapRedAndBlue; }
47 PMConversion pmConversion() const { return fPMConversion; }
48
49 // This function determines whether it is possible to choose PM->UPM and UPM->PM conversions
50 // for which in any PM->UPM->PM->UPM sequence the two UPM values are the same. This means that
51 // if pixels are read back to a UPM buffer, written back to PM to the GPU, and read back again
52 // both reads will produce the same result. This test is quite expensive and should not be run
53 // multiple times for a given context.
54 static void TestForPreservingPMConversions(GrContext* context,
55 PMConversion* PMToUPMRule,
56 PMConversion* UPMToPMRule);
57
58private:
59 GrConfigConversionEffect(GrTexture*,
60 bool swapRedAndBlue,
bsalomon@google.comb1456d72012-11-02 18:23:45 +000061 PMConversion pmConversion,
62 const SkMatrix& matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000063
joshualittb0a8a372014-09-23 09:50:21 -070064 virtual bool onIsEqual(const GrProcessor&) const SK_OVERRIDE;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000065
egdaniel1a8ecdf2014-10-03 06:24:12 -070066 virtual void onComputeInvariantOutput(InvariantOutput* inout) const SK_OVERRIDE;
67
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000068 bool fSwapRedAndBlue;
69 PMConversion fPMConversion;
70
joshualittb0a8a372014-09-23 09:50:21 -070071 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000072
73 typedef GrSingleTextureEffect INHERITED;
74};
75
76#endif