Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <drm/i915_drm.h>
  6. #include <kos32sys.h>
  7.  
  8. #define EGL_EGLEXT_PROTOTYPES
  9. #define GL_GLEXT_PROTOTYPES
  10.  
  11. #include "EGL/egl.h"
  12. #include "EGL/eglext.h"
  13. #include "GL/gl.h"
  14. #include "gbm.h"
  15. #include "pxgl.h"
  16.  
  17. static struct gbm_device *gbm;
  18.  
  19. static EGLConfig choose_config(EGLDisplay *dpy)
  20. {
  21.    EGLConfig config = NULL;
  22.    EGLint config_attribs[32];
  23.    EGLint num_configs, i;
  24.  
  25.    i = 0;
  26.    config_attribs[i++] = EGL_RED_SIZE;
  27.    config_attribs[i++] = 1;
  28.    config_attribs[i++] = EGL_GREEN_SIZE;
  29.    config_attribs[i++] = 1;
  30.    config_attribs[i++] = EGL_BLUE_SIZE;
  31.    config_attribs[i++] = 1;
  32.    config_attribs[i++] = EGL_DEPTH_SIZE;
  33.    config_attribs[i++] = 1;
  34.  
  35.    config_attribs[i++] = EGL_SURFACE_TYPE;
  36.    config_attribs[i++] = EGL_WINDOW_BIT;
  37.  
  38.    config_attribs[i++] = EGL_RENDERABLE_TYPE;
  39.    config_attribs[i++] = EGL_OPENGL_BIT;
  40.    config_attribs[i] = EGL_NONE;
  41.  
  42.    eglChooseConfig(dpy, config_attribs, &config, 1, &num_configs);
  43.  
  44.    return config;
  45. };
  46.  
  47. int egl_initialize(EGLDisplay *dpy, EGLConfig *config, EGLContext *context)
  48. {
  49.     EGLint major, minor;
  50.     int fd;
  51.  
  52.     fd = get_service("DISPLAY");
  53.     if(fd == 0)
  54.         return -1;
  55.  
  56.     gbm = gbm_create_device(fd);
  57.     if( gbm == NULL)
  58.     {
  59.         DBG("failed to initialize GBM device\n");
  60.         goto err_0;
  61.     };
  62.  
  63.     *dpy = eglGetDisplay(gbm);
  64.  
  65.     if (!eglInitialize(*dpy, &major, &minor))
  66.     {
  67.         DBG("failed to initialize EGL display\n");
  68.         goto err_1;
  69.     };
  70.  
  71.     DBG("EGL_VERSION = %s\n", eglQueryString(*dpy, EGL_VERSION));
  72.     DBG("EGL_VENDOR = %s\n", eglQueryString(*dpy, EGL_VENDOR));
  73.     DBG("EGL_EXTENSIONS = %s\n", eglQueryString(*dpy, EGL_EXTENSIONS));
  74.     DBG("EGL_CLIENT_APIS = %s\n", eglQueryString(*dpy, EGL_CLIENT_APIS));
  75.  
  76.     *config = choose_config(*dpy);
  77.     if( *config == NULL)
  78.     {
  79.         DBG("failed to choose a config\n");
  80.         goto err_2;
  81.     };
  82.  
  83.     eglBindAPI(EGL_OPENGL_API);
  84.     *context = eglCreateContext(*dpy, *config, EGL_NO_CONTEXT, NULL);
  85.     if (context == NULL)
  86.     {
  87.         DBG("failed to create context\n");
  88.         goto err_2;
  89.     };
  90.  
  91.     if (!eglMakeCurrent(*dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, *context))
  92.     {
  93.         DBG("failed to make context current");
  94.         goto err_3;
  95.     };
  96.  
  97.     return 0;
  98.  
  99. err_3:
  100.     eglDestroyContext(*dpy, *context);
  101. err_2:
  102.     eglTerminate(*dpy);
  103. err_1:
  104.     gbm_device_destroy(gbm);
  105. err_0:
  106.     return -1;
  107. };
  108.  
  109. void egl_destroy(EGLDisplay dpy, EGLContext context)
  110. {
  111.     eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  112.     eglDestroyContext(dpy, context);
  113.     eglTerminate(dpy);
  114.     gbm_device_destroy(gbm);
  115. };
  116.  
  117.