-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathflshow.c
945 lines (814 loc) · 25.9 KB
/
flshow.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
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdbool.h>
#include <termios.h>
#include <unistd.h>
#include "kmods/FL2000/include/fl2000_ioctl.h"
#define FL2K_NAME "/dev/fl2000-0"
/*
* definitions
*/
#define IMAGE_ASPECT_RATIO_16_10 0
#define IMAGE_ASPECT_RATIO_4_3 1
#define IMAGE_ASPECT_RATIO_5_4 2
#define IMAGE_ASPECT_RATIO_16_9 3
#define MAX_FRAME_BUFFER_SIZE 1920*1080*3
#define NUM_FRAME_BUFFERS 2
/*
* data structures
*/
struct resolution {
uint32_t width;
uint32_t height;
};
/*
* global variables
*/
struct monitor_info monitor_info;
struct resolution resolution_list[64];
uint32_t num_resolution;
uint8_t frame_buffers[NUM_FRAME_BUFFERS][MAX_FRAME_BUFFER_SIZE];
uint32_t mem_type = 0;
struct test_alloc phy_frame_buffers[NUM_FRAME_BUFFERS];
/*
* implementation
*/
int kbhit(void)
{
struct termios oldt, newt;
int ch;
int oldf;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf);
if(ch != EOF) {
ungetc(ch, stdin);
return 1;
}
return 0;
}
/*--------------------------------------------
add valid resolution to resolution_list[]
--------------------------------------------*/
void add_resolution(uint32_t width, uint32_t height)
{
unsigned int i;
bool resolution_found;
/*
* check if this resolution is already there
*/
resolution_found = false;
for (i = 0; i < num_resolution; i++) {
struct resolution * this = &resolution_list[i];
if (this->width == width && this->height == height) {
resolution_found = true;
break;
}
}
if (!resolution_found) {
resolution_list[num_resolution].width = width;
resolution_list[num_resolution].height = height;
fprintf(stdout, "entry [%u] = (%u, %u) added\n",
num_resolution, width, height);
num_resolution++;
}
}
/*-----------------------------------------------
decide Width Aand Height accroding to monitor EDID
ratio
------------------------------------------------*/
void fl2000_monitor_ratio_to_dimension(
uint8_t x,
uint8_t aspect_ratio,
uint32_t* width,
uint32_t* height)
{
uint32_t temp_width;
uint32_t temp_height;
temp_width = (x + 31) * 8;
switch (aspect_ratio) {
case IMAGE_ASPECT_RATIO_16_10:
printf("monitor ratio 16:10 -----\n");
temp_height = (temp_width / 16) * 10;
break;
case IMAGE_ASPECT_RATIO_4_3:
printf("monitor ratio 4:3 -----\n");
temp_height = (temp_width / 4) * 3;
break;
case IMAGE_ASPECT_RATIO_5_4:
printf("monitor ratio 16:10 -----\n");
temp_height = (temp_width / 5)*4;
break;
case IMAGE_ASPECT_RATIO_16_9:
default:
printf("monitor ratio 16:9 -----\n");
temp_height = ( temp_width / 16 ) * 9;
break;
}
*width = temp_width;
*height = temp_height;
}
void parse_edid(void)
{
uint8_t i;
uint32_t width;
uint32_t height;
uint8_t * const monitor_edid = monitor_info.edid;
/*
* EDID offset 38 ~ 53. Standard timing information. Upto 8 2-bytes.
* Unused fields are filled with 01 01
*/
for (i = 38; i < 53; i+= 2) {
uint8_t x = monitor_edid[i];
uint8_t ratio = monitor_edid[i + 1] >> 6;
uint8_t freq = (monitor_edid[i + 1] & 0x3F) + 60;
printf("EDID freq=%d\n",freq);
//if (monitor_edid[i] == 1 && monitor_edid[i + 1] == 1)
// continue;//break;
fl2000_monitor_ratio_to_dimension(
x,
ratio,
&width,
&height);
fprintf(stdout, "found (%u, %u) @ %u fps\n",
width, height, freq);
add_resolution(width, height);
}
/*
* check detailed timing descriptor
*/
for (i = 54; i < 125; i+= 18) {
uint8_t * entry = &monitor_edid[i];
uint32_t h_active, v_active;
/*
* NOT detailed timing descriptor
*/
if (entry[0] == 0 && entry[1] == 0)
break;
h_active = (entry[4] >> 4) << 8 | entry[2];
v_active = (entry[7] >> 4) << 8 | entry[5];
fprintf(stdout, "found (%u, %u) detailed timing desc\n",
h_active, v_active);
add_resolution(h_active, v_active);
}
}
void init_frame_by_test_pattern(
uint8_t * frame_buffer,
uint32_t width,
uint32_t height)
{
uint32_t y;
uint32_t x;
int color = 0;
/*
* init frame buffer with 24 RGB format.
* draw color stripe with 10 lines of R, 10 lines of G, 10 lines of B,
* 10 lines of white...
*/
for (y = 0; y < height; y++) {
uint32_t const pitch = width * 3;
for (x = 0; x < width; x++) {
uint8_t* pixel = frame_buffer + y * pitch + x * 3;
switch (color) {
case 0:
pixel[0] = 0;
pixel[1] = 0;
pixel[2] = 0xFF;
break;
case 1:
pixel[0] = 0;
pixel[1] = 0xFF;
pixel[2] = 0;
break;
case 2:
pixel[0] = 0xFF;
pixel[1] = 0;
pixel[2] = 0;
break;
default:
pixel[0] = 0xFF;
pixel[1] = 0xFF;
pixel[2] = 0xFF;
break;
}
}
if (y % 10 == 9)
color = (color + 1) % 4;
}
}
/*
* return true if a test_%u_%u.bmp exist, and is 24bpp format.
*/
bool init_frame_by_bmp_file(
uint8_t * frame_buffer,
uint32_t width,
uint32_t height,
uint32_t index,
//+++++ add uint8_t color_format in bool init_frame_by_bmp_file()
uint8_t color_format)
{
uint32_t const frame_size = width * height * 3;
char file_name[128];
uint8_t header[54];
size_t len;
bool file_ok = false;
FILE * bmp_file;
uint32_t bmp_width;
uint32_t bmp_height;
uint32_t bmp_bpp;
uint32_t i;
uint32_t nread;
//++++++ add rgb_tmp iniinit_frame_by_bmp_file()
uint8_t rgb_tmp[3];
uint32_t j,k,m;
uint8_t* rgb_line;
memset(file_name, 0, sizeof(file_name));
snprintf(file_name, sizeof(file_name), "test_%d_%d_%d.bmp",
width, height, index);
printf("-----------------open file: %s -------------------\n",file_name);
bmp_file = fopen(file_name, "r");
if (bmp_file == NULL) {
//fprintf(stderr, "%s not exists\n", file_name);
goto exit;
}
/*
* read the header
*/
len = fread(header, 1, 54, bmp_file);
if (len != 54) {
fprintf(stderr, "%s len(%d) != 54 ?\n",
file_name, (int) len);
goto exit;
}
/*
* check header
*/
if (header[0] != 'B' || header[1] != 'M' || header[10] != 0x36 ||
header[14] != 0x28) {
fprintf(stderr, "invalid header\n");
goto exit;
}
bmp_width = header[20] << 16 | header[19] << 8 | header[18];
bmp_height = header[24] << 16 | header[23] << 8 | header[22];
printf("bmp_height=%d, bmp_width=%d \n",bmp_height,bmp_width);
if (bmp_width != width || bmp_height != height) {
fprintf(stderr, "bmp width/height (%u,%u) mismatch!\n",
bmp_width, bmp_height);
goto exit;
}
bmp_bpp = header[28];
if (bmp_bpp != 24) {
fprintf(stderr, "bmp_bpp(%d) not 24?\n", bmp_bpp);
goto exit;
}
/*
* read the frame buffer from offset 54, in reverse order.
*/
printf(" read the frame buffer from offset 54, in reverse order....\n");
switch(color_format){
case COLOR_FORMAT_RGB_24:
// RGB_24 : Each pixel is comprised of 3 bytes and B occurs on byte[0], and
// G occurs on byte[1], and R occurs on byte[2]. This is the default format.
for (i = 0; i < height; i++) {
uint8_t * frame_offset;
frame_offset = frame_buffer + (height - i - 1) * width * 3;
nread=fread(frame_offset, 1, width * 3, bmp_file);
//printf("i=%d, nread=%d\n",i,nread);
}
break;
case COLOR_FORMAT_RGB_16_565:
//----- read 24BIT RGB data from bmp_file and covert to 16_565 FORMAT for frame_buffer
/* RGB_16_565: Each pixel is comprised by 16 bits in little endian order. The
* byte[0] is the least significant byte. Color B occurs on bits[0~4] of
* byte[0], Color G occurs on bits[5~7] of byte[0] and bits[0~2] of byte[1],
* Color R occurs on bits[3~7] of byte[1]. This color format is not encourged
* since it gives worse user experience.
*/
rgb_line=malloc(width*3);
for (i = 0; i < height; i++) {
nread=fread(rgb_line,1,width*3,bmp_file);
//--for every line, covert to RBG_565 FORMAT in reverse order......!!!!!
for( j=(height-i-1)*width;j<(height-i)*width;j++){ //--- see j as pixel No.
m=j%width;
//---RBG R in high position
frame_buffer[2*j] = ( ( (rgb_line[3*m+1]<<3) & 0xE0 ) | rgb_line[3*m]>>3 );
frame_buffer[2*j+1] =( ( rgb_line[3*m+2] & 0xF8 ) | ( rgb_line[3*m+1]>>5 ) );
}
}
free(rgb_line);
break;
case COLOR_FORMAT_RGB_16_555:
//----- read 24BIT RGB data from bmp_file and covert to 16_555 FORMAT for frame_buffer
/* RGB_16_555: Each pixel is comprised by 16 bits in little endian order. The
* byte[0] is the least significant byte. Color B occurs on bits[0~4] of
* byte[0], Color G occurs on bits[5~7] of byte[0] and bits[0~1] of byte[1],
* Color R occurs on bits[2~6] of byte[1]. The most signicant bit of byte[1]
* is not used. This color format is not encourged since it delivers worse
* user experience.
*/
rgb_line=malloc(width*3);
for (i = 0; i < height; i++) {
nread=fread(rgb_line,1,width*3,bmp_file);
//--for every line, covert to RBG_565 FORMAT in reverse order......!!!!!
for( j=(height-i-1)*width;j<(height-i)*width;j++){ //--- see j as pixel No.
m=j%width;
frame_buffer[2*j] = ( ( (rgb_line[3*m+1]<<2) & 0xE0 ) | rgb_line[3*m]>>3 );
frame_buffer[2*j+1] =( ( (rgb_line[3*m+2]>>1) & 0x7C ) | ( rgb_line[3*m+1]>>6 ) );
}
}
free(rgb_line);
break;
default:
printf("color format error!\n");
goto exit;
}
file_ok = true;
exit:
if (bmp_file != NULL)
fclose(bmp_file);
return file_ok;
}
int _alloc_frame(int fd, struct test_alloc * phy_alloc, uint32_t size)
{
int ret;
memset(phy_alloc, 0, sizeof(*phy_alloc));
phy_alloc->buffer_size = (uint64_t) size;
ret = ioctl(fd, IOCTL_FL2000_TEST_ALLOC_SURFACE, phy_alloc);
if (ret < 0)
fprintf(stderr,
"%s: IOCTL_FL2000_TEST_ALLOC_SURFACE failed %d\n",
__func__, ret);
return ret;
}
void _release_frame(int fd, struct test_alloc * phy_alloc)
{
int ret;
printf(" start _release_frame ....\n");
ret = ioctl(fd, IOCTL_FL2000_TEST_RELEASE_SURFACE, phy_alloc);
if (ret < 0)
fprintf(stderr,
"%s: IOCTL_FL2000_TEST_RELEASE_SURFACE failed %d\n",
__func__, ret);
}
/*--------------------------------------------------------
test with bmp file with name width_height_index.bmp
----------------------------------------------------------*/
void test_display(int fd, uint32_t width, uint32_t height, uint32_t index)
{
struct display_mode display_mode;
struct surface_info surface_info;
struct surface_update_info update_info;
uint8_t * frame_buffer;
int ret_val;
int i;
/*
* create surface
*/
memset(&surface_info, 0, sizeof(surface_info));
switch (mem_type) {
case SURFACE_TYPE_VIRTUAL_FRAGMENTED_VOLATILE:
case SURFACE_TYPE_VIRTUAL_FRAGMENTED_PERSISTENT:
frame_buffer = frame_buffers[0];
surface_info.handle = (unsigned long) frame_buffer;
surface_info.user_buffer = (unsigned long) frame_buffer;
printf("+++++---- surface_info.user_buffer = 0x%lx ----------\n",(unsigned long) surface_info.user_buffer);
break;
case SURFACE_TYPE_VIRTUAL_CONTIGUOUS:
ret_val = _alloc_frame(fd, &phy_frame_buffers[0],
//+++++ change to width * height * 2 if RGB_16
width * height * 3); //3);
if (ret_val < 0)
goto exit;
fprintf(stderr,
"usr_addr(0x%lx), phy_addr(0x%lx) allocated\n",
(unsigned long) phy_frame_buffers[0].usr_addr,
(unsigned long) phy_frame_buffers[0].phy_addr);
frame_buffer = (uint8_t*) (unsigned long) phy_frame_buffers[0].usr_addr;
surface_info.handle = phy_frame_buffers[0].usr_addr;
surface_info.user_buffer = phy_frame_buffers[0].usr_addr;
printf("+++++---- surface_info.user_buffer = 0x%lx ----------\n",(unsigned long) surface_info.user_buffer);
break;
case SURFACE_TYPE_PHYSICAL_CONTIGUOUS:
ret_val = _alloc_frame(fd, &phy_frame_buffers[0],
//+++++ change to width * height * 2 if RGB_16
width * height * 3); //3);
if (ret_val < 0)
goto exit;
fprintf(stderr,
"usr_addr(0x%lx), phy_addr(0x%lx) allocated\n",
(unsigned long) phy_frame_buffers[0].usr_addr,
(unsigned long) phy_frame_buffers[0].phy_addr);
frame_buffer = (uint8_t*) (unsigned long) phy_frame_buffers[0].usr_addr;
surface_info.handle = phy_frame_buffers[0].usr_addr;
surface_info.user_buffer = phy_frame_buffers[0].phy_addr;
printf("+++++---- surface_info.handle = phy_frame_buffers[0].usr_addr = 0x%lx ----------\n",(unsigned long) surface_info.handle);
printf("+++++---- surface_info.user_buffer = phy_frame_buffers[0].phy_addr = 0x%lx ----------\n",(unsigned long) surface_info.user_buffer);
break;
}
//======================= here may add a loop for read bmp files and notify surface update ======================
//+++++ Move following codes to just after IOCTL_FL2000_SET_DISPLAY_MODE.
//first you have to memset() to force real mem. allocation
memset(frame_buffer, 0, width*height*2);
/*
printf("start init_frame_by bmp_file OR test_pattern.....\n");
if (!init_frame_by_bmp_file(frame_buffer, width, height, index))
{
printf("!init_frame_by_bmp_file(),try to init_frame_by_test_pattern()....\n");
init_frame_by_test_pattern(frame_buffer, width, height);
}
*/
///----------------------------------------------------------------------///
//------surface_info.buffer_length: *2 for RGB_16 and *3 for RGB_24
// !!!!!!!!!!! surface_info.buffer_length MUST ACCORD WITH size of _alloc_frame() above !!!!!
// or
surface_info.buffer_length = width * height * 2; //2 for RGB_16 and 3 for RGB_24
surface_info.width = width;
surface_info.height = height;
//---- change pitch and color_format
// !!!! surface_info.pitch and color_format will be used to create surface and pin down pages
surface_info.pitch = width * 2; //-----2 for RGB_16 and 3 for RGB_24,-------- width*(bytes per pixle)
surface_info.color_format = COLOR_FORMAT_RGB_16_555;//565;//COLOR_FORMAT_RGB_24;
surface_info.type = mem_type;
fprintf(stderr, "create_surface(%u, %u) , type(0x%x)\n",
width, height, surface_info.type);
printf("start create surface....\n");
ret_val = ioctl(fd, IOCTL_FL2000_CREATE_SURFACE, &surface_info);
if (ret_val < 0) {
fprintf(stderr, "IOCTL_FL2000_CREATE_SURFACE failed %d\n",
ret_val);
goto exit;
}
/*
* set display mode
*/
memset(&display_mode, 0, sizeof(display_mode));
display_mode.width = width;
display_mode.height = height;
display_mode.refresh_rate = 60;//60 666666666666
// input_color_format will be used to get vr_params.input_bytes_per_pixel = 2;
//and input_bytes_per_pixel will finally be used in fl2000_compression.c
display_mode.input_color_format = COLOR_FORMAT_RGB_16_555;//565;//24;
//+++++ this setting must comply with fl2000_dongle.c
//output_color COLOR_FORMAT_ will be used to search corresponding VGA_BIG_TABLE_ in fl2000_dongle_set_params() of fl2000_dongle.c
//output_color_format will finally be used to fl2000_reg_bit_set() in fl2000_monitor.c
display_mode.output_color_format =COLOR_FORMAT_RGB_16_555;//565;//24;//16_555;//16_565;//COLOR_FORMAT_RGB_24;
ret_val = ioctl(fd, IOCTL_FL2000_SET_DISPLAY_MODE, &display_mode);
if (ret_val < 0) {
fprintf(stderr, "IOCTL_FL2000_SET_DISPLAY_MODE failed %d\n",
ret_val);
goto exit;
}
//+++++----- =========== move init_frame_by_bmp_file() call to just after IOCTL_FL2000_SET_DISPLAY_MODE.....
printf("start init_frame_by bmp_file OR test_pattern.....\n");
if (!init_frame_by_bmp_file(frame_buffer, width, height, index,display_mode.input_color_format))
{
printf("!init_frame_by_bmp_file(),try to init_frame_by_test_pattern()....\n");
init_frame_by_test_pattern(frame_buffer, width, height);
}
//----------------------------------//
/*
* send update to driver
*/
memset(&update_info, 0, sizeof(update_info));
update_info.handle = surface_info.handle;
update_info.user_buffer = surface_info.user_buffer;
//+++++ if NOT 24BIT format then update_info.buffer_length is not width * height*3bytes !!!!
if(display_mode.output_color_format == COLOR_FORMAT_RGB_16_565 || display_mode.output_color_format == COLOR_FORMAT_RGB_16_555){
//+++++----- adjust URB bufferlen, update_info.buffer_length = width * height*1
update_info.buffer_length = width * height*2;// * 2;
}
else{
update_info.buffer_length = width * height * 3;
}
//-----------------------------------------
fprintf(stdout, "IOCTL_FL2000_NOTIFY_SURFACE_UPDATE: display_mode(%u, %u)\n",width,height);
//+++++ start a for loop to update surface...
for(i=0;i<112500;i++)
{
ret_val = ioctl(fd, IOCTL_FL2000_NOTIFY_SURFACE_UPDATE, &update_info);
if (ret_val < 0) {
fprintf(stderr, "IOCTL_FL2000_NOTIFY_SURFACE_UPDATE failed %d\n",
ret_val);
goto exit;
}
printf("-----ioctl NOTIFY_SURFACE_UPDATE: i=%d usleep()....------\n",i);
}
//////////////////////////////
fprintf(stdout, "display_mode(%u, %u), press any key to continue\n",
width, height);
while (kbhit() == 0)
usleep(1000*100);
getchar();
/*
* disable output
*/
//+++++ Don't _SET_DISPLAY_MODE to 0 after render
printf("disable output-----\n");
//--set all param 0, as for shut down rending --
memset(&display_mode, 0, sizeof(display_mode));
ret_val = ioctl(fd, IOCTL_FL2000_SET_DISPLAY_MODE, &display_mode);
/*
* destroy surface
*/
printf("destroy surface -----\n");
ret_val = ioctl(fd, IOCTL_FL2000_DESTROY_SURFACE, &surface_info);
if (ret_val < 0) {
fprintf(stderr, "IOCTL_FL2000_DESTROY_SURFACE failed %d\n",
ret_val);
goto exit;
}
exit:
switch (mem_type) {
case SURFACE_TYPE_VIRTUAL_CONTIGUOUS:
case SURFACE_TYPE_PHYSICAL_CONTIGUOUS:
if (phy_frame_buffers[0].usr_addr != 0)
_release_frame(fd, &phy_frame_buffers[0]);
break;
}
}
void test_display_all(int fd)
{
int i;
for (i = num_resolution - 1; i >= 0; i--) {
struct resolution * this = &resolution_list[i];
test_display(fd, this->width, this->height, 0);
}
}
bool fl2000_is_connected(void)
{
struct stat f_stat;
int ret_val;
ret_val = stat(FL2K_NAME, &f_stat);
if (ret_val == 0)
return true;
return false;
}
void test_display_on_resolution(int fd, uint32_t width, uint32_t height)
{
struct display_mode display_mode;
struct surface_info surface_info;
struct surface_update_info update_info;
uint8_t * frame_buffer;
int ret_val;
int index;
bool bmp_ok;
unsigned int num_bmp;
// find how many bmp files are available, look for at most
// 16 test_xxxx_yyyy_z.bmp
num_bmp = 0;
for (index = 0; index < NUM_FRAME_BUFFERS; index++) {
char file_name[128];
FILE * bmp_file;
memset(file_name, 0, sizeof(file_name));
snprintf(file_name, sizeof(file_name), "test_%d_%d_%d.bmp",
width, height, index);
bmp_file = fopen(file_name, "r");
if (bmp_file == NULL)
break;
fclose(bmp_file);
num_bmp++;
}
// set display mode
memset(&display_mode, 0, sizeof(display_mode));
display_mode.width = width;
display_mode.height = height;
display_mode.refresh_rate = 60;
display_mode.input_color_format = COLOR_FORMAT_RGB_24;
display_mode.output_color_format = COLOR_FORMAT_RGB_24;
ret_val = ioctl(fd, IOCTL_FL2000_SET_DISPLAY_MODE, &display_mode);
if (ret_val < 0) {
fprintf(stderr, "IOCTL_FL2000_SET_DISPLAY_MODE failed %d\n",
ret_val);
goto exit;
}
// create surfaces
for (index = 0; index < num_bmp; index++) {
memset(&surface_info, 0, sizeof(surface_info));
switch (mem_type) {
case SURFACE_TYPE_VIRTUAL_FRAGMENTED_VOLATILE:
case SURFACE_TYPE_VIRTUAL_FRAGMENTED_PERSISTENT:
frame_buffer = frame_buffers[index];
init_frame_by_bmp_file(frame_buffer, width, height, index,display_mode.input_color_format);
surface_info.handle = (unsigned long) frame_buffer;
surface_info.user_buffer = (unsigned long) frame_buffer;
break;
case SURFACE_TYPE_VIRTUAL_CONTIGUOUS:
ret_val = _alloc_frame(fd, &phy_frame_buffers[index],
width * height * 3);
if (ret_val < 0)
goto exit;
fprintf(stderr,
"usr_addr(0x%lx), phy_addr(0x%lx) allocated\n",
(unsigned long) phy_frame_buffers[index].usr_addr,
(unsigned long) phy_frame_buffers[index].phy_addr);
frame_buffer = (uint8_t*) (unsigned long) phy_frame_buffers[index].usr_addr;
init_frame_by_bmp_file(frame_buffer, width, height, index,display_mode.input_color_format);
surface_info.handle = phy_frame_buffers[index].usr_addr;
surface_info.user_buffer = phy_frame_buffers[index].usr_addr;
break;
case SURFACE_TYPE_PHYSICAL_CONTIGUOUS:
ret_val = _alloc_frame(fd, &phy_frame_buffers[index],
width * height * 3);
if (ret_val < 0)
goto exit;
fprintf(stderr,
"usr_addr(0x%lx), phy_addr(0x%lx) allocated\n",
(unsigned long) phy_frame_buffers[index].usr_addr,
(unsigned long) phy_frame_buffers[index].phy_addr);
frame_buffer = (uint8_t*) (unsigned long) phy_frame_buffers[index].usr_addr;
init_frame_by_bmp_file(frame_buffer, width, height, index,display_mode.input_color_format);
surface_info.handle = phy_frame_buffers[index].usr_addr;
surface_info.user_buffer = phy_frame_buffers[index].phy_addr;
break;
}
surface_info.buffer_length = width * height * 3;
surface_info.width = width;
surface_info.height = height;
surface_info.pitch = width * 3;
surface_info.color_format = COLOR_FORMAT_RGB_24;
surface_info.type = mem_type;
ret_val = ioctl(fd, IOCTL_FL2000_CREATE_SURFACE, &surface_info);
if (ret_val < 0) {
fprintf(stderr, "IOCTL_FL2000_CREATE_SURFACE failed %d\n",
ret_val);
goto exit;
}
}
// for each primary surfaces, send update to kernel driver.
index = 0;
do {
int c;
if (!fl2000_is_connected())
break;
frame_buffer = frame_buffers[index];
memset(&update_info, 0, sizeof(update_info));
switch (mem_type) {
case SURFACE_TYPE_VIRTUAL_FRAGMENTED_VOLATILE:
case SURFACE_TYPE_VIRTUAL_FRAGMENTED_PERSISTENT:
update_info.handle = (unsigned long) frame_buffer;
update_info.user_buffer = (unsigned long) frame_buffer;
break;
case SURFACE_TYPE_VIRTUAL_CONTIGUOUS:
update_info.handle = phy_frame_buffers[index].usr_addr;
update_info.user_buffer = phy_frame_buffers[index].usr_addr;
break;
case SURFACE_TYPE_PHYSICAL_CONTIGUOUS:
update_info.handle = phy_frame_buffers[index].usr_addr;
update_info.user_buffer = phy_frame_buffers[index].phy_addr;
break;
default:
fprintf(stderr, "unkown mem_type(%u)?\n", mem_type);
break;
}
update_info.buffer_length = width * height * 3;
ret_val = ioctl(fd, IOCTL_FL2000_NOTIFY_SURFACE_UPDATE, &update_info);
if (ret_val < 0) {
fprintf(stderr, "IOCTL_FL2000_NOTIFY_SURFACE_UPDATE failed %d\n",
ret_val);
goto exit;
}
if (++index >= num_bmp)
index = 0;
if (kbhit() == 0) {
usleep(1000*10); // sleep for 10 ms
continue;
}
c = getchar();
if (c == 27)
break;
} while (true);
// disable output
memset(&display_mode, 0, sizeof(display_mode));
ret_val = ioctl(fd, IOCTL_FL2000_SET_DISPLAY_MODE, &display_mode);
// destroy all surfaces
for (index = 0; index < num_bmp; index++) {
frame_buffer = frame_buffers[index];
memset(&surface_info, 0, sizeof(surface_info));
switch (mem_type) {
case SURFACE_TYPE_VIRTUAL_FRAGMENTED_VOLATILE:
case SURFACE_TYPE_VIRTUAL_FRAGMENTED_PERSISTENT:
surface_info.handle = (unsigned long) frame_buffer;
surface_info.user_buffer = (unsigned long) frame_buffer;
break;
case SURFACE_TYPE_VIRTUAL_CONTIGUOUS:
_release_frame(fd, &phy_frame_buffers[index]);
surface_info.handle = phy_frame_buffers[index].usr_addr;
surface_info.user_buffer = phy_frame_buffers[index].usr_addr;
break;
case SURFACE_TYPE_PHYSICAL_CONTIGUOUS:
_release_frame(fd, &phy_frame_buffers[index]);
surface_info.handle = phy_frame_buffers[index].usr_addr;
surface_info.user_buffer = phy_frame_buffers[index].phy_addr;
break;
}
surface_info.buffer_length = width * height * 3;
surface_info.width = width;
surface_info.height = height;
surface_info.pitch = width * 3;
surface_info.color_format = COLOR_FORMAT_RGB_24;
surface_info.type = mem_type;
ret_val = ioctl(fd, IOCTL_FL2000_DESTROY_SURFACE, &surface_info);
if (ret_val < 0) {
fprintf(stderr, "IOCTL_FL2000_DESTROY_SURFACE failed %d\n",
ret_val);
goto exit;
}
}
exit:;
}
void main(int argc, char* argv[])
{
int ch;
int fd;
int ioctl_ret;
if (geteuid() != 0) {
fprintf(stderr, "need root privilege. try sudo %s\n", argv[0]);
return;
}
fd = open(FL2K_NAME, O_RDWR);
if (fd == -1) {
fprintf(stderr, "fl2000 device not connected?\n");
return;
}
ioctl_ret = ioctl(fd, IOCTL_FL2000_QUERY_MONITOR_INFO, &monitor_info);
if (ioctl_ret < 0) {
fprintf(stderr, "IOCTL_FL2000_QUERY_MONITOR_INFO fails %d\n",
ioctl_ret);
goto exit;
}
if (monitor_info.monitor_flags.connected == 0) {
fprintf(stderr, "no monitor connected to FL2000?\n");
goto exit;
}
if (argc < 2) {
usage:
fprintf(stderr, "usage: %s mem_type [[width] [height]]\n"
"mem_type is 0..3\n", argv[0]);
fprintf(stderr,
"eg1: to test with SURFACE_TYPE_VIRTUAL_FRAGMENTED_VOLATILE, type\n"
"%s 0\n", argv[0]);
fprintf(stderr,
"eg2: to test without SURFACE_TYPE_VIRTUAL_FRAGMENTED_PERSISTENT, type\n"
"%s 1\n", argv[0]);
fprintf(stderr,
"eg3: to test 1920x1080 with SURFACE_TYPE_VIRTUAL_FRAGMENTED_PERSISTENT, type\n"
"%s 1 1920 1080\n", argv[0]);
goto exit;
}
if (argc >= 2) {
switch (argv[1][0]) {
case '0':
mem_type = SURFACE_TYPE_VIRTUAL_FRAGMENTED_VOLATILE;
break;
case '1':
mem_type = SURFACE_TYPE_VIRTUAL_FRAGMENTED_PERSISTENT;
break;
case '2':
mem_type = SURFACE_TYPE_VIRTUAL_CONTIGUOUS;
break;
case '3':
mem_type = SURFACE_TYPE_PHYSICAL_CONTIGUOUS;
break;
}
}
if (argc > 2 && argc < 4)
goto usage;
parse_edid();
if (argc >= 4) {
int width, height;
width = atoi(argv[2]);
height = atoi(argv[3]);
if (width * height * 3 > MAX_FRAME_BUFFER_SIZE) {
fprintf(stderr, "image (%d, %d) too large)\n",
width, height);
goto exit;
}
test_display_on_resolution(fd, width, height);
}
else
{
//test_display_all(fd);
printf("---test_display()-----\n");
//test_display(fd,400,300,0);
test_display(fd,640,480,2);
// test_display(fd,640,350,0);
// test_display(fd,720,400,0);
// test_display(fd,800,600,2);
//test_display(fd,1280,768,1);
}
exit:
close(fd);
}