Beispiel #1
0
int
main (int argc, char **argv) {
  
    XEvent event;
    XSizeHints xsh;
    XSetWindowAttributes xswa;
    XVisualInfo *vinfo;

    glitz_drawable_format_t templ;
    glitz_drawable_format_t *dformat;
    unsigned long mask = 0;
    
    unsigned int width, height, window_width, window_height;
    int i;

    program_name = argv[0];
    
    for (i = 1; i < argc; i++) {
	if (!strcasecmp ("-image", argv[i]))
	    output_type = IMAGE_TYPE;

#ifdef CAIRO_HAS_XLIB_SURFACE
	else if (!strcasecmp ("-xrender", argv[i])) {
	    output_type = XRENDER_TYPE;
	} 
#endif

	else if (!strcasecmp ("-glx", argv[i])) {
	    output_type = GLX_TYPE;
	} else if (!strcasecmp ("-noaa", argv[i])) {
	    aa = 0;
	} else if (!strcasecmp ("-swaa", argv[i])) {
	    aa = 1;
	} else if (!strcasecmp ("-hwaa", argv[i])) {
	    aa = 3;
        } else {
            test_type = get_test_type (argv[i]);
        }
    }
  
    if (!test_type) {
	usage();
	exit(1);
    }

    if (output_type != GLX_TYPE && test_type >= OPENGL_TYPE) {
	printf ("Sorry, this test only works with OpenGL!\n");
	usage();
	exit(1);
    }

    window_width = width = WINDOW_WIDTH;
    window_height = height = WINDOW_HEIGHT;
    
    if (aa == 3)
	templ.samples = 4;
    else
	templ.samples = 1;
    
    templ.depth_size = 16;
    if (test_type == CUBE_TYPE)
	mask |= GLITZ_FORMAT_DEPTH_SIZE_MASK;

    mask |= GLITZ_FORMAT_SAMPLES_MASK;
    
    if ((dpy = XOpenDisplay (NULL)) == NULL) {
	fprintf(stderr, "%s: can't open display: %s\n", argv[0],
		XDisplayName (NULL));
	exit(1);
    }

    if (output_type != GLX_TYPE) {
	xsh.flags = PSize;
	xsh.width = width;
	xsh.height = height;
	xsh.x = 0;
	xsh.y = 0;
	
	win = XCreateWindow (dpy, RootWindow (dpy, DefaultScreen (dpy)), 
			     xsh.x, xsh.y, xsh.width, xsh.height,
			     0, CopyFromParent, CopyFromParent,
			     CopyFromParent, 0, &xswa);
  
	XSetStandardProperties (dpy, win, PACKAGE, PACKAGE, None,
				argv, argc, &xsh);
	XSetWMHints (dpy, win, &xwmh);

	XSelectInput (dpy, win, StructureNotifyMask);
	
    } else {

	xsh.flags = PSize;
	xsh.width = width;
	xsh.height = height;
	xsh.x = 0;
	xsh.y = 0;

	mask = 0;
	
	templ.doublebuffer = 1;
	mask |= GLITZ_FORMAT_DOUBLEBUFFER_MASK;
	
	dformat = glitz_glx_find_window_format (dpy, DefaultScreen (dpy),
						mask, &templ, 0);
	
	vinfo = glitz_glx_get_visual_info_from_format (dpy,
						       DefaultScreen (dpy),
						       dformat);
	xswa.colormap =
	    XCreateColormap (dpy,
			     RootWindow (dpy, DefaultScreen (dpy)), 
			     vinfo->visual, AllocNone);
	win = XCreateWindow (dpy, RootWindow (dpy, DefaultScreen (dpy)), 
			     xsh.x, xsh.y, xsh.width, xsh.height,
			     0, vinfo->depth, CopyFromParent,
			     vinfo->visual, CWColormap, &xswa);
  
	XSetStandardProperties (dpy, win, PACKAGE, PACKAGE, None,
				argv, argc, &xsh);
	XSetWMHints (dpy, win, &xwmh);

	XSelectInput (dpy, win, StructureNotifyMask);
    }
	
    switch (output_type) {
      
    case XRENDER_TYPE:
	resize_pixmap (width, height);
	break;
      
    case IMAGE_TYPE:
	resize_image (width, height);
	break;
    case GLX_TYPE:
	drawable =
	    glitz_glx_create_drawable_for_window (dpy, 0, dformat, win,
						  width, height);
	if (!drawable) {
	    printf ("failed to create glitz drawable\n");
	    exit (1);
	}
	break;
    }
  
    if (aa == 3 && dformat->samples < 2) {
	fprintf (stderr, "hardware multi-sampling not available\n");
	exit (1);
    }

    if (drawable) {
	surface = resize_glitz_drawable (drawable, dformat, width, height);
    }

    cr = cairo_create (surface);
    cairo_set_tolerance (cr, 0.5);

    setup (test_type);

    XMapWindow (dpy, win);

    for (;;) {
	if (XPending (dpy)) {
	    XNextEvent (dpy, &event);
	    if (event.type == ConfigureNotify) {
		width = event.xconfigure.width;
		height = event.xconfigure.height;
                
		switch (output_type) {
		    
#ifdef CAIRO_HAS_XLIB_SURFACE
		case XRENDER_TYPE:
		    resize_pixmap (width, height);
		    cairo_destroy (cr);
		    cr = cairo_create (surface);
		    cairo_set_tolerance (cr, 0.5);
		    break;
#endif
		    
		case IMAGE_TYPE:
		    resize_image (width, height);
		    cairo_destroy (cr);
		    cr = cairo_create (surface);
		    cairo_set_tolerance (cr, 0.5);
		    break;
		case GLX_TYPE:
		    cairo_surface_destroy (surface);
		    surface = resize_glitz_drawable (drawable, dformat, 
						     width, height);
		    cairo_destroy (cr);
		    cr = cairo_create (surface);
		    cairo_set_tolerance (cr, 0.5);
		    break;
		}
	    }
	} else {
	    render (test_type, output_type == GLX_TYPE);
       	    switch (output_type) {
		
#ifdef CAIRO_HAS_XLIB_SURFACE
	    case XRENDER_TYPE:
		XSetWindowBackgroundPixmap (dpy, win, pixmap);
		XClearWindow (dpy, win);
		break;
#endif
		
	    case IMAGE_TYPE: {
		GC gc;
		XImage *xim;

		pixmap = XCreatePixmap (dpy, DefaultRootWindow (dpy),
					width, height,
					DefaultDepth (dpy,
						      DefaultScreen (dpy)));
		xim = XCreateImage(dpy, DefaultVisual (dpy,
						       DefaultScreen (dpy)),
				   DefaultDepth(dpy, DefaultScreen (dpy)),
				   ZPixmap, 0, (char *) image,
				   width, height, 32, 0);
		gc = XCreateGC (dpy, pixmap, 0, NULL);
		XPutImage (dpy, pixmap, gc, xim, 0, 0, 0, 0, width, height);
                    
		XFreeGC (dpy, gc);
		xim->data = NULL;
		XDestroyImage (xim);

		XSetWindowBackgroundPixmap (dpy, win, pixmap);
		XClearWindow (dpy, win);
		XFreePixmap (dpy, pixmap);
	    } break;
	    }

	    XSync (dpy, 0);
	}
    }

    exit (1);
}
static glitz_surface_t *
_cairo_boilerplate_glitz_glx_create_surface_internal (glitz_format_name_t		 formatname,
						      int				 width,
						      int				 height,
						      glitz_glx_target_closure_t	*closure)
{
    Display                 * dpy = closure->dpy;
    int                       scr = closure->scr;
    glitz_drawable_format_t   templ;
    glitz_drawable_format_t * dformat = NULL;
    unsigned long             mask;
    glitz_drawable_t        * drawable = NULL;
    glitz_format_t          * format;
    glitz_surface_t         * sr;

    XSizeHints                xsh;
    XSetWindowAttributes      xswa;
    XVisualInfo             * vinfo;

    memset(&templ, 0, sizeof(templ));
    templ.color.red_size = 8;
    templ.color.green_size = 8;
    templ.color.blue_size = 8;
    templ.color.alpha_size = 8;
    templ.color.fourcc = GLITZ_FOURCC_RGB;
    templ.samples = 1;

    glitz_glx_init (NULL);

    mask = GLITZ_FORMAT_SAMPLES_MASK | GLITZ_FORMAT_FOURCC_MASK |
	GLITZ_FORMAT_RED_SIZE_MASK | GLITZ_FORMAT_GREEN_SIZE_MASK |
	GLITZ_FORMAT_BLUE_SIZE_MASK;
    if (formatname == GLITZ_STANDARD_ARGB32)
	mask |= GLITZ_FORMAT_ALPHA_SIZE_MASK;

    /* Try for a pbuffer first */
    if (!getenv("CAIRO_TEST_FORCE_GLITZ_WINDOW"))
	dformat = glitz_glx_find_pbuffer_format (dpy, scr, mask, &templ, 0);

    if (dformat) {
	closure->win = None;

	drawable = glitz_glx_create_pbuffer_drawable (dpy, scr, dformat,
						      width, height);
	if (!drawable)
	    goto FAIL;
    } else {
	/* No pbuffer, try window */
	dformat = glitz_glx_find_window_format (dpy, scr, mask, &templ, 0);

	if (!dformat)
	    goto FAIL;

	vinfo = glitz_glx_get_visual_info_from_format(dpy,
						      DefaultScreen(dpy),
						      dformat);

	if (!vinfo)
	    goto FAIL;

	xsh.flags = PSize;
	xsh.x = 0;
	xsh.y = 0;
	xsh.width = width;
	xsh.height = height;

	xswa.colormap = XCreateColormap (dpy, RootWindow(dpy, scr),
					 vinfo->visual, AllocNone);
	closure->win = XCreateWindow (dpy, RootWindow(dpy, scr),
				      xsh.x, xsh.y, xsh.width, xsh.height,
				      0, vinfo->depth, CopyFromParent,
				      vinfo->visual, CWColormap, &xswa);
	XFree (vinfo);

	drawable =
	    glitz_glx_create_drawable_for_window (dpy, scr,
						  dformat, closure->win,
						  width, height);

	if (!drawable)
	    goto DESTROY_WINDOW;
    }

    format = glitz_find_standard_format (drawable, formatname);
    if (!format)
	goto DESTROY_DRAWABLE;

    sr = glitz_surface_create (drawable, format, width, height, 0, NULL);
    if (!sr)
	goto DESTROY_DRAWABLE;

    if (closure->win == None || dformat->doublebuffer) {
	glitz_surface_attach (sr, drawable, GLITZ_DRAWABLE_BUFFER_BACK_COLOR);
    } else {
	XMapWindow (closure->dpy, closure->win);
	glitz_surface_attach (sr, drawable, GLITZ_DRAWABLE_BUFFER_FRONT_COLOR);
    }

    glitz_drawable_destroy (drawable);

    return sr;

 DESTROY_DRAWABLE:
    glitz_drawable_destroy (drawable);
 DESTROY_WINDOW:
    if (closure->win)
	XDestroyWindow (dpy, closure->win);
 FAIL:
    return NULL;
}