codec: avcodec: remove leftoff cc code
[vlc.git] / src / video_output / opengl.c
blobcdb02095d4c89287850986804789cc33e098117a
1 /*****************************************************************************
2 * opengl.c: VLC GL API
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 *****************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
25 #include <assert.h>
26 #include <stdlib.h>
28 #include <vlc_common.h>
29 #include <vlc_opengl.h>
30 #include "libvlc.h"
31 #include <vlc_modules.h>
33 #undef vlc_gl_Create
34 /**
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,
45 const char *name)
47 vlc_object_t *parent = (vlc_object_t *)wnd;
48 vlc_gl_t *gl;
49 const char *type;
51 switch (flags /*& VLC_OPENGL_API_MASK*/)
53 case VLC_OPENGL:
54 type = "opengl";
55 break;
56 case VLC_OPENGL_ES2:
57 type = "opengl es2";
58 break;
59 default:
60 return NULL;
63 gl = vlc_custom_create(parent, sizeof (*gl), "gl");
64 if (unlikely(gl == NULL))
65 return NULL;
67 gl->surface = wnd;
68 gl->module = module_need(gl, type, name, true);
69 if (gl->module == NULL)
71 vlc_object_release(gl);
72 return NULL;
75 return 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
88 int width;
89 int height;
90 vlc_mutex_t lock;
91 } vlc_gl_surface_t;
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);
101 sys->width = width;
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))
112 return NULL;
114 sys->width = cfg->width;
115 sys->height = cfg->height;
116 vlc_mutex_init(&sys->lock);
118 vout_window_owner_t owner = {
119 .sys = sys,
120 .resized = vlc_gl_surface_ResizeNotify,
123 vout_window_t *surface = vout_window_New(obj, "$window", cfg, &owner);
124 if (surface == NULL)
125 goto error;
126 if (wp != NULL)
127 *wp = surface;
129 /* TODO: support ES? */
130 vlc_gl_t *gl = vlc_gl_Create(surface, VLC_OPENGL, NULL);
131 if (gl == NULL) {
132 vout_window_Delete(surface);
133 return NULL;
136 vlc_gl_Resize(gl, cfg->width, cfg->height);
137 return gl;
139 error:
140 vlc_mutex_destroy(&sys->lock);
141 free(sys);
142 return NULL;
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
148 * updated.
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;
158 bool ret = false;
160 vlc_mutex_lock(&sys->lock);
161 if (sys->width >= 0 && sys->height >= 0)
163 *width = sys->width;
164 *height = sys->height;
165 sys->width = -1;
166 sys->height = -1;
168 vlc_gl_Resize(gl, *width, *height);
169 ret = true;
171 vlc_mutex_unlock(&sys->lock);
172 return ret;
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;
180 vlc_gl_Destroy(gl);
181 vout_window_Delete(surface);
182 vlc_mutex_destroy(&sys->lock);
183 free(sys);