-
Notifications
You must be signed in to change notification settings - Fork 11
/
egi_fbdev.c
1375 lines (1104 loc) · 38 KB
/
egi_fbdev.c
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*------------------------------------------------------------------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
Journal
2021-02-22:
1. init_fbdev(): Add zbuff.
2021-02-28:
1. Apply zbuff_on/pixz for FBDEV.
2. Add fb_reset_zbuff();
2021-03-17:
1. Add reinit_virt_fbdev().
2021-03-31:
1. Add init_virt_fbdev2(fb_dev, xres, yres, alpha, color).
2021-04-04:
1. Add member VFrameImg for virtual FBDEV
and accordingly revise following functions:
init_virt_fbdev(), init_virt_fbdev2(), release_virt_fbdev(), reinit_virt_fbdev()
int vfb_render().
2021-08-07:
1. Add fb_init_zbuff().
2021-08-12:
1. Add memeber FBDEV.flipZ
2021-11-11:
1. Add member FBDEV.zbuff_IgnoreEqual
2. init_virt_fbdev(): init FBDEV.zbuff_IgnoreEqual as FALSE.
2022-05-01:
1. Add virt_fbdev_updateImg()
2022-05-16:
1. Add fb_block_zbuff()
2023-04-24:
1. Add fb_gammaCorrect()
Midas Zhou
[email protected](Not in use since 2022_03_01)
-------------------------------------------------------------------*/
#include "egi.h"
#include "egi_timer.h"
#include "egi_fbdev.h"
#include "egi_fbgeom.h"
#include "egi_filo.h"
#include "egi_debug.h"
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <stdlib.h>
/* Global/SYS Framebuffer Device */
FBDEV gv_fb_dev={ .devname="/dev/fb0", .fbfd=-1, }; //__attribute__(( visibility ("hidden") )) ;
/*-------------------------------------
Initialize a FB device.
@fb_dev: Pointer to a static FBDEV.
Return:
0 OK
<0 Fails
-------------------------------------*/
int init_fbdev(FBDEV *fb_dev)
{
// int i;
// int bytes_per_pixel;
if(fb_dev->fbfd > 0) {
egi_dpstd("Input FBDEV already open!\n");
return -1;
}
if(fb_dev->devname==NULL) {
egi_dpstd("Input FBDEV name is invalid!\n");
return -1;
}
//fb_dev->fbfd=open(WEGI_FBDEV_NAME,O_RDWR|O_CLOEXEC);
fb_dev->fbfd=open(fb_dev->devname,O_RDWR|O_CLOEXEC);
if(fb_dev<0) {
egi_dperr("Open dev '%s'", fb_dev->devname);
return -1;
}
/* Get FBDEV Info */
egi_dpstd("Framebuffer device opened successfully.\n");
ioctl(fb_dev->fbfd,FBIOGET_FSCREENINFO,&(fb_dev->finfo));
ioctl(fb_dev->fbfd,FBIOGET_VSCREENINFO,&(fb_dev->vinfo));
//bytes_per_pixel=fb_dev->vinfo.bits_per_pixel>>3;
//fb_dev->screensize=fb_dev->vinfo.xres*fb_dev->vinfo.yres*(fb_dev->vinfo.bits_per_pixel>>3); /* >>3 /8 */
fb_dev->screensize=fb_dev->finfo.line_length*fb_dev->vinfo.yres; /* >>3 /8 */
/* Calloc zbuff */
fb_dev->zbuff=calloc(1, (fb_dev->vinfo.xres)*(fb_dev->vinfo.yres)*sizeof(typeof(*fb_dev->zbuff)));
if(fb_dev->zbuff==NULL) {
egi_dperr("Fail to calloc zbuff!");
close(fb_dev->fbfd); fb_dev->fbfd=-1;
return -2;
}
/* mmap FB */
fb_dev->map_fb=(unsigned char *)mmap(NULL,fb_dev->screensize,PROT_READ|PROT_WRITE, MAP_SHARED,
fb_dev->fbfd, 0);
if(fb_dev->map_fb==MAP_FAILED) {
egi_dperr("Fail to mmap map_fb!");
free(fb_dev->zbuff);
close(fb_dev->fbfd); fb_dev->fbfd=-1;
return -2;
}
/* mmap back mem, map_bk */
#if defined(ENABLE_BACK_BUFFER) || defined(LETS_NOTE)
fb_dev->map_buff=(unsigned char *)mmap(NULL,fb_dev->screensize*FBDEV_BUFFER_PAGES, PROT_READ|PROT_WRITE,
MAP_SHARED|MAP_ANONYMOUS, -1, 0);
if(fb_dev->map_buff==MAP_FAILED) {
egi_dperr("Fail to mmap map_buff!");
munmap(fb_dev->map_fb,fb_dev->screensize);
free(fb_dev->zbuff);
close(fb_dev->fbfd); fb_dev->fbfd=-1;
return -2;
}
fb_dev->npg=0; /* init current back buffer page number */
fb_dev->map_bk=fb_dev->map_buff; /* Point map_bk to map_buff */
#else /* -- DISABLE BACK BUFFER -- */
fb_dev->npg=0; /* not applicable */
fb_dev->map_bk=NULL; /* not applicable */
#endif
/* Init current back buffer page */
//fb_dev->npg=0;
//fb_dev->map_bk=fb_dev->map_buff;
/* Reset virtual FB, as EGI_IMGBUF */
fb_dev->virt_fb=NULL;
fb_dev->VFrameImg=NULL;
/* Reset pos_rotate */
fb_dev->pos_rotate=0;
fb_dev->pos_xres=fb_dev->vinfo.xres;
fb_dev->pos_yres=fb_dev->vinfo.yres;
/* Check: vinfo.xres NOT equals finfo.line_length/bytes_per_pixel */
if( fb_dev->finfo.line_length/(fb_dev->vinfo.bits_per_pixel>>3) != fb_dev->vinfo.xres )
{
fprintf(stderr,"\n\e[38;5;196;48;5;0m WARNING: vinfo.xres != finfo.line_length/bytes_per_pixel. \e[0m\n");
//egi_dperr("\n\e[38;5;196;48;5;0m WARNING: vinfo.xres != finfo.line_length/bytes_per_pixel. \e[0m");
}
/* Reset pixcolor and pixalpha */
fb_dev->pixcolor_on=false;
fb_dev->pixcolor=(30<<11)|(10<<5)|10;
fb_dev->pixalpha=255;
/* Init fb_filo */
fb_dev->filo_on=0;
fb_dev->fb_filo=egi_malloc_filo(1<<13, sizeof(FBPIX), FILO_AUTO_DOUBLE);//|FILO_AUTO_HALVE
if(fb_dev->fb_filo==NULL) {
egi_dpstd("Fail to malloc FB FILO!\n");
munmap(fb_dev->map_fb, fb_dev->screensize);
munmap(fb_dev->map_buff, fb_dev->screensize*FBDEV_BUFFER_PAGES);
free(fb_dev->zbuff);
close(fb_dev->fbfd); fb_dev->fbfd=-1;
return -3;
}
/* Assign gv_fb_box */
if(fb_dev==&gv_fb_dev) {
gv_fb_box.startxy.x=0;
gv_fb_box.startxy.y=0;
gv_fb_box.endxy.x=fb_dev->vinfo.xres-1;
gv_fb_box.endxy.y=fb_dev->vinfo.yres-1;
}
/* clear buffer */
// for(i=0; i<FBDEV_BUFFER_PAGES; i++) {
// fb_dev->buffer[i]=NULL;
// }
#if 1
// printf("init_dev successfully. fb_dev->map_fb=%p\n",fb_dev->map_fb);
printf("\e[38;5;34;48;5;0m");
printf("\n -------- FB Parameters --------\n");
printf(" bits_per_pixel: %d bits \n",fb_dev->vinfo.bits_per_pixel);
printf(" line_length: %d bytes\n",fb_dev->finfo.line_length);
printf(" xres: %d pixels, yres: %d pixels \n", fb_dev->vinfo.xres, fb_dev->vinfo.yres);
printf(" xoffset: %d, yoffset: %d \n", fb_dev->vinfo.xoffset, fb_dev->vinfo.yoffset);
printf(" screensize: %ld bytes\n", fb_dev->screensize);
printf(" Total buffer pages: %d\n", FBDEV_BUFFER_PAGES);
printf(" Zbuffer: ON\n");
printf(" ------------ EGi ------------\n\n");
printf("\e[0m\n");
#endif
return 0;
}
/*---------------------------------------------------
Release FB and free map
Virtual FBDEV: see release_virt_fbdev().
Note:
1. Input FBDEV is considered as statically allocated.
All members MUST be properly initialized and hold
valid memory space.
----------------------------------------------------*/
void release_fbdev(FBDEV *dev)
{
if( !dev || !dev->map_fb )
return;
if( dev->virt ) {
egi_dpstd("It's a virtual FBDEV! Call release_virt_fbdev() instead!\n");
return;
}
/* Free FILO, reset fb_filo to NULL inside */
egi_filo_free(&dev->fb_filo);
/* Unmap FB */
if( munmap(dev->map_fb, dev->screensize) != 0) {
egi_dperr("unmap dev->map_fb");
}
dev->map_fb=NULL;
/* Unmap FB back memory */
if( munmap(dev->map_buff,dev->screensize*FBDEV_BUFFER_PAGES) !=0 ) {
egi_dperr("unmap dev->map_buff");
}
dev->map_buff=NULL;
/* Free zbuff */
free(dev->zbuff);
dev->zbuff=NULL;
/* Close fbfd */
close(dev->fbfd);
dev->fbfd=-1;
}
/*----------- TODO: FB driver support ------------------------------------------
(Temporarily) Set screen Vinfo for FB HW, and update
fb_dev params accordingly.
NOTE:
1. This is NOT for soft po_rotate screen, it's for HW set screen params !!!
2. Not applicable for virtual FBDEV.
@old_vinfo: Old screen vinfo to be saved.
@new_vifno: new screen vinfo to be set.
Return:
0 OK
<0 Fails
---------------------------------------------------------------------------------*/
int fb_set_screenVinfo(FBDEV *fb_dev, struct fb_var_screeninfo *old_vinfo, const struct fb_var_screeninfo *new_vinfo)
{
if(fb_dev==NULL || fb_dev->map_buff==NULL)
return -1;
/* Set new params for HW FB */
/* Check: Driver FBIOPUT */
if(ioctl(fb_dev->fbfd,FBIOPUT_VSCREENINFO, new_vinfo)!=0) {
printf("%s: Fail to ioctl FBIOSET_VSCREENINFO\n",__func__);
return -2;
}
/* Save old vinfo */
if(old_vinfo!=NULL)
*old_vinfo=fb_dev->vinfo;
/* Read in new HW vinfo */
ioctl(fb_dev->fbfd,FBIOGET_VSCREENINFO,&(fb_dev->vinfo));
#if 1
printf("%s: New vinfo set as: xres=%d, yres=%d\n",__func__, fb_dev->vinfo.xres, fb_dev->vinfo.yres);
#endif
/* Reset fb_dev parmas */
//fb_dev->screensize=fb_dev->vinfo.xres*fb_dev->vinfo.yres*(fb_dev->vinfo.bits_per_pixel>>3); /* >>3 /8 */
fb_dev->screensize=fb_dev->vinfo.yres*fb_dev->finfo.line_length;
/* Note: pos_xres, pos_yres NOT updated! */
return 0;
}
/*----------- TODO: FB driver support --------------------------------------
(Temporarily) Set screen position via FB HW, and update
fb_dev params accordingly.
NOTE:
1. This is NOT for soft po_rotate screen, it's for HW set screen params !!!
2. Not applicable for virtual FBDEV.
@xres: xres of screen
@yres: yres of screen
@offx: X offset of screen image
@offy: Y offset of screen image
TODO: offx, offy NOT applicable yet!
Return:
0 OK
<0 Fails
--------------------------------------------------------------------------*/
int fb_set_screenPos(FBDEV *fb_dev, unsigned int xres, unsigned int yres)
// unsigned int xoff, unsigned yoff )
{
if(fb_dev==NULL || fb_dev->map_buff==NULL)
return -1;
struct fb_var_screeninfo new_vinfo=fb_dev->vinfo;
/* Set new params */
new_vinfo.xres=xres;
new_vinfo.yres=yres;
// new_vinfo.xoffset=xoff;
// new_vinfo.yoffset=yoff;
/* Set HW and update fb_dev */
if( fb_set_screenVinfo(fb_dev, NULL, &new_vinfo)!=0 )
return -2;
/* Note: pos_xres, pos_yres NOT updated! */
return 0;
}
/*--------------------------------------------------------------------
Initiate a virtual FB device with an EGI_IMGBUF
, which is only a ref. pointer and will NOT be freed
in release_virt_fbdev(), except explicitly set fb_dev->vimg_owner=true.
!!! WARNING !!! Caller clear fb_dev first.
@dev: Pointer to statically allocated FBDEV.
@fbimg: Pointer to an EGI_IMGBUF, as fb_dev->virt_fb.
MUST NOT be NULL!
@FramImg: Pointer to an EGI_IMGBUF, as fb_dev->VFrameImg.
MAY be NULL.
Return:
0 OK
<0 Fails
----------------------------------------------------------------------*/
int init_virt_fbdev(FBDEV *fb_dev, EGI_IMGBUF *fbimg, EGI_IMGBUF *FrameImg)
{
/* Check input data */
if( fbimg==NULL || fbimg->imgbuf==NULL || fbimg->width<=0 || fbimg->height<=0 ) {
egi_dpstd("Input fbimg is invalid!\n");
return -1;
}
/* Check FrameImg */
if( FrameImg ) {
if( FrameImg->imgbuf==NULL || FrameImg->width<=0 || FrameImg->height<=0 ) {
egi_dpstd("Input FrameImg is invalid!\n");
return -1;
}
/* Compare size with fbimg */
if( fbimg->width != FrameImg->width || fbimg->height != FrameImg->height ) {
egi_dpstd("Input FrameImg is invalid!\n");
return -1;
}
}
/* check alpha
* NULL is OK
*/
/* Set virt */
fb_dev->virt=true;
/* Disable FB parmas */
fb_dev->fbfd=-1;
fb_dev->map_fb=NULL;
fb_dev->map_buff=NULL;
fb_dev->map_bk=NULL;
fb_dev->fb_filo=NULL;
fb_dev->filo_on=false;
fb_dev->zbuff_on=false;
fb_dev->zbuff_IgnoreEqual=false;
/* Reset virtual FB, as EGI_IMGBUF */
fb_dev->virt_fb=fbimg;
fb_dev->VFrameImg=FrameImg;
/* XXX fb_dev->img_owner=false;
* NOTE: Since fb_dev is a static FBDEV, just let the caller set it before calling this function,
* see in init_virt_fbdev2(); thus it can print vimg_owner together with other paramters here.
*/
/* Reset pixcolor and pixalpha */
fb_dev->pixcolor_on=false;
fb_dev->pixcolor=(30<<11)|(10<<5)|10;
fb_dev->pixalpha=255;
/* Set params for virt FB */
fb_dev->vinfo.bits_per_pixel=16;
fb_dev->finfo.line_length=fbimg->width*2;
fb_dev->vinfo.xres=fbimg->width;
fb_dev->vinfo.yres=fbimg->height;
fb_dev->vinfo.xoffset=0;
fb_dev->vinfo.yoffset=0;
fb_dev->screensize=fbimg->height*fbimg->width;
/* Reset pos_rotate */
fb_dev->pos_rotate=0;
fb_dev->pos_xres=fb_dev->vinfo.xres;
fb_dev->pos_yres=fb_dev->vinfo.yres;
/* Clear buffer */
// for(i=0; i<FBDEV_BUFFER_PAGES; i++) {
// fb_dev->buffer[i]=NULL;
// }
#if 0
printf(" \n--- Virtual FB Parameters ---\n");
printf(" bits_per_pixel: %d bits \n", fb_dev->vinfo.bits_per_pixel);
printf(" line_length: %d bytes\n", fb_dev->finfo.line_length);
printf(" xres: %d pixels, yres: %d pixels \n", fb_dev->vinfo.xres, fb_dev->vinfo.yres);
printf(" xoffset: %d, yoffset: %d \n", fb_dev->vinfo.xoffset, fb_dev->vinfo.yoffset);
printf(" screensize: %ld bytes\n", fb_dev->screensize);
printf(" VFrameImg: %s\n", (fb_dev->VFrameImg!=NULL)?"Yes":"No");
printf(" Vimg_owner: %s\n", fb_dev->vimg_owner?"True":"False");
printf(" ----------------------------\n\n");
#endif
return 0;
}
/*---------------------------------------------------------------------
Update virt fbdev with NEW fbimg and FrameImg
ONLY update parameters related with fbimg and FrameImg!
@dev: Pointer to statically allocated FBDEV.
@fbimg: Pointer to an EGI_IMGBUF, as fb_dev->virt_fb.
MAY be NULL!
@FramImg: Pointer to an EGI_IMGBUF, as fb_dev->VFrameImg.
MAY be NULL.
Return:
0 OK
<0 Fails
----------------------------------------------------------------------*/
int virt_fbdev_updateImg(FBDEV *fb_dev, EGI_IMGBUF *fbimg, EGI_IMGBUF *FrameImg)
{
/* Check input data */
if(fbimg) {
if( fbimg==NULL || fbimg->imgbuf==NULL || fbimg->width<=0 || fbimg->height<=0 ) {
egi_dpstd("Input fbimg is invalid!\n");
return -1;
}
}
/* Check FrameImg */
if( FrameImg ) {
if( FrameImg->imgbuf==NULL || FrameImg->width<=0 || FrameImg->height<=0 ) {
egi_dpstd("Input FrameImg is invalid!\n");
return -1;
}
/* Compare size with fbimg */
if(fbimg) {
if( fbimg->width != FrameImg->width || fbimg->height != FrameImg->height ) {
egi_dpstd("Input FrameImg is invalid!\n");
return -1;
}
}
else {
if( fb_dev->virt_fb->width != FrameImg->width || fb_dev->virt_fb->height != FrameImg->height ) {
egi_dpstd("Input FrameImg invalid! NOT same size as virt_fb!\n");
return -1;
}
}
}
/* Reset virtual FB, as EGI_IMGBUF */
if(fbimg)
fb_dev->virt_fb=fbimg;
if(FrameImg)
fb_dev->VFrameImg=FrameImg;
/* XXX fb_dev->img_owner=false;
* NOTE: Since fb_dev is a static FBDEV, just let the caller set it before calling this function,
* see in init_virt_fbdev2(); thus it can print vimg_owner together with other paramters here.
*/
/* Set params for virt FB */
if(fbimg) {
fb_dev->finfo.line_length=fbimg->width*2;
fb_dev->vinfo.xres=fbimg->width;
fb_dev->vinfo.yres=fbimg->height;
fb_dev->screensize=fbimg->height*fbimg->width;
/* Reset pos_rotate */
//fb_dev->pos_rotate=0;
fb_dev->pos_xres=fb_dev->vinfo.xres;
fb_dev->pos_yres=fb_dev->vinfo.yres;
}
return 0;
}
/*--------------------------------------------------
Initiate a virtual FB device by creating EGI_IMGBUFs
as fb_dev->virt_fb, and fb_dev->VFrameImg.
@dev: Pointer to statically allocated FBDEV.
@xres: Width of virt_fb pointed IMGBUF.
@yres: Height of virt_fb pointed IMGBUF.
@alpha: Inital alpha value for virt_fb pointed IMGBUF
If <0, NO alpha.
@color: Initial color value for virt_fb pointed IMGBUF
Return:
0 OK
<0 Fails
---------------------------------------------------*/
int init_virt_fbdev2(FBDEV *fb_dev, int xres, int yres, int alpha, EGI_16BIT_COLOR color)
{
EGI_IMGBUF *fbimg=NULL;
EGI_IMGBUF *FrameImg=NULL;
/* Check input */
if( xres<=0 || yres<=0 )
return -1;
/* Limt Alpha */
if(alpha>255)
alpha=255;
/* Create fbimg */
if(alpha<0)
fbimg=egi_imgbuf_createWithoutAlpha( yres, xres, color);
else
fbimg=egi_imgbuf_create( yres, xres, (unsigned char)alpha, color);
if(fbimg==NULL) {
egi_dpstd("Fail to create fbimg!\n");
return -2;
}
/* Create FrameImg */
if(alpha<0)
FrameImg=egi_imgbuf_createWithoutAlpha( yres, xres, color);
else
FrameImg=egi_imgbuf_create( yres, xres, (unsigned char)alpha, color);
if(FrameImg==NULL) {
egi_dpstd("Fail to create FrameImg!\n");
egi_imgbuf_free2(&fbimg);
return -3;
}
/*** Set Ownership of virt_fb pointed IMGBUF
* NOTE: set it before init_virt_fbdev() to let print the information in init_virt_fbdev()! */
fb_dev->vimg_owner=true;
/* Init virt fbdev with fbimg and FrameImg */
if( init_virt_fbdev(fb_dev, fbimg, FrameImg)<0 ) {
egi_dpstd("Fail to init fbdev!\n");
egi_imgbuf_free2(&fbimg);
egi_imgbuf_free2(&FrameImg);
return -4;
}
return 0;
}
/*-----------------------------------------
Release a virtual FB
@dev: Pointer to statically allocated FBDEV.
------------------------------------------*/
void release_virt_fbdev(FBDEV *dev)
{
dev->virt=false;
if(dev->vimg_owner) {
egi_imgbuf_free2(&dev->virt_fb);
egi_imgbuf_free2(&dev->VFrameImg);
}
dev->virt_fb=NULL;
dev->VFrameImg=NULL;
}
/*--------------------------------------------------------
Re_initilize a virtual FBDEV. Just to upate the reference
pointer of fb_dev->virt_fb with eimg.
!!! CAUTION !!!
All previous parameters in fb_dev will all be reset!
@dev: Pointer to statically allocated FBDEV.
@eimg: Pointer to an EGI_IMGBUF.
Return:
0 OK
<0 Fails
--------------------------------------------------------*/
int reinit_virt_fbdev(FBDEV *fb_dev, EGI_IMGBUF *fbimg, EGI_IMGBUF *FrameImg)
{
/* If fb_dev is the Owner of virt_fb, then abort. */
if(fb_dev->vimg_owner) {
egi_dpstd("FBDEV owns the virt_fb IMGBUF! Can NOT reintialize it!\n");
return -1;
}
//release_virt_fbdev(fb_dev);
/* It's reenterable */
return init_virt_fbdev(fb_dev, fbimg, FrameImg);
}
/*-----------------------------------------------------------
Shift/switch fb_dev->map_bk to point to the indicated buffer
page, as for the current working buffer page.
@fb_dev: struct FBDEV to operate.
@numpg: The number/index of buffer page put into play.
[0 FBDEV_MAX_BUFFE-1]
-----------------------------------------------------------*/
inline void fb_shift_buffPage(FBDEV *fb_dev, unsigned int numpg)
{
if(fb_dev==NULL || fb_dev->map_buff==NULL)
return;
numpg=numpg%FBDEV_BUFFER_PAGES; /* Note: Modulo result is compiler depended */
fb_dev->map_bk=fb_dev->map_buff+fb_dev->screensize*numpg;
}
/*-----------------------------------------------------
Set/Reset DirectFB mode
@directFB: True, set map_bk to map_fb
False, set map_bk to map_buff
------------------------------------------------------*/
void fb_set_directFB(FBDEV *fb_dev, bool directFB)
{
if( fb_dev==NULL || fb_dev->map_bk==NULL || fb_dev->map_buff==NULL )
return;
if(directFB)
fb_dev->map_bk=fb_dev->map_fb;
else
fb_dev->map_bk=fb_dev->map_buff; /* Default as in init_fbdev() */
}
/*-------------------------------------------------
Get current FB mode.
Return:
-1: Invalid FB mode.
1: Direct FB mode.
1 to coincide with fb_set_directFB(,ture)
0: Buffered FB mode
---------------------------------------------------*/
int fb_get_FBmode(FBDEV *fb_dev)
{
if( fb_dev==NULL || fb_dev->map_bk==NULL || fb_dev->map_buff==NULL )
return -1;
if( fb_dev->map_bk==fb_dev->map_fb ) /* DirectFB mode */
return 0;
else /* fb_dev->map_bk==fb_dev->map_buff */ /* Non_DirectFB mode */
return 1;
}
/*-------------------------------------------
Prepare FB background and working buffer.
Init buffers with current FB mmap data.
-------------------------------------------*/
void fb_init_FBbuffers(FBDEV *fb_dev)
{
if( fb_dev==NULL || fb_dev->map_bk==NULL || fb_dev->map_buff==NULL )
return;
/* Prepare FB background buffer */
memcpy(fb_dev->map_buff+fb_dev->screensize, fb_dev->map_fb, fb_dev->screensize);
/* prepare FB working buffer */
memcpy(fb_dev->map_buff, fb_dev->map_buff+fb_dev->screensize, fb_dev->screensize);
}
/*---------------------------------------------------
Copy between buffer pages in a FB dev.
----------------------------------------------------*/
void fb_copy_FBbuffer(FBDEV *fb_dev,unsigned int from_numpg, unsigned int to_numpg)
{
if( fb_dev==NULL || fb_dev->map_bk==NULL || fb_dev->map_buff==NULL )
return;
if(from_numpg==to_numpg)
return;
from_numpg=from_numpg%FBDEV_BUFFER_PAGES; /* Note: Modulo result is compiler depended */
to_numpg=to_numpg%FBDEV_BUFFER_PAGES;
memcpy( fb_dev->map_buff+to_numpg*fb_dev->screensize,
fb_dev->map_buff+from_numpg*fb_dev->screensize,
fb_dev->screensize );
}
/*-----------------------------------------------------------
Clear FB back buffs by filling with given color
!!! --- OBSELET ---- !!!
To call following functions instead:
fb_clear_workBuff(), fb_clear_bkgBuff(), fb_clear_mapBuffer()
Also see: fb_clear_workBuff() for WEGI_16BIT_COLOR.
@fb_dev: struct FBDEV whose buffer to be cleared.
@color: Color used to fill the buffer, 16bit or 32bits.
Depends on fb_dev->vinfo.bits_per_pixel.
!!! Note: to make sure that type INT is 32bits !!!
------------------------------------------------------------*/
void fb_clear_backBuff(FBDEV *fb_dev, uint32_t color)
{
int i,j;
int bytes_per_pixel;
unsigned int pos;
unsigned int pixels; /* Total pixels in the buffer */
if( fb_dev==NULL || fb_dev->map_bk==NULL)
return;
bytes_per_pixel=fb_dev->vinfo.bits_per_pixel>>3;
pixels=fb_dev->vinfo.xres*fb_dev->vinfo.yres;
/* For 16bits RGB color pixel */
if(fb_dev->vinfo.bits_per_pixel==2*8) {
for(i=0; i<pixels; i++)
*(uint16_t *)(fb_dev->map_bk+(i<<1))=color;
}
/* For 32bits ARGB color pixel */
else if(fb_dev->vinfo.bits_per_pixel==4*8) {
#if 0
for(i=0; i<pixels; i++)
*(uint32_t *)(fb_dev->map_bk+(i<<2))=color;
#else
for(i=0; i < fb_dev->vinfo.yres; i++) {
for(j=0; j< fb_dev->vinfo.xres; j++) {
pos=i*fb_dev->finfo.line_length+j*bytes_per_pixel;
*(uint32_t *)(fb_dev->map_bk+pos)=color;
}
}
#endif
}
else
printf("%s: bits_per_pixel is neither 16 nor 32!\n",__func__);
//else --- NOT SUPPORT --
}
/*-------------------------------------------------------
Clear map_buff[numpg] with given color
For 16bit color only.
--------------------------------------------------------*/
void fb_clear_mapBuffer(FBDEV *dev, unsigned int numpg, EGI_16BIT_COLOR color)
{
int i;
unsigned int pixels; /* Total pixels in the buffer */
unsigned long buffpos;
if( dev==NULL || dev->map_bk==NULL || dev->map_buff==NULL )
return;
pixels=dev->screensize>>1; /* for 16bit color only */
numpg=numpg%FBDEV_BUFFER_PAGES; /* Note: Modulo result is compiler depended */
buffpos= dev->screensize*numpg;
for(i=0; i<pixels; i++)
*(uint16_t *)(dev->map_buff + buffpos + (i<<1))=color;
}
/*------------------------------------------------
Clear FB working buffer
-------------------------------------------------*/
void fb_clear_workBuff(FBDEV *fb_dev, EGI_16BIT_COLOR color)
{
fb_clear_mapBuffer(fb_dev, FBDEV_WORKING_BUFF, color);
fb_reset_zbuff(fb_dev);
}
/*------------------------------------------------
Clear FB background backbuffer
-------------------------------------------------*/
void fb_clear_bkgBuff(FBDEV *fb_dev, EGI_16BIT_COLOR color)
{
fb_clear_mapBuffer(fb_dev, FBDEV_BKG_BUFF, color);
fb_reset_zbuff(fb_dev);
}
/*---------------------------------
Reset zbuff values to zero.
---------------------------------*/
void fb_reset_zbuff(FBDEV *fb_dev)
{
unsigned int xres=fb_dev->vinfo.xres;
unsigned int yres=fb_dev->vinfo.yres;
if(fb_dev==NULL || fb_dev->zbuff==NULL)
return;
if(fb_dev->zbuff)
memset( fb_dev->zbuff, 0, xres*yres*sizeof(typeof(*(fb_dev->zbuff))) );
}
/*---------------------------------
Init. all zbuff[] to be z0.
---------------------------------*/
void fb_init_zbuff(FBDEV *fb_dev, int z0)
{
int i;
unsigned int xres=fb_dev->vinfo.xres;
unsigned int yres=fb_dev->vinfo.yres;
if(fb_dev==NULL || fb_dev->zbuff==NULL)
return;
for(i=0; i<xres*yres; i++)
fb_dev->zbuff[i]=z0;
}
/*-----------------------------------------------
Set pixZ value for a block area in fb_dev->zbuff].
@fb_dev: Pointer to FBDEV
@x0,y0: Start point of the block.
@w,h: Width and height of the block.
@pixz: Z value for all pixels in the block.
------------------------------------------------*/
void fb_block_zbuff(FBDEV *fb_dev, int x0, int y0, unsigned int w, unsigned int h, int pixz)
{
int i,j;
unsigned int xres=fb_dev->vinfo.xres;
unsigned int yres=fb_dev->vinfo.yres;
unsigned int xmax, ymax;
if(fb_dev==NULL || fb_dev->zbuff==NULL)
return;
if(x0 > (int)xres-1 ) return;
if(y0 > (int)yres-1 ) return;
/* Limit xymax */
xmax=x0+w;
if(xmax > xres) xmax=xres;
ymax=y0+h;
if(ymax > yres) ymax=yres;
/* Limit x0y0 */
if(x0<0) x0=0;
if(y0<0) y0=0;
/* Set Z value for the block. */
for(i=y0; i<ymax; i++) {
for(j=x0; j<xmax; j++) {
fb_dev->zbuff[i*xres+j]=pixz;
}
}
}
/*----------------------------------------------------
Refresh FB screen with FB back buffer map_buff[numpg]
-----------------------------------------------------*/
int fb_page_refresh(FBDEV *dev, unsigned int numpg)
{
if(dev==NULL)
return -1;
/* only for ENABLE_BACK_BUFFER */
if( dev->map_bk==NULL || dev->map_fb==NULL || dev->map_buff==NULL )
return -2;
numpg=numpg%FBDEV_BUFFER_PAGES; /* Note: Modulo result is compiler depended */
/* Try to synchronize with FB kernel VSYNC */
if( ioctl( dev->fbfd, FBIO_WAITFORVSYNC, 0) !=0 ) {
#ifdef LETS_NOTE
printf("Fail to ioctl FBIO_WAITFORVSYNC.\n");
} else { /* memcpy to FB, ignore VSYNC signal. */
#endif
switch(numpg) {
case 0:
memcpy(dev->map_fb, dev->map_buff, dev->screensize);
break;
case 1:
memcpy(dev->map_fb, dev->map_buff+dev->screensize, dev->screensize);
break;
case 2:
memcpy(dev->map_fb, dev->map_buff+(dev->screensize<<1), dev->screensize);
break;
default:
memcpy(dev->map_fb, dev->map_buff+dev->screensize*numpg, dev->screensize);
break;
}
}
return 0;
}
/*------------------------------------------------------------------------------------
Refresh FB screen lines with FB back buffer map_buff[numpg]
!!! NOTE: Soft Pos_roation has NO effect for this function. !!!
@startln: Starting Line index. [0 FBDEV.vinfo.yres-1]
@n: Number of lines to be refreshed.
-------------------------------------------------------------------------------------*/
void fb_lines_refresh(FBDEV *dev, unsigned int numpg, unsigned int startln, int n)
{
unsigned int xres;
unsigned int Bpp; /* byte per pixel */
unsigned int Bpl; /* bytes per line */
if(dev==NULL)
return;
if( dev->map_bk==NULL || dev->map_fb==NULL || dev->map_buff==NULL )
return;
if( n<=0 || startln > dev->vinfo.yres-1 )
return;
if( startln+n > dev->vinfo.yres ) /* startln+n-1 > dev->vinfo.yres-1 */
n=dev->vinfo.yres-startln;
if(n<=0)
return;
if(numpg>FBDEV_BUFFER_PAGES-1)
numpg=numpg%FBDEV_BUFFER_PAGES; /* Note: Modulo result is compiler depended */
xres=dev->vinfo.xres;
Bpp=dev->vinfo.bits_per_pixel>>3;
Bpl=Bpp*xres;
/* Try to synchronize with FB kernel VSYNC */
if( ioctl( dev->fbfd, FBIO_WAITFORVSYNC, 0) !=0 ) {
#ifdef LETS_NOTE
printf("Fail to ioctl FBIO_WAITFORVSYNC.\n");
} else { /* memcpy to FB, ignore VSYNC signal. */
#endif
switch(numpg) {
case 0:
memcpy(dev->map_fb+startln*Bpl, dev->map_buff+startln*Bpl, n*Bpl);
break;
case 1:
memcpy(dev->map_fb+startln*Bpl, dev->map_buff+dev->screensize+startln*Bpl, n*Bpl);
break;
case 2:
memcpy(dev->map_fb+startln*Bpl, dev->map_buff+(dev->screensize<<1)+startln*Bpl, n*Bpl);
break;
default:
memcpy(dev->map_fb+startln*Bpl, dev->map_buff+dev->screensize*numpg+startln*Bpl, n*Bpl);
break;
}
}
}
/*-----------------------------------------------------
Backup current page data in dev->map_fb
to dev->map_buff[index], which normally is
NOT current working back buff!
@dev: current FB device.
@buffNum: index of dev->map_buff[].
Retrun:
0 OK
<0 Fails
------------------------------------------------------*/
int fb_page_saveToBuff(FBDEV *dev, unsigned int buffNum)