forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvideo_driver.c
2240 lines (1876 loc) · 58.8 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-2016 - 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 <retro_assert.h>
#include <gfx/scaler/pixconv.h>
#include <gfx/scaler/scaler.h>
#ifdef HAVE_THREADS
#include <rthreads/rthreads.h>
#endif
#include <retro_common_api.h>
#include <file/config_file.h>
#include <features/features_cpu.h>
#include <file/file_path.h>
#include "video_thread_wrapper.h"
#include "../frontend/frontend_driver.h"
#include "video_context_driver.h"
#include "../record/record_driver.h"
#include "../config.def.h"
#include "../retroarch.h"
#include "../runloop.h"
#include "../performance_counters.h"
#include "../list_special.h"
#include "../core.h"
#include "../system.h"
#include "../command.h"
#include "../msg_hash.h"
#ifdef HAVE_MENU
#include "../menu/menu_setting.h"
#endif
#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
typedef struct video_driver_state
{
struct
{
retro_time_t samples[MEASURE_FRAME_TIME_SAMPLES_COUNT];
uint64_t count;
} frame_time;
enum retro_pixel_format pix_fmt;
unsigned video_width;
unsigned video_height;
float aspect_ratio;
struct
{
const void *data;
unsigned width;
unsigned height;
size_t pitch;
} frame_cache;
struct
{
rarch_softfilter_t *filter;
void *buffer;
unsigned scale;
unsigned out_bpp;
bool out_rgb32;
} filter;
} video_driver_state_t;
typedef struct video_pixel_scaler
{
struct scaler_ctx *scaler;
void *scaler_out;
} video_pixel_scaler_t;
/* 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;
static uintptr_t video_driver_window;
static enum rarch_display_type video_driver_display_type;
static char video_driver_title_buf[64];
static uint64_t video_driver_frame_count;
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;
static video_driver_state_t video_driver_state;
/* Used for 16-bit -> 16-bit conversions that take place before
* being passed to video driver. */
static video_pixel_scaler_t *video_driver_scaler_ptr = NULL;
char rotation_lut[4][32] =
{
"Normal",
"90 deg",
"180 deg",
"270 deg"
};
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 },
{ "1:1", 1.0f },
{ "2:1", 2.0f },
{ "3:2", 1.5f },
{ "3:4", 0.75f },
{ "4:1", 4.0f },
{ "4:4", 1.0f },
{ "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_VULKAN
&video_vulkan,
#endif
#ifdef HAVE_OPENGL
&video_gl,
#endif
#ifdef XENON
&video_xenon360,
#endif
#if defined(HAVE_D3D)
&video_d3d,
#endif
#ifdef HAVE_VITA2D
&video_vita2d,
#endif
#ifdef PSP
&video_psp1,
#endif
#ifdef _3DS
&video_ctr,
#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 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
&video_null,
NULL,
};
/**
* 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);
}
static bool hw_render_context_is_vulkan(enum retro_hw_context_type type)
{
return type == RETRO_HW_CONTEXT_VULKAN;
}
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;
}
/**
* 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
settings_t *settings = config_get_ptr();
if (settings->video.threaded
&& !video_driver_is_hw_context()
&& !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;
}
/**
* 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 0;
return video_driver_poke->get_current_framebuffer(video_driver_data);
}
retro_proc_address_t video_driver_get_proc_address(const char *sym)
{
if (!video_driver_poke || !video_driver_poke->get_proc_address)
return NULL;
return video_driver_poke->get_proc_address(video_driver_data, sym);
}
bool video_driver_set_shader(enum rarch_shader_type type,
const char *path)
{
if (!current_video->set_shader)
return false;
return current_video->set_shader(video_driver_data, type, path);
}
static void deinit_video_filter(void)
{
rarch_softfilter_free(video_driver_state.filter.filter);
#ifdef _3DS
linearFree(video_driver_state.filter.buffer);
#else
free(video_driver_state.filter.buffer);
#endif
memset(&video_driver_state.filter, 0,
sizeof(video_driver_state.filter));
}
static void init_video_filter(enum retro_pixel_format colfmt)
{
unsigned width, height, pow2_x, pow2_y, maxsize;
struct retro_game_geometry *geom = NULL;
settings_t *settings = config_get_ptr();
struct retro_system_av_info *av_info =
video_viewport_get_system_av_info();
deinit_video_filter();
if (!*settings->path.softfilter_plugin)
return;
/* Deprecated format. Gets pre-converted. */
if (colfmt == RETRO_PIXEL_FORMAT_0RGB1555)
colfmt = RETRO_PIXEL_FORMAT_RGB565;
if (video_driver_is_hw_context())
{
RARCH_WARN("Cannot use CPU filters when hardware rendering is used.\n");
return;
}
if (av_info)
geom = (struct retro_game_geometry*)&av_info->geometry;
if (!geom)
return;
width = geom->max_width;
height = geom->max_height;
video_driver_state.filter.filter = rarch_softfilter_new(
settings->path.softfilter_plugin,
RARCH_SOFTFILTER_THREADS_AUTO, colfmt, width, height);
if (!video_driver_state.filter.filter)
{
RARCH_ERR("Failed to load filter.\n");
return;
}
rarch_softfilter_get_max_output_size(video_driver_state.filter.filter,
&width, &height);
pow2_x = next_pow2(width);
pow2_y = next_pow2(height);
maxsize = MAX(pow2_x, pow2_y);
video_driver_state.filter.scale = maxsize / RARCH_SCALE_BASE;
video_driver_state.filter.out_rgb32 = rarch_softfilter_get_output_format(
video_driver_state.filter.filter) == RETRO_PIXEL_FORMAT_XRGB8888;
video_driver_state.filter.out_bpp =
video_driver_state.filter.out_rgb32 ?
sizeof(uint32_t) : sizeof(uint16_t);
/* TODO: Aligned output. */
#ifdef _3DS
video_driver_state.filter.buffer = linearMemAlign(width
* height * video_driver_state.filter.out_bpp, 0x80);
#else
video_driver_state.filter.buffer = malloc(width
* height * video_driver_state.filter.out_bpp);
#endif
if (!video_driver_state.filter.buffer)
goto error;
return;
error:
RARCH_ERR("Softfilter initialization failed.\n");
deinit_video_filter();
}
static void init_video_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("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("Cannot initialize input driver. Exiting ...\n");
retroarch_fail(1, "init_video_input()");
}
/**
* video_monitor_compute_fps_statistics:
*
* Computes monitor FPS statistics.
**/
static void video_monitor_compute_fps_statistics(void)
{
double avg_fps = 0.0;
double stddev = 0.0;
unsigned samples = 0;
settings_t *settings = config_get_ptr();
if (settings->video.threaded)
{
RARCH_LOG("Monitor FPS estimation is disabled for threaded video.\n");
return;
}
if (video_driver_state.frame_time.count <
(2 * MEASURE_FRAME_TIME_SAMPLES_COUNT))
{
RARCH_LOG(
"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("Average monitor Hz: %.6f Hz. (%.3f %% frame time deviation, based on %u last samples).\n",
avg_fps, 100.0 * stddev, samples);
}
}
static void deinit_pixel_converter(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 bool uninit_video_input(void)
{
command_event(CMD_EVENT_OVERLAY_DEINIT, NULL);
if (!video_driver_is_video_cache_context())
video_driver_deinit_hw_context();
if (
!input_driver_owns_driver() &&
!input_driver_is_data_ptr_same(video_driver_data)
)
input_driver_deinit();
if (
!video_driver_owns_driver()
&& video_driver_data
&& current_video && current_video->free
)
current_video->free(video_driver_data);
deinit_pixel_converter();
deinit_video_filter();
command_event(CMD_EVENT_SHADER_DIR_DEINIT, NULL);
video_monitor_compute_fps_statistics();
return true;
}
static bool init_video_pixel_converter(unsigned size)
{
struct retro_hw_render_callback *hwr =
video_driver_get_hw_context();
/* If pixel format is not 0RGB1555, we don't need to do
* any internal pixel conversion. */
if (video_driver_get_pixel_format() != 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");
video_driver_scaler_ptr = (video_pixel_scaler_t*)
calloc(1, sizeof(*video_driver_scaler_ptr));
if (!video_driver_scaler_ptr)
goto error;
video_driver_scaler_ptr->scaler = (struct scaler_ctx*)
calloc(1, sizeof(*video_driver_scaler_ptr->scaler));
if (!video_driver_scaler_ptr->scaler)
goto error;
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(video_driver_scaler_ptr->scaler))
goto error;
video_driver_scaler_ptr->scaler_out =
calloc(sizeof(uint16_t), size * size);
if (!video_driver_scaler_ptr->scaler_out)
goto error;
return true;
error:
deinit_pixel_converter();
deinit_video_filter();
return false;
}
static bool init_video(void)
{
unsigned max_dim, scale, width, height;
video_viewport_t *custom_vp = NULL;
const input_driver_t *tmp = NULL;
const struct retro_game_geometry *geom = NULL;
rarch_system_info_t *system = NULL;
video_info_t video = {0};
static uint16_t dummy_pixels[32] = {0};
settings_t *settings = config_get_ptr();
struct retro_system_av_info *av_info =
video_viewport_get_system_av_info();
runloop_ctl(RUNLOOP_CTL_SYSTEM_INFO_GET, &system);
init_video_filter(video_driver_state.pix_fmt);
command_event(CMD_EVENT_SHADER_DIR_INIT, NULL);
if (av_info)
geom = (const struct retro_game_geometry*)&av_info->geometry;
if (!geom)
{
RARCH_ERR("AV geometry not initialized, cannot initialize video driver.\n");
goto error;
}
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.filter)
scale = video_driver_state.filter.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->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->video.aspect_ratio_idx].value);
if (settings->video.fullscreen)
{
width = settings->video.fullscreen_x;
height = settings->video.fullscreen_y;
}
else
{
if (settings->video.force_aspect)
{
/* Do rounding here to simplify integer scale correctness. */
unsigned base_width =
roundf(geom->base_height * video_driver_get_aspect_ratio());
width = roundf(base_width * settings->video.scale);
}
else
width = roundf(geom->base_width * settings->video.scale);
height = roundf(geom->base_height * settings->video.scale);
}
if (width && height)
RARCH_LOG("Video @ %ux%u\n", width, height);
else
RARCH_LOG("Video @ fullscreen\n");
video_driver_display_type_set(RARCH_DISPLAY_NONE);
video_driver_display_set(0);
video_driver_window_set(0);
if (!init_video_pixel_converter(RARCH_SCALE_BASE * scale))
{
RARCH_ERR("Failed to initialize pixel converter.\n");
goto error;
}
video.width = width;
video.height = height;
video.fullscreen = settings->video.fullscreen;
video.vsync = settings->video.vsync && !runloop_ctl(RUNLOOP_CTL_IS_NONBLOCK_FORCED, NULL);
video.force_aspect = settings->video.force_aspect;
#ifdef GEKKO
video.viwidth = settings->video.viwidth;
video.vfilter = settings->video.vfilter;
#endif
video.smooth = settings->video.smooth;
video.input_scale = scale;
video.rgb32 = video_driver_state.filter.filter ?
video_driver_state.filter.out_rgb32 :
(video_driver_state.pix_fmt == RETRO_PIXEL_FORMAT_XRGB8888);
/* Reset video frame count */
video_driver_frame_count = 0;
tmp = input_get_ptr();
/* Need to grab the "real" video driver interface on a reinit. */
video_driver_find_driver();
#ifdef HAVE_THREADS
if (settings->video.threaded
&& !video_driver_is_hw_context())
{
/* Can't do hardware rendering with threaded driver currently. */
RARCH_LOG("Starting threaded video driver ...\n");
if (!video_init_thread((const video_driver_t**)¤t_video,
&video_driver_data,
input_get_double_ptr(), input_driver_get_data_ptr(),
current_video, &video))
{
RARCH_ERR("Cannot open threaded video driver ... Exiting ...\n");
goto error;
}
}
else
#endif
video_driver_data = current_video->init(&video, input_get_double_ptr(),
input_driver_get_data_ptr());
if (!video_driver_data)
{
RARCH_ERR("Cannot open video driver ... Exiting ...\n");
goto error;
}
video_driver_poke = NULL;
if (current_video->poke_interface)
current_video->poke_interface(video_driver_data, &video_driver_poke);
if (current_video->viewport_info && (!custom_vp->width ||
!custom_vp->height))
{
/* Force custom viewport to have sane parameters. */
custom_vp->width = width;
custom_vp->height = height;
video_driver_get_viewport_info(custom_vp);
}
video_driver_set_rotation(
(settings->video.rotation + system->rotation) % 4);
current_video->suppress_screensaver(video_driver_data,
settings->ui.suspend_screensaver_enable);
init_video_input(tmp);
command_event(CMD_EVENT_OVERLAY_DEINIT, NULL);
command_event(CMD_EVENT_OVERLAY_INIT, NULL);
video_driver_cached_frame_set(&dummy_pixels, 4, 4, 8);
#if defined(PSP)
video_driver_set_texture_frame(&dummy_pixels, false, 1, 1, 1.0f);
#endif
return true;
error:
retroarch_fail(1, "init_video()");
return false;
}
bool video_driver_set_viewport(unsigned width, unsigned height,
bool force_fullscreen, bool allow_rotate)
{
if (!current_video || !current_video->set_viewport)
return false;
current_video->set_viewport(video_driver_data, width, height,
force_fullscreen, allow_rotate);
return true;
}
bool video_driver_set_rotation(unsigned rotation)
{
if (!current_video || !current_video->set_rotation)
return false;
current_video->set_rotation(video_driver_data, rotation);
return true;
}
bool video_driver_set_video_mode(unsigned width,
unsigned height, bool fullscreen)
{
gfx_ctx_mode_t mode;
if (video_driver_poke && video_driver_poke->set_video_mode)
{
video_driver_poke->set_video_mode(video_driver_data,
width, height, fullscreen);
return true;
}
mode.width = width;
mode.height = height;
mode.fullscreen = fullscreen;
return video_context_driver_set_video_mode(&mode);
}
bool video_driver_get_video_output_size(unsigned *width, unsigned *height)
{
if (!video_driver_poke || !video_driver_poke->get_video_output_size)
return false;
video_driver_poke->get_video_output_size(video_driver_data,
width, height);
return true;
}
void video_driver_set_osd_msg(const char *msg,
const struct font_params *params, void *font)
{
if (video_driver_poke && video_driver_poke->set_osd_msg)
video_driver_poke->set_osd_msg(video_driver_data, msg, params, font);
}
void video_driver_set_texture_enable(bool enable, bool fullscreen)
{
if (video_driver_poke && video_driver_poke->set_texture_enable)
video_driver_poke->set_texture_enable(video_driver_data,
enable, fullscreen);
}
void video_driver_set_texture_frame(const void *frame, bool rgb32,
unsigned width, unsigned height, float alpha)
{
#ifdef HAVE_MENU
if (video_driver_poke && video_driver_poke->set_texture_frame)
video_driver_poke->set_texture_frame(video_driver_data,
frame, rgb32, width, height, alpha);
#endif
}
#ifdef HAVE_OVERLAY
bool video_driver_overlay_interface(const video_overlay_interface_t **iface)
{
if (!current_video || !current_video->overlay_interface)
return false;
current_video->overlay_interface(video_driver_data, iface);
return true;
}
#endif
void *video_driver_read_frame_raw(unsigned *width,
unsigned *height, size_t *pitch)
{
if (!current_video || !current_video->read_frame_raw)
return NULL;
return current_video->read_frame_raw(video_driver_data, width,
height, pitch);
}
void video_driver_set_filtering(unsigned index, bool smooth)
{
if (video_driver_poke && video_driver_poke->set_filtering)
video_driver_poke->set_filtering(video_driver_data, index, smooth);
}
void video_driver_cached_frame_set(const void *data, unsigned width,
unsigned height, size_t pitch)
{
video_driver_set_cached_frame_ptr(data);
video_driver_state.frame_cache.width = width;
video_driver_state.frame_cache.height = height;
video_driver_state.frame_cache.pitch = pitch;
}
void video_driver_cached_frame_get(const void **data, unsigned *width,
unsigned *height, size_t *pitch)
{
if (data)
*data = video_driver_state.frame_cache.data;
if (width)
*width = video_driver_state.frame_cache.width;
if (height)
*height = video_driver_state.frame_cache.height;
if (pitch)
*pitch = video_driver_state.frame_cache.pitch;
}
void video_driver_get_size(unsigned *width, unsigned *height)
{
if (width)
*width = video_driver_state.video_width;
if (height)
*height = video_driver_state.video_height;
}
void video_driver_set_size(unsigned *width, unsigned *height)
{
if (width)
video_driver_state.video_width = *width;
if (height)
video_driver_state.video_height = *height;
}
/**
* video_monitor_set_refresh_rate:
* @hz : New refresh rate for monitor.
*
* Sets monitor refresh rate to new value.
**/
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);
runloop_msg_queue_push(msg, 1, 180, false);
RARCH_LOG("%s\n", msg);
settings->video.refresh_rate = hz;
}
/**
* video_monitor_fps_statistics
* @refresh_rate : Monitor refresh rate.
* @deviation : Deviation from measured refresh rate.
* @sample_points : Amount of sampled points.
*
* Gets the monitor FPS statistics based on the current
* runtime.
*
* Returns: true (1) on success.
* false (0) if:
* a) threaded video mode is enabled
* b) less than 2 frame time samples.
* c) FPS monitor enable is off.
**/
bool video_monitor_fps_statistics(double *refresh_rate,
double *deviation, unsigned *sample_points)
{
unsigned i;
retro_time_t accum = 0, avg, accum_var = 0;
settings_t *settings = config_get_ptr();
unsigned samples = MIN(MEASURE_FRAME_TIME_SAMPLES_COUNT,
video_driver_state.frame_time.count);
if (settings->video.threaded || (samples < 2))
return false;
/* Measure statistics on frame time (microsecs), *not* FPS. */
for (i = 0; i < samples; i++)
accum += video_driver_state.frame_time.samples[i];
#if 0
for (i = 0; i < samples; i++)
RARCH_LOG("Interval #%u: %d usec / frame.\n",
i, (int)video_driver_state.frame_time.samples[i]);
#endif
avg = accum / samples;
/* Drop first measurement. It is likely to be bad. */
for (i = 0; i < samples; i++)
{
retro_time_t diff = video_driver_state.frame_time.samples[i] - avg;
accum_var += diff * diff;
}
*deviation = sqrt((double)accum_var / (samples - 1)) / avg;
*refresh_rate = 1000000.0 / avg;
*sample_points = samples;
return true;
}
/**
* video_monitor_get_fps:
* @buf : string suitable for Window title
* @size : size of buffer.
* @buf_fps : string of raw FPS only (optional).
* @size_fps : size of raw FPS buffer.
*
* Get the amount of frames per seconds.
*
* Returns: true if framerate per seconds could be obtained,
* otherwise false.
*
**/
bool video_monitor_get_fps(char *buf, size_t size,
char *buf_fps, size_t size_fps)
{
static retro_time_t curr_time;
static retro_time_t fps_time;
retro_time_t new_time = cpu_features_get_time_usec();
*buf = '\0';
if (video_driver_frame_count)
{
static float last_fps;
bool ret = false;
settings_t *settings = config_get_ptr();
unsigned write_index = video_driver_state.frame_time.count++ &
(MEASURE_FRAME_TIME_SAMPLES_COUNT - 1);
video_driver_state.frame_time.samples[write_index] = new_time - fps_time;
fps_time = new_time;
if ((video_driver_frame_count % FPS_UPDATE_INTERVAL) == 0)
{