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 "contrast_filter.h"
17 
18 namespace OHOS {
19 namespace Rosen {
ContrastFilter()20 ContrastFilter::ContrastFilter()
21 {
22     CreateProgram(GetVertexShader(), GetFragmentShader());
23 }
24 
SetValue(const std::string & key,std::shared_ptr<void> value,int size)25 void ContrastFilter::SetValue(const std::string& key, std::shared_ptr<void> value, int size)
26 {
27     if (key == "contrast" && size > 0) {
28         std::shared_ptr<float> contrast = std::static_pointer_cast<float>(value);
29         contrast_ = *(contrast.get());
30     }
31     LOGD("The contrast is  %{public}f.", contrast_);
32 }
33 
LoadFilterParams()34 void ContrastFilter::LoadFilterParams()
35 {
36     Use();
37     contrastID_ = glGetUniformLocation(program_->programID_, "contrast");
38     glUniform1f(contrastID_, contrast_);
39 }
40 
GetVertexShader()41 std::string ContrastFilter::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 ContrastFilter::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 contrast;
66         void main()
67         {
68             vec4 textureColor = texture(uTexture, texCoord);
69             fragColor = vec4(((textureColor.rgb - 0.5) * contrast + 0.5), textureColor.a);
70         }
71     )SHADER";
72 }
73 } // namespcae Rosen
74 } // namespace OHOS