display.c 7.54 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 92 93 94 95 96 97 98
{
    vout_display_t *vd = (vout_display_t *)obj;
    vout_display_sys_t *sys = malloc (sizeof (*sys));
    if (unlikely(sys == NULL))
        return VLC_ENOMEM;

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

99
    vout_window_t *surface = vout_display_NewWindow (vd, VOUT_WINDOW_TYPE_INVALID);
100
    if (surface == NULL)
101 102
    {
        msg_Err (vd, "parent window not available");
103
        goto error;
104
    }
105

106
    sys->gl = vlc_gl_Create (surface, API, "$" MODULE_VARNAME);
107 108 109
    if (sys->gl == NULL)
        goto error;

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

112 113 114
    /* Initialize video display */
    const vlc_fourcc_t *spu_chromas;

115 116 117
    if (vlc_gl_MakeCurrent (sys->gl))
        goto error;

118 119
    sys->vgl = vout_display_opengl_New (&vd->fmt, &spu_chromas, sys->gl,
                                        &vd->cfg->viewpoint);
120 121 122
    vlc_gl_ReleaseCurrent (sys->gl);

    if (sys->vgl == NULL)
123 124 125 126
        goto error;

    vd->sys = sys;
    vd->info.has_pictures_invalid = false;
127
    vd->info.subpicture_chromas = spu_chromas;
128 129 130 131 132 133 134 135 136 137
    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);
138 139
    if (surface != NULL)
        vout_display_DeleteWindow (vd, surface);
140 141 142 143 144 145 146 147 148 149 150
    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;
151 152
    vlc_gl_t *gl = sys->gl;
    vout_window_t *surface = gl->surface;
153

154
    vlc_gl_MakeCurrent (gl);
155
    vout_display_opengl_Delete (sys->vgl);
156
    vlc_gl_ReleaseCurrent (gl);
157

158 159
    vlc_gl_Destroy (gl);
    vout_display_DeleteWindow (vd, surface);
160 161 162 163 164 165 166 167 168 169
    free (sys);
}

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

170
    if (!sys->pool && vlc_gl_MakeCurrent (sys->gl) == VLC_SUCCESS)
171
    {
172
        sys->pool = vout_display_opengl_GetPool (sys->vgl, count);
173 174
        vlc_gl_ReleaseCurrent (sys->gl);
    }
175 176 177 178 179 180 181
    return sys->pool;
}

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

182 183
    if (vlc_gl_MakeCurrent (sys->gl) == VLC_SUCCESS)
        vout_display_opengl_Prepare (sys->vgl, pic, subpicture);
184
    vlc_gl_ReleaseCurrent (sys->gl);
185 186 187 188 189 190
}

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

191 192 193 194 195
    if (vlc_gl_MakeCurrent (sys->gl) == VLC_SUCCESS)
    {
        vout_display_opengl_Display (sys->vgl, &vd->source);
        vlc_gl_ReleaseCurrent (sys->gl);
    }
196

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

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
212
        vlc_assert_unreachable();
213 214 215 216 217 218
#endif

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

223
        vout_display_PlacePicture (&place, src, c, false);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
224
        vlc_gl_Resize (sys->gl, place.width, place.height);
225 226
        if (vlc_gl_MakeCurrent (sys->gl) != VLC_SUCCESS)
            return VLC_EGENERIC;
227
        vout_display_opengl_SetWindowAspectRatio(sys->vgl, (float)place.width / place.height);
228
        glViewport (place.x, place.y, place.width, place.height);
229
        vlc_gl_ReleaseCurrent (sys->gl);
230 231 232 233 234 235 236 237 238 239 240
        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);
241 242
        if (vlc_gl_MakeCurrent (sys->gl) != VLC_SUCCESS)
            return VLC_EGENERIC;
243
        vout_display_opengl_SetWindowAspectRatio(sys->vgl, (float)place.width / place.height);
244
        glViewport (place.x, place.y, place.width, place.height);
245
        vlc_gl_ReleaseCurrent (sys->gl);
246 247
        return VLC_SUCCESS;
      }
248 249 250
      case VOUT_DISPLAY_CHANGE_VIEWPOINT:
        return vout_display_opengl_SetViewpoint (sys->vgl,
            &va_arg (ap, const vout_display_cfg_t* )->viewpoint);
251 252 253 254 255
      default:
        msg_Err (vd, "Unknown request %d", query);
    }
    return VLC_EGENERIC;
}