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_sharder.h"
16 #include "display_common.h"
17 namespace OHOS {
18 namespace HDI {
19 namespace DISPLAY {
GlesSharder(GLenum type,const std::string & src)20 GlesSharder::GlesSharder(GLenum type, const std::string &src) : type_(type), source_(src)
21 {
22 DISPLAY_LOGD();
23 }
24
Get() const25 GLuint GlesSharder::Get() const
26 {
27 return handle_;
28 }
29
compile()30 bool GlesSharder::compile()
31 {
32 DISPLAY_LOGD();
33 GLint compiled = 0;
34 glCompileShader(handle_);
35 glGetShaderiv(handle_, GL_COMPILE_STATUS, &compiled);
36 if (compiled != GL_TRUE) {
37 DISPLAY_LOGE("Failed to compile sharder");
38 GLint logLen = 0;
39 glGetShaderiv(handle_, GL_INFO_LOG_LENGTH, &logLen);
40 if (logLen > 1) {
41 char *log = new char[logLen + 1]();
42 glGetShaderInfoLog(handle_, logLen + 1, nullptr, log);
43 DISPLAY_LOGE("sharder compile failed:");
44 DISPLAY_LOGE("%{public}s", log);
45 DISPLAY_LOGE("log information end");
46 delete[] log;
47 log = nullptr;
48 }
49 return false;
50 }
51 DISPLAY_LOGD("compiled success");
52 return true;
53 }
54
Init()55 bool GlesSharder::Init()
56 {
57 DISPLAY_LOGD();
58 DISPLAY_CHK_RETURN(((type_ != GL_VERTEX_SHADER) && (type_ != GL_FRAGMENT_SHADER)), false,
59 DISPLAY_LOGE("the type is invalide"));
60 handle_ = glCreateShader(type_);
61 DISPLAY_CHK_RETURN((handle_ == 0), false,
62 DISPLAY_LOGE("failed to create sharder glerror %{public}d", glGetError()));
63 const GLchar *shaderSource = source_.c_str();
64 glShaderSource(handle_, 1, &shaderSource, nullptr);
65 return compile();
66 }
67
~GlesSharder()68 GlesSharder::~GlesSharder()
69 {
70 if (handle_ != 0) {
71 glDeleteShader(handle_);
72 }
73 DISPLAY_LOGD();
74 }
75 }
76 }
77 }