Home | History | Annotate | Download | only in effects
      1 /*
      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 #include "GrSwizzle.h"
     13 
     14 class GrInvariantOutput;
     15 
     16 /**
     17  * This class is used to perform config conversions. Clients may want to read/write data that is
     18  * unpremultiplied. Additionally, the channels may also be swizzled for optimal readback/upload
     19  * performance.
     20  */
     21 class GrConfigConversionEffect : public GrSingleTextureEffect {
     22 public:
     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     static const GrFragmentProcessor* Create(GrTexture*, const GrSwizzle&, PMConversion,
     37                                              const SkMatrix&);
     38 
     39     const char* name() const override { return "Config Conversion"; }
     40 
     41     const GrSwizzle& swizzle() const { return fSwizzle; }
     42     PMConversion  pmConversion() const { return fPMConversion; }
     43 
     44     // This function determines whether it is possible to choose PM->UPM and UPM->PM conversions
     45     // for which in any PM->UPM->PM->UPM sequence the two UPM values are the same. This means that
     46     // if pixels are read back to a UPM buffer, written back to PM to the GPU, and read back again
     47     // both reads will produce the same result. This test is quite expensive and should not be run
     48     // multiple times for a given context.
     49     static void TestForPreservingPMConversions(GrContext* context,
     50                                                PMConversion* PMToUPMRule,
     51                                                PMConversion* UPMToPMRule);
     52 
     53 private:
     54     GrConfigConversionEffect(GrTexture*,
     55                              const GrSwizzle&,
     56                              PMConversion pmConversion,
     57                              const SkMatrix& matrix);
     58 
     59     GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
     60 
     61     void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override;
     62 
     63     bool onIsEqual(const GrFragmentProcessor&) const override;
     64 
     65     void onComputeInvariantOutput(GrInvariantOutput* inout) const override;
     66 
     67     GrSwizzle       fSwizzle;
     68     PMConversion    fPMConversion;
     69 
     70     GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
     71 
     72     typedef GrSingleTextureEffect INHERITED;
     73 };
     74 
     75 #endif
     76