• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 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 GrGLSLColorSpaceXformHelper_DEFINED
9 #define GrGLSLColorSpaceXformHelper_DEFINED
10 
11 #include "modules/skcms/skcms.h"
12 #include "src/core/SkColorSpacePriv.h"
13 #include "src/core/SkColorSpaceXformSteps.h"
14 #include "src/gpu/ganesh/GrColorSpaceXform.h"
15 #include "src/gpu/ganesh/glsl/GrGLSLUniformHandler.h"
16 
17 /**
18  * Helper class to assist with using GrColorSpaceXform within an FP. This manages all of the
19  * uniforms needed, and can be passed to shader builder functions to automatically generate the
20  * correct color space transformation code.
21  */
22 class GrGLSLColorSpaceXformHelper : public SkNoncopyable {
23 public:
GrGLSLColorSpaceXformHelper()24     GrGLSLColorSpaceXformHelper() {}
25 
26     void emitCode(GrGLSLUniformHandler* uniformHandler, const GrColorSpaceXform* colorSpaceXform,
27                   uint32_t visibility = kFragment_GrShaderFlag) {
28         SkASSERT(uniformHandler);
29         if (colorSpaceXform) {
30             fFlags = colorSpaceXform->fSteps.flags;
31             if (this->applySrcTF()) {
32                 fSrcTFVar = uniformHandler->addUniformArray(nullptr, visibility, SkSLType::kFloat,
33                                                             "SrcTF", kNumTransferFnCoeffs);
34                 fSrcTFType = skcms_TransferFunction_getType(&colorSpaceXform->fSteps.srcTF);
35             }
36             if (this->applyGamutXform()) {
37                 fGamutXformVar = uniformHandler->addUniform(nullptr, visibility, SkSLType::kFloat3x3,
38                                                             "ColorXform");
39             }
40             if (this->applyDstTF()) {
41                 fDstTFVar = uniformHandler->addUniformArray(nullptr, visibility, SkSLType::kFloat,
42                                                             "DstTF", kNumTransferFnCoeffs);
43                 fDstTFType = skcms_TransferFunction_getType(&colorSpaceXform->fSteps.dstTFInv);
44             }
45         }
46     }
47 
setData(const GrGLSLProgramDataManager & pdman,const GrColorSpaceXform * colorSpaceXform)48     void setData(const GrGLSLProgramDataManager& pdman, const GrColorSpaceXform* colorSpaceXform) {
49         if (this->applySrcTF()) {
50             pdman.set1fv(fSrcTFVar, kNumTransferFnCoeffs, &colorSpaceXform->fSteps.srcTF.g);
51         }
52         if (this->applyGamutXform()) {
53             pdman.setMatrix3f(fGamutXformVar, colorSpaceXform->fSteps.src_to_dst_matrix);
54         }
55         if (this->applyDstTF()) {
56             pdman.set1fv(fDstTFVar, kNumTransferFnCoeffs, &colorSpaceXform->fSteps.dstTFInv.g);
57         }
58     }
59 
isNoop()60     bool isNoop() const { return (0 == fFlags.mask()); }
61 
applyUnpremul()62     bool applyUnpremul() const   { return fFlags.unpremul; }
applySrcTF()63     bool applySrcTF() const      { return fFlags.linearize; }
applyGamutXform()64     bool applyGamutXform() const { return fFlags.gamut_transform; }
applyDstTF()65     bool applyDstTF() const      { return fFlags.encode; }
applyPremul()66     bool applyPremul() const     { return fFlags.premul; }
67 
srcTFType()68     skcms_TFType srcTFType() const { return fSrcTFType; }
dstTFType()69     skcms_TFType dstTFType() const { return fDstTFType; }
70 
srcTFUniform()71     GrGLSLProgramDataManager::UniformHandle srcTFUniform() const { return fSrcTFVar; }
gamutXformUniform()72     GrGLSLProgramDataManager::UniformHandle gamutXformUniform() const { return fGamutXformVar; }
dstTFUniform()73     GrGLSLProgramDataManager::UniformHandle dstTFUniform() const { return fDstTFVar; }
74 
75 private:
76     static const int kNumTransferFnCoeffs = 7;
77 
78     GrGLSLProgramDataManager::UniformHandle fSrcTFVar;
79     GrGLSLProgramDataManager::UniformHandle fGamutXformVar;
80     GrGLSLProgramDataManager::UniformHandle fDstTFVar;
81     SkColorSpaceXformSteps::Flags fFlags;
82     skcms_TFType fSrcTFType;
83     skcms_TFType fDstTFType;
84 };
85 
86 #endif
87