• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_9 = 9;
32     constexpr int32_t NUM_8 = 8;
33     constexpr int32_t NUM_7 = 7;
34     constexpr int32_t NUM_6 = 6;
35     constexpr int32_t NUM_5 = 5;
36     constexpr int32_t NUM_4 = 4;
37     constexpr int32_t NUM_3 = 3;
38     constexpr int32_t NUM_2 = 2;
39     constexpr int32_t NUM_1 = 1;
40     constexpr int32_t NUM_1024 = 1024;
41     constexpr int32_t NUM_180 = 180;
42     constexpr int32_t NUM_72 = 72;
43     constexpr int32_t NUM_54 = 54;
44     constexpr int32_t NUM_18 = 18;
45     /**
46      * Vertex shader.
47      */
48     const char EGLDRAW_VERTEX_SHADER[] = "#version 300 es\n"
49                                          "layout(location = 0) in vec4 a_position;\n"
50                                          "layout(location = 1) in vec4 a_color;   \n"
51                                          "out vec4 v_color;                       \n"
52                                          "void main()                             \n"
53                                          "{                                       \n"
54                                          "   gl_Position = a_position;            \n"
55                                          "   v_color = a_color;                   \n"
56                                          "}                                       \n";
57 
58     /**
59      * Fragment shader.
60      */
61     const char EGLDRAW_FRAGMENT_SHADER[] = "#version 300 es\n"
62                                            "precision mediump float;                  \n"
63                                            "in vec4 v_color;                          \n"
64                                            "out vec4 fragColor;                       \n"
65                                            "void main()                               \n"
66                                            "{                                         \n"
67                                            "   fragColor = v_color;                   \n"
68                                            "}                                         \n";
69 
70     /**
71      * Texture Render Vertex shader.
72      */
73     const char EGLTR_VERTEX_SHADER[] = "#version 300 es\n"
74                                        "layout (location = 0) in vec3 aPos;                  \n"
75                                        "layout (location = 1) in vec3 aColor;                \n"
76                                        "layout (location = 2) in vec2 aTexCoord;             \n"
77                                        "out vec3 ourColor;                                     \n"
78                                        "out vec2 TexCoord;                                     \n"
79                                        "void main()                                            \n"
80                                        "{                                                      \n"
81                                        "    gl_Position = vec4(aPos, 1.0);                     \n"
82                                        "    ourColor = aColor;                                 \n"
83                                        "    TexCoord = vec2(aTexCoord.x, aTexCoord.y);         \n"
84                                        "}                                                      \n";
85 
86     /**
87      * Texture Render Fragment shader.
88      */
89     const char EGLTR_FRAGMENT_SHADER[] = "#version 300 es   \n"
90                                          "precision mediump float;                           \n"
91                                          "in vec3 ourColor;                                  \n"
92                                          "in vec2 TexCoord;                                  \n"
93                                          "uniform lowp sampler2D texture1;                   \n"
94                                          "out vec4 FragColor;                                \n"
95                                          "void main()                                        \n"
96                                          "{                                                  \n"
97                                          "    FragColor = texture(texture1, TexCoord);       \n"
98                                          "}                                                \n";
99 
100     /**
101      * Background color #801dae 青莲.
102      */
103     const GLfloat QL_BACKGROUND_COLOR[] = {128.0f / 255, 29.0f / 255, 174.0f / 255, 1.0f};
104 
105     /**
106      * Background color #f20c00 石榴红.
107      */
108     const GLfloat SLH_BACKGROUND_COLOR[] = {242.0f / 255, 12.0f / 255, 00.0f / 255, 1.0f};
109 
110     /**
111      * Background color #f20c00 花青.
112      */
113     const GLfloat HQ_BACKGROUND_COLOR[] = {00.0f / 255, 52.0f / 255, 114.0f / 255, 1.0f};
114 
115     /**
116      * Draw color #7E8FFB.
117      */
118     const GLfloat DRAW_COLOR[] = {126.0f / 255, 143.0f / 255, 251.0f / 255, 1.0f};
119 
120     /**
121      * Draw color #a3d900.
122      */
123     const GLfloat CH_DRAW_COLOR[] = {126.0f / 255, 143.0f / 255, 251.0f / 255, 1.0f};
124 
125     /**
126      * Draw color #057748.
127      */
128     const GLfloat SHL_DRAW_COLOR[] = {5.0f / 255, 119.0f / 255, 72.0f / 255, 1.0f};
129 
130 
131     /**
132      * Change color #92D6CC.
133      */
134     const GLfloat CHANGE_COLOR[] = {146.0f / 255, 214.0f / 255, 204.0f / 255, 1.0f};
135 
136     /**
137      * Background area.
138      */
139     const GLfloat BACKGROUND_RECTANGLE_VERTICES[] = {-1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f, -1.0f};
140 
141     /**
142      * Get context parameter count.
143      */
144     const size_t GET_CONTEXT_PARAM_CNT = 1;
145 
146     /**
147      * Fifty percent.
148      */
149     const float FIFTY_PERCENT = 0.5;
150 
151     /**
152      * Pointer size.
153      */
154     const GLint POINTER_SIZE = 2;
155 
156     /**
157      * Triangle fan size.
158      */
159     const GLsizei TRIANGLE_FAN_SIZE = 4;
160 
161     /**
162      * Egl red size default.
163      */
164     const int EGL_RED_SIZE_DEFAULT = 8;
165 
166     /**
167      * Egl green size default.
168      */
169     const int EGL_GREEN_SIZE_DEFAULT = 8;
170 
171     /**
172      * Egl blue size default.
173      */
174     const int EGL_BLUE_SIZE_DEFAULT = 8;
175 
176     /**
177      * Egl alpha size default.
178      */
179     const int EGL_ALPHA_SIZE_DEFAULT = 8;
180 
181     /**
182      * Default x position.
183      */
184     const int DEFAULT_X_POSITION = 0;
185 
186     /**
187      * Default y position.
188      */
189     const int DEFAULT_Y_POSITION = 0;
190 
191     /**
192      * Gl red default.
193      */
194     const GLfloat GL_RED_DEFAULT = 0.0;
195 
196     /**
197      * Gl green default.
198      */
199     const GLfloat GL_GREEN_DEFAULT = 0.0;
200 
201     /**
202      * Gl blue default.
203      */
204     const GLfloat GL_BLUE_DEFAULT = 0.0;
205 
206     /**
207      * Gl alpha default.
208      */
209     const GLfloat GL_ALPHA_DEFAULT = 1.0;
210 
211     /**
212      * Program error.
213      */
214     const GLuint PROGRAM_ERROR = 0;
215 
216     /**
217      * Shape vertices size.
218      */
219     const int SHAPE_VERTICES_SIZE = 8;
220 
221     /**
222      * Position handle name.
223      */
224     const char POSITION_NAME[] = "a_position";
225 
226     /**
227      * Position error.
228      */
229     const GLint POSITION_ERROR = -1;
230 
231     /**
232      * Config attribute list.
233      */
234     const EGLint ATTRIB_LIST[] = {
235         // Key,value.
236         EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RED_SIZE, EGL_RED_SIZE_DEFAULT, EGL_GREEN_SIZE, EGL_GREEN_SIZE_DEFAULT,
237         EGL_BLUE_SIZE, EGL_BLUE_SIZE_DEFAULT, EGL_ALPHA_SIZE, EGL_ALPHA_SIZE_DEFAULT, EGL_RENDERABLE_TYPE,
238         EGL_OPENGL_ES2_BIT,
239         // End.
240         EGL_NONE};
241 
242     /**
243      * Context attributes.
244      */
245     const EGLint CONTEXT_ATTRIBS[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
246 } // namespace
EglContextInit(void * window,int width,int height)247 bool EGLCore::EglContextInit(void* window, int width, int height)
248 {
249     OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "EGLCore", "EglContextInit execute");
250     if ((window == nullptr) || (width <= 0) || (height <= 0)) {
251         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "EglContextInit: param error");
252         return false;
253     }
254 
255     UpdateSize(width, height);
256     eglWindow_ = static_cast<EGLNativeWindowType>(window);
257 
258     // Init Display.
259     eglDisplay_ = eglGetDisplay(EGL_DEFAULT_DISPLAY);
260     if (eglDisplay_ == EGL_NO_DISPLAY) {
261         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "eglGetDisplay: unable to get EGL Display");
262         return false;
263     }
264 
265     EGLint majorVersion;
266     EGLint minorVersion;
267     if (!eglInitialize(eglDisplay_, &majorVersion, &minorVersion)) {
268         OH_LOG_Print(
269             LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "eglInitialize: unable to get initialize EGL Display");
270         return false;
271     }
272 
273     // Select configuration.
274     const EGLint maxConfigSize = 1;
275     EGLint numConfigs;
276     if (!eglChooseConfig(eglDisplay_, ATTRIB_LIST, &eglConfig_, maxConfigSize, &numConfigs)) {
277         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "eglChooseConfig: unable to choose configs");
278         return false;
279     }
280 
281     return CreateEnvironment();
282 }
283 
doCreateEnvironment()284 bool EGLCore::doCreateEnvironment()
285 {
286     if (!eglMakeCurrent(eglDisplay_, eglSurface_, eglSurface_, eglContext_)) {
287         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "eglMakeCurrent failed");
288         eglDestroyContext(eglDisplay_, eglContext_);
289         eglDestroySurface(eglDisplay_, eglSurface_);
290         eglTerminate(eglDisplay_);
291         return false;
292     }
293     // Create program.
294     program_ = TRCreateProgram(EGLTR_VERTEX_SHADER, EGLTR_FRAGMENT_SHADER);
295     if (program_ == PROGRAM_ERROR) {
296         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "CreateProgram: unable to create program");
297         return false;
298     }
299     OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "EGLCore", "CreateProgram: success!");
300     return true;
301 }
302 
CreateEnvironment()303 bool EGLCore::CreateEnvironment()
304 {
305     // Create surface.
306     if (eglWindow_ == nullptr) {
307         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "eglWindow_ is null");
308         return false;
309     }
310     eglSurface_ = eglCreateWindowSurface(eglDisplay_, eglConfig_, eglWindow_, nullptr);
311     if (eglSurface_ == nullptr) {
312         OH_LOG_Print(
313             LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "eglCreateWindowSurface: unable to create");
314         eglTerminate(eglDisplay_);
315         return false;
316     }
317     // Create context.
318     eglContext_ = eglCreateContext(eglDisplay_, eglConfig_, EGL_NO_CONTEXT, CONTEXT_ATTRIBS);
319     if (eglContext_ == nullptr) {
320         OH_LOG_Print(
321             LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "eglCreateWindowSurface: unable to create");
322         eglDestroySurface(eglDisplay_, eglSurface_);
323         eglTerminate(eglDisplay_);
324         return false;
325     }
326 
327     return doCreateEnvironment();
328 }
329 
TRBackground()330 void EGLCore::TRBackground()
331 {
332     float vertices[] = {
333         // positions          // colors           // texture coords
334          0.5f,  0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f, // top right
335          0.5f, -0.5f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f, // bottom right
336         -0.5f, -0.5f, 0.0f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f // bottom left
337     };
338     unsigned int indices[] = {
339         0, 1, 3, // first triangle
340         1, 2, 3  // second triangle
341     };
342     unsigned int vbo = 0;
343     unsigned int vao = 0;
344     unsigned int ebo = 0;
345     glGenVertexArrays(1, &vao);
346     glGenBuffers(1, &vbo);
347     glGenBuffers(1, &ebo);
348 
349     glBindVertexArray(vao);
350 
351     glBindBuffer(GL_ARRAY_BUFFER, vbo);
352     glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
353 
354     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
355     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
356 
357     // position attribute
358     glVertexAttribPointer(0, NUM_3, GL_FLOAT, GL_FALSE, NUM_8 * sizeof(float), (void*)0);
359     glEnableVertexAttribArray(0);
360     // color attribute
361     glVertexAttribPointer(1, NUM_3, GL_FLOAT, GL_FALSE, NUM_8 * sizeof(float), (void*)(NUM_3 * sizeof(float)));
362     glEnableVertexAttribArray(1);
363     // texture coord attribute
364     glVertexAttribPointer(NUM_2, NUM_2, GL_FLOAT, GL_FALSE, NUM_8 * sizeof(float), (void*)(NUM_6 * sizeof(float)));
365     glEnableVertexAttribArray(NUM_2);
366 
367     // load and create a texture
368     // -------------------------
369     unsigned int texture;
370     glGenTextures(1, &texture);
371     // all upcoming GL_TEXTURE_2D operations now have effect on this texture object
372     glBindTexture(GL_TEXTURE_2D, texture);
373     // set the texture wrapping parameters
374     // set texture wrapping to GL_CLAMP_TO_EDGE (default wrapping method)
375     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
376     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
377     // set texture filtering parameters
378     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
379     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
380     // load image, create texture and generate mipmaps
381 
382     glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
383     glClear(GL_COLOR_BUFFER_BIT);
384 
385     glBindTexture(GL_TEXTURE_2D, texture);
386 
387     glUseProgram(program_);
388 
389     glBindVertexArray(vao);
390     glDrawElements(GL_TRIANGLES, NUM_6, GL_UNSIGNED_INT, 0);
391 
392     eglSwapBuffers(eglDisplay_, eglSurface_);
393 }
394 
Background()395 void EGLCore::Background()
396 {
397     GLint position = PrepareDraw();
398     if (position == POSITION_ERROR) {
399         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Background get position failed");
400         return;
401     }
402 
403     if (!ExecuteDrawBG(position, SLH_BACKGROUND_COLOR,
404         BACKGROUND_RECTANGLE_VERTICES, sizeof(BACKGROUND_RECTANGLE_VERTICES))) {
405         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Background execute draw failed");
406         return;
407     }
408 
409     if (!FinishDraw()) {
410         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Background FinishDraw failed");
411         return;
412     }
413 }
414 
ReadBmpData(FILE * file,uint32_t bmpSize)415 void EGLCore::ReadBmpData(FILE *file, uint32_t bmpSize)
416 {
417     if (bmpSize < 0) {
418         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "ResfdExecuteCB malloc failed!");
419         return;
420     }
421     unsigned char *bmpData = (unsigned char *)malloc(bmpSize);
422     if (bmpData == nullptr) {
423         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "ResfdExecuteCB malloc failed!");
424         fclose(file);
425         return false;
426     }
427     if (fseek(file, foff_ + NUM_54, SEEK_SET) != 0) {
428         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "ResfdExecuteCB fseek failed!");
429         return false;
430     }
431     size_t readCnt = fread(bmpData, bmpSize, NUM_1, file);
432     if (readCnt == 0) {
433         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "ResfdExecuteCB read_cnt is 0!");
434     }
435 
436     if (fclose(file) != 0) {
437         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "ResfdExecuteCB fclose failed!");
438         return false;
439     }
440     OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "EGLCore",
441         "bmp_width:%{public}d, height%{public}d, size%{public}d, bit%{public}d, cnt%{public}zu",
442         bmpWidth, bmpHeight, bmpSize, bmpBit, readCnt);
443     free(bmpData);
444 }
445 
LoadTexture()446 GLuint EGLCore::LoadTexture()
447 {
448     uint32_t bmpWidth = 0;
449     uint32_t bmpHeight = 0;
450     uint32_t bmpSize = 0;
451     uint8_t bmpBit = 0;
452 
453     FILE *file = fdopen(fd_, "r");
454     if (file == nullptr) {
455         OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "EGLCore", "ResfdExecuteCB fdopen failed!");
456         return false;
457     }
458 
459     if (fseek(file, foff_ + NUM_18, SEEK_SET) != 0) {
460         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "ResfdExecuteCB fseek failed!");
461         return false;
462     }
463 
464     if (fread(&bmpWidth, NUM_4, NUM_1, file) != 0) {
465         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "ResfdExecuteCB fread failed!");
466     }
467     if (fread(&bmpHeight, NUM_4, NUM_1, file) != 0) {
468         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "ResfdExecuteCB fread failed!");
469     }
470     if (fread(&bmpBit, NUM_1, NUM_1, file) != 0) {
471         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "ResfdExecuteCB fread failed!");
472     }
473     if (fread(&bmpBit, NUM_1, NUM_1, file) != 0) {
474         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "ResfdExecuteCB fread failed!");
475     }
476     if (fread(&bmpBit, NUM_1, NUM_1, file) != 0) {
477         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "ResfdExecuteCB fread failed!");
478     }
479     bmpSize = flen_ - NUM_54;
480     ReadBmpData(FILE *file, uint32_t bmpSize);
481 
482     return 0;
483 }
484 
Display(GLuint texGround)485 void EGLCore::Display(GLuint texGround)
486 {
487     // 清空颜色缓冲区
488     glClear(GL_COLOR_BUFFER_BIT);
489 
490     // 使用着色器程序
491     glUseProgram(program_);
492 
493     // 绑定纹理
494     glBindTexture(GL_TEXTURE_2D, texGround);
495 
496     // 获取顶点坐标和纹理坐标的位置
497     GLint aPositionLocation = glGetAttribLocation(program_, "aPosition");
498     GLint aTexCoordLocation = glGetAttribLocation(program_, "aTexCoord");
499 
500     // 启用顶点属性数组
501     glEnableVertexAttribArray(aPositionLocation);
502     glEnableVertexAttribArray(aTexCoordLocation);
503 
504     // 设置顶点属性指针
505     glVertexAttribPointer(aPositionLocation, NUM_3, GL_FLOAT, GL_FALSE, 0, BACKGROUND_RECTANGLE_VERTICES);
506     glVertexAttribPointer(aTexCoordLocation, NUM_2, GL_FLOAT, GL_FALSE, 0, BACKGROUND_RECTANGLE_VERTICES);
507 
508     // 绘制矩形
509     glDrawArrays(GL_TRIANGLES, 0, NUM_6);
510 
511     // 禁用顶点属性数组
512     glDisableVertexAttribArray(aPositionLocation);
513     glDisableVertexAttribArray(aTexCoordLocation);
514 
515     eglSwapBuffers(eglDisplay_, eglSurface_);
516 }
517 
DrawBmp(uint32_t fd,uint32_t foff,uint32_t flen,int & hasDraw)518 void EGLCore::DrawBmp(uint32_t fd, uint32_t foff, uint32_t flen, int& hasDraw)
519 {
520     flag_ = false;
521     OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "EGLCore", "Drawbmp");
522 
523     FILE *file = fdopen(fd, "r");
524     if (file == nullptr) {
525         OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "EGLCore", "ResfdExecuteCB fdopen failed!");
526         return;
527     }
528 
529     if (fseek(file, foff, SEEK_SET) != 0) {
530         OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "EGLCore", "ResfdExecuteCB fseek failed!");
531         fclose(file);
532         return;
533     }
534 
535     if (flen <= 0) {
536         OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "EGLCore", "ResfdExecuteCB fseek failed!");
537         fclose(file);
538         return;
539     }
540     unsigned char *mediaData = new unsigned char[flen];
541     if (mediaData == nullptr) {
542         OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "EGLCore", "new buffer failed!");
543         fclose(file);
544         return;
545     }
546     size_t readCnt = fread(mediaData, sizeof(unsigned char), flen, file);
547     if (readCnt > 0) {
548         OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "EGLCore", "readcnt: %{public}zu", readCnt);
549     }
550 
551     int width = 0;
552     int height = 0;
553     unsigned char *pdata = mediaData + NUM_18;
554     OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN,
555         "EGLCore", "width: %{public}d", *(static_case<uint32_t *>(pdata)));
556     width = *(static_case<uint32_t *>(pdata));
557     pdata = mediaData + NUM_18 + NUM_4;
558     OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN,
559         "EGLCore", "height: %{public}d", *(static_case<uint32_t *>(pdata)));
560     height = *(static_case<uint32_t *>(pdata));
561 
562     float vertices[] = {
563         // positions          // colors           // texture coords
564          1.0f,  1.0f, 0.0f,   1.0f, 0.0f, 0.0f,   2.0f, 2.0f, // top right
565          1.0f,  -1.0f, 0.0f,   0.0f, 1.0f, 0.0f,   2.0f, -1.0f, // bottom right
566          -1.0f,  -1.0f, 0.0f,   0.0f, 0.0f, 1.0f,   -1.0f, -1.0f, // bottom left
567          -1.0f,  1.0f, 0.0f,   1.0f, 1.0f, 0.0f,   -1.0f, 2.0f  // top left
568     };
569 
570     unsigned int indices[] = {
571         0, 1, 3, // first triangle
572         1, 2, 3  // second triangle
573     };
574     unsigned int vbo = 0;
575     unsigned int vao = 0;
576     unsigned int ebo = 0;
577     glGenVertexArrays(1, &vao);
578     glGenBuffers(1, &vbo);
579     glGenBuffers(1, &ebo);
580 
581     glBindVertexArray(vao);
582 
583     glBindBuffer(GL_ARRAY_BUFFER, vbo);
584     glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
585 
586     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
587     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
588 
589     // position attribute
590     glVertexAttribPointer(0, NUM_3, GL_FLOAT, GL_FALSE, NUM_8 * sizeof(float), (void*)0);
591     glEnableVertexAttribArray(0);
592     // color attribute
593     glVertexAttribPointer(1, NUM_3, GL_FLOAT, GL_FALSE, NUM_8 * sizeof(float), (void*)(NUM_3 * sizeof(float)));
594     glEnableVertexAttribArray(1);
595     // texture coord attribute
596     glVertexAttribPointer(NUM_2, NUM_2, GL_FLOAT, GL_FALSE, NUM_8 * sizeof(float), (void*)(NUM_6 * sizeof(float)));
597     glEnableVertexAttribArray(NUM_2);
598 
599     // load and create a texture
600     // -------------------------
601     unsigned int texture;
602     glGenTextures(1, &texture);
603     glBindTexture(GL_TEXTURE_2D, texture);
604     // all upcoming GL_TEXTURE_2D operations now have effect on this texture object
605     // set the texture wrapping parameters
606     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
607     // set texture wrapping to GL_CLAMP_TO_EDGE (default wrapping method)
608     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
609     // set texture filtering parameters
610     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
611     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
612     // load image, create texture and generate mipmaps
613 
614     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, mediaData);
615     glGenerateMipmap(GL_TEXTURE_2D);
616 
617     glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
618     glClear(GL_COLOR_BUFFER_BIT);
619 
620     glBindTexture(GL_TEXTURE_2D, texture);
621 
622     glUseProgram(program_);
623 
624     glBindVertexArray(vao);
625     glDrawElements(GL_TRIANGLES, NUM_6, GL_UNSIGNED_INT, 0);
626 
627     eglSwapBuffers(eglDisplay_, eglSurface_);
628 
629     hasDraw = 1;
630     flag_ = true;
631 }
632 
Draw(int & hasDraw)633 void EGLCore::Draw(int& hasDraw)
634 {
635     flag_ = false;
636     OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "EGLCore", "Draw");
637     GLint position = PrepareDraw();
638     if (position == POSITION_ERROR) {
639         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Draw get position failed");
640         return;
641     }
642 
643     if (!ExecuteDrawBG(position, QL_BACKGROUND_COLOR,
644         BACKGROUND_RECTANGLE_VERTICES, sizeof(BACKGROUND_RECTANGLE_VERTICES))) {
645         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Draw execute draw background failed");
646         return;
647     }
648 
649     // Divided into five quadrilaterals and calculate one of the quadrilateral's Vertices
650     GLfloat rotateX = 0;
651     GLfloat rotateY = FIFTY_PERCENT * height_;
652     GLfloat centerX = 0;
653     // Convert DEG(54° & 18°) to RAD
654     GLfloat centerY = -rotateY * (M_PI / NUM_180 * NUM_54) * (M_PI / NUM_180 * NUM_54);
655     // Convert DEG(18°) to RAD
656     GLfloat leftX = -rotateY * (M_PI / NUM_180 * NUM_18);
657     GLfloat leftY = 0;
658     // Convert DEG(18°) to RAD
659     GLfloat rightX = rotateY * (M_PI / NUM_180 * NUM_18);
660     GLfloat rightY = 0;
661 
662     const GLfloat shapeVertices[] = { centerX / width_, centerY / height_, leftX / width_, leftY / height_,
663         rotateX / width_, rotateY / height_, rightX / width_, rightY / height_ };
664 
665     if (!ExecuteDrawStar(position, DRAW_COLOR, shapeVertices, sizeof(shapeVertices))) {
666         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Draw execute draw shape failed");
667         return;
668     }
669 
670     if (!FinishDraw()) {
671         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Draw FinishDraw failed");
672         return;
673     }
674     hasDraw = 1;
675 
676     flag_ = true;
677 }
678 
ChangeColor(int & hasChangeColor)679 void EGLCore::ChangeColor(int& hasChangeColor)
680 {
681     if (!flag_) {
682         return;
683     }
684     OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "EGLCore", "ChangeColor");
685     GLint position = PrepareDraw();
686     if (position == POSITION_ERROR) {
687         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "ChangeColor get position failed");
688         return;
689     }
690 
691     if (!ExecuteDrawBG(position, SLH_BACKGROUND_COLOR,
692         BACKGROUND_RECTANGLE_VERTICES, sizeof(BACKGROUND_RECTANGLE_VERTICES))) {
693         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "ChangeColor execute draw background failed");
694         return;
695     }
696 
697     // Divided into five quadrilaterals and calculate one of the quadrilateral's Vertices
698     GLfloat rotateX = 0;
699     GLfloat rotateY = FIFTY_PERCENT * height_;
700     GLfloat centerX = 0;
701     // Convert DEG(54° & 18°) to RAD
702     GLfloat centerY = -rotateY * (M_PI / NUM_180 * NUM_54) * (M_PI / NUM_180 * NUM_18);
703     // Convert DEG(18°) to RAD
704     GLfloat leftX = -rotateY * (M_PI / NUM_180 * NUM_18);
705     GLfloat leftY = 0;
706     // Convert DEG(18°) to RAD
707     GLfloat rightX = rotateY * (M_PI / NUM_180 * NUM_18);
708     GLfloat rightY = 0;
709 
710     const GLfloat shapeVertices[] = { centerX / width_, centerY / height_, leftX / width_, leftY / height_,
711         rotateX / width_, rotateY / height_, rightX / width_, rightY / height_ };
712 
713     if (!ExecuteDrawNewStar(0, CHANGE_COLOR, shapeVertices, sizeof(shapeVertices))) {
714         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Draw execute draw shape failed");
715         return;
716     }
717 
718     // Convert DEG(72°) to RAD
719     GLfloat rad = M_PI / NUM_180 * NUM_72;
720     // Rotate four times
721     for (int i = 0; i < NUM_4; ++i) {
722         Rotate2d(centerX, centerY, &rotateX, &rotateY, rad);
723         Rotate2d(centerX, centerY, &leftX, &leftY, rad);
724         Rotate2d(centerX, centerY, &rightX, &rightY, rad);
725         const GLfloat shapeVertices[] = { centerX / width_, centerY / height_, leftX / width_, leftY / height_,
726             rotateX / width_, rotateY / height_, rightX / width_, rightY / height_ };
727 
728         if (!ExecuteDrawNewStar(position, CHANGE_COLOR, shapeVertices, sizeof(shapeVertices))) {
729             OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Draw execute draw shape failed");
730             return;
731         }
732     }
733 
734     if (!FinishDraw()) {
735         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "ChangeColor FinishDraw failed");
736     }
737     hasChangeColor = 1;
738 }
739 
PrepareDraw()740 GLint EGLCore::PrepareDraw()
741 {
742     if ((eglDisplay_ == nullptr) || (eglSurface_ == nullptr) || (eglContext_ == nullptr) ||
743         (!eglMakeCurrent(eglDisplay_, eglSurface_, eglSurface_, eglContext_))) {
744         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "PrepareDraw: param error");
745         return POSITION_ERROR;
746     }
747 
748     // The gl function has no return value.
749     glViewport(DEFAULT_X_POSITION, DEFAULT_Y_POSITION, width_, height_);
750     glClearColor(GL_RED_DEFAULT, GL_GREEN_DEFAULT, GL_BLUE_DEFAULT, GL_ALPHA_DEFAULT);
751     glClear(GL_COLOR_BUFFER_BIT);
752     glUseProgram(program_);
753 
754     return glGetAttribLocation(program_, POSITION_NAME);
755 }
756 
ExecuteDrawBG(GLint position,const GLfloat * color,const GLfloat shapeVertices[],unsigned long vertSize)757 bool EGLCore::ExecuteDrawBG(GLint position, const GLfloat* color, const GLfloat shapeVertices[], unsigned long vertSize)
758 {
759     if ((position > 0) || (color == nullptr) || (vertSize / sizeof(shapeVertices[0])) != SHAPE_VERTICES_SIZE) {
760         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "ExecuteDraw: param error");
761         return false;
762     }
763 
764     // The gl function has no return value.
765     glVertexAttribPointer(position, POINTER_SIZE, GL_FLOAT, GL_FALSE, 0, shapeVertices);
766     glEnableVertexAttribArray(position);
767     glVertexAttrib4fv(1, color);
768     glDrawArrays(GL_TRIANGLE_FAN, 0, TRIANGLE_FAN_SIZE);
769     glDisableVertexAttribArray(position);
770 
771     return true;
772 }
773 
ExecuteDrawStar(GLint position,const GLfloat * color,const GLfloat shapeVertices[],unsigned long vertSize)774 bool EGLCore::ExecuteDrawStar(
775     GLint position, const GLfloat* color, const GLfloat shapeVertices[], unsigned long vertSize)
776 {
777     if ((position > 0) || (color == nullptr) || (vertSize / sizeof(shapeVertices[0])) != SHAPE_VERTICES_SIZE) {
778         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "ExecuteDraw: param error");
779         return false;
780     }
781 
782     // The gl function has no return value.
783     glVertexAttribPointer(position, POINTER_SIZE, GL_FLOAT, GL_FALSE, 0, shapeVertices);
784     glVertexAttribPointer(1, POINTER_SIZE, GL_FLOAT, GL_FALSE, 0, color);
785     glEnableVertexAttribArray(position);
786     glEnableVertexAttribArray(1);
787     glVertexAttrib4fv(1, color);
788     glDrawArrays(GL_TRIANGLE_FAN, 0, TRIANGLE_FAN_SIZE);
789     glDisableVertexAttribArray(position);
790     glDisableVertexAttribArray(1);
791 
792     return true;
793 }
794 
ExecuteDrawNewStar(GLint position,const GLfloat * color,const GLfloat shapeVertices[],unsigned long vertSize)795 bool EGLCore::ExecuteDrawNewStar(
796     GLint position, const GLfloat* color, const GLfloat shapeVertices[], unsigned long vertSize)
797 {
798     if ((position > 0) || (color == nullptr) || (vertSize / sizeof(shapeVertices[0])) != SHAPE_VERTICES_SIZE) {
799         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "ExecuteDraw: param error");
800         return false;
801     }
802 
803     // The gl function has no return value.
804     glVertexAttribPointer(position, POINTER_SIZE, GL_FLOAT, GL_FALSE, 0, shapeVertices);
805     glEnableVertexAttribArray(position);
806     glVertexAttrib4fv(1, color);
807     glDrawArrays(GL_TRIANGLE_FAN, 0, TRIANGLE_FAN_SIZE);
808     glDisableVertexAttribArray(position);
809 
810     return true;
811 }
812 
Rotate2d(GLfloat centerX,GLfloat centerY,GLfloat * rotateX,GLfloat * rotateY,GLfloat theta)813 void EGLCore::Rotate2d(GLfloat centerX, GLfloat centerY, GLfloat* rotateX, GLfloat* rotateY, GLfloat theta)
814 {
815     GLfloat tempX = cos(theta) * (*rotateX - centerX) - sin(theta) * (*rotateY - centerY);
816     GLfloat tempY = sin(theta) * (*rotateX - centerX) + cos(theta) * (*rotateY - centerY);
817     *rotateX = tempX + centerX;
818     *rotateY = tempY + centerY;
819 }
820 
FinishDraw()821 bool EGLCore::FinishDraw()
822 {
823     // The gl function has no return value.
824     glFlush();
825     glFinish();
826     return eglSwapBuffers(eglDisplay_, eglSurface_);
827 }
828 
LoadShader(GLenum type,const char * shaderSrc)829 GLuint EGLCore::LoadShader(GLenum type, const char* shaderSrc)
830 {
831     if ((type <= 0) || (shaderSrc == nullptr)) {
832         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "glCreateShader type or shaderSrc error");
833         return PROGRAM_ERROR;
834     }
835 
836     GLuint shader = glCreateShader(type);
837     if (shader == 0) {
838         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "glCreateShader unable to load shader");
839         return PROGRAM_ERROR;
840     }
841 
842     // The gl function has no return value.
843     glShaderSource(shader, 1, &shaderSrc, nullptr);
844     glCompileShader(shader);
845 
846     GLint compiled;
847     glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
848     if (compiled != 0) {
849         return shader;
850     }
851 
852     GLint infoLen = 0;
853     glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
854     if (infoLen <= 1) {
855         glDeleteShader(shader);
856         return PROGRAM_ERROR;
857     }
858 
859     char* infoLog = (char*)malloc(sizeof(char) * (infoLen + 1));
860     if (infoLog != nullptr) {
861         glGetShaderInfoLog(shader, infoLen, nullptr, infoLog);
862         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "glCompileShader error = %s", infoLog);
863         free(infoLog);
864         infoLog = nullptr;
865     }
866     glDeleteShader(shader);
867     return PROGRAM_ERROR;
868 }
869 
CheckCompileErrors(unsigned int shader,std::string type)870 void EGLCore::CheckCompileErrors(unsigned int shader, std::string type)
871 {
872     int success;
873     char infoLog[NUM_1024];
874     if (type != "PROGRAM") {
875         glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
876         if (!success) {
877             glGetShaderInfoLog(shader, NUM_1024, nullptr, infoLog);
878             OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore",
879                 "ERROR::SHADER_COMPILATION_ERROR of type: %{public}s", type.c_str());
880         }
881     } else {
882         glGetProgramiv(shader, GL_LINK_STATUS, &success);
883         if (!success) {
884             glGetProgramInfoLog(shader, NUM_1024, nullptr, infoLog);
885             OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore",
886                 "ERROR::PROGRAM_LINKING_ERROR of type: %{public}s", type.c_str());
887         }
888     }
889 }
890 
TRCreateProgram(const char * vertexShader,const char * fragShader)891 GLuint EGLCore::TRCreateProgram(const char *vertexShader, const char *fragShader)
892 {
893     if ((vertexShader == nullptr) || (fragShader == nullptr)) {
894         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore",
895                      "createProgram: vertexShader or fragShader is null");
896         return PROGRAM_ERROR;
897     }
898 
899     unsigned int vertex = 0;
900     unsigned int fragment = 0;
901     // vertex shader
902     vertex = glCreateShader(GL_VERTEX_SHADER);
903     glShaderSource(vertex, 1, &vertexShader, nullptr);
904     glCompileShader(vertex);
905     CheckCompileErrors(vertex, "VERTEX");
906     // fragment Shader
907     fragment = glCreateShader(GL_FRAGMENT_SHADER);
908     glShaderSource(fragment, 1, &fragShader, nullptr);
909     glCompileShader(fragment);
910     CheckCompileErrors(fragment, "FRAGMENT");
911 
912     GLuint program = glCreateProgram();
913     if (program == PROGRAM_ERROR) {
914         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "createProgram program error");
915         glDeleteShader(vertex);
916         glDeleteShader(fragment);
917         return PROGRAM_ERROR;
918     }
919 
920     // The gl function has no return value.
921     glAttachShader(program, vertex);
922     glAttachShader(program, fragment);
923     glLinkProgram(program);
924     CheckCompileErrors(program, "PROGRAM");
925     GLint linked;
926     glGetProgramiv(program, GL_LINK_STATUS, &linked);
927     if (linked != 0) {
928         glDeleteShader(vertex);
929         glDeleteShader(fragment);
930         return program;
931     }
932 
933     OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "createProgram linked error");
934     GLint infoLen = 0;
935     glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLen);
936     if (infoLen > 1) {
937         char *infoLog = (char *)malloc(sizeof(char) * (infoLen + 1));
938         glGetProgramInfoLog(program, infoLen, nullptr, infoLog);
939         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "glLinkProgram error = %{public}s", infoLog);
940         free(infoLog);
941         infoLog = nullptr;
942     }
943     glDeleteShader(vertex);
944     glDeleteShader(fragment);
945     glDeleteProgram(program);
946     return PROGRAM_ERROR;
947 }
948 
CreateProgram(const char * vertexShader,const char * fragShader)949 GLuint EGLCore::CreateProgram(const char* vertexShader, const char* fragShader)
950 {
951     if ((vertexShader == nullptr) || (fragShader == nullptr)) {
952         OH_LOG_Print(
953             LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "createProgram: vertexShader or fragShader is null");
954         return PROGRAM_ERROR;
955     }
956 
957     GLuint vertex = LoadShader(GL_VERTEX_SHADER, vertexShader);
958     if (vertex == PROGRAM_ERROR) {
959         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "createProgram vertex error");
960         return PROGRAM_ERROR;
961     }
962 
963     GLuint fragment = LoadShader(GL_FRAGMENT_SHADER, fragShader);
964     if (fragment == PROGRAM_ERROR) {
965         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "createProgram fragment error");
966         return PROGRAM_ERROR;
967     }
968 
969     GLuint program = glCreateProgram();
970     if (program == PROGRAM_ERROR) {
971         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "createProgram program error");
972         glDeleteShader(vertex);
973         glDeleteShader(fragment);
974         return PROGRAM_ERROR;
975     }
976 
977     // The gl function has no return value.
978     glAttachShader(program, vertex);
979     glAttachShader(program, fragment);
980     glLinkProgram(program);
981 
982     GLint linked;
983     glGetProgramiv(program, GL_LINK_STATUS, &linked);
984     if (linked != 0) {
985         glDeleteShader(vertex);
986         glDeleteShader(fragment);
987         return program;
988     }
989 
990     OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "createProgram linked error");
991     GLint infoLen = 0;
992     glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLen);
993     if (infoLen > 1) {
994         char* infoLog = (char*)malloc(sizeof(char) * (infoLen + 1));
995         glGetProgramInfoLog(program, infoLen, nullptr, infoLog);
996         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "glLinkProgram error = %s", infoLog);
997         free(infoLog);
998         infoLog = nullptr;
999     }
1000     glDeleteShader(vertex);
1001     glDeleteShader(fragment);
1002     glDeleteProgram(program);
1003     return PROGRAM_ERROR;
1004 }
1005 
UpdateSize(int width,int height)1006 void EGLCore::UpdateSize(int width, int height)
1007 {
1008     width_ = width;
1009     height_ = height;
1010     if (width_ > 0) {
1011         widthPercent_ = FIFTY_PERCENT * height_ / width_;
1012     }
1013 }
1014 
Release()1015 void EGLCore::Release()
1016 {
1017     if ((eglDisplay_ == nullptr) || (eglSurface_ == nullptr) || (!eglDestroySurface(eglDisplay_, eglSurface_))) {
1018         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Release eglDestroySurface failed");
1019     }
1020 
1021     if ((eglDisplay_ == nullptr) || (eglContext_ == nullptr) || (!eglDestroyContext(eglDisplay_, eglContext_))) {
1022         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Release eglDestroyContext failed");
1023     }
1024 
1025     if ((eglDisplay_ == nullptr) || (!eglTerminate(eglDisplay_))) {
1026         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "EGLCore", "Release eglTerminate failed");
1027     }
1028 }
1029 } // namespace NativeXComponentSample
1030