# HG changeset patch # User Eleni Maria Stea # Date 1530731783 -10800 # Node ID 665119a0580e16ec90e4785f264af8b5a27f5dad example program that uses gbm buffers diff -r 000000000000 -r 665119a0580e Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Makefile Wed Jul 04 22:16:23 2018 +0300 @@ -0,0 +1,21 @@ +src = $(wildcard src/*.cc) +obj = $(src:.cc=.o) +dep = $(obj:.o=.d) +bin = test + +dbg = -g +opt = -O0 +inc = -Isrc -I/usr/lib/i386-linux-gnu + +CXX = g++ +CXXFLAGS = -pedantic -Wall $(dbg) $(opt) $(inc) `sdl2-config --cflags` `pkg-config --cflags libdrm` +LDFLAGS = `sdl2-config --libs` -lGLESv2 -lgbm -lEGL -ldrm -ludev + +$(bin): $(obj) + $(CXX) -o $@ $(obj) $(LDFLAGS) + +-include $(dep) + +.PHONY: clean +clean: + rm -f $(obj) $(bin) diff -r 000000000000 -r 665119a0580e src/drmtest.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/drmtest.cc Wed Jul 04 22:16:23 2018 +0300 @@ -0,0 +1,131 @@ +/* + * Copyright © 2007 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + * Authors: + * Eric Anholt + * + */ + +#include + +#include +#include +#include +#include +#include "drmtest.h" + +#define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE +#include + +static int is_master(int fd) +{ + drm_client_t client; + int ret; + + /* Check that we're the only opener and authed. */ + client.idx = 0; + ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client); + assert (ret == 0); + if (!client.auth) + return 0; + client.idx = 1; + ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client); + if (ret != -1 || errno != EINVAL) + return 0; + + return 1; +} + +/** Open the first DRM device matching the criteria */ +int drm_open_matching(const char *pci_glob, int flags) +{ + struct udev *udev; + struct udev_enumerate *e; + struct udev_device *device, *parent; + struct udev_list_entry *entry; + const char *pci_id, *path; + int fd; + + udev = udev_new(); + if (udev == NULL) { + fprintf(stderr, "failed to initialize udev context\n"); + abort(); + } + + fd = -1; + e = udev_enumerate_new(udev); + udev_enumerate_add_match_subsystem(e, "drm"); + udev_enumerate_scan_devices(e); + udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { + path = udev_list_entry_get_name(entry); + device = udev_device_new_from_syspath(udev, path); + parent = udev_device_get_parent(device); + /* Filter out KMS output devices. */ + if (strcmp(udev_device_get_subsystem(parent), "pci") != 0) + continue; + pci_id = udev_device_get_property_value(parent, "PCI_ID"); + if (fnmatch(pci_glob, pci_id, 0) != 0) + continue; + fd = open(udev_device_get_devnode(device), O_RDWR); + if (fd < 0) + continue; + if ((flags & DRM_TEST_MASTER) && !is_master(fd)) { + close(fd); + fd = -1; + continue; + } + + break; + } + udev_enumerate_unref(e); + udev_unref(udev); + + return fd; +} + +int drm_open_any(void) +{ + int fd = drm_open_matching("*:*", 0); + + if (fd < 0) { + fprintf(stderr, "failed to open any drm device\n"); + abort(); + } + + return fd; +} + +/** + * Open the first DRM device we can find where we end up being the master. + */ +int drm_open_any_master(void) +{ + int fd = drm_open_matching("*:*", DRM_TEST_MASTER); + + if (fd < 0) { + fprintf(stderr, "failed to open any drm device\n"); + abort(); + } + + return fd; + +} diff -r 000000000000 -r 665119a0580e src/drmtest.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/drmtest.h Wed Jul 04 22:16:23 2018 +0300 @@ -0,0 +1,40 @@ +/* + * Copyright © 2007 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + * Authors: + * Eric Anholt + * + */ + +#include +#include +#include +#include +#include + +#include + +#define DRM_TEST_MASTER 0x01 + +int drm_open_any(void); +int drm_open_any_master(void); +int drm_open_matching(const char *pci_glob, int flags); diff -r 000000000000 -r 665119a0580e src/test.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test.cc Wed Jul 04 22:16:23 2018 +0300 @@ -0,0 +1,115 @@ +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "drmtest.h" + +int fd = -1; +static struct gbm_device *gbm_dev; +struct gbm_bo *bo; +struct gbm_surface *gbm_surf; + +int main(int argc, char **argv) +{ +// if((fd = open("/dev/dri/card0", O_RDWR) == -1)) { +// fprintf(stderr, "can't open device: %s\n", strerror(errno)); +// exit(1); +// } + if((fd = drm_open_any()) == - 1) { + fprintf(stderr, "can't open device: %s\n", strerror(errno)); + exit(1); + } + + printf("fd: %d\n", fd); + + if(!(gbm_dev = gbm_create_device(fd))) { + fprintf(stderr, "can't create gbm device: %s\n", strerror(errno)); + exit(1); + } + + if(!(gbm_surf = gbm_surface_create(gbm_dev, 640, 480, GBM_BO_FORMAT_XRGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING))) { + fprintf(stderr, "can't create gbm surface: %s\n", strerror(errno)); + exit(1); + } + + EGLDisplay dpy; + dpy = eglGetDisplay((_XDisplay*)gbm_dev); + EGLint major, minor; + + //const char *ver; + const char *extensions; + eglInitialize(dpy, &major, &minor); + //ver = eglQueryString(dpy, EGL_VERSION); + extensions = eglQueryString(dpy, EGL_EXTENSIONS); + + if (!strstr(extensions, "EGL_KHR_surfaceless_context")) { + fprintf(stderr, "no surfaceless support, cannot initialize\n"); + exit(1); + } + + SDL_Window *win; + SDL_GLContext ctx; + + if(SDL_Init(SDL_INIT_VIDEO) == -1) { + fprintf(stderr, "failed to initialize sdl\n"); + return 1; + } + + SDL_GL_SetAttribute(SDL_GL_CONTEXT_EGL, 1); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); + SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + //SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); + + if(!(bo = gbm_bo_create(gbm_dev, 640, 480, GBM_BO_FORMAT_XRGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING))) { + fprintf(stderr, "Unable to create gbm buffer object!\n"); + } + + win = SDL_CreateWindow("foo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, + SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN); + + if(!win) { + fprintf(stderr, "failed to create window: %s\n", SDL_GetError()); + return 1; + } + + if(!(ctx = SDL_GL_CreateContext(win))) { + fprintf(stderr, "failed to create OpenGL context: %s\n", SDL_GetError()); + return 1; + } + + glClearColor(1.0, 0.0, 0.0, 1.0); + glClear(GL_COLOR_BUFFER_BIT); + + SDL_GL_SwapWindow(win); + SDL_Delay(5000); + + gbm_device_destroy(gbm_dev); + SDL_GL_DeleteContext(win); + SDL_DestroyWindow(win); + SDL_Quit(); + return 0; +}