Advertisement
vpeter

test egl

Sep 22nd, 2022 (edited)
961
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.32 KB | None | 0 0
  1. /*
  2.    https://github.com/emscripten-core/emscripten/blob/main/test/test_egl.c
  3.    https://github.com/emscripten-core/emscripten/issues/4796
  4.    https://exchangetuts.com/opengl-es20-offscreen-context-for-fbo-rendering-1640599143994493
  5.  
  6.    ~/CoreELEC/CoreELEC.20/build.CoreELEC-Amlogic-ne.arm-20/toolchain/bin/armv8a-libreelec-linux-gnueabihf-gcc -I~/CoreELEC/CoreELEC.20/build.CoreELEC-Amlogic-ne.arm-20/toolchain/armv8a-libreelec-linux-gnueabihf/sysroot/usr/include test_egl_1.c -L~/CoreELEC/CoreELEC.20/build.CoreELEC-Amlogic-ne.arm-20/toolchain/armv8a-libreelec-linux-gnueabihf/sysroot/usr/lib -lEGL -o test_egl_1
  7.  */
  8. /*
  9.  * Copyright 2013 The Emscripten Authors.  All rights reserved.
  10.  * Emscripten is available under two separate licenses, the MIT license and the
  11.  * University of Illinois/NCSA Open Source License.  Both these licenses can be
  12.  * found in the LICENSE file.
  13.  */
  14.  
  15. #include <assert.h>
  16. #include <stdio.h>
  17. #include <EGL/egl.h>
  18.  
  19. #include <GLES2/gl2.h>
  20.  
  21. int main(int argc, char *argv[])
  22. {
  23.     EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
  24.     assert(display != EGL_NO_DISPLAY);
  25.     assert(eglGetError() == EGL_SUCCESS);
  26.  
  27.     EGLint major = 0, minor = 0;
  28.     EGLBoolean ret = eglInitialize(display, &major, &minor);
  29.     assert(eglGetError() == EGL_SUCCESS);
  30.     assert(ret == EGL_TRUE);
  31.     assert(major * 10000 + minor >= 10004);
  32.  
  33.     EGLint numConfigs;
  34.     ret = eglGetConfigs(display, NULL, 0, &numConfigs);
  35.     assert(eglGetError() == EGL_SUCCESS);
  36.     assert(ret == EGL_TRUE);
  37.  
  38. #if 0
  39.     EGLint attribs[] = {
  40.       EGL_RED_SIZE, 5,
  41.       EGL_GREEN_SIZE, 6,
  42.       EGL_BLUE_SIZE, 5,
  43.       EGL_NONE
  44.     };
  45. #else
  46.   eglBindAPI(EGL_OPENGL_ES_API);
  47.  
  48.   EGLint attribs[5];
  49.   attribs[0] = EGL_SURFACE_TYPE;
  50.   attribs[1] = EGL_WINDOW_BIT;
  51.   attribs[2] = EGL_RENDERABLE_TYPE;
  52.   attribs[3] = EGL_OPENGL_ES2_BIT;
  53.   attribs[4] = EGL_NONE;
  54.  
  55. #endif
  56.  
  57.     EGLConfig config;
  58.     ret = eglChooseConfig(display, attribs, &config, 1, &numConfigs);    
  59.     if (numConfigs != 1) {
  60.     printf("Error: eglChooseConfig(): config not found (numConfigs=%d).\n", numConfigs);
  61.     return(1);
  62.   }
  63.  
  64.     assert(eglGetError() == EGL_SUCCESS);
  65.     assert(ret == EGL_TRUE);
  66.  
  67. /////////////////////////////////////////////////////////////////
  68.     //EGLNativeWindowType dummyWindow;
  69.  
  70.     printf("before eglCreateWindowSurface\n");
  71.     //EGLSurface surface = eglCreateWindowSurface(display, config, dummyWindow, NULL);
  72.     EGLSurface surface = eglCreateWindowSurface(display, config, (EGLNativeWindowType) NULL, NULL);
  73.  
  74.     printf("surface=%p\n", surface);
  75.     printf("eglGetError()=0x%x\n", eglGetError());
  76.     assert(surface != 0);
  77.     assert(eglGetError() == EGL_SUCCESS);
  78.     printf("after eglCreateWindowSurface EGL_SUCCESS\n");
  79.    
  80.   EGLBoolean ret2 = eglDestroySurface(display, surface);
  81.     assert(eglGetError() == EGL_SUCCESS);
  82.     assert(ret2 == EGL_TRUE);
  83.  
  84.     printf("done\n");
  85.     return 0;
  86. /////////////////////////////////////////////////////////////////
  87.  
  88.  
  89.  
  90.     // WebGL maps to GLES2. GLES1 is not supported.
  91.     EGLint contextAttribsOld[] =
  92.     {
  93.         EGL_CONTEXT_CLIENT_VERSION, 1,
  94.         EGL_NONE
  95.     };
  96.     EGLContext context = eglCreateContext(display, config, NULL, contextAttribsOld);
  97.     assert(eglGetError() != EGL_SUCCESS);
  98.  
  99.     //Test for invalid attribs
  100.     EGLint contextInvalidAttribs[] =
  101.     {
  102.       EGL_CONTEXT_CLIENT_VERSION, 2,
  103.       0xFFFF, -1,
  104.       EGL_NONE
  105.     };
  106.     context = eglCreateContext(display, config, NULL, contextInvalidAttribs);
  107.     assert(eglGetError() != EGL_SUCCESS);
  108.     assert(context == 0);
  109.     //Test for missing terminator
  110.     EGLint contextAttribsMissingTerm[] =
  111.     {
  112.         EGL_CONTEXT_CLIENT_VERSION, 2,
  113.     };
  114.     context = eglCreateContext(display, config, NULL, contextAttribsMissingTerm);
  115.     assert(eglGetError() != EGL_SUCCESS);
  116.     assert(context == 0);
  117.     //Test for null terminator
  118.     EGLint contextAttribsNullTerm[] =
  119.     {
  120.         EGL_CONTEXT_CLIENT_VERSION, 2,
  121.         0
  122.     };
  123.     context = eglCreateContext(display, config, NULL, contextAttribsNullTerm);
  124.     assert(eglGetError() != EGL_SUCCESS);
  125.     assert(context == 0);
  126.     //Test for invalid and null terminator
  127.     EGLint contextAttribsNullTermInvalid[] =
  128.     {
  129.         0,
  130.     };
  131.     context = eglCreateContext(display, config, NULL, contextAttribsNullTermInvalid);
  132.     assert(eglGetError() != EGL_SUCCESS);
  133.     assert(context == 0);
  134.  
  135.     // The correct attributes, should create a good EGL context
  136.     EGLint contextAttribs[] =
  137.     {
  138.         EGL_CONTEXT_CLIENT_VERSION, 2,
  139.         EGL_NONE
  140.     };
  141.     context = eglCreateContext(display, config, NULL, contextAttribs);
  142.     assert(eglGetError() == EGL_SUCCESS);
  143.     assert(context != 0);
  144.  
  145.     assert(eglGetCurrentContext() == 0); // Creating a context does not yet activate it.
  146.     assert(eglGetError() == EGL_SUCCESS);
  147.  
  148.     ret = eglMakeCurrent(display, surface, surface, context);
  149.     assert(eglGetError() == EGL_SUCCESS);
  150.     assert(ret == EGL_TRUE);
  151.     assert(eglGetCurrentContext() == context);
  152.     assert(eglGetCurrentSurface(EGL_READ) == surface);
  153.     assert(eglGetCurrentSurface(EGL_DRAW) == surface);
  154.  
  155.     glClearColor(1.0,0.0,0.0,0.5);
  156.     glClear(GL_COLOR_BUFFER_BIT);
  157.  
  158.     ret = eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  159.     assert(eglGetError() == EGL_SUCCESS);
  160.     assert(ret == EGL_TRUE);
  161.     assert(eglGetCurrentContext() == EGL_NO_CONTEXT);
  162.     assert(eglGetCurrentSurface(EGL_READ) == EGL_NO_SURFACE);
  163.     assert(eglGetCurrentSurface(EGL_DRAW) == EGL_NO_SURFACE);
  164.  
  165.     assert(eglSwapInterval(display, 0) == EGL_TRUE);
  166.     assert(eglGetError() == EGL_SUCCESS);
  167.     assert(eglSwapInterval(display, 1) == EGL_TRUE);
  168.     assert(eglGetError() == EGL_SUCCESS);
  169.     assert(eglSwapInterval(display, 2) == EGL_TRUE);
  170.     assert(eglGetError() == EGL_SUCCESS);
  171.  
  172.     ret = eglTerminate(display);
  173.     assert(eglGetError() == EGL_SUCCESS);
  174.     assert(ret == EGL_TRUE);
  175.  
  176.     // eglGetProcAddress() without active GL context and/or connected EGL display (after eglTerminate) is required to work, even though the returned function
  177.     // pointers cannot be called unless an active context is available:
  178.     // "return value of NULL indicates that the specified function does not exist for the implementation."
  179.     // "Client API function pointers returned by eglGetProcAddress are independent of the display and the currently bound client API context, and may be used by any client API context which supports the function."
  180.     // At https://www.khronos.org/registry/EGL/specs/eglspec.1.5.pdf, pages 82-32.
  181.     assert(eglGetProcAddress("glClear") != 0);
  182.     assert(eglGetProcAddress("glWakaWaka") == 0);
  183.  
  184.     printf("test finished\n");
  185.     return 0;
  186. }
  187.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement