take two
[profile/ivi/xorg-x11-drv-intel.git] / test / basic-stress.c
1 #include <stdint.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 #include <X11/Xutil.h> /* for XDestroyImage */
6 #include <pixman.h>
7
8 #include "test.h"
9
10 static void fill_rect(struct test_target *tt,
11                       int alu, int color,
12                       int x, int y, int w, int h)
13 {
14         XGCValues val;
15
16         val.function = alu;
17         val.foreground = color;
18         XChangeGC(tt->dpy->dpy, tt->gc, GCFunction | GCForeground, &val);
19
20         XFillRectangle(tt->dpy->dpy, tt->draw, tt->gc, x, y, w, h);
21 }
22
23 static void clear(struct test_target *tt)
24 {
25         fill_rect(tt,
26                   GXcopy, 0,
27                   0, 0, tt->width, tt->height);
28 }
29
30 static void fill(struct test_target *real,
31                  struct test_target *ref)
32 {
33         int x = rand() % (2*real->width) - real->width;
34         int y = rand() % (2*real->height) - real->height;
35         int w = rand() % (2*real->width);
36         int h = rand() % (2*real->height);
37         int color = rand();
38         int alu = rand() % 16;
39
40         fill_rect(real, alu, color, x, y, w, h);
41         fill_rect(ref, alu, color, x, y, w, h);
42 }
43
44 static void copy(struct test_target *real,
45                  struct test_target *ref)
46 {
47         int sx = rand() % (2*real->width) - ref->width;
48         int sy = rand() % (2*real->height) - ref->height;
49         int dx = rand() % (2*real->width) - ref->width;
50         int dy = rand() % (2*real->height) - ref->height;
51         int w = rand() % (2*real->width);
52         int h = rand() % (2*real->height);
53         XGCValues val;
54
55         val.function = rand() % 16;
56
57         XChangeGC(real->dpy->dpy, real->gc, GCFunction, &val);
58         XCopyArea(real->dpy->dpy,
59                   real->draw, real->draw, real->gc,
60                   sx, sy, w, h, dx, dy);
61
62         XChangeGC(ref->dpy->dpy, ref->gc, GCFunction, &val);
63         XCopyArea(ref->dpy->dpy,
64                   ref->draw, ref->draw, ref->gc,
65                   sx, sy, w, h, dx, dy);
66 }
67
68 static void _put(struct test_target *tt,
69                  int x, int y, int w,int h, int color, int alu)
70 {
71         XImage image;
72         XGCValues val;
73
74         val.function = alu;
75
76         test_init_image(&image, &tt->dpy->shm, tt->format, w, h);
77         pixman_fill((uint32_t*)image.data,
78                     image.bytes_per_line/sizeof(uint32_t),
79                     image.bits_per_pixel,
80                     0, 0, w, h, color);
81
82         XChangeGC(tt->dpy->dpy, tt->gc, GCFunction, &val);
83         if (rand() & 1) {
84                 XShmPutImage(tt->dpy->dpy, tt->draw, tt->gc, &image,
85                              0, 0, x, y, w, h, 0);
86                 XSync(tt->dpy->dpy, 1);
87         } else {
88                 XPutImage(tt->dpy->dpy, tt->draw, tt->gc, &image,
89                           0, 0, x, y, w, h);
90         }
91 }
92
93 static void put(struct test_target *real,
94                 struct test_target *ref)
95 {
96         int x = rand() % (2*real->width) - real->width;
97         int y = rand() % (2*real->height) - real->height;
98         int w = rand() % real->width;
99         int h = rand() % real->height;
100         int color = rand();
101         int alu = rand() % 16;
102
103         _put(real, x, y, w, h, color, alu);
104         _put(ref, x, y, w, h, color, alu);
105 }
106
107 static void rect_tests(struct test *test, int iterations, enum target target)
108 {
109         struct test_target real, ref;
110         void (* const ops[])(struct test_target *, struct test_target *) = {
111                 copy,
112                 fill,
113                 put,
114         };
115         int n;
116
117         printf("Running mixed ops stress against %s: ",
118                test_target_name(target));
119         fflush(stdout);
120
121         test_target_create_render(&test->real, target, &real);
122         test_target_create_render(&test->ref, target, &ref);
123
124         clear(&real);
125         clear(&ref);
126
127         for (n = 0; n < iterations; n++)
128                 ops[rand() % ARRAY_SIZE(ops)](&real, &ref);
129
130         test_compare(test,
131                      real.draw, real.format,
132                      ref.draw, ref.format,
133                      0, 0, real.width, real.height,
134                      "");
135
136         printf("passed [%d iterations]\n", n);
137
138         test_target_destroy_render(&test->real, &real);
139         test_target_destroy_render(&test->ref, &ref);
140 }
141
142 int main(int argc, char **argv)
143 {
144         struct test test;
145         int i;
146
147         test_init(&test, argc, argv);
148
149         for (i = 0; i <= DEFAULT_ITERATIONS; i++) {
150                 int iterations = 1 << i;
151                 rect_tests(&test, iterations, 0);
152                 rect_tests(&test, iterations, 1);
153         }
154
155         return 0;
156 }