summaryrefslogtreecommitdiff
path: root/main.c
blob: ab9ccde85adfd4159dfa5812f712f6fb2af9dfa6 (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
#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);

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

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;

   printf("---------------------------------------------------------------\n");
   printf("Test: %s\n", description);
   t1 = get_time();
   for (i = 0; i < REPS; i++) func();
   XSync(disp, False);
   t2 = get_time();
   t = t2 - t1;
   printf("Time: %3.3f sec.\n", t);
}

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
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);
}

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);
}

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);
}

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);
}

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);
}

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);
}

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);
}

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);
}

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);
}

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);
}

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);
}

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);
}

int count = 0;

void
test_over_scale_general_nearest_x(void)
{
   int w, h;
   
   w = 1 + ((surf_img->w * count) / (REPS / 16));
   h = 1 + ((surf_img->h * count) / (REPS / 16));
   xrender_surf_blend(disp, surf_img, surf_win, 0, 0, w, h, 0);
   count++;
}

void
test_over_off_scale_general_nearest_x(void)
{
   int w, h;
   
   w = 1 + ((surf_img->w * count) / (REPS / 16));
   h = 1 + ((surf_img->h * count) / (REPS / 16));
   xrender_surf_blend(disp, surf_img, surf_off, 0, 0, w, h, 0);
   count++;
}

void
test_over_scale_general_nearest_imlib2(void)
{
   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();
   ww = 1 + ((w * count) / (REPS / 16));
   hh = 1 + ((h * count) / (REPS / 16));
   imlib_context_set_image(im_win);
   imlib_blend_image_onto_image(im_img, 0, 0, 0, w, h, 0, 0, ww, hh);
   count++;
}

void
test_over_scale_general_smooth_x(void)
{
   int w, h;
   
   w = 1 + ((surf_img->w * count) / (REPS / 16));
   h = 1 + ((surf_img->h * count) / (REPS / 16));
   xrender_surf_blend(disp, surf_img, surf_win, 0, 0, w, h, 1);
   count++;
}

void
test_over_off_scale_general_smooth_x(void)
{
   int w, h;
   
   w = 1 + ((surf_img->w * count) / (REPS / 16));
   h = 1 + ((surf_img->h * count) / (REPS / 16));
   xrender_surf_blend(disp, surf_img, surf_off, 0, 0, w, h, 1);
   count++;
}

void
test_over_scale_general_smooth_imlib2(void)
{
   int w, h, ww, hh;
   
   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();
   ww = 1 + ((w * count) / (REPS / 16));
   hh = 1 + ((h * count) / (REPS / 16));
   if ((ww < w) && (hh < h))
     imlib_context_set_anti_alias(0);
   else
     imlib_context_set_anti_alias(1);
   imlib_context_set_image(im_win);   
   imlib_blend_image_onto_image(im_img, 0, 0, 0, w, h, 0, 0, ww, hh);
   count++;
}

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);
   printf("*** ROUND 6 ***\n");
   count = 0;
   time_test("Test Xrender doing general nearest scaled Over blends", test_over_scale_general_nearest_x);
   count = 0;
   time_test("Test Xrender (offscreen) doing general nearest scaled Over blends", test_over_off_scale_general_nearest_x);
   count = 0;
   time_test("Test Imlib2 doing general nearest scaled Over blends", test_over_scale_general_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");
   count = 0;
   time_test("Test Xrender doing general smooth scaled Over blends", test_over_scale_general_smooth_x);
   count = 0;
   time_test("Test Xrender (offscreen) doing general smooth scaled Over blends", test_over_off_scale_general_smooth_x);
   count = 0;
   time_test("Test Imlib2 doing general smooth scaled Over blends", test_over_scale_general_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;
}