• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 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 #include "include/private/SkSLString.h"
9 #include "src/gpu/Blend.h"
10 #include "src/gpu/ganesh/glsl/GrGLSLBlend.h"
11 #include "src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.h"
12 #include "src/gpu/ganesh/glsl/GrGLSLProgramBuilder.h"
13 
14 namespace GrGLSLBlend {
15 
BlendExpression(const GrProcessor * processor,GrGLSLUniformHandler * uniformHandler,GrGLSLProgramDataManager::UniformHandle * blendUniform,const char * srcColor,const char * dstColor,SkBlendMode mode)16 std::string BlendExpression(const GrProcessor* processor,
17                             GrGLSLUniformHandler* uniformHandler,
18                             GrGLSLProgramDataManager::UniformHandle* blendUniform,
19                             const char* srcColor,
20                             const char* dstColor,
21                             SkBlendMode mode) {
22     auto info = skgpu::GetReducedBlendModeInfo(mode);
23     if (info.fUniformData.empty()) {
24         return SkSL::String::printf("%s(%s, %s)", info.fFunction, srcColor, dstColor);
25     }
26 
27     SkSLType skslType = (SkSLType)((int)SkSLType::kHalf + info.fUniformData.size() - 1);
28     SkASSERT(skslType >= SkSLType::kHalf && skslType <= SkSLType::kHalf4);
29 
30     const char* blendUniName;
31     *blendUniform = uniformHandler->addUniform(processor, kFragment_GrShaderFlag,
32                                                skslType, "blend", &blendUniName);
33     return SkSL::String::printf("%s(%s, %s, %s)", info.fFunction, blendUniName, srcColor, dstColor);
34 }
35 
BlendKey(SkBlendMode mode)36 int BlendKey(SkBlendMode mode) {
37     switch (mode) {
38         case SkBlendMode::kSrcOver:
39         case SkBlendMode::kDstOver:
40         case SkBlendMode::kSrcIn:
41         case SkBlendMode::kDstIn:
42         case SkBlendMode::kSrcOut:
43         case SkBlendMode::kDstOut:
44         case SkBlendMode::kSrcATop:
45         case SkBlendMode::kDstATop:
46         case SkBlendMode::kXor:
47         case SkBlendMode::kPlus:
48             return -1;
49 
50         case SkBlendMode::kHue:
51         case SkBlendMode::kSaturation:
52         case SkBlendMode::kLuminosity:
53         case SkBlendMode::kColor:
54             return -2;
55 
56         case SkBlendMode::kOverlay:
57         case SkBlendMode::kHardLight:
58             return -3;
59 
60         case SkBlendMode::kDarken:
61         case SkBlendMode::kLighten:
62             return -4;
63 
64         default:
65             return (int)mode;
66     }
67 }
68 
SetBlendModeUniformData(const GrGLSLProgramDataManager & pdman,GrGLSLProgramDataManager::UniformHandle blendUniform,SkBlendMode mode)69 void SetBlendModeUniformData(const GrGLSLProgramDataManager& pdman,
70                              GrGLSLProgramDataManager::UniformHandle blendUniform,
71                              SkBlendMode mode) {
72     auto info = skgpu::GetReducedBlendModeInfo(mode);
73     switch (info.fUniformData.size()) {
74         case 0:
75             /* no uniform data necessary */
76             break;
77         case 1:
78             pdman.set1f(blendUniform, info.fUniformData[0]);
79             break;
80         case 2:
81             pdman.set2f(blendUniform, info.fUniformData[0], info.fUniformData[1]);
82             break;
83         case 3:
84             pdman.set3f(blendUniform, info.fUniformData[0], info.fUniformData[1],
85                                       info.fUniformData[2]);
86             break;
87         case 4:
88             pdman.set4f(blendUniform, info.fUniformData[0], info.fUniformData[1],
89                                       info.fUniformData[2], info.fUniformData[3]);
90             break;
91     }
92 }
93 
94 }  // namespace GrGLSLBlend
95