1 /*****************************************************************************
2 * display.c: "vout display" managment
3 *****************************************************************************
4 * Copyright (C) 2009 Laurent Aimar
7 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_video_splitter.h>
34 #include <vlc_vout_display.h>
43 /* It must be present as long as a vout_display_t must be created using a dummy
44 * vout (as an opengl provider) */
45 #define ALLOW_DUMMY_VOUT
47 static void SplitterClose(vout_display_t *vd);
49 /*****************************************************************************
50 * FIXME/TODO see how to have direct rendering here (interact with vout.c)
51 *****************************************************************************/
52 static picture_t *VideoBufferNew(filter_t *filter)
54 vout_display_t *vd = (vout_display_t*)filter->p_owner;
55 const video_format_t *fmt = &filter->fmt_out.video;
57 assert(vd->fmt.i_chroma == fmt->i_chroma &&
58 vd->fmt.i_width == fmt->i_width &&
59 vd->fmt.i_height == fmt->i_height);
61 picture_pool_t *pool = vout_display_Pool(vd, 3);
64 return picture_pool_Get(pool);
66 static void VideoBufferDelete(filter_t *filter, picture_t *picture)
69 picture_Release(picture);
72 static int FilterAllocationInit(filter_t *filter, void *vd)
74 filter->pf_video_buffer_new = VideoBufferNew;
75 filter->pf_video_buffer_del = VideoBufferDelete;
80 static void FilterAllocationClean(filter_t *filter)
82 filter->pf_video_buffer_new = NULL;
83 filter->pf_video_buffer_del = NULL;
84 filter->p_owner = NULL;
87 /*****************************************************************************
89 *****************************************************************************/
92 * It creates a new vout_display_t using the given configuration.
94 static vout_display_t *vout_display_New(vlc_object_t *obj,
95 const char *module, bool load_module,
96 const video_format_t *fmt,
97 const vout_display_cfg_t *cfg,
98 vout_display_owner_t *owner)
101 vout_display_t *vd = vlc_object_create(obj, sizeof(*vd));
104 video_format_Copy(&vd->source, fmt);
106 /* Picture buffer does not have the concept of aspect ratio */
107 video_format_Copy(&vd->fmt, fmt);
108 vd->fmt.i_sar_num = 0;
109 vd->fmt.i_sar_den = 0;
111 vd->info.is_slow = false;
112 vd->info.has_double_click = false;
113 vd->info.has_hide_mouse = false;
114 vd->info.has_pictures_invalid = false;
126 vlc_object_attach(vd, obj);
129 vd->module = module_need(vd, "vout display", module, module && *module != '\0');
131 vlc_object_release(vd);
141 * It deletes a vout_display_t
143 static void vout_display_Delete(vout_display_t *vd)
146 module_unneed(vd, vd->module);
148 vlc_object_release(vd);
152 * It controls a vout_display_t
154 static int vout_display_Control(vout_display_t *vd, int query, ...)
159 va_start(args, query);
160 result = vd->control(vd, query, args);
165 static void vout_display_Manage(vout_display_t *vd)
172 void vout_display_GetDefaultDisplaySize(unsigned *width, unsigned *height,
173 const video_format_t *source,
174 const vout_display_cfg_t *cfg)
176 if (cfg->display.width > 0 && cfg->display.height > 0) {
177 *width = cfg->display.width;
178 *height = cfg->display.height;
179 } else if (cfg->display.width > 0) {
180 *width = cfg->display.width;
181 *height = (int64_t)source->i_visible_height * source->i_sar_den * cfg->display.width * cfg->display.sar.num /
182 source->i_visible_width / source->i_sar_num / cfg->display.sar.den;
183 } else if (cfg->display.height > 0) {
184 *width = (int64_t)source->i_visible_width * source->i_sar_num * cfg->display.height * cfg->display.sar.den /
185 source->i_visible_height / source->i_sar_den / cfg->display.sar.num;
186 *height = cfg->display.height;
187 } else if (source->i_sar_num >= source->i_sar_den) {
188 *width = (int64_t)source->i_visible_width * source->i_sar_num * cfg->display.sar.den / source->i_sar_den / cfg->display.sar.num;
189 *height = source->i_visible_height;
191 *width = source->i_visible_width;
192 *height = (int64_t)source->i_visible_height * source->i_sar_den * cfg->display.sar.num / source->i_sar_num / cfg->display.sar.den;
195 *width = *width * cfg->zoom.num / cfg->zoom.den;
196 *height = *height * cfg->zoom.num / cfg->zoom.den;
200 void vout_display_PlacePicture(vout_display_place_t *place,
201 const video_format_t *source,
202 const vout_display_cfg_t *cfg,
206 memset(place, 0, sizeof(*place));
207 if (cfg->display.width <= 0 || cfg->display.height <= 0)
211 unsigned display_width;
212 unsigned display_height;
214 if (cfg->is_display_filled) {
215 display_width = cfg->display.width;
216 display_height = cfg->display.height;
218 vout_display_cfg_t cfg_tmp = *cfg;
220 cfg_tmp.display.width = 0;
221 cfg_tmp.display.height = 0;
222 vout_display_GetDefaultDisplaySize(&display_width, &display_height,
226 display_width = __MIN(display_width, cfg->display.width);
227 display_height = __MIN(display_height, cfg->display.height);
231 const unsigned width = source->i_visible_width;
232 const unsigned height = source->i_visible_height;
233 /* Compute the height if we use the width to fill up display_width */
234 const int64_t scaled_height = (int64_t)height * display_width * cfg->display.sar.num * source->i_sar_den / width / source->i_sar_num / cfg->display.sar.den;
235 /* And the same but switching width/height */
236 const int64_t scaled_width = (int64_t)width * display_height * cfg->display.sar.den * source->i_sar_num / height / source->i_sar_den / cfg->display.sar.num;
238 /* We keep the solution that avoid filling outside the display */
239 if (scaled_width <= cfg->display.width) {
240 place->width = scaled_width;
241 place->height = display_height;
243 place->width = display_width;
244 place->height = scaled_height;
247 /* Compute position */
248 switch (cfg->align.horizontal) {
249 case VOUT_DISPLAY_ALIGN_LEFT:
252 case VOUT_DISPLAY_ALIGN_RIGHT:
253 place->x = cfg->display.width - place->width;
256 place->x = (cfg->display.width - place->width) / 2;
260 switch (cfg->align.vertical) {
261 case VOUT_DISPLAY_ALIGN_TOP:
264 case VOUT_DISPLAY_ALIGN_BOTTOM:
265 place->y = cfg->display.height - place->height;
268 place->y = (cfg->display.height - place->height) / 2;
273 struct vout_display_owner_sys_t {
275 bool is_wrapper; /* Is the current display a wrapper */
276 vout_display_t *wrapper; /* Vout display wrapper */
279 vout_display_cfg_t cfg;
280 unsigned wm_state_initial;
296 bool ch_display_filled;
297 bool is_display_filled;
325 video_format_t source;
326 filter_chain_t *filters;
328 /* Lock protecting the variables used by
329 * VoutDisplayEvent(ie vout_display_SendEvent) */
336 mtime_t last_pressed;
342 mtime_t double_click_timeout;
343 mtime_t hide_timeout;
351 bool ch_display_size;
354 bool display_is_fullscreen;
355 bool display_is_forced;
359 #ifdef ALLOW_DUMMY_VOUT
360 vlc_mouse_t vout_mouse;
364 static void DummyVoutSendDisplayEventMouse(vout_thread_t *, vlc_mouse_t *fallback, const vlc_mouse_t *m);
366 static void VoutDisplayCreateRender(vout_display_t *vd)
368 vout_display_owner_sys_t *osys = vd->owner.sys;
370 osys->filters = NULL;
372 video_format_t v_src = vd->source;
376 video_format_t v_dst = vd->fmt;
380 video_format_t v_dst_cmp = v_dst;
381 if ((v_src.i_chroma == VLC_CODEC_J420 && v_dst.i_chroma == VLC_CODEC_I420) ||
382 (v_src.i_chroma == VLC_CODEC_J422 && v_dst.i_chroma == VLC_CODEC_I422) ||
383 (v_src.i_chroma == VLC_CODEC_J440 && v_dst.i_chroma == VLC_CODEC_I440) ||
384 (v_src.i_chroma == VLC_CODEC_J444 && v_dst.i_chroma == VLC_CODEC_I444))
385 v_dst_cmp.i_chroma = v_src.i_chroma;
387 const bool convert = memcmp(&v_src, &v_dst_cmp, sizeof(v_src)) != 0;
391 msg_Dbg(vd, "A filter to adapt decoder to display is needed");
393 osys->filters = filter_chain_New(vd, "video filter2", false,
394 FilterAllocationInit,
395 FilterAllocationClean, vd);
396 assert(osys->filters); /* TODO critical */
400 es_format_InitFromVideo(&src, &v_src);
406 for (int i = 0; i < 1 + (v_dst_cmp.i_chroma != v_dst.i_chroma); i++) {
408 es_format_InitFromVideo(&dst, i == 0 ? &v_dst : &v_dst_cmp);
410 filter_chain_Reset(osys->filters, &src, &dst);
411 filter = filter_chain_AppendFilter(osys->filters,
412 NULL, NULL, &src, &dst);
418 msg_Err(vd, "VoutDisplayCreateRender FAILED");
424 static void VoutDisplayDestroyRender(vout_display_t *vd)
426 vout_display_owner_sys_t *osys = vd->owner.sys;
429 filter_chain_Delete(osys->filters);
432 static void VoutDisplayResetRender(vout_display_t *vd)
434 VoutDisplayDestroyRender(vd);
435 VoutDisplayCreateRender(vd);
437 static void VoutDisplayEventMouse(vout_display_t *vd, int event, va_list args)
439 vout_display_owner_sys_t *osys = vd->owner.sys;
441 vlc_mutex_lock(&osys->lock);
444 vlc_mouse_t m = osys->mouse.state;
445 bool is_ignored = false;
448 case VOUT_DISPLAY_EVENT_MOUSE_STATE: {
449 const int x = (int)va_arg(args, int);
450 const int y = (int)va_arg(args, int);
451 const int button_mask = (int)va_arg(args, int);
456 m.i_pressed = button_mask;
459 case VOUT_DISPLAY_EVENT_MOUSE_MOVED: {
460 const int x = (int)va_arg(args, int);
461 const int y = (int)va_arg(args, int);
462 if (x != osys->mouse.state.i_x || y != osys->mouse.state.i_y) {
463 //msg_Dbg(vd, "VoutDisplayEvent 'mouse' @%d,%d", x, y);
467 m.b_double_click = false;
473 case VOUT_DISPLAY_EVENT_MOUSE_PRESSED:
474 case VOUT_DISPLAY_EVENT_MOUSE_RELEASED: {
475 const int button = (int)va_arg(args, int);
476 const int button_mask = 1 << button;
478 /* Ignore inconsistent event */
479 if ((event == VOUT_DISPLAY_EVENT_MOUSE_PRESSED && (osys->mouse.state.i_pressed & button_mask)) ||
480 (event == VOUT_DISPLAY_EVENT_MOUSE_RELEASED && !(osys->mouse.state.i_pressed & button_mask))) {
486 msg_Dbg(vd, "VoutDisplayEvent 'mouse button' %d t=%d", button, event);
488 m.b_double_click = false;
489 if (event == VOUT_DISPLAY_EVENT_MOUSE_PRESSED)
490 m.i_pressed |= button_mask;
492 m.i_pressed &= ~button_mask;
495 case VOUT_DISPLAY_EVENT_MOUSE_DOUBLE_CLICK:
496 msg_Dbg(vd, "VoutDisplayEvent 'double click'");
498 m.b_double_click = true;
505 vlc_mutex_unlock(&osys->lock);
509 /* Emulate double-click if needed */
510 if (!vd->info.has_double_click &&
511 vlc_mouse_HasPressed(&osys->mouse.state, &m, MOUSE_BUTTON_LEFT)) {
512 const mtime_t i_date = mdate();
514 if (i_date - osys->mouse.last_pressed < osys->mouse.double_click_timeout ) {
515 m.b_double_click = true;
516 osys->mouse.last_pressed = 0;
518 osys->mouse.last_pressed = mdate();
523 osys->mouse.state = m;
526 osys->mouse.ch_activity = true;
527 if (!vd->info.has_hide_mouse)
528 osys->mouse.last_moved = mdate();
531 vout_SendEventMouseVisible(osys->vout);
532 #ifdef ALLOW_DUMMY_VOUT
533 DummyVoutSendDisplayEventMouse(osys->vout, &osys->vout_mouse, &m);
535 vout_SendDisplayEventMouse(osys->vout, &m);
537 vlc_mutex_unlock(&osys->lock);
540 static void VoutDisplayEvent(vout_display_t *vd, int event, va_list args)
542 vout_display_owner_sys_t *osys = vd->owner.sys;
545 case VOUT_DISPLAY_EVENT_CLOSE: {
546 msg_Dbg(vd, "VoutDisplayEvent 'close'");
547 vout_SendEventClose(osys->vout);
550 case VOUT_DISPLAY_EVENT_KEY: {
551 const int key = (int)va_arg(args, int);
552 msg_Dbg(vd, "VoutDisplayEvent 'key' 0x%2.2x", key);
553 vout_SendEventKey(osys->vout, key);
556 case VOUT_DISPLAY_EVENT_MOUSE_STATE:
557 case VOUT_DISPLAY_EVENT_MOUSE_MOVED:
558 case VOUT_DISPLAY_EVENT_MOUSE_PRESSED:
559 case VOUT_DISPLAY_EVENT_MOUSE_RELEASED:
560 case VOUT_DISPLAY_EVENT_MOUSE_DOUBLE_CLICK:
561 VoutDisplayEventMouse(vd, event, args);
564 case VOUT_DISPLAY_EVENT_FULLSCREEN: {
565 const int is_fullscreen = (int)va_arg(args, int);
567 msg_Dbg(vd, "VoutDisplayEvent 'fullscreen' %d", is_fullscreen);
569 vlc_mutex_lock(&osys->lock);
570 if (!is_fullscreen != !osys->is_fullscreen) {
571 osys->ch_fullscreen = true;
572 osys->is_fullscreen = is_fullscreen;
574 vlc_mutex_unlock(&osys->lock);
578 case VOUT_DISPLAY_EVENT_WINDOW_STATE: {
579 const unsigned state = va_arg(args, unsigned);
581 msg_Dbg(vd, "VoutDisplayEvent 'window state' %u", state);
583 vlc_mutex_lock(&osys->lock);
584 if (state != osys->wm_state) {
585 osys->ch_wm_state = true;
586 osys->wm_state = state;
588 vlc_mutex_unlock(&osys->lock);
592 case VOUT_DISPLAY_EVENT_DISPLAY_SIZE: {
593 const int width = (int)va_arg(args, int);
594 const int height = (int)va_arg(args, int);
595 const bool is_fullscreen = (bool)va_arg(args, int);
596 msg_Dbg(vd, "VoutDisplayEvent 'resize' %dx%d %s",
597 width, height, is_fullscreen ? "fullscreen" : "window");
600 vlc_mutex_lock(&osys->lock);
602 osys->ch_display_size = true;
603 osys->display_width = width;
604 osys->display_height = height;
605 osys->display_is_fullscreen = is_fullscreen;
606 osys->display_is_forced = false;
608 vlc_mutex_unlock(&osys->lock);
612 case VOUT_DISPLAY_EVENT_PICTURES_INVALID: {
613 msg_Warn(vd, "VoutDisplayEvent 'pictures invalid'");
616 assert(vd->info.has_pictures_invalid);
618 vlc_mutex_lock(&osys->lock);
619 osys->reset_pictures = true;
620 vlc_mutex_unlock(&osys->lock);
624 msg_Err(vd, "VoutDisplayEvent received event %d", event);
625 /* TODO add an assert when all event are handled */
630 static vout_window_t *VoutDisplayNewWindow(vout_display_t *vd, const vout_window_cfg_t *cfg)
632 vout_display_owner_sys_t *osys = vd->owner.sys;
634 return vout_NewDisplayWindow(osys->vout, vd, cfg);
636 static void VoutDisplayDelWindow(vout_display_t *vd, vout_window_t *window)
638 vout_display_owner_sys_t *osys = vd->owner.sys;
640 vout_DeleteDisplayWindow(osys->vout, vd, window);
643 static void VoutDisplayFitWindow(vout_display_t *vd, bool default_size)
645 vout_display_owner_sys_t *osys = vd->owner.sys;
646 vout_display_cfg_t cfg = osys->cfg;
648 if (!cfg.is_display_filled)
651 cfg.display.width = 0;
653 cfg.display.height = 0;
655 cfg.display.height = osys->height_saved;
660 unsigned display_width;
661 unsigned display_height;
662 vout_display_GetDefaultDisplaySize(&display_width, &display_height,
665 vlc_mutex_lock(&osys->lock);
667 osys->ch_display_size = true;
668 osys->display_width = display_width;
669 osys->display_height = display_height;
670 osys->display_is_fullscreen = osys->cfg.is_fullscreen;
671 osys->display_is_forced = true;
673 vlc_mutex_unlock(&osys->lock);
676 static void VoutDisplayCropRatio(unsigned *x, unsigned *y,
677 unsigned *width, unsigned *height,
678 const video_format_t *source,
679 unsigned num, unsigned den)
681 unsigned scaled_width = (uint64_t)source->i_visible_height * num * source->i_sar_den / den / source->i_sar_num;
682 unsigned scaled_height = (uint64_t)source->i_visible_width * den * source->i_sar_num / num / source->i_sar_den;
684 if (scaled_width < source->i_visible_width) {
685 *x = (source->i_visible_width - scaled_width) / 2;
687 *width = scaled_width;
688 *height = source->i_visible_height;
691 *y = (source->i_visible_height - scaled_height) / 2;
692 *width = source->i_visible_width;
693 *height = scaled_height;
695 *x += source->i_x_offset;
696 *y += source->i_y_offset;
699 void vout_ManageDisplay(vout_display_t *vd, bool allow_reset_pictures)
701 vout_display_owner_sys_t *osys = vd->owner.sys;
703 vout_display_Manage(vd);
705 /* Handle mouse timeout */
706 const mtime_t date = mdate();
707 bool hide_mouse = false;
709 vlc_mutex_lock(&osys->lock);
711 if (!osys->mouse.is_hidden &&
712 osys->mouse.last_moved + osys->mouse.hide_timeout < date) {
713 osys->mouse.is_hidden = hide_mouse = true;
714 } else if (osys->mouse.ch_activity) {
715 osys->mouse.is_hidden = false;
717 osys->mouse.ch_activity = false;
718 vlc_mutex_unlock(&osys->lock);
721 if (!vd->info.has_hide_mouse) {
722 msg_Dbg(vd, "auto hidding mouse");
723 vout_display_Control(vd, VOUT_DISPLAY_HIDE_MOUSE);
725 vout_SendEventMouseHidden(osys->vout);
728 bool reset_render = false;
731 vlc_mutex_lock(&osys->lock);
733 bool ch_fullscreen = osys->ch_fullscreen;
734 bool is_fullscreen = osys->is_fullscreen;
735 osys->ch_fullscreen = false;
737 bool ch_wm_state = osys->ch_wm_state;
738 unsigned wm_state = osys->wm_state;
739 osys->ch_wm_state = false;
741 bool ch_display_size = osys->ch_display_size;
742 int display_width = osys->display_width;
743 int display_height = osys->display_height;
744 bool display_is_fullscreen = osys->display_is_fullscreen;
745 bool display_is_forced = osys->display_is_forced;
746 osys->ch_display_size = false;
749 if (allow_reset_pictures) {
750 reset_pictures = osys->reset_pictures;
751 osys->reset_pictures = false;
753 reset_pictures = false;
756 vlc_mutex_unlock(&osys->lock);
758 if (!ch_fullscreen &&
761 !osys->ch_display_filled &&
767 if (!osys->cfg.is_fullscreen && osys->fit_window != 0) {
768 VoutDisplayFitWindow(vd, osys->fit_window == -1);
769 osys->fit_window = 0;
777 vout_display_cfg_t cfg = osys->cfg;
779 cfg.is_fullscreen = is_fullscreen;
780 cfg.display.width = cfg.is_fullscreen ? 0 : osys->width_saved;
781 cfg.display.height = cfg.is_fullscreen ? 0 : osys->height_saved;
783 if (vout_display_Control(vd, VOUT_DISPLAY_CHANGE_FULLSCREEN, &cfg)) {
784 msg_Err(vd, "Failed to set fullscreen");
785 is_fullscreen = osys->cfg.is_fullscreen;
787 osys->cfg.is_fullscreen = is_fullscreen;
790 vout_SendEventFullscreen(osys->vout, osys->cfg.is_fullscreen);
794 if (ch_display_size) {
795 vout_display_cfg_t cfg = osys->cfg;
796 cfg.display.width = display_width;
797 cfg.display.height = display_height;
799 if (!cfg.is_fullscreen != !display_is_fullscreen ||
800 vout_display_Control(vd, VOUT_DISPLAY_CHANGE_DISPLAY_SIZE, &cfg, display_is_forced)) {
801 if (!cfg.is_fullscreen == !display_is_fullscreen)
802 msg_Err(vd, "Failed to resize display");
804 /* We ignore the resized */
805 display_width = osys->cfg.display.width;
806 display_height = osys->cfg.display.height;
808 osys->cfg.display.width = display_width;
809 osys->cfg.display.height = display_height;
811 if (!display_is_fullscreen) {
812 osys->width_saved = display_width;
813 osys->height_saved = display_height;
817 if (osys->ch_display_filled) {
818 vout_display_cfg_t cfg = osys->cfg;
820 cfg.is_display_filled = osys->is_display_filled;
822 if (vout_display_Control(vd, VOUT_DISPLAY_CHANGE_DISPLAY_FILLED, &cfg)) {
823 msg_Err(vd, "Failed to change display filled state");
824 osys->is_display_filled = osys->cfg.is_display_filled;
826 osys->cfg.is_display_filled = osys->is_display_filled;
827 osys->ch_display_filled = false;
829 vout_SendEventDisplayFilled(osys->vout, osys->cfg.is_display_filled);
833 vout_display_cfg_t cfg = osys->cfg;
835 cfg.zoom.num = osys->zoom.num;
836 cfg.zoom.den = osys->zoom.den;
838 if (10 * cfg.zoom.num <= cfg.zoom.den) {
841 } else if (cfg.zoom.num >= 10 * cfg.zoom.den) {
846 if (vout_display_Control(vd, VOUT_DISPLAY_CHANGE_ZOOM, &cfg)) {
847 msg_Err(vd, "Failed to change zoom");
848 osys->zoom.num = osys->cfg.zoom.num;
849 osys->zoom.den = osys->cfg.zoom.den;
851 osys->fit_window = -1;
854 osys->cfg.zoom.num = osys->zoom.num;
855 osys->cfg.zoom.den = osys->zoom.den;
856 osys->ch_zoom = false;
858 vout_SendEventZoom(osys->vout, osys->cfg.zoom.num, osys->cfg.zoom.den);
862 if (vout_display_Control(vd, VOUT_DISPLAY_CHANGE_WINDOW_STATE, wm_state)) {
863 msg_Err(vd, "Failed to set on top");
864 wm_state = osys->wm_state;
866 osys->wm_state_initial = wm_state;
869 vout_SendEventOnTop(osys->vout, osys->wm_state_initial);
873 video_format_t source = vd->source;
875 if (osys->sar.num > 0 && osys->sar.den > 0) {
876 source.i_sar_num = osys->sar.num;
877 source.i_sar_den = osys->sar.den;
879 source.i_sar_num = osys->source.i_sar_num;
880 source.i_sar_den = osys->source.i_sar_den;
883 if (vout_display_Control(vd, VOUT_DISPLAY_CHANGE_SOURCE_ASPECT, &source)) {
884 /* There nothing much we can do. The only reason a vout display
885 * does not support it is because it need the core to add black border
886 * to the video for it.
887 * TODO add black borders ?
889 msg_Err(vd, "Failed to change source AR");
891 } else if (!osys->fit_window) {
892 osys->fit_window = 1;
895 osys->sar.num = source.i_sar_num;
896 osys->sar.den = source.i_sar_den;
897 osys->ch_sar = false;
900 if (osys->sar.num == osys->source.i_sar_num &&
901 osys->sar.den == osys->source.i_sar_den)
903 vout_SendEventSourceAspect(osys->vout, 0, 0);
907 unsigned dar_num, dar_den;
908 vlc_ureduce( &dar_num, &dar_den,
909 osys->sar.num * vd->source.i_visible_width,
910 osys->sar.den * vd->source.i_visible_height,
912 vout_SendEventSourceAspect(osys->vout, dar_num, dar_den);
914 /* If a crop ratio is requested, recompute the parameters */
915 if (osys->crop.num > 0 && osys->crop.den > 0)
916 osys->ch_crop = true;
920 video_format_t source = vd->source;
922 unsigned crop_num = osys->crop.num;
923 unsigned crop_den = osys->crop.den;
924 if (crop_num > 0 && crop_den > 0) {
925 video_format_t fmt = osys->source;
926 fmt.i_sar_num = source.i_sar_num;
927 fmt.i_sar_den = source.i_sar_den;
928 VoutDisplayCropRatio(&osys->crop.x, &osys->crop.y,
929 &osys->crop.width, &osys->crop.height,
930 &fmt, crop_num, crop_den);
933 source.i_x_offset = osys->crop.x;
934 source.i_y_offset = osys->crop.y;
935 source.i_visible_width = osys->crop.width;
936 source.i_visible_height = osys->crop.height;
939 const bool is_valid = source.i_x_offset < source.i_width &&
940 source.i_y_offset < source.i_height &&
941 source.i_x_offset + source.i_visible_width <= source.i_width &&
942 source.i_y_offset + source.i_visible_height <= source.i_height &&
943 source.i_visible_width > 0 && source.i_visible_height > 0;
945 if (!is_valid || vout_display_Control(vd, VOUT_DISPLAY_CHANGE_SOURCE_CROP, &source)) {
947 msg_Err(vd, "Failed to change source crop TODO implement crop at core");
949 msg_Err(vd, "Invalid crop requested");
952 crop_num = osys->crop_saved.num;
953 crop_den = osys->crop_saved.den;
954 /* FIXME implement cropping in the core if not supported by the
957 } else if (!osys->fit_window) {
958 osys->fit_window = 1;
961 osys->crop.x = source.i_x_offset;
962 osys->crop.y = source.i_y_offset;
963 osys->crop.width = source.i_visible_width;
964 osys->crop.height = source.i_visible_height;
965 osys->crop.num = crop_num;
966 osys->crop.den = crop_den;
967 osys->ch_crop = false;
969 /* TODO fix when a ratio is used (complicated). */
970 const unsigned left = osys->crop.x - osys->source.i_x_offset;
971 const unsigned top = osys->crop.y - osys->source.i_y_offset;
972 const unsigned right = osys->source.i_visible_width - (osys->crop.width + osys->crop.x);
973 const unsigned bottom = osys->source.i_visible_height - (osys->crop.height + osys->crop.y);
974 vout_SendEventSourceCrop(osys->vout,
975 osys->crop.num, osys->crop.den,
976 left, top, right, bottom);
980 if (reset_pictures) {
981 if (vout_display_Control(vd, VOUT_DISPLAY_RESET_PICTURES)) {
982 /* FIXME what to do here ? */
983 msg_Err(vd, "Failed to reset pictures (probably fatal)");
989 VoutDisplayResetRender(vd);
992 bool vout_AreDisplayPicturesInvalid(vout_display_t *vd)
994 vout_display_owner_sys_t *osys = vd->owner.sys;
996 vlc_mutex_lock(&osys->lock);
997 const bool reset_pictures = osys->reset_pictures;
998 vlc_mutex_unlock(&osys->lock);
1000 return reset_pictures;
1003 bool vout_IsDisplayFiltered(vout_display_t *vd)
1005 vout_display_owner_sys_t *osys = vd->owner.sys;
1007 return osys->filters != NULL;
1010 picture_t *vout_FilterDisplay(vout_display_t *vd, picture_t *picture)
1012 vout_display_owner_sys_t *osys = vd->owner.sys;
1014 assert(osys->filters);
1015 return filter_chain_VideoFilter(osys->filters, picture);
1018 void vout_SetDisplayFullscreen(vout_display_t *vd, bool is_fullscreen)
1020 vout_display_owner_sys_t *osys = vd->owner.sys;
1022 vlc_mutex_lock(&osys->lock);
1023 if (!osys->is_fullscreen != !is_fullscreen) {
1024 osys->ch_fullscreen = true;
1025 osys->is_fullscreen = is_fullscreen;
1027 vlc_mutex_unlock(&osys->lock);
1030 void vout_SetDisplayFilled(vout_display_t *vd, bool is_filled)
1032 vout_display_owner_sys_t *osys = vd->owner.sys;
1034 if (!osys->is_display_filled != !is_filled) {
1035 osys->ch_display_filled = true;
1036 osys->is_display_filled = is_filled;
1040 void vout_SetDisplayZoom(vout_display_t *vd, int num, int den)
1042 vout_display_owner_sys_t *osys = vd->owner.sys;
1044 if (osys->is_display_filled ||
1045 osys->zoom.num != num || osys->zoom.den != den) {
1046 osys->ch_zoom = true;
1047 osys->zoom.num = num;
1048 osys->zoom.den = den;
1052 void vout_SetWindowState(vout_display_t *vd, unsigned state)
1054 vout_display_owner_sys_t *osys = vd->owner.sys;
1056 vlc_mutex_lock(&osys->lock);
1057 if (osys->wm_state != state) {
1058 osys->ch_wm_state = true;
1059 osys->wm_state = state;
1061 vlc_mutex_unlock(&osys->lock);
1064 void vout_SetDisplayAspect(vout_display_t *vd, unsigned sar_num, unsigned sar_den)
1066 vout_display_owner_sys_t *osys = vd->owner.sys;
1068 if (osys->sar.num != sar_num || osys->sar.den != sar_den) {
1069 osys->ch_sar = true;
1070 osys->sar.num = sar_num;
1071 osys->sar.den = sar_den;
1074 void vout_SetDisplayCrop(vout_display_t *vd,
1075 unsigned crop_num, unsigned crop_den,
1076 unsigned x, unsigned y, unsigned width, unsigned height)
1078 vout_display_owner_sys_t *osys = vd->owner.sys;
1080 if (osys->crop.x != x || osys->crop.y != y ||
1081 osys->crop.width != width || osys->crop.height != height ||
1082 (crop_num > 0 && crop_den > 0 &&
1083 (crop_num != osys->crop.num || crop_den != osys->crop.den))) {
1087 osys->crop.width = width;
1088 osys->crop.height = height;
1089 osys->crop.num = crop_num;
1090 osys->crop.den = crop_den;
1092 osys->ch_crop = true;
1095 vout_opengl_t *vout_GetDisplayOpengl(vout_display_t *vd)
1098 if (vout_display_Control(vd, VOUT_DISPLAY_GET_OPENGL, &gl))
1103 static vout_display_t *DisplayNew(vout_thread_t *vout,
1104 const video_format_t *source_org,
1105 const vout_display_state_t *state,
1107 bool is_wrapper, vout_display_t *wrapper,
1108 mtime_t double_click_timeout,
1109 mtime_t hide_timeout,
1110 const vout_display_owner_t *owner_ptr)
1113 vout_display_owner_sys_t *osys = calloc(1, sizeof(*osys));
1114 vout_display_cfg_t *cfg = &osys->cfg;
1117 osys->wm_state_initial = VOUT_WINDOW_STATE_NORMAL;
1118 osys->sar_initial.num = state->sar.num;
1119 osys->sar_initial.den = state->sar.den;
1120 vout_display_GetDefaultDisplaySize(&cfg->display.width, &cfg->display.height,
1124 osys->is_wrapper = is_wrapper;
1125 osys->wrapper = wrapper;
1127 vlc_mutex_init(&osys->lock);
1129 vlc_mouse_Init(&osys->mouse.state);
1130 osys->mouse.last_moved = mdate();
1131 osys->mouse.double_click_timeout = double_click_timeout;
1132 osys->mouse.hide_timeout = hide_timeout;
1133 osys->is_fullscreen = cfg->is_fullscreen;
1135 osys->display_width = cfg->display.width;
1136 osys->height_saved =
1137 osys->display_height = cfg->display.height;
1138 osys->is_display_filled = cfg->is_display_filled;
1139 osys->zoom.num = cfg->zoom.num;
1140 osys->zoom.den = cfg->zoom.den;
1141 osys->wm_state = state->is_on_top ? VOUT_WINDOW_STATE_ABOVE
1142 : VOUT_WINDOW_STATE_NORMAL;
1143 osys->fit_window = 0;
1145 osys->source = *source_org;
1147 video_format_t source = *source_org;
1153 source.i_visible_width =
1154 osys->crop.width = source.i_width;
1155 source.i_visible_height =
1156 osys->crop.height = source.i_height;
1157 osys->crop_saved.num = 0;
1158 osys->crop_saved.den = 0;
1162 osys->sar.num = osys->sar_initial.num ? osys->sar_initial.num : source.i_sar_num;
1163 osys->sar.den = osys->sar_initial.den ? osys->sar_initial.den : source.i_sar_den;
1164 #ifdef ALLOW_DUMMY_VOUT
1165 vlc_mouse_Init(&osys->vout_mouse);
1168 vout_display_owner_t owner;
1172 owner.event = VoutDisplayEvent;
1173 owner.window_new = VoutDisplayNewWindow;
1174 owner.window_del = VoutDisplayDelWindow;
1179 vout_display_t *p_display = vout_display_New(VLC_OBJECT(vout),
1180 module, !is_wrapper,
1181 &source, cfg, &owner);
1187 VoutDisplayCreateRender(p_display);
1189 /* Setup delayed request */
1190 if (osys->sar.num != source_org->i_sar_num ||
1191 osys->sar.den != source_org->i_sar_den)
1192 osys->ch_sar = true;
1193 if (osys->wm_state != osys->wm_state_initial)
1194 osys->ch_wm_state = true;
1195 if (osys->crop.x != source_org->i_x_offset ||
1196 osys->crop.y != source_org->i_y_offset ||
1197 osys->crop.width != source_org->i_visible_width ||
1198 osys->crop.height != source_org->i_visible_height)
1199 osys->ch_crop = true;
1204 void vout_DeleteDisplay(vout_display_t *vd, vout_display_state_t *state)
1206 vout_display_owner_sys_t *osys = vd->owner.sys;
1209 if (!osys->is_wrapper )
1210 state->cfg = osys->cfg;
1211 state->is_on_top = (osys->wm_state & VOUT_WINDOW_STATE_ABOVE) != 0;
1212 state->sar.num = osys->sar_initial.num;
1213 state->sar.den = osys->sar_initial.den;
1216 VoutDisplayDestroyRender(vd);
1217 if (osys->is_wrapper)
1219 vout_display_Delete(vd);
1220 vlc_mutex_destroy(&osys->lock);
1224 /*****************************************************************************
1226 *****************************************************************************/
1227 vout_display_t *vout_NewDisplay(vout_thread_t *vout,
1228 const video_format_t *source,
1229 const vout_display_state_t *state,
1231 mtime_t double_click_timeout,
1232 mtime_t hide_timeout)
1234 return DisplayNew(vout, source, state, module, false, NULL,
1235 double_click_timeout, hide_timeout, NULL);
1238 /*****************************************************************************
1240 *****************************************************************************/
1241 struct vout_display_sys_t {
1242 picture_pool_t *pool;
1243 video_splitter_t *splitter;
1247 picture_t **picture;
1248 vout_display_t **display;
1250 struct video_splitter_owner_t {
1251 vout_display_t *wrapper;
1254 static vout_window_t *SplitterNewWindow(vout_display_t *vd, const vout_window_cfg_t *cfg_ptr)
1256 vout_display_owner_sys_t *osys = vd->owner.sys;
1258 vout_window_cfg_t cfg = *cfg_ptr;
1259 cfg.is_standalone = true;
1260 cfg.x += 0;//output->window.i_x; FIXME
1261 cfg.y += 0;//output->window.i_y;
1263 return vout_NewDisplayWindow(osys->vout, vd, &cfg);
1265 static void SplitterDelWindow(vout_display_t *vd, vout_window_t *window)
1267 vout_display_owner_sys_t *osys = vd->owner.sys;
1269 vout_DeleteDisplayWindow(osys->vout, vd, window);
1271 static void SplitterEvent(vout_display_t *vd, int event, va_list args)
1273 //vout_display_owner_sys_t *osys = vd->owner.sys;
1277 case VOUT_DISPLAY_EVENT_MOUSE_STATE:
1278 case VOUT_DISPLAY_EVENT_MOUSE_MOVED:
1279 case VOUT_DISPLAY_EVENT_MOUSE_PRESSED:
1280 case VOUT_DISPLAY_EVENT_MOUSE_RELEASED:
1284 case VOUT_DISPLAY_EVENT_MOUSE_DOUBLE_CLICK:
1285 case VOUT_DISPLAY_EVENT_KEY:
1286 case VOUT_DISPLAY_EVENT_CLOSE:
1287 case VOUT_DISPLAY_EVENT_FULLSCREEN:
1288 case VOUT_DISPLAY_EVENT_DISPLAY_SIZE:
1289 case VOUT_DISPLAY_EVENT_PICTURES_INVALID:
1290 VoutDisplayEvent(vd, event, args);
1294 msg_Err(vd, "SplitterEvent TODO");
1299 static picture_pool_t *SplitterPool(vout_display_t *vd, unsigned count)
1301 vout_display_sys_t *sys = vd->sys;
1303 sys->pool = picture_pool_NewFromFormat(&vd->fmt, count);
1306 static void SplitterPrepare(vout_display_t *vd, picture_t *picture)
1308 vout_display_sys_t *sys = vd->sys;
1310 picture_Hold(picture);
1312 if (video_splitter_Filter(sys->splitter, sys->picture, picture)) {
1313 for (int i = 0; i < sys->count; i++)
1314 sys->picture[i] = NULL;
1315 picture_Release(picture);
1319 for (int i = 0; i < sys->count; i++) {
1320 if (vout_IsDisplayFiltered(sys->display[i]))
1321 sys->picture[i] = vout_FilterDisplay(sys->display[i], sys->picture[i]);
1322 if (sys->picture[i])
1323 vout_display_Prepare(sys->display[i], sys->picture[i]);
1326 static void SplitterDisplay(vout_display_t *vd, picture_t *picture)
1328 vout_display_sys_t *sys = vd->sys;
1330 for (int i = 0; i < sys->count; i++) {
1331 if (sys->picture[i])
1332 vout_display_Display(sys->display[i], sys->picture[i]);
1334 picture_Release(picture);
1336 static int SplitterControl(vout_display_t *vd, int query, va_list args)
1338 return VLC_EGENERIC;
1340 static void SplitterManage(vout_display_t *vd)
1342 vout_display_sys_t *sys = vd->sys;
1344 for (int i = 0; i < sys->count; i++)
1345 vout_ManageDisplay(sys->display[i], true);
1348 static int SplitterPictureNew(video_splitter_t *splitter, picture_t *picture[])
1350 vout_display_sys_t *wsys = splitter->p_owner->wrapper->sys;
1352 for (int i = 0; i < wsys->count; i++) {
1353 if (vout_IsDisplayFiltered(wsys->display[i])) {
1354 /* TODO use a pool ? */
1355 picture[i] = picture_NewFromFormat(&wsys->display[i]->source);
1357 picture_pool_t *pool = vout_display_Pool(wsys->display[i], 1);
1358 picture[i] = pool ? picture_pool_Get(pool) : NULL;
1361 for (int j = 0; j < i; j++)
1362 picture_Release(picture[j]);
1363 return VLC_EGENERIC;
1368 static void SplitterPictureDel(video_splitter_t *splitter, picture_t *picture[])
1370 vout_display_sys_t *wsys = splitter->p_owner->wrapper->sys;
1372 for (int i = 0; i < wsys->count; i++)
1373 picture_Release(picture[i]);
1375 static void SplitterClose(vout_display_t *vd)
1377 vout_display_sys_t *sys = vd->sys;
1380 video_splitter_t *splitter = sys->splitter;
1381 free(splitter->p_owner);
1382 video_splitter_Delete(splitter);
1385 picture_pool_Delete(sys->pool);
1388 for (int i = 0; i < sys->count; i++)
1389 vout_DeleteDisplay(sys->display[i], NULL);
1390 TAB_CLEAN(sys->count, sys->display);
1396 vout_display_t *vout_NewSplitter(vout_thread_t *vout,
1397 const video_format_t *source,
1398 const vout_display_state_t *state,
1400 const char *splitter_module,
1401 mtime_t double_click_timeout,
1402 mtime_t hide_timeout)
1404 video_splitter_t *splitter =
1405 video_splitter_New(VLC_OBJECT(vout), splitter_module, source);
1410 vout_display_t *wrapper =
1411 DisplayNew(vout, source, state, module, true, NULL,
1412 double_click_timeout, hide_timeout, NULL);
1414 video_splitter_Delete(splitter);
1417 vout_display_sys_t *sys = malloc(sizeof(*sys));
1420 sys->picture = calloc(splitter->i_output, sizeof(*sys->picture));
1423 sys->splitter = splitter;
1426 wrapper->pool = SplitterPool;
1427 wrapper->prepare = SplitterPrepare;
1428 wrapper->display = SplitterDisplay;
1429 wrapper->control = SplitterControl;
1430 wrapper->manage = SplitterManage;
1434 video_splitter_owner_t *owner = malloc(sizeof(*owner));
1437 owner->wrapper = wrapper;
1438 splitter->p_owner = owner;
1439 splitter->pf_picture_new = SplitterPictureNew;
1440 splitter->pf_picture_del = SplitterPictureDel;
1443 TAB_INIT(sys->count, sys->display);
1444 for (int i = 0; i < splitter->i_output; i++) {
1445 vout_display_owner_t owner;
1447 owner.event = SplitterEvent;
1448 owner.window_new = SplitterNewWindow;
1449 owner.window_del = SplitterDelWindow;
1451 const video_splitter_output_t *output = &splitter->p_output[i];
1452 vout_display_state_t ostate;
1454 memset(&ostate, 0, sizeof(ostate));
1455 ostate.cfg.is_fullscreen = false;
1456 ostate.cfg.display = state->cfg.display;
1457 ostate.cfg.align.horizontal = 0; /* TODO */
1458 ostate.cfg.align.vertical = 0; /* TODO */
1459 ostate.cfg.is_display_filled = true;
1460 ostate.cfg.zoom.num = 1;
1461 ostate.cfg.zoom.den = 1;
1463 vout_display_t *vd = DisplayNew(vout, &output->fmt, &ostate,
1464 output->psz_module ? output->psz_module : module,
1466 double_click_timeout, hide_timeout, &owner);
1468 vout_DeleteDisplay(wrapper, NULL);
1471 TAB_APPEND(sys->count, sys->display, vd);
1477 /*****************************************************************************
1479 *****************************************************************************/
1480 #include "vout_internal.h"
1481 void vout_SendDisplayEventMouse(vout_thread_t *vout, const vlc_mouse_t *m)
1485 /* The check on p_spu is needed as long as ALLOW_DUMMY_VOUT is defined */
1486 if (vout->p->p_spu && spu_ProcessMouse( vout->p->p_spu, m, &vout->p->display.vd->source))
1489 vlc_mutex_lock( &vout->p->vfilter_lock );
1490 if (vout->p->vfilter_chain) {
1491 if (!filter_chain_MouseFilter(vout->p->vfilter_chain, &tmp, m))
1494 vlc_mutex_unlock( &vout->p->vfilter_lock );
1496 if (vlc_mouse_HasMoved(&vout->p->mouse, m)) {
1497 vout_SendEventMouseMoved(vout, m->i_x, m->i_y);
1499 if (vlc_mouse_HasButton(&vout->p->mouse, m)) {
1500 static const int buttons[] = {
1502 MOUSE_BUTTON_CENTER,
1504 MOUSE_BUTTON_WHEEL_UP,
1505 MOUSE_BUTTON_WHEEL_DOWN,
1508 for (int i = 0; buttons[i] >= 0; i++) {
1509 const int button = buttons[i];
1510 if (vlc_mouse_HasPressed(&vout->p->mouse, m, button))
1511 vout_SendEventMousePressed(vout, button);
1512 else if (vlc_mouse_HasReleased(&vout->p->mouse, m, button))
1513 vout_SendEventMouseReleased(vout, button);
1516 if (m->b_double_click)
1517 vout_SendEventMouseDoubleClick(vout);
1518 vout->p->mouse = *m;
1520 #ifdef ALLOW_DUMMY_VOUT
1521 static void DummyVoutSendDisplayEventMouse(vout_thread_t *vout, vlc_mouse_t *fallback, const vlc_mouse_t *m)
1523 vout_thread_sys_t p;
1526 p.mouse = *fallback;
1527 vlc_mutex_init(&p.vfilter_lock);
1528 p.vfilter_chain = NULL;
1532 vout_SendDisplayEventMouse(vout, m);
1533 if (vout->p == &p) {
1534 vlc_mutex_destroy(&p.vfilter_lock);
1535 *fallback = p.mouse;
1540 vout_window_t * vout_NewDisplayWindow(vout_thread_t *vout, vout_display_t *vd, const vout_window_cfg_t *cfg)
1543 vout_window_cfg_t cfg_override = *cfg;
1545 if( !var_InheritBool( vout, "embedded-video" ) )
1546 cfg_override.is_standalone = true;
1548 return vout_window_New(VLC_OBJECT(vout), NULL, &cfg_override);
1550 void vout_DeleteDisplayWindow(vout_thread_t *vout, vout_display_t *vd, vout_window_t *window)
1554 vout_window_Delete(window);