gbm-sdl2-example

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/test.cc	Wed Jul 04 22:16:23 2018 +0300
     1.3 @@ -0,0 +1,115 @@
     1.4 +#include <errno.h>
     1.5 +#include <stdio.h>
     1.6 +#include <stdlib.h>
     1.7 +#include <string.h>
     1.8 +
     1.9 +#include <SDL2/SDL.h>
    1.10 +#include <GLES2/gl2.h>
    1.11 +#include <EGL/egl.h>
    1.12 +#include <EGL/eglext.h>
    1.13 +#include <EGL/eglplatform.h>
    1.14 +#include <KHR/khrplatform.h>
    1.15 +#include <gbm.h>
    1.16 +
    1.17 +#include <fcntl.h>
    1.18 +#include <fnmatch.h>
    1.19 +#include <sys/stat.h>
    1.20 +#include <stdio.h>
    1.21 +#include <stdlib.h>
    1.22 +#include <unistd.h>
    1.23 +#include <assert.h>
    1.24 +#include <errno.h>
    1.25 +#include <libudev.h>
    1.26 +
    1.27 +#include "drmtest.h"
    1.28 +
    1.29 +int fd = -1;
    1.30 +static struct gbm_device *gbm_dev;
    1.31 +struct gbm_bo *bo;
    1.32 +struct gbm_surface *gbm_surf;
    1.33 +
    1.34 +int main(int argc, char **argv)
    1.35 +{
    1.36 +//	if((fd = open("/dev/dri/card0", O_RDWR) == -1)) {
    1.37 +//		fprintf(stderr, "can't open device: %s\n", strerror(errno));
    1.38 +//		exit(1);
    1.39 +//	}
    1.40 +	if((fd = drm_open_any()) == - 1) {
    1.41 +		fprintf(stderr, "can't open device: %s\n", strerror(errno));
    1.42 +		exit(1);
    1.43 +	}
    1.44 +
    1.45 +	printf("fd: %d\n", fd);
    1.46 +
    1.47 +	if(!(gbm_dev = gbm_create_device(fd))) {
    1.48 +		fprintf(stderr, "can't create gbm device: %s\n", strerror(errno));
    1.49 +		exit(1);
    1.50 +	}
    1.51 +
    1.52 +	if(!(gbm_surf = gbm_surface_create(gbm_dev, 640, 480,  GBM_BO_FORMAT_XRGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING))) {
    1.53 +		fprintf(stderr, "can't create gbm surface: %s\n", strerror(errno));
    1.54 +		exit(1);
    1.55 +	}
    1.56 +
    1.57 +	EGLDisplay dpy;
    1.58 +	dpy = eglGetDisplay((_XDisplay*)gbm_dev);
    1.59 +	EGLint major, minor;
    1.60 +
    1.61 +	//const char *ver;
    1.62 +	const char *extensions;
    1.63 +	eglInitialize(dpy, &major, &minor);
    1.64 +	//ver = eglQueryString(dpy, EGL_VERSION);
    1.65 +	extensions = eglQueryString(dpy, EGL_EXTENSIONS);
    1.66 +
    1.67 +	if (!strstr(extensions, "EGL_KHR_surfaceless_context")) {
    1.68 +		fprintf(stderr, "no surfaceless support, cannot initialize\n");
    1.69 +		exit(1);
    1.70 +	}
    1.71 +
    1.72 +	SDL_Window *win;
    1.73 +	SDL_GLContext ctx;
    1.74 +
    1.75 +	if(SDL_Init(SDL_INIT_VIDEO) == -1) {
    1.76 +		fprintf(stderr, "failed to initialize sdl\n");
    1.77 +		return 1;
    1.78 +	}
    1.79 +
    1.80 +	SDL_GL_SetAttribute(SDL_GL_CONTEXT_EGL, 1);
    1.81 +	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
    1.82 +	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
    1.83 +	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
    1.84 +	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    1.85 +	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
    1.86 +	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
    1.87 +	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    1.88 +	//SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
    1.89 +
    1.90 +	if(!(bo = gbm_bo_create(gbm_dev, 640, 480, GBM_BO_FORMAT_XRGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING))) {
    1.91 +		fprintf(stderr, "Unable to create gbm buffer object!\n");
    1.92 +	}
    1.93 +
    1.94 +	win = SDL_CreateWindow("foo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480,
    1.95 +			SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
    1.96 +
    1.97 +	if(!win) {
    1.98 +		fprintf(stderr, "failed to create window: %s\n", SDL_GetError());
    1.99 +		return 1;
   1.100 +	}
   1.101 +
   1.102 +	if(!(ctx = SDL_GL_CreateContext(win))) {
   1.103 +		fprintf(stderr, "failed to create OpenGL context: %s\n", SDL_GetError());
   1.104 +		return 1;
   1.105 +	}
   1.106 +
   1.107 +	glClearColor(1.0, 0.0, 0.0, 1.0);
   1.108 +	glClear(GL_COLOR_BUFFER_BIT);
   1.109 +
   1.110 +	SDL_GL_SwapWindow(win);
   1.111 +	SDL_Delay(5000);
   1.112 +
   1.113 +	gbm_device_destroy(gbm_dev);
   1.114 +	SDL_GL_DeleteContext(win);
   1.115 +	SDL_DestroyWindow(win);
   1.116 +	SDL_Quit();
   1.117 +	return 0;
   1.118 +}