1 /*
2  * Copyright (C) 2024 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 "algorithm_program.h"
17 
18 #include "base/math/math_utils.h"
19 #include "graphic/gl_utils.h"
20 
21 namespace OHOS {
22 namespace Media {
23 namespace Effect {
AlgorithmProgram(RenderContext * context,const std::string vertex,const std::string fragment)24 AlgorithmProgram::AlgorithmProgram(RenderContext *context, const std::string vertex, const std::string fragment)
25 {
26     context_ = context;
27     shader_ = new RenderGeneralProgram(context, vertex.c_str(), fragment.c_str());
28     shader_->Init();
29     vertexShaderCode_ = vertex;
30     fragmentShaderCode_ = fragment;
31 }
32 
~AlgorithmProgram()33 AlgorithmProgram::~AlgorithmProgram()
34 {
35     if (shader_) {
36         shader_->Release();
37         delete shader_;
38         shader_ = nullptr;
39     }
40 }
41 
UpdateShader(const std::string vertex,const std::string fragment)42 void AlgorithmProgram::UpdateShader(const std::string vertex, const std::string fragment)
43 {
44     if (strcmp(vertex.c_str(), vertexShaderCode_.c_str()) == 0 &&
45         strcmp(fragment.c_str(), fragmentShaderCode_.c_str()) == 0) {
46         return;
47     }
48     if (shader_) {
49         shader_->Release();
50         delete shader_;
51         shader_ = nullptr;
52     }
53     shader_ = new RenderGeneralProgram(context_, vertex.c_str(), fragment.c_str());
54     shader_->Init();
55     vertexShaderCode_ = vertex;
56     fragmentShaderCode_ = fragment;
57 }
58 
Bind()59 void AlgorithmProgram::Bind()
60 {
61     shader_->Bind();
62 }
63 
Unbind()64 void AlgorithmProgram::Unbind()
65 {
66     shader_->Unbind();
67 }
68 
GetAttributeLocation(const std::string attributeName)69 int AlgorithmProgram::GetAttributeLocation(const std::string attributeName)
70 {
71     return shader_->GetAttributeLocation(attributeName.c_str());
72 }
73 
GetUniformLocation(const std::string uniformName)74 int AlgorithmProgram::GetUniformLocation(const std::string uniformName)
75 {
76     return shader_->GetUniformLocation(uniformName.c_str());
77 }
78 
SetInt(const std::string name,int value)79 void AlgorithmProgram::SetInt(const std::string name, int value)
80 {
81     shader_->SetUniform(name.c_str(), value);
82 }
83 
SetFloat(const std::string name,float value)84 void AlgorithmProgram::SetFloat(const std::string name, float value)
85 {
86     shader_->SetUniform(name.c_str(), value);
87 }
88 
SetMat4(const std::string name,const void * value)89 void AlgorithmProgram::SetMat4(const std::string name, const void *value)
90 {
91     shader_->SetUniform(name.c_str(), value);
92 }
93 
BindTexture(const std::string name,int unitId,int textureId,GLenum target)94 void AlgorithmProgram::BindTexture(const std::string name, int unitId, int textureId, GLenum target)
95 {
96     glActiveTexture(GL_TEXTURE0 + unitId);
97     glBindTexture(target, textureId);
98     glUniform1i(GetUniformLocation(name), unitId);
99     GLUtils::CheckError(__FILE_NAME__, __LINE__);
100 }
101 
UnBindTexture(int unitId,GLenum target)102 void AlgorithmProgram::UnBindTexture(int unitId, GLenum target)
103 {
104     glActiveTexture(GL_TEXTURE0 + unitId);
105     glBindTexture(target, GL_NONE);
106 }
107 
GetShader()108 RenderGeneralProgram *AlgorithmProgram::GetShader()
109 {
110     return shader_;
111 }
112 } // namespace Effect
113 } // namespace Media
114 } // namespace OHOS