|
| 1 | +#include <errno.h> |
| 2 | +#include <fcntl.h> |
| 3 | +#include <stdbool.h> |
| 4 | +#include <stdio.h> |
| 5 | +#include <stdlib.h> |
| 6 | +#include <string.h> |
| 7 | + |
| 8 | +#include "common.h" |
| 9 | + |
| 10 | +static struct gbm gbm; |
| 11 | +static struct egl egl; |
| 12 | + |
| 13 | +const struct gbm * init_gbm(int drm_fd, int w, int h, uint64_t modifier) |
| 14 | +{ |
| 15 | + gbm.dev = gbm_create_device(drm_fd); |
| 16 | + |
| 17 | + if (modifier != DRM_FORMAT_MOD_INVALID) { |
| 18 | + fprintf(stderr, "Modifiers requested but support isn't available\n"); |
| 19 | + return NULL; |
| 20 | + } |
| 21 | + gbm.surface = gbm_surface_create(gbm.dev, w, h, GBM_FORMAT_XRGB8888, |
| 22 | + GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING); |
| 23 | + |
| 24 | + if (!gbm.surface) { |
| 25 | + printf("failed to create gbm surface\n"); |
| 26 | + return NULL; |
| 27 | + } |
| 28 | + |
| 29 | + gbm.width = w; |
| 30 | + gbm.height = h; |
| 31 | + |
| 32 | + return &gbm; |
| 33 | +} |
| 34 | + |
| 35 | +static bool has_ext(const char *extension_list, const char *ext) |
| 36 | +{ |
| 37 | + const char *ptr = extension_list; |
| 38 | + int len = strlen(ext); |
| 39 | + |
| 40 | + if (ptr == NULL || *ptr == '\0') |
| 41 | + return false; |
| 42 | + |
| 43 | + while (true) { |
| 44 | + ptr = strstr(ptr, ext); |
| 45 | + if (!ptr) |
| 46 | + return false; |
| 47 | + |
| 48 | + if (ptr[len] == ' ' || ptr[len] == '\0') |
| 49 | + return true; |
| 50 | + |
| 51 | + ptr += len; |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +struct egl* egl_init(const struct gbm *gbm) |
| 56 | +{ |
| 57 | + EGLint major, minor, n; |
| 58 | + |
| 59 | + static const EGLint context_attribs[] = { |
| 60 | + EGL_CONTEXT_CLIENT_VERSION, 2, |
| 61 | + EGL_NONE |
| 62 | + }; |
| 63 | + |
| 64 | + static const EGLint config_attribs[] = { |
| 65 | + EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
| 66 | + EGL_RED_SIZE, 1, |
| 67 | + EGL_GREEN_SIZE, 1, |
| 68 | + EGL_BLUE_SIZE, 1, |
| 69 | + EGL_ALPHA_SIZE, 0, |
| 70 | + EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 71 | + EGL_NONE |
| 72 | + }; |
| 73 | + const char *egl_exts_client, *egl_exts_dpy, *gl_exts; |
| 74 | + |
| 75 | +#define get_proc_client(ext, name) do { \ |
| 76 | + if (has_ext(egl_exts_client, #ext)) \ |
| 77 | + egl.name = (void *)eglGetProcAddress(#name); \ |
| 78 | + } while (0) |
| 79 | +#define get_proc_dpy(ext, name) do { \ |
| 80 | + if (has_ext(egl_exts_dpy, #ext)) \ |
| 81 | + egl.name = (void *)eglGetProcAddress(#name); \ |
| 82 | + } while (0) |
| 83 | + |
| 84 | +#define get_proc_gl(ext, name) do { \ |
| 85 | + if (has_ext(gl_exts, #ext)) \ |
| 86 | + egl.name = (void *)eglGetProcAddress(#name); \ |
| 87 | + } while (0) |
| 88 | + |
| 89 | + egl_exts_client = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS); |
| 90 | + get_proc_client(EGL_EXT_platform_base, eglGetPlatformDisplayEXT); |
| 91 | + |
| 92 | + if (egl.eglGetPlatformDisplayEXT) { |
| 93 | + egl.display = egl.eglGetPlatformDisplayEXT(EGL_PLATFORM_GBM_KHR, |
| 94 | + gbm->dev, NULL); |
| 95 | + } else { |
| 96 | + egl.display = eglGetDisplay((void *)gbm->dev); |
| 97 | + } |
| 98 | + |
| 99 | + if (!eglInitialize(egl.display, &major, &minor)) { |
| 100 | + printf("failed to initialize\n"); |
| 101 | + return NULL; |
| 102 | + } |
| 103 | + |
| 104 | + egl_exts_dpy = eglQueryString(egl.display, EGL_EXTENSIONS); |
| 105 | + get_proc_dpy(EGL_KHR_image_base, eglCreateImageKHR); |
| 106 | + get_proc_dpy(EGL_KHR_image_base, eglDestroyImageKHR); |
| 107 | + get_proc_dpy(EGL_KHR_fence_sync, eglCreateSyncKHR); |
| 108 | + get_proc_dpy(EGL_KHR_fence_sync, eglDestroySyncKHR); |
| 109 | + get_proc_dpy(EGL_KHR_fence_sync, eglWaitSyncKHR); |
| 110 | + get_proc_dpy(EGL_KHR_fence_sync, eglClientWaitSyncKHR); |
| 111 | + printf("Using display %p with EGL version %d.%d\n", |
| 112 | + egl.display, major, minor); |
| 113 | + |
| 114 | + printf("===================================\n"); |
| 115 | + printf("EGL information:\n"); |
| 116 | + printf(" version: \"%s\"\n", eglQueryString(egl.display, EGL_VERSION)); |
| 117 | + printf(" vendor: \"%s\"\n", eglQueryString(egl.display, EGL_VENDOR)); |
| 118 | + printf(" client extensions: \"%s\"\n", egl_exts_client); |
| 119 | + printf(" display extensions: \"%s\"\n", egl_exts_dpy); |
| 120 | + printf("===================================\n"); |
| 121 | + |
| 122 | + if (!eglBindAPI(EGL_OPENGL_ES_API)) { |
| 123 | + printf("failed to bind api EGL_OPENGL_ES_API\n"); |
| 124 | + return NULL; |
| 125 | + } |
| 126 | + |
| 127 | + if (!eglChooseConfig(egl.display, config_attribs, &egl.config, 1, &n) || n != 1) { |
| 128 | + printf("failed to choose config: %d\n", n); |
| 129 | + return NULL; |
| 130 | + } |
| 131 | + |
| 132 | + egl.context = eglCreateContext(egl.display, egl.config, |
| 133 | + EGL_NO_CONTEXT, context_attribs); |
| 134 | + if (egl.context == NULL) { |
| 135 | + printf("failed to create context\n"); |
| 136 | + return NULL; |
| 137 | + } |
| 138 | + |
| 139 | + egl.surface = eglCreateWindowSurface(egl.display, egl.config, |
| 140 | + (EGLNativeWindowType)gbm->surface, NULL); |
| 141 | + if (egl.surface == EGL_NO_SURFACE) { |
| 142 | + printf("failed to create egl surface\n"); |
| 143 | + return NULL; |
| 144 | + } |
| 145 | + |
| 146 | + /* connect the context to the surface */ |
| 147 | + eglMakeCurrent(egl.display, egl.surface, egl.surface, egl.context); |
| 148 | + |
| 149 | + gl_exts = (char *) glGetString(GL_EXTENSIONS); |
| 150 | + printf("OpenGL ES 2.x information:\n"); |
| 151 | + printf(" version: \"%s\"\n", glGetString(GL_VERSION)); |
| 152 | + printf(" shading language version: \"%s\"\n", glGetString(GL_SHADING_LANGUAGE_VERSION)); |
| 153 | + printf(" vendor: \"%s\"\n", glGetString(GL_VENDOR)); |
| 154 | + printf(" renderer: \"%s\"\n", glGetString(GL_RENDERER)); |
| 155 | + printf(" extensions: \"%s\"\n", gl_exts); |
| 156 | + printf("===================================\n"); |
| 157 | + |
| 158 | + get_proc_gl(GL_OES_EGL_image, glEGLImageTargetTexture2DOES); |
| 159 | + |
| 160 | + return &egl; |
| 161 | +} |
0 commit comments