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