forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 2
/
video_driver.c
3772 lines (3201 loc) · 107 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-2017 - 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 <stddef.h>
#include <string.h>
#include <compat/strl.h>
#include <retro_common_api.h>
#include <file/config_file.h>
#include <features/features_cpu.h>
#include <file/file_path.h>
#include <string/stdstring.h>
#include <retro_math.h>
#include <retro_assert.h>
#include <gfx/scaler/pixconv.h>
#include <gfx/scaler/scaler.h>
#include <gfx/video_frame.h>
#include <formats/image.h>
#include "../audio/audio_driver.h"
#include "../menu/menu_shader.h"
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
#include "../dynamic.h"
#ifdef HAVE_THREADS
#include <rthreads/rthreads.h>
#endif
#ifdef HAVE_MENU
#include "../menu/menu_driver.h"
#include "../menu/menu_setting.h"
#endif
#include "video_thread_wrapper.h"
#include "video_driver.h"
#include "video_display_server.h"
#include "video_crt_switch.h"
#include "../frontend/frontend_driver.h"
#include "../record/record_driver.h"
#include "../config.def.h"
#include "../configuration.h"
#include "../driver.h"
#include "../retroarch.h"
#include "../input/input_driver.h"
#include "../list_special.h"
#include "../core.h"
#include "../command.h"
#include "../msg_hash.h"
#include "../verbosity.h"
#define MEASURE_FRAME_TIME_SAMPLES_COUNT (2 * 1024)
#define TIME_TO_FPS(last_time, new_time, frames) ((1000000.0f * (frames)) / ((new_time) - (last_time)))
#define FPS_UPDATE_INTERVAL 256
#ifdef HAVE_THREADS
#define video_driver_is_threaded_internal() ((!video_driver_is_hw_context() && video_driver_threaded) ? true : false)
#else
#define video_driver_is_threaded_internal() (false)
#endif
#ifdef HAVE_THREADS
#define video_driver_lock() \
if (display_lock) \
slock_lock(display_lock)
#define video_driver_unlock() \
if (display_lock) \
slock_unlock(display_lock)
#define video_driver_context_lock() \
if (context_lock) \
slock_lock(context_lock)
#define video_driver_context_unlock() \
if (context_lock) \
slock_unlock(context_lock)
#define video_driver_lock_free() \
slock_free(display_lock); \
slock_free(context_lock); \
display_lock = NULL; \
context_lock = NULL
#define video_driver_threaded_lock(is_threaded) \
if (is_threaded) \
video_driver_lock()
#define video_driver_threaded_unlock(is_threaded) \
if (is_threaded) \
video_driver_unlock()
#else
#define video_driver_lock() ((void)0)
#define video_driver_unlock() ((void)0)
#define video_driver_lock_free() ((void)0)
#define video_driver_threaded_lock(is_threaded) ((void)0)
#define video_driver_threaded_unlock(is_threaded) ((void)0)
#define video_driver_context_lock() ((void)0)
#define video_driver_context_unlock() ((void)0)
#endif
typedef struct video_pixel_scaler
{
struct scaler_ctx *scaler;
void *scaler_out;
} video_pixel_scaler_t;
static void (*video_driver_cb_shader_use)(void *data,
void *shader_data, unsigned index, bool set_active);
static bool (*video_driver_cb_shader_set_mvp)(void *data,
void *shader_data, const void *mat_data);
bool (*video_driver_cb_has_focus)(void);
/* Opaque handles to currently running window.
* Used by e.g. input drivers which bind to a window.
* Drivers are responsible for setting these if an input driver
* could potentially make use of this. */
static uintptr_t video_driver_display = 0;
static uintptr_t video_driver_window = 0;
static rarch_softfilter_t *video_driver_state_filter = NULL;
static void *video_driver_state_buffer = NULL;
static unsigned video_driver_state_scale = 0;
static unsigned video_driver_state_out_bpp = 0;
static bool video_driver_state_out_rgb32 = false;
static bool video_driver_crt_switching_active = false;
static struct retro_system_av_info video_driver_av_info;
static enum retro_pixel_format video_driver_pix_fmt = RETRO_PIXEL_FORMAT_0RGB1555;
static const void *frame_cache_data = NULL;
static unsigned frame_cache_width = 0;
static unsigned frame_cache_height = 0;
static size_t frame_cache_pitch = 0;
static bool video_driver_threaded = false;
static float video_driver_core_hz = 0.0f;
static float video_driver_aspect_ratio = 0.0f;
static unsigned video_driver_width = 0;
static unsigned video_driver_height = 0;
static enum rarch_display_type video_driver_display_type = RARCH_DISPLAY_NONE;
static char video_driver_title_buf[64] = {0};
static char video_driver_window_title[512] = {0};
static bool video_driver_window_title_update = true;
static retro_time_t video_driver_frame_time_samples[MEASURE_FRAME_TIME_SAMPLES_COUNT];
static uint64_t video_driver_frame_time_count = 0;
static uint64_t video_driver_frame_count = 0;
static void *video_driver_data = NULL;
static video_driver_t *current_video = NULL;
/* Interface for "poking". */
static const video_poke_interface_t *video_driver_poke = NULL;
/* Used for 15-bit -> 16-bit conversions that take place before
* being passed to video driver. */
static video_pixel_scaler_t *video_driver_scaler_ptr = NULL;
static struct retro_hw_render_callback hw_render;
static const struct
retro_hw_render_context_negotiation_interface *
hw_render_context_negotiation = NULL;
/* Graphics driver requires RGBA byte order data (ABGR on little-endian)
* for 32-bit.
* This takes effect for overlay and shader cores that wants to load
* data into graphics driver. Kinda hackish to place it here, it is only
* used for GLES.
* TODO: Refactor this better. */
static bool video_driver_use_rgba = false;
static bool video_driver_data_own = false;
static bool video_driver_active = false;
static video_driver_frame_t frame_bak = NULL;
/* If set during context deinit, the driver should keep
* graphics context alive to avoid having to reset all
* context state. */
static bool video_driver_cache_context = false;
/* Set to true by driver if context caching succeeded. */
static bool video_driver_cache_context_ack = false;
static uint8_t *video_driver_record_gpu_buffer = NULL;
#ifdef HAVE_THREADS
static slock_t *display_lock = NULL;
static slock_t *context_lock = NULL;
#endif
static gfx_ctx_driver_t current_video_context;
static void *video_context_data = NULL;
/**
* dynamic.c:dynamic_request_hw_context will try to set flag data when the context
* is in the middle of being rebuilt; in these cases we will save flag
* data and set this to true.
* When the context is reinit, it checks this, reads from
* deferred_flag_data and cleans it.
*
* TODO - Dirty hack, fix it better
*/
static bool deferred_video_context_driver_set_flags = false;
static gfx_ctx_flags_t deferred_flag_data = {0};
static bool video_started_fullscreen = false;
static shader_backend_t *current_shader = NULL;
static void *current_shader_data = NULL;
struct aspect_ratio_elem aspectratio_lut[ASPECT_RATIO_END] = {
{ "4:3", 1.3333f },
{ "16:9", 1.7778f },
{ "16:10", 1.6f },
{ "16:15", 16.0f / 15.0f },
{ "21:9", 21.0f / 9.0f },
{ "1:1", 1.0f },
{ "2:1", 2.0f },
{ "3:2", 1.5f },
{ "3:4", 0.75f },
{ "4:1", 4.0f },
{ "9:16", 0.5625f },
{ "5:4", 1.25f },
{ "6:5", 1.2f },
{ "7:9", 0.7777f },
{ "8:3", 2.6666f },
{ "8:7", 1.1428f },
{ "19:12", 1.5833f },
{ "19:14", 1.3571f },
{ "30:17", 1.7647f },
{ "32:9", 3.5555f },
{ "Config", 0.0f },
{ "Square pixel", 1.0f },
{ "Core provided", 1.0f },
{ "Custom", 0.0f }
};
static const video_driver_t *video_drivers[] = {
#ifdef HAVE_OPENGL
&video_gl,
#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 HAVE_VITA2D
&video_vita2d,
#endif
#ifdef PSP
&video_psp1,
#endif
#ifdef PS2
&video_ps2,
#endif
#ifdef _3DS
&video_ctr,
#endif
#ifdef SWITCH
&video_switch,
#endif
#ifdef HAVE_SDL
&video_sdl,
#endif
#ifdef HAVE_SDL2
&video_sdl2,
#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__)
&video_gdi,
#endif
#ifdef DJGPP
&video_vga,
#endif
#ifdef HAVE_SIXEL
&video_sixel,
#endif
#ifdef HAVE_CACA
&video_caca,
#endif
&video_null,
NULL,
};
static const gfx_ctx_driver_t *gfx_ctx_drivers[] = {
#if defined(ORBIS)
&orbis_ctx,
#endif
#if defined(HAVE_LIBNX) && defined(HAVE_OPENGL)
&switch_ctx,
#endif
#if defined(__CELLOS_LV2__)
&gfx_ctx_ps3,
#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(HAVE_OPENGL) || defined(HAVE_VULKAN))
&gfx_ctx_wgl,
#endif
#if defined(HAVE_WAYLAND)
&gfx_ctx_wayland,
#endif
#if defined(HAVE_X11) && !defined(HAVE_OPENGLES)
#if defined(HAVE_OPENGL) || defined(HAVE_VULKAN)
&gfx_ctx_x,
#endif
#endif
#if defined(HAVE_X11) && defined(HAVE_OPENGL) && defined(HAVE_EGL)
&gfx_ctx_x_egl,
#endif
#if defined(HAVE_KMS)
&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)
&gfx_ctx_cocoagl,
#endif
#if defined(__APPLE__) && !defined(TARGET_IPHONE_SIMULATOR) && !defined(TARGET_OS_IPHONE)
&gfx_ctx_cgl,
#endif
#if (defined(HAVE_SDL) || defined(HAVE_SDL2)) && defined(HAVE_OPENGL)
&gfx_ctx_sdl_gl,
#endif
#ifdef HAVE_OSMESA
&gfx_ctx_osmesa,
#endif
#ifdef EMSCRIPTEN
&gfx_ctx_emscripten,
#endif
#if defined(HAVE_VULKAN) && defined(HAVE_VULKAN_DISPLAY)
&gfx_ctx_khr_display,
#endif
#if defined(_WIN32) && !defined(_XBOX) && !defined(__WINRT__)
&gfx_ctx_gdi,
#endif
#ifdef HAVE_SIXEL
&gfx_ctx_sixel,
#endif
&gfx_ctx_null,
NULL
};
static const shader_backend_t *shader_ctx_drivers[] = {
#ifdef HAVE_GLSL
&gl_glsl_backend,
#endif
#ifdef HAVE_CG
&gl_cg_backend,
#endif
&shader_null_backend,
NULL
};
bool video_driver_started_fullscreen(void)
{
return video_started_fullscreen;
}
/* Stub functions */
static void update_window_title_null(void *data, void *data2)
{
}
static void swap_buffers_null(void *data, void *data2)
{
}
static bool get_metrics_null(void *data, enum display_metric_types type,
float *value)
{
return false;
}
static bool set_resize_null(void *a, unsigned b, unsigned c)
{
return false;
}
/**
* video_driver_find_handle:
* @idx : index of driver to get handle to.
*
* Returns: handle to video driver at index. Can be NULL
* if nothing found.
**/
const void *video_driver_find_handle(int idx)
{
const void *drv = video_drivers[idx];
if (!drv)
return NULL;
return drv;
}
/**
* video_driver_find_ident:
* @idx : index of driver to get handle to.
*
* Returns: Human-readable identifier of video driver at index. Can be NULL
* if nothing found.
**/
const char *video_driver_find_ident(int idx)
{
const video_driver_t *drv = video_drivers[idx];
if (!drv)
return NULL;
return drv->ident;
}
/**
* 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);
}
bool video_driver_is_threaded(void)
{
return video_driver_is_threaded_internal();
}
#ifdef HAVE_VULKAN
static bool hw_render_context_is_vulkan(enum retro_hw_context_type type)
{
return type == RETRO_HW_CONTEXT_VULKAN;
}
#endif
#if defined(HAVE_OPENGL)
static bool hw_render_context_is_gl(enum retro_hw_context_type type)
{
switch (type)
{
case RETRO_HW_CONTEXT_OPENGL:
case RETRO_HW_CONTEXT_OPENGLES2:
case RETRO_HW_CONTEXT_OPENGL_CORE:
case RETRO_HW_CONTEXT_OPENGLES3:
case RETRO_HW_CONTEXT_OPENGLES_VERSION:
return true;
default:
break;
}
return false;
}
#endif
bool *video_driver_get_threaded(void)
{
return &video_driver_threaded;
}
void video_driver_set_threaded(bool val)
{
video_driver_threaded = val;
}
/**
* 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(bool force_nonthreaded_data)
{
#ifdef HAVE_THREADS
if (video_driver_is_threaded_internal() && !force_nonthreaded_data)
return video_thread_get_ptr(NULL);
#endif
return video_driver_data;
}
const char *video_driver_get_ident(void)
{
return (current_video) ? current_video->ident : NULL;
}
const video_poke_interface_t *video_driver_get_poke(void)
{
return video_driver_poke;
}
static bool video_context_has_focus(void)
{
return current_video_context.has_focus && current_video_context.has_focus(video_context_data);
}
static bool video_driver_has_focus(void)
{
return current_video && current_video->focus && current_video->focus(video_driver_data);
}
static bool null_driver_has_focus(void)
{
return true;
}
static void video_context_driver_reset(void)
{
if (!current_video_context.get_metrics)
current_video_context.get_metrics = get_metrics_null;
if (!current_video_context.update_window_title)
current_video_context.update_window_title = update_window_title_null;
if (!current_video_context.set_resize)
current_video_context.set_resize = set_resize_null;
if (!current_video_context.swap_buffers)
current_video_context.swap_buffers = swap_buffers_null;
if (current_video_context.has_focus)
video_driver_cb_has_focus = video_context_has_focus;
}
bool video_context_driver_set(const gfx_ctx_driver_t *data)
{
if (!data)
return false;
current_video_context = *data;
video_context_driver_reset();
return true;
}
void video_context_driver_destroy(void)
{
current_video_context.init = NULL;
current_video_context.bind_api = NULL;
current_video_context.swap_interval = NULL;
current_video_context.set_video_mode = NULL;
current_video_context.get_video_size = NULL;
current_video_context.get_video_output_size = NULL;
current_video_context.get_video_output_prev = NULL;
current_video_context.get_video_output_next = NULL;
current_video_context.get_metrics = get_metrics_null;
current_video_context.translate_aspect = NULL;
current_video_context.update_window_title = update_window_title_null;
current_video_context.check_window = NULL;
current_video_context.set_resize = set_resize_null;
current_video_context.has_focus = NULL;
current_video_context.suppress_screensaver = NULL;
current_video_context.has_windowed = NULL;
current_video_context.swap_buffers = swap_buffers_null;
current_video_context.input_driver = NULL;
current_video_context.get_proc_address = NULL;
current_video_context.image_buffer_init = NULL;
current_video_context.image_buffer_write = NULL;
current_video_context.show_mouse = NULL;
current_video_context.ident = NULL;
current_video_context.get_flags = NULL;
current_video_context.set_flags = NULL;
current_video_context.bind_hw_render = NULL;
current_video_context.get_context_data = NULL;
current_video_context.make_current = NULL;
}
/**
* video_driver_get_current_framebuffer:
*
* Gets pointer to current hardware renderer framebuffer object.
* Used by RETRO_ENVIRONMENT_SET_HW_RENDER.
*
* Returns: pointer to hardware framebuffer object, otherwise 0.
**/
uintptr_t video_driver_get_current_framebuffer(void)
{
if (video_driver_poke && video_driver_poke->get_current_framebuffer)
return video_driver_poke->get_current_framebuffer(video_driver_data);
return 0;
}
retro_proc_address_t video_driver_get_proc_address(const char *sym)
{
if (video_driver_poke && video_driver_poke->get_proc_address)
return video_driver_poke->get_proc_address(video_driver_data, sym);
return NULL;
}
bool video_driver_set_shader(enum rarch_shader_type type,
const char *path)
{
if (current_video->set_shader)
return current_video->set_shader(video_driver_data, type, path);
return false;
}
static void video_driver_filter_free(void)
{
if (video_driver_state_filter)
rarch_softfilter_free(video_driver_state_filter);
video_driver_state_filter = NULL;
if (video_driver_state_buffer)
{
#ifdef _3DS
linearFree(video_driver_state_buffer);
#else
free(video_driver_state_buffer);
#endif
}
video_driver_state_buffer = NULL;
video_driver_state_scale = 0;
video_driver_state_out_bpp = 0;
video_driver_state_out_rgb32 = false;
}
static void video_driver_init_filter(enum retro_pixel_format colfmt_int)
{
unsigned pow2_x, pow2_y, maxsize;
void *buf = NULL;
settings_t *settings = config_get_ptr();
struct retro_game_geometry *geom = &video_driver_av_info.geometry;
unsigned width = geom->max_width;
unsigned height = geom->max_height;
/* Deprecated format. Gets pre-converted. */
enum retro_pixel_format colfmt =
(colfmt_int == RETRO_PIXEL_FORMAT_0RGB1555) ?
RETRO_PIXEL_FORMAT_RGB565 : colfmt_int;
if (video_driver_is_hw_context())
{
RARCH_WARN("Cannot use CPU filters when hardware rendering is used.\n");
return;
}
video_driver_state_filter = rarch_softfilter_new(
settings->paths.path_softfilter_plugin,
RARCH_SOFTFILTER_THREADS_AUTO, colfmt, width, height);
if (!video_driver_state_filter)
{
RARCH_ERR("[Video]: Failed to load filter.\n");
return;
}
rarch_softfilter_get_max_output_size(video_driver_state_filter,
&width, &height);
pow2_x = next_pow2(width);
pow2_y = next_pow2(height);
maxsize = MAX(pow2_x, pow2_y);
video_driver_state_scale = maxsize / RARCH_SCALE_BASE;
video_driver_state_out_rgb32 = rarch_softfilter_get_output_format(
video_driver_state_filter) ==
RETRO_PIXEL_FORMAT_XRGB8888;
video_driver_state_out_bpp = video_driver_state_out_rgb32 ?
sizeof(uint32_t) :
sizeof(uint16_t);
/* TODO: Aligned output. */
#ifdef _3DS
buf = linearMemAlign(
width * height * video_driver_state_out_bpp, 0x80);
#else
buf = malloc(
width * height * video_driver_state_out_bpp);
#endif
if (!buf)
{
RARCH_ERR("[Video]: Softfilter initialization failed.\n");
video_driver_filter_free();
return;
}
video_driver_state_buffer = buf;
}
static void video_driver_init_input(const input_driver_t *tmp)
{
const input_driver_t **input = input_get_double_ptr();
if (*input)
return;
/* Video driver didn't provide an input driver,
* so we use configured one. */
RARCH_LOG("[Video]: Graphics driver did not initialize an input driver."
" Attempting to pick a suitable driver.\n");
if (tmp)
*input = tmp;
else
input_driver_find_driver();
/* This should never really happen as tmp (driver.input) is always
* found before this in find_driver_input(), or we have aborted
* in a similar fashion anyways. */
if (!input_get_ptr())
goto error;
if (input_driver_init())
return;
error:
RARCH_ERR("[Video]: Cannot initialize input driver. Exiting ...\n");
retroarch_fail(1, "video_driver_init_input()");
}
/**
* video_driver_monitor_compute_fps_statistics:
*
* Computes monitor FPS statistics.
**/
static void video_driver_monitor_compute_fps_statistics(void)
{
double avg_fps = 0.0;
double stddev = 0.0;
unsigned samples = 0;
if (video_driver_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.0 * stddev, samples);
}
}
static void video_driver_pixel_converter_free(void)
{
if (!video_driver_scaler_ptr)
return;
scaler_ctx_gen_reset(video_driver_scaler_ptr->scaler);
if (video_driver_scaler_ptr->scaler)
free(video_driver_scaler_ptr->scaler);
video_driver_scaler_ptr->scaler = NULL;
if (video_driver_scaler_ptr->scaler_out)
free(video_driver_scaler_ptr->scaler_out);
video_driver_scaler_ptr->scaler_out = NULL;
if (video_driver_scaler_ptr)
free(video_driver_scaler_ptr);
video_driver_scaler_ptr = NULL;
}
static void video_driver_free_internal(void)
{
#ifdef HAVE_THREADS
bool is_threaded = video_driver_is_threaded_internal();
#endif
command_event(CMD_EVENT_OVERLAY_DEINIT, NULL);
if (!video_driver_is_video_cache_context())
video_driver_free_hw_context();
if (
!input_driver_owns_driver() &&
!input_driver_is_data_ptr_same(video_driver_data)
)
input_driver_deinit();
if (
!video_driver_data_own
&& video_driver_data
&& current_video && current_video->free
)
current_video->free(video_driver_data);
video_driver_pixel_converter_free();
video_driver_filter_free();
command_event(CMD_EVENT_SHADER_DIR_DEINIT, NULL);
#ifdef HAVE_THREADS
if (is_threaded)
return;
#endif
video_driver_monitor_compute_fps_statistics();
}
static bool video_driver_pixel_converter_init(unsigned size)
{
struct retro_hw_render_callback *hwr =
video_driver_get_hw_context();
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 true;
/* No need to perform pixel conversion for HW rendering contexts. */
if (hwr && hwr->context_type != RETRO_HW_CONTEXT_NONE)
return true;
RARCH_WARN("0RGB1555 pixel format is deprecated,"
" and will be slower. For 15/16-bit, RGB565"
" format is preferred.\n");
scalr = (video_pixel_scaler_t*)calloc(1, sizeof(*scalr));
if (!scalr)
goto error;
video_driver_scaler_ptr = scalr;
scalr_ctx = (struct scaler_ctx*)calloc(1, sizeof(*scalr_ctx));
if (!scalr_ctx)
goto error;
video_driver_scaler_ptr->scaler = scalr_ctx;
video_driver_scaler_ptr->scaler->scaler_type = SCALER_TYPE_POINT;
video_driver_scaler_ptr->scaler->in_fmt = SCALER_FMT_0RGB1555;
/* TODO: Pick either ARGB8888 or RGB565 depending on driver. */
video_driver_scaler_ptr->scaler->out_fmt = SCALER_FMT_RGB565;
if (!scaler_ctx_gen_filter(scalr_ctx))
goto error;
scalr_out = calloc(sizeof(uint16_t), size * size);
if (!scalr_out)
goto error;
video_driver_scaler_ptr->scaler_out = scalr_out;
return true;
error:
video_driver_pixel_converter_free();
video_driver_filter_free();
return false;
}
static bool video_driver_init_internal(bool *video_is_threaded)
{
video_info_t video;
unsigned max_dim, scale, width, height;
video_viewport_t *custom_vp = NULL;
const input_driver_t *tmp = NULL;
rarch_system_info_t *system = NULL;
static uint16_t dummy_pixels[32] = {0};
settings_t *settings = config_get_ptr();
struct retro_game_geometry *geom = &video_driver_av_info.geometry;
if (!string_is_empty(settings->paths.path_softfilter_plugin))
video_driver_init_filter(video_driver_pix_fmt);
max_dim = MAX(geom->max_width, geom->max_height);
scale = next_pow2(max_dim) / RARCH_SCALE_BASE;
scale = MAX(scale, 1);
if (video_driver_state_filter)
scale = video_driver_state_scale;
/* Update core-dependent aspect ratio values. */
video_driver_set_viewport_square_pixel();
video_driver_set_viewport_core();
video_driver_set_viewport_config();
/* Update CUSTOM viewport. */
custom_vp = video_viewport_get_custom();
if (settings->uints.video_aspect_ratio_idx == ASPECT_RATIO_CUSTOM)
{
float default_aspect = aspectratio_lut[ASPECT_RATIO_CORE].value;
aspectratio_lut[ASPECT_RATIO_CUSTOM].value =
(custom_vp->width && custom_vp->height) ?
(float)custom_vp->width / custom_vp->height : default_aspect;
}
video_driver_set_aspect_ratio_value(
aspectratio_lut[settings->uints.video_aspect_ratio_idx].value);
if (settings->bools.video_fullscreen|| retroarch_is_forced_fullscreen())
{
width = settings->uints.video_fullscreen_x;
height = settings->uints.video_fullscreen_y;
}
else
{
/* To-Do: remove when the new window resizing core is hooked */
if (settings->bools.video_window_save_positions &&
(settings->uints.window_position_width || settings->uints.window_position_height))
{
width = settings->uints.window_position_width;
height = settings->uints.window_position_height;
}
else
{
if (settings->bools.video_force_aspect)