1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2011 RĂ©mi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
28 #include <vlc_common.h>
29 #include <vlc_opengl.h>
31 #include <vlc_modules.h>
35 * Creates an OpenGL context (and its underlying surface).
37 * @note In most cases, you should vlc_gl_MakeCurrent() afterward.
39 * @param wnd window to use as OpenGL surface
40 * @param flags OpenGL context type
41 * @param name module name (or NULL for auto)
42 * @return a new context, or NULL on failure
44 vlc_gl_t *vlc_gl_Create(struct vout_window_t *wnd, unsigned flags,
47 vlc_object_t *parent = (vlc_object_t *)wnd;
51 switch (flags /*& VLC_OPENGL_API_MASK*/)
66 gl = vlc_custom_create(parent, sizeof (*gl), "gl");
67 if (unlikely(gl == NULL))
71 gl->module = module_need(gl, type, name, true);
72 if (gl->module == NULL)
74 vlc_object_release(gl);
81 void vlc_gl_Destroy(vlc_gl_t *gl)
83 module_unneed(gl, gl->module);
84 vlc_object_release(gl);
87 #include <vlc_vout_window.h>
89 typedef struct vlc_gl_surface
96 static void vlc_gl_surface_ResizeNotify(vout_window_t *surface,
97 unsigned width, unsigned height)
99 vlc_gl_surface_t *sys = surface->owner.sys;
101 msg_Dbg(surface, "resized to %ux%u", width, height);
103 vlc_mutex_lock(&sys->lock);
105 sys->height = height;
106 vlc_mutex_unlock(&sys->lock);
109 vlc_gl_t *vlc_gl_surface_Create(vlc_object_t *obj,
110 const vout_window_cfg_t *cfg,
111 struct vout_window_t **restrict wp)
113 vlc_gl_surface_t *sys = malloc(sizeof (*sys));
114 if (unlikely(sys == NULL))
117 sys->width = cfg->width;
118 sys->height = cfg->height;
119 vlc_mutex_init(&sys->lock);
121 vout_window_owner_t owner = {
123 .resized = vlc_gl_surface_ResizeNotify,
126 vout_window_t *surface = vout_window_New(obj, "$window", cfg, &owner);
132 /* TODO: support ES? */
133 vlc_gl_t *gl = vlc_gl_Create(surface, VLC_OPENGL, NULL);
135 vout_window_Delete(surface);
139 vlc_gl_Resize(gl, cfg->width, cfg->height);
143 vlc_mutex_destroy(&sys->lock);
149 * Checks if the dimensions of the surface used by the OpenGL context have
150 * changed (since the previous call), and the OpenGL viewport should be
152 * \return true if at least one dimension has changed, false otherwise
153 * \warning This function is intrinsically race-prone.
154 * The dimensions can change asynchronously.
156 bool vlc_gl_surface_CheckSize(vlc_gl_t *gl, unsigned *restrict width,
157 unsigned *restrict height)
159 vout_window_t *surface = gl->surface;
160 vlc_gl_surface_t *sys = surface->owner.sys;
163 vlc_mutex_lock(&sys->lock);
164 if (sys->width >= 0 && sys->height >= 0)
167 *height = sys->height;
171 vlc_gl_Resize(gl, *width, *height);
174 vlc_mutex_unlock(&sys->lock);
178 void vlc_gl_surface_Destroy(vlc_gl_t *gl)
180 vout_window_t *surface = gl->surface;
181 vlc_gl_surface_t *sys = surface->owner.sys;
184 vout_window_Delete(surface);
185 vlc_mutex_destroy(&sys->lock);