]> git.sesse.net Git - vlc/blob - src/video_output/opengl.c
macosx: fix wrong ref counting in media info
[vlc] / src / video_output / opengl.c
1 /*****************************************************************************
2  * opengl.c: VLC GL API
3  *****************************************************************************
4  * Copyright (C) 2011 RĂ©mi Denis-Courmont
5  *
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.
10  *
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.
15  *
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  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <assert.h>
26 #include <stdlib.h>
27
28 #include <vlc_common.h>
29 #include <vlc_opengl.h>
30 #include "libvlc.h"
31 #include <vlc_modules.h>
32
33 #undef vlc_gl_Create
34 /**
35  * Creates an OpenGL context (and its underlying surface).
36  *
37  * @note In most cases, you should vlc_gl_MakeCurrent() afterward.
38  *
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
43  */
44 vlc_gl_t *vlc_gl_Create(struct vout_window_t *wnd, unsigned flags,
45                         const char *name)
46 {
47     vlc_object_t *parent = (vlc_object_t *)wnd;
48     vlc_gl_t *gl;
49     const char *type;
50
51     switch (flags /*& VLC_OPENGL_API_MASK*/)
52     {
53         case VLC_OPENGL:
54             type = "opengl";
55             break;
56         case VLC_OPENGL_ES:
57             type = "opengl es";
58             break;
59         case VLC_OPENGL_ES2:
60             type = "opengl es2";
61             break;
62         default:
63             return NULL;
64     }
65
66     gl = vlc_custom_create(parent, sizeof (*gl), "gl");
67     if (unlikely(gl == NULL))
68         return NULL;
69
70     gl->surface = wnd;
71     gl->module = module_need(gl, type, name, true);
72     if (gl->module == NULL)
73     {
74         vlc_object_release(gl);
75         return NULL;
76     }
77
78     return gl;
79 }
80
81 void vlc_gl_Destroy(vlc_gl_t *gl)
82 {
83     module_unneed(gl, gl->module);
84     vlc_object_release(gl);
85 }
86
87 #include <vlc_vout_window.h>
88
89 typedef struct vlc_gl_surface
90 {
91     int width;
92     int height;
93     vlc_mutex_t lock;
94 } vlc_gl_surface_t;
95
96 static void vlc_gl_surface_ResizeNotify(vout_window_t *surface,
97                                         unsigned width, unsigned height)
98 {
99     vlc_gl_surface_t *sys = surface->owner.sys;
100
101     msg_Dbg(surface, "resized to %ux%u", width, height);
102
103     vlc_mutex_lock(&sys->lock);
104     sys->width = width;
105     sys->height = height;
106     vlc_mutex_unlock(&sys->lock);
107 }
108
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)
112 {
113     vlc_gl_surface_t *sys = malloc(sizeof (*sys));
114     if (unlikely(sys == NULL))
115         return NULL;
116
117     sys->width = cfg->width;
118     sys->height = cfg->height;
119     vlc_mutex_init(&sys->lock);
120
121     vout_window_owner_t owner = {
122         .sys = sys,
123         .resized = vlc_gl_surface_ResizeNotify,
124     };
125
126     vout_window_t *surface = vout_window_New(obj, "$window", cfg, &owner);
127     if (surface == NULL)
128         goto error;
129     if (wp != NULL)
130         *wp = surface;
131
132     /* TODO: support ES? */
133     vlc_gl_t *gl = vlc_gl_Create(surface, VLC_OPENGL, NULL);
134     if (gl == NULL) {
135         vout_window_Delete(surface);
136         return NULL;
137     }
138
139     vlc_gl_Resize(gl, cfg->width, cfg->height);
140     return gl;
141
142 error:
143     vlc_mutex_destroy(&sys->lock);
144     free(sys);
145     return NULL;
146 }
147
148 /**
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
151  * updated.
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.
155  */
156 bool vlc_gl_surface_CheckSize(vlc_gl_t *gl, unsigned *restrict width,
157                               unsigned *restrict height)
158 {
159     vout_window_t *surface = gl->surface;
160     vlc_gl_surface_t *sys = surface->owner.sys;
161     bool ret = false;
162
163     vlc_mutex_lock(&sys->lock);
164     if (sys->width >= 0 && sys->height >= 0)
165     {
166         *width = sys->width;
167         *height = sys->height;
168         sys->width = -1;
169         sys->height = -1;
170
171         vlc_gl_Resize(gl, *width, *height);
172         ret = true;
173     }
174     vlc_mutex_unlock(&sys->lock);
175     return ret;
176 }
177
178 void vlc_gl_surface_Destroy(vlc_gl_t *gl)
179 {
180     vout_window_t *surface = gl->surface;
181     vlc_gl_surface_t *sys = surface->owner.sys;
182
183     vlc_gl_Destroy(gl);
184     vout_window_Delete(surface);
185     vlc_mutex_destroy(&sys->lock);
186     free(sys);
187 }