1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "saturation_filter.h"
17
18 namespace OHOS {
19 namespace Rosen {
SaturationFilter()20 SaturationFilter::SaturationFilter()
21 {
22 CreateProgram(GetVertexShader(), GetFragmentShader());
23 }
24
SetValue(const std::string & key,std::shared_ptr<void> value,int size)25 void SaturationFilter::SetValue(const std::string& key, std::shared_ptr<void> value, int size)
26 {
27 if (key == "saturation" && size > 0) {
28 std::shared_ptr<float> saturation = std::static_pointer_cast<float>(value);
29 saturation_ = *(saturation.get());
30 }
31 LOGD("The saturation is %{public}f.", saturation_);
32 }
33
LoadFilterParams()34 void SaturationFilter::LoadFilterParams()
35 {
36 Use();
37 saturationID_ = glGetUniformLocation(program_->programID_, "saturation");
38 glUniform1f(saturationID_, saturation_);
39 }
40
GetVertexShader()41 std::string SaturationFilter::GetVertexShader()
42 {
43 return R"SHADER(#version 320 es
44 precision mediump float;
45
46 layout (location = 0) in vec3 vertexCoord;
47 layout (location = 1) in vec2 inputTexCoord;
48 out vec2 texCoord;
49
50 void main()
51 {
52 gl_Position = vec4(vertexCoord, 1.0);
53 texCoord = inputTexCoord;
54 }
55 )SHADER";
56 }
57
GetFragmentShader()58 std::string SaturationFilter::GetFragmentShader()
59 {
60 return R"SHADER(#version 320 es
61 precision mediump float;
62 in vec2 texCoord;
63 out vec4 fragColor;
64 uniform sampler2D uTexture;
65 uniform float saturation;
66 const mediump vec3 luminanceWeighting = vec3(0.2125, 0.7154, 0.0721);
67 void main()
68 {
69 vec4 textureColor = texture(uTexture, texCoord);
70 float luminance = dot(textureColor.rgb, luminanceWeighting);
71 vec3 greyScaleColor = vec3(luminance);
72
73 fragColor = vec4(mix(greyScaleColor, textureColor.rgb, saturation), textureColor.a);
74 }
75 )SHADER";
76 }
77 } // namespcae Rosen
78 } // namespace OHOS