Do premul and r/b swap conversions in a custom effect

Review URL: https://codereview.appspot.com/6473060/



git-svn-id: http://skia.googlecode.com/svn/trunk@5284 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/gpu/effects/GrConfigConversionEffect.h b/src/gpu/effects/GrConfigConversionEffect.h
new file mode 100644
index 0000000..9bb7e3a
--- /dev/null
+++ b/src/gpu/effects/GrConfigConversionEffect.h
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2012 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef GrConfigConversionEffect_DEFINED
+#define GrConfigConversionEffect_DEFINED
+
+#include "GrSingleTextureEffect.h"
+
+class GrGLConfigConversionEffect;
+
+/**
+ * This class is used to perform config conversions. Clients may want to read/write data that is
+ * unpremultiplied. Also on some systems reading/writing BGRA or RGBA is faster. In those cases we
+ * read/write using the faster path and perform an R/B swap in the shader if the client data is in
+ * the slower config.
+ */
+class GrConfigConversionEffect : public GrSingleTextureEffect {
+public:
+    /**
+     * The PM->UPM or UPM->PM conversions to apply.
+     */
+    enum PMConversion {
+        kNone_PMConversion = 0,
+        kMulByAlpha_RoundUp_PMConversion,
+        kMulByAlpha_RoundDown_PMConversion,
+        kDivByAlpha_RoundUp_PMConversion,
+        kDivByAlpha_RoundDown_PMConversion,
+
+        kPMConversionCnt
+    };
+
+    // This will fail if the config is not 8888 and a PM conversion is requested.
+    static GrCustomStage* Create(GrTexture*,
+                                 bool swapRedAndBlue,
+                                 PMConversion pmConversion = kNone_PMConversion);
+
+    static const char* Name() { return "Config Conversion"; }
+    typedef GrGLConfigConversionEffect GLProgramStage;
+
+    virtual const GrProgramStageFactory& getFactory() const SK_OVERRIDE;
+    virtual bool isEqual(const GrCustomStage&) const SK_OVERRIDE;
+
+    bool swapsRedAndBlue() const { return fSwapRedAndBlue; }
+    PMConversion  pmConversion() const { return fPMConversion; }
+
+    // This function determines whether it is possible to choose PM->UPM and UPM->PM conversions
+    // for which in any PM->UPM->PM->UPM sequence the two UPM values are the same. This means that
+    // if pixels are read back to a UPM buffer, written back to PM to the GPU, and read back again
+    // both reads will produce the same result. This test is quite expensive and should not be run
+    // multiple times for a given context.
+    static void TestForPreservingPMConversions(GrContext* context,
+                                               PMConversion* PMToUPMRule,
+                                               PMConversion* UPMToPMRule);
+
+private:
+    GrConfigConversionEffect(GrTexture*,
+                            bool swapRedAndBlue,
+                            PMConversion pmConversion);
+
+    bool            fSwapRedAndBlue;
+    PMConversion    fPMConversion;
+
+    GR_DECLARE_CUSTOM_STAGE_TEST;
+
+    typedef GrSingleTextureEffect INHERITED;
+};
+
+#endif