summaryrefslogtreecommitdiff
path: root/main.c
blob: abd4ae01dade009ec0a4c6c5089ccf4827b030d8 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include <X11/Xlib.h>
#include <X11/extensions/Xrender.h>

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

#include "utils.h"
#include "surface.h"
#include "testscenarios.h"
#include "config.h"

int               win_w = 538;
int               win_h = 451;
Display          *disp = NULL;
Window            win;
int               test_type = AllTests;
Bool              useARGBWindow;
double            test_time = 2.0;

RenderOp all_render_ops[] = {
    {"Clear",               PictOpClear,               1},
    {"Src",                 PictOpSrc,                 1},
    {"Dst",                 PictOpDst,                 1},
    {"Over",                PictOpOver,                1},
    {"OverReverse",         PictOpOverReverse,         1},
    {"In",                  PictOpIn,                  1},
    {"InReverse",           PictOpInReverse,           1},
    {"Out",                 PictOpOut,                 1},
    {"OutReverse",          PictOpOutReverse,          1},
    {"Atop",                PictOpAtop,                1},
    {"AtopReverse",         PictOpAtopReverse,         1},
    {"Xor",                 PictOpXor,                 1},
    {"Add",                 PictOpAdd,                 1},
    {"Saturate",            PictOpSaturate,            1},
    {"DisjointClear",       PictOpDisjointClear,       1},
    {"DisjointSrc",         PictOpDisjointSrc,         0},
    {"DisjointDst",         PictOpDisjointDst,         0},
    {"DisjointOver",        PictOpDisjointOver,        0},
    {"DisjointOverReverse", PictOpDisjointOverReverse, 0},
    {"DisjointIn",          PictOpDisjointIn,          0},
    {"DisjointInReverse",   PictOpDisjointInReverse,   0},
    {"DisjointOut",         PictOpDisjointOut,         0},
    {"DisjointOutReverse",  PictOpDisjointOutReverse,  0},
    {"DisjointAtop",        PictOpDisjointAtop,        0},
    {"DisjointAtopReverse", PictOpDisjointAtopReverse, 0},
    {"DisjointXor",         PictOpDisjointXor,         0},
    {"ConjointClear",       PictOpConjointClear,       0},
    {"ConjointSrc",         PictOpConjointSrc,         0},
    {"ConjointDst",         PictOpConjointDst,         0},
    {"ConjointOver",        PictOpConjointOver,        0},
    {"ConjointOverReverse", PictOpConjointOverReverse, 0},
    {"ConjointIn",          PictOpConjointIn,          0},
    {"ConjointInReverse",   PictOpConjointInReverse,   0},
    {"ConjointOut",         PictOpConjointOut,         0},
    {"ConjointOutReverse",  PictOpConjointOutReverse,  0},
    {"ConjointAtop",        PictOpConjointAtop,        0},
    {"ConjointAtopReverse", PictOpConjointAtopReverse, 0},
    {"ConjointXor",         PictOpConjointXor,         0},
    {NULL,                  0,                         0}
};


void
test_oper(int op, XRenderSurf *src, XRenderSurf *mask, XRenderSurf *dst)
{
    int x, y;

    x = rand() % (dst->w - (src->w/2));
    y = rand() % (dst->h - (src->h/2));
    xrender_surf_blend(disp, op, src, mask, dst,
                       0, 0, 0, 0, x, y, src->w, src->h);
}

static Picture createBackgroundPicture()
{
    unsigned int w, h;
    char *data;
    Picture back;
    XRenderSurf *surf;

    readPng("images/fdo-translucent.png", &data, &w, &h);
    surf = xrender_surf_new(disp, win, PictStandardARGB32, w, h, False);
    xrender_surf_populate(disp, surf, w, h, data);
    XRenderPictureAttributes pa;
    pa.repeat = 1;
    XRenderChangePicture(disp, surf->pic, CPRepeat, &pa);

    back = surf->pic;

    if (surf->allocated)
        XFreePixmap(disp, surf->draw);
    free(surf);

    return back;
}

void
main_loop(void)
{
    RenderOp *current_op;
    Picture backgroundPict;
    int xrenderMajor, xrenderMinor;
    int j = 0;
    int numberOfScenarios = 0;

    XRenderQueryVersion(disp, &xrenderMajor, &xrenderMinor);

    srand(time(0));

    printf("X Server from: %s, Release: %d\n", ServerVendor(disp), VendorRelease(disp));
    printf("\tXrender version: %d.%d\n", xrenderMajor, xrenderMinor);
    printf("---------------------------------------------\n");


    backgroundPict = createBackgroundPicture();

    TestScenario *scenarios = create_test_scenarios(disp, win, win_w, win_h, &numberOfScenarios);

    XRenderComposite(disp, PictOpSrc, backgroundPict, None,
                     scenarios[0].dst->pic, 0, 0, 0, 0, 0, 0, win_w, win_h);
    XFlush(disp);

    for (current_op = &all_render_ops[0]; current_op->name; current_op++)
    {
        if (current_op->disabled)
            continue;

        printf("Test: %s\n", current_op->name);

        for (j = 0; j < numberOfScenarios; ++j) {
            TestScenario *currentTest = &(scenarios[j]);

            XRenderComposite(disp, PictOpSrc, backgroundPict, None,
                             currentTest->dst->pic, 0, 0, 0, 0, 0, 0, win_w,
                             win_h);

            if (test_type != AllTests) {
                if (!(currentTest->types & test_type))
                    continue;
            }

            time_test(currentTest->name, test_oper, current_op,
                      currentTest->src, currentTest->mask, currentTest->dst);

            XSync(disp, False);
        }
    }
}


void
display_help()
{
    printf("./xrenderbenchmark \n");
    printf("\t -help\t\tdisplays help\n");
    printf("\t -argb\t\tuses a 32-bit ARGB window\n");
    printf("\t -ops ops\tconfigures which ops to test\n");
    printf("\t -tests type\truns only specified tests\n");
    printf("\t\ttype can be: plain, alpha, mask, transformation, filter,\n");
    printf("\t\tand convolution\n");
    printf("\t -time sec\tcontrol the test time in seconds (default: 2 sec)\n");
    printf("\t \n");
}

void
process_args(int argc, char **argv)
{
    int i;
    for (i = 1; i < argc; ++i) {
        char *arg = argv[i];

        if (!strcmp("-help", arg)) {
            display_help();
            exit(1);
        } else if (!strcmp("-argb", arg)) {
            useARGBWindow = True;
        } else if (!strcmp("-ops", arg)) {
            char *opname, *nextname;
            RenderOp *op;

            ++i;
            if (i == argc) {
                fprintf(stderr, "Error: -ops expected a comma-separated list of ops\n");
                exit(1);
            }

            nextname = argv[i];

            for (op = &all_render_ops[0]; op->name; op++)
                op->disabled = True;

            while ((opname = strsep(&nextname, ",")) != NULL) {
                for (op = &all_render_ops[0]; op->name; op++) {
                    if (strcasecmp(op->name, opname) != 0)
                        continue;
                    op->disabled = False;
                    break;
                }
                if (!op->name) {
                    display_help();
                    exit(1);
                }
            }
        } else if (!strcmp("-tests", arg)) {
            ++i;
            if (i == argc) {
                fprintf(stderr, "Error: Must specify a test type with -tests option\n");
                exit(1);
            }
            arg = argv[i];
            if (!strcmp("plain", arg)) {
                test_type = PlainTest;
            } else if (!strcmp("alpha", arg)) {
                test_type = AlphaTest;
            } else if (!strcmp("mask", arg)) {
                test_type = MaskTest;
            } else if (!strcmp("transformation", arg)) {
                test_type = TransformationTest;
            } else if (!strcmp("filter", arg)) {
                test_type = FilterTest;
            } else if (!strcmp("convolution", arg)) {
                test_type = ConvolutionTest;
            } else {
                fprintf(stderr, "Unrecognized test type: '%s'\n", arg);
            }
        } else if (!strcmp("-time", arg)) {
            ++i;
            if (i == argc) {
                fprintf(stderr, "Error: Must specify a time in seconds with the -time option\n");
                exit(1);
            }
            arg = argv[i];
            test_time = strtod(argv[i], &arg);
            if (arg == argv[i] || *arg != '\0') {
                fprintf(stderr, "'%s': unrecognized number\n", argv[i]);
                exit(1);
            }
        } else {
            display_help();
            fprintf(stderr, "Unrecognized option: '%s'\n", arg);
            exit(1);
        }
    }

}

int
main(int argc, char **argv)
{
    struct sigaction saAlarmHandler;

    printf(PACKAGE " version " VERSION "\n");
    process_args(argc, argv);
    disp = XOpenDisplay(0);
    if (!disp) {
	fprintf(stderr, "Error: Cannot connect to display!\n");
	exit(-1);
    }

    saAlarmHandler.sa_handler = (void*)alarmhandler;
    saAlarmHandler.sa_flags = SA_RESTART;

    sigaction(SIGALRM, &saAlarmHandler, NULL);

    setup_window();
    main_loop();
    return 0;
}