-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframebuffer.cpp
457 lines (376 loc) · 10 KB
/
framebuffer.cpp
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
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include "parser.h"
#include "globals.h"
#include <fb_device.h>
#include "framebuffer.h"
const char circle[]={
9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
9,9,1,1,1,1,1,1,1,1,1,1,1,9,9,
9,9,1,1,1,1,1,1,1,1,1,1,1,9,9,
9,9,1,1,1,1,1,1,1,1,1,1,1,9,9,
9,9,1,1,1,1,1,1,1,1,1,1,1,9,9,
9,9,1,1,1,1,1,1,1,1,1,1,1,9,9,
9,9,1,1,1,1,1,1,1,1,1,1,1,9,9,
9,9,1,1,1,1,1,1,1,1,1,1,1,9,9,
9,9,1,1,1,1,1,1,1,1,1,1,1,9,9,
9,9,1,1,1,1,1,1,1,1,1,1,1,9,9,
9,9,1,1,1,1,1,1,1,1,1,1,1,9,9,
9,9,1,1,1,1,1,1,1,1,1,1,1,9,9,
9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
9,9,9,9,9,9,9,9,9,9,9,9,9,9,9
};
Cfb* Cfb::getInstance()
{
static Cfb* instance = NULL;
if(!instance)
instance = new Cfb();
return instance;
}
Cfb::Cfb()
{
cpars = CParser::getInstance();
memset(&lfb, 0, sizeof(lfb));
memset(&lbb, 0, sizeof(lbb));
}
Cfb::~Cfb()
{
Cleanup();
}
int Cfb::init()
{
FT_Error error;
fb = sx = ex = sy = ey = -1;
// open Framebuffer
fb = open(FB_DEVICE, O_RDWR);
// init framebuffer
if(ioctl(fb, FBIOGET_FSCREENINFO, &fix_screeninfo) == -1)
{
printf("[%s] <FBIOGET_FSCREENINFO failed>\n",BASENAME);
return(2);
}
if(ioctl(fb, FBIOGET_VSCREENINFO, &var_screeninfo) == -1)
{
printf("[%s] <FBIOGET_VSCREENINFO failed>\n",BASENAME);
return(2);
}
if(!(lfb = (unsigned char*)mmap(0, fix_screeninfo.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fb, 0)))
{
printf("[%s] <mapping of Framebuffer failed>\n",BASENAME);
return(2);
}
// init fontlibrary
if((error = FT_Init_FreeType(&library)))
{
printf("[%s] <FT_Init_FreeType failed with Errorcode 0x%.2X>",BASENAME, error);
munmap(lfb, fix_screeninfo.smem_len);
return(2);
}
if((error = FTC_Manager_New(library, 1, 2, 0, &MyFaceRequester, this, &manager)))
{
printf("[%s] <FTC_Manager_New failed with Errorcode 0x%.2X>\n",BASENAME, error);
FT_Done_FreeType(library);
munmap(lfb, fix_screeninfo.smem_len);
return(2);
}
if((error = FTC_SBitCache_New(manager, &cache)))
{
printf("[%s] <FTC_SBitCache_New failed with Errorcode 0x%.2X>\n",BASENAME, error);
FTC_Manager_Done(manager);
FT_Done_FreeType(library);
munmap(lfb, fix_screeninfo.smem_len);
return(2);
}
if((error = FTC_Manager_Lookup_Face(manager, (char *)FONT, &face)))
{
printf("[%s] <FTC_Manager_Lookup_Face failed with Errorcode 0x%.2X>\n",BASENAME, error);
FTC_Manager_Done(manager);
FT_Done_FreeType(library);
munmap(lfb, fix_screeninfo.smem_len);
return(2);
}
use_kerning = FT_HAS_KERNING(face);
desc.face_id = (char *)FONT;
desc.flags = FT_LOAD_MONOCHROME;
// init backbuffer
if(!(lbb = (unsigned char*)malloc(fix_screeninfo.line_length*var_screeninfo.yres)))
{
printf("[%s] <allocating of Backbuffer failed>\n",BASENAME);
FTC_Manager_Done(manager);
FT_Done_FreeType(library);
munmap(lfb, fix_screeninfo.smem_len);
return(2);
}
// volle Bildschirmauflösung reservieren (Parameter: buffer, color, size in bytes)
memset(lbb, 0, fix_screeninfo.line_length*var_screeninfo.yres);
printf("[%s] - init: FB %dx%dx%d stride %d\n",BASENAME, var_screeninfo.xres, var_screeninfo.yres, var_screeninfo.bits_per_pixel, fix_screeninfo.line_length);;
cpars->read_neutrino_osd_conf(&ex,&sx,&ey,&sy,NEUTRINOCONF);
if((ex == -1) || (sx == -1) || (ey == -1) || (sy == -1))
{
sx = 80;
ex = var_screeninfo.xres - 80;
sy = 80;
ey = var_screeninfo.yres - 80;
}
int mwidth = ex-sx;
int mheight = ey-sy;
//mwidth = 620;
startx = sx + (((ex-sx) - mwidth)/2);
starty = sy + (((ey-sy) - mheight)/2);
//vyres = var_screeninfo.yres;
return(0);
}
/******************************************************************************
* MyFaceRequester
******************************************************************************/
FT_Error Cfb::MyFaceRequester(FTC_FaceID face_id, FT_Library library, FT_Pointer request_data, FT_Face *aface)
{
FT_Error result;
result = FT_New_Face(library, (char *)face_id, 0, aface);
if(!result)
{
printf("[%s] - <Font \"%s\" loaded>\n",BASENAME, (char*)face_id);
}
else
{
printf("[%s] <Font \"%s\" failed>\n",BASENAME, (char*)face_id);
}
return result;
}
/******************************************************************************
* RenderChar
******************************************************************************/
int Cfb::RenderChar(FT_ULong currentchar, int sx, int sy, int ex, int color)
{
int row, pitch, bit, x = 0, y = 0;
FT_Error error;
FT_UInt glyphindex;
FT_Vector kerning;
FTC_Node anode;
//load char
if(!(glyphindex = FT_Get_Char_Index(face, currentchar)))
{
printf("[%s] <FT_Get_Char_Index for Char \"%#.2X\" failed: \"undefined character code\">\n",BASENAME, (int)currentchar);
return 0;
}
if((error = FTC_SBitCache_Lookup(cache, &desc, glyphindex, &sbit, &anode)))
{
printf("[%s] <FTC_SBitCache_Lookup for Char \"%#.2X\" failed with Errorcode 0x%.2X>\n",BASENAME, (int)currentchar, error);
return 0;
}
if(use_kerning)
{
FT_Get_Kerning(face, prev_glyphindex, glyphindex, ft_kerning_default, &kerning);
prev_glyphindex = glyphindex;
kerning.x >>= 6;
}
else
{
kerning.x = 0;
}
// render char
if(color != -1) /* don't render char, return charwidth only */
{
if(sx + sbit->xadvance >= ex)
{
return -1; /* limit to maxwidth */
}
for(row = 0; row < sbit->height; row++)
{
for(pitch = 0; pitch < sbit->pitch; pitch++)
{
for(bit = 7; bit >= 0; bit--)
{
if(pitch*8 + 7-bit >= sbit->width)
{
break; /* render needed bits only */
}
if((sbit->buffer[row * sbit->pitch + pitch]) & 1<<bit)
{
memcpy ( lbb + startx*4 + sx*4 + ( sbit->left + kerning.x + x ) *4 + fix_screeninfo.line_length* ( starty + sy - sbit->top + y ),cpars->bgra[color],4 );
}
x++;
}
}
x = 0;
y++;
}
}
// return charwidth
return sbit->xadvance + kerning.x;
}
/******************************************************************************
* GetStringLen
******************************************************************************/
int Cfb::GetStringLen(const char *string)
{
int stringlen = 0;
// reset kerning
prev_glyphindex = 0;
// calc len
while(*string != '\0')
{
stringlen += RenderChar(*string, -1, -1, -1, -1);
string++;
}
return stringlen;
}
/******************************************************************************
* RenderString
******************************************************************************/
void Cfb::RenderString(const char *string, int sx, int sy, int maxwidth, int layout, int size, int color)
{
int stringlen, ex, charwidth;
// set size
if(size == SMALL)
{
desc.width = desc.height = 26;
}
else if(size == NORMAL)
{
desc.width = desc.height = 32;
}
else
{
desc.width = desc.height = 40;
}
// set alignment
if(layout != LEFT)
{
stringlen = GetStringLen(string);
switch(layout)
{
case CENTER:
if(stringlen < maxwidth)
{
sx += (maxwidth - stringlen)/2;
}
break;
case RIGHT:
if(stringlen < maxwidth)
{
sx += maxwidth - stringlen;
}
}
}
// reset kerning
prev_glyphindex = 0;
// render string
ex = sx + maxwidth;
while(*string != '\0')
{
if((charwidth = RenderChar(*string, sx, sy, ex, color)) == -1)
{
return; /* string > maxwidth */
}
sx += charwidth;
string++;
}
}
/******************************************************************************
* RenderBox
******************************************************************************/
void Cfb::RenderBox(int sx, int sy, int ex, int ey, int mode, int color)
{
int loop;
if(mode == FILL)
{
for(; sy <= ey; sy++)
{
HorLine(sx, sy, ex-sx+1, color);
}
}
else
{
// hor lines
for(loop = sx; loop <= ex; loop++)
{
SetPixel(loop, sy , color);
SetPixel(loop, sy+1, color);
SetPixel(loop, ey-1, color);
SetPixel(loop, ey , color);
}
// ver lines
for(loop = sy; loop <= ey; loop++)
{
SetPixel(sx , loop, color);
SetPixel(sx+1, loop, color);
SetPixel(ex-1, loop, color);
SetPixel(ex , loop, color);
}
}
}
/******************************************************************************
* RenderCircle
******************************************************************************/
void Cfb::RenderCircle(int sx, int sy, int color)
{
int x, y;
for (y=0; y<15; y++)
for (x=0; x<15; x++)
if (circle[x+y*15])
{
if (circle[x+y*15] == 1)
SetPixel(sx + x, sy + y, color);
else
SetPixel(sx + x, sy + y, (int)circle[x+y*15]);
}
}
/******************************************************************************
* HorLine / SetPixel / PaintIcon
******************************************************************************/
//#define SetPixel(x, y, c) memcpy(lbb + ((startx + (x))<<2) + fix_screeninfo.line_length*(starty + (y)), bgra[c], 4)
void Cfb::HorLine(int x, int y, int l, int color)
{
for (l+=x; x<l; x++)
SetPixel(x, y, color);
}
void Cfb::SetPixel(int x, int y, int c)
{
memcpy(lbb + ((startx + (x))<<2) + fix_screeninfo.line_length*(starty + (y)), cpars->bgra[c], 4);
}
void Cfb::PaintIcon(unsigned char *icon, int sx, int sy)
{
struct rawHeader header;
uint16_t width, height;
int x, y;
memcpy(&header, icon, sizeof(struct rawHeader));
width = (header.width_hi << 8) | header.width_lo;
height = (header.height_hi << 8) | header.height_lo;
icon+=sizeof(struct rawHeader);
for (y=0; y<height; y++)
{
for (x=0; x<width; x+=2)
{
unsigned char pix;
pix = (*icon & 0xf0) >> 4;
if (pix != header.transp)
SetPixel(sx+x, sy+y, pix+1);
pix = (*icon++ & 0x0f);
if (pix != header.transp)
SetPixel(sx+x+1, sy+y, pix+1);
}
}
}
void Cfb::FBPaint(void)
{
memcpy(lfb, lbb, fix_screeninfo.line_length*var_screeninfo.yres);
}
void Cfb::FBClear(void)
{
RenderBox(0, 0, ex-sx, ey-sy, FILL, 0);
}
/******************************************************************************
* Cleanup
******************************************************************************/
void Cfb::Cleanup (void)
{
FTC_Manager_Done(manager);
FT_Done_FreeType(library);
free(lbb);
munmap(lfb, fix_screeninfo.smem_len);
close(fb);
}