1 /*
2 * Copyright (c) 2023 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 "egl_core.h"
17
18 #include <EGL/egl.h>
19 #include <EGL/eglext.h>
20 #include <EGL/eglplatform.h>
21 #include <GLES3/gl3.h>
22 #include <cmath>
23 #include <cstdio>
24 #include <hilog/log.h>
25
26 #include "../common/common.h"
27 #include "plugin_render.h"
28
29 namespace NativeXComponentSample {
30 namespace {
31 constexpr int32_t NUM_4 = 4;
32 /**
33 * Vertex shader.
34 */
35 const char VERTEX_SHADER[] = "#version 300 es\n"
36 "layout(location = 0) in vec4 a_position;\n"
37 "layout(location = 1) in vec4 a_color; \n"
38 "out vec4 v_color; \n"
39 "void main() \n"
40 "{ \n"
41 " gl_Position = a_position; \n"
42 " v_color = a_color; \n"
43 "} \n";
44
45 /**
46 * Fragment shader.
47 */
48 const char FRAGMENT_SHADER[] = "#version 300 es\n"
49 "precision mediump float; \n"
50 "in vec4 v_color; \n"
51 "out vec4 fragColor; \n"
52 "void main() \n"
53 "{ \n"
54 " fragColor = v_color; \n"
55 "} \n";
56
57 /**
58 * Background color #f4f4f4.
59 */
60 const GLfloat BACKGROUND_COLOR[] = {244.0f / 255, 244.0f / 255, 244.0f / 255, 1.0f};
61
62 /**
63 * Draw color #7E8FFB.
64 */
65 const GLfloat DRAW_COLOR[] = {126.0f / 255, 143.0f / 255, 251.0f / 255, 1.0f};
66
67 /**
68 * Change color #92D6CC.
69 */
70 const GLfloat CHANGE_COLOR[] = {146.0f / 255, 214.0f / 255, 204.0f / 255, 1.0f};
71
72 /**
73 * Background area.
74 */
75 const GLfloat BACKGROUND_RECTANGLE_VERTICES[] = {
76 -1.0f, 1.0f,
77 1.0f, 1.0f,
78 1.0f, -1.0f,
79 -1.0f, -1.0f};
80
81 /**
82 * Get context parameter count.
83 */
84 const size_t GET_CONTEXT_PARAM_CNT = 1;
85
86 /**
87 * Fifty percent.
88 */
89 const float FIFTY_PERCENT = 0.5;
90
91 /**
92 * Pointer size.
93 */
94 const GLint POINTER_SIZE = 2;
95
96 /**
97 * Triangle fan size.
98 */
99 const GLsizei TRIANGLE_FAN_SIZE = 4;
100
101 /**
102 * Egl red size default.
103 */
104 const int EGL_RED_SIZE_DEFAULT = 8;
105
106 /**
107 * Egl green size default.
108 */
109 const int EGL_GREEN_SIZE_DEFAULT = 8;
110
111 /**
112 * Egl blue size default.
113 */
114 const int EGL_BLUE_SIZE_DEFAULT = 8;
115
116 /**
117 * Egl alpha size default.
118 */
119 const int EGL_ALPHA_SIZE_DEFAULT = 8;
120
121 /**
122 * Default x position.
123 */
124 const int DEFAULT_X_POSITION = 0;
125
126 /**
127 * Default y position.
128 */
129 const int DEFAULT_Y_POSITION = 0;
130
131 /**
132 * Gl red default.
133 */
134 const GLfloat GL_RED_DEFAULT = 0.0;
135
136 /**
137 * Gl green default.
138 */
139 const GLfloat GL_GREEN_DEFAULT = 0.0;
140
141 /**
142 * Gl blue default.
143 */
144 const GLfloat GL_BLUE_DEFAULT = 0.0;
145
146 /**
147 * Gl alpha default.
148 */
149 const GLfloat GL_ALPHA_DEFAULT = 1.0;
150
151 /**
152 * Program error.
153 */
154 const GLuint PROGRAM_ERROR = 0;
155
156 /**
157 * Shape vertices size.
158 */
159 const int SHAPE_VERTICES_SIZE = 8;
160
161 /**
162 * Position handle name.
163 */
164 const char POSITION_NAME[] = "a_position";
165
166 /**
167 * Position error.
168 */
169 const GLint POSITION_ERROR = -1;
170
171 /**
172 * Config attribute list.
173 */
174 const EGLint ATTRIB_LIST[] = {
175 // Key,value.
176 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
177 EGL_RED_SIZE, EGL_RED_SIZE_DEFAULT,
178 EGL_GREEN_SIZE, EGL_GREEN_SIZE_DEFAULT,
179 EGL_BLUE_SIZE, EGL_BLUE_SIZE_DEFAULT,
180 EGL_ALPHA_SIZE, EGL_ALPHA_SIZE_DEFAULT,
181 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
182 // End.
183 EGL_NONE};
184
185 /**
186 * Context attributes.
187 */
188 const EGLint CONTEXT_ATTRIBS[] = {
189 EGL_CONTEXT_CLIENT_VERSION, 2,
190 EGL_NONE};
191 } // namespace
EglContextInit(void * window,int width,int height)192 bool EGLCore::EglContextInit(void* window, int width, int height)
193 {
194 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "EGLCore", "EglContextInit execute");
195 if ((window == nullptr) || (width <= 0) || (height <= 0)) {
196 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "EglContextInit: param error");
197 return false;
198 }
199
200 UpdateSize(width, height);
201 eglWindow_ = static_cast<EGLNativeWindowType>(window);
202
203 // Init display.
204 eglDisplay_ = eglGetDisplay(EGL_DEFAULT_DISPLAY);
205 if (eglDisplay_ == EGL_NO_DISPLAY) {
206 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "eglGetDisplay: unable to get EGL display");
207 return false;
208 }
209
210 EGLint majorVersion;
211 EGLint minorVersion;
212 if (!eglInitialize(eglDisplay_, &majorVersion, &minorVersion)) {
213 OH_LOG_Print(
214 LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "eglInitialize: unable to get initialize EGL display");
215 return false;
216 }
217
218 // Select configuration.
219 const EGLint maxConfigSize = 1;
220 EGLint numConfigs;
221 if (!eglChooseConfig(eglDisplay_, ATTRIB_LIST, &eglConfig_, maxConfigSize, &numConfigs)) {
222 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "eglChooseConfig: unable to choose configs");
223 return false;
224 }
225
226 return CreateEnvironment();
227 }
228
CreateEnvironment()229 bool EGLCore::CreateEnvironment()
230 {
231 // Create surface.
232 if (eglWindow_ == nullptr) {
233 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "eglWindow_ is null");
234 return false;
235 }
236 eglSurface_ = eglCreateWindowSurface(eglDisplay_, eglConfig_, eglWindow_, NULL);
237 if (eglSurface_ == nullptr) {
238 OH_LOG_Print(
239 LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "eglCreateWindowSurface: unable to create surface");
240 return false;
241 }
242 // Create context.
243 eglContext_ = eglCreateContext(eglDisplay_, eglConfig_, EGL_NO_CONTEXT, CONTEXT_ATTRIBS);
244 if (!eglMakeCurrent(eglDisplay_, eglSurface_, eglSurface_, eglContext_)) {
245 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "eglMakeCurrent failed");
246 return false;
247 }
248 // Create program.
249 program_ = CreateProgram(VERTEX_SHADER, FRAGMENT_SHADER);
250 if (program_ == PROGRAM_ERROR) {
251 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "CreateProgram: unable to create program");
252 return false;
253 }
254 return true;
255 }
256
Background()257 void EGLCore::Background()
258 {
259 GLint position = PrepareDraw();
260 if (position == POSITION_ERROR) {
261 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Background get position failed");
262 return;
263 }
264
265 if (!ExecuteDraw(position, BACKGROUND_COLOR,
266 BACKGROUND_RECTANGLE_VERTICES, sizeof(BACKGROUND_RECTANGLE_VERTICES))) {
267 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Background execute draw failed");
268 return;
269 }
270
271 if (!FinishDraw()) {
272 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Background FinishDraw failed");
273 return;
274 }
275 }
276
Draw(int & hasDraw)277 void EGLCore::Draw(int& hasDraw)
278 {
279 flag_ = false;
280 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "EGLCore", "Draw");
281 GLint position = PrepareDraw();
282 if (position == POSITION_ERROR) {
283 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Draw get position failed");
284 return;
285 }
286
287 if (!ExecuteDraw(position, BACKGROUND_COLOR,
288 BACKGROUND_RECTANGLE_VERTICES, sizeof(BACKGROUND_RECTANGLE_VERTICES))) {
289 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Draw execute draw background failed");
290 return;
291 }
292
293 // Divided into five quadrilaterals and calculate one of the quadrilateral's Vertices
294 GLfloat rotateX = 0;
295 GLfloat rotateY = FIFTY_PERCENT * height_;
296 GLfloat centerX = 0;
297 // Convert DEG(54° & 18°) to RAD
298 GLfloat centerY = -rotateY * (M_PI / 180 * 54) * (M_PI / 180 * 18);
299 // Convert DEG(18°) to RAD
300 GLfloat leftX = -rotateY * (M_PI / 180 * 18);
301 GLfloat leftY = 0;
302 // Convert DEG(18°) to RAD
303 GLfloat rightX = rotateY * (M_PI / 180 * 18);
304 GLfloat rightY = 0;
305
306 const GLfloat shapeVertices[] = { centerX / width_, centerY / height_, leftX / width_, leftY / height_,
307 rotateX / width_, rotateY / height_, rightX / width_, rightY / height_ };
308
309 if (!ExecuteDrawStar(position, DRAW_COLOR, shapeVertices, sizeof(shapeVertices))) {
310 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Draw execute draw shape failed");
311 return;
312 }
313
314 // Convert DEG(72°) to RAD
315 GLfloat rad = M_PI / 180 * 72;
316 // Rotate four times
317 for (int i = 0; i < NUM_4; ++i) {
318 Rotate2d(centerX, centerY, &rotateX, &rotateY, rad);
319 Rotate2d(centerX, centerY, &leftX, &leftY, rad);
320 Rotate2d(centerX, centerY, &rightX, &rightY, rad);
321
322 const GLfloat shapeVertices[] = { centerX / width_, centerY / height_, leftX / width_, leftY / height_,
323 rotateX / width_, rotateY / height_, rightX / width_, rightY / height_ };
324
325 if (!ExecuteDrawStar(position, DRAW_COLOR, shapeVertices, sizeof(shapeVertices))) {
326 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Draw execute draw shape failed");
327 return;
328 }
329 }
330
331 if (!FinishDraw()) {
332 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Draw FinishDraw failed");
333 return;
334 }
335 hasDraw = 1;
336
337 flag_ = true;
338 }
339
ChangeColor(int & hasChangeColor)340 void EGLCore::ChangeColor(int& hasChangeColor)
341 {
342 if (!flag_) {
343 return;
344 }
345 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "EGLCore", "ChangeColor");
346 GLint position = PrepareDraw();
347 if (position == POSITION_ERROR) {
348 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "ChangeColor get position failed");
349 return;
350 }
351
352 if (!ExecuteDraw(position, BACKGROUND_COLOR,
353 BACKGROUND_RECTANGLE_VERTICES, sizeof(BACKGROUND_RECTANGLE_VERTICES))) {
354 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "ChangeColor execute draw background failed");
355 return;
356 }
357
358 // Divided into five quadrilaterals and calculate one of the quadrilateral's Vertices
359 GLfloat rotateX = 0;
360 GLfloat rotateY = FIFTY_PERCENT * height_;
361 GLfloat centerX = 0;
362 // Convert DEG(54° & 18°) to RAD
363 GLfloat centerY = -rotateY * (M_PI / 180 * 54) * (M_PI / 180 * 18);
364 // Convert DEG(18°) to RAD
365 GLfloat leftX = -rotateY * (M_PI / 180 * 18);
366 GLfloat leftY = 0;
367 // Convert DEG(18°) to RAD
368 GLfloat rightX = rotateY * (M_PI / 180 * 18);
369 GLfloat rightY = 0;
370
371 const GLfloat shapeVertices[] = { centerX / width_, centerY / height_, leftX / width_, leftY / height_,
372 rotateX / width_, rotateY / height_, rightX / width_, rightY / height_ };
373
374 if (!ExecuteDrawNewStar(0, CHANGE_COLOR, shapeVertices, sizeof(shapeVertices))) {
375 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Draw execute draw shape failed");
376 return;
377 }
378
379 // Convert DEG(72°) to RAD
380 GLfloat rad = M_PI / 180 * 72;
381 // Rotate four times
382 for (int i = 0; i < NUM_4; ++i) {
383 Rotate2d(centerX, centerY, &rotateX, &rotateY, rad);
384 Rotate2d(centerX, centerY, &leftX, &leftY, rad);
385 Rotate2d(centerX, centerY, &rightX, &rightY, rad);
386 const GLfloat shapeVertices[] = { centerX / width_, centerY / height_, leftX / width_, leftY / height_,
387 rotateX / width_, rotateY / height_, rightX / width_, rightY / height_ };
388
389 if (!ExecuteDrawNewStar(position, CHANGE_COLOR, shapeVertices, sizeof(shapeVertices))) {
390 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Draw execute draw shape failed");
391 return;
392 }
393 }
394
395 if (!FinishDraw()) {
396 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "ChangeColor FinishDraw failed");
397 }
398 hasChangeColor = 1;
399 }
400
PrepareDraw()401 GLint EGLCore::PrepareDraw()
402 {
403 if ((eglDisplay_ == nullptr) || (eglSurface_ == nullptr) || (eglContext_ == nullptr) ||
404 (!eglMakeCurrent(eglDisplay_, eglSurface_, eglSurface_, eglContext_))) {
405 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "PrepareDraw: param error");
406 return POSITION_ERROR;
407 }
408
409 // The gl function has no return value.
410 glViewport(DEFAULT_X_POSITION, DEFAULT_Y_POSITION, width_, height_);
411 glClearColor(GL_RED_DEFAULT, GL_GREEN_DEFAULT, GL_BLUE_DEFAULT, GL_ALPHA_DEFAULT);
412 glClear(GL_COLOR_BUFFER_BIT);
413 glUseProgram(program_);
414
415 return glGetAttribLocation(program_, POSITION_NAME);
416 }
417
ExecuteDraw(GLint position,const GLfloat * color,const GLfloat shapeVertices[],unsigned long vertSize)418 bool EGLCore::ExecuteDraw(GLint position, const GLfloat* color, const GLfloat shapeVertices[], unsigned long vertSize)
419 {
420 if ((position > 0) || (color == nullptr) || (vertSize / sizeof(shapeVertices[0])) != SHAPE_VERTICES_SIZE) {
421 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "ExecuteDraw: param error");
422 return false;
423 }
424
425 // The gl function has no return value.
426 glVertexAttribPointer(position, POINTER_SIZE, GL_FLOAT, GL_FALSE, 0, shapeVertices);
427 glEnableVertexAttribArray(position);
428 glVertexAttrib4fv(1, color);
429 glDrawArrays(GL_TRIANGLE_FAN, 0, TRIANGLE_FAN_SIZE);
430 glDisableVertexAttribArray(position);
431
432 return true;
433 }
434
ExecuteDrawStar(GLint position,const GLfloat * color,const GLfloat shapeVertices[],unsigned long vertSize)435 bool EGLCore::ExecuteDrawStar(
436 GLint position, const GLfloat* color, const GLfloat shapeVertices[], unsigned long vertSize)
437 {
438 if ((position > 0) || (color == nullptr) || (vertSize / sizeof(shapeVertices[0])) != SHAPE_VERTICES_SIZE) {
439 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "ExecuteDraw: param error");
440 return false;
441 }
442
443 // The gl function has no return value.
444 glVertexAttribPointer(position, POINTER_SIZE, GL_FLOAT, GL_FALSE, 0, shapeVertices);
445 glVertexAttribPointer(1, POINTER_SIZE, GL_FLOAT, GL_FALSE, 0, color);
446 glEnableVertexAttribArray(position);
447 glEnableVertexAttribArray(1);
448 glVertexAttrib4fv(1, color);
449 glDrawArrays(GL_TRIANGLE_FAN, 0, TRIANGLE_FAN_SIZE);
450 glDisableVertexAttribArray(position);
451 glDisableVertexAttribArray(1);
452
453 return true;
454 }
455
ExecuteDrawNewStar(GLint position,const GLfloat * color,const GLfloat shapeVertices[],unsigned long vertSize)456 bool EGLCore::ExecuteDrawNewStar(
457 GLint position, const GLfloat* color, const GLfloat shapeVertices[], unsigned long vertSize)
458 {
459 if ((position > 0) || (color == nullptr) || (vertSize / sizeof(shapeVertices[0])) != SHAPE_VERTICES_SIZE) {
460 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "ExecuteDraw: param error");
461 return false;
462 }
463
464 // The gl function has no return value.
465 glVertexAttribPointer(position, POINTER_SIZE, GL_FLOAT, GL_FALSE, 0, shapeVertices);
466 glEnableVertexAttribArray(position);
467 glVertexAttrib4fv(1, color);
468 glDrawArrays(GL_TRIANGLE_FAN, 0, TRIANGLE_FAN_SIZE);
469 glDisableVertexAttribArray(position);
470
471 return true;
472 }
473
Rotate2d(GLfloat centerX,GLfloat centerY,GLfloat * rotateX,GLfloat * rotateY,GLfloat theta)474 void EGLCore::Rotate2d(GLfloat centerX, GLfloat centerY, GLfloat* rotateX, GLfloat* rotateY, GLfloat theta)
475 {
476 GLfloat tempX = cos(theta) * (*rotateX - centerX) - sin(theta) * (*rotateY - centerY);
477 GLfloat tempY = sin(theta) * (*rotateX - centerX) + cos(theta) * (*rotateY - centerY);
478 *rotateX = tempX + centerX;
479 *rotateY = tempY + centerY;
480 }
481
FinishDraw()482 bool EGLCore::FinishDraw()
483 {
484 // The gl function has no return value.
485 glFlush();
486 glFinish();
487 return eglSwapBuffers(eglDisplay_, eglSurface_);
488 }
489
LoadShader(GLenum type,const char * shaderSrc)490 GLuint EGLCore::LoadShader(GLenum type, const char* shaderSrc)
491 {
492 if ((type <= 0) || (shaderSrc == nullptr)) {
493 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "glCreateShader type or shaderSrc error");
494 return PROGRAM_ERROR;
495 }
496
497 GLuint shader = glCreateShader(type);
498 if (shader == 0) {
499 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "glCreateShader unable to load shader");
500 return PROGRAM_ERROR;
501 }
502
503 // The gl function has no return value.
504 glShaderSource(shader, 1, &shaderSrc, nullptr);
505 glCompileShader(shader);
506
507 GLint compiled;
508 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
509 if (compiled != 0) {
510 return shader;
511 }
512
513 GLint infoLen = 0;
514 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
515 if (infoLen <= 1) {
516 glDeleteShader(shader);
517 return PROGRAM_ERROR;
518 }
519
520 char* infoLog = (char*)malloc(sizeof(char) * (infoLen + 1));
521 if (infoLog != nullptr) {
522 memset(infoLog, 0, infoLen + 1);
523 glGetShaderInfoLog(shader, infoLen, nullptr, infoLog);
524 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "glCompileShader error = %s", infoLog);
525 free(infoLog);
526 infoLog = nullptr;
527 }
528 glDeleteShader(shader);
529 return PROGRAM_ERROR;
530 }
531
CreateProgram(const char * vertexShader,const char * fragShader)532 GLuint EGLCore::CreateProgram(const char* vertexShader, const char* fragShader)
533 {
534 if ((vertexShader == nullptr) || (fragShader == nullptr)) {
535 OH_LOG_Print(
536 LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "createProgram: vertexShader or fragShader is null");
537 return PROGRAM_ERROR;
538 }
539
540 GLuint vertex = LoadShader(GL_VERTEX_SHADER, vertexShader);
541 if (vertex == PROGRAM_ERROR) {
542 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "createProgram vertex error");
543 return PROGRAM_ERROR;
544 }
545
546 GLuint fragment = LoadShader(GL_FRAGMENT_SHADER, fragShader);
547 if (fragment == PROGRAM_ERROR) {
548 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "createProgram fragment error");
549 return PROGRAM_ERROR;
550 }
551
552 GLuint program = glCreateProgram();
553 if (program == PROGRAM_ERROR) {
554 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "createProgram program error");
555 glDeleteShader(vertex);
556 glDeleteShader(fragment);
557 return PROGRAM_ERROR;
558 }
559
560 // The gl function has no return value.
561 glAttachShader(program, vertex);
562 glAttachShader(program, fragment);
563 glLinkProgram(program);
564
565 GLint linked;
566 glGetProgramiv(program, GL_LINK_STATUS, &linked);
567 if (linked != 0) {
568 glDeleteShader(vertex);
569 glDeleteShader(fragment);
570 return program;
571 }
572
573 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "createProgram linked error");
574 GLint infoLen = 0;
575 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLen);
576 if (infoLen > 1) {
577 char* infoLog = (char*)malloc(sizeof(char) * (infoLen + 1));
578 memset(infoLog, 0, infoLen + 1);
579 glGetProgramInfoLog(program, infoLen, nullptr, infoLog);
580 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "glLinkProgram error = %s", infoLog);
581 free(infoLog);
582 infoLog = nullptr;
583 }
584 glDeleteShader(vertex);
585 glDeleteShader(fragment);
586 glDeleteProgram(program);
587 return PROGRAM_ERROR;
588 }
589
UpdateSize(int width,int height)590 void EGLCore::UpdateSize(int width, int height)
591 {
592 width_ = width;
593 height_ = height;
594 if (width_ > 0) {
595 widthPercent_ = FIFTY_PERCENT * height_ / width_;
596 }
597 }
598
Release()599 void EGLCore::Release()
600 {
601 if ((eglDisplay_ == nullptr) || (eglSurface_ == nullptr) || (!eglDestroySurface(eglDisplay_, eglSurface_))) {
602 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Release eglDestroySurface failed");
603 }
604
605 if ((eglDisplay_ == nullptr) || (eglContext_ == nullptr) || (!eglDestroyContext(eglDisplay_, eglContext_))) {
606 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Release eglDestroyContext failed");
607 }
608
609 if ((eglDisplay_ == nullptr) || (!eglTerminate(eglDisplay_))) {
610 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Release eglTerminate failed");
611 }
612 }
613 } // namespace NativeXComponentSample
614