]> git.sesse.net Git - vlc/blob - modules/video_output/opengl.c
decoder: fix data race in input_DecoderFrameNext()
[vlc] / 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 <assert.h>
33
34 #include <vlc_common.h>
35 #include <vlc_picture_pool.h>
36 #include <vlc_subpicture.h>
37 #include <vlc_opengl.h>
38 #include <vlc_memory.h>
39
40 #include "opengl.h"
41
42 #ifndef GL_CLAMP_TO_EDGE
43 # define GL_CLAMP_TO_EDGE 0x812F
44 #endif
45
46 #if USE_OPENGL_ES == 2 || defined(__APPLE__)
47 #   define PFNGLGETPROGRAMIVPROC             typeof(glGetProgramiv)*
48 #   define PFNGLGETPROGRAMINFOLOGPROC        typeof(glGetProgramInfoLog)*
49 #   define PFNGLGETSHADERIVPROC              typeof(glGetShaderiv)*
50 #   define PFNGLGETSHADERINFOLOGPROC         typeof(glGetShaderInfoLog)*
51 #   define PFNGLGETUNIFORMLOCATIONPROC       typeof(glGetUniformLocation)*
52 #   define PFNGLGETATTRIBLOCATIONPROC        typeof(glGetAttribLocation)*
53 #   define PFNGLVERTEXATTRIBPOINTERPROC      typeof(glVertexAttribPointer)*
54 #   define PFNGLENABLEVERTEXATTRIBARRAYPROC  typeof(glEnableVertexAttribArray)*
55 #   define PFNGLUNIFORMMATRIX4FVPROC         typeof(glUniformMatrix4fv)*
56 #   define PFNGLUNIFORM4FVPROC               typeof(glUniform4fv)*
57 #   define PFNGLUNIFORM4FPROC                typeof(glUniform4f)*
58 #   define PFNGLUNIFORM1IPROC                typeof(glUniform1i)*
59 #   define PFNGLCREATESHADERPROC             typeof(glCreateShader)*
60 #   define PFNGLSHADERSOURCEPROC             typeof(glShaderSource)*
61 #   define PFNGLCOMPILESHADERPROC            typeof(glCompileShader)*
62 #   define PFNGLDELETESHADERPROC             typeof(glDeleteShader)*
63 #   define PFNGLCREATEPROGRAMPROC            typeof(glCreateProgram)*
64 #   define PFNGLLINKPROGRAMPROC              typeof(glLinkProgram)*
65 #   define PFNGLUSEPROGRAMPROC               typeof(glUseProgram)*
66 #   define PFNGLDELETEPROGRAMPROC            typeof(glDeleteProgram)*
67 #   define PFNGLATTACHSHADERPROC             typeof(glAttachShader)*
68 #   define PFNGLGENBUFFERSPROC               typeof(glGenBuffers)*
69 #   define PFNGLBINDBUFFERPROC               typeof(glBindBuffer)*
70 #   define PFNGLBUFFERDATAPROC               typeof(glBufferData)*
71 #   define PFNGLDELETEBUFFERSPROC            typeof(glDeleteBuffers)*
72 #if defined(__APPLE__) && USE_OPENGL_ES
73 #   import <CoreFoundation/CoreFoundation.h>
74 #endif
75 #endif
76
77 #if USE_OPENGL_ES
78 #   define GLSL_VERSION "100"
79 #   define VLCGL_TEXTURE_COUNT 1
80 #   define VLCGL_PICTURE_MAX 1
81 #   define PRECISION "precision highp float;"
82 #if USE_OPENGL_ES == 2
83 #   define SUPPORTS_SHADERS
84 #   define glClientActiveTexture(x)
85 #else
86 #   define SUPPORTS_FIXED_PIPELINE
87 #   define GL_MAX_TEXTURE_IMAGE_UNITS GL_MAX_TEXTURE_UNITS
88 #endif
89 #else
90 #   define GLSL_VERSION "120"
91 #   define VLCGL_TEXTURE_COUNT 1
92 #   define VLCGL_PICTURE_MAX 128
93 #   define PRECISION ""
94 #   define SUPPORTS_SHADERS
95 #   define SUPPORTS_FIXED_PIPELINE
96 #endif
97
98 static const vlc_fourcc_t gl_subpicture_chromas[] = {
99     VLC_CODEC_RGBA,
100     0
101 };
102
103 typedef struct {
104     GLuint   texture;
105     unsigned format;
106     unsigned type;
107     unsigned width;
108     unsigned height;
109
110     float    alpha;
111
112     float    top;
113     float    left;
114     float    bottom;
115     float    right;
116
117     float    tex_width;
118     float    tex_height;
119 } gl_region_t;
120
121 struct vout_display_opengl_t {
122
123     vlc_gl_t   *gl;
124
125     video_format_t fmt;
126     const vlc_chroma_description_t *chroma;
127
128     int        tex_target;
129     int        tex_format;
130     int        tex_internal;
131     int        tex_type;
132
133     int        tex_width[PICTURE_PLANE_MAX];
134     int        tex_height[PICTURE_PLANE_MAX];
135
136     GLuint     texture[VLCGL_TEXTURE_COUNT][PICTURE_PLANE_MAX];
137
138     int         region_count;
139     gl_region_t *region;
140
141
142     picture_pool_t *pool;
143
144     /* index 0 for normal and 1 for subtitle overlay */
145     GLuint     program[2];
146     GLint      shader[3]; //3. is for the common vertex shader
147     int        local_count;
148     GLfloat    local_value[16];
149
150     GLuint vertex_buffer_object;
151     GLuint texture_buffer_object[PICTURE_PLANE_MAX];
152
153     GLuint *subpicture_buffer_object;
154     int    subpicture_buffer_object_count;
155
156     /* Shader variables commands*/
157 #ifdef SUPPORTS_SHADERS
158     PFNGLGETUNIFORMLOCATIONPROC      GetUniformLocation;
159     PFNGLGETATTRIBLOCATIONPROC       GetAttribLocation;
160     PFNGLVERTEXATTRIBPOINTERPROC     VertexAttribPointer;
161     PFNGLENABLEVERTEXATTRIBARRAYPROC EnableVertexAttribArray;
162
163     PFNGLUNIFORMMATRIX4FVPROC   UniformMatrix4fv;
164     PFNGLUNIFORM4FVPROC         Uniform4fv;
165     PFNGLUNIFORM4FPROC          Uniform4f;
166     PFNGLUNIFORM1IPROC          Uniform1i;
167
168     /* Shader command */
169     PFNGLCREATESHADERPROC CreateShader;
170     PFNGLSHADERSOURCEPROC ShaderSource;
171     PFNGLCOMPILESHADERPROC CompileShader;
172     PFNGLDELETESHADERPROC   DeleteShader;
173
174     PFNGLCREATEPROGRAMPROC CreateProgram;
175     PFNGLLINKPROGRAMPROC   LinkProgram;
176     PFNGLUSEPROGRAMPROC    UseProgram;
177     PFNGLDELETEPROGRAMPROC DeleteProgram;
178
179     PFNGLATTACHSHADERPROC  AttachShader;
180
181     /* Shader log commands */
182     PFNGLGETPROGRAMIVPROC  GetProgramiv;
183     PFNGLGETPROGRAMINFOLOGPROC GetProgramInfoLog;
184     PFNGLGETSHADERIVPROC   GetShaderiv;
185     PFNGLGETSHADERINFOLOGPROC GetShaderInfoLog;
186
187     PFNGLGENBUFFERSPROC    GenBuffers;
188     PFNGLBINDBUFFERPROC    BindBuffer;
189     PFNGLBUFFERDATAPROC    BufferData;
190     PFNGLDELETEBUFFERSPROC DeleteBuffers;
191 #endif
192
193 #if defined(_WIN32)
194     PFNGLACTIVETEXTUREPROC  ActiveTexture;
195     PFNGLCLIENTACTIVETEXTUREPROC  ClientActiveTexture;
196 #endif
197
198
199     /* multitexture */
200     bool use_multitexture;
201
202     /* Non-power-of-2 texture size support */
203     bool supports_npot;
204
205     uint8_t *texture_temp_buf;
206     int      texture_temp_buf_size;
207 };
208
209 static inline int GetAlignedSize(unsigned size)
210 {
211     /* Return the smallest larger or equal power of 2 */
212     unsigned align = 1 << (8 * sizeof (unsigned) - clz(size));
213     return ((align >> 1) == size) ? size : align;
214 }
215
216 #if !USE_OPENGL_ES
217 static bool IsLuminance16Supported(int target)
218 {
219     GLuint texture;
220
221     glGenTextures(1, &texture);
222     glBindTexture(target, texture);
223     glTexImage2D(target, 0, GL_LUMINANCE16,
224                  64, 64, 0, GL_LUMINANCE, GL_UNSIGNED_SHORT, NULL);
225     GLint size = 0;
226     glGetTexLevelParameteriv(target, 0, GL_TEXTURE_LUMINANCE_SIZE, &size);
227
228     glDeleteTextures(1, &texture);
229
230     return size == 16;
231 }
232 #endif
233
234 #ifdef SUPPORTS_SHADERS
235 static void BuildVertexShader(vout_display_opengl_t *vgl,
236                               GLint *shader)
237 {
238     /* Basic vertex shader */
239     const char *vertexShader =
240         "#version " GLSL_VERSION "\n"
241         PRECISION
242         "varying vec4 TexCoord0,TexCoord1, TexCoord2;"
243         "attribute vec4 MultiTexCoord0,MultiTexCoord1,MultiTexCoord2;"
244         "attribute vec2 VertexPosition;"
245         "uniform mat4 RotationMatrix;"
246         "void main() {"
247         " TexCoord0 = MultiTexCoord0;"
248         " TexCoord1 = MultiTexCoord1;"
249         " TexCoord2 = MultiTexCoord2;"
250         " gl_Position = RotationMatrix * vec4(VertexPosition, 0.0, 1.0);"
251         "}";
252
253     *shader = vgl->CreateShader(GL_VERTEX_SHADER);
254     vgl->ShaderSource(*shader, 1, &vertexShader, NULL);
255     vgl->CompileShader(*shader);
256 }
257
258 static void BuildYUVFragmentShader(vout_display_opengl_t *vgl,
259                                    GLint *shader,
260                                    int *local_count,
261                                    GLfloat *local_value,
262                                    const video_format_t *fmt,
263                                    float yuv_range_correction)
264
265 {
266     /* [R/G/B][Y U V O] from TV range to full range
267      * XXX we could also do hue/brightness/constrast/gamma
268      * by simply changing the coefficients
269      */
270     const float matrix_bt601_tv2full[12] = {
271         1.164383561643836,  0.0000,             1.596026785714286, -0.874202217873451 ,
272         1.164383561643836, -0.391762290094914, -0.812967647237771,  0.531667823499146 ,
273         1.164383561643836,  2.017232142857142,  0.0000,            -1.085630789302022 ,
274     };
275     const float matrix_bt709_tv2full[12] = {
276         1.164383561643836,  0.0000,             1.792741071428571, -0.972945075016308 ,
277         1.164383561643836, -0.21324861427373,  -0.532909328559444,  0.301482665475862 ,
278         1.164383561643836,  2.112401785714286,  0.0000,            -1.133402217873451 ,
279     };
280     const float (*matrix) = fmt->i_height > 576 ? matrix_bt709_tv2full
281                                                 : matrix_bt601_tv2full;
282
283     /* Basic linear YUV -> RGB conversion using bilinear interpolation */
284     const char *template_glsl_yuv =
285         "#version " GLSL_VERSION "\n"
286         PRECISION
287         "uniform sampler2D Texture0;"
288         "uniform sampler2D Texture1;"
289         "uniform sampler2D Texture2;"
290         "uniform vec4      Coefficient[4];"
291         "varying vec4      TexCoord0,TexCoord1,TexCoord2;"
292
293         "void main(void) {"
294         " vec4 x,y,z,result;"
295         " x  = texture2D(Texture0, TexCoord0.st);"
296         " %c = texture2D(Texture1, TexCoord1.st);"
297         " %c = texture2D(Texture2, TexCoord2.st);"
298
299         " result = x * Coefficient[0] + Coefficient[3];"
300         " result = (y * Coefficient[1]) + result;"
301         " result = (z * Coefficient[2]) + result;"
302         " gl_FragColor = result;"
303         "}";
304     bool swap_uv = fmt->i_chroma == VLC_CODEC_YV12 ||
305                    fmt->i_chroma == VLC_CODEC_YV9;
306
307     char *code;
308     if (asprintf(&code, template_glsl_yuv,
309                  swap_uv ? 'z' : 'y',
310                  swap_uv ? 'y' : 'z') < 0)
311         code = NULL;
312
313     for (int i = 0; i < 4; i++) {
314         float correction = i < 3 ? yuv_range_correction : 1.f;
315         /* We place coefficient values for coefficient[4] in one array from matrix values.
316            Notice that we fill values from top down instead of left to right.*/
317         for (int j = 0; j < 4; j++)
318             local_value[*local_count + i*4+j] = j < 3 ? correction * matrix[j*4+i]
319                                                       : 0.f;
320     }
321     (*local_count) += 4;
322
323
324     *shader = vgl->CreateShader(GL_FRAGMENT_SHADER);
325     vgl->ShaderSource(*shader, 1, (const char **)&code, NULL);
326     vgl->CompileShader(*shader);
327
328     free(code);
329 }
330
331 #if 0
332 static void BuildRGBFragmentShader(vout_display_opengl_t *vgl,
333                                    GLint *shader)
334 {
335     // Simple shader for RGB
336     const char *code =
337         "#version " GLSL_VERSION "\n"
338         PRECISION
339         "uniform sampler2D Texture[3];"
340         "varying vec4 TexCoord0,TexCoord1,TexCoord2;"
341         "void main()"
342         "{ "
343         "  gl_FragColor = texture2D(Texture[0], TexCoord0.st);"
344         "}";
345     *shader = vgl->CreateShader(GL_FRAGMENT_SHADER);
346     vgl->ShaderSource(*shader, 1, &code, NULL);
347     vgl->CompileShader(*shader);
348 }
349 #endif
350
351 static void BuildRGBAFragmentShader(vout_display_opengl_t *vgl,
352                                    GLint *shader)
353 {
354     // Simple shader for RGBA
355     const char *code =
356         "#version " GLSL_VERSION "\n"
357         PRECISION
358         "uniform sampler2D Texture;"
359         "uniform vec4 FillColor;"
360         "varying vec4 TexCoord0;"
361         "void main()"
362         "{ "
363         "  gl_FragColor = texture2D(Texture, TexCoord0.st) * FillColor;"
364         "}";
365     *shader = vgl->CreateShader(GL_FRAGMENT_SHADER);
366     vgl->ShaderSource(*shader, 1, &code, NULL);
367     vgl->CompileShader(*shader);
368 }
369
370 static void BuildXYZFragmentShader(vout_display_opengl_t *vgl,
371                                    GLint *shader)
372 {
373     /* Shader for XYZ to RGB correction
374      * 3 steps :
375      *  - XYZ gamma correction
376      *  - XYZ to RGB matrix conversion
377      *  - reverse RGB gamma correction
378      */
379       const char *code =
380         "#version " GLSL_VERSION "\n"
381         PRECISION
382         "uniform sampler2D Texture0;"
383         "uniform vec4 xyz_gamma = vec4(2.6);"
384         "uniform vec4 rgb_gamma = vec4(1.0/2.2);"
385         // WARN: matrix Is filled column by column (not row !)
386         "uniform mat4 matrix_xyz_rgb = mat4("
387         "    3.240454 , -0.9692660, 0.0556434, 0.0,"
388         "   -1.5371385,  1.8760108, -0.2040259, 0.0,"
389         "    -0.4985314, 0.0415560, 1.0572252,  0.0,"
390         "    0.0,      0.0,         0.0,        1.0 "
391         " );"
392
393         "varying vec4 TexCoord0;"
394         "void main()"
395         "{ "
396         " vec4 v_in, v_out;"
397         " v_in  = texture2D(Texture0, TexCoord0.st);"
398         " v_in = pow(v_in, xyz_gamma);"
399         " v_out = matrix_xyz_rgb * v_in ;"
400         " v_out = pow(v_out, rgb_gamma) ;"
401         " v_out = clamp(v_out, 0.0, 1.0) ;"
402         " gl_FragColor = v_out;"
403         "}";
404     *shader = vgl->CreateShader(GL_FRAGMENT_SHADER);
405     vgl->ShaderSource(*shader, 1, &code, NULL);
406     vgl->CompileShader(*shader);
407 }
408
409 #endif
410
411 vout_display_opengl_t *vout_display_opengl_New(video_format_t *fmt,
412                                                const vlc_fourcc_t **subpicture_chromas,
413                                                vlc_gl_t *gl)
414 {
415     vout_display_opengl_t *vgl = calloc(1, sizeof(*vgl));
416     if (!vgl)
417         return NULL;
418
419     vgl->gl = gl;
420     if (vlc_gl_Lock(vgl->gl)) {
421         free(vgl);
422         return NULL;
423     }
424
425     if (vgl->gl->getProcAddress == NULL) {
426         fprintf(stderr, "getProcAddress not implemented, bailing out\n");
427         free(vgl);
428         return NULL;
429     }
430
431     const char *extensions = (const char *)glGetString(GL_EXTENSIONS);
432 #if !USE_OPENGL_ES
433     const unsigned char *ogl_version = glGetString(GL_VERSION);
434     bool supports_shaders = strverscmp((const char *)ogl_version, "2.0") >= 0;
435 #else
436     bool supports_shaders = false;
437 #endif
438
439 #if USE_OPENGL_ES == 2
440     vgl->CreateShader  = glCreateShader;
441     vgl->ShaderSource  = glShaderSource;
442     vgl->CompileShader = glCompileShader;
443     vgl->AttachShader  = glAttachShader;
444
445     vgl->GetProgramiv  = glGetProgramiv;
446     vgl->GetShaderiv   = glGetShaderiv;
447     vgl->GetProgramInfoLog  = glGetProgramInfoLog;
448     vgl->GetShaderInfoLog   = glGetShaderInfoLog;
449
450     vgl->DeleteShader  = glDeleteShader;
451
452     vgl->GetUniformLocation = glGetUniformLocation;
453     vgl->GetAttribLocation  = glGetAttribLocation;
454     vgl->VertexAttribPointer= glVertexAttribPointer;
455     vgl->EnableVertexAttribArray = glEnableVertexAttribArray;
456     vgl->UniformMatrix4fv = glUniformMatrix4fv;
457     vgl->Uniform4fv    = glUniform4fv;
458     vgl->Uniform4f     = glUniform4f;
459     vgl->Uniform1i     = glUniform1i;
460
461     vgl->CreateProgram = glCreateProgram;
462     vgl->LinkProgram   = glLinkProgram;
463     vgl->UseProgram    = glUseProgram;
464     vgl->DeleteProgram = glDeleteProgram;
465
466     vgl->GenBuffers    = glGenBuffers;
467     vgl->BindBuffer    = glBindBuffer;
468     vgl->BufferData    = glBufferData;
469     vgl->DeleteBuffers = glDeleteBuffers;
470
471     supports_shaders = true;
472 #elif defined(SUPPORTS_SHADERS)
473     vgl->CreateShader  = (PFNGLCREATESHADERPROC)vlc_gl_GetProcAddress(vgl->gl, "glCreateShader");
474     vgl->ShaderSource  = (PFNGLSHADERSOURCEPROC)vlc_gl_GetProcAddress(vgl->gl, "glShaderSource");
475     vgl->CompileShader = (PFNGLCOMPILESHADERPROC)vlc_gl_GetProcAddress(vgl->gl, "glCompileShader");
476     vgl->AttachShader  = (PFNGLATTACHSHADERPROC)vlc_gl_GetProcAddress(vgl->gl, "glAttachShader");
477
478     vgl->GetProgramiv  = (PFNGLGETPROGRAMIVPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetProgramiv");
479     vgl->GetShaderiv   = (PFNGLGETSHADERIVPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetShaderiv");
480     vgl->GetProgramInfoLog  = (PFNGLGETPROGRAMINFOLOGPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetProgramInfoLog");
481     vgl->GetShaderInfoLog   = (PFNGLGETSHADERINFOLOGPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetShaderInfoLog");
482
483     vgl->DeleteShader  = (PFNGLDELETESHADERPROC)vlc_gl_GetProcAddress(vgl->gl, "glDeleteShader");
484
485     vgl->GetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetUniformLocation");
486     vgl->GetAttribLocation  = (PFNGLGETATTRIBLOCATIONPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetAttribLocation");
487     vgl->VertexAttribPointer= (PFNGLVERTEXATTRIBPOINTERPROC)vlc_gl_GetProcAddress(vgl->gl, "glVertexAttribPointer");
488     vgl->EnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYPROC)vlc_gl_GetProcAddress(vgl->gl, "glEnableVertexAttribArray");
489     vgl->UniformMatrix4fv   = (PFNGLUNIFORMMATRIX4FVPROC)vlc_gl_GetProcAddress(vgl->gl,"glUniformMatrix4fv");
490     vgl->Uniform4fv    = (PFNGLUNIFORM4FVPROC)vlc_gl_GetProcAddress(vgl->gl,"glUniform4fv");
491     vgl->Uniform4f     = (PFNGLUNIFORM4FPROC)vlc_gl_GetProcAddress(vgl->gl,"glUniform4f");
492     vgl->Uniform1i     = (PFNGLUNIFORM1IPROC)vlc_gl_GetProcAddress(vgl->gl,"glUniform1i");
493
494     vgl->CreateProgram = (PFNGLCREATEPROGRAMPROC)vlc_gl_GetProcAddress(vgl->gl, "glCreateProgram");
495     vgl->LinkProgram   = (PFNGLLINKPROGRAMPROC)vlc_gl_GetProcAddress(vgl->gl, "glLinkProgram");
496     vgl->UseProgram    = (PFNGLUSEPROGRAMPROC)vlc_gl_GetProcAddress(vgl->gl, "glUseProgram");
497     vgl->DeleteProgram = (PFNGLDELETEPROGRAMPROC)vlc_gl_GetProcAddress(vgl->gl, "glDeleteProgram");
498
499     vgl->GenBuffers    = (PFNGLGENBUFFERSPROC)vlc_gl_GetProcAddress(vgl->gl, "glGenBuffers");
500     vgl->BindBuffer    = (PFNGLBINDBUFFERPROC)vlc_gl_GetProcAddress(vgl->gl, "glBindBuffer");
501     vgl->BufferData    = (PFNGLBUFFERDATAPROC)vlc_gl_GetProcAddress(vgl->gl, "glBufferData");
502     vgl->DeleteBuffers = (PFNGLDELETEBUFFERSPROC)vlc_gl_GetProcAddress(vgl->gl, "glDeleteBuffers");
503
504     if (!vgl->CreateShader || !vgl->ShaderSource || !vgl->CreateProgram)
505         supports_shaders = false;
506 #endif
507
508 #if defined(_WIN32)
509     vgl->ActiveTexture = (PFNGLACTIVETEXTUREPROC)vlc_gl_GetProcAddress(vgl->gl, "glActiveTexture");
510     vgl->ClientActiveTexture = (PFNGLCLIENTACTIVETEXTUREPROC)vlc_gl_GetProcAddress(vgl->gl, "glClientActiveTexture");
511 #   define glActiveTexture vgl->ActiveTexture
512 #   define glClientActiveTexture vgl->ClientActiveTexture
513 #endif
514
515     vgl->supports_npot = HasExtension(extensions, "GL_ARB_texture_non_power_of_two") ||
516                          HasExtension(extensions, "GL_APPLE_texture_2D_limited_npot");
517
518 #if USE_OPENGL_ES == 2
519     /* OpenGL ES 2 includes support for non-power of 2 textures by specification
520      * so checks for extensions are bound to fail. Check for OpenGL ES version instead. */
521     vgl->supports_npot = true;
522 #endif
523
524     GLint max_texture_units = 0;
525     glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &max_texture_units);
526
527 #ifdef __APPLE__
528 #if USE_OPENGL_ES
529     /* work-around an iOS 6 bug */
530     if (kCFCoreFoundationVersionNumber >= 786.)
531         max_texture_units = 8;
532     supports_shaders = true;
533 #endif
534 #endif
535
536     /* Initialize with default chroma */
537     vgl->fmt = *fmt;
538     vgl->fmt.i_chroma = VLC_CODEC_RGB32;
539 #   if defined(WORDS_BIGENDIAN)
540     vgl->fmt.i_rmask  = 0xff000000;
541     vgl->fmt.i_gmask  = 0x00ff0000;
542     vgl->fmt.i_bmask  = 0x0000ff00;
543 #   else
544     vgl->fmt.i_rmask  = 0x000000ff;
545     vgl->fmt.i_gmask  = 0x0000ff00;
546     vgl->fmt.i_bmask  = 0x00ff0000;
547 #   endif
548     vgl->tex_target   = GL_TEXTURE_2D;
549     vgl->tex_format   = GL_RGBA;
550     vgl->tex_internal = GL_RGBA;
551     vgl->tex_type     = GL_UNSIGNED_BYTE;
552     /* Use YUV if possible and needed */
553     bool need_fs_yuv = false;
554     bool need_fs_xyz = false;
555     bool need_fs_rgba = USE_OPENGL_ES == 2;
556     float yuv_range_correction = 1.0;
557
558     if (max_texture_units >= 3 && supports_shaders && vlc_fourcc_IsYUV(fmt->i_chroma)) {
559         const vlc_fourcc_t *list = vlc_fourcc_GetYUVFallback(fmt->i_chroma);
560         while (*list) {
561             const vlc_chroma_description_t *dsc = vlc_fourcc_GetChromaDescription(*list);
562             if (dsc && dsc->plane_count == 3 && dsc->pixel_size == 1) {
563                 need_fs_yuv       = true;
564                 vgl->fmt          = *fmt;
565                 vgl->fmt.i_chroma = *list;
566                 vgl->tex_format   = GL_LUMINANCE;
567                 vgl->tex_internal = GL_LUMINANCE;
568                 vgl->tex_type     = GL_UNSIGNED_BYTE;
569                 yuv_range_correction = 1.0;
570                 break;
571 #if !USE_OPENGL_ES
572             } else if (dsc && dsc->plane_count == 3 && dsc->pixel_size == 2 &&
573                        IsLuminance16Supported(vgl->tex_target)) {
574                 need_fs_yuv       = true;
575                 vgl->fmt          = *fmt;
576                 vgl->fmt.i_chroma = *list;
577                 vgl->tex_format   = GL_LUMINANCE;
578                 vgl->tex_internal = GL_LUMINANCE16;
579                 vgl->tex_type     = GL_UNSIGNED_SHORT;
580                 yuv_range_correction = (float)((1 << 16) - 1) / ((1 << dsc->pixel_bits) - 1);
581                 break;
582 #endif
583             }
584             list++;
585         }
586     }
587
588     if (fmt->i_chroma == VLC_CODEC_XYZ12) {
589         need_fs_xyz       = true;
590         vgl->fmt          = *fmt;
591         vgl->fmt.i_chroma = VLC_CODEC_XYZ12;
592         vgl->tex_format   = GL_RGB;
593         vgl->tex_internal = GL_RGB;
594         vgl->tex_type     = GL_UNSIGNED_SHORT;
595     }
596     vgl->chroma = vlc_fourcc_GetChromaDescription(vgl->fmt.i_chroma);
597     assert(vgl->chroma != NULL);
598     vgl->use_multitexture = vgl->chroma->plane_count > 1;
599
600     /* Texture size */
601     for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
602         int w = vgl->fmt.i_visible_width  * vgl->chroma->p[j].w.num / vgl->chroma->p[j].w.den;
603         int h = vgl->fmt.i_visible_height * vgl->chroma->p[j].h.num / vgl->chroma->p[j].h.den;
604         if (vgl->supports_npot) {
605             vgl->tex_width[j]  = w;
606             vgl->tex_height[j] = h;
607         } else {
608             vgl->tex_width[j]  = GetAlignedSize(w);
609             vgl->tex_height[j] = GetAlignedSize(h);
610         }
611     }
612
613     /* Build program if needed */
614     vgl->program[0] =
615     vgl->program[1] = 0;
616     vgl->shader[0] =
617     vgl->shader[1] =
618     vgl->shader[2] = -1;
619     vgl->local_count = 0;
620     if (supports_shaders && (need_fs_yuv || need_fs_xyz|| need_fs_rgba)) {
621 #ifdef SUPPORTS_SHADERS
622         if (need_fs_xyz)
623             BuildXYZFragmentShader(vgl, &vgl->shader[0]);
624         else
625             BuildYUVFragmentShader(vgl, &vgl->shader[0], &vgl->local_count,
626                                 vgl->local_value, fmt, yuv_range_correction);
627
628         BuildRGBAFragmentShader(vgl, &vgl->shader[1]);
629         BuildVertexShader(vgl, &vgl->shader[2]);
630
631         /* Check shaders messages */
632         for (unsigned j = 0; j < 3; j++) {
633             int infoLength;
634             vgl->GetShaderiv(vgl->shader[j], GL_INFO_LOG_LENGTH, &infoLength);
635             if (infoLength <= 1)
636                 continue;
637
638             char *infolog = malloc(infoLength);
639             int charsWritten;
640             vgl->GetShaderInfoLog(vgl->shader[j], infoLength, &charsWritten, infolog);
641             fprintf(stderr, "shader %d: %s\n", j, infolog);
642             free(infolog);
643         }
644
645         vgl->program[0] = vgl->CreateProgram();
646         vgl->AttachShader(vgl->program[0], vgl->shader[0]);
647         vgl->AttachShader(vgl->program[0], vgl->shader[2]);
648         vgl->LinkProgram(vgl->program[0]);
649
650         vgl->program[1] = vgl->CreateProgram();
651         vgl->AttachShader(vgl->program[1], vgl->shader[1]);
652         vgl->AttachShader(vgl->program[1], vgl->shader[2]);
653         vgl->LinkProgram(vgl->program[1]);
654
655         /* Check program messages */
656         for (GLuint i = 0; i < 2; i++) {
657             int infoLength = 0;
658             vgl->GetProgramiv(vgl->program[i], GL_INFO_LOG_LENGTH, &infoLength);
659             if (infoLength <= 1)
660                 continue;
661             char *infolog = malloc(infoLength);
662             int charsWritten;
663             vgl->GetProgramInfoLog(vgl->program[i], infoLength, &charsWritten, infolog);
664             fprintf(stderr, "shader program %d: %s\n", i, infolog);
665             free(infolog);
666
667             /* If there is some message, better to check linking is ok */
668             GLint link_status = GL_TRUE;
669             vgl->GetProgramiv(vgl->program[i], GL_LINK_STATUS, &link_status);
670             if (link_status == GL_FALSE) {
671                 fprintf(stderr, "Unable to use program %d\n", i);
672                 free(vgl);
673                 return NULL;
674             }
675         }
676 #else
677         (void)yuv_range_correction;
678 #endif
679     }
680
681     /* */
682     glDisable(GL_BLEND);
683     glDisable(GL_DEPTH_TEST);
684     glDepthMask(GL_FALSE);
685     glDisable(GL_CULL_FACE);
686     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
687     glClear(GL_COLOR_BUFFER_BIT);
688
689 #ifdef SUPPORTS_SHADERS
690     vgl->GenBuffers(1, &vgl->vertex_buffer_object);
691     vgl->GenBuffers(vgl->chroma->plane_count, vgl->texture_buffer_object);
692
693     /* Initial number of allocated buffer objects for subpictures, will grow dynamically. */
694     int subpicture_buffer_object_count = 8;
695     vgl->subpicture_buffer_object = malloc(subpicture_buffer_object_count * sizeof(GLuint));
696     if (!vgl->subpicture_buffer_object) {
697         vlc_gl_Unlock(vgl->gl);
698         vout_display_opengl_Delete(vgl);
699         return NULL;
700     }
701     vgl->subpicture_buffer_object_count = subpicture_buffer_object_count;
702     vgl->GenBuffers(vgl->subpicture_buffer_object_count, vgl->subpicture_buffer_object);
703 #endif
704
705     vlc_gl_Unlock(vgl->gl);
706
707     /* */
708     for (int i = 0; i < VLCGL_TEXTURE_COUNT; i++) {
709         for (int j = 0; j < PICTURE_PLANE_MAX; j++)
710             vgl->texture[i][j] = 0;
711     }
712     vgl->region_count = 0;
713     vgl->region = NULL;
714     vgl->pool = NULL;
715
716     *fmt = vgl->fmt;
717     if (subpicture_chromas) {
718         *subpicture_chromas = gl_subpicture_chromas;
719     }
720     return vgl;
721 }
722
723 void vout_display_opengl_Delete(vout_display_opengl_t *vgl)
724 {
725     /* */
726     if (!vlc_gl_Lock(vgl->gl)) {
727         glFinish();
728         glFlush();
729         for (int i = 0; i < VLCGL_TEXTURE_COUNT; i++)
730             glDeleteTextures(vgl->chroma->plane_count, vgl->texture[i]);
731         for (int i = 0; i < vgl->region_count; i++) {
732             if (vgl->region[i].texture)
733                 glDeleteTextures(1, &vgl->region[i].texture);
734         }
735         free(vgl->region);
736
737 #ifdef SUPPORTS_SHADERS
738         if (vgl->program[0]) {
739             for (int i = 0; i < 2; i++)
740                 vgl->DeleteProgram(vgl->program[i]);
741             for (int i = 0; i < 3; i++)
742                 vgl->DeleteShader(vgl->shader[i]);
743         }
744         vgl->DeleteBuffers(1, &vgl->vertex_buffer_object);
745         vgl->DeleteBuffers(vgl->chroma->plane_count, vgl->texture_buffer_object);
746         if (vgl->subpicture_buffer_object_count > 0)
747             vgl->DeleteBuffers(vgl->subpicture_buffer_object_count, vgl->subpicture_buffer_object);
748         free(vgl->subpicture_buffer_object);
749 #endif
750
751         free(vgl->texture_temp_buf);
752         vlc_gl_Unlock(vgl->gl);
753     }
754     if (vgl->pool)
755         picture_pool_Release(vgl->pool);
756     free(vgl);
757 }
758
759 picture_pool_t *vout_display_opengl_GetPool(vout_display_opengl_t *vgl, unsigned requested_count)
760 {
761     if (vgl->pool)
762         return vgl->pool;
763
764     /* Allocate our pictures */
765     picture_t *picture[VLCGL_PICTURE_MAX] = {NULL, };
766     unsigned count;
767
768     for (count = 0; count < __MIN(VLCGL_PICTURE_MAX, requested_count); count++) {
769         picture[count] = picture_NewFromFormat(&vgl->fmt);
770         if (!picture[count])
771             break;
772     }
773     if (count <= 0)
774         return NULL;
775
776     /* Wrap the pictures into a pool */
777     vgl->pool = picture_pool_New(count, picture);
778     if (!vgl->pool)
779         goto error;
780
781     /* Allocates our textures */
782     if (vlc_gl_Lock(vgl->gl))
783         return vgl->pool;
784
785     for (int i = 0; i < VLCGL_TEXTURE_COUNT; i++) {
786         glGenTextures(vgl->chroma->plane_count, vgl->texture[i]);
787         for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
788             if (vgl->use_multitexture) {
789                 glActiveTexture(GL_TEXTURE0 + j);
790                 glClientActiveTexture(GL_TEXTURE0 + j);
791             }
792             glBindTexture(vgl->tex_target, vgl->texture[i][j]);
793
794 #if !USE_OPENGL_ES
795             /* Set the texture parameters */
796             glTexParameterf(vgl->tex_target, GL_TEXTURE_PRIORITY, 1.0);
797             glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
798 #endif
799
800             glTexParameteri(vgl->tex_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
801             glTexParameteri(vgl->tex_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
802             glTexParameteri(vgl->tex_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
803             glTexParameteri(vgl->tex_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
804
805             /* Call glTexImage2D only once, and use glTexSubImage2D later */
806             glTexImage2D(vgl->tex_target, 0,
807                          vgl->tex_internal, vgl->tex_width[j], vgl->tex_height[j],
808                          0, vgl->tex_format, vgl->tex_type, NULL);
809         }
810     }
811
812     vlc_gl_Unlock(vgl->gl);
813
814     return vgl->pool;
815
816 error:
817     for (unsigned i = 0; i < count; i++)
818         picture_Release(picture[i]);
819     return NULL;
820 }
821
822 #define ALIGN(x, y) (((x) + ((y) - 1)) & ~((y) - 1))
823 static void Upload(vout_display_opengl_t *vgl, int in_width, int in_height,
824                    int in_full_width, int in_full_height,
825                    int w_num, int w_den, int h_num, int h_den,
826                    int pitch, int pixel_pitch,
827                    int full_upload, const uint8_t *pixels,
828                    int tex_target, int tex_format, int tex_type)
829 {
830     int width       =       in_width * w_num / w_den;
831     int full_width  =  in_full_width * w_num / w_den;
832     int height      =      in_height * h_num / h_den;
833     int full_height = in_full_height * h_num / h_den;
834     // This unpack alignment is the default, but setting it just in case.
835     glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
836 #ifndef GL_UNPACK_ROW_LENGTH
837     int dst_width = full_upload ? full_width : width;
838     int dst_pitch = ALIGN(dst_width * pixel_pitch, 4);
839     if ( pitch != dst_pitch )
840     {
841         int buf_size = dst_pitch * full_height * pixel_pitch;
842         const uint8_t *source = pixels;
843         uint8_t *destination;
844         if( !vgl->texture_temp_buf || vgl->texture_temp_buf_size < buf_size )
845         {
846             free( vgl->texture_temp_buf );
847             vgl->texture_temp_buf = xmalloc( buf_size );
848             vgl->texture_temp_buf_size = buf_size;
849         }
850         destination = vgl->texture_temp_buf;
851
852         for( int h = 0; h < height ; h++ )
853         {
854             memcpy( destination, source, width * pixel_pitch );
855             source += pitch;
856             destination += dst_pitch;
857         }
858         if (full_upload)
859             glTexImage2D( tex_target, 0, tex_format,
860                           full_width, full_height,
861                           0, tex_format, tex_type, vgl->texture_temp_buf );
862         else
863             glTexSubImage2D( tex_target, 0,
864                              0, 0,
865                              width, height,
866                              tex_format, tex_type, vgl->texture_temp_buf );
867     } else {
868 #else
869     (void) width;
870     (void) height;
871     (void) vgl;
872     {
873         glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch / pixel_pitch);
874 #endif
875         if (full_upload)
876             glTexImage2D(tex_target, 0, tex_format,
877                          full_width, full_height,
878                          0, tex_format, tex_type, pixels);
879         else
880             glTexSubImage2D(tex_target, 0,
881                             0, 0,
882                             width, height,
883                             tex_format, tex_type, pixels);
884     }
885 }
886
887 int vout_display_opengl_Prepare(vout_display_opengl_t *vgl,
888                                 picture_t *picture, subpicture_t *subpicture)
889 {
890     if (vlc_gl_Lock(vgl->gl))
891         return VLC_EGENERIC;
892
893     /* Update the texture */
894     for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
895         if (vgl->use_multitexture) {
896             glActiveTexture(GL_TEXTURE0 + j);
897             glClientActiveTexture(GL_TEXTURE0 + j);
898         }
899         glBindTexture(vgl->tex_target, vgl->texture[0][j]);
900
901         Upload(vgl, picture->format.i_visible_width, vgl->fmt.i_visible_height,
902                vgl->fmt.i_width, vgl->fmt.i_height,
903                vgl->chroma->p[j].w.num, vgl->chroma->p[j].w.den, vgl->chroma->p[j].h.num, vgl->chroma->p[j].h.den,
904                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);
905     }
906
907     int         last_count = vgl->region_count;
908     gl_region_t *last = vgl->region;
909
910     vgl->region_count = 0;
911     vgl->region       = NULL;
912
913     if (subpicture) {
914
915         int count = 0;
916         for (subpicture_region_t *r = subpicture->p_region; r; r = r->p_next)
917             count++;
918
919         vgl->region_count = count;
920         vgl->region       = calloc(count, sizeof(*vgl->region));
921
922         if (vgl->use_multitexture) {
923             glActiveTexture(GL_TEXTURE0 + 0);
924             glClientActiveTexture(GL_TEXTURE0 + 0);
925         }
926         int i = 0;
927         for (subpicture_region_t *r = subpicture->p_region; r; r = r->p_next, i++) {
928             gl_region_t *glr = &vgl->region[i];
929
930             glr->format = GL_RGBA;
931             glr->type   = GL_UNSIGNED_BYTE;
932             glr->width  = r->fmt.i_visible_width;
933             glr->height = r->fmt.i_visible_height;
934             if (!vgl->supports_npot) {
935                 glr->width  = GetAlignedSize(glr->width);
936                 glr->height = GetAlignedSize(glr->height);
937                 glr->tex_width  = (float) r->fmt.i_visible_width  / glr->width;
938                 glr->tex_height = (float) r->fmt.i_visible_height / glr->height;
939             } else {
940                 glr->tex_width  = 1.0;
941                 glr->tex_height = 1.0;
942             }
943             glr->alpha  = (float)subpicture->i_alpha * r->i_alpha / 255 / 255;
944             glr->left   =  2.0 * (r->i_x                          ) / subpicture->i_original_picture_width  - 1.0;
945             glr->top    = -2.0 * (r->i_y                          ) / subpicture->i_original_picture_height + 1.0;
946             glr->right  =  2.0 * (r->i_x + r->fmt.i_visible_width ) / subpicture->i_original_picture_width  - 1.0;
947             glr->bottom = -2.0 * (r->i_y + r->fmt.i_visible_height) / subpicture->i_original_picture_height + 1.0;
948
949             glr->texture = 0;
950             /* Try to recycle the textures allocated by the previous
951                call to this function. */
952             for (int j = 0; j < last_count; j++) {
953                 if (last[j].texture &&
954                     last[j].width  == glr->width &&
955                     last[j].height == glr->height &&
956                     last[j].format == glr->format &&
957                     last[j].type   == glr->type) {
958                     glr->texture = last[j].texture;
959                     memset(&last[j], 0, sizeof(last[j]));
960                     break;
961                 }
962             }
963
964             const int pixels_offset = r->fmt.i_y_offset * r->p_picture->p->i_pitch +
965                                       r->fmt.i_x_offset * r->p_picture->p->i_pixel_pitch;
966             if (glr->texture) {
967                 /* A texture was successfully recycled, reuse it. */
968                 glBindTexture(GL_TEXTURE_2D, glr->texture);
969                 Upload(vgl, r->fmt.i_visible_width, r->fmt.i_visible_height, glr->width, glr->height, 1, 1, 1, 1,
970                        r->p_picture->p->i_pitch, r->p_picture->p->i_pixel_pitch, 0,
971                        &r->p_picture->p->p_pixels[pixels_offset], GL_TEXTURE_2D, glr->format, glr->type);
972             } else {
973                 /* Could not recycle a previous texture, generate a new one. */
974                 glGenTextures(1, &glr->texture);
975                 glBindTexture(GL_TEXTURE_2D, glr->texture);
976 #if !USE_OPENGL_ES
977                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, 1.0);
978                 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
979 #endif
980                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
981                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
982                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
983                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
984                 Upload(vgl, r->fmt.i_visible_width, r->fmt.i_visible_height, glr->width, glr->height, 1, 1, 1, 1,
985                        r->p_picture->p->i_pitch, r->p_picture->p->i_pixel_pitch, 1,
986                        &r->p_picture->p->p_pixels[pixels_offset], GL_TEXTURE_2D, glr->format, glr->type);
987             }
988         }
989     }
990     for (int i = 0; i < last_count; i++) {
991         if (last[i].texture)
992             glDeleteTextures(1, &last[i].texture);
993     }
994     free(last);
995
996     vlc_gl_Unlock(vgl->gl);
997     VLC_UNUSED(subpicture);
998     return VLC_SUCCESS;
999 }
1000
1001 static const GLfloat identity[] = {
1002     1.0f, 0.0f, 0.0f, 0.0f,
1003     0.0f, 1.0f, 0.0f, 0.0f,
1004     0.0f, 0.0f, 1.0f, 0.0f,
1005     0.0f, 0.0f, 0.0f, 1.0f
1006 };
1007
1008 static void orientationTransformMatrix(GLfloat matrix[static 16], video_orientation_t orientation) {
1009
1010     memcpy(matrix, identity, sizeof(identity));
1011
1012     const int k_cos_pi = -1;
1013     const int k_cos_pi_2 = 0;
1014     const int k_cos_n_pi_2 = 0;
1015
1016     const int k_sin_pi = 0;
1017     const int k_sin_pi_2 = 1;
1018     const int k_sin_n_pi_2 = -1;
1019
1020     bool rotate = false;
1021     int cos = 0, sin = 0;
1022
1023     switch (orientation) {
1024
1025         case ORIENT_ROTATED_90:
1026             cos = k_cos_pi_2;
1027             sin = k_sin_pi_2;
1028             rotate = true;
1029             break;
1030         case ORIENT_ROTATED_180:
1031             cos = k_cos_pi;
1032             sin = k_sin_pi;
1033             rotate = true;
1034             break;
1035         case ORIENT_ROTATED_270:
1036             cos = k_cos_n_pi_2;
1037             sin = k_sin_n_pi_2;
1038             rotate = true;
1039             break;
1040         case ORIENT_HFLIPPED:
1041             matrix[0 * 4 + 0] = -1;
1042             break;
1043         case ORIENT_VFLIPPED:
1044             matrix[1 * 4 + 1] = -1;
1045             break;
1046         case ORIENT_TRANSPOSED:
1047             matrix[0 * 4 + 0] = 0;
1048             matrix[0 * 4 + 1] = -1;
1049             matrix[1 * 4 + 0] = -1;
1050             matrix[1 * 4 + 1] = 0;
1051             break;
1052         case ORIENT_ANTI_TRANSPOSED:
1053             matrix[0 * 4 + 0] = 0;
1054             matrix[0 * 4 + 1] = 1;
1055             matrix[1 * 4 + 0] = 1;
1056             matrix[1 * 4 + 1] = 0;
1057             break;
1058         default:
1059             break;
1060     }
1061
1062     if (rotate) {
1063
1064         matrix[0 * 4 + 0] = cos;
1065         matrix[0 * 4 + 1] = -sin;
1066         matrix[1 * 4 + 0] = sin;
1067         matrix[1 * 4 + 1] = cos;
1068     }
1069 }
1070
1071 #ifdef SUPPORTS_FIXED_PIPELINE
1072 static void DrawWithoutShaders(vout_display_opengl_t *vgl,
1073                                float *left, float *top, float *right, float *bottom)
1074 {
1075     static const GLfloat vertexCoord[] = {
1076         -1.0f, -1.0f,
1077          1.0f, -1.0f,
1078         -1.0f,  1.0f,
1079          1.0f,  1.0f,
1080     };
1081
1082     const GLfloat textureCoord[] = {
1083         left[0],  bottom[0],
1084         right[0], bottom[0],
1085         left[0],  top[0],
1086         right[0], top[0]
1087     };
1088
1089     GLfloat transformMatrix[16];
1090     orientationTransformMatrix(transformMatrix, vgl->fmt.orientation);
1091
1092     glPushMatrix();
1093     glMatrixMode(GL_MODELVIEW);
1094     glLoadMatrixf(transformMatrix);
1095
1096     glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
1097     glEnable(vgl->tex_target);
1098     glActiveTexture(GL_TEXTURE0 + 0);
1099     glClientActiveTexture(GL_TEXTURE0 + 0);
1100
1101     glBindTexture(vgl->tex_target, vgl->texture[0][0]);
1102
1103     glEnableClientState(GL_VERTEX_ARRAY);
1104     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1105
1106     glTexCoordPointer(2, GL_FLOAT, 0, textureCoord);
1107     glVertexPointer(2, GL_FLOAT, 0, vertexCoord);
1108
1109     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1110
1111     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1112     glDisableClientState(GL_VERTEX_ARRAY);
1113     glDisable(vgl->tex_target);
1114
1115     glPopMatrix();
1116
1117 }
1118 #endif
1119
1120 #ifdef SUPPORTS_SHADERS
1121 static void DrawWithShaders(vout_display_opengl_t *vgl,
1122                             float *left, float *top, float *right, float *bottom,
1123                             int program)
1124 {
1125     vgl->UseProgram(vgl->program[program]);
1126     if (program == 0) {
1127         if (vgl->chroma->plane_count == 3) {
1128             vgl->Uniform4fv(vgl->GetUniformLocation(vgl->program[0], "Coefficient"), 4, vgl->local_value);
1129             vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture0"), 0);
1130             vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture1"), 1);
1131             vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture2"), 2);
1132         }
1133         else if (vgl->chroma->plane_count == 1) {
1134             vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture0"), 0);
1135         }
1136     } else {
1137         vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[1], "Texture0"), 0);
1138         vgl->Uniform4f(vgl->GetUniformLocation(vgl->program[1], "FillColor"), 1.0f, 1.0f, 1.0f, 1.0f);
1139     }
1140
1141     static const GLfloat vertexCoord[] = {
1142         -1.0,  1.0,
1143         -1.0, -1.0,
1144          1.0,  1.0,
1145          1.0, -1.0,
1146     };
1147
1148     GLfloat transformMatrix[16];
1149     orientationTransformMatrix(transformMatrix, vgl->fmt.orientation);
1150
1151     for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
1152         const GLfloat textureCoord[] = {
1153             left[j],  top[j],
1154             left[j],  bottom[j],
1155             right[j], top[j],
1156             right[j], bottom[j],
1157         };
1158         glActiveTexture(GL_TEXTURE0+j);
1159         glClientActiveTexture(GL_TEXTURE0+j);
1160         glBindTexture(vgl->tex_target, vgl->texture[0][j]);
1161
1162         vgl->BindBuffer(GL_ARRAY_BUFFER, vgl->texture_buffer_object[j]);
1163         vgl->BufferData(GL_ARRAY_BUFFER, sizeof(textureCoord), textureCoord, GL_STATIC_DRAW);
1164
1165         char attribute[20];
1166         snprintf(attribute, sizeof(attribute), "MultiTexCoord%1d", j);
1167         vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[program], attribute));
1168         vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[program], attribute), 2, GL_FLOAT, 0, 0, 0);
1169     }
1170     glActiveTexture(GL_TEXTURE0 + 0);
1171     glClientActiveTexture(GL_TEXTURE0 + 0);
1172
1173     vgl->BindBuffer(GL_ARRAY_BUFFER, vgl->vertex_buffer_object);
1174     vgl->BufferData(GL_ARRAY_BUFFER, sizeof(vertexCoord), vertexCoord, GL_STATIC_DRAW);
1175     vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[program], "VertexPosition"));
1176     vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[program], "VertexPosition"), 2, GL_FLOAT, 0, 0, 0);
1177
1178     vgl->UniformMatrix4fv(vgl->GetUniformLocation(vgl->program[program], "RotationMatrix"), 1, GL_FALSE, transformMatrix);
1179
1180     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1181 }
1182 #endif
1183
1184 int vout_display_opengl_Display(vout_display_opengl_t *vgl,
1185                                 const video_format_t *source)
1186 {
1187     if (vlc_gl_Lock(vgl->gl))
1188         return VLC_EGENERIC;
1189
1190     /* Why drawing here and not in Render()? Because this way, the
1191        OpenGL providers can call vout_display_opengl_Display to force redraw.i
1192        Currently, the OS X provider uses it to get a smooth window resizing */
1193     glClear(GL_COLOR_BUFFER_BIT);
1194
1195     /* Draw the picture */
1196     float left[PICTURE_PLANE_MAX];
1197     float top[PICTURE_PLANE_MAX];
1198     float right[PICTURE_PLANE_MAX];
1199     float bottom[PICTURE_PLANE_MAX];
1200     for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
1201         /* glTexCoord works differently with GL_TEXTURE_2D and
1202            GL_TEXTURE_RECTANGLE_EXT */
1203         float scale_w, scale_h;
1204
1205         if (vgl->tex_target == GL_TEXTURE_2D) {
1206             scale_w = (float)vgl->chroma->p[j].w.num / vgl->chroma->p[j].w.den / vgl->tex_width[j];
1207             scale_h = (float)vgl->chroma->p[j].h.num / vgl->chroma->p[j].h.den / vgl->tex_height[j];
1208
1209         } else {
1210             scale_w = 1.0;
1211             scale_h = 1.0;
1212         }
1213         /* Warning: if NPOT is not supported a larger texture is
1214            allocated. This will cause right and bottom coordinates to
1215            land on the edge of two texels with the texels to the
1216            right/bottom uninitialized by the call to
1217            glTexSubImage2D. This might cause a green line to appear on
1218            the right/bottom of the display.
1219            There are two possible solutions:
1220            - Manually mirror the edges of the texture.
1221            - Add a "-1" when computing right and bottom, however the
1222            last row/column might not be displayed at all.
1223         */
1224         left[j]   = (source->i_x_offset +                       0 ) * scale_w;
1225         top[j]    = (source->i_y_offset +                       0 ) * scale_h;
1226         right[j]  = (source->i_x_offset + source->i_visible_width ) * scale_w;
1227         bottom[j] = (source->i_y_offset + source->i_visible_height) * scale_h;
1228     }
1229
1230 #ifdef SUPPORTS_SHADERS
1231     if (vgl->program[0] && (vgl->chroma->plane_count == 3 || vgl->chroma->plane_count == 1))
1232         DrawWithShaders(vgl, left, top, right, bottom, 0);
1233     else if (vgl->program[1] && vgl->chroma->plane_count == 1)
1234         DrawWithShaders(vgl, left, top, right, bottom, 1);
1235     else
1236 #endif
1237     {
1238 #ifdef SUPPORTS_FIXED_PIPELINE
1239         DrawWithoutShaders(vgl, left, top, right, bottom);
1240 #endif
1241     }
1242
1243     /* Draw the subpictures */
1244     if (vgl->program[1]) {
1245 #ifdef SUPPORTS_SHADERS
1246         // Change the program for overlays
1247         vgl->UseProgram(vgl->program[1]);
1248         vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[1], "Texture"), 0);
1249 #endif
1250     }
1251
1252 #ifdef SUPPORTS_FIXED_PIPELINE
1253     glEnable(GL_TEXTURE_2D);
1254 #endif
1255     glEnable(GL_BLEND);
1256     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1257
1258 #ifdef SUPPORTS_SHADERS
1259     /* We need two buffer objects for each region: for vertex and texture coordinates. */
1260     if (2 * vgl->region_count > vgl->subpicture_buffer_object_count) {
1261         if (vgl->subpicture_buffer_object_count > 0)
1262             vgl->DeleteBuffers(vgl->subpicture_buffer_object_count, vgl->subpicture_buffer_object);
1263         vgl->subpicture_buffer_object_count = 0;
1264
1265         int new_count = 2 * vgl->region_count;
1266         vgl->subpicture_buffer_object = realloc_or_free(vgl->subpicture_buffer_object, new_count * sizeof(GLuint));
1267         if (!vgl->subpicture_buffer_object) {
1268             vlc_gl_Unlock(vgl->gl);
1269             return VLC_ENOMEM;
1270         }
1271
1272         vgl->subpicture_buffer_object_count = new_count;
1273         vgl->GenBuffers(vgl->subpicture_buffer_object_count, vgl->subpicture_buffer_object);
1274     }
1275 #endif
1276
1277     glActiveTexture(GL_TEXTURE0 + 0);
1278     glClientActiveTexture(GL_TEXTURE0 + 0);
1279     for (int i = 0; i < vgl->region_count; i++) {
1280         gl_region_t *glr = &vgl->region[i];
1281         const GLfloat vertexCoord[] = {
1282             glr->left,  glr->top,
1283             glr->left,  glr->bottom,
1284             glr->right, glr->top,
1285             glr->right, glr->bottom,
1286         };
1287         const GLfloat textureCoord[] = {
1288             0.0, 0.0,
1289             0.0, glr->tex_height,
1290             glr->tex_width, 0.0,
1291             glr->tex_width, glr->tex_height,
1292         };
1293
1294         glBindTexture(GL_TEXTURE_2D, glr->texture);
1295         if (vgl->program[1]) {
1296 #ifdef SUPPORTS_SHADERS
1297             vgl->Uniform4f(vgl->GetUniformLocation(vgl->program[1], "FillColor"), 1.0f, 1.0f, 1.0f, glr->alpha);
1298
1299             vgl->BindBuffer(GL_ARRAY_BUFFER, vgl->subpicture_buffer_object[2 * i]);
1300             vgl->BufferData(GL_ARRAY_BUFFER, sizeof(textureCoord), textureCoord, GL_STATIC_DRAW);
1301             vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[1], "MultiTexCoord0"));
1302             vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[1], "MultiTexCoord0"), 2, GL_FLOAT, 0, 0, 0);
1303
1304             vgl->BindBuffer(GL_ARRAY_BUFFER, vgl->subpicture_buffer_object[2 * i + 1]);
1305             vgl->BufferData(GL_ARRAY_BUFFER, sizeof(vertexCoord), vertexCoord, GL_STATIC_DRAW);
1306             vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[1], "VertexPosition"));
1307             vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[1], "VertexPosition"), 2, GL_FLOAT, 0, 0, 0);
1308
1309             // Subpictures have the correct orientation:
1310             vgl->UniformMatrix4fv(vgl->GetUniformLocation(vgl->program[1], "RotationMatrix"), 1, GL_FALSE, identity);
1311 #endif
1312         } else {
1313 #ifdef SUPPORTS_FIXED_PIPELINE
1314             glEnableClientState(GL_VERTEX_ARRAY);
1315             glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1316             glColor4f(1.0f, 1.0f, 1.0f, glr->alpha);
1317             glTexCoordPointer(2, GL_FLOAT, 0, textureCoord);
1318             glVertexPointer(2, GL_FLOAT, 0, vertexCoord);
1319 #endif
1320         }
1321
1322         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1323
1324         if (!vgl->program[1]) {
1325 #ifdef SUPPORTS_FIXED_PIPELINE
1326             glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1327             glDisableClientState(GL_VERTEX_ARRAY);
1328 #endif
1329         }
1330     }
1331     glDisable(GL_BLEND);
1332 #ifdef SUPPORTS_FIXED_PIPELINE
1333     glDisable(GL_TEXTURE_2D);
1334 #endif
1335
1336     /* Display */
1337     vlc_gl_Swap(vgl->gl);
1338
1339     vlc_gl_Unlock(vgl->gl);
1340     return VLC_SUCCESS;
1341 }
1342