display.c 52.3 KB
Newer Older
1
2
3
4
5
6
7
8
/*****************************************************************************
 * display.c: "vout display" managment
 *****************************************************************************
 * Copyright (C) 2009 Laurent Aimar
 * $Id$
 *
 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
 *
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
9
10
11
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
12
13
14
15
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
16
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
18
 *
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
19
20
21
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>

#include <vlc_common.h>
#include <vlc_video_splitter.h>
#include <vlc_vout_display.h>
#include <vlc_vout.h>
36
#include <vlc_block.h>
37
#include <vlc_modules.h>
38
39
40
41
42
43
44

#include <libvlc.h>

#include "display.h"

#include "event.h"

45
46
47
48
/* It must be present as long as a vout_display_t must be created using a dummy
 * vout (as an opengl provider) */
#define ALLOW_DUMMY_VOUT

49
50
51
52
53
54
55
static void SplitterClose(vout_display_t *vd);

/*****************************************************************************
 * FIXME/TODO see how to have direct rendering here (interact with vout.c)
 *****************************************************************************/
static picture_t *VideoBufferNew(filter_t *filter)
{
56
    vout_display_t *vd = (vout_display_t*)filter->p_owner;
57
58
    const video_format_t *fmt = &filter->fmt_out.video;

59
60
61
62
    assert(vd->fmt.i_chroma == fmt->i_chroma &&
           vd->fmt.i_width  == fmt->i_width  &&
           vd->fmt.i_height == fmt->i_height);

63
    picture_pool_t *pool = vout_display_Pool(vd, 3);
64
65
66
    if (!pool)
        return NULL;
    return picture_pool_Get(pool);
67
68
69
70
71
72
73
}
static void VideoBufferDelete(filter_t *filter, picture_t *picture)
{
    VLC_UNUSED(filter);
    picture_Release(picture);
}

74
static int  FilterAllocationInit(filter_t *filter, void *vd)
75
{
76
77
78
    filter->pf_video_buffer_new = VideoBufferNew;
    filter->pf_video_buffer_del = VideoBufferDelete;
    filter->p_owner             = vd;
79
80
81
82
83

    return VLC_SUCCESS;
}
static void FilterAllocationClean(filter_t *filter)
{
84
85
86
    filter->pf_video_buffer_new = NULL;
    filter->pf_video_buffer_del = NULL;
    filter->p_owner             = NULL;
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
}

/*****************************************************************************
 *
 *****************************************************************************/

/**
 * It creates a new vout_display_t using the given configuration.
 */
static vout_display_t *vout_display_New(vlc_object_t *obj,
                                        const char *module, bool load_module,
                                        const video_format_t *fmt,
                                        const vout_display_cfg_t *cfg,
                                        vout_display_owner_t *owner)
{
    /* */
103
    vout_display_t *vd = vlc_custom_create(obj, sizeof(*vd), "vout display" );
104
105
106
107
108
109
110
111
112
113
114
115
116

    /* */
    video_format_Copy(&vd->source, fmt);

    /* Picture buffer does not have the concept of aspect ratio */
    video_format_Copy(&vd->fmt, fmt);
    vd->fmt.i_sar_num = 0;
    vd->fmt.i_sar_den = 0;

    vd->info.is_slow = false;
    vd->info.has_double_click = false;
    vd->info.has_hide_mouse = false;
    vd->info.has_pictures_invalid = false;
117
    vd->info.has_event_thread = false;
118
    vd->info.subpicture_chromas = NULL;
119
120

    vd->cfg = cfg;
121
    vd->pool = NULL;
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
    vd->prepare = NULL;
    vd->display = NULL;
    vd->control = NULL;
    vd->manage = NULL;
    vd->sys = NULL;

    vd->owner = *owner;

    if (load_module) {
        vd->module = module_need(vd, "vout display", module, module && *module != '\0');
        if (!vd->module) {
            vlc_object_release(vd);
            return NULL;
        }
    } else {
        vd->module = NULL;
    }
    return vd;
}

/**
 * It deletes a vout_display_t
 */
static void vout_display_Delete(vout_display_t *vd)
{
    if (vd->module)
        module_unneed(vd, vd->module);

    vlc_object_release(vd);
}

/**
 * It controls a vout_display_t
 */
static int vout_display_Control(vout_display_t *vd, int query, ...)
{
    va_list args;
    int result;

    va_start(args, query);
    result = vd->control(vd, query, args);
    va_end(args);

    return result;
}
167

168
169
static void vout_display_Manage(vout_display_t *vd)
{
170
171
    if (vd->manage)
        vd->manage(vd);
172
173
174
}

/* */
175
void vout_display_GetDefaultDisplaySize(unsigned *width, unsigned *height,
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
                                        const video_format_t *source,
                                        const vout_display_cfg_t *cfg)
{
    if (cfg->display.width > 0 && cfg->display.height > 0) {
        *width  = cfg->display.width;
        *height = cfg->display.height;
    } else if (cfg->display.width > 0) {
        *width  = cfg->display.width;
        *height = (int64_t)source->i_visible_height * source->i_sar_den * cfg->display.width * cfg->display.sar.num /
            source->i_visible_width / source->i_sar_num / cfg->display.sar.den;
    } else if (cfg->display.height > 0) {
        *width  = (int64_t)source->i_visible_width * source->i_sar_num * cfg->display.height * cfg->display.sar.den /
            source->i_visible_height / source->i_sar_den / cfg->display.sar.num;
        *height = cfg->display.height;
    } else if (source->i_sar_num >= source->i_sar_den) {
        *width  = (int64_t)source->i_visible_width * source->i_sar_num * cfg->display.sar.den / source->i_sar_den / cfg->display.sar.num;
        *height = source->i_visible_height;
    } else {
        *width  = source->i_visible_width;
        *height = (int64_t)source->i_visible_height * source->i_sar_den * cfg->display.sar.num / source->i_sar_num / cfg->display.sar.den;
    }

    *width  = *width  * cfg->zoom.num / cfg->zoom.den;
    *height = *height * cfg->zoom.num / cfg->zoom.den;
}

/* */
void vout_display_PlacePicture(vout_display_place_t *place,
                               const video_format_t *source,
                               const vout_display_cfg_t *cfg,
                               bool do_clipping)
{
    /* */
    memset(place, 0, sizeof(*place));
    if (cfg->display.width <= 0 || cfg->display.height <= 0)
        return;

    /* */
214
215
    unsigned display_width;
    unsigned display_height;
216
217
218
219
220
221
222
223
224

    if (cfg->is_display_filled) {
        display_width  = cfg->display.width;
        display_height = cfg->display.height;
    } else {
        vout_display_cfg_t cfg_tmp = *cfg;

        cfg_tmp.display.width  = 0;
        cfg_tmp.display.height = 0;
225
        vout_display_GetDefaultDisplaySize(&display_width, &display_height,
226
227
228
229
230
231
232
233
                                           source, &cfg_tmp);

        if (do_clipping) {
            display_width  = __MIN(display_width,  cfg->display.width);
            display_height = __MIN(display_height, cfg->display.height);
        }
    }

234
235
    const unsigned width  = source->i_visible_width;
    const unsigned height = source->i_visible_height;
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
    /* Compute the height if we use the width to fill up display_width */
    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;
    /* And the same but switching width/height */
    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;

    /* We keep the solution that avoid filling outside the display */
    if (scaled_width <= cfg->display.width) {
        place->width  = scaled_width;
        place->height = display_height;
    } else {
        place->width  = display_width;
        place->height = scaled_height;
    }

    /*  Compute position */
    switch (cfg->align.horizontal) {
    case VOUT_DISPLAY_ALIGN_LEFT:
        place->x = 0;
        break;
    case VOUT_DISPLAY_ALIGN_RIGHT:
        place->x = cfg->display.width - place->width;
        break;
    default:
Erwan Tulou's avatar
Erwan Tulou committed
259
        place->x = ((int)cfg->display.width - (int)place->width) / 2;
260
261
262
263
264
265
266
267
268
269
270
        break;
    }

    switch (cfg->align.vertical) {
    case VOUT_DISPLAY_ALIGN_TOP:
        place->y = 0;
        break;
    case VOUT_DISPLAY_ALIGN_BOTTOM:
        place->y = cfg->display.height - place->height;
        break;
    default:
Erwan Tulou's avatar
Erwan Tulou committed
271
        place->y = ((int)cfg->display.height - (int)place->height) / 2;
272
273
274
275
276
277
278
279
280
281
282
        break;
    }
}

struct vout_display_owner_sys_t {
    vout_thread_t   *vout;
    bool            is_wrapper;  /* Is the current display a wrapper */
    vout_display_t  *wrapper; /* Vout display wrapper */

    /* */
    vout_display_cfg_t cfg;
283
    unsigned     wm_state_initial;
284
285
286
287
288
289
    struct {
        unsigned num;
        unsigned den;
    } sar_initial;

    /* */
Laurent Aimar's avatar
Laurent Aimar committed
290
291
    unsigned width_saved;
    unsigned height_saved;
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307

    struct {
        unsigned num;
        unsigned den;
    } crop_saved;

    /* */
    bool ch_display_filled;
    bool is_display_filled;

    bool ch_zoom;
    struct {
        int  num;
        int  den;
    } zoom;

308
309
    bool ch_wm_state;
    unsigned wm_state;
310
311
312
313
314
315
316
317
318

    bool ch_sar;
    struct {
        unsigned num;
        unsigned den;
    } sar;

    bool ch_crop;
    struct {
319
320
321
322
        int      left;
        int      top;
        int      right;
        int      bottom;
323
324
325
326
327
328
329
        unsigned num;
        unsigned den;
    } crop;

    /* */
    video_format_t source;
    filter_chain_t *filters;
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

    /* Lock protecting the variables used by
     * VoutDisplayEvent(ie vout_display_SendEvent) */
    vlc_mutex_t lock;

    /* mouse state */
    struct {
        vlc_mouse_t state;

        mtime_t last_pressed;
        mtime_t last_moved;
        bool    is_hidden;
        bool    ch_activity;

        /* */
        mtime_t double_click_timeout;
        mtime_t hide_timeout;
    } mouse;

    bool reset_pictures;

    bool ch_fullscreen;
    bool is_fullscreen;

    bool ch_display_size;
    int  display_width;
    int  display_height;
    bool display_is_fullscreen;
358
    bool display_is_forced;
359

360
361
    int  fit_window;

362
363
364
365
366
    struct {
        vlc_thread_t thread;
        block_fifo_t *fifo;
    } event;

367
368
369
#ifdef ALLOW_DUMMY_VOUT
    vlc_mouse_t vout_mouse;
#endif
370
371
};

372
373
static void DummyVoutSendDisplayEventMouse(vout_thread_t *, vlc_mouse_t *fallback, const vlc_mouse_t *m);

374
375
376
377
378
379
380
381
382
383
384
385
386
387
static void VoutDisplayCreateRender(vout_display_t *vd)
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

    osys->filters = NULL;

    video_format_t v_src = vd->source;
    v_src.i_sar_num = 0;
    v_src.i_sar_den = 0;

    video_format_t v_dst = vd->fmt;
    v_dst.i_sar_num = 0;
    v_dst.i_sar_den = 0;

388
389
390
391
392
393
394
395
    video_format_t v_dst_cmp = v_dst;
    if ((v_src.i_chroma == VLC_CODEC_J420 && v_dst.i_chroma == VLC_CODEC_I420) ||
        (v_src.i_chroma == VLC_CODEC_J422 && v_dst.i_chroma == VLC_CODEC_I422) ||
        (v_src.i_chroma == VLC_CODEC_J440 && v_dst.i_chroma == VLC_CODEC_I440) ||
        (v_src.i_chroma == VLC_CODEC_J444 && v_dst.i_chroma == VLC_CODEC_I444))
        v_dst_cmp.i_chroma = v_src.i_chroma;

    const bool convert = memcmp(&v_src, &v_dst_cmp, sizeof(v_src)) != 0;
396
397
398
    if (!convert)
        return;

dionoea's avatar
dionoea committed
399
    msg_Dbg(vd, "A filter to adapt decoder to display is needed");
400

401
402
    osys->filters = filter_chain_New(vd, "video filter2", false,
                                     FilterAllocationInit,
403
                                     FilterAllocationClean, vd);
404
405
406
407
408
409
410
411
412
    assert(osys->filters); /* TODO critical */

    /* */
    es_format_t src;
    es_format_InitFromVideo(&src, &v_src);

    /* */
    es_format_t dst;

413
414
    filter_t *filter;
    for (int i = 0; i < 1 + (v_dst_cmp.i_chroma != v_dst.i_chroma); i++) {
415

416
417
418
419
420
421
422
423
        es_format_InitFromVideo(&dst, i == 0 ? &v_dst : &v_dst_cmp);

        filter_chain_Reset(osys->filters, &src, &dst);
        filter = filter_chain_AppendFilter(osys->filters,
                                           NULL, NULL, &src, &dst);
        if (filter)
            break;
    }
424
    if (!filter)
425
        msg_Err(vd, "Failed to adapt decoder format to display");
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
}

static void VoutDisplayDestroyRender(vout_display_t *vd)
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

    if (osys->filters)
        filter_chain_Delete(osys->filters);
}

static void VoutDisplayResetRender(vout_display_t *vd)
{
    VoutDisplayDestroyRender(vd);
    VoutDisplayCreateRender(vd);
}
441
static void VoutDisplayEventMouse(vout_display_t *vd, int event, va_list args)
442
443
444
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

445
446
    vlc_mutex_lock(&osys->lock);

447
    /* */
448
449
    vlc_mouse_t m = osys->mouse.state;
    bool is_ignored = false;
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466

    switch (event) {
    case VOUT_DISPLAY_EVENT_MOUSE_STATE: {
        const int x = (int)va_arg(args, int);
        const int y = (int)va_arg(args, int);
        const int button_mask = (int)va_arg(args, int);

        vlc_mouse_Init(&m);
        m.i_x = x;
        m.i_y = y;
        m.i_pressed = button_mask;
        break;
    }
    case VOUT_DISPLAY_EVENT_MOUSE_MOVED: {
        const int x = (int)va_arg(args, int);
        const int y = (int)va_arg(args, int);

467
468
469
470
471
        //msg_Dbg(vd, "VoutDisplayEvent 'mouse' @%d,%d", x, y);

        m.i_x = x;
        m.i_y = y;
        m.b_double_click = false;
472
473
474
475
476
477
478
        break;
    }
    case VOUT_DISPLAY_EVENT_MOUSE_PRESSED:
    case VOUT_DISPLAY_EVENT_MOUSE_RELEASED: {
        const int button = (int)va_arg(args, int);
        const int button_mask = 1 << button;

479
480
481
482
        /* Ignore inconsistent event */
        if ((event == VOUT_DISPLAY_EVENT_MOUSE_PRESSED  &&  (osys->mouse.state.i_pressed & button_mask)) ||
            (event == VOUT_DISPLAY_EVENT_MOUSE_RELEASED && !(osys->mouse.state.i_pressed & button_mask))) {
            is_ignored = true;
483
            break;
484
        }
485
486
487
488
489
490
491
492
493
494
495

        /* */
        msg_Dbg(vd, "VoutDisplayEvent 'mouse button' %d t=%d", button, event);

        m.b_double_click = false;
        if (event == VOUT_DISPLAY_EVENT_MOUSE_PRESSED)
            m.i_pressed |= button_mask;
        else
            m.i_pressed &= ~button_mask;
        break;
    }
496
    case VOUT_DISPLAY_EVENT_MOUSE_DOUBLE_CLICK:
497
498
499
        msg_Dbg(vd, "VoutDisplayEvent 'double click'");

        m.b_double_click = true;
500
501
502
503
504
        break;
    default:
        assert(0);
    }

505
506
    if (is_ignored) {
        vlc_mutex_unlock(&osys->lock);
507
        return;
508
    }
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526

    /* Emulate double-click if needed */
    if (!vd->info.has_double_click &&
        vlc_mouse_HasPressed(&osys->mouse.state, &m, MOUSE_BUTTON_LEFT)) {
        const mtime_t i_date = mdate();

        if (i_date - osys->mouse.last_pressed < osys->mouse.double_click_timeout ) {
            m.b_double_click = true;
            osys->mouse.last_pressed = 0;
        } else {
            osys->mouse.last_pressed = mdate();
        }
    }

    /* */
    osys->mouse.state = m;

    /* */
527
    osys->mouse.ch_activity = true;
528
529
530
531
532
    if (!vd->info.has_hide_mouse)
        osys->mouse.last_moved = mdate();

    /* */
    vout_SendEventMouseVisible(osys->vout);
533
534
535
#ifdef ALLOW_DUMMY_VOUT
    DummyVoutSendDisplayEventMouse(osys->vout, &osys->vout_mouse, &m);
#else
536
    vout_SendDisplayEventMouse(osys->vout, &m);
537
#endif
538
    vlc_mutex_unlock(&osys->lock);
539
540
}

541
VLC_NORETURN
542
543
static void *VoutDisplayEventKeyDispatch(void *data)
{
544
    vout_display_owner_sys_t *osys = data;
545
546
547

    for (;;) {
        block_t *event = block_FifoGet(osys->event.fifo);
548
549

        int cancel = vlc_savecancel();
550
551
552
553
554

        int key;
        memcpy(&key, event->p_buffer, sizeof(key));
        vout_SendEventKey(osys->vout, key);
        block_Release(event);
555
556

        vlc_restorecancel(cancel);
557
558
559
560
561
562
563
564
565
566
567
568
    }
}

static void VoutDisplayEventKey(vout_display_t *vd, int key)
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

    if (!osys->event.fifo) {
        osys->event.fifo = block_FifoNew();
        if (!osys->event.fifo)
            return;
        if (vlc_clone(&osys->event.thread, VoutDisplayEventKeyDispatch,
569
                      osys, VLC_THREAD_PRIORITY_LOW)) {
570
571
572
573
574
575
576
577
578
579
580
581
            block_FifoRelease(osys->event.fifo);
            osys->event.fifo = NULL;
            return;
        }
    }
    block_t *event = block_Alloc(sizeof(key));
    if (event) {
        memcpy(event->p_buffer, &key, sizeof(key));
        block_FifoPut(osys->event.fifo, event);
    }
}

582
583
584
585
586
587
588
589
590
591
592
593
594
static void VoutDisplayEvent(vout_display_t *vd, int event, va_list args)
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

    switch (event) {
    case VOUT_DISPLAY_EVENT_CLOSE: {
        msg_Dbg(vd, "VoutDisplayEvent 'close'");
        vout_SendEventClose(osys->vout);
        break;
    }
    case VOUT_DISPLAY_EVENT_KEY: {
        const int key = (int)va_arg(args, int);
        msg_Dbg(vd, "VoutDisplayEvent 'key' 0x%2.2x", key);
595
596
597
598
        if (vd->info.has_event_thread)
            vout_SendEventKey(osys->vout, key);
        else
            VoutDisplayEventKey(vd, key);
599
600
        break;
    }
601
602
603
604
605
606
607
    case VOUT_DISPLAY_EVENT_MOUSE_STATE:
    case VOUT_DISPLAY_EVENT_MOUSE_MOVED:
    case VOUT_DISPLAY_EVENT_MOUSE_PRESSED:
    case VOUT_DISPLAY_EVENT_MOUSE_RELEASED:
    case VOUT_DISPLAY_EVENT_MOUSE_DOUBLE_CLICK:
        VoutDisplayEventMouse(vd, event, args);
        break;
608
609
610
611
612
613

    case VOUT_DISPLAY_EVENT_FULLSCREEN: {
        const int is_fullscreen = (int)va_arg(args, int);

        msg_Dbg(vd, "VoutDisplayEvent 'fullscreen' %d", is_fullscreen);

614
615
616
617
618
619
        vlc_mutex_lock(&osys->lock);
        if (!is_fullscreen != !osys->is_fullscreen) {
            osys->ch_fullscreen = true;
            osys->is_fullscreen = is_fullscreen;
        }
        vlc_mutex_unlock(&osys->lock);
620
621
622
        break;
    }

623
624
    case VOUT_DISPLAY_EVENT_WINDOW_STATE: {
        const unsigned state = va_arg(args, unsigned);
625

626
        msg_Dbg(vd, "VoutDisplayEvent 'window state' %u", state);
627
628

        vlc_mutex_lock(&osys->lock);
629
630
631
        if (state != osys->wm_state) {
            osys->ch_wm_state = true;
            osys->wm_state = state;
632
633
634
635
636
        }
        vlc_mutex_unlock(&osys->lock);
        break;
    }

637
638
639
    case VOUT_DISPLAY_EVENT_DISPLAY_SIZE: {
        const int width  = (int)va_arg(args, int);
        const int height = (int)va_arg(args, int);
640
641
642
        const bool is_fullscreen = (bool)va_arg(args, int);
        msg_Dbg(vd, "VoutDisplayEvent 'resize' %dx%d %s",
                width, height, is_fullscreen ? "fullscreen" : "window");
643
644

        /* */
645
646
        vlc_mutex_lock(&osys->lock);

647
648
649
650
        osys->ch_display_size       = true;
        osys->display_width         = width;
        osys->display_height        = height;
        osys->display_is_fullscreen = is_fullscreen;
651
        osys->display_is_forced     = false;
652
653

        vlc_mutex_unlock(&osys->lock);
654
655
656
657
658
659
660
661
        break;
    }

    case VOUT_DISPLAY_EVENT_PICTURES_INVALID: {
        msg_Warn(vd, "VoutDisplayEvent 'pictures invalid'");

        /* */
        assert(vd->info.has_pictures_invalid);
662
663

        vlc_mutex_lock(&osys->lock);
664
        osys->reset_pictures = true;
665
        vlc_mutex_unlock(&osys->lock);
666
667
668
669
670
671
672
673
674
675
676
677
678
        break;
    }
    default:
        msg_Err(vd, "VoutDisplayEvent received event %d", event);
        /* TODO add an assert when all event are handled */
        break;
    }
}

static vout_window_t *VoutDisplayNewWindow(vout_display_t *vd, const vout_window_cfg_t *cfg)
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

679
680
681
682
683
684
685
#ifdef ALLOW_DUMMY_VOUT
    if (!osys->vout->p) {
        vout_window_cfg_t cfg_override = *cfg;

        if (!var_InheritBool(osys->vout, "embedded-video"))
            cfg_override.is_standalone = true;

686
        return vout_window_New(VLC_OBJECT(osys->vout), "$window", &cfg_override);
687
688
    }
#endif
689
690
691
692
693
694
    return vout_NewDisplayWindow(osys->vout, vd, cfg);
}
static void VoutDisplayDelWindow(vout_display_t *vd, vout_window_t *window)
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

695
696
697
698
699
700
701
#ifdef ALLOW_DUMMY_VOUT
    if (!osys->vout->p) {
        if( window)
            vout_window_Delete(window);
        return;
    }
#endif
702
703
704
    vout_DeleteDisplayWindow(osys->vout, vd, window);
}

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
static void VoutDisplayFitWindow(vout_display_t *vd, bool default_size)
{
    vout_display_owner_sys_t *osys = vd->owner.sys;
    vout_display_cfg_t cfg = osys->cfg;

    if (!cfg.is_display_filled)
        return;

    cfg.display.width = 0;
    if (default_size) {
        cfg.display.height = 0;
    } else {
        cfg.display.height = osys->height_saved;
        cfg.zoom.num = 1;
        cfg.zoom.den = 1;
    }

    unsigned display_width;
    unsigned display_height;
    vout_display_GetDefaultDisplaySize(&display_width, &display_height,
                                       &vd->source, &cfg);

    vlc_mutex_lock(&osys->lock);

    osys->ch_display_size       = true;
    osys->display_width         = display_width;
    osys->display_height        = display_height;
    osys->display_is_fullscreen = osys->cfg.is_fullscreen;
    osys->display_is_forced     = true;

    vlc_mutex_unlock(&osys->lock);
}

738
static void VoutDisplayCropRatio(int *left, int *top, int *right, int *bottom,
739
740
741
742
743
744
745
                                 const video_format_t *source,
                                 unsigned num, unsigned den)
{
    unsigned scaled_width  = (uint64_t)source->i_visible_height * num * source->i_sar_den / den / source->i_sar_num;
    unsigned scaled_height = (uint64_t)source->i_visible_width  * den * source->i_sar_num / num / source->i_sar_den;

    if (scaled_width < source->i_visible_width) {
746
747
748
749
        *left   = (source->i_visible_width - scaled_width) / 2;
        *top    = 0;
        *right  = *left + scaled_width;
        *bottom = *top  + source->i_visible_height;
750
    } else {
751
752
753
754
        *left   = 0;
        *top    = (source->i_visible_height - scaled_height) / 2;
        *right  = *left + source->i_visible_width;
        *bottom = *top  + scaled_height;
755
756
757
    }
}

758
bool vout_ManageDisplay(vout_display_t *vd, bool allow_reset_pictures)
759
760
761
762
763
764
765
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

    vout_display_Manage(vd);

    /* Handle mouse timeout */
    const mtime_t date = mdate();
766
767
768
769
    bool  hide_mouse = false;

    vlc_mutex_lock(&osys->lock);

770
771
    if (!osys->mouse.is_hidden &&
        osys->mouse.last_moved + osys->mouse.hide_timeout < date) {
772
773
774
775
776
777
778
779
        osys->mouse.is_hidden = hide_mouse = true;
    } else if (osys->mouse.ch_activity) {
        osys->mouse.is_hidden = false;
    }
    osys->mouse.ch_activity = false;
    vlc_mutex_unlock(&osys->lock);

    if (hide_mouse) {
780
        if (!vd->info.has_hide_mouse) {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
781
            msg_Dbg(vd, "auto hiding mouse cursor");
782
783
784
785
786
            vout_display_Control(vd, VOUT_DISPLAY_HIDE_MOUSE);
        }
        vout_SendEventMouseHidden(osys->vout);
    }

787
    bool reset_render = false;
788
    for (;;) {
789
790
791
792
793
794
795

        vlc_mutex_lock(&osys->lock);

        bool ch_fullscreen  = osys->ch_fullscreen;
        bool is_fullscreen  = osys->is_fullscreen;
        osys->ch_fullscreen = false;

796
797
798
        bool ch_wm_state  = osys->ch_wm_state;
        unsigned wm_state  = osys->wm_state;
        osys->ch_wm_state = false;
799

800
801
802
803
        bool ch_display_size       = osys->ch_display_size;
        int  display_width         = osys->display_width;
        int  display_height        = osys->display_height;
        bool display_is_fullscreen = osys->display_is_fullscreen;
804
        bool display_is_forced     = osys->display_is_forced;
805
806
        osys->ch_display_size = false;

807
808
809
810
811
812
813
        bool reset_pictures;
        if (allow_reset_pictures) {
            reset_pictures = osys->reset_pictures;
            osys->reset_pictures = false;
        } else {
            reset_pictures = false;
        }
814
815
816
817
818
819

        vlc_mutex_unlock(&osys->lock);

        if (!ch_fullscreen &&
            !ch_display_size &&
            !reset_pictures &&
820
821
            !osys->ch_display_filled &&
            !osys->ch_zoom &&
822
            !ch_wm_state &&
823
            !osys->ch_sar &&
824
825
826
827
828
829
830
            !osys->ch_crop) {

            if (!osys->cfg.is_fullscreen && osys->fit_window != 0) {
                VoutDisplayFitWindow(vd, osys->fit_window == -1);
                osys->fit_window = 0;
                continue;
            }
831
            break;
832
        }
833
834

        /* */
835
        if (ch_fullscreen) {
836
837
            vout_display_cfg_t cfg = osys->cfg;

838
            cfg.is_fullscreen  = is_fullscreen;
839
840
841
842
843
            cfg.display.width  = cfg.is_fullscreen ? 0 : osys->width_saved;
            cfg.display.height = cfg.is_fullscreen ? 0 : osys->height_saved;

            if (vout_display_Control(vd, VOUT_DISPLAY_CHANGE_FULLSCREEN, &cfg)) {
                msg_Err(vd, "Failed to set fullscreen");
844
                is_fullscreen = osys->cfg.is_fullscreen;
845
846
            } else if (!is_fullscreen) {
                vout_display_Control(vd, VOUT_DISPLAY_CHANGE_DISPLAY_SIZE, &cfg, true);
847
            }
848
            osys->cfg.is_fullscreen = is_fullscreen;
849
850
851
852
853
854

            /* */
            vout_SendEventFullscreen(osys->vout, osys->cfg.is_fullscreen);
        }

        /* */
855
        if (ch_display_size) {
856
            vout_display_cfg_t cfg = osys->cfg;
857
858
            cfg.display.width  = display_width;
            cfg.display.height = display_height;
859

860
            if (!cfg.is_fullscreen != !display_is_fullscreen ||
861
                vout_display_Control(vd, VOUT_DISPLAY_CHANGE_DISPLAY_SIZE, &cfg, display_is_forced)) {
862
                if (!cfg.is_fullscreen == !display_is_fullscreen)
863
                    msg_Err(vd, "Failed to resize display");
864
865

                /* We ignore the resized */
866
867
                display_width  = osys->cfg.display.width;
                display_height = osys->cfg.display.height;
868
            }
869
870
            osys->cfg.display.width  = display_width;
            osys->cfg.display.height = display_height;
871

872
873
874
            if (!display_is_fullscreen) {
                osys->width_saved  = display_width;
                osys->height_saved = display_height;
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
            }
        }
        /* */
        if (osys->ch_display_filled) {
            vout_display_cfg_t cfg = osys->cfg;

            cfg.is_display_filled = osys->is_display_filled;

            if (vout_display_Control(vd, VOUT_DISPLAY_CHANGE_DISPLAY_FILLED, &cfg)) {
                msg_Err(vd, "Failed to change display filled state");
                osys->is_display_filled = osys->cfg.is_display_filled;
            }
            osys->cfg.is_display_filled = osys->is_display_filled;
            osys->ch_display_filled = false;

            vout_SendEventDisplayFilled(osys->vout, osys->cfg.is_display_filled);
        }
        /* */
        if (osys->ch_zoom) {
            vout_display_cfg_t cfg = osys->cfg;

            cfg.zoom.num = osys->zoom.num;
            cfg.zoom.den = osys->zoom.den;

            if (10 * cfg.zoom.num <= cfg.zoom.den) {
                cfg.zoom.num = 1;
                cfg.zoom.den = 10;
            } else if (cfg.zoom.num >= 10 * cfg.zoom.den) {
                cfg.zoom.num = 10;
                cfg.zoom.den = 1;
            }

            if (vout_display_Control(vd, VOUT_DISPLAY_CHANGE_ZOOM, &cfg)) {
                msg_Err(vd, "Failed to change zoom");
                osys->zoom.num = osys->cfg.zoom.num;
                osys->zoom.den = osys->cfg.zoom.den;
911
912
            } else {
                osys->fit_window = -1;
913
914
915
916
917
918
919
920
921
            }

            osys->cfg.zoom.num = osys->zoom.num;
            osys->cfg.zoom.den = osys->zoom.den;
            osys->ch_zoom = false;

            vout_SendEventZoom(osys->vout, osys->cfg.zoom.num, osys->cfg.zoom.den);
        }
        /* */
922
923
        if (ch_wm_state) {
            if (vout_display_Control(vd, VOUT_DISPLAY_CHANGE_WINDOW_STATE, wm_state)) {
924
                msg_Err(vd, "Failed to set on top");
925
                wm_state = osys->wm_state;
926
            }
927
            osys->wm_state_initial = wm_state;
928
929

            /* */
930
            vout_SendEventOnTop(osys->vout, osys->wm_state_initial);
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
        }
        /* */
        if (osys->ch_sar) {
            video_format_t source = vd->source;

            if (osys->sar.num > 0 && osys->sar.den > 0) {
                source.i_sar_num = osys->sar.num;
                source.i_sar_den = osys->sar.den;
            } else {
                source.i_sar_num = osys->source.i_sar_num;
                source.i_sar_den = osys->source.i_sar_den;
            }

            if (vout_display_Control(vd, VOUT_DISPLAY_CHANGE_SOURCE_ASPECT, &source)) {
                /* There nothing much we can do. The only reason a vout display
                 * does not support it is because it need the core to add black border
                 * to the video for it.
                 * TODO add black borders ?
                 */
                msg_Err(vd, "Failed to change source AR");
                source = vd->source;
952
953
            } else if (!osys->fit_window) {
                osys->fit_window = 1;
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
            }
            vd->source = source;
            osys->sar.num = source.i_sar_num;
            osys->sar.den = source.i_sar_den;
            osys->ch_sar  = false;

            /* */
            if (osys->sar.num == osys->source.i_sar_num &&
                osys->sar.den == osys->source.i_sar_den)
            {
                vout_SendEventSourceAspect(osys->vout, 0, 0);
            }
            else
            {
                unsigned dar_num, dar_den;
                vlc_ureduce( &dar_num, &dar_den,
                             osys->sar.num * vd->source.i_visible_width,
                             osys->sar.den * vd->source.i_visible_height,
                             65536);
                vout_SendEventSourceAspect(osys->vout, dar_num, dar_den);
            }
975
976
977
            /* If a crop ratio is requested, recompute the parameters */
            if (osys->crop.num > 0 && osys->crop.den > 0)
                osys->ch_crop = true;
978
979
980
981
        }
        /* */
        if (osys->ch_crop) {
            video_format_t source = vd->source;
982

983
984
            unsigned crop_num = osys->crop.num;
            unsigned crop_den = osys->crop.den;
985
986
987
988
            if (crop_num > 0 && crop_den > 0) {
                video_format_t fmt = osys->source;
                fmt.i_sar_num = source.i_sar_num;
                fmt.i_sar_den = source.i_sar_den;
989
990
                VoutDisplayCropRatio(&osys->crop.left,  &osys->crop.top,
                                     &osys->crop.right, &osys->crop.bottom,
991
992
                                     &fmt, crop_num, crop_den);
            }
993
994
            const int right_max  = osys->source.i_x_offset + osys->source.i_visible_width;
            const int bottom_max = osys->source.i_y_offset + osys->source.i_visible_height;
995
            int left   = VLC_CLIP((int)osys->source.i_x_offset + osys->crop.left,
996
                                0, right_max - 1);
997
            int top    = VLC_CLIP((int)osys->source.i_y_offset + osys->crop.top,
998
999
1000
                                0, bottom_max - 1);
            int right, bottom;
            if (osys->crop.right <= 0)