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 "vertical_blur_filter.h"
17
18 namespace OHOS {
19 namespace Rosen {
VerticalBlurFilter()20 VerticalBlurFilter::VerticalBlurFilter()
21 {
22 CreateProgram(GetVertexShader(), GetFragmentShader());
23 }
24
SetValue(const std::string & key,std::shared_ptr<void> value,int size)25 void VerticalBlurFilter::SetValue(const std::string& key, std::shared_ptr<void> value, int size)
26 {
27 if (key == "offset" && size > 0) {
28 std::shared_ptr<float> offset = std::static_pointer_cast<float>(value);
29 for (int i = 0; i < size; i++) {
30 offset_[i] = *(offset.get() + i);
31 }
32 }
33 if (key == "weight" && size > 0) {
34 std::shared_ptr<float> weight = std::static_pointer_cast<float>(value);
35 for (int i = 0; i < size; i++) {
36 weight_[i] = *(weight.get() + i);
37 }
38 }
39 }
40
LoadFilterParams()41 void VerticalBlurFilter::LoadFilterParams()
42 {
43 Use();
44 weightID_ = glGetUniformLocation(program_->programID_, "weight");
45 offsetID_ = glGetUniformLocation(program_->programID_, "offset");
46 glUniform1fv(weightID_, RADIUS, weight_);
47 glUniform1fv(offsetID_, RADIUS, offset_);
48 }
49
GetVertexShader()50 std::string VerticalBlurFilter::GetVertexShader()
51 {
52 return R"SHADER(#version 320 es
53 precision mediump float;
54
55 layout (location = 0) in vec3 vertexCoord;
56 layout (location = 1) in vec2 inputTexCoord;
57 out vec2 texCoord;
58
59 void main()
60 {
61 gl_Position = vec4(vertexCoord, 1.0);
62 texCoord = inputTexCoord;
63 }
64 )SHADER";
65 }
66
GetFragmentShader()67 std::string VerticalBlurFilter::GetFragmentShader()
68 {
69 return R"SHADER(#version 320 es
70 precision mediump float;
71 precision mediump int;
72
73 const int RADIUS = 3;
74 in vec2 texCoord;
75 out vec4 fragColor;
76
77 uniform sampler2D uTexture;
78 uniform float weight[3];
79 uniform float offset[3];
80
81 void main()
82 {
83 float tex_offset = 1.0 / float(textureSize(uTexture, 0).y); // gets size of single texel
84 vec3 result = texture(uTexture, texCoord).rgb * weight[0]; // current fragment's contribution
85
86 for (int i = 1; i < RADIUS; ++i)
87 {
88 result += texture(uTexture, texCoord + vec2(0.0, tex_offset) * offset[i]).rgb * weight[i];
89 result += texture(uTexture, texCoord - vec2(0.0, tex_offset) * offset[i]).rgb * weight[i];
90 }
91
92 fragColor = vec4(result, 1.0);
93 }
94 )SHADER";
95 }
96 } // namespcae Rosen
97 } // namespace OHOS