summaryrefslogtreecommitdiff
path: root/main.c
blob: b35612eae8b26ae111f45d88b9f14e015c0fc065 (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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
#include "main.h"

#define REPS 4096

typedef struct _xrender_surf
{
   int       w, h;
   int       depth;
   Visual   *vis;
   Drawable  draw;
   Picture   pic;
   int       allocated : 1;
} Xrender_Surf;

double get_time(void);
void   time_test(char *description, void (*func) (void));

Xrender_Surf *xrender_surf_new(Display *disp, Drawable draw, Visual *vis, int w, int h, int alpha);
Xrender_Surf *xrender_surf_adopt(Display *disp, Drawable draw, Visual *vis, int w, int h);
void          xrender_surf_free(Display *disp, Xrender_Surf *rs);
void          xrender_surf_populate(Display *disp, Xrender_Surf *rs, int w, int h, int *img_data);
void          xrender_surf_blend(Display *disp, Xrender_Surf *src, Xrender_Surf *dst, int x, int y, int w, int h, int smooth);

void populate_from_file(Display *disp, Xrender_Surf *rs, char *file);
void main_loop(void);
void setup_window(void);
void sync_after_test(void);

int               win_w = 320;
int               win_h = 320;
static Display   *disp = NULL;
static Window     win;
long long         pixels_counter;

double
get_time(void)
{
   struct timeval      timev;
   
   gettimeofday(&timev, NULL);
   return (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
}

void
time_test(char *description, void (*func) (void))
{
   double t1, t2, t;
   int i;

   pixels_counter = 0;
   printf("---------------------------------------------------------------\n");
   printf("Test: %s\n", description);
   t1 = get_time();
   for (i = 0; i < REPS; i++) func();
   sync_after_test();
   t2 = get_time();
   t = t2 - t1;
   printf("Time: %3.3f sec.\n", t);
   if (pixels_counter > 0 && t > 0)
      printf("Perf: %3.3f MPix/s\n", (double)pixels_counter / ((double)t * 1000000.));
}

Xrender_Surf *
xrender_surf_new(Display *disp, Drawable draw, Visual *vis, int w, int h, int alpha)
{
   Xrender_Surf *rs;
   XRenderPictFormat *fmt;
   XRenderPictureAttributes att;
   
   rs = calloc(1, sizeof(Xrender_Surf));
   if (alpha)
     fmt = XRenderFindStandardFormat(disp, PictStandardARGB32);
   else
     fmt = XRenderFindStandardFormat(disp, PictStandardRGB24);
   rs->w = w;
   rs->h = h;
   rs->depth = fmt->depth;
   rs->vis = vis;
   rs->draw = XCreatePixmap(disp, draw, w, h, fmt->depth);
   att.dither = 1;
   att.component_alpha = 1;
   att.repeat = 0;
   rs->pic = XRenderCreatePicture(disp, rs->draw, fmt, CPRepeat | CPDither | CPComponentAlpha, &att);
   rs->allocated = 1;
   return rs;
}

Xrender_Surf *
xrender_surf_adopt(Display *disp, Drawable draw, Visual *vis, int w, int h)
{
   Xrender_Surf *rs;
   XRenderPictFormat *fmt;
   XRenderPictureAttributes att;
   
   rs = calloc(1, sizeof(Xrender_Surf));
   fmt = XRenderFindVisualFormat(disp, vis);
   rs->w = w;
   rs->h = h;
   rs->depth = fmt->depth;
   rs->vis = vis;
   rs->draw = draw;
   att.dither = 1;
   att.component_alpha = 1;
   att.repeat = 0;
   rs->pic = XRenderCreatePicture(disp, rs->draw, fmt, CPRepeat | CPDither | CPComponentAlpha, &att);
   rs->allocated = 0;
   return rs;
}

void
xrender_surf_free(Display *disp, Xrender_Surf *rs)
{
   if (rs->allocated) XFreePixmap(disp, rs->draw);
   XRenderFreePicture(disp, rs->pic);
   free(rs);
}

void
xrender_surf_populate(Display *disp, Xrender_Surf *rs, int w, int h, int *img_data)
{
   GC gc;
   XGCValues gcv;
   XImage *xim;
   int x, y;
   
   /* yes this isn't optimal - i know.. i just want some data for now */
   gc = XCreateGC(disp, rs->draw, 0, &gcv);
   xim = XCreateImage(disp, rs->vis, rs->depth, ZPixmap, 0, NULL, w, h, 32, 0);
   xim->data = malloc(xim->bytes_per_line * xim->height);
   for (y = 0; y < h; y++)
     {
	for (x = 0; x < w; x++)
	  {
	     int pixel;
	     int a, r, g, b;
	     
	     pixel = img_data[(y * w) + x];
	     a = (pixel >> 24) & 0xff;
	     r = (pixel >> 16) & 0xff;
	     g = (pixel >> 8 ) & 0xff;
	     b = (pixel      ) & 0xff;
	     r = (r * (a + 1)) / 256;
	     g = (g * (a + 1)) / 256;
	     b = (b * (a + 1)) / 256;
	     XPutPixel(xim, x, y, (a << 24) | (r << 16) | (g << 8) | b);
	  }
     }
   XPutImage(disp, rs->draw, gc, xim, 0, 0, 0, 0, w, h);
   free(xim->data);
   xim->data = NULL;
   XDestroyImage(xim);
   XFreeGC(disp, gc);
}

void
xrender_surf_blend(Display *disp, Xrender_Surf *src, Xrender_Surf *dst, int x, int y, int w, int h, int smooth)
{
   XFilters *flt;
   XTransform xf;
   
   xf.matrix[0][0] = (65536 * src->w) / w; xf.matrix[0][1] = 0; xf.matrix[0][2] = 0;
   xf.matrix[1][0] = 0; xf.matrix[1][1] = (65536 * src->h) / h; xf.matrix[1][2] = 0;
   xf.matrix[2][0] = 0; xf.matrix[2][1] = 0; xf.matrix[2][2] = 65536;
   if (smooth) XRenderSetPictureFilter(disp, src->pic, "bilinear", NULL, 0);
   else XRenderSetPictureFilter(disp, src->pic, "nearest", NULL, 0);
   XRenderSetPictureTransform(disp, src->pic, &xf);
   XRenderComposite(disp, PictOpOver, src->pic, None, dst->pic, 0, 0, 0, 0, x, y, w, h);
}

void
populate_from_file(Display *disp, Xrender_Surf *rs, char *file)
{
   Imlib_Image im;
   DATA32 *pixels;
   int w, h;
   
   im = imlib_load_image(file);
   imlib_context_set_image(im);
   pixels = imlib_image_get_data_for_reading_only();
   w = imlib_image_get_width();
   h = imlib_image_get_height();
   xrender_surf_populate(disp, rs, w, h, pixels);
   imlib_image_put_back_data(pixels);
   imlib_free_image_and_decache();   
}

Xrender_Surf *surf_win = NULL;
Xrender_Surf *surf_off = NULL;
Xrender_Surf *surf_img = NULL;

Imlib_Image   im_win = NULL;
Imlib_Image   im_img = NULL;

void
touch_drawable(Display *disp, Drawable draw)
{
   XImage *image = image = XGetImage(disp, draw, 0, 0, 1, 1, 0xffffffff, ZPixmap);
   if (image)
      XDestroyImage(image);
}

/*
 * Try to read from all the surfaces in order to ensure that the
 * rendering is done and all the pending operations finished for them.
 */
void
sync_after_test()
{
   touch_drawable(disp, surf_win->draw);
   touch_drawable(disp, surf_off->draw);
   touch_drawable(disp, surf_img->draw);
   XSync(disp, False);
}

void
test_over_x(void)
{
   int x, y;
   
   x = rand() % (surf_win->w - surf_img->w);
   y = rand() % (surf_win->h - surf_img->h);
   xrender_surf_blend(disp, surf_img, surf_win, x, y, surf_img->w, surf_img->h, 1);
   pixels_counter += surf_img->w * surf_img->h;
}

void
test_over_off_x(void)
{
   int x, y;
   
   x = rand() % (surf_off->w - surf_img->w);
   y = rand() % (surf_off->h - surf_img->h);
   xrender_surf_blend(disp, surf_img, surf_off, x, y, surf_img->w, surf_img->h, 1);
   pixels_counter += surf_img->w * surf_img->h;
}

void
test_over_imlib2(void)
{
   int x, y;
   int w, h, ww, hh;
   
   imlib_context_set_anti_alias(1);
   imlib_context_set_image(im_win);
   ww = imlib_image_get_width();
   hh = imlib_image_get_height();
   imlib_context_set_image(im_img);
   w = imlib_image_get_width();
   h = imlib_image_get_height();
   x = rand() % (ww - w);
   y = rand() % (hh - h);
   imlib_context_set_image(im_win);   
   imlib_blend_image_onto_image(im_img, 0, 0, 0, w, h, x, y, w, h);
   pixels_counter += w * h;
}

void
test_over_scale_half_x(void)
{
   int x, y;
   
   x = rand() % (surf_win->w - (surf_img->w / 2));
   y = rand() % (surf_win->h - (surf_img->h / 2));
   xrender_surf_blend(disp, surf_img, surf_win, x, y, surf_img->w / 2, surf_img->h / 2, 0);
   pixels_counter += (surf_img->w / 2) * (surf_img->h / 2);
}

void
test_over_off_scale_half_x(void)
{
   int x, y;
   
   x = rand() % (surf_off->w - (surf_img->w / 2));
   y = rand() % (surf_off->h - (surf_img->h / 2));
   xrender_surf_blend(disp, surf_img, surf_off, x, y, surf_img->w / 2, surf_img->h / 2, 0);
   pixels_counter += (surf_img->w / 2) * (surf_img->h / 2);
}

void
test_over_scale_half_imlib2(void)
{
   int x, y;
   int w, h, ww, hh;
   
   imlib_context_set_anti_alias(0);
   imlib_context_set_image(im_win);
   ww = imlib_image_get_width();
   hh = imlib_image_get_height();
   imlib_context_set_image(im_img);
   w = imlib_image_get_width();
   h = imlib_image_get_height();
   x = rand() % (ww - (w / 2));
   y = rand() % (hh - (h / 2));
   imlib_context_set_image(im_win);   
   imlib_blend_image_onto_image(im_img, 0, 0, 0, w, h, x, y, w / 2, h / 2);
   pixels_counter += (w / 2) * (h / 2);
}

void
test_over_scale_double_smooth_x(void)
{
   int x, y;
   
   x = rand() % (surf_win->w - (surf_img->w * 2));
   y = rand() % (surf_win->h - (surf_img->h * 2));
   xrender_surf_blend(disp, surf_img, surf_win, x, y, surf_img->w * 2, surf_img->h * 2, 1);
   pixels_counter += (surf_img->w * 2) * (surf_img->h * 2);
}

void
test_over_off_scale_double_smooth_x(void)
{
   int x, y;
   
   x = rand() % (surf_off->w - (surf_img->w * 2));
   y = rand() % (surf_off->h - (surf_img->h * 2));
   xrender_surf_blend(disp, surf_img, surf_off, x, y, surf_img->w * 2, surf_img->h * 2, 1);
   pixels_counter += (surf_img->w * 2) * (surf_img->h * 2);
}

void
test_over_scale_double_smooth_imlib2(void)
{
   int x, y;
   int w, h, ww, hh;
   
   imlib_context_set_anti_alias(1);
   imlib_context_set_image(im_win);
   ww = imlib_image_get_width();
   hh = imlib_image_get_height();
   imlib_context_set_image(im_img);
   w = imlib_image_get_width();
   h = imlib_image_get_height();
   x = rand() % (ww - (w * 2));
   y = rand() % (hh - (h * 2));
   imlib_context_set_image(im_win);   
   imlib_blend_image_onto_image(im_img, 0, 0, 0, w, h, x, y, w * 2, h * 2);
   pixels_counter += (w * 2) * (h * 2);
}

void
test_over_scale_double_nearest_x(void)
{
   int x, y;
   
   x = rand() % (surf_win->w - (surf_img->w * 2));
   y = rand() % (surf_win->h - (surf_img->h * 2));
   xrender_surf_blend(disp, surf_img, surf_win, x, y, surf_img->w * 2, surf_img->h * 2, 0);
   pixels_counter += (surf_img->w * 2) * (surf_img->h * 2);
}

void
test_over_off_scale_double_nearest_x(void)
{
   int x, y;
   
   x = rand() % (surf_off->w - (surf_img->w * 2));
   y = rand() % (surf_off->h - (surf_img->h * 2));
   xrender_surf_blend(disp, surf_img, surf_off, x, y, surf_img->w * 2, surf_img->h * 2, 0);
   pixels_counter += (surf_img->w * 2) * (surf_img->h * 2);
}

void
test_over_scale_double_nearest_imlib2(void)
{
   int x, y;
   int w, h, ww, hh;
   
   imlib_context_set_anti_alias(0);
   imlib_context_set_image(im_win);
   ww = imlib_image_get_width();
   hh = imlib_image_get_height();
   imlib_context_set_image(im_img);
   w = imlib_image_get_width();
   h = imlib_image_get_height();
   x = rand() % (ww - (w * 2));
   y = rand() % (hh - (h * 2));
   imlib_context_set_image(im_win);   
   imlib_blend_image_onto_image(im_img, 0, 0, 0, w, h, x, y, w * 2, h * 2);
   pixels_counter += (w * 2) * (h * 2);
}

/*
 * A coefficient responsible for making the number of
 * read and written pixels approximately the same
 */
#define MAX_SCALE 1.427050983 

void
test_over_scale_rand_smooth_x(void)
{
   int x, y;
   int max_w = MAX_SCALE * surf_img->w;
   int max_h = MAX_SCALE * surf_img->h;
   int dst_w = surf_img->w / 2 + rand() % (max_w - surf_img->w / 2);
   int dst_h = surf_img->w / 2 + rand() % (max_w - surf_img->w / 2);

   x = rand() % (surf_win->w - dst_w);
   y = rand() % (surf_win->h - dst_h);
   xrender_surf_blend(disp, surf_img, surf_win, x, y, dst_w, dst_h, 1);
   pixels_counter += dst_w * dst_h;
}

void
test_over_off_scale_rand_smooth_x(void)
{
   int x, y;
   int max_w, max_h, dst_w, dst_h;
   max_w = MAX_SCALE * surf_img->w;
   max_h = MAX_SCALE * surf_img->h;
   dst_w = surf_img->w / 2 + rand() % (max_w - surf_img->w / 2);
   dst_h = surf_img->w / 2 + rand() % (max_w - surf_img->w / 2);

   x = rand() % (surf_off->w - dst_w);
   y = rand() % (surf_off->h - dst_h);
   xrender_surf_blend(disp, surf_img, surf_off, x, y, dst_w, dst_h, 1);
   pixels_counter += dst_w * dst_h;
}

void
test_over_scale_rand_smooth_imlib2(void)
{
   int x, y;
   int w, h, ww, hh;
   int max_w, max_h, dst_w, dst_h;

   imlib_context_set_anti_alias(1);
   imlib_context_set_image(im_win);
   ww = imlib_image_get_width();
   hh = imlib_image_get_height();
   imlib_context_set_image(im_img);
   w = imlib_image_get_width();
   h = imlib_image_get_height();
   max_w = MAX_SCALE * w;
   max_h = MAX_SCALE * h;
   dst_w = w / 2 + rand() % (max_w - w / 2);
   dst_h = w / 2 + rand() % (max_w - w / 2);
   x = rand() % (ww - dst_w);
   y = rand() % (hh - dst_h);
   imlib_context_set_image(im_win);   
   imlib_blend_image_onto_image(im_img, 0, 0, 0, w, h, x, y, dst_w, dst_h);
   pixels_counter += dst_w * dst_h;
}

void
test_over_scale_rand_nearest_x(void)
{
   int x, y;
   int max_w, max_h, dst_w, dst_h;
   max_w = MAX_SCALE * surf_img->w;
   max_h = MAX_SCALE * surf_img->h;
   dst_w = surf_img->w / 2 + rand() % (max_w - surf_img->w / 2);
   dst_h = surf_img->w / 2 + rand() % (max_w - surf_img->w / 2);

   x = rand() % (surf_off->w - dst_w);
   y = rand() % (surf_off->h - dst_h);
   xrender_surf_blend(disp, surf_img, surf_win, x, y, dst_w, dst_h, 0);
   pixels_counter += dst_w * dst_h;
}

void
test_over_off_scale_rand_nearest_x(void)
{
   int x, y;
   int max_w, max_h, dst_w, dst_h;
   max_w = MAX_SCALE * surf_img->w;
   max_h = MAX_SCALE * surf_img->h;
   dst_w = surf_img->w / 2 + rand() % (max_w - surf_img->w / 2);
   dst_h = surf_img->w / 2 + rand() % (max_w - surf_img->w / 2);

   x = rand() % (surf_off->w - dst_w);
   y = rand() % (surf_off->h - dst_h);
   xrender_surf_blend(disp, surf_img, surf_off, x, y, dst_w, dst_h, 0);
   pixels_counter += dst_w * dst_h;
}

void
test_over_scale_rand_nearest_imlib2(void)
{
   int x, y;
   int w, h, ww, hh;
   int max_w, max_h, dst_w, dst_h;
   
   imlib_context_set_anti_alias(0);
   imlib_context_set_image(im_win);
   ww = imlib_image_get_width();
   hh = imlib_image_get_height();
   imlib_context_set_image(im_img);
   w = imlib_image_get_width();
   h = imlib_image_get_height();
   max_w = MAX_SCALE * w;
   max_h = MAX_SCALE * h;
   dst_w = w / 2 + rand() % (max_w - w / 2);
   dst_h = w / 2 + rand() % (max_w - w / 2);
   x = rand() % (ww - dst_w);
   y = rand() % (hh - dst_h);
   imlib_context_set_image(im_win);
   imlib_blend_image_onto_image(im_img, 0, 0, 0, w, h, x, y, dst_w, dst_h);
   pixels_counter += dst_w * dst_h;
}

void
main_loop(void)
{
   /* printf query filters */
   printf("Available XRENDER filters:\n");
     {
	int i;
	XFilters *flt;
	
	flt = XRenderQueryFilters(disp, win);
	for (i = 0; i < flt->nfilter; i++) printf("%s\n", flt->filter[i]);
     }
   printf("Setup...\n");
   /* setup */
   surf_win = xrender_surf_adopt(disp, win, DefaultVisual(disp, DefaultScreen(disp)), win_w, win_h);
   surf_off = xrender_surf_new(disp, win, DefaultVisual(disp, DefaultScreen(disp)), 320, 320, 0);
   surf_img = xrender_surf_new(disp, win, DefaultVisual(disp, DefaultScreen(disp)), 100, 100, 1);
   populate_from_file(disp, surf_win, "tst_opaque.png");
   populate_from_file(disp, surf_off, "tst_opaque.png");
   populate_from_file(disp, surf_img, "tst_transparent.png");
   
   im_win = imlib_load_image("tst_opaque.png");
   im_img = imlib_load_image("tst_transparent.png");

   printf("*** ROUND 1 ***\n");
   srand(7);
   time_test("Test Xrender doing non-scaled Over blends", test_over_x);
   srand(7);
   time_test("Test Xrender (offscreen) doing non-scaled Over blends", test_over_off_x);
   srand(7);
   time_test("Test Imlib2 doing non-scaled Over blends", test_over_imlib2);
   imlib_context_set_image(im_win);   
   imlib_context_set_display(disp);
   imlib_context_set_visual(DefaultVisual(disp, DefaultScreen(disp)));
   imlib_context_set_colormap(DefaultColormap(disp, DefaultScreen(disp)));
   imlib_context_set_drawable(win);
   imlib_render_image_on_drawable(0, 0);
   sleep(2);
   printf("*** ROUND 2 ***\n");
   srand(7);
   time_test("Test Xrender doing 1/2 scaled Over blends", test_over_scale_half_x);
   srand(7);
   time_test("Test Xrender (offscreen) doing 1/2 scaled Over blends", test_over_off_scale_half_x);
   srand(7);
   time_test("Test Imlib2 doing 1/2 scaled Over blends", test_over_scale_half_imlib2);
   imlib_context_set_image(im_win);   
   imlib_context_set_display(disp);
   imlib_context_set_visual(DefaultVisual(disp, DefaultScreen(disp)));
   imlib_context_set_colormap(DefaultColormap(disp, DefaultScreen(disp)));
   imlib_context_set_drawable(win);
   imlib_render_image_on_drawable(0, 0);
   sleep(2);
   printf("*** ROUND 3 ***\n");
   srand(7);
   time_test("Test Xrender doing 2* smooth scaled Over blends", test_over_scale_double_smooth_x);
   srand(7);
   time_test("Test Xrender (offscreen) doing 2* smooth scaled Over blends", test_over_off_scale_double_smooth_x);
   srand(7);
   time_test("Test Imlib2 doing 2* smooth scaled Over blends", test_over_scale_double_smooth_imlib2);
   imlib_context_set_image(im_win);   
   imlib_context_set_display(disp);
   imlib_context_set_visual(DefaultVisual(disp, DefaultScreen(disp)));
   imlib_context_set_colormap(DefaultColormap(disp, DefaultScreen(disp)));
   imlib_context_set_drawable(win);
   imlib_render_image_on_drawable(0, 0);
   sleep(2);
   printf("*** ROUND 4 ***\n");
   srand(7);
   time_test("Test Xrender doing 2* nearest scaled Over blends", test_over_scale_double_nearest_x);
   srand(7);
   time_test("Test Xrender (offscreen) doing 2* nearest scaled Over blends", test_over_off_scale_double_nearest_x);
   srand(7);
   time_test("Test Imlib2 doing 2* nearest scaled Over blends", test_over_scale_double_nearest_imlib2);
   imlib_context_set_image(im_win);   
   imlib_context_set_display(disp);
   imlib_context_set_visual(DefaultVisual(disp, DefaultScreen(disp)));
   imlib_context_set_colormap(DefaultColormap(disp, DefaultScreen(disp)));
   imlib_context_set_drawable(win);
   imlib_render_image_on_drawable(0, 0);
   sleep(2);
   printf("*** ROUND 6 ***\n");
   srand(7);
   time_test("Test Xrender doing random nearest scaled Over blends", test_over_scale_rand_nearest_x);
   srand(7);
   time_test("Test Xrender (offscreen) doing random nearest scaled Over blends", test_over_off_scale_rand_nearest_x);
   srand(7);
   time_test("Test Imlib2 doing random nearest scaled Over blends", test_over_scale_rand_nearest_imlib2);
   imlib_context_set_image(im_win);   
   imlib_context_set_display(disp);
   imlib_context_set_visual(DefaultVisual(disp, DefaultScreen(disp)));
   imlib_context_set_colormap(DefaultColormap(disp, DefaultScreen(disp)));
   imlib_context_set_drawable(win);
   imlib_render_image_on_drawable(0, 0);
   sleep(2);
   printf("*** ROUND 7 ***\n");
   srand(7);
   time_test("Test Xrender doing random smooth scaled Over blends", test_over_scale_rand_smooth_x);
   srand(7);
   time_test("Test Xrender (offscreen) doing random smooth scaled Over blends", test_over_off_scale_rand_smooth_x);
   srand(7);
   time_test("Test Imlib2 doing random smooth scaled Over blends", test_over_scale_rand_smooth_imlib2);
   imlib_context_set_image(im_win);   
   imlib_context_set_display(disp);
   imlib_context_set_visual(DefaultVisual(disp, DefaultScreen(disp)));
   imlib_context_set_colormap(DefaultColormap(disp, DefaultScreen(disp)));
   imlib_context_set_drawable(win);
   imlib_render_image_on_drawable(0, 0);
   sleep(2);
   
   XSync(disp, False);
}

void
setup_window(void)
{
   XSetWindowAttributes att;
   XClassHint *xch;

   att.background_pixmap = None;
   att.colormap = DefaultColormap(disp, DefaultScreen(disp));
   att.border_pixel = 0;
   att.event_mask = 
     ButtonPressMask | 
     ButtonReleaseMask |
     EnterWindowMask |
     LeaveWindowMask |
     PointerMotionMask | 
     ExposureMask | 
     StructureNotifyMask | 
     KeyPressMask | 
     KeyReleaseMask;
   win = XCreateWindow(disp,
		       RootWindow(disp, DefaultScreen(disp)),
		       0, 0, win_w, win_h, 0,
		       DefaultDepth(disp, DefaultScreen(disp)),
		       InputOutput,
		       DefaultVisual(disp, DefaultScreen(disp)),
		       CWColormap | CWBorderPixel | CWEventMask | CWBackPixmap,
		       &att);
   XStoreName(disp, win, "Render Test Program");
   xch = XAllocClassHint();
   xch->res_name = "Main";
   xch->res_class = "Render_Demo";
   XSetClassHint(disp, win, xch);
   XFree(xch);
   XMapWindow(disp, win);
   XSync(disp, False);
   usleep(200000);
   XSync(disp, False);
}

int 
main(int argc, char **argv)
{
   disp = XOpenDisplay(NULL);
   if (!disp)
     {
	printf("ERROR: Cannot connect to display!\n");
	exit(-1);
     }
   setup_window();
   main_loop();
   return 0;
}