Path: blob/21.2-virgl/src/gallium/targets/graw-xlib/graw_xlib.c
4565 views
#include "pipe/p_compiler.h"1#include "pipe/p_context.h"2#include "pipe/p_screen.h"3#include "util/u_debug.h"4#include "util/u_memory.h"5#include "target-helpers/inline_sw_helper.h"6#include "target-helpers/inline_debug_helper.h"7#include "frontend/xlibsw_api.h"8#include "frontend/graw.h"9#include "sw/xlib/xlib_sw_winsys.h"1011#include <X11/Xlib.h>12#include <X11/Xlibint.h>13#include <X11/Xutil.h>14#include <stdio.h>1516static struct {17Display *display;18void (*draw)(void);19} graw;202122static struct pipe_screen *23graw_create_screen( void )24{25struct pipe_screen *screen = NULL;26struct sw_winsys *winsys = NULL;2728/* Create the underlying winsys, which performs presents to Xlib29* drawables:30*/31winsys = xlib_create_sw_winsys( graw.display );32if (winsys == NULL)33return NULL;3435screen = sw_screen_create( winsys );3637/* Inject any wrapping layers we want to here:38*/39return debug_screen_wrap( screen );40}414243struct pipe_screen *44graw_create_window_and_screen( int x,45int y,46unsigned width,47unsigned height,48enum pipe_format format,49void **handle)50{51struct pipe_screen *screen = NULL;52struct xlib_drawable *xlib_handle = NULL;53XSetWindowAttributes attr;54Window root;55Window win = 0;56XVisualInfo templat, *visinfo = NULL;57unsigned mask;58int n;59int scrnum;6061graw.display = XOpenDisplay(NULL);62if (graw.display == NULL)63return NULL;6465scrnum = DefaultScreen( graw.display );66root = RootWindow( graw.display, scrnum );676869if (graw.display == NULL)70goto fail;7172xlib_handle = CALLOC_STRUCT(xlib_drawable);73if (xlib_handle == NULL)74goto fail;757677mask = VisualScreenMask | VisualDepthMask | VisualClassMask;78templat.screen = DefaultScreen(graw.display);79templat.depth = 32;80templat.class = TrueColor;8182visinfo = XGetVisualInfo(graw.display, mask, &templat, &n);83if (!visinfo) {84printf("Error: couldn't get an RGB, Double-buffered visual\n");85exit(1);86}8788/* See if the requirested pixel format matches the visual */89if (visinfo->red_mask == 0xff0000 &&90visinfo->green_mask == 0xff00 &&91visinfo->blue_mask == 0xff) {92if (format != PIPE_FORMAT_BGRA8888_UNORM)93goto fail;94}95else if (visinfo->red_mask == 0xff &&96visinfo->green_mask == 0xff00 &&97visinfo->blue_mask == 0xff0000) {98if (format != PIPE_FORMAT_RGBA8888_UNORM)99goto fail;100}101else {102goto fail;103}104105/* window attributes */106attr.background_pixel = 0;107attr.border_pixel = 0;108attr.colormap = XCreateColormap( graw.display, root, visinfo->visual, AllocNone);109attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;110/* XXX this is a bad way to get a borderless window! */111mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;112113win = XCreateWindow( graw.display, root, x, y, width, height,1140, visinfo->depth, InputOutput,115visinfo->visual, mask, &attr );116117118/* set hints and properties */119{120char *name = NULL;121XSizeHints sizehints;122sizehints.x = x;123sizehints.y = y;124sizehints.width = width;125sizehints.height = height;126sizehints.flags = USSize | USPosition;127XSetNormalHints(graw.display, win, &sizehints);128XSetStandardProperties(graw.display, win, name, name,129None, (char **)NULL, 0, &sizehints);130}131132XMapWindow(graw.display, win);133while (1) {134XEvent e;135XNextEvent( graw.display, &e );136if (e.type == MapNotify && e.xmap.window == win) {137break;138}139}140141xlib_handle->visual = visinfo->visual;142xlib_handle->drawable = (Drawable)win;143xlib_handle->depth = visinfo->depth;144*handle = (void *)xlib_handle;145146screen = graw_create_screen();147if (screen == NULL)148goto fail;149150free(visinfo);151return screen;152153fail:154if (screen)155screen->destroy(screen);156157FREE(xlib_handle);158159free(visinfo);160161if (win)162XDestroyWindow(graw.display, win);163164return NULL;165}166167168void169graw_set_display_func( void (*draw)( void ) )170{171graw.draw = draw;172}173174void175graw_main_loop( void )176{177int i;178for (i = 0; i < 10; i++) {179graw.draw();180sleep(1);181}182}183184185186