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 "scale_filter.h"
17 
18 namespace OHOS {
19 namespace Rosen {
ScaleFilter()20 ScaleFilter::ScaleFilter()
21 {
22     CreateProgram(GetVertexShader(), GetFragmentShader());
23 }
24 
SetValue(const std::string & key,std::shared_ptr<void> value,int size)25 void ScaleFilter::SetValue(const std::string& key, std::shared_ptr<void> value, int size)
26 {
27     if (key == "scale" && size > 0) {
28         std::shared_ptr<float> scale = std::static_pointer_cast<float>(value);
29         scale_ = *(scale.get());
30     }
31     LOGD("The scale is %{public}f.", scale_);
32 }
33 
SetScale(float scale)34 void ScaleFilter::SetScale(float scale)
35 {
36     scale_ = scale;
37 }
38 
DoProcess(ProcessData & data)39 void ScaleFilter::DoProcess(ProcessData& data)
40 {
41     data.textureHeight = std::floorf(scale_ * data.textureHeight);
42     data.textureWidth = std::floorf(scale_ * data.textureWidth);
43     std::swap(data.srcTextureID, data.dstTextureID);
44 }
45 
GetVertexShader()46 std::string ScaleFilter::GetVertexShader()
47 {
48     return R"SHADER(#version 320 es
49         precision mediump float;
50         layout (location = 0) in vec3 vertexCoord;
51         layout (location = 1) in vec2 inputTexCoord;
52         out vec2 texCoord;
53 
54         void main()
55         {
56             gl_Position = vec4(vertexCoord, 1.0);
57             texCoord = inputTexCoord;
58         }
59     )SHADER";
60 }
61 
GetFragmentShader()62 std::string ScaleFilter::GetFragmentShader()
63 {
64     return R"SHADER(#version 320 es
65         precision mediump float;
66         in vec2 texCoord;
67         out vec4 fragColor;
68         uniform sampler2D uTexture;
69         void main()
70         {
71             fragColor = texture(uTexture, texCoord);
72         }
73     )SHADER";
74 }
75 } // namespcae Rosen
76 } // namespace OHOS