display.c 35.4 KB
Newer Older
1
/*****************************************************************************
2
 * display.c: Android video output module
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
 *****************************************************************************
 * Copyright (C) 2014 VLC authors and VideoLAN
 *
 * Authors: Thomas Guillem <thomas@gllm.fr>
 *          Felix Abecassis <felix.abecassis@gmail.com>
 *          Ming Hu <tewilove@gmail.com>
 *          Ludovic Fauvet <etix@l0cal.com>
 *          Sébastien Toque <xilasz@gmail.com>
 *
 * 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
 * (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
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * 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.
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_vout_display.h>
#include <vlc_picture_pool.h>
#include <vlc_filter.h>

#include <dlfcn.h>

39
#include "display.h"
40 41 42 43 44 45 46 47 48 49
#include "utils.h"

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
#define USE_ANWP
#define CHROMA_TEXT N_("Chroma used")
#define CHROMA_LONGTEXT N_(\
    "Force use of a specific chroma for output. Default is RGB32.")

50
#define CFG_PREFIX "android-display-"
51 52
static int  Open (vlc_object_t *);
static void Close(vlc_object_t *);
53
static void SubpicturePrepare(vout_display_t *vd, subpicture_t *subpicture);
54 55 56 57 58

vlc_module_begin()
    set_category(CAT_VIDEO)
    set_subcategory(SUBCAT_VIDEO_VOUT)
    set_description(N_("Android video output"))
59
    set_capability("vout display", 260)
60
    add_shortcut("android-display")
61
    add_string(CFG_PREFIX "chroma", NULL, CHROMA_TEXT, CHROMA_LONGTEXT, true)
62 63 64 65 66 67 68
    set_callbacks(Open, Close)
vlc_module_end()

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/

69
#define THREAD_NAME "android-display"
70 71 72 73 74 75 76 77

static const vlc_fourcc_t subpicture_chromas[] =
{
    VLC_CODEC_RGBA,
    0
};

static picture_pool_t   *Pool  (vout_display_t *, unsigned);
78
static void             Prepare(vout_display_t *, picture_t *, subpicture_t *);
79 80 81 82 83 84 85 86 87 88 89 90
static void             Display(vout_display_t *, picture_t *, subpicture_t *);
static int              Control(vout_display_t *, int, va_list);

typedef struct android_window android_window;
struct android_window
{
    video_format_t fmt;
    int i_android_hal;
    unsigned int i_angle;
    unsigned int i_pic_count;
    unsigned int i_min_undequeued;
    bool b_use_priv;
91
    bool b_opaque;
92

93
    enum AWindow_ID id;
94
    ANativeWindow *p_surface;
95
    jobject       *p_jsurface;
96
    native_window_priv *p_surface_priv;
97 98
};

99 100 101 102 103 104 105
typedef struct buffer_bounds buffer_bounds;
struct buffer_bounds
{
    uint8_t *p_pixels;
    ARect bounds;
};

106 107
struct vout_display_sys_t
{
108
    vout_window_t *embed;
109 110
    picture_pool_t *pool;

111 112 113
    int i_display_width;
    int i_display_height;

114 115
    AWindowHandler *p_awh;
    native_window_api_t *anw;
116 117
    native_window_priv_api_t anwp;
    bool b_has_anwp;
118 119 120 121

    android_window *p_window;
    android_window *p_sub_window;

122
    bool b_sub_invalid;
123 124
    filter_t *p_spu_blend;
    picture_t *p_sub_pic;
125
    buffer_bounds *p_sub_buffer_bounds;
126
    bool b_sub_pic_locked;
127
    int64_t i_sub_last_order;
128
    ARect sub_last_region;
129 130 131 132 133 134

    bool b_has_subpictures;

    uint8_t hash[16];
};

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
#define PRIV_WINDOW_FORMAT_YV12 0x32315659

static inline int ChromaToAndroidHal(vlc_fourcc_t i_chroma)
{
    switch (i_chroma) {
        case VLC_CODEC_YV12:
        case VLC_CODEC_I420:
            return PRIV_WINDOW_FORMAT_YV12;
        case VLC_CODEC_RGB16:
            return WINDOW_FORMAT_RGB_565;
        case VLC_CODEC_RGB32:
            return WINDOW_FORMAT_RGBX_8888;
        case VLC_CODEC_RGBA:
            return WINDOW_FORMAT_RGBA_8888;
        default:
            return -1;
    }
}

154 155
static int UpdateWindowSize(vout_display_sys_t *sys, video_format_t *p_fmt,
                            bool b_cropped)
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
{
    unsigned int i_width, i_height;
    unsigned int i_sar_num = 1, i_sar_den = 1;
    video_format_t rot_fmt;

    video_format_ApplyRotation(&rot_fmt, p_fmt);

    if (rot_fmt.i_sar_num != 0 && rot_fmt.i_sar_den != 0) {
        i_sar_num = rot_fmt.i_sar_num;
        i_sar_den = rot_fmt.i_sar_den;
    }
    if (b_cropped) {
        i_width = rot_fmt.i_visible_width;
        i_height = rot_fmt.i_visible_height;
    } else {
        i_width = rot_fmt.i_width;
        i_height = rot_fmt.i_height;
    }

175 176 177 178
    AWindowHandler_setWindowLayout(sys->p_awh, i_width, i_height,
                                   rot_fmt.i_visible_width,
                                   rot_fmt.i_visible_height,
                                   i_sar_num, i_sar_den);
179 180 181
    return 0;
}

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
static void AndroidOpaquePicture_DetachVout(picture_t *p_pic)
{
    picture_sys_t *p_picsys = p_pic->p_sys;

    vlc_mutex_lock(&p_picsys->priv.hw.lock);
    p_pic->p_sys->p_vd_sys = NULL;
    /* Release p_picsys if references from VOUT and from decoder are NULL */
    if (!p_picsys->p_vd_sys && !p_picsys->priv.hw.p_dec)
    {
        vlc_mutex_unlock(&p_picsys->priv.hw.lock);
        vlc_mutex_destroy(&p_picsys->priv.hw.lock);
        free(p_picsys);
    }
    else
        vlc_mutex_unlock(&p_picsys->priv.hw.lock);
    free(p_pic);
}

static picture_t *PictureAlloc(vout_display_sys_t *sys, video_format_t *fmt,
                               bool b_opaque)
202 203 204 205 206 207 208 209 210 211 212
{
    picture_t *p_pic;
    picture_resource_t rsc;
    picture_sys_t *p_picsys = calloc(1, sizeof(*p_picsys));

    if (unlikely(p_picsys == NULL))
        return NULL;

    p_picsys->p_vd_sys = sys;

    memset(&rsc, 0, sizeof(picture_resource_t));
213 214 215 216
    rsc.p_sys = p_picsys;

    if (b_opaque)
    {
217 218
        p_picsys->priv.hw.p_surface = sys->p_window->p_surface;
        p_picsys->priv.hw.p_jsurface =  sys->p_window->p_jsurface;
219 220 221 222
        p_picsys->priv.hw.i_index = -1;
        vlc_mutex_init(&p_picsys->priv.hw.lock);
        rsc.pf_destroy = AndroidOpaquePicture_DetachVout;
    }
223 224 225 226 227 228 229 230 231 232 233 234

    p_pic = picture_NewFromResource(fmt, &rsc);
    if (!p_pic)
    {
        free(p_picsys);
        return NULL;
    }
    return p_pic;
}

static void FixSubtitleFormat(vout_display_sys_t *sys)
{
235
    video_format_t *p_subfmt;
236 237
    video_format_t fmt;
    int i_width, i_height;
238
    int i_video_width, i_video_height;
239 240 241
    int i_display_width, i_display_height;
    double aspect;

242 243 244 245
    if (!sys->p_sub_window)
        return;
    p_subfmt = &sys->p_sub_window->fmt;

246
    video_format_ApplyRotation(&fmt, &sys->p_window->fmt);
247

248
    if (fmt.i_visible_width == 0 || fmt.i_visible_height == 0) {
249 250
        i_video_width = fmt.i_width;
        i_video_height = fmt.i_height;
251
    } else {
252 253
        i_video_width = fmt.i_visible_width;
        i_video_height = fmt.i_visible_height;
254
    }
255 256 257

    if (fmt.i_sar_num > 0 && fmt.i_sar_den > 0) {
        if (fmt.i_sar_num >= fmt.i_sar_den)
258
            i_video_width = i_video_width * fmt.i_sar_num / fmt.i_sar_den;
259
        else
260
            i_video_height = i_video_height * fmt.i_sar_den / fmt.i_sar_num;
261 262 263 264 265
    }

    if (sys->p_window->i_angle == 90 || sys->p_window->i_angle == 180) {
        i_display_width = sys->i_display_height;
        i_display_height = sys->i_display_width;
266
        aspect = i_video_height / (double) i_video_width;
267 268 269
    } else {
        i_display_width = sys->i_display_width;
        i_display_height = sys->i_display_height;
270
        aspect = i_video_width / (double) i_video_height;
271 272 273 274 275
    }

    if (i_display_width / aspect < i_display_height) {
        i_width = i_display_width;
        i_height = i_display_width / aspect;
276
    } else {
277 278
        i_width = i_display_height * aspect;
        i_height = i_display_height;
279
    }
280

281 282 283 284 285 286
    // Use the biggest size available
    if (i_width * i_height < i_video_width * i_video_height) {
        i_width = i_video_width;
        i_height = i_video_height;
    }

287 288 289 290 291 292 293 294
    p_subfmt->i_width =
    p_subfmt->i_visible_width = i_width;
    p_subfmt->i_height =
    p_subfmt->i_visible_height = i_height;
    p_subfmt->i_x_offset = 0;
    p_subfmt->i_y_offset = 0;
    p_subfmt->i_sar_num = 1;
    p_subfmt->i_sar_den = 1;
295
    sys->b_sub_invalid = true;
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
}

#define ALIGN_16_PIXELS( x ) ( ( ( x ) + 15 ) / 16 * 16 )
static void SetupPictureYV12(picture_t *p_picture, uint32_t i_in_stride)
{
    /* according to document of android.graphics.ImageFormat.YV12 */
    int i_stride = ALIGN_16_PIXELS(i_in_stride);
    int i_c_stride = ALIGN_16_PIXELS(i_stride / 2);

    p_picture->p->i_pitch = i_stride;

    /* Fill chroma planes for planar YUV */
    for (int n = 1; n < p_picture->i_planes; n++)
    {
        const plane_t *o = &p_picture->p[n-1];
        plane_t *p = &p_picture->p[n];

        p->p_pixels = o->p_pixels + o->i_lines * o->i_pitch;
        p->i_pitch  = i_c_stride;
        p->i_lines  = p_picture->format.i_height / 2;
        /*
          Explicitly set the padding lines of the picture to black (127 for YUV)
          since they might be used by Android during rescaling.
        */
        int visible_lines = p_picture->format.i_visible_height / 2;
        if (visible_lines < p->i_lines)
            memset(&p->p_pixels[visible_lines * p->i_pitch], 127, (p->i_lines - visible_lines) * p->i_pitch);
    }

    if (vlc_fourcc_AreUVPlanesSwapped(p_picture->format.i_chroma,
                                      VLC_CODEC_YV12)) {
        uint8_t *p_tmp = p_picture->p[1].p_pixels;
        p_picture->p[1].p_pixels = p_picture->p[2].p_pixels;
        p_picture->p[2].p_pixels = p_tmp;
    }
}

333
static void AndroidWindow_DisconnectSurface(vout_display_sys_t *sys,
334 335
                                            android_window *p_window,
                                            bool b_clear)
336
{
337
    if (p_window->p_surface_priv) {
338
        sys->anwp.disconnect(p_window->p_surface_priv);
339
        p_window->p_surface_priv = NULL;
340
    }
341
    if (p_window->p_surface) {
342
        AWindowHandler_releaseANativeWindow(sys->p_awh, p_window->id, b_clear);
343
        p_window->p_surface = NULL;
344
    }
345
}
346

347 348 349
static int AndroidWindow_ConnectSurface(vout_display_sys_t *sys,
                                        android_window *p_window)
{
350
    if (!p_window->p_surface) {
351 352 353
        p_window->p_surface = AWindowHandler_getANativeWindow(sys->p_awh,
                                                              p_window->id);
        if (!p_window->p_surface)
354
            return -1;
355 356 357
        if (p_window->b_opaque)
            p_window->p_jsurface = AWindowHandler_getSurface(sys->p_awh,
                                                             p_window->id);
358 359 360 361 362
    }

    return 0;
}

363
static android_window *AndroidWindow_New(vout_display_t *vd,
364
                                         video_format_t *p_fmt,
365
                                         enum AWindow_ID id,
366 367
                                         bool b_use_priv)
{
368 369
    vout_display_sys_t *sys = vd->sys;
    android_window *p_window = NULL;
370

371
    p_window = calloc(1, sizeof(android_window));
372
    if (!p_window)
373
        goto error;
374

375 376 377
    p_window->id = id;
    p_window->b_opaque = p_fmt->i_chroma == VLC_CODEC_ANDROID_OPAQUE;
    if (!p_window->b_opaque) {
378
        p_window->b_use_priv = sys->b_has_anwp && b_use_priv;
379 380

        p_window->i_android_hal = ChromaToAndroidHal(p_fmt->i_chroma);
381 382
        if (p_window->i_android_hal == -1)
            goto error;
383 384
    }

385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
    switch (p_fmt->orientation)
    {
        case ORIENT_ROTATED_90:
            p_window->i_angle = 90;
            break;
        case ORIENT_ROTATED_180:
            p_window->i_angle = 180;
            break;
        case ORIENT_ROTATED_270:
            p_window->i_angle = 270;
            break;
        default:
            p_window->i_angle = 0;
    }
    if (p_window->b_use_priv)
400
        p_window->fmt = *p_fmt;
401
    else
402 403
        video_format_ApplyRotation(&p_window->fmt, p_fmt);
    p_window->i_pic_count = 1;
404

405 406 407 408
    if (AndroidWindow_ConnectSurface(sys, p_window) != 0)
    {
        if (id == AWindow_Video)
            msg_Err(vd, "can't get Video Surface");
409 410
        else if (id == AWindow_Subtitles)
            msg_Err(vd, "can't get Subtitles Surface");
411
        goto error;
412 413
    }

414
    return p_window;
415 416 417
error:
    free(p_window);
    return NULL;
418 419
}

420
static void AndroidWindow_Destroy(vout_display_t *vd,
421
                                  android_window *p_window, bool b_clear)
422
{
423
    AndroidWindow_DisconnectSurface(vd->sys, p_window, b_clear);
424 425 426 427 428 429
    free(p_window);
}

static int AndroidWindow_UpdateCrop(vout_display_sys_t *sys,
                                    android_window *p_window)
{
430
    if (!p_window->p_surface_priv)
431 432
        return -1;

433 434 435 436 437
    return sys->anwp.setCrop(p_window->p_surface_priv,
                             p_window->fmt.i_x_offset,
                             p_window->fmt.i_y_offset,
                             p_window->fmt.i_visible_width,
                             p_window->fmt.i_visible_height);
438 439
}

440
static int AndroidWindow_SetupANWP(vout_display_sys_t *sys,
441 442
                                   android_window *p_window,
                                   bool b_java_configured)
443
{
444 445
    unsigned int i_max_buffer_count = 0;

446
    if (!p_window->p_surface_priv)
447
        p_window->p_surface_priv = sys->anwp.connect(p_window->p_surface);
448

449
    if (!p_window->p_surface_priv)
450 451
        goto error;

452
    if (sys->anwp.setUsage(p_window->p_surface_priv, false, 0) != 0)
453 454
        goto error;

455
    if (!b_java_configured
456 457 458 459
        && sys->anwp.setBuffersGeometry(p_window->p_surface_priv,
                                        p_window->fmt.i_width,
                                        p_window->fmt.i_height,
                                        p_window->i_android_hal) != 0)
460 461
        goto error;

462 463
    sys->anwp.getMinUndequeued(p_window->p_surface_priv,
                               &p_window->i_min_undequeued);
464

465
    sys->anwp.getMaxBufferCount(p_window->p_surface_priv, &i_max_buffer_count);
466

467
    if ((p_window->i_min_undequeued + p_window->i_pic_count) >
468 469
         i_max_buffer_count)
        p_window->i_pic_count = i_max_buffer_count - p_window->i_min_undequeued;
470

471 472 473
    if (sys->anwp.setBufferCount(p_window->p_surface_priv,
                                 p_window->i_pic_count +
                                 p_window->i_min_undequeued) != 0)
474 475
        goto error;

476 477
    if (sys->anwp.setOrientation(p_window->p_surface_priv,
                                 p_window->i_angle) != 0)
478 479 480 481 482 483
        goto error;

    AndroidWindow_UpdateCrop(sys, p_window);

    return 0;
error:
484
    if (p_window->p_surface_priv) {
485
        sys->anwp.disconnect(p_window->p_surface_priv);
486
        p_window->p_surface_priv = NULL;
487 488 489 490 491 492 493
    }
    p_window->b_use_priv = false;
    if (p_window->i_angle != 0)
        video_format_ApplyRotation(&p_window->fmt, &p_window->fmt);
    return -1;
}

494 495 496
static int AndroidWindow_ConfigureJavaSurface(vout_display_sys_t *sys,
                                              android_window *p_window,
                                              bool *p_java_configured)
497
{
498 499 500 501 502 503
    /* setBuffersGeometry is broken before ics. Use
     * AJavaWindow_setBuffersGeometry to configure the surface on the java side
     * synchronously.  AJavaWindow_setBuffersGeometry return en error when you
     * don't need to call it (ie, after ics). if this call succeed, you need to
     * get a new surface handle. That's why AndroidWindow_DisconnectSurface is
     * called here. */
504
    if (AWindowHandler_setBuffersGeometry(sys->p_awh, p_window->id,
505 506 507 508 509
                                          p_window->fmt.i_width,
                                          p_window->fmt.i_height,
                                          p_window->i_android_hal) == VLC_SUCCESS)
    {
        *p_java_configured = true;
510
        AndroidWindow_DisconnectSurface(sys, p_window, false);
511
        if (AndroidWindow_ConnectSurface(sys, p_window) != 0)
512
            return -1;
513 514 515
    } else
        *p_java_configured = false;

Thomas Guillem's avatar
Thomas Guillem committed
516 517 518 519
    return 0;
}

static int AndroidWindow_SetupANW(vout_display_sys_t *sys,
520 521
                                  android_window *p_window,
                                  bool b_java_configured)
Thomas Guillem's avatar
Thomas Guillem committed
522 523 524 525
{
    p_window->i_pic_count = 1;
    p_window->i_min_undequeued = 0;

526
    if (!b_java_configured && sys->anw->setBuffersGeometry)
527
        return sys->anw->setBuffersGeometry(p_window->p_surface,
528 529 530
                                            p_window->fmt.i_width,
                                            p_window->fmt.i_height,
                                            p_window->i_android_hal);
531 532
    else
        return 0;
533 534 535
}

static int AndroidWindow_Setup(vout_display_sys_t *sys,
536 537
                               android_window *p_window,
                               unsigned int i_pic_count)
538
{
539 540
    bool b_java_configured = false;

541 542 543
    if (i_pic_count != 0)
        p_window->i_pic_count = i_pic_count;

544
    if (!p_window->b_opaque) {
Thomas Guillem's avatar
Thomas Guillem committed
545
        int align_pixels;
546
        picture_t *p_pic = PictureAlloc(sys, &p_window->fmt, false);
Thomas Guillem's avatar
Thomas Guillem committed
547 548 549 550 551 552 553

        // For RGB (32 or 16) we need to align on 8 or 4 pixels, 16 pixels for YUV
        align_pixels = (16 / p_pic->p[0].i_pixel_pitch) - 1;
        p_window->fmt.i_height = p_pic->format.i_height;
        p_window->fmt.i_width = (p_pic->format.i_width + align_pixels) & ~align_pixels;
        picture_Release(p_pic);

554 555 556
        if (AndroidWindow_ConfigureJavaSurface(sys, p_window,
                                               &b_java_configured) != 0)
            return -1;
Thomas Guillem's avatar
Thomas Guillem committed
557

558 559 560 561 562 563
        if (!p_window->b_use_priv
            || AndroidWindow_SetupANWP(sys, p_window, b_java_configured) != 0) {
            if (AndroidWindow_SetupANW(sys, p_window, b_java_configured) != 0)
                return -1;
        }
    } else {
Thomas Guillem's avatar
Thomas Guillem committed
564 565
        sys->p_window->i_pic_count = 31; // TODO
        sys->p_window->i_min_undequeued = 0;
566 567 568 569 570 571 572
    }

    return 0;
}

static void AndroidWindow_UnlockPicture(vout_display_sys_t *sys,
                                        android_window *p_window,
573 574
                                        picture_t *p_pic,
                                        bool b_render)
575 576 577 578 579 580 581 582 583
{
    picture_sys_t *p_picsys = p_pic->p_sys;

    if (p_window->b_use_priv) {
        void *p_handle = p_picsys->priv.sw.p_handle;

        if (p_handle == NULL)
            return;

584
        sys->anwp.unlockData(p_window->p_surface_priv, p_handle, b_render);
585
    } else
586
        sys->anw->unlockAndPost(p_window->p_surface);
587 588 589 590 591 592 593 594 595 596 597 598
}

static int AndroidWindow_LockPicture(vout_display_sys_t *sys,
                                     android_window *p_window,
                                     picture_t *p_pic)
{
    picture_sys_t *p_picsys = p_pic->p_sys;

    if (p_window->b_use_priv) {
        void *p_handle;
        int err;

599 600
        err = sys->anwp.lockData(p_window->p_surface_priv,
                                 &p_handle, &p_picsys->priv.sw.buf);
601 602 603 604
        if (err != 0)
            return -1;
        p_picsys->priv.sw.p_handle = p_handle;
    } else {
605
        if (sys->anw->winLock(p_window->p_surface,
606
                              &p_picsys->priv.sw.buf, NULL) != 0)
607 608 609 610 611 612
            return -1;
    }
    if (p_picsys->priv.sw.buf.width < 0 ||
        p_picsys->priv.sw.buf.height < 0 ||
        (unsigned)p_picsys->priv.sw.buf.width < p_window->fmt.i_width ||
        (unsigned)p_picsys->priv.sw.buf.height < p_window->fmt.i_height) {
613
        AndroidWindow_UnlockPicture(sys, p_window, p_pic, false);
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
        return -1;
    }

    p_pic->p[0].p_pixels = p_picsys->priv.sw.buf.bits;
    p_pic->p[0].i_lines = p_picsys->priv.sw.buf.height;
    p_pic->p[0].i_pitch = p_pic->p[0].i_pixel_pitch * p_picsys->priv.sw.buf.stride;

    if (p_picsys->priv.sw.buf.format == PRIV_WINDOW_FORMAT_YV12)
        SetupPictureYV12(p_pic, p_picsys->priv.sw.buf.stride);

    return 0;
}

static void SetRGBMask(video_format_t *p_fmt)
{
    switch(p_fmt->i_chroma) {
        case VLC_CODEC_RGB16:
            p_fmt->i_bmask = 0x0000001f;
            p_fmt->i_gmask = 0x000007e0;
            p_fmt->i_rmask = 0x0000f800;
            break;

        case VLC_CODEC_RGB32:
        case VLC_CODEC_RGBA:
            p_fmt->i_rmask = 0x000000ff;
            p_fmt->i_gmask = 0x0000ff00;
            p_fmt->i_bmask = 0x00ff0000;
            break;
    }
}

static int Open(vlc_object_t *p_this)
{
    vout_display_t *vd = (vout_display_t*)p_this;
    vout_display_sys_t *sys;
    video_format_t sub_fmt;

651 652 653
    if (vd->fmt.projection_mode != PROJECTION_MODE_RECTANGULAR)
        return VLC_EGENERIC;

654 655 656 657
    vout_window_t *embed =
        vout_display_NewWindow(vd, VOUT_WINDOW_TYPE_ANDROID_NATIVE);

    if (embed == NULL)
658
        return VLC_EGENERIC;
659
    assert(embed->handle.anativewindow);
660 661 662 663

    /* Allocate structure */
    vd->sys = sys = (struct vout_display_sys_t*)calloc(1, sizeof(*sys));
    if (!sys)
664
    {
665 666
        vout_display_DeleteWindow(vd, embed);
        return VLC_ENOMEM;
667
    }
668 669 670

    sys->embed = embed;
    sys->p_awh = embed->handle.anativewindow;
671
    sys->anw = AWindowHandler_getANativeWindowAPI(sys->p_awh);
672 673

#ifdef USE_ANWP
674 675
    sys->b_has_anwp = android_loadNativeWindowPrivApi(&sys->anwp) == 0;
    if (!sys->b_has_anwp)
676 677 678
        msg_Warn(vd, "Could not initialize NativeWindow Priv API.");
#endif

679 680 681
    sys->i_display_width = vd->cfg->display.width;
    sys->i_display_height = vd->cfg->display.height;

682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
    if (vd->fmt.i_chroma != VLC_CODEC_ANDROID_OPAQUE) {
        /* Setup chroma */
        char *psz_fcc = var_InheritString(vd, CFG_PREFIX "chroma");
        if (psz_fcc) {
            vd->fmt.i_chroma = vlc_fourcc_GetCodecFromString(VIDEO_ES, psz_fcc);
            free(psz_fcc);
        } else
            vd->fmt.i_chroma = VLC_CODEC_RGB32;

        switch(vd->fmt.i_chroma) {
            case VLC_CODEC_YV12:
                /* avoid swscale usage by asking for I420 instead since the
                 * vout already has code to swap the buffers */
                vd->fmt.i_chroma = VLC_CODEC_I420;
            case VLC_CODEC_I420:
                break;
            case VLC_CODEC_RGB16:
            case VLC_CODEC_RGB32:
            case VLC_CODEC_RGBA:
                SetRGBMask(&vd->fmt);
                video_format_FixRgb(&vd->fmt);
                break;
            default:
                goto error;
        }
Thomas Guillem's avatar
Thomas Guillem committed
707
    }
708

709
    sys->p_window = AndroidWindow_New(vd, &vd->fmt, AWindow_Video, true);
Thomas Guillem's avatar
Thomas Guillem committed
710 711
    if (!sys->p_window)
        goto error;
712

713
    if (AndroidWindow_Setup(sys, sys->p_window, 0) != 0)
Thomas Guillem's avatar
Thomas Guillem committed
714
        goto error;
715

Thomas Guillem's avatar
Thomas Guillem committed
716
    /* use software rotation if we don't use private anw */
717
    if (!sys->p_window->b_opaque && !sys->p_window->b_use_priv)
Thomas Guillem's avatar
Thomas Guillem committed
718
        video_format_ApplyRotation(&vd->fmt, &vd->fmt);
719

720
    msg_Dbg(vd, "using %s", sys->p_window->b_opaque ? "opaque" :
Thomas Guillem's avatar
Thomas Guillem committed
721
            (sys->p_window->b_use_priv ? "ANWP" : "ANW"));
722

723 724 725 726 727 728
    video_format_ApplyRotation(&sub_fmt, &vd->fmt);
    sub_fmt.i_chroma = subpicture_chromas[0];
    SetRGBMask(&sub_fmt);
    video_format_FixRgb(&sub_fmt);
    sys->p_sub_window = AndroidWindow_New(vd, &sub_fmt, AWindow_Subtitles, false);
    if (sys->p_sub_window) {
729

730 731 732 733 734 735
        FixSubtitleFormat(sys);
        sys->i_sub_last_order = -1;

        /* Export the subpicture capability of this vout. */
        vd->info.subpicture_chromas = subpicture_chromas;
    }
736 737 738

    /* Setup vout_display */
    vd->pool    = Pool;
739
    vd->prepare = Prepare;
740 741
    vd->display = Display;
    vd->control = Control;
742
    vd->manage  = NULL;
743
    vd->info.is_slow = !sys->p_window->b_opaque;
744 745

    /* Fix initial state */
746
    vout_display_SendEventFullscreen(vd, true);
747 748 749 750 751

    return VLC_SUCCESS;

error:
    Close(p_this);
752
    return VLC_EGENERIC;
753 754 755 756 757 758 759
}

static void Close(vlc_object_t *p_this)
{
    vout_display_t *vd = (vout_display_t *)p_this;
    vout_display_sys_t *sys = vd->sys;

760 761 762 763 764 765 766 767 768
    /* Check if SPU regions have been properly cleared, and clear them if they
     * were not. */
    if (sys->b_has_subpictures)
    {
        SubpicturePrepare(vd, NULL);
        if (sys->b_sub_pic_locked)
            AndroidWindow_UnlockPicture(sys, sys->p_sub_window, sys->p_sub_pic, true);
    }

769 770 771
    if (sys->pool)
        picture_pool_Release(sys->pool);
    if (sys->p_window)
772
        AndroidWindow_Destroy(vd, sys->p_window, true);
773 774 775 776 777

    if (sys->p_sub_pic)
        picture_Release(sys->p_sub_pic);
    if (sys->p_spu_blend)
        filter_DeleteBlend(sys->p_spu_blend);
778
    free(sys->p_sub_buffer_bounds);
779
    if (sys->p_sub_window)
780
        AndroidWindow_Destroy(vd, sys->p_sub_window, false);
781

782 783
    if (sys->embed)
        vout_display_DeleteWindow(vd, sys->embed);
784 785 786 787

    free(sys);
}

788
static void UnlockPicture(picture_t *p_pic, bool b_render)
789 790
{
    picture_sys_t *p_picsys = p_pic->p_sys;
791
    vout_display_sys_t *sys = p_picsys->p_vd_sys;
792

793 794 795 796 797 798 799 800
    if (p_picsys->b_locked)
    {
        if (sys->p_window->b_opaque)
            AndroidOpaquePicture_Release(p_picsys, b_render);
        else
            AndroidWindow_UnlockPicture(sys, sys->p_window, p_pic, b_render);
        p_picsys->b_locked  = false;
    }
801 802
}

803
static int PoolLockPicture(picture_t *p_pic)
804 805
{
    picture_sys_t *p_picsys = p_pic->p_sys;
806
    vout_display_sys_t *sys = p_picsys->p_vd_sys;
807

808 809
    if (!sys->p_window->b_opaque
     && AndroidWindow_LockPicture(sys, sys->p_window, p_pic) != 0)
810
        return -1;
811

812 813 814 815 816 817 818
    p_picsys->b_locked = true;
    return 0;
}

static void PoolUnlockPicture(picture_t *p_pic)
{
    UnlockPicture(p_pic, false);
819 820 821 822 823 824 825 826 827
}

static picture_pool_t *PoolAlloc(vout_display_t *vd, unsigned requested_count)
{
    vout_display_sys_t *sys = vd->sys;
    picture_pool_t *pool = NULL;
    picture_t **pp_pics = NULL;
    unsigned int i = 0;

Thomas Guillem's avatar
Thomas Guillem committed
828
    msg_Dbg(vd, "PoolAlloc: request %d frames", requested_count);
829
    if (AndroidWindow_Setup(sys, sys->p_window, requested_count) != 0)
Thomas Guillem's avatar
Thomas Guillem committed
830 831
        goto error;

832
    requested_count = sys->p_window->i_pic_count;
Thomas Guillem's avatar
Thomas Guillem committed
833
    msg_Dbg(vd, "PoolAlloc: got %d frames", requested_count);
834

835
    UpdateWindowSize(sys, &sys->p_window->fmt, sys->p_window->b_use_priv);
836 837 838 839 840

    pp_pics = calloc(requested_count, sizeof(picture_t));

    for (i = 0; i < requested_count; i++)
    {
841 842
        picture_t *p_pic = PictureAlloc(sys, &sys->p_window->fmt,
                                        sys->p_window->b_opaque);
843 844 845 846 847 848 849 850 851 852
        if (!p_pic)
            goto error;

        pp_pics[i] = p_pic;
    }

    picture_pool_configuration_t pool_cfg;
    memset(&pool_cfg, 0, sizeof(pool_cfg));
    pool_cfg.picture_count = requested_count;
    pool_cfg.picture       = pp_pics;
853 854
    pool_cfg.lock          = PoolLockPicture;
    pool_cfg.unlock        = PoolUnlockPicture;
855 856 857 858 859 860 861 862 863 864 865
    pool = picture_pool_NewExtended(&pool_cfg);

error:
    if (!pool && pp_pics) {
        for (unsigned j = 0; j < i; j++)
            picture_Release(pp_pics[j]);
    }
    free(pp_pics);
    return pool;
}

866 867 868 869 870 871 872 873 874
static void SubtitleRegionToBounds(subpicture_t *subpicture,
                                   ARect *p_out_bounds)
{
    if (subpicture) {
        for (subpicture_region_t *r = subpicture->p_region; r != NULL; r = r->p_next) {
            ARect new_bounds;

            new_bounds.left = r->i_x;
            new_bounds.top = r->i_y;
875 876
            new_bounds.right = r->fmt.i_visible_width + r->i_x;
            new_bounds.bottom = r->fmt.i_visible_height + r->i_y;
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
            if (r == &subpicture->p_region[0])
                *p_out_bounds = new_bounds;
            else {
                if (p_out_bounds->left > new_bounds.left)
                    p_out_bounds->left = new_bounds.left;
                if (p_out_bounds->right < new_bounds.right)
                    p_out_bounds->right = new_bounds.right;
                if (p_out_bounds->top > new_bounds.top)
                    p_out_bounds->top = new_bounds.top;
                if (p_out_bounds->bottom < new_bounds.bottom)
                    p_out_bounds->bottom = new_bounds.bottom;
            }
        }
    } else {
        p_out_bounds->left = p_out_bounds->top = 0;
        p_out_bounds->right = p_out_bounds->bottom = 0;
    }
}

static void SubtitleGetDirtyBounds(vout_display_t *vd,
                                   subpicture_t *subpicture,
                                   ARect *p_out_bounds)
899 900
{
    vout_display_sys_t *sys = vd->sys;
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
    int i = 0;
    bool b_found = false;

    /* Try to find last bounds set by current locked buffer.
     * Indeed, even if we can lock only one buffer at a time, differents
     * buffers can be locked. This functions will find the last bounds set by
     * the current buffer. */
    if (sys->p_sub_buffer_bounds) {
        for (; sys->p_sub_buffer_bounds[i].p_pixels != NULL; ++i) {
            buffer_bounds *p_bb = &sys->p_sub_buffer_bounds[i];
            if (p_bb->p_pixels == sys->p_sub_pic->p[0].p_pixels) {
                *p_out_bounds = p_bb->bounds;
                b_found = true;
                break;
            }
        }
    }

919 920 921 922 923 924 925 926 927
    if (!b_found
     || p_out_bounds->left < 0
     || p_out_bounds->right < 0
     || (unsigned int) p_out_bounds->right > sys->p_sub_pic->format.i_width
     || p_out_bounds->bottom < 0
     || p_out_bounds->top < 0
     || (unsigned int) p_out_bounds->top > sys->p_sub_pic->format.i_height)
    {
        /* default is full picture */
928 929 930 931 932 933 934 935 936 937
        p_out_bounds->left = 0;
        p_out_bounds->top = 0;
        p_out_bounds->right = sys->p_sub_pic->format.i_width;
        p_out_bounds->bottom = sys->p_sub_pic->format.i_height;
    }

    /* buffer not found, add it to the array */
    if (!sys->p_sub_buffer_bounds
     || sys->p_sub_buffer_bounds[i].p_pixels == NULL) {
        buffer_bounds *p_bb = realloc(sys->p_sub_buffer_bounds,
938
                                      (i + 2) * sizeof(buffer_bounds));
939 940 941 942 943 944
        if (p_bb) {
            sys->p_sub_buffer_bounds = p_bb;
            sys->p_sub_buffer_bounds[i].p_pixels = sys->p_sub_pic->p[0].p_pixels;
            sys->p_sub_buffer_bounds[i+1].p_pixels = NULL;
        }
    }
945

946 947 948 949 950 951
    /* set buffer bounds */
    if (sys->p_sub_buffer_bounds
     && sys->p_sub_buffer_bounds[i].p_pixels != NULL)
        SubtitleRegionToBounds(subpicture, &sys->p_sub_buffer_bounds[i].bounds);
}

952
static void SubpicturePrepare(vout_display_t *vd, subpicture_t *subpicture)
953 954 955 956
{
    vout_display_sys_t *sys = vd->sys;
    ARect memset_bounds;

957 958
    SubtitleRegionToBounds(subpicture, &memset_bounds);

959 960
    if( subpicture )
    {
961 962
        if( subpicture->i_order == sys->i_sub_last_order
         && memcmp( &memset_bounds, &sys->sub_last_region, sizeof(ARect) ) == 0 )
963
            return;
964

965
        sys->i_sub_last_order = subpicture->i_order;
966
        sys->sub_last_region = memset_bounds;
967 968 969 970 971
    }

    if (AndroidWindow_LockPicture(sys, sys->p_sub_window, sys->p_sub_pic) != 0)
        return;

972
    sys->b_sub_pic_locked = true;
973

974
    /* Clear the subtitles surface. */
975 976 977 978 979 980 981 982
    SubtitleGetDirtyBounds(vd, subpicture, &memset_bounds);
    const int x_pixels_offset = memset_bounds.left
                                * sys->p_sub_pic->p[0].i_pixel_pitch;
    const int i_line_size = (memset_bounds.right - memset_bounds.left)
                            * sys->p_sub_pic->p->i_pixel_pitch;
    for (int y = memset_bounds.top; y < memset_bounds.bottom; y++)
        memset(&sys->p_sub_pic->p[0].p_pixels[y * sys->p_sub_pic->p[0].i_pitch
                                              + x_pixels_offset], 0, i_line_size);
983 984 985 986 987 988 989 990 991 992 993 994 995 996

    if (subpicture)
        picture_BlendSubpicture(sys->p_sub_pic, sys->p_spu_blend, subpicture);
}

static picture_pool_t *Pool(vout_display_t *vd, unsigned requested_count)
{
    vout_display_sys_t *sys = vd->sys;

    if (sys->pool == NULL)
        sys->pool = PoolAlloc(vd, requested_count);
    return sys->pool;
}

997 998
static void Prepare(vout_display_t *vd, picture_t *picture,
                    subpicture_t *subpicture)
999 1000
{
    vout_display_sys_t *sys = vd->sys;
1001
    VLC_UNUSED(picture);
1002

1003
    if (subpicture && sys->p_sub_window) {
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
        if (sys->b_sub_invalid) {
            sys->b_sub_invalid = false;
            if (sys->p_sub_pic) {
                picture_Release(sys->p_sub_pic);
                sys->p_sub_pic = NULL;
            }
            if (sys->p_spu_blend) {
                filter_DeleteBlend(sys->p_spu_blend);
                sys->p_spu_blend = NULL;
            }
1014 1015
            free(sys->p_sub_buffer_bounds);
            sys->p_sub_buffer_bounds = NULL;
1016 1017
        }

1018 1019
        if (!sys->p_sub_pic
         && AndroidWindow_Setup(sys, sys->p_sub_window, 1) == 0)
1020
            sys->p_sub_pic = PictureAlloc(sys, &sys->p_sub_window->fmt, false);
1021
        if (!sys->p_spu_blend && sys->p_sub_pic)
1022 1023
            sys->p_spu_blend = filter_NewBlend(VLC_OBJECT(vd),
                                               &sys->p_sub_pic->format);
1024

1025
        if (sys->p_sub_pic && sys->p_spu_blend)
1026 1027 1028 1029 1030 1031 1032
            sys->b_has_subpictures = true;
    }
    /* As long as no subpicture was received, do not call
       SubpictureDisplay since JNI calls and clearing the subtitles
       surface are expensive operations. */
    if (sys->b_has_subpictures)
    {
1033
        SubpicturePrepare(vd, subpicture);
1034 1035 1036 1037 1038 1039 1040 1041
        if (!subpicture)
        {
            /* The surface has been cleared and there is no new
               subpicture to upload, do not clear again until a new
               subpicture is received. */
            sys->b_has_subpictures = false;
        }
    }
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
}

static void Display(vout_display_t *vd, picture_t *picture,
                    subpicture_t *subpicture)
{
    vout_display_sys_t *sys = vd->sys;

    /* refcount lowers to 0, and pool_cfg.unlock is called */
    UnlockPicture(picture, true);
    picture_Release(picture);
1052

1053 1054 1055 1056
    if (sys->b_sub_pic_locked) {
        sys->b_sub_pic_locked = false;
        AndroidWindow_UnlockPicture(sys, sys->p_sub_window, sys->p_sub_pic, true);
    }
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
    if (subpicture)
        subpicture_Delete(subpicture);
}

static void CopySourceAspect(video_format_t *p_dest,
                             const video_format_t *p_src)
{
    p_dest->i_sar_num = p_src->i_sar_num;
    p_dest->i_sar_den = p_src->i_sar_den;
}

static int Control(vout_display_t *vd, int query, va_list args)
{
    vout_display_sys_t *sys = vd->sys;

    switch (query) {
    case VOUT_DISPLAY_HIDE_MOUSE:
1074
    case VOUT_DISPLAY_CHANGE_FULLSCREEN:
1075 1076 1077
        return VLC_SUCCESS;
    case VOUT_DISPLAY_RESET_PICTURES:
    {
1078
        if (sys->p_window->b_opaque)
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
            return VLC_EGENERIC;

        msg_Dbg(vd, "resetting pictures");

        if (sys->pool != NULL)
        {
            picture_pool_Release(sys->pool);
            sys->pool = NULL;
        }
        return VLC_SUCCESS;
    }
    case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
    case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
    {
Thomas Guillem's avatar
Thomas Guillem committed
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
        msg_Dbg(vd, "change source crop/aspect");
        const video_format_t *source = va_arg(args, const video_format_t *);

        if (query == VOUT_DISPLAY_CHANGE_SOURCE_CROP) {
            video_format_CopyCrop(&sys->p_window->fmt, source);
            AndroidWindow_UpdateCrop(sys, sys->p_window);
        } else
            CopySourceAspect(&sys->p_window->fmt, source);

        UpdateWindowSize(sys, &sys->p_window->fmt,
                         sys->p_window->b_use_priv);
1104
        FixSubtitleFormat(sys);
Thomas Guillem's avatar
Thomas Guillem committed
1105 1106 1107 1108 1109
        return VLC_SUCCESS;
    }
    case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
    {
        const vout_display_cfg_t *cfg = va_arg(args, const vout_display_cfg_t *);
1110

Thomas Guillem's avatar
Thomas Guillem committed
1111 1112 1113 1114 1115
        sys->i_display_width = cfg->display.width;
        sys->i_display_height = cfg->display.height;
        msg_Dbg(vd, "change display size: %dx%d", sys->i_display_width,
                                                  sys->i_display_height);
        FixSubtitleFormat(sys);
1116 1117 1118
        return VLC_SUCCESS;
    }
    default:
1119
        msg_Warn(vd, "Unknown request in android-display: %d", query);
1120 1121 1122 1123 1124
    case VOUT_DISPLAY_CHANGE_ZOOM:
    case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
        return VLC_EGENERIC;
    }
}