summaryrefslogtreecommitdiffstats
path: root/tests/test-pango-gdk.c
blob: 1ff041c69b75a79468c3585a0d091f24a8217899 (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
/*
  test-pango-gdk.c -- Measure fullscreen write speed under X. 

  Copyright (C) 2005 Matthew Allum, Openedhand Ltd. 

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.

  Matthew Allum mallum@openedhand.com

  ===
 
  Compile with

  gcc -Wall -O2 ` pkg-config --libs --cflags gtk+-2.0` test-pango-gdk.c -o test-pango-gdk

*/

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

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>

#include <pango/pango.h>
#include <pango/pangoxft.h>
#include <gtk/gtk.h>
#include <gdk/gdkx.h>

#define VERSION "0.1"

#define DEFAULT_TEXT_STR "abcdefghijklmnopqrstuvwxyz"
#define DEFAULT_FONT     "sans serif 18"
#define DEFAULT_N_LINES  20
#define DEFAULT_N_CYCLES 100

static char *TextStr     = DEFAULT_TEXT_STR;
static char *TextFont    = DEFAULT_FONT;
static int   TextNLines  = DEFAULT_N_LINES;
static int   TotalCycles = 100;

static GtkWidget *window;

static Display *dpy;
static int      scr;	
//static Visual  *vis;
//static Window   root, win;
//static int      depth;
//static GC       gc;

static Bool     Verbose;


static unsigned long long
GetTimeInMillis(void)
{
  struct timeval  tp;

  gettimeofday(&tp, 0);
  return (unsigned long long)(tp.tv_sec * 1000) + (tp.tv_usec / 1000);
}

static void
x_open(void)
{
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_default_size (GTK_WINDOW (window), gdk_screen_get_width (gdk_screen_get_default()), gdk_screen_get_height (gdk_screen_get_default()));
  gtk_widget_show (window);
  gtk_widget_map (window);
  gtk_widget_set_double_buffered (window, FALSE);
  gdk_window_fullscreen (window->window);
  dpy = gdk_x11_get_default_xdisplay ();
  scr = gdk_x11_get_default_screen ();
  gdk_display_sync (gdk_display_get_default ());
}

static void
x_blit(void)
{
  int          x = 0, y = 0, i, j, nchars;

  PangoContext         *pgo_context;
  PangoFontMap         *pgo_fontmap;
  PangoFontDescription *fontdes;
  PangoFont            *font;
  PangoFontMetrics     *metrics;

  XftDraw              *xftdraw;
  XftColor              xftcol;    
  XRenderColor          colortmp;

  unsigned long long start_clock, finish_clock, diff_clock; 

  start_clock = GetTimeInMillis(); 

  colortmp.red   = 0x0;
  colortmp.green = 0x0;
  colortmp.blue  = 0x0;
  colortmp.alpha = 0xffff; 

  XftColorAllocValue(dpy, DefaultVisual(dpy, scr), 
		     DefaultColormap(dpy, scr),
		     &colortmp, &xftcol);


  pgo_context = pango_xft_get_context (dpy, scr);
  pgo_fontmap = pango_xft_get_font_map (dpy, scr);

  if ((fontdes = pango_font_description_from_string(TextFont)) == NULL)
    {
      fprintf(stderr, "Failed to load font '%s', exiting.", TextFont);
      exit(-1);
    }

  pango_context_set_font_description(pgo_context, fontdes);

  if ((font = pango_font_map_load_font (pgo_fontmap, 
				       pgo_context, fontdes)) == NULL)
    {
      fprintf(stderr, "Failed to load font '%s', exiting.", TextFont);
      exit(-1);
    }

  metrics = pango_font_get_metrics(font, NULL);
  xftdraw = XftDrawCreate(dpy, gdk_x11_drawable_get_xid(window->window),DefaultVisual(dpy, scr),
			  DefaultColormap(dpy, scr));
  
 finish_clock = GetTimeInMillis();

  if (Verbose)
    {
      printf("test-pango-gdk: Font loaded, drawable + color created in %lli ms\n",
	     finish_clock - start_clock);
    }

  start_clock = GetTimeInMillis(); 
  
  for (j=0; j<TotalCycles; j++)
    {
      y = PANGO_PIXELS(pango_font_metrics_get_ascent(metrics));
      
      for (i=0; i<TextNLines; i++)
	{
	  unsigned char *str = NULL;
	  GList *items_head = NULL, *items = NULL;
	  PangoAttrList *attr_list = NULL;

	  /* XXX We dont do markup ? 
	     GError *error;

	     pango_parse_markup (text, strlen(text), 
	     0,
	     &attr_list,
	     (char **)&str,
	     NULL,
	     &error);
	  */

	  x = 0;

	  attr_list = pango_attr_list_new (); /* no markup - empty attributes */
	  str       = strdup(TextStr);

	  /* analyse string, breaking up into items */
	  items_head = items = pango_itemize (pgo_context, str, 
					      0, strlen(TextStr),
					      attr_list, NULL);

	  while (items)
	    {
	      PangoItem        *this   = (PangoItem *)items->data;
	      PangoGlyphString *glyphs = pango_glyph_string_new ();
	      PangoRectangle    rect;
       
	      /* shape current item into run of glyphs */
	      pango_shape  (&str[this->offset], this->length, 
			    &this->analysis, glyphs);

	      /* render the glyphs */
	      pango_xft_render (xftdraw, 
				&xftcol,
				this->analysis.font,
				glyphs,
				x, y);

	      /* calculate rendered area */
	      pango_glyph_string_extents (glyphs,
					  this->analysis.font,
					  &rect,
					  NULL);

	      x += ( rect.x + rect.width ) / PANGO_SCALE;
       
	      pango_item_free (this);
	      pango_glyph_string_free (glyphs);

	      items = items->next;
	    }

	  if (attr_list)  pango_attr_list_unref (attr_list);
	  if (str)        free(str); 
	  if (items_head) g_list_free (items_head);

	  y += ( PANGO_PIXELS(pango_font_metrics_get_ascent(metrics)) + PANGO_PIXELS(pango_font_metrics_get_descent(metrics) ));
	}

      XSync(dpy, False);
    }

  /* render fonts here */

  finish_clock = GetTimeInMillis();

  diff_clock  = finish_clock - start_clock;

  nchars = strlen(TextStr) * TextNLines * TotalCycles;

  printf("test-pango-gdk: Total time %lli ms, %i glyphs rendered = approx %lli glyphs per second\n",
	 diff_clock, nchars, ( 1000 * nchars ) / diff_clock);

}

static void
usage(void)
{
  fprintf(stderr, 
	  "test-pango " VERSION "\n"
	  "usage: test-pango [options..]\n"
          "Options are;\n"
          "-display <X display>\n"
	  "--verbose\n"
	  "--text-str <str> text to render ( defaults to alphabet )\n"
	  "--font <str> Xft font to use ( defaults to " DEFAULT_FONT ")\n"
	  "--nlines <int> Number of lines to draw per cycle\n"
	  "--cycles <int>  number of times to runs the test ( default 100)\n"
	  );


  exit(1);
} 

int 
main (int argc, char **argv)
{
  int i;

  gtk_init (&argc, &argv);

  for (i = 1; i < argc; i++) {

    if (!strcmp ("--verbose", argv[i]) || !strcmp ("-v", argv[i])) {
      Verbose = True;
      continue;
    }

    if (!strcmp ("--text-str", argv[i]) ) {
      if (++i>=argc) usage ();
      TextStr = argv[i];
      continue;
    }

    if (!strcmp ("--font", argv[i]) ) {
      if (++i>=argc) usage ();
      TextFont = argv[i];
      continue;
    }

    if (!strcmp ("--nlines", argv[i]) ) {
      if (++i>=argc) usage ();
      TextNLines = atoi(argv[i]);
      if (TextNLines < 1) usage();
      continue;
    }

    if (!strcmp ("--cycles", argv[i]))  {
      if (++i>=argc) usage ();
      TotalCycles = atoi(argv[i]);
      if (TotalCycles < 1) usage();
      continue;
    }

    usage();
  }

  x_open();

  x_blit();

  return 0;
}