summaryrefslogtreecommitdiff
path: root/utils.c
blob: 7f71f0ecb91d4061910e74ce8e75e10a376fa9ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "utils.h"

#include "surface.h"
#include "testscenarios.h"

#include <X11/Xlib.h>
#include <X11/extensions/Xrender.h>

#include <math.h>
#include <sys/time.h>
#include <time.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

extern int win_w;
extern int win_h;
extern Display   *disp;
extern Window     win;
extern Bool useARGBWindow;
extern double test_time;

static const char *const FILLER =
"......................................................................";

double
get_time(void)
{
    struct timeval      timev;

    gettimeofday(&timev, NULL);
    return (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
}

static int keep_running = 0;

void
time_test(char *description, void (*func) (int op, XRenderSurf *src, XRenderSurf *mask, XRenderSurf *dst),
          RenderOp *op, XRenderSurf *src, XRenderSurf *mask, XRenderSurf *dst)
{
    char buf[51];
    struct itimerval t;
    int frame_cnt = 0;
    double elapsed_time;
    struct timeval start, end;
    XImage *img;
    snprintf(buf, 50, "%s%s", description, FILLER);

    t.it_interval.tv_sec = 0;
    t.it_interval.tv_usec = 0;
    t.it_value.tv_sec = floor(test_time);
    t.it_value.tv_usec = (test_time - floor(test_time)) * 1000000;

    printf("\t\t %s", buf);
    fflush(NULL);
    keep_running = 1;
    setitimer(ITIMER_REAL, &t, NULL);

    gettimeofday(&start, NULL);

    while (keep_running) {
        func(op->op, src, mask, dst);
        ++frame_cnt;
        // Avoid queuing up way too many X requests in case rendering is really
        // slow.  Otherwise, we could overshoot the target time significantly.
        if (frame_cnt % 50 == 0)
            XSync(disp, 0);
    }

    // Read back a pixel to flush any queued GPU rendering.
    img = XGetImage(disp, src->draw, 0, 0, 1, 1, AllPlanes, ZPixmap);

    gettimeofday(&end, NULL);

    XDestroyImage(img);

    elapsed_time = end.tv_sec - start.tv_sec + (end.tv_usec / 1000000.0 - start.tv_usec / 1000000.0);

    printf ("%d frames in %g seconds = %.3f FPS\n", frame_cnt, elapsed_time,
            frame_cnt / elapsed_time);
}

void
populate_from_file(Display *disp, XRenderSurf *rs, const char *file)
{
    char *img_data;
    unsigned int w, h;

    readPng(file, &img_data, &w, &h);
    xrender_surf_populate(disp, rs, w, h, img_data);
}


void
setup_window(void)
{
    XSetWindowAttributes att;
    XClassHint *xch;
    int scrn = DefaultScreen(disp);
    int depth = DefaultDepth(disp, scrn);
    Window parent = RootWindow(disp, scrn);
    Visual *vis = DefaultVisual(disp, scrn);

    if (useARGBWindow) {
        // Override the default visual and find a 32-bit one instead.
        XVisualInfo vis_info;

        depth = 32;
        if (!XMatchVisualInfo(disp, scrn, depth, TrueColor, &vis_info)) {
            fprintf(stderr, "Failed to find a 32-bit TrueColor visual\n");
            exit(1);
        }
        vis = vis_info.visual;
    }

    att.background_pixmap = None;
    att.colormap = XCreateColormap(disp, parent, vis, AllocNone);
    att.border_pixel = 0;
    att.event_mask =
        ButtonPressMask |
        ButtonReleaseMask |
        EnterWindowMask |
        LeaveWindowMask |
        PointerMotionMask |
        ExposureMask |
        StructureNotifyMask |
        KeyPressMask |
        KeyReleaseMask;
    win = XCreateWindow(disp, parent, 0, 0, win_w, win_h, 0, depth,
                        InputOutput, vis,
                        CWColormap | CWBorderPixel | CWEventMask | CWBackPixmap,
                        &att);
    XStoreName(disp, win, "XRender Benchmark");
    xch = XAllocClassHint();
    xch->res_name = "Main";
    xch->res_class = "XRenderBenchmark";
    XSetClassHint(disp, win, xch);
    XFree(xch);
    XMapWindow(disp, win);
    XSync(disp, False);
}

void alarmhandler(int sig)
{
    if (sig == SIGALRM) {
        keep_running = 0;
    }
}