opengl: Avoid warnings about an unused variable
[videolan:vlc.git] / modules / video_output / opengl.c
1 /*****************************************************************************
2  * opengl.c: OpenGL and OpenGL ES output common code
3  *****************************************************************************
4  * Copyright (C) 2004-2013 VLC authors and VideoLAN
5  * Copyright (C) 2009, 2011 Laurent Aimar
6  *
7  * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
8  *          Ilkka Ollakka <ileoo@videolan.org>
9  *          Rémi Denis-Courmont
10  *          Adrien Maglo <magsoft at videolan dot org>
11  *          Felix Paul Kühne <fkuehne at videolan dot org>
12  *          Pierre d'Herbemont <pdherbemont at videolan dot org>
13  *
14  * This program is free software; you can redistribute it and/or modify it
15  * under the terms of the GNU Lesser General Public License as published by
16  * the Free Software Foundation; either version 2.1 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * along with this program; if not, write to the Free Software Foundation,
26  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_picture_pool.h>
34 #include <vlc_subpicture.h>
35 #include <vlc_opengl.h>
36
37 #include "opengl.h"
38
39 #ifndef GL_CLAMP_TO_EDGE
40 # define GL_CLAMP_TO_EDGE 0x812F
41 #endif
42
43 #if USE_OPENGL_ES == 2 || defined(__APPLE__)
44 #   define PFNGLGETPROGRAMIVPROC             typeof(glGetProgramiv)*
45 #   define PFNGLGETPROGRAMINFOLOGPROC        typeof(glGetProgramInfoLog)*
46 #   define PFNGLGETSHADERIVPROC              typeof(glGetShaderiv)*
47 #   define PFNGLGETSHADERINFOLOGPROC         typeof(glGetShaderInfoLog)*
48 #   define PFNGLGETUNIFORMLOCATIONPROC       typeof(glGetUniformLocation)*
49 #   define PFNGLGETATTRIBLOCATIONPROC        typeof(glGetAttribLocation)*
50 #   define PFNGLVERTEXATTRIBPOINTERPROC      typeof(glVertexAttribPointer)*
51 #   define PFNGLENABLEVERTEXATTRIBARRAYPROC  typeof(glEnableVertexAttribArray)*
52 #   define PFNGLUNIFORM4FVPROC               typeof(glUniform4fv)*
53 #   define PFNGLUNIFORM4FPROC                typeof(glUniform4f)*
54 #   define PFNGLUNIFORM1IPROC                typeof(glUniform1i)*
55 #   define PFNGLCREATESHADERPROC             typeof(glCreateShader)*
56 #   define PFNGLSHADERSOURCEPROC             typeof(glShaderSource)*
57 #   define PFNGLCOMPILESHADERPROC            typeof(glCompileShader)*
58 #   define PFNGLDELETESHADERPROC             typeof(glDeleteShader)*
59 #   define PFNGLCREATEPROGRAMPROC            typeof(glCreateProgram)*
60 #   define PFNGLLINKPROGRAMPROC              typeof(glLinkProgram)*
61 #   define PFNGLUSEPROGRAMPROC               typeof(glUseProgram)*
62 #   define PFNGLDELETEPROGRAMPROC            typeof(glDeleteProgram)*
63 #   define PFNGLATTACHSHADERPROC             typeof(glAttachShader)*
64 #if defined(__APPLE__) && USE_OPENGL_ES
65 #   import <CoreFoundation/CoreFoundation.h>
66 #endif
67 #endif
68
69 #if USE_OPENGL_ES
70 #   define GLSL_VERSION "100"
71 #   define VLCGL_TEXTURE_COUNT 1
72 #   define VLCGL_PICTURE_MAX 1
73 #   define PRECISION "precision highp float;"
74 #if USE_OPENGL_ES == 2
75 #   define SUPPORTS_SHADERS
76 #   define glClientActiveTexture(x)
77 #else
78 #   define SUPPORTS_FIXED_PIPELINE
79 #   define GL_MAX_TEXTURE_IMAGE_UNITS GL_MAX_TEXTURE_UNITS
80 #endif
81 #else
82 #   define GLSL_VERSION "120"
83 #   define VLCGL_TEXTURE_COUNT 1
84 #   define VLCGL_PICTURE_MAX 128
85 #   define PRECISION ""
86 #   define SUPPORTS_SHADERS
87 #   define SUPPORTS_FIXED_PIPELINE
88 #endif
89
90 static const vlc_fourcc_t gl_subpicture_chromas[] = {
91     VLC_CODEC_RGBA,
92     0
93 };
94
95 typedef struct {
96     GLuint   texture;
97     unsigned format;
98     unsigned type;
99     unsigned width;
100     unsigned height;
101
102     float    alpha;
103
104     float    top;
105     float    left;
106     float    bottom;
107     float    right;
108
109     float    tex_width;
110     float    tex_height;
111 } gl_region_t;
112
113 struct vout_display_opengl_t {
114
115     vlc_gl_t   *gl;
116
117     video_format_t fmt;
118     const vlc_chroma_description_t *chroma;
119
120     int        tex_target;
121     int        tex_format;
122     int        tex_internal;
123     int        tex_type;
124
125     int        tex_width[PICTURE_PLANE_MAX];
126     int        tex_height[PICTURE_PLANE_MAX];
127
128     GLuint     texture[VLCGL_TEXTURE_COUNT][PICTURE_PLANE_MAX];
129
130     int         region_count;
131     gl_region_t *region;
132
133
134     picture_pool_t *pool;
135
136     /* index 0 for normal and 1 for subtitle overlay */
137     GLuint     program[2];
138     GLint      shader[3]; //3. is for the common vertex shader
139     int        local_count;
140     GLfloat    local_value[16];
141
142     /* Shader variables commands*/
143 #ifdef SUPPORTS_SHADERS
144     PFNGLGETUNIFORMLOCATIONPROC      GetUniformLocation;
145     PFNGLGETATTRIBLOCATIONPROC       GetAttribLocation;
146     PFNGLVERTEXATTRIBPOINTERPROC     VertexAttribPointer;
147     PFNGLENABLEVERTEXATTRIBARRAYPROC EnableVertexAttribArray;
148
149     PFNGLUNIFORM4FVPROC   Uniform4fv;
150     PFNGLUNIFORM4FPROC    Uniform4f;
151     PFNGLUNIFORM1IPROC    Uniform1i;
152
153     /* Shader command */
154     PFNGLCREATESHADERPROC CreateShader;
155     PFNGLSHADERSOURCEPROC ShaderSource;
156     PFNGLCOMPILESHADERPROC CompileShader;
157     PFNGLDELETESHADERPROC   DeleteShader;
158
159     PFNGLCREATEPROGRAMPROC CreateProgram;
160     PFNGLLINKPROGRAMPROC   LinkProgram;
161     PFNGLUSEPROGRAMPROC    UseProgram;
162     PFNGLDELETEPROGRAMPROC DeleteProgram;
163
164     PFNGLATTACHSHADERPROC  AttachShader;
165
166     /* Shader log commands */
167     PFNGLGETPROGRAMIVPROC  GetProgramiv;
168     PFNGLGETPROGRAMINFOLOGPROC GetProgramInfoLog;
169     PFNGLGETSHADERIVPROC   GetShaderiv;
170     PFNGLGETSHADERINFOLOGPROC GetShaderInfoLog;
171 #endif
172
173
174     /* multitexture */
175     bool use_multitexture;
176
177     /* Non-power-of-2 texture size support */
178     bool supports_npot;
179
180     uint8_t *texture_temp_buf;
181     int      texture_temp_buf_size;
182 };
183
184 static inline int GetAlignedSize(unsigned size)
185 {
186     /* Return the smallest larger or equal power of 2 */
187     unsigned align = 1 << (8 * sizeof (unsigned) - clz(size));
188     return ((align >> 1) == size) ? size : align;
189 }
190
191 #if !USE_OPENGL_ES
192 static bool IsLuminance16Supported(int target)
193 {
194     GLuint texture;
195
196     glGenTextures(1, &texture);
197     glBindTexture(target, texture);
198     glTexImage2D(target, 0, GL_LUMINANCE16,
199                  64, 64, 0, GL_LUMINANCE, GL_UNSIGNED_SHORT, NULL);
200     GLint size = 0;
201     glGetTexLevelParameteriv(target, 0, GL_TEXTURE_LUMINANCE_SIZE, &size);
202
203     glDeleteTextures(1, &texture);
204
205     return size == 16;
206 }
207 #endif
208
209 #ifdef SUPPORTS_SHADERS
210 static void BuildVertexShader(vout_display_opengl_t *vgl,
211                               GLint *shader)
212 {
213     /* Basic vertex shader */
214     const char *vertexShader =
215         "#version " GLSL_VERSION "\n"
216         PRECISION
217         "varying vec4 TexCoord0,TexCoord1, TexCoord2;"
218         "attribute vec4 MultiTexCoord0,MultiTexCoord1,MultiTexCoord2;"
219         "attribute vec4 VertexPosition;"
220         "void main() {"
221         " TexCoord0 = MultiTexCoord0;"
222         " TexCoord1 = MultiTexCoord1;"
223         " TexCoord2 = MultiTexCoord2;"
224         " gl_Position = VertexPosition;"
225         "}";
226
227     *shader = vgl->CreateShader(GL_VERTEX_SHADER);
228     vgl->ShaderSource(*shader, 1, &vertexShader, NULL);
229     vgl->CompileShader(*shader);
230 }
231
232 static void BuildYUVFragmentShader(vout_display_opengl_t *vgl,
233                                    GLint *shader,
234                                    int *local_count,
235                                    GLfloat *local_value,
236                                    const video_format_t *fmt,
237                                    float yuv_range_correction)
238
239 {
240     /* [R/G/B][Y U V O] from TV range to full range
241      * XXX we could also do hue/brightness/constrast/gamma
242      * by simply changing the coefficients
243      */
244     const float matrix_bt601_tv2full[12] = {
245         1.164383561643836,  0.0000,             1.596026785714286, -0.874202217873451 ,
246         1.164383561643836, -0.391762290094914, -0.812967647237771,  0.531667823499146 ,
247         1.164383561643836,  2.017232142857142,  0.0000,            -1.085630789302022 ,
248     };
249     const float matrix_bt709_tv2full[12] = {
250         1.164383561643836,  0.0000,             1.792741071428571, -0.972945075016308 ,
251         1.164383561643836, -0.21324861427373,  -0.532909328559444,  0.301482665475862 ,
252         1.164383561643836,  2.112401785714286,  0.0000,            -1.133402217873451 ,
253     };
254     const float (*matrix) = fmt->i_height > 576 ? matrix_bt709_tv2full
255                                                 : matrix_bt601_tv2full;
256
257     /* Basic linear YUV -> RGB conversion using bilinear interpolation */
258     const char *template_glsl_yuv =
259         "#version " GLSL_VERSION "\n"
260         PRECISION
261         "uniform sampler2D Texture0;"
262         "uniform sampler2D Texture1;"
263         "uniform sampler2D Texture2;"
264         "uniform vec4      Coefficient[4];"
265         "varying vec4      TexCoord0,TexCoord1,TexCoord2;"
266
267         "void main(void) {"
268         " vec4 x,y,z,result;"
269         " x  = texture2D(Texture0, TexCoord0.st);"
270         " %c = texture2D(Texture1, TexCoord1.st);"
271         " %c = texture2D(Texture2, TexCoord2.st);"
272
273         " result = x * Coefficient[0] + Coefficient[3];"
274         " result = (y * Coefficient[1]) + result;"
275         " result = (z * Coefficient[2]) + result;"
276         " gl_FragColor = result;"
277         "}";
278     bool swap_uv = fmt->i_chroma == VLC_CODEC_YV12 ||
279                    fmt->i_chroma == VLC_CODEC_YV9;
280
281     char *code;
282     if (asprintf(&code, template_glsl_yuv,
283                  swap_uv ? 'z' : 'y',
284                  swap_uv ? 'y' : 'z') < 0)
285         code = NULL;
286
287     for (int i = 0; i < 4; i++) {
288         float correction = i < 3 ? yuv_range_correction : 1.0;
289         /* We place coefficient values for coefficient[4] in one array from matrix values.
290            Notice that we fill values from top down instead of left to right.*/
291         for (int j = 0; j < 4; j++)
292             local_value[*local_count + i*4+j] = j < 3 ? correction * matrix[j*4+i]
293                                                       : 0.0 ;
294     }
295     (*local_count) += 4;
296
297
298     *shader = vgl->CreateShader(GL_FRAGMENT_SHADER);
299     vgl->ShaderSource(*shader, 1, (const char **)&code, NULL);
300     vgl->CompileShader(*shader);
301
302     free(code);
303 }
304
305 #if 0
306 static void BuildRGBFragmentShader(vout_display_opengl_t *vgl,
307                                    GLint *shader)
308 {
309     // Simple shader for RGB
310     const char *code =
311         "#version " GLSL_VERSION "\n"
312         PRECISION
313         "uniform sampler2D Texture[3];"
314         "varying vec4 TexCoord0,TexCoord1,TexCoord2;"
315         "void main()"
316         "{ "
317         "  gl_FragColor = texture2D(Texture[0], TexCoord0.st);"
318         "}";
319     *shader = vgl->CreateShader(GL_FRAGMENT_SHADER);
320     vgl->ShaderSource(*shader, 1, &code, NULL);
321     vgl->CompileShader(*shader);
322 }
323 #endif
324
325 static void BuildRGBAFragmentShader(vout_display_opengl_t *vgl,
326                                    GLint *shader)
327 {
328     // Simple shader for RGBA
329     const char *code =
330         "#version " GLSL_VERSION "\n"
331         PRECISION
332         "uniform sampler2D Texture;"
333         "uniform vec4 FillColor;"
334         "varying vec4 TexCoord0;"
335         "void main()"
336         "{ "
337         "  gl_FragColor = texture2D(Texture, TexCoord0.st) * FillColor;"
338         "}";
339     *shader = vgl->CreateShader(GL_FRAGMENT_SHADER);
340     vgl->ShaderSource(*shader, 1, &code, NULL);
341     vgl->CompileShader(*shader);
342 }
343 #endif
344
345 vout_display_opengl_t *vout_display_opengl_New(video_format_t *fmt,
346                                                const vlc_fourcc_t **subpicture_chromas,
347                                                vlc_gl_t *gl)
348 {
349     vout_display_opengl_t *vgl = calloc(1, sizeof(*vgl));
350     if (!vgl)
351         return NULL;
352
353     vgl->gl = gl;
354     if (vlc_gl_Lock(vgl->gl)) {
355         free(vgl);
356         return NULL;
357     }
358
359     if (vgl->gl->getProcAddress == NULL) {
360         fprintf(stderr, "getProcAddress not implemented, bailing out\n");
361         free(vgl);
362         return NULL;
363     }
364
365     const char *extensions = (const char *)glGetString(GL_EXTENSIONS);
366 #if !USE_OPENGL_ES
367     const unsigned char *ogl_version = glGetString(GL_VERSION);
368     bool supports_shaders = strverscmp((const char *)ogl_version, "2.0") >= 0;
369 #else
370     bool supports_shaders = false;
371 #endif
372
373 #if USE_OPENGL_ES == 2
374     vgl->CreateShader  = glCreateShader;
375     vgl->ShaderSource  = glShaderSource;
376     vgl->CompileShader = glCompileShader;
377     vgl->AttachShader  = glAttachShader;
378
379     vgl->GetProgramiv  = glGetProgramiv;
380     vgl->GetShaderiv   = glGetShaderiv;
381     vgl->GetProgramInfoLog  = glGetProgramInfoLog;
382     vgl->GetShaderInfoLog   = glGetShaderInfoLog;
383
384     vgl->DeleteShader  = glDeleteShader;
385
386     vgl->GetUniformLocation = glGetUniformLocation;
387     vgl->GetAttribLocation  = glGetAttribLocation;
388     vgl->VertexAttribPointer= glVertexAttribPointer;
389     vgl->EnableVertexAttribArray = glEnableVertexAttribArray;
390     vgl->Uniform4fv    = glUniform4fv;
391     vgl->Uniform4f     = glUniform4f;
392     vgl->Uniform1i     = glUniform1i;
393
394     vgl->CreateProgram = glCreateProgram;
395     vgl->LinkProgram   = glLinkProgram;
396     vgl->UseProgram    = glUseProgram;
397     vgl->DeleteProgram = glDeleteProgram;
398     supports_shaders = true;
399 #elif defined(SUPPORTS_SHADERS)
400     vgl->CreateShader  = (PFNGLCREATESHADERPROC)vlc_gl_GetProcAddress(vgl->gl, "glCreateShader");
401     vgl->ShaderSource  = (PFNGLSHADERSOURCEPROC)vlc_gl_GetProcAddress(vgl->gl, "glShaderSource");
402     vgl->CompileShader = (PFNGLCOMPILESHADERPROC)vlc_gl_GetProcAddress(vgl->gl, "glCompileShader");
403     vgl->AttachShader  = (PFNGLATTACHSHADERPROC)vlc_gl_GetProcAddress(vgl->gl, "glAttachShader");
404
405     vgl->GetProgramiv  = (PFNGLGETPROGRAMIVPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetProgramiv");
406     vgl->GetShaderiv   = (PFNGLGETSHADERIVPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetShaderiv");
407     vgl->GetProgramInfoLog  = (PFNGLGETPROGRAMINFOLOGPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetProgramInfoLog");
408     vgl->GetShaderInfoLog   = (PFNGLGETSHADERINFOLOGPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetShaderInfoLog");
409
410     vgl->DeleteShader  = (PFNGLDELETESHADERPROC)vlc_gl_GetProcAddress(vgl->gl, "glDeleteShader");
411
412     vgl->GetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetUniformLocation");
413     vgl->GetAttribLocation  = (PFNGLGETATTRIBLOCATIONPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetAttribLocation");
414     vgl->VertexAttribPointer= (PFNGLVERTEXATTRIBPOINTERPROC)vlc_gl_GetProcAddress(vgl->gl, "glVertexAttribPointer");
415     vgl->EnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYPROC)vlc_gl_GetProcAddress(vgl->gl, "glEnableVertexAttribArray");
416     vgl->Uniform4fv    = (PFNGLUNIFORM4FVPROC)vlc_gl_GetProcAddress(vgl->gl,"glUniform4fv");
417     vgl->Uniform4f     = (PFNGLUNIFORM4FPROC)vlc_gl_GetProcAddress(vgl->gl,"glUniform4f");
418     vgl->Uniform1i     = (PFNGLUNIFORM1IPROC)vlc_gl_GetProcAddress(vgl->gl,"glUniform1i");
419
420     vgl->CreateProgram = (PFNGLCREATEPROGRAMPROC)vlc_gl_GetProcAddress(vgl->gl, "glCreateProgram");
421     vgl->LinkProgram   = (PFNGLLINKPROGRAMPROC)vlc_gl_GetProcAddress(vgl->gl, "glLinkProgram");
422     vgl->UseProgram    = (PFNGLUSEPROGRAMPROC)vlc_gl_GetProcAddress(vgl->gl, "glUseProgram");
423     vgl->DeleteProgram = (PFNGLDELETEPROGRAMPROC)vlc_gl_GetProcAddress(vgl->gl, "glDeleteProgram");
424
425     if (!vgl->CreateShader || !vgl->ShaderSource || !vgl->CreateProgram)
426         supports_shaders = false;
427 #endif
428
429     vgl->supports_npot = HasExtension(extensions, "GL_ARB_texture_non_power_of_two") ||
430                          HasExtension(extensions, "GL_APPLE_texture_2D_limited_npot");
431
432 #if USE_OPENGL_ES == 2
433     /* OpenGL ES 2 includes support for non-power of 2 textures by specification
434      * so checks for extensions are bound to fail. Check for OpenGL ES version instead. */
435     vgl->supports_npot = true;
436 #endif
437
438     GLint max_texture_units = 0;
439     glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &max_texture_units);
440
441 #ifdef __APPLE__
442 #if USE_OPENGL_ES
443     /* work-around an iOS 6 bug */
444     if (kCFCoreFoundationVersionNumber >= 786.)
445         max_texture_units = 8;
446     supports_shaders = true;
447 #endif
448 #endif
449
450     /* Initialize with default chroma */
451     vgl->fmt = *fmt;
452     vgl->fmt.i_chroma = VLC_CODEC_RGB32;
453 #   if defined(WORDS_BIGENDIAN)
454     vgl->fmt.i_rmask  = 0xff000000;
455     vgl->fmt.i_gmask  = 0x00ff0000;
456     vgl->fmt.i_bmask  = 0x0000ff00;
457 #   else
458     vgl->fmt.i_rmask  = 0x000000ff;
459     vgl->fmt.i_gmask  = 0x0000ff00;
460     vgl->fmt.i_bmask  = 0x00ff0000;
461 #   endif
462     vgl->tex_target   = GL_TEXTURE_2D;
463     vgl->tex_format   = GL_RGBA;
464     vgl->tex_internal = GL_RGBA;
465     vgl->tex_type     = GL_UNSIGNED_BYTE;
466     /* Use YUV if possible and needed */
467     bool need_fs_yuv = false;
468     bool need_fs_rgba = USE_OPENGL_ES == 2;
469     float yuv_range_correction = 1.0;
470
471     if (max_texture_units >= 3 && supports_shaders && vlc_fourcc_IsYUV(fmt->i_chroma)) {
472         const vlc_fourcc_t *list = vlc_fourcc_GetYUVFallback(fmt->i_chroma);
473         while (*list) {
474             const vlc_chroma_description_t *dsc = vlc_fourcc_GetChromaDescription(*list);
475             if (dsc && dsc->plane_count == 3 && dsc->pixel_size == 1) {
476                 need_fs_yuv       = true;
477                 vgl->fmt          = *fmt;
478                 vgl->fmt.i_chroma = *list;
479                 vgl->tex_format   = GL_LUMINANCE;
480                 vgl->tex_internal = GL_LUMINANCE;
481                 vgl->tex_type     = GL_UNSIGNED_BYTE;
482                 yuv_range_correction = 1.0;
483                 break;
484 #if !USE_OPENGL_ES
485             } else if (dsc && dsc->plane_count == 3 && dsc->pixel_size == 2 &&
486                        IsLuminance16Supported(vgl->tex_target)) {
487                 need_fs_yuv       = true;
488                 vgl->fmt          = *fmt;
489                 vgl->fmt.i_chroma = *list;
490                 vgl->tex_format   = GL_LUMINANCE;
491                 vgl->tex_internal = GL_LUMINANCE16;
492                 vgl->tex_type     = GL_UNSIGNED_SHORT;
493                 yuv_range_correction = (float)((1 << 16) - 1) / ((1 << dsc->pixel_bits) - 1);
494                 break;
495 #endif
496             }
497             list++;
498         }
499     }
500
501     vgl->chroma = vlc_fourcc_GetChromaDescription(vgl->fmt.i_chroma);
502     vgl->use_multitexture = vgl->chroma->plane_count > 1;
503
504     /* Texture size */
505     for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
506         int w = vgl->fmt.i_width  * vgl->chroma->p[j].w.num / vgl->chroma->p[j].w.den;
507         int h = vgl->fmt.i_height * vgl->chroma->p[j].h.num / vgl->chroma->p[j].h.den;
508         if (vgl->supports_npot) {
509             vgl->tex_width[j]  = w;
510             vgl->tex_height[j] = h;
511         } else {
512             vgl->tex_width[j]  = GetAlignedSize(w);
513             vgl->tex_height[j] = GetAlignedSize(h);
514         }
515     }
516
517     /* Build program if needed */
518     vgl->program[0] =
519     vgl->program[1] = 0;
520     vgl->shader[0] =
521     vgl->shader[1] =
522     vgl->shader[2] = -1;
523     vgl->local_count = 0;
524     if (supports_shaders && (need_fs_yuv || need_fs_rgba)) {
525 #ifdef SUPPORTS_SHADERS
526         BuildYUVFragmentShader(vgl, &vgl->shader[0], &vgl->local_count,
527                                vgl->local_value, fmt, yuv_range_correction);
528         BuildRGBAFragmentShader(vgl, &vgl->shader[1]);
529         BuildVertexShader(vgl, &vgl->shader[2]);
530
531         /* Check shaders messages */
532         for (unsigned j = 0; j < 3; j++) {
533             int infoLength;
534             vgl->GetShaderiv(vgl->shader[j], GL_INFO_LOG_LENGTH, &infoLength);
535             if (infoLength <= 1)
536                 continue;
537
538             char *infolog = malloc(infoLength);
539             int charsWritten;
540             vgl->GetShaderInfoLog(vgl->shader[j], infoLength, &charsWritten, infolog);
541             fprintf(stderr, "shader %d: %s\n", j, infolog);
542             free(infolog);
543         }
544
545         vgl->program[0] = vgl->CreateProgram();
546         vgl->AttachShader(vgl->program[0], vgl->shader[0]);
547         vgl->AttachShader(vgl->program[0], vgl->shader[2]);
548         vgl->LinkProgram(vgl->program[0]);
549
550         vgl->program[1] = vgl->CreateProgram();
551         vgl->AttachShader(vgl->program[1], vgl->shader[1]);
552         vgl->AttachShader(vgl->program[1], vgl->shader[2]);
553         vgl->LinkProgram(vgl->program[1]);
554
555         /* Check program messages */
556         for (GLuint i = 0; i < 2; i++) {
557             int infoLength = 0;
558             vgl->GetProgramiv(vgl->program[i], GL_INFO_LOG_LENGTH, &infoLength);
559             if (infoLength <= 1)
560                 continue;
561             char *infolog = malloc(infoLength);
562             int charsWritten;
563             vgl->GetProgramInfoLog(vgl->program[i], infoLength, &charsWritten, infolog);
564             fprintf(stderr, "shader program %d: %s\n", i, infolog);
565             free(infolog);
566
567             /* If there is some message, better to check linking is ok */
568             GLint link_status = GL_TRUE;
569             vgl->GetProgramiv(vgl->program[i], GL_LINK_STATUS, &link_status);
570             if (link_status == GL_FALSE) {
571                 fprintf(stderr, "Unable to use program %d\n", i);
572                 free(vgl);
573                 return NULL;
574             }
575         }
576 #else
577         (void)yuv_range_correction;
578 #endif
579     }
580
581     /* */
582     glDisable(GL_BLEND);
583     glDisable(GL_DEPTH_TEST);
584     glDepthMask(GL_FALSE);
585     glDisable(GL_CULL_FACE);
586     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
587     glClear(GL_COLOR_BUFFER_BIT);
588
589     vlc_gl_Unlock(vgl->gl);
590
591     /* */
592     for (int i = 0; i < VLCGL_TEXTURE_COUNT; i++) {
593         for (int j = 0; j < PICTURE_PLANE_MAX; j++)
594             vgl->texture[i][j] = 0;
595     }
596     vgl->region_count = 0;
597     vgl->region = NULL;
598     vgl->pool = NULL;
599
600     *fmt = vgl->fmt;
601     if (subpicture_chromas) {
602         *subpicture_chromas = gl_subpicture_chromas;
603     }
604     return vgl;
605 }
606
607 void vout_display_opengl_Delete(vout_display_opengl_t *vgl)
608 {
609     /* */
610     if (!vlc_gl_Lock(vgl->gl)) {
611         glFinish();
612         glFlush();
613         for (int i = 0; i < VLCGL_TEXTURE_COUNT; i++)
614             glDeleteTextures(vgl->chroma->plane_count, vgl->texture[i]);
615         for (int i = 0; i < vgl->region_count; i++) {
616             if (vgl->region[i].texture)
617                 glDeleteTextures(1, &vgl->region[i].texture);
618         }
619         free(vgl->region);
620
621 #ifdef SUPPORTS_SHADERS
622         if (vgl->program[0]) {
623             for (int i = 0; i < 2; i++)
624                 vgl->DeleteProgram(vgl->program[i]);
625             for (int i = 0; i < 3; i++)
626                 vgl->DeleteShader(vgl->shader[i]);
627         }
628 #endif
629
630         free(vgl->texture_temp_buf);
631         vlc_gl_Unlock(vgl->gl);
632     }
633     if (vgl->pool)
634         picture_pool_Delete(vgl->pool);
635     free(vgl);
636 }
637
638 picture_pool_t *vout_display_opengl_GetPool(vout_display_opengl_t *vgl, unsigned requested_count)
639 {
640     if (vgl->pool)
641         return vgl->pool;
642
643     /* Allocate our pictures */
644     picture_t *picture[VLCGL_PICTURE_MAX] = {NULL, };
645     unsigned count;
646
647     for (count = 0; count < __MIN(VLCGL_PICTURE_MAX, requested_count); count++) {
648         picture[count] = picture_NewFromFormat(&vgl->fmt);
649         if (!picture[count])
650             break;
651     }
652     if (count <= 0)
653         return NULL;
654
655     /* Wrap the pictures into a pool */
656     picture_pool_configuration_t cfg;
657     memset(&cfg, 0, sizeof(cfg));
658     cfg.picture_count = count;
659     cfg.picture       = picture;
660     vgl->pool = picture_pool_NewExtended(&cfg);
661     if (!vgl->pool)
662         goto error;
663
664     /* Allocates our textures */
665     if (vlc_gl_Lock(vgl->gl))
666         return vgl->pool;
667
668     for (int i = 0; i < VLCGL_TEXTURE_COUNT; i++) {
669         glGenTextures(vgl->chroma->plane_count, vgl->texture[i]);
670         for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
671             if (vgl->use_multitexture) {
672                 glActiveTexture(GL_TEXTURE0 + j);
673                 glClientActiveTexture(GL_TEXTURE0 + j);
674             }
675             glBindTexture(vgl->tex_target, vgl->texture[i][j]);
676
677 #if !USE_OPENGL_ES
678             /* Set the texture parameters */
679             glTexParameterf(vgl->tex_target, GL_TEXTURE_PRIORITY, 1.0);
680             glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
681 #endif
682
683             glTexParameteri(vgl->tex_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
684             glTexParameteri(vgl->tex_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
685             glTexParameteri(vgl->tex_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
686             glTexParameteri(vgl->tex_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
687
688             /* Call glTexImage2D only once, and use glTexSubImage2D later */
689             glTexImage2D(vgl->tex_target, 0,
690                          vgl->tex_internal, vgl->tex_width[j], vgl->tex_height[j],
691                          0, vgl->tex_format, vgl->tex_type, NULL);
692         }
693     }
694
695     vlc_gl_Unlock(vgl->gl);
696
697     return vgl->pool;
698
699 error:
700     for (unsigned i = 0; i < count; i++)
701         picture_Release(picture[i]);
702     return NULL;
703 }
704
705 #define ALIGN(x, y) (((x) + ((y) - 1)) & ~((y) - 1))
706 static void Upload(vout_display_opengl_t *vgl, int in_width, int in_height,
707                    int in_full_width, int in_full_height,
708                    int w_num, int w_den, int h_num, int h_den,
709                    int pitch, int pixel_pitch,
710                    int full_upload, const uint8_t *pixels,
711                    int tex_target, int tex_format, int tex_type)
712 {
713     int width       =       in_width * w_num / w_den;
714     int full_width  =  in_full_width * w_num / w_den;
715     int height      =      in_height * h_num / h_den;
716     int full_height = in_full_height * h_num / h_den;
717     // This unpack alignment is the default, but setting it just in case.
718     glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
719 #ifndef GL_UNPACK_ROW_LENGTH
720     int dst_width = full_upload ? full_width : width;
721     int dst_pitch = ALIGN(dst_width * pixel_pitch, 4);
722     if ( pitch != dst_pitch )
723     {
724         int buf_size = dst_pitch * full_height * pixel_pitch;
725         const uint8_t *source = pixels;
726         uint8_t *destination;
727         if( !vgl->texture_temp_buf || vgl->texture_temp_buf_size < buf_size )
728         {
729             free( vgl->texture_temp_buf );
730             vgl->texture_temp_buf = xmalloc( buf_size );
731             vgl->texture_temp_buf_size = buf_size;
732         }
733         destination = vgl->texture_temp_buf;
734
735         for( int h = 0; h < height ; h++ )
736         {
737             memcpy( destination, source, width * pixel_pitch );
738             source += pitch;
739             destination += dst_pitch;
740         }
741         if (full_upload)
742             glTexImage2D( tex_target, 0, tex_format,
743                           full_width, full_height,
744                           0, tex_format, tex_type, vgl->texture_temp_buf );
745         else
746             glTexSubImage2D( tex_target, 0,
747                              0, 0,
748                              width, height,
749                              tex_format, tex_type, vgl->texture_temp_buf );
750     } else {
751 #else
752     (void) width;
753     (void) height;
754     (void) vgl;
755     {
756         glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch / pixel_pitch);
757 #endif
758         if (full_upload)
759             glTexImage2D(tex_target, 0, tex_format,
760                          full_width, full_height,
761                          0, tex_format, tex_type, pixels);
762         else
763             glTexSubImage2D(tex_target, 0,
764                             0, 0,
765                             width, height,
766                             tex_format, tex_type, pixels);
767     }
768 }
769
770 int vout_display_opengl_Prepare(vout_display_opengl_t *vgl,
771                                 picture_t *picture, subpicture_t *subpicture)
772 {
773     if (vlc_gl_Lock(vgl->gl))
774         return VLC_EGENERIC;
775
776     /* Update the texture */
777     for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
778         if (vgl->use_multitexture) {
779             glActiveTexture(GL_TEXTURE0 + j);
780             glClientActiveTexture(GL_TEXTURE0 + j);
781         }
782         glBindTexture(vgl->tex_target, vgl->texture[0][j]);
783
784         Upload(vgl, picture->format.i_visible_width, vgl->fmt.i_visible_height,
785                vgl->fmt.i_width, vgl->fmt.i_height,
786                vgl->chroma->p[j].w.num, vgl->chroma->p[j].w.den, vgl->chroma->p[j].h.num, vgl->chroma->p[j].h.den,
787                picture->p[j].i_pitch, picture->p[j].i_pixel_pitch, 0, picture->p[j].p_pixels, vgl->tex_target, vgl->tex_format, vgl->tex_type);
788     }
789
790     int         last_count = vgl->region_count;
791     gl_region_t *last = vgl->region;
792
793     vgl->region_count = 0;
794     vgl->region       = NULL;
795
796     if (subpicture) {
797
798         int count = 0;
799         for (subpicture_region_t *r = subpicture->p_region; r; r = r->p_next)
800             count++;
801
802         vgl->region_count = count;
803         vgl->region       = calloc(count, sizeof(*vgl->region));
804
805         if (vgl->use_multitexture) {
806             glActiveTexture(GL_TEXTURE0 + 0);
807             glClientActiveTexture(GL_TEXTURE0 + 0);
808         }
809         int i = 0;
810         for (subpicture_region_t *r = subpicture->p_region; r; r = r->p_next, i++) {
811             gl_region_t *glr = &vgl->region[i];
812
813             glr->format = GL_RGBA;
814             glr->type   = GL_UNSIGNED_BYTE;
815             glr->width  = r->fmt.i_visible_width;
816             glr->height = r->fmt.i_visible_height;
817             if (!vgl->supports_npot) {
818                 glr->width  = GetAlignedSize(glr->width);
819                 glr->height = GetAlignedSize(glr->height);
820                 glr->tex_width  = (float) r->fmt.i_visible_width  / glr->width;
821                 glr->tex_height = (float) r->fmt.i_visible_height / glr->height;
822             } else {
823                 glr->tex_width  = 1.0;
824                 glr->tex_height = 1.0;
825             }
826             glr->alpha  = (float)subpicture->i_alpha * r->i_alpha / 255 / 255;
827             glr->left   =  2.0 * (r->i_x                          ) / subpicture->i_original_picture_width  - 1.0;
828             glr->top    = -2.0 * (r->i_y                          ) / subpicture->i_original_picture_height + 1.0;
829             glr->right  =  2.0 * (r->i_x + r->fmt.i_visible_width ) / subpicture->i_original_picture_width  - 1.0;
830             glr->bottom = -2.0 * (r->i_y + r->fmt.i_visible_height) / subpicture->i_original_picture_height + 1.0;
831
832             glr->texture = 0;
833             for (int j = 0; j < last_count; j++) {
834                 if (last[j].texture &&
835                     last[j].width  == glr->width &&
836                     last[j].height == glr->height &&
837                     last[j].format == glr->format &&
838                     last[j].type   == glr->type) {
839                     glr->texture = last[j].texture;
840                     memset(&last[j], 0, sizeof(last[j]));
841                     break;
842                 }
843             }
844
845             const int pixels_offset = r->fmt.i_y_offset * r->p_picture->p->i_pitch +
846                                       r->fmt.i_x_offset * r->p_picture->p->i_pixel_pitch;
847             if (glr->texture) {
848                 glBindTexture(GL_TEXTURE_2D, glr->texture);
849                 Upload(vgl, r->fmt.i_visible_width, r->fmt.i_visible_height, glr->width, glr->height, 1, 1, 1, 1,
850                        r->p_picture->p->i_pitch, r->p_picture->p->i_pixel_pitch, 0,
851                        &r->p_picture->p->p_pixels[pixels_offset], GL_TEXTURE_2D, glr->format, glr->type);
852             } else {
853                 glGenTextures(1, &glr->texture);
854                 glBindTexture(GL_TEXTURE_2D, glr->texture);
855 #if !USE_OPENGL_ES
856                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, 1.0);
857                 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
858 #endif
859                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
860                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
861                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
862                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
863                 Upload(vgl, r->fmt.i_visible_width, r->fmt.i_visible_height, glr->width, glr->height, 1, 1, 1, 1,
864                        r->p_picture->p->i_pitch, r->p_picture->p->i_pixel_pitch, 1,
865                        &r->p_picture->p->p_pixels[pixels_offset], GL_TEXTURE_2D, glr->format, glr->type);
866             }
867         }
868     }
869     for (int i = 0; i < last_count; i++) {
870         if (last[i].texture)
871             glDeleteTextures(1, &last[i].texture);
872     }
873     free(last);
874
875     vlc_gl_Unlock(vgl->gl);
876     VLC_UNUSED(subpicture);
877     return VLC_SUCCESS;
878 }
879
880 #ifdef SUPPORTS_FIXED_PIPELINE
881 static void DrawWithoutShaders(vout_display_opengl_t *vgl,
882                                float *left, float *top, float *right, float *bottom)
883 {
884     static const GLfloat vertexCoord[] = {
885         -1.0f, -1.0f,
886          1.0f, -1.0f,
887         -1.0f,  1.0f,
888          1.0f,  1.0f,
889     };
890
891     const GLfloat textureCoord[] = {
892         left[0],  bottom[0],
893         right[0], bottom[0],
894         left[0],  top[0],
895         right[0], top[0]
896     };
897
898     glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
899     glEnable(vgl->tex_target);
900     glActiveTexture(GL_TEXTURE0 + 0);
901     glClientActiveTexture(GL_TEXTURE0 + 0);
902
903     glBindTexture(vgl->tex_target, vgl->texture[0][0]);
904
905     glEnableClientState(GL_VERTEX_ARRAY);
906     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
907
908     glTexCoordPointer(2, GL_FLOAT, 0, textureCoord);
909     glVertexPointer(2, GL_FLOAT, 0, vertexCoord);
910
911     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
912
913     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
914     glDisableClientState(GL_VERTEX_ARRAY);
915     glDisable(vgl->tex_target);
916 }
917 #endif
918
919 #ifdef SUPPORTS_SHADERS
920 static void DrawWithShaders(vout_display_opengl_t *vgl,
921                             float *left, float *top, float *right, float *bottom,
922                             int program)
923 {
924     vgl->UseProgram(vgl->program[program]);
925     if (program == 0) {
926         vgl->Uniform4fv(vgl->GetUniformLocation(vgl->program[0], "Coefficient"), 4, vgl->local_value);
927         vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture0"), 0);
928         vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture1"), 1);
929         vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture2"), 2);
930     } else {
931         vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[1], "Texture0"), 0);
932         vgl->Uniform4f(vgl->GetUniformLocation(vgl->program[1], "FillColor"), 1.0f, 1.0f, 1.0f, 1.0f);
933     }
934
935     static const GLfloat vertexCoord[] = {
936         -1.0,  1.0,
937         -1.0, -1.0,
938          1.0,  1.0,
939          1.0, -1.0,
940     };
941
942     for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
943         const GLfloat textureCoord[] = {
944             left[j],  top[j],
945             left[j],  bottom[j],
946             right[j], top[j],
947             right[j], bottom[j],
948         };
949         glActiveTexture(GL_TEXTURE0+j);
950         glClientActiveTexture(GL_TEXTURE0+j);
951         glEnable(vgl->tex_target);
952         glBindTexture(vgl->tex_target, vgl->texture[0][j]);
953
954         char attribute[20];
955         snprintf(attribute, sizeof(attribute), "MultiTexCoord%1d", j);
956         vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[program], attribute));
957         vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[program], attribute), 2, GL_FLOAT, 0, 0, textureCoord);
958     }
959     glActiveTexture(GL_TEXTURE0 + 0);
960     glClientActiveTexture(GL_TEXTURE0 + 0);
961     vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[program], "VertexPosition"));
962     vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[program], "VertexPosition"), 2, GL_FLOAT, 0, 0, vertexCoord);
963
964     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
965 }
966 #endif
967
968 int vout_display_opengl_Display(vout_display_opengl_t *vgl,
969                                 const video_format_t *source)
970 {
971     if (vlc_gl_Lock(vgl->gl))
972         return VLC_EGENERIC;
973
974     /* Why drawing here and not in Render()? Because this way, the
975        OpenGL providers can call vout_display_opengl_Display to force redraw.i
976        Currently, the OS X provider uses it to get a smooth window resizing */
977     glClear(GL_COLOR_BUFFER_BIT);
978
979     /* Draw the picture */
980     float left[PICTURE_PLANE_MAX];
981     float top[PICTURE_PLANE_MAX];
982     float right[PICTURE_PLANE_MAX];
983     float bottom[PICTURE_PLANE_MAX];
984     for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
985         /* glTexCoord works differently with GL_TEXTURE_2D and
986            GL_TEXTURE_RECTANGLE_EXT */
987         float scale_w, scale_h;
988
989         if (vgl->tex_target == GL_TEXTURE_2D) {
990             scale_w = (float)vgl->chroma->p[j].w.num / vgl->chroma->p[j].w.den / vgl->tex_width[j];
991             scale_h = (float)vgl->chroma->p[j].h.num / vgl->chroma->p[j].h.den / vgl->tex_height[j];
992
993         } else {
994             scale_w = 1.0;
995             scale_h = 1.0;
996         }
997         left[j]   = (source->i_x_offset +                       0 ) * scale_w;
998         top[j]    = (source->i_y_offset +                       0 ) * scale_h;
999         right[j]  = (source->i_x_offset + source->i_visible_width ) * scale_w;
1000         bottom[j] = (source->i_y_offset + source->i_visible_height) * scale_h;
1001     }
1002
1003 #ifdef SUPPORTS_SHADERS
1004     if (vgl->program[0] && vgl->chroma->plane_count == 3)
1005         DrawWithShaders(vgl, left, top, right, bottom, 0);
1006     else if (vgl->program[1] && vgl->chroma->plane_count == 1)
1007         DrawWithShaders(vgl, left, top, right, bottom, 1);
1008     else
1009 #endif
1010     {
1011 #ifdef SUPPORTS_FIXED_PIPELINE
1012         DrawWithoutShaders(vgl, left, top, right, bottom);
1013 #endif
1014     }
1015
1016     /* Draw the subpictures */
1017     if (vgl->program[1]) {
1018 #ifdef SUPPORTS_SHADERS
1019         // Change the program for overlays
1020         vgl->UseProgram(vgl->program[1]);
1021         vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[1], "Texture"), 0);
1022 #endif
1023     }
1024
1025     glEnable(GL_TEXTURE_2D);
1026     glEnable(GL_BLEND);
1027     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1028
1029     glActiveTexture(GL_TEXTURE0 + 0);
1030     glClientActiveTexture(GL_TEXTURE0 + 0);
1031     for (int i = 0; i < vgl->region_count; i++) {
1032         gl_region_t *glr = &vgl->region[i];
1033         const GLfloat vertexCoord[] = {
1034             glr->left,  glr->top,
1035             glr->left,  glr->bottom,
1036             glr->right, glr->top,
1037             glr->right, glr->bottom,
1038         };
1039         const GLfloat textureCoord[] = {
1040             0.0, 0.0,
1041             0.0, glr->tex_height,
1042             glr->tex_width, 0.0,
1043             glr->tex_width, glr->tex_height,
1044         };
1045
1046         glBindTexture(GL_TEXTURE_2D, glr->texture);
1047         if (vgl->program[1]) {
1048 #ifdef SUPPORTS_SHADERS
1049             vgl->Uniform4f(vgl->GetUniformLocation(vgl->program[1], "FillColor"), 1.0f, 1.0f, 1.0f, glr->alpha);
1050             vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[1], "MultiTexCoord0"));
1051             vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[1], "MultiTexCoord0"), 2, GL_FLOAT, 0, 0, textureCoord);
1052             vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[1], "VertexPosition"));
1053             vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[1], "VertexPosition"), 2, GL_FLOAT, 0, 0, vertexCoord);
1054 #endif
1055         } else {
1056 #ifdef SUPPORTS_FIXED_PIPELINE
1057             glEnableClientState(GL_VERTEX_ARRAY);
1058             glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1059             glColor4f(1.0f, 1.0f, 1.0f, glr->alpha);
1060             glTexCoordPointer(2, GL_FLOAT, 0, textureCoord);
1061             glVertexPointer(2, GL_FLOAT, 0, vertexCoord);
1062 #endif
1063         }
1064
1065         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1066
1067         if (!vgl->program[1]) {
1068 #ifdef SUPPORTS_FIXED_PIPELINE
1069             glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1070             glDisableClientState(GL_VERTEX_ARRAY);
1071 #endif
1072         }
1073     }
1074     glDisable(GL_BLEND);
1075     glDisable(GL_TEXTURE_2D);
1076
1077     /* Display */
1078     vlc_gl_Swap(vgl->gl);
1079
1080     vlc_gl_Unlock(vgl->gl);
1081     return VLC_SUCCESS;
1082 }
1083