display.c 51.3 KB
Newer Older
1
/*****************************************************************************
2
 * display.c: "vout display" management
3 4 5 6 7 8
 *****************************************************************************
 * 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
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>
31
#include <stdnoreturn.h>
32 33 34 35 36

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

#include <libvlc.h>

#include "display.h"
45
#include "window.h"
46 47 48 49 50 51 52 53 54 55

#include "event.h"

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 = filter->owner.sys;
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 74 75 76 77 78 79 80 81 82
/*****************************************************************************
 *
 *****************************************************************************/

/**
 * 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)
{
    /* */
83
    vout_display_t *vd = vlc_custom_create(obj, sizeof(*vd), "vout display" );
84 85 86 87 88 89 90 91 92 93 94 95 96

    /* */
    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;
97
    vd->info.needs_event_thread = false;
98
    vd->info.subpicture_chromas = NULL;
99 100

    vd->cfg = cfg;
101
    vd->pool = NULL;
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
    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);

Hannes Domani's avatar
Hannes Domani committed
130 131 132
    video_format_Clean(&vd->source);
    video_format_Clean(&vd->fmt);

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    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;
}
150

151 152
static void vout_display_Manage(vout_display_t *vd)
{
153 154
    if (vd->manage)
        vd->manage(vd);
155 156 157
}

/* */
158
void vout_display_GetDefaultDisplaySize(unsigned *width, unsigned *height,
159 160 161
                                        const video_format_t *source,
                                        const vout_display_cfg_t *cfg)
{
162
    if (cfg->display.width != 0 && cfg->display.height != 0) {
163 164
        *width  = cfg->display.width;
        *height = cfg->display.height;
165
    } else if (cfg->display.width != 0) {
166 167 168
        *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;
169
    } else if (cfg->display.height != 0) {
170 171 172 173 174 175 176 177 178 179 180 181 182
        *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;
183 184 185 186 187 188 189

    if (ORIENT_IS_SWAP(source->orientation)) {

        unsigned store = *width;
        *width = *height;
        *height = store;
    }
190 191 192 193 194 195 196 197 198 199
}

/* */
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));
200
    if (cfg->display.width == 0 || cfg->display.height == 0)
201 202 203
        return;

    /* */
204 205
    unsigned display_width;
    unsigned display_height;
206

207
    video_format_t source_rot;
208
    video_format_ApplyRotation(&source_rot, source);
209 210
    source = &source_rot;

211 212 213 214 215 216 217 218
    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;
219
        vout_display_GetDefaultDisplaySize(&display_width, &display_height,
220 221 222 223 224 225 226 227
                                           source, &cfg_tmp);

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

228 229
    const unsigned width  = source->i_visible_width;
    const unsigned height = source->i_visible_height;
230
    /* Compute the height if we use the width to fill up display_width */
231
    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);
232
    /* And the same but switching width/height */
233
    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);
234

235 236 237 238 239 240 241 242 243
    if (source->projection_mode == PROJECTION_MODE_RECTANGULAR) {
        /* 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;
        }
244
    } else {
245 246
        /* No need to preserve an aspect ratio for 360 video.
         * They can fill the display. */
247
        place->width  = display_width;
248
        place->height = display_height;
249 250 251 252 253 254 255 256 257 258 259
    }

    /*  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
260
        place->x = ((int)cfg->display.width - (int)place->width) / 2;
261 262 263 264 265 266 267 268 269 270 271
        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
272
        place->y = ((int)cfg->display.height - (int)place->height) / 2;
273 274 275 276
        break;
    }
}

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
void vout_display_SendMouseMovedDisplayCoordinates(vout_display_t *vd, video_orientation_t orient_display, int m_x, int m_y, vout_display_place_t *place)
{
    video_format_t source_rot = vd->source;
    video_format_TransformTo(&source_rot, orient_display);

    if (place->width > 0 && place->height > 0) {

        int x = (int)(source_rot.i_x_offset +
                            (int64_t)(m_x - place->x) * source_rot.i_visible_width / place->width);
        int y = (int)(source_rot.i_y_offset +
                            (int64_t)(m_y - place->y) * source_rot.i_visible_height/ place->height);

        video_transform_t transform = video_format_GetTransform(vd->source.orientation, orient_display);

        int store;

        switch (transform) {

            case TRANSFORM_R90:
                store = x;
                x = y;
                y = vd->source.i_visible_height - store;
                break;
            case TRANSFORM_R180:
                x = vd->source.i_visible_width - x;
                y = vd->source.i_visible_height - y;
                break;
            case TRANSFORM_R270:
                store = x;
                x = vd->source.i_visible_width - y;
                y = store;
                break;
            case TRANSFORM_HFLIP:
                x = vd->source.i_visible_width - x;
                break;
            case TRANSFORM_VFLIP:
                y = vd->source.i_visible_height - y;
                break;
            case TRANSFORM_TRANSPOSE:
                store = x;
                x = y;
                y = store;
                break;
            case TRANSFORM_ANTI_TRANSPOSE:
                store = x;
                x = vd->source.i_visible_width - y;
                y = vd->source.i_visible_height - store;
                break;
            default:
                break;
        }

        vout_display_SendEventMouseMoved (vd, x, y);
    }
}

333
typedef struct {
334 335 336 337 338 339
    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;
340
    vlc_rational_t sar_initial;
341 342

    /* */
Laurent Aimar's avatar
Laurent Aimar committed
343 344
    unsigned width_saved;
    unsigned height_saved;
345 346 347 348 349 350

    /* */
    bool ch_display_filled;
    bool is_display_filled;

    bool ch_zoom;
351
    vlc_rational_t zoom;
352
#if defined(_WIN32) || defined(__OS2__)
353 354
    bool ch_wm_state;
    unsigned wm_state;
355 356
    unsigned wm_state_initial;
#endif
357
    bool ch_sar;
358
    vlc_rational_t sar;
359 360 361

    bool ch_crop;
    struct {
362 363 364 365
        int      left;
        int      top;
        int      right;
        int      bottom;
366 367 368 369
        unsigned num;
        unsigned den;
    } crop;

370 371 372
    bool ch_viewpoint;
    vlc_viewpoint_t viewpoint;

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

    /* 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;
403

404 405
    int  fit_window;

406 407 408 409
    struct {
        vlc_thread_t thread;
        block_fifo_t *fifo;
    } event;
410
} vout_display_owner_sys_t;
411

412
static int VoutDisplayCreateRender(vout_display_t *vd)
413 414 415 416 417 418 419 420 421 422 423 424 425
{
    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;

426 427 428 429 430 431 432 433
    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;
434
    if (!convert)
435
        return 0;
436

437 438
    msg_Dbg(vd, "A filter to adapt decoder %4.4s to display %4.4s is needed",
            (const char *)&v_src.i_chroma, (const char *)&v_dst.i_chroma);
439

440 441 442 443 444 445 446 447
    filter_owner_t owner = {
        .sys = vd,
        .video = {
            .buffer_new = VideoBufferNew,
        },
    };

    osys->filters = filter_chain_NewVideo(vd, false, &owner);
448 449
    if (unlikely(osys->filters == NULL))
        abort(); /* TODO critical */
450 451 452 453 454 455

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

    /* */
456 457
    int ret;

458
    for (int i = 0; i < 1 + (v_dst_cmp.i_chroma != v_dst.i_chroma); i++) {
Hannes Domani's avatar
Hannes Domani committed
459
        es_format_t dst;
460

461 462 463
        es_format_InitFromVideo(&dst, i == 0 ? &v_dst : &v_dst_cmp);

        filter_chain_Reset(osys->filters, &src, &dst);
464
        ret = filter_chain_AppendConverter(osys->filters, &src, &dst);
Hannes Domani's avatar
Hannes Domani committed
465
        es_format_Clean(&dst);
466
        if (ret == 0)
467 468
            break;
    }
Hannes Domani's avatar
Hannes Domani committed
469
    es_format_Clean(&src);
470

471
    if (ret != 0) {
472
        msg_Err(vd, "Failed to adapt decoder format to display");
473
        filter_chain_Delete(osys->filters);
474
        osys->filters = NULL;
475
    }
476
    return ret;
477 478 479 480 481 482 483 484 485 486
}

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

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

487
static int VoutDisplayResetRender(vout_display_t *vd)
488 489
{
    VoutDisplayDestroyRender(vd);
490
    return VoutDisplayCreateRender(vd);
491
}
492

493
static void VoutDisplayEventMouse(vout_display_t *vd, int event, va_list args)
494 495 496
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

497 498
    vlc_mutex_lock(&osys->lock);

499
    /* */
500 501
    vlc_mouse_t m = osys->mouse.state;
    bool is_ignored = false;
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518

    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);

519 520 521 522 523
        //msg_Dbg(vd, "VoutDisplayEvent 'mouse' @%d,%d", x, y);

        m.i_x = x;
        m.i_y = y;
        m.b_double_click = false;
524 525 526 527 528 529 530
        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;

531 532 533 534
        /* 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;
535
            break;
536
        }
537 538 539 540 541 542 543 544 545 546 547

        /* */
        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;
    }
548
    case VOUT_DISPLAY_EVENT_MOUSE_DOUBLE_CLICK:
549 550 551
        msg_Dbg(vd, "VoutDisplayEvent 'double click'");

        m.b_double_click = true;
552 553
        break;
    default:
554
        vlc_assert_unreachable();
555 556
    }

557 558
    if (is_ignored) {
        vlc_mutex_unlock(&osys->lock);
559
        return;
560
    }
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578

    /* 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;

    /* */
579
    osys->mouse.ch_activity = true;
580 581 582 583 584 585
    if (!vd->info.has_hide_mouse)
        osys->mouse.last_moved = mdate();

    /* */
    vout_SendEventMouseVisible(osys->vout);
    vout_SendDisplayEventMouse(osys->vout, &m);
586
    vlc_mutex_unlock(&osys->lock);
587 588
}

589
noreturn static void *VoutDisplayEventKeyDispatch(void *data)
590
{
591
    vout_display_owner_sys_t *osys = data;
592 593 594

    for (;;) {
        block_t *event = block_FifoGet(osys->event.fifo);
595 596

        int cancel = vlc_savecancel();
597 598 599 600 601

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

        vlc_restorecancel(cancel);
604 605 606 607 608 609 610 611 612 613 614 615
    }
}

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,
616
                      osys, VLC_THREAD_PRIORITY_LOW)) {
617 618 619 620 621 622 623 624 625 626 627 628
            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);
    }
}

629 630 631 632 633 634 635 636 637 638 639 640 641
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);
642
        if (vd->info.needs_event_thread)
643
            VoutDisplayEventKey(vd, key);
644 645
        else
            vout_SendEventKey(osys->vout, key);
646 647
        break;
    }
648 649 650 651 652 653 654
    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;
655 656 657 658 659 660

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

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

661 662 663 664 665 666
        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);
667 668
        break;
    }
669
#if defined(_WIN32) || defined(__OS2__)
670 671
    case VOUT_DISPLAY_EVENT_WINDOW_STATE: {
        const unsigned state = va_arg(args, unsigned);
672

673
        msg_Dbg(vd, "VoutDisplayEvent 'window state' %u", state);
674 675

        vlc_mutex_lock(&osys->lock);
676 677 678
        if (state != osys->wm_state) {
            osys->ch_wm_state = true;
            osys->wm_state = state;
679 680 681 682
        }
        vlc_mutex_unlock(&osys->lock);
        break;
    }
683
#endif
684 685 686
    case VOUT_DISPLAY_EVENT_DISPLAY_SIZE: {
        const int width  = (int)va_arg(args, int);
        const int height = (int)va_arg(args, int);
687
        msg_Dbg(vd, "VoutDisplayEvent 'resize' %dx%d", width, height);
688 689

        /* */
690 691
        vlc_mutex_lock(&osys->lock);

692 693 694
        osys->ch_display_size   = true;
        osys->display_width     = width;
        osys->display_height    = height;
695 696

        vlc_mutex_unlock(&osys->lock);
697 698 699 700 701 702 703 704
        break;
    }

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

        /* */
        assert(vd->info.has_pictures_invalid);
705 706

        vlc_mutex_lock(&osys->lock);
707
        osys->reset_pictures = true;
708
        vlc_mutex_unlock(&osys->lock);
709 710 711 712 713 714 715 716 717
        break;
    }
    default:
        msg_Err(vd, "VoutDisplayEvent received event %d", event);
        /* TODO add an assert when all event are handled */
        break;
    }
}

718
static vout_window_t *VoutDisplayNewWindow(vout_display_t *vd, unsigned type)
719 720
{
    vout_display_owner_sys_t *osys = vd->owner.sys;
721
    vout_window_t *window = vout_NewDisplayWindow(osys->vout, type);
722 723 724
    if (window != NULL)
        vout_display_window_Attach(window, vd);
    return window;
725
}
726

727 728 729 730
static void VoutDisplayDelWindow(vout_display_t *vd, vout_window_t *window)
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

731 732
    if (window != NULL)
        vout_display_window_Detach(window);
733
    vout_DeleteDisplayWindow(osys->vout, window);
734 735
}

736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
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);
757
    vout_SetDisplayWindowSize(osys->vout, display_width, display_height);
758 759
}

760
static void VoutDisplayCropRatio(int *left, int *top, int *right, int *bottom,
761 762 763 764 765 766 767
                                 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) {
768 769 770 771
        *left   = (source->i_visible_width - scaled_width) / 2;
        *top    = 0;
        *right  = *left + scaled_width;
        *bottom = *top  + source->i_visible_height;
772
    } else {
773 774 775 776
        *left   = 0;
        *top    = (source->i_visible_height - scaled_height) / 2;
        *right  = *left + source->i_visible_width;
        *bottom = *top  + scaled_height;
777 778 779
    }
}

780
bool vout_ManageDisplay(vout_display_t *vd, bool allow_reset_pictures)
781 782 783 784 785 786 787
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

    vout_display_Manage(vd);

    /* Handle mouse timeout */
    const mtime_t date = mdate();
788 789 790 791
    bool  hide_mouse = false;

    vlc_mutex_lock(&osys->lock);

792 793
    if (!osys->mouse.is_hidden &&
        osys->mouse.last_moved + osys->mouse.hide_timeout < date) {
794 795
        osys->mouse.is_hidden = hide_mouse = true;
    } else if (osys->mouse.ch_activity) {
796 797
        if (osys->mouse.is_hidden)
            vout_HideWindowMouse(osys->vout, false);
798 799 800 801 802 803
        osys->mouse.is_hidden = false;
    }
    osys->mouse.ch_activity = false;
    vlc_mutex_unlock(&osys->lock);

    if (hide_mouse) {
804 805 806
        msg_Dbg(vd, "auto hiding mouse cursor");
        if (vout_HideWindowMouse(osys->vout, true) != VLC_SUCCESS
         && !vd->info.has_hide_mouse)
807 808 809 810
            vout_display_Control(vd, VOUT_DISPLAY_HIDE_MOUSE);
        vout_SendEventMouseHidden(osys->vout);
    }

811
    bool reset_render = false;
812
    for (;;) {
813 814 815 816 817 818 819

        vlc_mutex_lock(&osys->lock);

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

820
#if defined(_WIN32) || defined(__OS2__)
821 822 823
        bool ch_wm_state  = osys->ch_wm_state;
        unsigned wm_state  = osys->wm_state;
        osys->ch_wm_state = false;
824
#endif
825

826 827 828 829 830
        bool ch_display_size       = osys->ch_display_size;
        int  display_width         = osys->display_width;
        int  display_height        = osys->display_height;
        osys->ch_display_size = false;

831 832 833 834 835 836 837
        bool reset_pictures;
        if (allow_reset_pictures) {
            reset_pictures = osys->reset_pictures;
            osys->reset_pictures = false;
        } else {
            reset_pictures = false;
        }
838 839 840 841 842 843

        vlc_mutex_unlock(&osys->lock);

        if (!ch_fullscreen &&
            !ch_display_size &&
            !reset_pictures &&
844 845
            !osys->ch_display_filled &&
            !osys->ch_zoom &&
846
#if defined(_WIN32) || defined(__OS2__)
847
            !ch_wm_state &&
848
#endif
849
            !osys->ch_sar &&
850 851
            !osys->ch_crop &&
            !osys->ch_viewpoint) {
852 853 854 855 856 857

            if (!osys->cfg.is_fullscreen && osys->fit_window != 0) {
                VoutDisplayFitWindow(vd, osys->fit_window == -1);
                osys->fit_window = 0;
                continue;
            }
858
            break;
859
        }
860 861

        /* */
862
        if (ch_fullscreen) {
863 864
            if (vout_display_Control(vd, VOUT_DISPLAY_CHANGE_FULLSCREEN,
                                     is_fullscreen) == VLC_SUCCESS) {
865 866 867 868 869 870
                osys->cfg.is_fullscreen = is_fullscreen;

                if (!is_fullscreen)
                    vout_SetDisplayWindowSize(osys->vout, osys->width_saved,
                                              osys->height_saved);
            } else {
871
                is_fullscreen = osys->cfg.is_fullscreen;
872 873

                msg_Err(vd, "Failed to set fullscreen");
874 875 876 877
            }
        }

        /* */
878
        if (ch_display_size) {
879
            vout_display_cfg_t cfg = osys->cfg;
880 881
            cfg.display.width  = display_width;
            cfg.display.height = display_height;
882

883 884 885
            osys->width_saved  = osys->cfg.display.width;
            osys->height_saved = osys->cfg.display.height;

886
            vout_display_Control(vd, VOUT_DISPLAY_CHANGE_DISPLAY_SIZE, &cfg);
887

888 889
            osys->cfg.display.width  = display_width;
            osys->cfg.display.height = display_height;
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
        }
        /* */
        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;
        }
        /* */
        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;
923 924
            } else {
                osys->fit_window = -1;
925 926 927 928 929 930
            }

            osys->cfg.zoom.num = osys->zoom.num;
            osys->cfg.zoom.den = osys->zoom.den;
            osys->ch_zoom = false;
        }
931
#if defined(_WIN32) || defined(__OS2__)
932
        /* */
933 934
        if (ch_wm_state) {
            if (vout_display_Control(vd, VOUT_DISPLAY_CHANGE_WINDOW_STATE, wm_state)) {
935
                msg_Err(vd, "Failed to set on top");
936
                wm_state = osys->wm_state;
937
            }
938
            osys->wm_state_initial = wm_state;
939
        }
940
#endif
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
        /* */
        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;
961 962
            } else if (!osys->fit_window) {
                osys->fit_window = 1;
963 964 965 966 967 968
            }
            vd->source = source;
            osys->sar.num = source.i_sar_num;
            osys->sar.den = source.i_sar_den;
            osys->ch_sar  = false;

969
            /* If a crop ratio is requested, recompute the parameters */
970
            if (osys->crop.num != 0 && osys->crop.den != 0)
971
                osys->ch_crop = true;
972 973 974 975
        }
        /* */
        if (osys->ch_crop) {
            video_format_t source = vd->source;
976

977 978
            unsigned crop_num = osys->crop.num;
            unsigned crop_den = osys->crop.den;
979
            if (crop_num != 0 && crop_den != 0) {
980 981 982
                video_format_t fmt = osys->source;
                fmt.i_sar_num = source.i_sar_num;
                fmt.i_sar_den = source.i_sar_den;
983 984
                VoutDisplayCropRatio(&osys->crop.left,  &osys->crop.top,
                                     &osys->crop.right, &osys->crop.bottom,
985 986
                                     &fmt, crop_num, crop_den);
            }
987 988
            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;
989
            int left   = VLC_CLIP((int)osys->source.i_x_offset + osys->crop.left,
990
                                0, right_max - 1);
991
            int top    = VLC_CLIP((int)osys->source.i_y_offset + osys->crop.top,
992 993 994 995 996 997
                                0, bottom_max - 1);
            int right, bottom;
            if (osys->crop.right <= 0)
                right = (int)(osys->source.i_x_offset + osys->source.i_visible_width) + osys->crop.right;
            else
                right = (int)osys->source.i_x_offset + osys->crop.right;
998
            right = VLC_CLIP(right, left + 1, right_max);
999 1000 1001 1002
            if (osys->crop.bottom <= 0)
                bottom = (int)(osys->source.i_y_offset + osys->source.i_visible_height) + osys->crop.bottom;
            else
                bottom = (int)osys->source.i_y_offset + osys->crop.bottom;
1003
            bottom = VLC_CLIP(bottom, top + 1, bottom_max);
1004 1005 1006 1007 1008 1009 1010 1011 1012

            source.i_x_offset       = left;
            source.i_y_offset       = top;
            source.i_visible_width  = right - left;
            source.i_visible_height = bottom - top;
            video_format_Print(VLC_OBJECT(vd), "SOURCE ", &osys->source);
            video_format_Print(VLC_OBJECT(vd), "CROPPED", &source);
            if (vout_display_Control(vd, VOUT_DISPLAY_CHANGE_SOURCE_CROP, &source)) {
                msg_Err(vd, "Failed to change source crop TODO implement crop at core");
1013 1014

                source = vd->source;
Steve Lhomme's avatar
Steve Lhomme committed
1015 1016
                crop_num = 0;
                crop_den = 0;
1017 1018 1019
                /* FIXME implement cropping in the core if not supported by the
                 * vout module (easy)
                 */
1020 1021
            } else if (!osys->fit_window) {
                osys->fit_window = 1;
1022 1023
            }
            vd->source = source;
1024 1025 1026 1027 1028 1029 1030
            osys->crop.left   = source.i_x_offset - osys->source.i_x_offset;
            osys->crop.top    = source.i_y_offset - osys->source.i_y_offset;
            /* FIXME for right/bottom we should keep the 'type' border vs window */
            osys->crop.right  = (source.i_x_offset + source.i_visible_width) -
                                (osys->source.i_x_offset + osys->source.i_visible_width);
            osys->crop.bottom = (source.i_y_offset + source.i_visible_height) -
                                (osys->source.i_y_offset + osys->source.i_visible_height);
1031 1032 1033 1034
            osys->crop.num    = crop_num;
            osys->crop.den    = crop_den;
            osys->ch_crop = false;
        }
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
        if (osys->ch_viewpoint) {
            vout_display_cfg_t cfg = osys->cfg;

            cfg.viewpoint = osys->viewpoint;

            if (vout_display_Control(vd, VOUT_DISPLAY_CHANGE_VIEWPOINT, &cfg)) {
                msg_Err(vd, "Failed to change Viewpoint");
                osys->viewpoint = osys->cfg.viewpoint;
            }
            osys->cfg.viewpoint = osys->viewpoint;
            osys->ch_viewpoint  = false;
        }
1047 1048

        /* */
1049
        if (reset_pictures) {
1050 1051 1052 1053
            if (vout_display_Control(vd, VOUT_DISPLAY_RESET_PICTURES)) {
                /* FIXME what to do here ? */
                msg_Err(vd, "Failed to reset pictures (probably fatal)");
            }
1054
            reset_render = true;
1055 1056
        }
    }
1057
    if (reset_render)
1058
        VoutDisplayResetRender(vd);
1059 1060

    return reset_render;
1061 1062 1063 1064 1065 1066
}

bool vout_AreDisplayPicturesInvalid(vout_display_t *vd)
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

1067 1068 1069 1070 1071
    vlc_mutex_lock(&osys->lock);
    const bool reset_pictures = osys->reset_pictures;
    vlc_mutex_unlock(&osys->lock);

    return reset_pictures;
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
}

bool vout_IsDisplayFiltered(vout_display_t *vd)
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

    return osys->filters != NULL;
}

picture_t *vout_FilterDisplay(vout_display_t *vd, picture_t *picture)
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

1085
    assert(osys->filters);
1086 1087 1088 1089
    if (filter_chain_GetLength(osys->filters) <= 0) {
        picture_Release(picture);
        return NULL;
    }
1090 1091 1092
    return filter_chain_VideoFilter(osys->filters, picture);
}

1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
void vout_UpdateDisplaySourceProperties(vout_display_t *vd, const video_format_t *source)
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

    if (source->i_sar_num * osys->source.i_sar_den !=
        source->i_sar_den * osys->source.i_sar_num) {

        osys->source.i_sar_num = source->i_sar_num;
        osys->source.i_sar_den = source->i_sar_den;
        vlc_ureduce(&osys->source.i_sar_num, &osys->source.i_sar_den,
                    osys->source.i_sar_num, osys->source.i_sar_den, 0);

        /* FIXME it will override any AR that the user would have forced */
        osys->ch_sar = true;
        osys->sar.num = osys->source.i_sar_num;
        osys->sar.den = osys->source.i_sar_den;
    }
    if (source->i_x_offset       != osys->source.i_x_offset ||
        source->i_y_offset       != osys->source.i_y_offset ||
        source->i_visible_width  != osys->source.i_visible_width ||
        source->i_visible_height != osys->source.i_visible_height) {

        video_format_CopyCrop(&osys->source, source);

        /* Force the vout to reapply the current user crop settings over the new decoder
         * crop settings. */
        osys->ch_crop = true;
    }
}

1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
void vout_SetDisplayFilled(vout_display_t *vd, bool is_filled)
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

    if (!osys->is_display_filled != !is_filled) {
        osys->ch_display_filled = true;
        osys->is_display_filled = is_filled;
    }
}

1133
void vout_SetDisplayZoom(vout_display_t *vd, unsigned num, unsigned den)
1134 1135 1136
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

1137
    if (num != 0 && den != 0) {
1138 1139 1140 1141 1142 1143
        vlc_ureduce(&num, &den, num, den, 0);
    } else {
        num = 1;
        den = 1;
    }

1144 1145
    if (osys->is_display_filled ||
        osys->zoom.num != num || osys->zoom.den != den) {
1146 1147 1148 1149 1150
        osys->ch_zoom = true;
        osys->zoom.num = num;
        osys->zoom.den = den;
    }
}
1151

1152
void vout_SetDisplayAspect(vout_display_t *vd, unsigned dar_num, unsigned dar_den)
1153 1154 1155
{
    vout_display_owner_sys_t *osys = vd->owner.sys;

1156 1157 1158 1159 1160 1161
    unsigned sar_num, sar_den;
    if (dar_num > 0 && dar_den > 0) {
        sar_num = dar_num * osys->source.i_visible_height;
        sar_den = dar_den * osys->source.i_visible_width;
        vlc_ureduce(&sar_num, &sa