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 #include <gles_program.h>
16 #include "display_common.h"
17 namespace OHOS {
18 namespace HDI {
19 namespace DISPLAY {
Init(const std::string & vertexSharderSrc,const std::string & fragmentSharderSrc)20 bool GlesProgram::Init(const std::string &vertexSharderSrc, const std::string &fragmentSharderSrc)
21 {
22 DISPLAY_LOGD();
23 bool ret = false;
24 // init sharder
25 auto vertexSharder = std::make_unique<GlesSharder>(GL_VERTEX_SHADER, vertexSharderSrc);
26 ret = vertexSharder->Init();
27 DISPLAY_CHK_RETURN((!ret), false, DISPLAY_LOGE("Failed to init vertex sharder"));
28 auto fragmentSharder = std::make_unique<GlesSharder>(GL_FRAGMENT_SHADER, fragmentSharderSrc);
29 ret = fragmentSharder->Init();
30 DISPLAY_CHK_RETURN((!ret), false, DISPLAY_LOGE("Failed to init fragment sharder"));
31 // init program
32 handle_ = glCreateProgram();
33 DISPLAY_CHK_RETURN((handle_ == 0), false, DISPLAY_LOGE("Failed to create program"));
34
35 glAttachShader(handle_, vertexSharder->Get());
36 glAttachShader(handle_, fragmentSharder->Get());
37 glLinkProgram(handle_);
38 vertexSharder_ = std::move(vertexSharder);
39 fragmentSharder_ = std::move(fragmentSharder);
40 GLint linked = 0;
41 glGetProgramiv(handle_, GL_LINK_STATUS, &linked);
42 if (linked != GL_TRUE) {
43 DISPLAY_LOGE("Failed to link program");
44 GLint logLen = 0;
45 glGetProgramiv(handle_, GL_INFO_LOG_LENGTH, &logLen);
46 if (logLen > 1) {
47 char *log = new char[logLen + 1]();
48 glGetProgramInfoLog(handle_, logLen + 1, nullptr, log);
49 DISPLAY_LOGE("link program log:");
50 DISPLAY_LOGE("%{public}s", log);
51 DISPLAY_LOGE("link program log end");
52 delete[] log;
53 }
54 return false;
55 }
56
57 return true;
58 }
59
~GlesProgram()60 GlesProgram::~GlesProgram()
61 {
62 if (vertexSharder_ != nullptr) {
63 glDetachShader(handle_, vertexSharder_->Get());
64 }
65 if (fragmentSharder_ != nullptr) {
66 glDetachShader(handle_, fragmentSharder_->Get());
67 }
68 glDeleteProgram(handle_);
69 }
70
Get() const71 GLuint GlesProgram::Get() const
72 {
73 return handle_;
74 }
75
Use() const76 void GlesProgram::Use() const
77 {
78 DISPLAY_LOGD();
79 glUseProgram(handle_);
80 }
81
SetUniform(const GLchar * name,GLint value)82 void GlesProgram::SetUniform(const GLchar *name, GLint value)
83 {
84 DISPLAY_LOGD();
85 GLint location = glGetUniformLocation(handle_, name);
86 if (location < 0) {
87 DISPLAY_LOGE("can not find the uniform %{public}s", name);
88 return;
89 }
90 glUniform1i(location, value);
91 }
92 }
93 }
94 }