display.c 7.45 KB
Newer Older
1
/**
2
 * @file display.c
3 4 5 6 7
 * @brief OpenGL video output module
 */
/*****************************************************************************
 * Copyright © 2010-2011 Rémi Denis-Courmont
 *
8 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
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Lesser General Public License for more details.
17
 *
18 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

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdlib.h>
#include <assert.h>

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_vout_display.h>
#include <vlc_opengl.h>
34
#include "vout_helper.h"
35 36

/* Plugin callbacks */
37
static int Open (vlc_object_t *);
38 39 40 41 42 43 44 45
static void Close (vlc_object_t *);

#define GL_TEXT N_("OpenGL extension")
#define GLES2_TEXT N_("OpenGL ES 2 extension")
#define PROVIDER_LONGTEXT N_( \
    "Extension through which to use the Open Graphics Library (OpenGL).")

vlc_module_begin ()
46
#if defined (USE_OPENGL_ES2)
47 48
# define API VLC_OPENGL_ES2
# define MODULE_VARNAME "gles2"
49 50
    set_shortname (N_("OpenGL ES2"))
    set_description (N_("OpenGL for Embedded Systems 2 video output"))
Thomas Guillem's avatar
Thomas Guillem committed
51
    set_capability ("vout display", 265)
52
    set_callbacks (Open, Close)
53
    add_shortcut ("opengles2", "gles2")
54
    add_module ("gles2", "opengl es2", NULL,
55 56
                GLES2_TEXT, PROVIDER_LONGTEXT, true)

57
#else
58

59 60 61
# define API VLC_OPENGL
# define MODULE_VARNAME "gl"
    set_shortname (N_("OpenGL"))
62
    set_description (N_("OpenGL video output"))
63 64
    set_category (CAT_VIDEO)
    set_subcategory (SUBCAT_VIDEO_VOUT)
65
    set_capability ("vout display", 270)
66 67
    set_callbacks (Open, Close)
    add_shortcut ("opengl", "gl")
68
    add_module ("gl", "opengl", NULL,
69 70
                GL_TEXT, PROVIDER_LONGTEXT, true)
#endif
71 72 73 74
vlc_module_end ()

struct vout_display_sys_t
{
75
    vout_display_opengl_t *vgl;
76 77 78 79 80 81 82 83 84 85 86 87 88
    vlc_gl_t *gl;
    picture_pool_t *pool;
};

/* Display callbacks */
static picture_pool_t *Pool (vout_display_t *, unsigned);
static void PictureRender (vout_display_t *, picture_t *, subpicture_t *);
static void PictureDisplay (vout_display_t *, picture_t *, subpicture_t *);
static int Control (vout_display_t *, int, va_list);

/**
 * Allocates a surface and an OpenGL context for video output.
 */
89
static int Open (vlc_object_t *obj)
90 91
{
    vout_display_t *vd = (vout_display_t *)obj;
92 93 94 95

    if (vd->fmt.i_chroma == VLC_CODEC_ANDROID_OPAQUE)
        return VLC_EGENERIC;

96 97 98 99 100 101 102
    vout_display_sys_t *sys = malloc (sizeof (*sys));
    if (unlikely(sys == NULL))
        return VLC_ENOMEM;

    sys->gl = NULL;
    sys->pool = NULL;

103
    vout_window_t *surface = vout_display_NewWindow (vd, VOUT_WINDOW_TYPE_INVALID);
104
    if (surface == NULL)
105 106
    {
        msg_Err (vd, "parent window not available");
107
        goto error;
108
    }
109

110
    sys->gl = vlc_gl_Create (surface, API, "$" MODULE_VARNAME);
111 112 113
    if (sys->gl == NULL)
        goto error;

114
    vlc_gl_Resize (sys->gl, vd->cfg->display.width, vd->cfg->display.height);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
115

116 117 118
    /* Initialize video display */
    const vlc_fourcc_t *spu_chromas;

119 120 121
    if (vlc_gl_MakeCurrent (sys->gl))
        goto error;

122 123
    sys->vgl = vout_display_opengl_New (&vd->fmt, &spu_chromas, sys->gl,
                                        &vd->cfg->viewpoint);
124 125 126
    vlc_gl_ReleaseCurrent (sys->gl);

    if (sys->vgl == NULL)
127 128 129 130
        goto error;

    vd->sys = sys;
    vd->info.has_pictures_invalid = false;
131
    vd->info.subpicture_chromas = spu_chromas;
132 133 134 135 136 137 138 139 140 141
    vd->pool = Pool;
    vd->prepare = PictureRender;
    vd->display = PictureDisplay;
    vd->control = Control;
    vd->manage = NULL;
    return VLC_SUCCESS;

error:
    if (sys->gl != NULL)
        vlc_gl_Destroy (sys->gl);
142 143
    if (surface != NULL)
        vout_display_DeleteWindow (vd, surface);
144 145 146 147 148 149 150 151 152 153 154
    free (sys);
    return VLC_EGENERIC;
}

/**
 * Destroys the OpenGL context.
 */
static void Close (vlc_object_t *obj)
{
    vout_display_t *vd = (vout_display_t *)obj;
    vout_display_sys_t *sys = vd->sys;
155 156
    vlc_gl_t *gl = sys->gl;
    vout_window_t *surface = gl->surface;
157

158
    vlc_gl_MakeCurrent (gl);
159
    vout_display_opengl_Delete (sys->vgl);
160
    vlc_gl_ReleaseCurrent (gl);
161

162 163
    vlc_gl_Destroy (gl);
    vout_display_DeleteWindow (vd, surface);
164 165 166 167 168 169 170 171 172 173 174
    free (sys);
}

/**
 * Returns picture buffers
 */
static picture_pool_t *Pool (vout_display_t *vd, unsigned count)
{
    vout_display_sys_t *sys = vd->sys;

    if (!sys->pool)
175 176
    {
        vlc_gl_MakeCurrent (sys->gl);
177
        sys->pool = vout_display_opengl_GetPool (sys->vgl, count);
178 179
        vlc_gl_ReleaseCurrent (sys->gl);
    }
180 181 182 183 184 185 186
    return sys->pool;
}

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

187
    vlc_gl_MakeCurrent (sys->gl);
188
    vout_display_opengl_Prepare (sys->vgl, pic, subpicture);
189
    vlc_gl_ReleaseCurrent (sys->gl);
190 191 192 193 194 195
}

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

196
    vlc_gl_MakeCurrent (sys->gl);
197
    vout_display_opengl_Display (sys->vgl, &vd->source);
198 199
    vlc_gl_ReleaseCurrent (sys->gl);

200
    picture_Release (pic);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
201 202
    if (subpicture != NULL)
        subpicture_Delete(subpicture);
203 204 205 206 207 208 209 210 211 212 213 214
}

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

    switch (query)
    {
      case VOUT_DISPLAY_HIDE_MOUSE: /* FIXME TODO */
        break;
#ifndef NDEBUG
      case VOUT_DISPLAY_RESET_PICTURES: // not needed
215
        vlc_assert_unreachable();
216 217 218 219 220 221
#endif

      case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
      case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
      case VOUT_DISPLAY_CHANGE_ZOOM:
      {
222
        const vout_display_cfg_t *c = va_arg (ap, const vout_display_cfg_t *);
223 224 225
        const video_format_t *src = &vd->source;
        vout_display_place_t place;

226
        vout_display_PlacePicture (&place, src, c, false);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
227
        vlc_gl_Resize (sys->gl, place.width, place.height);
228
        vlc_gl_MakeCurrent (sys->gl);
229
        vout_display_opengl_SetWindowAspectRatio(sys->vgl, (float)place.width / place.height);
230
        glViewport (place.x, place.y, place.width, place.height);
231
        vlc_gl_ReleaseCurrent (sys->gl);
232 233 234 235 236 237 238 239 240 241 242
        return VLC_SUCCESS;
      }

      case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
      case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
      {
        const vout_display_cfg_t *cfg = vd->cfg;
        const video_format_t *src = va_arg (ap, const video_format_t *);
        vout_display_place_t place;

        vout_display_PlacePicture (&place, src, cfg, false);
243
        vlc_gl_MakeCurrent (sys->gl);
244
        vout_display_opengl_SetWindowAspectRatio(sys->vgl, (float)place.width / place.height);
245
        glViewport (place.x, place.y, place.width, place.height);
246
        vlc_gl_ReleaseCurrent (sys->gl);
247 248
        return VLC_SUCCESS;
      }
249 250 251
      case VOUT_DISPLAY_CHANGE_VIEWPOINT:
        return vout_display_opengl_SetViewpoint (sys->vgl,
            &va_arg (ap, const vout_display_cfg_t* )->viewpoint);
252 253 254 255 256
      default:
        msg_Err (vd, "Unknown request %d", query);
    }
    return VLC_EGENERIC;
}