gbm-sdl2-example

view src/test.cc @ 0:665119a0580e

example program that uses gbm buffers
author Eleni Maria Stea <eleni@mutantstargoat.com>
date Wed, 04 Jul 2018 22:16:23 +0300
parents
children
line source
1 #include <errno.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
6 #include <SDL2/SDL.h>
7 #include <GLES2/gl2.h>
8 #include <EGL/egl.h>
9 #include <EGL/eglext.h>
10 #include <EGL/eglplatform.h>
11 #include <KHR/khrplatform.h>
12 #include <gbm.h>
14 #include <fcntl.h>
15 #include <fnmatch.h>
16 #include <sys/stat.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <assert.h>
21 #include <errno.h>
22 #include <libudev.h>
24 #include "drmtest.h"
26 int fd = -1;
27 static struct gbm_device *gbm_dev;
28 struct gbm_bo *bo;
29 struct gbm_surface *gbm_surf;
31 int main(int argc, char **argv)
32 {
33 // if((fd = open("/dev/dri/card0", O_RDWR) == -1)) {
34 // fprintf(stderr, "can't open device: %s\n", strerror(errno));
35 // exit(1);
36 // }
37 if((fd = drm_open_any()) == - 1) {
38 fprintf(stderr, "can't open device: %s\n", strerror(errno));
39 exit(1);
40 }
42 printf("fd: %d\n", fd);
44 if(!(gbm_dev = gbm_create_device(fd))) {
45 fprintf(stderr, "can't create gbm device: %s\n", strerror(errno));
46 exit(1);
47 }
49 if(!(gbm_surf = gbm_surface_create(gbm_dev, 640, 480, GBM_BO_FORMAT_XRGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING))) {
50 fprintf(stderr, "can't create gbm surface: %s\n", strerror(errno));
51 exit(1);
52 }
54 EGLDisplay dpy;
55 dpy = eglGetDisplay((_XDisplay*)gbm_dev);
56 EGLint major, minor;
58 //const char *ver;
59 const char *extensions;
60 eglInitialize(dpy, &major, &minor);
61 //ver = eglQueryString(dpy, EGL_VERSION);
62 extensions = eglQueryString(dpy, EGL_EXTENSIONS);
64 if (!strstr(extensions, "EGL_KHR_surfaceless_context")) {
65 fprintf(stderr, "no surfaceless support, cannot initialize\n");
66 exit(1);
67 }
69 SDL_Window *win;
70 SDL_GLContext ctx;
72 if(SDL_Init(SDL_INIT_VIDEO) == -1) {
73 fprintf(stderr, "failed to initialize sdl\n");
74 return 1;
75 }
77 SDL_GL_SetAttribute(SDL_GL_CONTEXT_EGL, 1);
78 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
79 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
80 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
81 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
82 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
83 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
84 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
85 //SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
87 if(!(bo = gbm_bo_create(gbm_dev, 640, 480, GBM_BO_FORMAT_XRGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING))) {
88 fprintf(stderr, "Unable to create gbm buffer object!\n");
89 }
91 win = SDL_CreateWindow("foo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480,
92 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
94 if(!win) {
95 fprintf(stderr, "failed to create window: %s\n", SDL_GetError());
96 return 1;
97 }
99 if(!(ctx = SDL_GL_CreateContext(win))) {
100 fprintf(stderr, "failed to create OpenGL context: %s\n", SDL_GetError());
101 return 1;
102 }
104 glClearColor(1.0, 0.0, 0.0, 1.0);
105 glClear(GL_COLOR_BUFFER_BIT);
107 SDL_GL_SwapWindow(win);
108 SDL_Delay(5000);
110 gbm_device_destroy(gbm_dev);
111 SDL_GL_DeleteContext(win);
112 SDL_DestroyWindow(win);
113 SDL_Quit();
114 return 0;
115 }