summaryrefslogtreecommitdiffstats
path: root/tests/test-freetype.c
blob: df9e17bd14baa404ea7561c07191327c7e6eef9c (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
/*
  test-fb.c -- Measure fullscreen framebuffer write speed. 
  Version 0.5

  Copyright (C) 2003 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 `freetype-config --libs --cflags` test-freetype.c -o test-freetype

*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <linux/fb.h>
#include <linux/kd.h>
#include <linux/vt.h>

#include <ft2build.h>
#include FT_FREETYPE_H
#include "freetype/ftsynth.h"

#define VERSION "0.1"

#define DEFAULT_TEXT_STR "abcdefghijklmnopqrstuvwxyz"
#define DEFAULT_FONT    "/usr/share/fonts/truetype/ttf-bitstream-vera/Vera.ttf"
#define DEFAULT_SIZE     20
#define DEFAULT_N_LINES  20
#define DEFAULT_N_CYCLES 100

static char *TextStr     = DEFAULT_TEXT_STR;
static char *TextFont    = DEFAULT_FONT;
static int   TextSize    = DEFAULT_SIZE;
static int   TextNLines  = DEFAULT_N_LINES;

static int con_fd = -1, fb_fd, last_vt = -1;
static struct fb_fix_screeninfo fix;
static struct fb_var_screeninfo vinfo;
static struct fb_cmap cmap;
static char  *fbuffer;
static int    fb_fd=0;
static int    xres, yres;
static int    Verbose = 0;
static int    TotalCycles = 100;

static char *defaultfbdevice = "/dev/fb0";
static char *defaultconsoledevice = "none";

static FT_Library FreetypeLibrary;

static int   WantMultiBlit = 0;

static int 
framebuffer_open (char *fbdevice,
		  char *consoledevice);
static void
framebuffer_blit(void);

static void 
framebuffer_close(void);

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 int 
framebuffer_open (char *fbdevice,
		  char *consoledevice)
{
  struct vt_stat vts;
  char vtname[128];
  int fd, nr;
  unsigned short col[2];
  int error;
  
  if (fbdevice == NULL) 
    fbdevice = defaultfbdevice;
  
  if (consoledevice == NULL) 
    consoledevice = defaultconsoledevice;
  
  if (strcmp(consoledevice,"none") != 0) 
    {
      sprintf(vtname,"%s%d", consoledevice, 1);
      fd = open(vtname, O_WRONLY);

      if (fd < 0) 
	{
	  perror("open consoledevice");
	  return -1;
	}
 
      /* Now get next available terminal */
      if (ioctl(fd, VT_OPENQRY, &nr) < 0) 
	{
	  perror("ioctl VT_OPENQRY");
	  return -1;
	}
      close(fd);  /* Close initial */
    
      /* Now open next available */
      sprintf(vtname, "%s%d", consoledevice, nr);
    
      con_fd = open(vtname, O_RDWR | O_NDELAY);
      if (con_fd < 0) 
	{
	  perror("open tty");
	  return -1;
	}
    
      /* Get info about it */
      if (ioctl(con_fd, VT_GETSTATE, &vts) == 0)
	last_vt = vts.v_active;
    
      /* Activate it */
      if (ioctl(con_fd, VT_ACTIVATE, nr) < 0) 
	{
	  perror("VT_ACTIVATE");
	  close(con_fd);
	  return -1;
	}
    
      /* Wait for it to activate */
      if (ioctl(con_fd, VT_WAITACTIVE, nr) < 0) 
	{
	  perror("VT_WAITACTIVE");
	  close(con_fd);
	  return -1;
	}
    
      /* Set the graphics mode */
      if (ioctl(con_fd, KDSETMODE, KD_GRAPHICS) < 0) 
	{
	  perror("KDSETMODE");
	  close(con_fd);
	  return -1;
	}
    }
  
  /* Now open the framebuffer */
  fb_fd = open(fbdevice, O_RDWR);
  if (fb_fd == -1) 
    {
      perror("open fbdevice");
      return -1;
    }

  /* Get the framebuffer info */
  if (ioctl(fb_fd, FBIOGET_FSCREENINFO, &fix) < 0) 
    {
      perror("ioctl FBIOGET_FSCREENINFO");
      close(fb_fd);
      return -1;
    }
  
  if (ioctl(fb_fd, FBIOGET_VSCREENINFO, &vinfo) < 0) 
    {
      perror("ioctl FBIOGET_VSCREENINFO");
      close(fb_fd);
      return -1;
    }

  xres = vinfo.xres;
  yres = vinfo.yres;
  
  cmap.start = 0;
  cmap.len = 2;
  cmap.red = col;
  cmap.green = col;
  cmap.blue = col;
  cmap.transp = NULL;
  
  col[0] = 0;
  col[1] = 0xffff;
  
  /*
  if(var.bits_per_pixel==8) 
    {
      if (ioctl(fb_fd, FBIOPUTCMAP, &cmap) < 0) 
	{
	  perror("ioctl FBIOPUTCMAP");
	  close(fb_fd);
	  return -1;
	}
    }
  */

  if(vinfo.bits_per_pixel != 16)
    {
      perror("Framebuffer is not 16bpp.");
      close(fb_fd);
      return -1;
    }

  fbuffer = mmap(NULL, fix.smem_len, 
		 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, 
		 fb_fd, 0);

  if (fbuffer == (char *)-1) 
    {
      perror("mmap framebuffer");
      close(fb_fd);
      return -1;
    }

  memset(fbuffer,0,fix.smem_len);

  /* freetype */

  error = FT_Init_FreeType( &FreetypeLibrary ); 
  
  if ( error ) {
    printf("Error in init\n");
    exit(2);
  }

  
  return 0;
}

static void 
framebuffer_close(void)
{
  munmap(fbuffer, fix.smem_len);
  close(fb_fd);
  
  if(con_fd != -1) 
    {
    
      if (ioctl(con_fd, KDSETMODE, KD_TEXT) < 0)
	perror("KDSETMODE");
    
      if (last_vt >= 0)
	if (ioctl(con_fd, VT_ACTIVATE, last_vt))
	  perror("VT_ACTIVATE");
    
      close(con_fd);
    }
}

static void
framebuffer_blit(void)
{
  int x = 0, y = 200, location = 0, i, j, xx, yy;
  char  *data = NULL, *c;
  int    size = vinfo.yres * vinfo.xres * (vinfo.bits_per_pixel/8);
  unsigned long long start_clock,finish_clock,diff_clock;
  int     cycles = 0, error;
  FT_Face    face;

  data = malloc( size );

  start_clock = GetTimeInMillis(); 

  error = FT_New_Face( FreetypeLibrary, TextFont, 0, &face );

  if ( error == FT_Err_Unknown_File_Format ) {
    printf("Unknown font format\n");
    exit(-1);
  }

  if ( error == FT_Err_Cannot_Open_Stream ) {
    printf("Could not find/open font resource" );
    exit(-1);
  }

  if (error) {
    printf("Error %d occured, exiting\n", error);
  }

  FT_Set_Pixel_Sizes(face, 0, TextSize);

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

  start_clock = GetTimeInMillis(); 
  
  for (j=0; j<TotalCycles; j++)
    {
      y = 0;
      for (i=0; i<TextNLines; i++)
	{
	  c = &TextStr[0];

	  while (*c != '\0')
	    {
	      error = FT_Load_Glyph(face, (int)*c, FT_LOAD_DEFAULT);
	      if (error) {
		printf("WARNING: Glyph load error!\n");
	      }

	      if (face->glyph->format != FT_GLYPH_FORMAT_BITMAP) 
		{
		  error = FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL);
		  if (error) {
		    printf("WARNING: Glyph rendering error!\n");
		  }
		}

		for (yy=0; yy < face->glyph->bitmap.rows; yy++) 
		  for (xx=0; xx < face->glyph->bitmap.width; xx++) 
		    {
		      if (face->glyph->bitmap.buffer[yy*face->glyph->bitmap.width + xx] > 0)
			{
			  int r = 0xff, g = 0xff, b =0xff;
			  location = (x + xx + vinfo.xoffset) * (vinfo.bits_per_pixel/8) + ((y + yy + vinfo.yoffset) * fix.line_length);

			  *((unsigned short int*)(fbuffer + location)) = 0xff; // (r<<11 | g << 5 | b);
			  // printf("paint pixel %i %i,%i\n ", location, x+xx, y+yy);
			}
		    }
		
		c++;
		x += face->glyph->bitmap.width;
	    }

	  y += face->glyph->bitmap.rows;

	}
    }
  
  free(data);
}

static void
usage(void)
{
  fprintf(stderr, 
	  "test-fb " VERSION "\n"
	  "usage: test-fb [--verbose] [--multiblit] [--cycles <int>]\n");
  exit(1);
} 

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

  for (i = 1; i < argc; i++) {
    if (!strcmp ("--verbose", argv[i]) || !strcmp ("-v", argv[i])) {
      Verbose = 1;
      continue;
    }
    if (!strcmp ("--multiblit", argv[i])) {
      WantMultiBlit = 1;
      continue;
    }

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

    usage();
  }

  if (framebuffer_open(NULL, NULL) == -1)
    {
      perror("framebuffer_open failed.");
      exit(0);
    }


  framebuffer_blit();

  framebuffer_close();

  return 0;
}