forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo_driver.c
4243 lines (3725 loc) · 135 KB
/
video_driver.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
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2021 - Daniel De Matteis
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <math.h>
#include <string/stdstring.h>
#include <retro_math.h>
#include <retro_assert.h>
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
#include <gfx/video_frame.h>
#include "../config.def.h"
#include "video_driver.h"
#include "video_filter.h"
#include "video_display_server.h"
#include "gfx_animation.h"
#ifdef HAVE_GFX_WIDGETS
#include "gfx_widgets.h"
#endif
#ifdef HAVE_THREADS
#include "video_thread_wrapper.h"
#endif
#ifdef HAVE_MENU
#include "../menu/menu_driver.h"
#endif
#ifdef _WIN32
#include "common/win32_common.h"
#endif
#include "../audio/audio_driver.h"
#include "../frontend/frontend_driver.h"
#include "../record/record_driver.h"
#include "../ui/ui_companion_driver.h"
#include "../driver.h"
#include "../file_path_special.h"
#include "../list_special.h"
#include "../retroarch.h"
#include "../verbosity.h"
#define TIME_TO_FPS(last_time, new_time, frames) ((1000000.0f * (frames)) / ((new_time) - (last_time)))
typedef struct
{
struct string_list *list;
enum gfx_ctx_api api;
} gfx_api_gpu_map;
static gfx_api_gpu_map gpu_map[] = {
{ NULL, GFX_CTX_VULKAN_API },
{ NULL, GFX_CTX_DIRECT3D10_API },
{ NULL, GFX_CTX_DIRECT3D11_API },
{ NULL, GFX_CTX_DIRECT3D12_API }
};
static const video_display_server_t dispserv_null = {
NULL, /* init */
NULL, /* destroy */
NULL, /* set_window_opacity */
NULL, /* set_window_progress */
NULL, /* set_window_decorations */
NULL, /* set_resolution */
NULL, /* get_resolution_list */
NULL, /* get_output_options */
NULL, /* set_screen_orientation */
NULL, /* get_screen_orientation */
NULL, /* get_flags */
"null"
};
#ifdef HAVE_VULKAN
static const gfx_ctx_driver_t *gfx_ctx_vk_drivers[] = {
#if defined(__APPLE__)
&gfx_ctx_cocoavk,
#endif
#if defined(_WIN32) && !defined(__WINRT__)
&gfx_ctx_w_vk,
#endif
#if defined(ANDROID)
&gfx_ctx_vk_android,
#endif
#if defined(HAVE_WAYLAND)
&gfx_ctx_vk_wayland,
#endif
#if defined(HAVE_X11)
&gfx_ctx_vk_x,
#endif
#if defined(HAVE_VULKAN_DISPLAY)
&gfx_ctx_khr_display,
#endif
&gfx_ctx_null,
NULL
};
#endif
static const gfx_ctx_driver_t *gfx_ctx_gl_drivers[] = {
#if defined(ORBIS)
&orbis_ctx,
#endif
#if defined(HAVE_VITAGL) | defined(HAVE_VITAGLES)
&vita_ctx,
#endif
#if !defined(__PSL1GHT__) && defined(__PS3__)
&gfx_ctx_ps3,
#endif
#if defined(HAVE_LIBNX) && defined(HAVE_OPENGL)
&switch_ctx,
#endif
#if defined(HAVE_VIDEOCORE)
&gfx_ctx_videocore,
#endif
#if defined(HAVE_MALI_FBDEV)
&gfx_ctx_mali_fbdev,
#endif
#if defined(HAVE_VIVANTE_FBDEV)
&gfx_ctx_vivante_fbdev,
#endif
#if defined(HAVE_OPENDINGUX_FBDEV)
&gfx_ctx_opendingux_fbdev,
#endif
#if defined(_WIN32) && !defined(__WINRT__) && (defined(HAVE_OPENGL) || defined(HAVE_OPENGL1) || defined(HAVE_OPENGL_CORE))
&gfx_ctx_wgl,
#endif
#if defined(__WINRT__) && defined(HAVE_OPENGLES)
&gfx_ctx_uwp,
#endif
#if defined(HAVE_WAYLAND)
&gfx_ctx_wayland,
#endif
#if defined(HAVE_X11) && !defined(HAVE_OPENGLES)
#if defined(HAVE_OPENGL) || defined(HAVE_OPENGL1) || defined(HAVE_OPENGL_CORE)
&gfx_ctx_x,
#endif
#endif
#if defined(HAVE_X11) && defined(HAVE_OPENGL) && defined(HAVE_EGL)
&gfx_ctx_x_egl,
#endif
#if defined(HAVE_KMS)
#if defined(HAVE_ODROIDGO2)
&gfx_ctx_go2_drm,
#endif
&gfx_ctx_drm,
#endif
#if defined(ANDROID)
&gfx_ctx_android,
#endif
#if defined(__QNX__)
&gfx_ctx_qnx,
#endif
#if defined(HAVE_COCOA) || defined(HAVE_COCOATOUCH) || defined(HAVE_COCOA_METAL)
#if defined(HAVE_OPENGL) || defined(HAVE_OPENGLES)
&gfx_ctx_cocoagl,
#endif
#endif
#if (defined(HAVE_SDL) || defined(HAVE_SDL2)) && (defined(HAVE_OPENGL) || defined(HAVE_OPENGL1) || defined(HAVE_OPENGL_CORE))
&gfx_ctx_sdl_gl,
#endif
#ifdef HAVE_OSMESA
&gfx_ctx_osmesa,
#endif
#ifdef EMSCRIPTEN
&gfx_ctx_emscripten,
#endif
&gfx_ctx_null,
NULL
};
struct aspect_ratio_elem aspectratio_lut[ASPECT_RATIO_END] = {
{ 1.3333f, "4:3" },
{ 1.7778f, "16:9" },
{ 1.6f, "16:10" },
{ 16.0f / 15.0f, "16:15" },
{ 21.0f / 9.0f, "21:9" },
{ 1.0f, "1:1" },
{ 2.0f, "2:1" },
{ 1.5f, "3:2" },
{ 0.75f, "3:4" },
{ 4.0f, "4:1" },
{ 0.5625f, "9:16" },
{ 1.25f, "5:4" },
{ 1.2f, "6:5" },
{ 0.7777f, "7:9" },
{ 2.6666f, "8:3" },
{ 1.1428f, "8:7" },
{ 1.5833f, "19:12" },
{ 1.3571f, "19:14" },
{ 1.7647f, "30:17" },
{ 3.5555f, "32:9" },
{ 0.0f, "Config" },
{ 1.0f, "Square pixel" },
{ 1.0f, "Core provided" },
{ 0.0f, "Custom" },
{ 1.3333f, "Full" }
};
static void *video_null_init(const video_info_t *video,
input_driver_t **input, void **input_data)
{
*input = NULL;
*input_data = NULL;
frontend_driver_install_signal_handler();
return (void*)-1;
}
static bool video_null_frame(void *data, const void *frame,
unsigned frame_width, unsigned frame_height, uint64_t frame_count,
unsigned pitch, const char *msg, video_frame_info_t *video_info)
{
return true;
}
static void video_null_free(void *data) { }
static void video_null_set_nonblock_state(void *a, bool b, bool c, unsigned d) { }
static bool video_null_alive(void *data) { return frontend_driver_get_signal_handler_state() != 1; }
static bool video_null_focus(void *data) { return true; }
static bool video_null_has_windowed(void *data) { return true; }
static bool video_null_suppress_screensaver(void *data, bool enable) { return false; }
static bool video_null_set_shader(void *data,
enum rarch_shader_type type, const char *path) { return false; }
video_driver_t video_null = {
video_null_init,
video_null_frame,
video_null_set_nonblock_state,
video_null_alive,
video_null_focus,
video_null_suppress_screensaver,
video_null_has_windowed,
video_null_set_shader,
video_null_free,
"null",
NULL, /* set_viewport */
NULL, /* set_rotation */
NULL, /* viewport_info */
NULL, /* read_viewport */
NULL, /* read_frame_raw */
#ifdef HAVE_OVERLAY
NULL, /* overlay_interface */
#endif
#ifdef HAVE_VIDEO_LAYOUT
NULL,
#endif
NULL, /* get_poke_interface */
};
const video_driver_t *video_drivers[] = {
#ifdef __PSL1GHT__
&video_gcm,
#endif
#ifdef HAVE_VITA2D
&video_vita2d,
#endif
#ifdef HAVE_OPENGL
&video_gl2,
#endif
#if defined(HAVE_OPENGL_CORE)
&video_gl3,
#endif
#ifdef HAVE_OPENGL1
&video_gl1,
#endif
#ifdef HAVE_VULKAN
&video_vulkan,
#endif
#ifdef HAVE_METAL
&video_metal,
#endif
#ifdef XENON
&video_xenon360,
#endif
#if defined(HAVE_D3D12)
&video_d3d12,
#endif
#if defined(HAVE_D3D11)
&video_d3d11,
#endif
#if defined(HAVE_D3D10)
&video_d3d10,
#endif
#if defined(HAVE_D3D9)
&video_d3d9,
#endif
#if defined(HAVE_D3D8)
&video_d3d8,
#endif
#ifdef PSP
&video_psp1,
#endif
#ifdef PS2
&video_ps2,
#endif
#ifdef _3DS
&video_ctr,
#endif
#ifdef SWITCH
&video_switch,
#endif
#ifdef HAVE_ODROIDGO2
&video_oga,
#endif
#if defined(HAVE_SDL) && !defined(HAVE_SDL_DINGUX)
&video_sdl,
#endif
#ifdef HAVE_SDL2
&video_sdl2,
#endif
#ifdef HAVE_SDL_DINGUX
#if defined(RS90) || defined(MIYOO)
&video_sdl_rs90,
#else
&video_sdl_dingux,
#endif
#endif
#ifdef HAVE_XVIDEO
&video_xvideo,
#endif
#ifdef GEKKO
&video_gx,
#endif
#ifdef WIIU
&video_wiiu,
#endif
#ifdef HAVE_VG
&video_vg,
#endif
#ifdef HAVE_OMAP
&video_omap,
#endif
#ifdef HAVE_EXYNOS
&video_exynos,
#endif
#ifdef HAVE_DISPMANX
&video_dispmanx,
#endif
#ifdef HAVE_SUNXI
&video_sunxi,
#endif
#ifdef HAVE_PLAIN_DRM
&video_drm,
#endif
#ifdef HAVE_XSHM
&video_xshm,
#endif
#if defined(_WIN32) && !defined(_XBOX) && !defined(__WINRT__)
#ifdef HAVE_GDI
&video_gdi,
#endif
#endif
#ifdef DJGPP
&video_vga,
#endif
#ifdef HAVE_FPGA
&video_fpga,
#endif
#ifdef HAVE_SIXEL
&video_sixel,
#endif
#ifdef HAVE_CACA
&video_caca,
#endif
#ifdef HAVE_NETWORK_VIDEO
&video_network,
#endif
&video_null,
NULL,
};
static video_driver_state_t video_driver_st = { 0 };
static const video_display_server_t *current_display_server =
&dispserv_null;
struct retro_hw_render_callback *video_driver_get_hw_context(void)
{
video_driver_state_t *video_st = &video_driver_st;
return VIDEO_DRIVER_GET_HW_CONTEXT_INTERNAL(video_st);
}
video_driver_state_t *video_state_get_ptr(void)
{
return &video_driver_st;
}
void crt_switch_driver_refresh(void)
{
video_driver_reinit(DRIVERS_CMD_ALL);
}
#ifdef HAVE_THREADS
void *video_thread_get_ptr(video_driver_state_t *video_st)
{
const thread_video_t *thr = (const thread_video_t*)video_st->data;
if (thr)
return thr->driver_data;
return NULL;
}
#endif
/**
* video_driver_get_ptr:
*
* Use this if you need the real video driver
* and driver data pointers.
*
* Returns: video driver's userdata.
**/
void *video_driver_get_ptr(void)
{
video_driver_state_t *video_st = &video_driver_st;
return VIDEO_DRIVER_GET_PTR_INTERNAL(video_st);
}
video_driver_t *hw_render_context_driver(
enum retro_hw_context_type type, int major, int minor)
{
switch (type)
{
case RETRO_HW_CONTEXT_OPENGL_CORE:
#ifdef HAVE_OPENGL_CORE
return &video_gl3;
#else
break;
#endif
case RETRO_HW_CONTEXT_OPENGL:
#ifdef HAVE_OPENGL
return &video_gl2;
#else
break;
#endif
case RETRO_HW_CONTEXT_DIRECT3D:
#if defined(HAVE_D3D9)
if (major == 9)
return &video_d3d9;
#endif
#if defined(HAVE_D3D11)
if (major == 11)
return &video_d3d11;
#endif
break;
case RETRO_HW_CONTEXT_VULKAN:
#if defined(HAVE_VULKAN)
return &video_vulkan;
#else
break;
#endif
default:
case RETRO_HW_CONTEXT_NONE:
break;
}
return NULL;
}
const char *hw_render_context_name(
enum retro_hw_context_type type, int major, int minor)
{
#ifdef HAVE_OPENGL_CORE
if (type == RETRO_HW_CONTEXT_OPENGL_CORE)
return "glcore";
#endif
#ifdef HAVE_OPENGL
switch (type)
{
case RETRO_HW_CONTEXT_OPENGLES2:
case RETRO_HW_CONTEXT_OPENGLES3:
case RETRO_HW_CONTEXT_OPENGLES_VERSION:
case RETRO_HW_CONTEXT_OPENGL:
#ifndef HAVE_OPENGL_CORE
case RETRO_HW_CONTEXT_OPENGL_CORE:
#endif
return "gl";
default:
break;
}
#endif
#ifdef HAVE_VULKAN
if (type == RETRO_HW_CONTEXT_VULKAN)
return "vulkan";
#endif
#ifdef HAVE_D3D11
if (type == RETRO_HW_CONTEXT_DIRECT3D && major == 11)
return "d3d11";
#endif
#ifdef HAVE_D3D9
if (type == RETRO_HW_CONTEXT_DIRECT3D && major == 9)
return "d3d9";
#endif
return "N/A";
}
enum retro_hw_context_type hw_render_context_type(const char *s)
{
#ifdef HAVE_OPENGL_CORE
if (string_is_equal(s, "glcore"))
return RETRO_HW_CONTEXT_OPENGL_CORE;
#endif
#ifdef HAVE_OPENGL
if (string_is_equal(s, "gl"))
return RETRO_HW_CONTEXT_OPENGL;
#endif
#ifdef HAVE_VULKAN
if (string_is_equal(s, "vulkan"))
return RETRO_HW_CONTEXT_VULKAN;
#endif
#ifdef HAVE_D3D11
if (string_is_equal(s, "d3d11"))
return RETRO_HW_CONTEXT_DIRECT3D;
#endif
#ifdef HAVE_D3D11
if (string_is_equal(s, "d3d9"))
return RETRO_HW_CONTEXT_DIRECT3D;
#endif
return RETRO_HW_CONTEXT_NONE;
}
/* string list stays owned by the caller and must be available at
* all times after the video driver is inited */
void video_driver_set_gpu_api_devices(
enum gfx_ctx_api api, struct string_list *list)
{
int i;
for (i = 0; i < ARRAY_SIZE(gpu_map); i++)
{
if (api == gpu_map[i].api)
{
gpu_map[i].list = list;
break;
}
}
}
struct string_list* video_driver_get_gpu_api_devices(enum gfx_ctx_api api)
{
int i;
for (i = 0; i < ARRAY_SIZE(gpu_map); i++)
{
if (api == gpu_map[i].api)
return gpu_map[i].list;
}
return NULL;
}
/**
* video_driver_translate_coord_viewport:
* @mouse_x : Pointer X coordinate.
* @mouse_y : Pointer Y coordinate.
* @res_x : Scaled X coordinate.
* @res_y : Scaled Y coordinate.
* @res_screen_x : Scaled screen X coordinate.
* @res_screen_y : Scaled screen Y coordinate.
*
* Translates pointer [X,Y] coordinates into scaled screen
* coordinates based on viewport info.
*
* Returns: true (1) if successful, false if video driver doesn't support
* viewport info.
**/
bool video_driver_translate_coord_viewport(
struct video_viewport *vp,
int mouse_x, int mouse_y,
int16_t *res_x, int16_t *res_y,
int16_t *res_screen_x, int16_t *res_screen_y)
{
int norm_vp_width = (int)vp->width;
int norm_vp_height = (int)vp->height;
int norm_full_vp_width = (int)vp->full_width;
int norm_full_vp_height = (int)vp->full_height;
int scaled_screen_x = -0x8000; /* OOB */
int scaled_screen_y = -0x8000; /* OOB */
int scaled_x = -0x8000; /* OOB */
int scaled_y = -0x8000; /* OOB */
if (norm_vp_width <= 0 ||
norm_vp_height <= 0 ||
norm_full_vp_width <= 0 ||
norm_full_vp_height <= 0)
return false;
if (mouse_x >= 0 && mouse_x <= norm_full_vp_width)
scaled_screen_x = ((2 * mouse_x * 0x7fff)
/ norm_full_vp_width) - 0x7fff;
if (mouse_y >= 0 && mouse_y <= norm_full_vp_height)
scaled_screen_y = ((2 * mouse_y * 0x7fff)
/ norm_full_vp_height) - 0x7fff;
mouse_x -= vp->x;
mouse_y -= vp->y;
if (mouse_x >= 0 && mouse_x <= norm_vp_width)
scaled_x = ((2 * mouse_x * 0x7fff)
/ norm_vp_width) - 0x7fff;
else
scaled_x = -0x8000; /* OOB */
if (mouse_y >= 0 && mouse_y <= norm_vp_height)
scaled_y = ((2 * mouse_y * 0x7fff)
/ norm_vp_height) - 0x7fff;
*res_x = scaled_x;
*res_y = scaled_y;
*res_screen_x = scaled_screen_x;
*res_screen_y = scaled_screen_y;
return true;
}
void video_monitor_compute_fps_statistics(uint64_t
frame_time_count)
{
double avg_fps = 0.0;
double stddev = 0.0;
unsigned samples = 0;
if (frame_time_count <
(2 * MEASURE_FRAME_TIME_SAMPLES_COUNT))
{
RARCH_LOG(
"[Video]: Does not have enough samples for monitor refresh rate"
" estimation. Requires to run for at least %u frames.\n",
2 * MEASURE_FRAME_TIME_SAMPLES_COUNT);
return;
}
if (video_monitor_fps_statistics(&avg_fps, &stddev, &samples))
{
RARCH_LOG("[Video]: Average monitor Hz: %.6f Hz. (%.3f %% frame time"
" deviation, based on %u last samples).\n",
avg_fps, 100.0f * stddev, samples);
}
}
void video_monitor_set_refresh_rate(float hz)
{
char msg[128];
settings_t *settings = config_get_ptr();
snprintf(msg, sizeof(msg),
"Setting refresh rate to: %.3f Hz.", hz);
if (settings->bools.notification_show_refresh_rate)
runloop_msg_queue_push(msg, 1, 180, false, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
RARCH_LOG("[Video]: %s\n", msg);
configuration_set_float(settings,
settings->floats.video_refresh_rate,
hz);
}
void video_driver_force_fallback(const char *driver)
{
settings_t *settings = config_get_ptr();
ui_msg_window_t *msg_window = NULL;
configuration_set_string(settings,
settings->arrays.video_driver,
driver);
command_event(CMD_EVENT_MENU_SAVE_CURRENT_CONFIG, NULL);
#if defined(_WIN32) && !defined(_XBOX) && !defined(__WINRT__) && !defined(WINAPI_FAMILY)
/* UI companion driver is not inited yet, just call into it directly */
msg_window = &ui_msg_window_win32;
#endif
if (msg_window)
{
char text[PATH_MAX_LENGTH];
ui_msg_window_state window_state;
char *title = strdup(msg_hash_to_str(MSG_ERROR));
text[0] = '\0';
snprintf(text, sizeof(text),
msg_hash_to_str(MENU_ENUM_LABEL_VALUE_VIDEO_DRIVER_FALLBACK),
driver);
window_state.buttons = UI_MSG_WINDOW_OK;
window_state.text = strdup(text);
window_state.title = title;
window_state.window = NULL;
msg_window->error(&window_state);
free(title);
}
exit(1);
}
static bool get_metrics_null(void *data, enum display_metric_types type,
float *value) { return false; }
static bool video_context_driver_get_metrics_null(
void *data, enum display_metric_types type,
float *value) { return false; }
void video_context_driver_destroy(gfx_ctx_driver_t *ctx_driver)
{
if (!ctx_driver)
return;
ctx_driver->init = NULL;
ctx_driver->bind_api = NULL;
ctx_driver->swap_interval = NULL;
ctx_driver->set_video_mode = NULL;
ctx_driver->get_video_size = NULL;
ctx_driver->get_video_output_size = NULL;
ctx_driver->get_video_output_prev = NULL;
ctx_driver->get_video_output_next = NULL;
ctx_driver->get_metrics =
video_context_driver_get_metrics_null;
ctx_driver->translate_aspect = NULL;
ctx_driver->update_window_title = NULL;
ctx_driver->check_window = NULL;
ctx_driver->set_resize = NULL;
ctx_driver->suppress_screensaver = NULL;
ctx_driver->swap_buffers = NULL;
ctx_driver->input_driver = NULL;
ctx_driver->get_proc_address = NULL;
ctx_driver->image_buffer_init = NULL;
ctx_driver->image_buffer_write = NULL;
ctx_driver->show_mouse = NULL;
ctx_driver->ident = NULL;
ctx_driver->get_flags = NULL;
ctx_driver->set_flags = NULL;
ctx_driver->bind_hw_render = NULL;
ctx_driver->get_context_data = NULL;
ctx_driver->make_current = NULL;
}
const gfx_ctx_driver_t *video_context_driver_init(
bool core_set_shared_context,
settings_t *settings,
void *data,
const gfx_ctx_driver_t *ctx,
const char *ident,
enum gfx_ctx_api api, unsigned major,
unsigned minor, bool hw_render_ctx,
void **ctx_data)
{
if (!ctx->bind_api(data, api, major, minor))
{
RARCH_WARN("Failed to bind API (#%u, version %u.%u)"
" on context driver \"%s\".\n",
(unsigned)api, major, minor, ctx->ident);
return NULL;
}
if (!(*ctx_data = ctx->init(data)))
return NULL;
if (ctx->bind_hw_render)
{
bool video_shared_context =
settings->bools.video_shared_context || core_set_shared_context;
ctx->bind_hw_render(*ctx_data,
video_shared_context && hw_render_ctx);
}
return ctx;
}
/**
* config_get_video_driver_options:
*
* Get an enumerated list of all video driver names, separated by '|'.
*
* Returns: string listing of all video driver names, separated by '|'.
**/
const char* config_get_video_driver_options(void)
{
return char_list_new_special(STRING_LIST_VIDEO_DRIVERS, NULL);
}
void video_driver_pixel_converter_free(
video_pixel_scaler_t *scalr)
{
if (!scalr)
return;
if (scalr->scaler)
{
scaler_ctx_gen_reset(scalr->scaler);
free(scalr->scaler);
}
if (scalr->scaler_out)
free(scalr->scaler_out);
scalr->scaler = NULL;
scalr->scaler_out = NULL;
free(scalr);
}
video_pixel_scaler_t *video_driver_pixel_converter_init(
const enum retro_pixel_format video_driver_pix_fmt,
struct retro_hw_render_callback *hwr,
unsigned size)
{
void *scalr_out = NULL;
video_pixel_scaler_t *scalr = NULL;
struct scaler_ctx *scalr_ctx = NULL;
/* If pixel format is not 0RGB1555, we don't need to do
* any internal pixel conversion. */
if (video_driver_pix_fmt != RETRO_PIXEL_FORMAT_0RGB1555)
return NULL;
/* No need to perform pixel conversion for HW rendering contexts. */
if (hwr && hwr->context_type != RETRO_HW_CONTEXT_NONE)
return NULL;
RARCH_WARN("[Video]: 0RGB1555 pixel format is deprecated,"
" and will be slower. For 15/16-bit, RGB565"
" format is preferred.\n");
if (!(scalr = (video_pixel_scaler_t*)malloc(sizeof(*scalr))))
goto error;
scalr->scaler = NULL;
scalr->scaler_out = NULL;
if (!(scalr_ctx = (struct scaler_ctx*)calloc(1, sizeof(*scalr_ctx))))
goto error;
scalr->scaler = scalr_ctx;
scalr->scaler->scaler_type = SCALER_TYPE_POINT;
scalr->scaler->in_fmt = SCALER_FMT_0RGB1555;
/* TODO/FIXME: Pick either ARGB8888 or RGB565 depending on driver. */
scalr->scaler->out_fmt = SCALER_FMT_RGB565;
if (!scaler_ctx_gen_filter(scalr_ctx))
goto error;
if (!(scalr_out = calloc(sizeof(uint16_t), size * size)))
goto error;
scalr->scaler_out = scalr_out;
return scalr;
error:
video_driver_pixel_converter_free(scalr);
#ifdef HAVE_VIDEO_FILTER
video_driver_filter_free();
#endif
return NULL;
}
struct video_viewport *video_viewport_get_custom(void)
{
return &config_get_ptr()->video_viewport_custom;
}
bool video_driver_monitor_adjust_system_rates(
float timing_skew_hz,
float video_refresh_rate,
bool vrr_runloop_enable,
float audio_max_timing_skew,
double input_fps)
{
if (!vrr_runloop_enable)
{
float timing_skew = fabs(
1.0f - input_fps / timing_skew_hz);
/* We don't want to adjust pitch too much. If we have extreme cases,
* just don't readjust at all. */
if (timing_skew <= audio_max_timing_skew)
return true;
RARCH_LOG("[Video]: Timings deviate too much. Will not adjust."
" (Display = %.2f Hz, Game = %.2f Hz)\n",
video_refresh_rate,
(float)input_fps);
}
return input_fps <= timing_skew_hz;
}
void video_driver_reset_custom_viewport(settings_t *settings)
{
struct video_viewport *custom_vp = &settings->video_viewport_custom;
custom_vp->width = 0;
custom_vp->height = 0;
custom_vp->x = 0;
custom_vp->y = 0;
}
struct retro_system_av_info *video_viewport_get_system_av_info(void)
{
return &video_driver_st.av_info;
}
void video_driver_gpu_record_deinit(void)
{
video_driver_state_t *video_st = &video_driver_st;
if (video_st->record_gpu_buffer)
free(video_st->record_gpu_buffer);
video_st->record_gpu_buffer = NULL;
}
void recording_dump_frame(
const void *data, unsigned width,
unsigned height, size_t pitch, bool is_idle)
{
struct record_video_data ffemu_data;
video_driver_state_t
*video_st = &video_driver_st;
recording_state_t
*record_st = recording_state_get_ptr();
ffemu_data.data = data;
ffemu_data.width = width;
ffemu_data.height = height;
ffemu_data.pitch = (int)pitch;
ffemu_data.is_dupe = false;
if (video_st->record_gpu_buffer)
{
struct video_viewport vp;
vp.x = 0;
vp.y = 0;
vp.width = 0;
vp.height = 0;
vp.full_width = 0;
vp.full_height = 0;
video_driver_get_viewport_info(&vp);
if (!vp.width || !vp.height)
{
RARCH_WARN("[Recording]: %s\n",
msg_hash_to_str(MSG_VIEWPORT_SIZE_CALCULATION_FAILED));
video_driver_gpu_record_deinit();
recording_dump_frame(
data, width, height, pitch, is_idle);
return;
}
/* User has resized. We kinda have a problem now. */
if ( vp.width != record_st->gpu_width ||
vp.height != record_st->gpu_height)
{
RARCH_WARN("[Recording]: %s\n",
msg_hash_to_str(MSG_RECORDING_TERMINATED_DUE_TO_RESIZE));
runloop_msg_queue_push(
msg_hash_to_str(MSG_RECORDING_TERMINATED_DUE_TO_RESIZE),
1, 180, true,
NULL, MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
command_event(CMD_EVENT_RECORD_DEINIT, NULL);
return;
}
/* Big bottleneck.
* Since we might need to do read-backs asynchronously,
* it might take 3-4 times before this returns true. */
if (!video_driver_read_viewport(video_st->record_gpu_buffer, is_idle))
return;
ffemu_data.pitch = (int)(record_st->gpu_width * 3);
ffemu_data.width = (unsigned)record_st->gpu_width;
ffemu_data.height = (unsigned)record_st->gpu_height;
ffemu_data.data = video_st->record_gpu_buffer + (ffemu_data.height - 1) * ffemu_data.pitch;
ffemu_data.pitch = -ffemu_data.pitch;
}
else
ffemu_data.is_dupe = !data;
record_st->driver->push_video(record_st->data, &ffemu_data);
}