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*/)
63 gl
= vlc_custom_create(parent
, sizeof (*gl
), "gl");
64 if (unlikely(gl
== NULL
))
68 gl
->module
= module_need(gl
, type
, name
, true);
69 if (gl
->module
== NULL
)
71 vlc_object_release(gl
);
78 void vlc_gl_Destroy(vlc_gl_t
*gl
)
80 module_unneed(gl
, gl
->module
);
81 vlc_object_release(gl
);
84 #include <vlc_vout_window.h>
86 typedef struct vlc_gl_surface
93 static void vlc_gl_surface_ResizeNotify(vout_window_t
*surface
,
94 unsigned width
, unsigned height
)
96 vlc_gl_surface_t
*sys
= surface
->owner
.sys
;
98 msg_Dbg(surface
, "resized to %ux%u", width
, height
);
100 vlc_mutex_lock(&sys
->lock
);
102 sys
->height
= height
;
103 vlc_mutex_unlock(&sys
->lock
);
106 vlc_gl_t
*vlc_gl_surface_Create(vlc_object_t
*obj
,
107 const vout_window_cfg_t
*cfg
,
108 struct vout_window_t
**restrict wp
)
110 vlc_gl_surface_t
*sys
= malloc(sizeof (*sys
));
111 if (unlikely(sys
== NULL
))
114 sys
->width
= cfg
->width
;
115 sys
->height
= cfg
->height
;
116 vlc_mutex_init(&sys
->lock
);
118 vout_window_owner_t owner
= {
120 .resized
= vlc_gl_surface_ResizeNotify
,
123 vout_window_t
*surface
= vout_window_New(obj
, "$window", cfg
, &owner
);
129 /* TODO: support ES? */
130 vlc_gl_t
*gl
= vlc_gl_Create(surface
, VLC_OPENGL
, NULL
);
132 vout_window_Delete(surface
);
136 vlc_gl_Resize(gl
, cfg
->width
, cfg
->height
);
140 vlc_mutex_destroy(&sys
->lock
);
146 * Checks if the dimensions of the surface used by the OpenGL context have
147 * changed (since the previous call), and the OpenGL viewport should be
149 * \return true if at least one dimension has changed, false otherwise
150 * \warning This function is intrinsically race-prone.
151 * The dimensions can change asynchronously.
153 bool vlc_gl_surface_CheckSize(vlc_gl_t
*gl
, unsigned *restrict width
,
154 unsigned *restrict height
)
156 vout_window_t
*surface
= gl
->surface
;
157 vlc_gl_surface_t
*sys
= surface
->owner
.sys
;
160 vlc_mutex_lock(&sys
->lock
);
161 if (sys
->width
>= 0 && sys
->height
>= 0)
164 *height
= sys
->height
;
168 vlc_gl_Resize(gl
, *width
, *height
);
171 vlc_mutex_unlock(&sys
->lock
);
175 void vlc_gl_surface_Destroy(vlc_gl_t
*gl
)
177 vout_window_t
*surface
= gl
->surface
;
178 vlc_gl_surface_t
*sys
= surface
->owner
.sys
;
181 vout_window_Delete(surface
);
182 vlc_mutex_destroy(&sys
->lock
);